//<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.
//
////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////
/// file    Mvideo.c
/// @brief  Main API in XC library
/// @author MStar Semiconductor Inc.
///////////////////////////////////////////////////////////////////////////////////////////////////
#define  MVIDEO_C

//-------------------------------------------------------------------------------------------------
//  Include Files
//-------------------------------------------------------------------------------------------------
// Common Definition
#ifdef MSOS_TYPE_LINUX_KERNEL
#include <linux/string.h>
#include <linux/module.h>   /* Needed by all modules */
#include <linux/kernel.h>   /* Needed for KERN_INFO */
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/compat.h>
#else
#include <string.h>
#include <stdlib.h>
#endif
//#include <assert.h>

#include "UFO.h"

#include "MsCommon.h"
#include "MsVersion.h"
#include "MsOS.h"
#include "mhal_xc_chip_config.h"
#include "utopia.h"
#include "utopia_dapi.h"

// Internal Definition
#include "drvXC_IOPort.h"
#include "apiXC.h"
#include "apiXC_DWIN.h"
#include "XC_DWIN_private.h"
#include "apiXC_Adc.h"
#include "apiXC_Auto.h"
#include "drv_sc_display.h"
#include "drv_sc_isr.h"
#include "apiXC_PCMonitor.h"
#include "apiXC_ModeParse.h"
#include "drvXC_HDMI_if.h"
#include "mvideo_context.h"
#include "drv_sc_ip.h"
#if (LD_ENABLE==1)
#include "mdrv_ld.h"
#include "mdrv_ldalgo.h"
#endif
#include "mdrv_sc_3d.h"
#include "drv_sc_menuload.h"
#include "drvXC_ADC_Internal.h"
#include "mhal_adc.h"
#include "mdrv_frc.h"
#include "mhal_frc.h"
#include "mhal_sc.h"
#include "mhal_dynamicscaling.h"
#include "XC_private.h"
#include "apiXC_v2.h"
#include "drvXC_HDMI_Internal.h"
#include "drvXC_HDMI_if_v2.h"
#include "mhal_adc.h"

#include "xc_Analog_Reg.h"
#include "hwreg_ipmux.h"

#include "drv_sc_scaling.h"
#include "drv_sc_mux.h"
#include "drv_sc_DIP_scaling.h"
#include "mdrv_sc_dynamicscaling.h"
#include "drvscaler_nr.h"
#include "drvMMIO.h"
#include "mhal_pip.h"

#include "drvPQ.h"
#include "apiXC_private.h"
//Add for A5
#include "mhal_dip.h"

// Tobe refined
#include "xc_hwreg_utility2.h"
#include "mhal_mux.h"
#include "mhal_hdmi.h"
#include "mhal_ip.h"
#include "mhal_sc.h"
#include "mhal_menuload.h"

#ifndef DONT_USE_CMA
#ifndef MSOS_TYPE_NUTTX
#if (XC_SUPPORT_CMA ==TRUE)
#ifdef MSOS_TYPE_LINUX_KERNEL
#include "drvCMAPool_v2.h"
#else
#include "drvCMAPool.h"
#endif
#include "mdrv_cma_pool_st.h"
#include "halCHIP.h"
#endif
#endif
#endif
#include "drvSYS.h"

//mvop for xc all mvop API
#include "drvMVOP.h"
#include "tee_client_api.h"
#include "drvMIU.h"

#ifdef STELLAR
#include "apiXC_cus.h"
#include "mhal_s_disp_ctl.h"
#endif

#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"
#include "apiMBX_St.h"
#include "apiMBX.h"
#if (XC_SUPPORT_IOMMU == TRUE)
#include "drvMMA.h"
#endif

/*
    XC_INITDATA_VERSION                      current version : 1    //1: Add an field eScartIDPort_Sel for port selection
*/


//PQ_Function_Info    s_PQ_Function_Info = {0, 0, 0, 0, 0, 0, 0, 0, 0};
PQ_Function_Info    s_PQ_Function_Info[2];//MAX_XC_DEVICE_NUM // = {{0, 0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0, 0}};
ST_XC_QOS_CALLBACK_INFO g_stXCQosCallBackInfo;


MS_U8 MApi_Pnl_Get_LPLL_Type(void);
MS_BOOL MApi_PNL_Get_TCON_Capability(void);
MS_BOOL _MDrv_XC_TEE_Send_Cmd(void *pInstance,EN_XC_OPTEE_ACTION action,void *para,MS_U32 u32size);
//void MApi_PNL_CalExtLPLLSETbyDClk(MS_U32 ldHz);
#ifdef PATCH_TCON_BRING_UP
const char* MDrv_PNL_GetName(void);
#endif
#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) && defined(CONFIG_MSTAR_XC_SET_PQBINPATH))
extern void KApi_XC_SetPQBinPath_Ext(EN_KDRV_XC_PQ_BIN_PATH eXCPqBinPath,MS_U8 u8size,char* b_XCPQBinFilePath);
#endif
extern MS_BOOL MDrv_XC_DetectFieldOrder(ST_KDRV_XC_FOD_INFO* pstFODInfo);
extern MS_BOOL _MApi_XC_DIP_ProcessMode(SCALER_DIP_WIN eWindow, EN_DIP_MODE enMode,void *pModeSettings);
extern MS_BOOL _MApi_XC_DIP_SetBuffer(ST_DIP_Buffer *pstBuf,SCALER_DIP_WIN eWindow);
extern MS_BOOL _MApi_XC_DIP_SetXCPathInfo(SCALER_DIP_WIN eWindow,ST_DIP_XCPathInfo *pstXCInfo);

//-------------------------------------------------------------------------------------------------
//  Driver Compiler Options
//-------------------------------------------------------------------------------------------------
#ifdef UFO_XC_HDR
#if (UFO_XC_HDR_VERSION == 2)
#ifdef MSOS_TYPE_LINUX
static MS_S32   _s32FdScaler = -1;
#endif
#endif
#endif

//-------------------------------------------------------------------------------------------------
//  Local Defines
//-------------------------------------------------------------------------------------------------

#define VERIFY_MVIDEO_FPGA  0

#ifndef XC_HLINEARSCALING_VER
#define XC_HLINEARSCALING_VER 0
#endif

#ifndef ENABLE_DI_8LB_MODE
#define ENABLE_DI_8LB_MODE 0
#endif

#ifndef ENABLE_CAL_XCMEM_FROM_MADIVR
#define ENABLE_CAL_XCMEM_FROM_MADIVR 0
#endif

#ifdef MSOS_TYPE_LINUX_KERNEL
#define DELAY_FRAME_NUM 5
#define THREADWAITTIME 1000
#if (XC_STR_DTV_ENABLE_THREAD ==1)
static MS_BOOL bResumeThreadStart[E_XC_DEVICE_MAX] = {FALSE,FALSE};
static MS_BOOL bResumeThreadDone[E_XC_DEVICE_MAX] = {FALSE,FALSE};
#endif
#endif

#ifdef MSOS_TYPE_OPTEE
#define MAX_OPTEE_ERROR_CNT 3
#endif

#define XC_STR_PCMONITOR_MAX_TETRY_TIME 40
#define IsSrcMVOP(x) (IsSrcTypeStorage(x) || IsSrcTypeDTV(x))

#define PanelVTT (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VTotal) //  //panel VTT-----> 2260
#define NoDelayLockPointDelta (PanelVTT-pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height)//(45)  //blank 2260-2160 = 100

#define WINDOWST (gSrcInfo[MAIN_WINDOW])
#define NoDelayLockPointAfa 20

#define HPixelShiftRangeMax 32
#define VPixelShiftJustscanRangeMax 16
#define VPixelShiftOverscanRangeMax 10

static MS_U32 _u32SVPVersion = 3;  //If DMS set video pipeline, the SVP version will be 4 and trigger SVP 4.0 flow

//#define SUPPORT_HDMI20_420
//-------------------------------------------------------------------------------------------------
//  Local Structurs
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
//  Global Variables
//-------------------------------------------------------------------------------------------------
extern MS_U8 gu8PatternIP;
extern MS_BOOL gbStrPatternEnable;
//-------------------------------------------------------------------------------------------------
//  Local Variables
//-------------------------------------------------------------------------------------------------
static MSIF_Version _api_xc_version = {
    .DDI = { XC_API_VERSION },
};

#ifdef CONFIG_UTOPIA_PROC_DBG_SUPPORT
    MS_U32 u32SetWindowTime;
#endif

#if ((XC_SUPPORT_IOMMU == TRUE) && (XC_MMA_ALLOC_USE_THREAD == TRUE))
//for IOMMU MMA
#define TICK_PER_ONE_MS     (1) //Note: confirm Kernel fisrt
#define MSOS_WAIT_FOREVER   (0xffffff00/TICK_PER_ONE_MS)
#define XC_STACK_SIZE 8192


typedef struct DLL_PACKED
{
    MS_S32  s32TaskId;
    MS_S32  s32EventId;
    //MS_S8   s8TaskName[32];
    //MS_S8   s8EventName[32];
    MS_U8   u8TaskRefCnt;
    MS_U8   u8EventRefCnt;
    //MS_U8   u8TaskStack[VDEC_EX_STACK_SIZE];
}ST_XC_MMA_ALLOC_TASK;

static ST_XC_MMA_ALLOC_TASK _gstMMATask[MAX_WINDOW] =
{
    {-1, -1},{-1,-1},
};

typedef struct DLL_PACKED
{
    SCALER_WIN enWindow;
    void *pInstance;
#if !defined (__aarch64__)
    //dummy
    MS_U32 u32Dummy;
#endif
}ST_XC_MMA_ALLLOC_INFO;

ST_XC_MMA_ALLLOC_INFO _gstMMAInfo[MAX_WINDOW] = {{MAX_WINDOW,NULL},{MAX_WINDOW,NULL}};
#endif

//-------------------------------------------------------------------------------------------------
//  Extern Variables
//-------------------------------------------------------------------------------------------------
#ifndef CONFIG_MBOOT //Add For GPL (content protection)
extern MS_U8 MDrv_AUTH_IPCheck(MS_U8 u8Bit);
#else
#define MDrv_AUTH_IPCheck(args...)  0
#endif //End of GPL content protection

extern void MApi_XC_ACE_GetColorMatrix_U2(void *pInstance, MS_BOOL eWindow, MS_U16* pu16Matrix);
extern void MApi_XC_ACE_SetColorMatrix_U2( void *pInstance, MS_BOOL eWindow, MS_U16* pu16Matrix);

//-------------------------------------------------------------------------------------------------
//  Debug Functions
//-------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------
//  Local Functions
//-------------------------------------------------------------------------------------------------
MS_BOOL _MDrv_XC_MBX_Init(void* pInstance);
MS_BOOL _MDrv_XC_REE_SendMBX(void* pInstance,EN_XC_OPTEE_ACTION action,void *pPara);
MS_BOOL _MDrv_XC_REE_MBX_CheckSendMSG(MBX_Msg *pMsg);
MS_BOOL _MDrv_XC_REE_MBX_SendMSG(void* pInstance,MBX_Msg *pMsg,void *pPara);
MS_BOOL _MDrv_XC_SVP_MirrorModeDetection(void* pInstance,SCALER_WIN eWindow);

XC_OPTEE_HANDLER st_xc_handler;

#if ((XC_SUPPORT_IOMMU == TRUE) && (XC_MMA_ALLOC_USE_THREAD == TRUE))

MS_BOOL Mdrv_XC_Task_WaitEventDone(void *pInstance,E_XC_MMA_EVENT enEvent, E_XC_MMA_EVENT enEventAck, MS_U32 u32WaitMs,SCALER_WIN enWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    ST_XC_MMA_ALLOC_TASK *pstTask = NULL;
    MS_BOOL bRet = FALSE;
    MS_U32 u32TimeDiff = 0;

#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 1)
    SCALER_WIN enWindowID = MAX_WINDOW;

    if(psXCInstPri->u32DeviceID == 0)
    {
        enWindowID = MAIN_WINDOW;
    }
    else if(psXCInstPri->u32DeviceID == 1)
    {
        enWindowID = SUB_WINDOW
    }
    else
    {
        printf("Error not support this window\n");
    }

    enWindow = MAIN_WINDOW;
    pstTask = &(_gstMMATask[enWindowID]);
#else
    pstTask = &(_gstMMATask[enWindow]);
#endif

    if (pstTask->s32EventId < 0)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s, %d] EventId=0x%x invaild\n",__FUNCTION__, __LINE__,(unsigned int)pstTask->s32EventId);

        return FALSE;
    }

    u32TimeDiff = MsOS_GetSystemTime();
    bRet = MsOS_SetEvent(pstTask->s32EventId, enEvent);
    u32TimeDiff = MsOS_GetSystemTime() - u32TimeDiff;

    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s, %d] MsOS_SetEvent %s, time=%dms\n",__FUNCTION__, __LINE__,bRet ? "success" : "fail",(unsigned int)u32TimeDiff);

    if ((enEventAck != E_XC_MMA_EVENT_OFF) && bRet)
    {
        MS_U32 u32EventFlag = 0;

        u32TimeDiff = MsOS_GetSystemTime();
        bRet = MsOS_WaitEvent(pstTask->s32EventId,
        enEventAck,
        &u32EventFlag,
        E_OR_CLEAR,
        u32WaitMs);
        u32TimeDiff = MsOS_GetSystemTime() - u32TimeDiff;

        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s, %d] MsOS_WaitEvent %s, time=%dms\n",__FUNCTION__, __LINE__,bRet ? "success" : "fail",(unsigned int)u32TimeDiff);
    }

    return bRet;
}

void _Mdrv_XC_MMAControlMonitor(ST_XC_MMA_ALLLOC_INFO *stMMAInfo)
{
    void *pInstance = stMMAInfo->pInstance;
    SCALER_WIN enWindow = stMMAInfo->enWindow;

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    ST_XC_MMA_ALLOC_TASK *pstTask = NULL;
    MS_BOOL bRun = TRUE;

    if (pInstance == NULL)
    {
        printf("[%s, %d] fail, pInstance == NULL",__FUNCTION__, __LINE__);
        return;
    }

#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 1)
    SCALER_WIN enWindowID = MAX_WINDOW;

    if(psXCInstPri->u32DeviceID == 0)
    {
        enWindowID = MAIN_WINDOW;
    }
    else if(psXCInstPri->u32DeviceID == 1)
    {
        enWindowID = SUB_WINDOW
    }
    else
    {
        printf("Error not support this window\n");
    }

    enWindow = MAIN_WINDOW;
    pstTask = &(_gstMMATask[enWindowID]);
#else
    pstTask = &(_gstMMATask[enWindow]);
#endif

    while (bRun)
    {
        MS_U32 u32TimeDiff = 0;
        MS_U32 u32EventFlag = 0;
        MsOS_WaitEvent(pstTask->s32EventId,
            (
            E_XC_MMA_EVENT_TASK_DELETE |
            E_XC_MMA_EVENT_XC_GET |
            E_XC_MMA_EVENT_XC_PUT |
            E_XC_MMA_EVENT_FRC_L_GET |
            E_XC_MMA_EVENT_FRC_R_GET |
            E_XC_MMA_EVENT_FRC_PQ_GET |
            E_XC_MMA_EVENT_FRC_PUT |
            E_XC_MMA_EVENT_HDMI_DMA_GET |
            E_XC_MMA_EVENT_HDMI_DMA_PUT |
            E_XC_MMA_EVENT_HSE_GET |
            E_XC_MMA_EVENT_HSE_PUT
            ),
            &u32EventFlag,
            E_OR_CLEAR,
            MSOS_WAIT_FOREVER);
        //XC get and release MMA
        if (u32EventFlag & E_XC_MMA_EVENT_XC_GET)
        {
            MS_PHY u64MMAAddr = 0;
            MS_U32 u32GetMMASize = 0;
            MS_BOOL bSecure = pXCResourcePrivate->stdrvXC_MVideo.abSecure[enWindow];//secure buffer
            u32GetMMASize = MHal_XC_GetMMAUsingBufferSize(pInstance,enWindow);

            u32TimeDiff = MsOS_GetSystemTime();
            u64MMAAddr = MHal_XC_GetMMAAddr(pInstance,u32GetMMASize,bSecure,enWindow);
            u32TimeDiff = MsOS_GetSystemTime() - u32TimeDiff;
            if(u64MMAAddr != XC_INVALID_MEM)
            {
                pXCResourcePrivate->stdrvXC_MVideo.au32XCSCMIMemSize[enWindow] = u32GetMMASize;
                pXCResourcePrivate->stdrvXC_MVideo.au32PreMMAXCSize[enWindow] = u32GetMMASize;
                pXCResourcePrivate->stdrvXC_MVideo.aphyXCMMABaseAddress[enWindow] = u64MMAAddr;
                printf("[%s,%5d][MMA], u32MMAAddr = 0x%llx finish, time = %d\n",__func__, __LINE__,u64MMAAddr,u32TimeDiff);
            }
            else
            {
                printf("[%s][%d] XC alloc mma fail\n",__FUNCTION__,__LINE__);
            }
            MsOS_SetEvent(pstTask->s32EventId, E_XC_MMA_EVENT_XC_GET_DONE);
        }

        if (u32EventFlag & E_XC_MMA_EVENT_XC_PUT)
        {
            MS_PHY phyBaseAddress = pXCResourcePrivate->stdrvXC_MVideo.aphyXCMMABaseAddress[enWindow];
            u32TimeDiff = MsOS_GetSystemTime();
            if(phyBaseAddress != XC_INVALID_MEM)
            {
                MHal_XC_ReleaseMMA(pInstance,enWindow);
            }
            u32TimeDiff = MsOS_GetSystemTime() - u32TimeDiff;

            printf("[%s,%5d]release MMA finish, time = %d\n",__func__, __LINE__,u32TimeDiff);

            MsOS_SetEvent(pstTask->s32EventId, E_XC_MMA_EVENT_XC_PUT_DONE);
        }

        //FRC L get MMA
        if (u32EventFlag & E_XC_MMA_EVENT_FRC_L_GET)
        {
            MS_PHY phyFRCLeftBaseaddress = 0;

            MS_U32 u32FRCMmaSize = 0;
            if(!(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_DUAL_MIU))
            {
                u32FRCMmaSize = MHal_FRC_GetFrcLMemorySize(pInstance) + MHal_FRC_GetFrcRMemorySize(pInstance);
            }
            else
            {
                u32FRCMmaSize = MHal_FRC_GetFrcLMemorySize(pInstance);
            }
            MS_U8 au8FRCLeftBufTag[XC_TAG_NAME_LEN_MAX] = FRC_LEFT_TAG_NAME;
            MS_BOOL bSecure = pXCResourcePrivate->stdrvXC_MVideo.abSecure[enWindow];//secure buffer
            u32TimeDiff = MsOS_GetSystemTime();
            phyFRCLeftBaseaddress = MHal_FRC_GetMMAAddr(pInstance,u32FRCMmaSize,au8FRCLeftBufTag,bSecure,enWindow);
            u32TimeDiff = MsOS_GetSystemTime() - u32TimeDiff;
            if(phyFRCLeftBaseaddress != XC_INVALID_MEM)
            {
                pXCResourcePrivate->stdrvXC_MVideo.phyFRCLeftMMABaseAddress = phyFRCLeftBaseaddress;
                pXCResourcePrivate->stdrvXC_MVideo.u32PreMMAFRC_LSize = u32FRCMmaSize;
                printf("[%s,%5d][MMA], phyFRCLeftBaseaddress = %llx finish, time = %d\n",__func__, __LINE__,phyFRCLeftBaseaddress,u32TimeDiff);
            }
            else
            {
                printf("[%s][%d] FRC alloc mma fail\n",__FUNCTION__,__LINE__);
            }



            MsOS_SetEvent(pstTask->s32EventId, E_XC_MMA_EVENT_FRC_L_GET_DONE);
        }

        //FRC R get MMA
        if (u32EventFlag & E_XC_MMA_EVENT_FRC_R_GET)
        {
            MS_PHY phyFRCRightBaseaddress = 0;
            MS_U32 u32FRCMmaSize = MHal_FRC_GetFrcRMemorySize(pInstance);
            MS_U8 au8FRCRightBufTag[XC_TAG_NAME_LEN_MAX] = FRC_RIGHT_TAG_NAME;
            MS_BOOL bSecure = pXCResourcePrivate->stdrvXC_MVideo.abSecure[enWindow];//secure buffer
            u32TimeDiff = MsOS_GetSystemTime();
            phyFRCRightBaseaddress = MHal_FRC_GetMMAAddr(pInstance,u32FRCMmaSize,au8FRCRightBufTag,bSecure,enWindow);
            u32TimeDiff = MsOS_GetSystemTime() - u32TimeDiff;
            if(phyFRCRightBaseaddress != XC_INVALID_MEM)
            {
                pXCResourcePrivate->stdrvXC_MVideo.phyFRCRightMMABaseAddress = phyFRCRightBaseaddress;
                pXCResourcePrivate->stdrvXC_MVideo.u32PreMMAFRC_RSize = u32FRCMmaSize;
                printf("[%s,%5d][MMA], phyFRCReftBaseaddress = %llx finish, time = %d\n",__func__, __LINE__,phyFRCRightBaseaddress,u32TimeDiff);
            }
            else
            {
                printf("[%s][%d] FRC_R alloc mma fail\n",__FUNCTION__,__LINE__);
            }
            MsOS_SetEvent(pstTask->s32EventId, E_XC_MMA_EVENT_FRC_R_GET_DONE);
        }

        //FRC PQ get MMA
        if (u32EventFlag & E_XC_MMA_EVENT_FRC_PQ_GET)
        {
            MS_PHY phyFRCPQBaseaddress = 0;
            MS_U32 u32FRCMmaSize = MHal_FRC_GetFrcPQMemorySize(pInstance);
            MS_U8 au8FRCPQBufTag[XC_TAG_NAME_LEN_MAX] = FRC_PQ_TAG_NAME;
            MS_BOOL bSecure = pXCResourcePrivate->stdrvXC_MVideo.abSecure[enWindow];//secure buffer
            u32TimeDiff = MsOS_GetSystemTime();
            phyFRCPQBaseaddress = MHal_FRC_GetMMAAddr(pInstance,u32FRCMmaSize,au8FRCPQBufTag,bSecure,enWindow);
            u32TimeDiff = MsOS_GetSystemTime() - u32TimeDiff;
            if(phyFRCPQBaseaddress != XC_INVALID_MEM)
            {
                pXCResourcePrivate->stdrvXC_MVideo.phyFRCPQMMABaseAddress = phyFRCPQBaseaddress;
                pXCResourcePrivate->stdrvXC_MVideo.u32PreMMAFRC_PQSize = u32FRCMmaSize;
                printf("[%s,%5d][MMA], phyFRCReftBaseaddress = %llx finish, time = %d\n",__func__, __LINE__,phyFRCPQBaseaddress,u32TimeDiff);
            }
            else
            {
                printf("[%s][%d] FRC_PQ alloc mma fail\n",__FUNCTION__,__LINE__);
            }



            MsOS_SetEvent(pstTask->s32EventId, E_XC_MMA_EVENT_FRC_PQ_GET_DONE);
        }

        //FRC MMA release
        if (u32EventFlag & E_XC_MMA_EVENT_FRC_PUT)
        {
            u32TimeDiff = MsOS_GetSystemTime();
            MHal_FRC_ReleaseMMA(pInstance);
            u32TimeDiff = MsOS_GetSystemTime() - u32TimeDiff;

            printf("[%s,%5d]release FRC MMA finish, time = %d\n",__func__, __LINE__,u32TimeDiff);

            MsOS_SetEvent(pstTask->s32EventId, E_XC_MMA_EVENT_FRC_PUT_DONE);
        }

        // HDMI DMA get MMA
        if (u32EventFlag & E_XC_MMA_EVENT_HDMI_DMA_GET)
        {
            XC_HDR_DMA_INIT_INFO stDMAInitInfo = {0,};
            MS_PHY u64HdmiDmaBaseaddress = 0;
            MS_U32 u32MmaSize =  XC_HDMI_DMA_MMA_SIZE;
            MS_BOOL bSecure = pXCResourcePrivate->stdrvXC_MVideo.abSecure[enWindow];//secure buffer

            u64HdmiDmaBaseaddress = MHal_XC_HdmiDMAGetMMAAddr(pInstance,u32MmaSize,bSecure,enWindow);

            stDMAInitInfo.phyBaseAddr = u64HdmiDmaBaseaddress;
            stDMAInitInfo.u32Size = u32MmaSize;
            u32TimeDiff = MsOS_GetSystemTime();
            MHal_XC_HDR_Control(pInstance, E_XC_HDR_CTRL_INIT_DMA, (void *)(&stDMAInitInfo));
            u32TimeDiff = MsOS_GetSystemTime() - u32TimeDiff;

            printf("[%s,%5d][MMA], u64HdmiDmaBaseaddress = %llx finish, time = %d\n",__func__, __LINE__,u64HdmiDmaBaseaddress,u32TimeDiff);

            MsOS_SetEvent(pstTask->s32EventId, E_XC_MMA_EVENT_HDMI_DMA_GET_DONE);
        }

        //HDMI DMA MMA release
        if (u32EventFlag & E_XC_MMA_EVENT_HDMI_DMA_PUT)
        {
            u32TimeDiff = MsOS_GetSystemTime();
            MHal_XC_HdmiDMAReleaseMMA(pInstance,enWindow);
            u32TimeDiff = MsOS_GetSystemTime() - u32TimeDiff;

            printf("[%s,%5d]release HDMI DMA MMA finish, time = %d\n",__func__, __LINE__,u32TimeDiff);

            MsOS_SetEvent(pstTask->s32EventId, E_XC_MMA_EVENT_HDMI_DMA_PUT_DONE);
        }

        // HSE get MMA
        if (u32EventFlag & E_XC_MMA_EVENT_HSE_GET)
        {
            MS_PHY phyHSEBaseaddress = 0;
            MS_BOOL bSecure = pXCResourcePrivate->stdrvXC_MVideo.abSecure[enWindow];//secure buffer

            u32TimeDiff = MsOS_GetSystemTime();
            phyHSEBaseaddress = MHal_XC_HseGetMMAAddr(pInstance, XC_HSE_MMA_SIZE, bSecure, enWindow);
            u32TimeDiff = MsOS_GetSystemTime() - u32TimeDiff;
            if(phyHSEBaseaddress != XC_INVALID_MEM)
            {
                pXCResourcePrivate->stdrvXC_MVideo.phyHSEMMABaseAddress = phyHSEBaseaddress;
                printf("[%s,%5d][MMA], phyHSEBaseaddress = 0x%llx finish, time = %d\n", __func__, __LINE__, phyHSEBaseaddress, u32TimeDiff);
            }
            else
            {
                printf("[%s][%d] HSE alloc mma fail\n",__FUNCTION__,__LINE__);
            }
            MsOS_SetEvent(pstTask->s32EventId, E_XC_MMA_EVENT_HSE_GET_DONE);
        }

        //HSE MMA release
        if (u32EventFlag & E_XC_MMA_EVENT_HSE_PUT)
        {
            u32TimeDiff = MsOS_GetSystemTime();
            MHal_XC_HseReleaseMMA(pInstance, enWindow);
            u32TimeDiff = MsOS_GetSystemTime() - u32TimeDiff;
            printf("[%s,%5d]Release HSE MMA finish, time = %d\n",__func__, __LINE__,u32TimeDiff);
            MsOS_SetEvent(pstTask->s32EventId, E_XC_MMA_EVENT_HSE_PUT_DONE);
        }

        if (u32EventFlag & E_XC_MMA_EVENT_TASK_DELETE)
        {
            bRun = FALSE;
        }

        pXCResourcePrivate->stdrvXC_MVideo.u32MMATimeDiff = u32TimeDiff;
    }

    MsOS_SetEvent(pstTask->s32EventId, E_XC_MMA_EVENT_TASK_DELETE_DONE);
}

static MS_BOOL _Mdrv_XC_MMAControlThreadDelete(void *pInstance,SCALER_WIN enWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    ST_XC_MMA_ALLOC_TASK *pstTask = NULL;
    MS_BOOL bRet = FALSE;

#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 1)
    SCALER_WIN enWindowID = MAX_WINDOW;

    if(psXCInstPri->u32DeviceID == 0)
    {
        enWindowID = MAIN_WINDOW;
    }
    else if(psXCInstPri->u32DeviceID == 1)
    {
        enWindowID = SUB_WINDOW
    }
    else
    {
        printf("Error not support this window\n");
    }
    enWindow = MAIN_WINDOW;
    pstTask = &(_gstMMATask[enWindowID]);
#else
    pstTask = &(_gstMMATask[enWindow]);
#endif

    if (pstTask->s32TaskId < 0)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s, %d] Task(id=0x%x) invaild\n",__FUNCTION__, __LINE__,(unsigned int)pstTask->s32TaskId);

        bRet = FALSE;
    }
    else
    {
        if(--pstTask->u8TaskRefCnt == 0)
        {
            bRet = Mdrv_XC_Task_WaitEventDone(
            pInstance,
            E_XC_MMA_EVENT_TASK_DELETE,
            E_XC_MMA_EVENT_TASK_DELETE_DONE,
            TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
            enWindow);
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s, %d] Task(id=0x%x) WaitEvent delete done %s\n",__FUNCTION__, __LINE__,(unsigned int)pstTask->s32TaskId,
            bRet ? "success" : "fail");

            pstTask->s32TaskId = -1;
        }
        else
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s, %d] Task(id=0x%x) u8TaskRefCnt=%d\n",__FUNCTION__, __LINE__,(unsigned int)pstTask->s32TaskId, pstTask->u8TaskRefCnt);
        }
    }

    return bRet;
}

static MS_BOOL _Mdrv_XC_MMAControlThreadCreate(ST_XC_MMA_ALLLOC_INFO *stMMAInfo)
{
    void *pInstance = stMMAInfo->pInstance;
    SCALER_WIN enWindow = stMMAInfo->enWindow;

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    ST_XC_MMA_ALLOC_TASK *pstTask = NULL;
    MS_BOOL bRet = FALSE;

#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 1)
    SCALER_WIN enWindowID = MAX_WINDOW;

    if(psXCInstPri->u32DeviceID == 0)
    {
        enWindowID = MAIN_WINDOW;
    }
    else if(psXCInstPri->u32DeviceID == 1)
    {
        enWindowID = SUB_WINDOW
    }
    else
    {
        printf("Error not support this window\n");
    }
    pstTask = &(_gstMMATask[enWindowID]);
#else
    pstTask = &(_gstMMATask[enWindow]);
#endif

    pstTask->s32TaskId = MsOS_CreateTask(
        (TaskEntry)_Mdrv_XC_MMAControlMonitor,
        (MS_VIRT)stMMAInfo,
        E_TASK_PRI_MEDIUM,
        TRUE,
        NULL,
        32,
        "XC_MMA_CONTROL");

    if (pstTask->s32TaskId >= 0)
    {
        bRet =TRUE;
    }
    else
    {
        printf("%s,%d,Thread Task create failed\n",__FUNCTION__,__LINE__);

        bRet = FALSE;
    }

    if (bRet)
    {
        pstTask->u8TaskRefCnt++;
    }

    return bRet;
}
MS_BOOL _Mdrv_XC_MMAControlThreadDeleteEventGroup(void *pInstance,SCALER_WIN enWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    ST_XC_MMA_ALLOC_TASK *pstTask = NULL;
    MS_BOOL bRet = FALSE;

#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 1)
    SCALER_WIN enWindowID = MAX_WINDOW;

    if(psXCInstPri->u32DeviceID == 0)
    {
        enWindowID = MAIN_WINDOW;
    }
    else if(psXCInstPri->u32DeviceID == 1)
    {
        enWindowID = SUB_WINDOW
    }
    else
    {
        printf("Error not support this window\n");
    }
    pstTask = &(_gstMMATask[enWindowID]);
#else
    pstTask = &(_gstMMATask[enWindow]);
#endif

    if (pstTask->s32EventId < 0)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s, %d] Event(Id=0x%x) invaild\n",__FUNCTION__, __LINE__,(unsigned int)pstTask->s32EventId);

        bRet = FALSE;
    }
    else
    {
        if(--pstTask->u8EventRefCnt == 0)
        {
            bRet = MsOS_DeleteEventGroup(pstTask->s32EventId);
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s, %d] Event(Id=0x%x) MsOS_DeleteEventGroup %s\n",__FUNCTION__, __LINE__,(unsigned int)pstTask->s32EventId,bRet ? "success" : "fail");

            pstTask->s32EventId = -1;
        }
        else
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s, %d] Task(id=0x%x) u8TaskRefCnt=%d\n",__FUNCTION__, __LINE__,(unsigned int)pstTask->s32EventId, pstTask->u8EventRefCnt);
        }
    }

    return bRet;
}

static MS_BOOL _Mdrv_XC_MMAControlThreadCreateEventGroup(void *pInstance,SCALER_WIN enWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    ST_XC_MMA_ALLOC_TASK *pstTask = NULL;
    MS_BOOL bRet = FALSE;

#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 1)
    SCALER_WIN enWindowID = MAX_WINDOW;

    if(psXCInstPri->u32DeviceID == 0)
    {
        enWindowID = MAIN_WINDOW;
    }
    else if(psXCInstPri->u32DeviceID == 1)
    {
        enWindowID = SUB_WINDOW
    }
    else
    {
        printf("Error not support this window\n");
    }
    pstTask = &(_gstMMATask[enWindowID]);
#else
    pstTask = &(_gstMMATask[enWindow]);
#endif

    if (pstTask->s32EventId >= 0)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s, %d] EventGroup(Id=0x%x) exist\n",__FUNCTION__, __LINE__, (unsigned int)pstTask->s32EventId);
    }
    else
    {
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 1)
        if(psXCInstPri->u32DeviceID == 0)
        {
            pstTask->s32EventId = MsOS_CreateEventGroup("XC_MAIN");
        }
        else if(psXCInstPri->u32DeviceID  == 1)
        {
            pstTask->s32EventId = MsOS_CreateEventGroup("XC_SUB");
        }
        else
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s, %d] not support this window\n",__FUNCTION__, __LINE__);
        }
#else
        if(enWindow == MAIN_WINDOW)
        {
            pstTask->s32EventId = MsOS_CreateEventGroup("XC_MAIN");
        }
        else if(enWindow == SUB_WINDOW)
        {
            pstTask->s32EventId = MsOS_CreateEventGroup("XC_SUB");
        }
        else
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s, %d] not support this window\n",__FUNCTION__, __LINE__);
        }
#endif
        if(pstTask->s32EventId >= 0)
        {
            bRet = TRUE;
        }
        else
        {
            bRet = FALSE;
        }
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s, %d] MsOS_CreateEventGroup(Id=0x%x) %s\n",__FUNCTION__, __LINE__,(unsigned int)pstTask->s32EventId,bRet?"success" : "fail");
    }

    if(bRet)
    {
        pstTask->u8EventRefCnt++;
    }

    return bRet;
}

MS_BOOL _Mdrv_XC_MMA_Task_Exit(void *pInstance,SCALER_WIN enWindow)
{
    _Mdrv_XC_MMAControlThreadDelete(pInstance,enWindow);
    _Mdrv_XC_MMAControlThreadDeleteEventGroup(pInstance,enWindow);
    return TRUE;
}

MS_BOOL _Mdrv_XC_MMA_Task_Init(void *pInstance,SCALER_WIN enWindow)
{
    ST_XC_MMA_ALLLOC_INFO *pstMMAInfo = NULL;

    if (_Mdrv_XC_MMAControlThreadCreateEventGroup(pInstance,enWindow) == FALSE)
    {
        return FALSE;
    }

    _gstMMAInfo[enWindow].enWindow = enWindow;
    _gstMMAInfo[enWindow].pInstance = pInstance;

    pstMMAInfo = &(_gstMMAInfo[enWindow]);
    if (_Mdrv_XC_MMAControlThreadCreate(pstMMAInfo) == FALSE)
    {
        _Mdrv_XC_MMAControlThreadDeleteEventGroup(pInstance,enWindow);
        return FALSE;
    }

    return TRUE;
}

#endif

#ifndef DONT_USE_CMA
#if (XC_SUPPORT_CMA ==TRUE)
static MS_BOOL _MDrv_XC_CheckIfConfigCMA(void *pInstance, SCALER_WIN eWindow)
{
#ifndef ANDROID
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    if (
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
#ifdef UFO_XC_SUPPORT_SUB_CMA
    ((psXCInstPri->u32DeviceID==0)||(psXCInstPri->u32DeviceID==1))
#else
    (psXCInstPri->u32DeviceID==0)
#endif
#elif (HW_DESIGN_4K2K_VER == 7)
    (psXCInstPri->u32DeviceID == 0) && (MAIN_WINDOW==eWindow)
#else
    (MAIN_WINDOW==eWindow)
#endif

#ifdef CHIP_ENABLE_DMS //in MApi_XC_SetWindow_U2(): if CHIP_ENABLE_DMS== TURE, then g_bSWDSGenScenario = false,MDrv_XC_Is_UpdateDsCmd indicates ds procedure
    && (!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
#else
    && (!g_bSWDSGenScenario) //bUpdate_DS_CMD maybe changed during ds procedure, g_bSWDSGenScenario indicates it's ds procedure
#endif
#if (XC_SUPPORT_IOMMU == TRUE)
        &&((pXCResourcePrivate->stdrvXC_MVideo_Context.stMMAfeature.u32Support == MSOS_MMA_NOT_SUPPORT)
           || (pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[eWindow] == FALSE))
#endif
    && (pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].isEnable == FALSE))
    {
        return TRUE;
    }
    else
#endif
    {
        return FALSE;
    }
}
#endif
#endif

//-------------------------------------------------------------------------------------------------
//  Global Functions
//-------------------------------------------------------------------------------------------------
#ifndef MSOS_TYPE_NUTTX
E_APIXC_ReturnValue MApi_XC_Enable_LockFreqOnly_U2(void* pInstance,MS_BOOL bEnable)
{
    E_APIXC_ReturnValue enRet = E_APIXC_RET_OK;

    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[E_XC_POOL_ID_INTERNAL_VARIABLE],(void**)(&pXCResourcePrivate));

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    pXCResourcePrivate->stdrvXC_MVideo_Context.g_bEnableLockFreqOnly = bEnable;
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    return enRet;
}

E_APIXC_ReturnValue MApi_XC_Enable_LockFreqOnly(MS_BOOL bEnable)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_Enable_LockFreqOnly XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_ENABLE_LOCKFREQONLY, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }

}
#endif
#if(XC_CONTEXT_ENABLE)
MS_U8 _XC_Client[] = {"Linux XC driver"};

static MS_BOOL _MApi_XC_Init_Context(void *pInstance, MS_BOOL *pbFirstDrvInstant)
{
    MS_BOOL bRet = TRUE;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    *pbFirstDrvInstant = pXCResourcePrivate->stdrvXC_MVideo.bNeedInitShared;

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return bRet;
}
#endif
#ifndef MSOS_TYPE_NUTTX
//-------------------------------------------------------------------------------------------------
/// Exit the XC
/// @return @ref MS_BOOL
//-------------------------------------------------------------------------------------------------
MS_BOOL MApi_XC_Exit_U2(void* pInstance)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    if (_XC_Mutex == -1)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return TRUE;
    }
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

#if ((HW_DESIGN_4K2K_VER == 6) && FRC_INSIDE)
        MS_U8 u8LPLL_Type =(MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type;

        if (IsVBY1_16LANE(u8LPLL_Type))
        {
            MHal_XC_PixelShiftStatusChange(pInstance, EN_XC_PIXEL_SHIFT_DISABLE);
        }
        // Maserati mode
        if ((u8LPLL_Type >= E_XC_PNL_LPLL_VBY1_10BIT_16LANE)&&(u8LPLL_Type <= E_XC_PNL_LPLL_VBY1_8BIT_16LANE))
        {
            // case 4: no FSC + FRC
            if(MDrv_XC_MLoad_GetStatus(pInstance) == E_MLOAD_ENABLED)
            {
                _MLOAD_ENTRY(pInstance);
            }
            else
            {
                printf("mload is not enable, please check !\n");
            }
            MHal_FRC_Set_Pipe_Delay_Reset(pInstance, DISABLE, TRUE);

            MHal_FRC_Set_TimingStable(pInstance, TRUE);
            MHal_FRC_ByPass_Enable(pInstance, FALSE);  // enable FRC
            MHal_FRC_Set_STGEN_ODCLK(pInstance, E_FRC_STGEN_ODCLK_1, TRUE);

            MHal_FRC_Set_Composer_User_Mode(pInstance, E_FRC_COMPOSER_SOURCE_MODE_VIP, TRUE);
            MHal_FRC_Set_FSC_DE_Selection(pInstance, E_FRC_FSC_SOURCE_MODE_MDE);
            MHal_FRC_TGEN_Enable_Lock_Source(pInstance, DISABLE, E_FRC_TGEN_LOCK_SOURCE_MODE_FROM_TGEN, TRUE);
            MDrv_XC_MLoad_Fire(pInstance, TRUE);
            if(MDrv_XC_MLoad_GetStatus(pInstance) == E_MLOAD_ENABLED)
            {
                _MLOAD_RETURN(pInstance);
            }
            else
            {
                printf("mload is not enable, please check !\n");
            }
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFSCEnabled = FALSE;
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFRCEnabled = TRUE;
        }
        else
        {
            // For Manhattan mode
            // no FSC+ no FRC
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFSCEnabled = FALSE;
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFRCEnabled = FALSE;
            if(MDrv_XC_MLoad_GetStatus(pInstance) == E_MLOAD_ENABLED)
            {
                _MLOAD_ENTRY(pInstance);
            }
            else
            {
                printf("mload is not enable, please check !\n");
            }
            MHal_FRC_Set_Pipe_Delay_Reset(pInstance, DISABLE, TRUE);
            MHal_FRC_AdjustGOPPosition(pInstance);
            MHal_FRC_ByPass_Enable(pInstance, TRUE); // disable FRC
            MHal_FRC_Set_STGEN_ODCLK(pInstance, E_FRC_STGEN_ODCLK_1, TRUE);

            MHal_FRC_Set_Composer_User_Mode(pInstance, E_FRC_COMPOSER_SOURCE_MODE_VIP, TRUE);
            MHal_FRC_Set_FSC_DE_Selection(pInstance, E_FRC_FSC_SOURCE_MODE_MDE);
            MHal_FRC_TGEN_Enable_Lock_Source(pInstance, DISABLE, E_FRC_TGEN_LOCK_SOURCE_MODE_FROM_TGEN, TRUE);
            MDrv_XC_MLoad_Fire(pInstance, TRUE);
            if(MDrv_XC_MLoad_GetStatus(pInstance) == E_MLOAD_ENABLED)
            {
                _MLOAD_RETURN(pInstance);
            }
            else
            {
                printf("mload is not enable, please check !\n");
            }
        }
#endif
    _XC_ENTRY(pInstance);
#ifdef K3_U2
    if(psXCInstPri->u32DeviceID == E_XC_DEVICE0)
    {
        if(pXCResourcePrivate->stdrvXC_MVideo._stXC_ApiInfo.u8MaxWindowNum > 1)
        {
            // DISABLE FIClk1
            MDrv_WriteRegBit(REG_CKG_FICLK_F1, ENABLE, CKG_FICLK_F1_GATED);                // Enable clock

            // DISABLE IDClk1
            MDrv_WriteRegBit(REG_CKG_IDCLK1, ENABLE, CKG_IDCLK1_GATED);

            // DISABLE SIDClk1
            MDrv_WriteRegBit(REG_CKG_SIDCLK1, ENABLE, CKG_SIDCLK1_GATED);
        }

        // DISABLE FIClk2
        MDrv_WriteRegBit(REG_CKG_FICLK_F2, ENABLE, CKG_FICLK_F2_GATED);                    // Enable clock

        // DISABLE FClk
        MDrv_WriteRegBit(REG_CKG_FCLK, ENABLE, CKG_FCLK_GATED);                            // Enable clock

        // DISABLE IDClk2
        MDrv_WriteRegBit(REG_CKG_IDCLK2, ENABLE, CKG_IDCLK2_GATED);                        // Enable clock

        // DISABLE SIDClk2
        MDrv_WriteRegBit(REG_CKG_SIDCLK2, ENABLE, CKG_SIDCLK2_GATED);
    }
    else // pDeviceId->u32Id == E_XC_DEVICE1
    {
        // DISABLE FIClk2
        MDrv_WriteRegBit(REG_CKG_SC1_FECLK_F2, ENABLE, CKG_SC1_FECLK_F2_GATED);                    // Enable clock

        // DISABLE FClk
        MDrv_WriteRegBit(REG_CKG_SC1_FCLK, ENABLE, CKG_SC1_FCLK_GATED);                            // Enable clock

        // DISABLE IDClk2
        MDrv_WriteRegBit(REG_CKG_SC1_IDCLK2, ENABLE, CKG_SC1_IDCLK2_GATED);                        // Enable clock

        // DISABLE SIDClk2
        MDrv_WriteRegBit(REG_CKG_SC1_SIDCLK2, ENABLE, CKG_SC1_SIDCLK2_GATED);
    }
#else

#if(HW_DESIGN_4K2K_VER == 4)
    if(psXCInstPri->u32DeviceID == E_XC_DEVICE0)
    {
        if(pXCResourcePrivate->stdrvXC_MVideo._stXC_ApiInfo.u8MaxWindowNum > 1)
        {
            // DISABLE FIClk1
            MDrv_WriteRegBit(REG_CKG_FICLK_F1, ENABLE, CKG_FICLK_F1_GATED);                // Enable clock

            // DISABLE IDClk1
            MDrv_WriteRegBit(REG_CKG_IDCLK1, ENABLE, CKG_IDCLK1_GATED);
        }

            // DISABLE EDCLK_F2
        Hal_SC_set_edclk(pInstance, 0x00, DISABLE, MAIN_WINDOW);
            // DISABLE EDCLK_F1
        Hal_SC_set_edclk(pInstance, 0x00, DISABLE, SUB_WINDOW);

        // DISABLE FICLK2_F2
        Hal_SC_set_ficlk2(pInstance, 0x00, DISABLE, MAIN_WINDOW);

        // DISABLE FIClk_F2
        MDrv_WriteRegBit(REG_CKG_FICLK_F2, ENABLE, CKG_FICLK_F2_GATED);                    // Enable clock

        // DISABLE FClk
        MDrv_WriteRegBit(REG_CKG_FCLK, ENABLE, CKG_FCLK_GATED);                            // Enable clock

        // DISABLE IDClk2
        MDrv_WriteRegBit(REG_CKG_IDCLK2, ENABLE, CKG_IDCLK2_GATED);                        // Enable clock
    }
    else
    {
#ifdef MONACO_SC2_PATCH
        // DISABLE FIClk_F2
        MDrv_WriteRegBit(REG_CKG_S2_FICLK2_F2, ENABLE, CKG_FICLK2_F2_GATED);                    // Enable clock
#endif
        // DISABLE FClk
        MDrv_WriteRegBit(REG_CKG_S2_FCLK, ENABLE, CKG_S2_FCLK_GATED);                            // Enable clock

        // DISABLE IDClk2
        MDrv_WriteRegBit(REG_CKG_S2_IDCLK2, ENABLE, CKG_S2_IDCLK2_GATED);                        // Enable clock

    }
#else
    if(pXCResourcePrivate->stdrvXC_MVideo._stXC_ApiInfo.u8MaxWindowNum > 1)
    {
        // DISABLE FIClk1
        MDrv_WriteRegBit(REG_CKG_FICLK_F1, ENABLE, CKG_FICLK_F1_GATED);                // Enable clock

        // DISABLE IDClk1
        MDrv_WriteRegBit(REG_CKG_IDCLK1, ENABLE, CKG_IDCLK1_GATED);
    }

        // DISABLE EDCLK_F2
    Hal_SC_set_edclk(pInstance, 0x00, DISABLE, MAIN_WINDOW);
        // DISABLE EDCLK_F1
    Hal_SC_set_edclk(pInstance, 0x00, DISABLE, SUB_WINDOW);

    // DISABLE FICLK2_F2
    Hal_SC_set_ficlk2(pInstance, 0x00, DISABLE, MAIN_WINDOW);

    // DISABLE FIClk_F2
    MDrv_WriteRegBit(REG_CKG_FICLK_F2, ENABLE, CKG_FICLK_F2_GATED);                    // Enable clock

    // DISABLE FClk
    MDrv_WriteRegBit(REG_CKG_FCLK, ENABLE, CKG_FCLK_GATED);                            // Enable clock

    // DISABLE IDClk2
    MDrv_WriteRegBit(REG_CKG_IDCLK2, ENABLE, CKG_IDCLK2_GATED);                        // Enable clock

    MHal_XC_ClockControl(pInstance, DISABLE,CKG_SC_SLOW_DEFAULT);


#endif
#endif
    _XC_RETURN(pInstance);

//DISABLE Interrupt
#ifndef MSOS_TYPE_LINUX_KERNEL
    if(E_XC_DEVICE0 == psXCInstPri->u32DeviceID)
    {
        MsOS_DisableInterrupt(XC_MAIN_SC_IRQ);
    }
    else if(E_XC_DEVICE1 == psXCInstPri->u32DeviceID)
    {
        MsOS_DisableInterrupt(E_INT_IRQ_EC_BRIDGE);
        MsOS_DisableInterrupt(E_INT_IRQ_DISP1);
    }
#endif

    // Release ISR
#ifdef MSOS_TYPE_LINUX_KERNEL
    if(E_XC_DEVICE0 == psXCInstPri->u32DeviceID)
    {
        MsOS_DetachInterrupt_Shared(XC_MAIN_SC_IRQ);
    }
    else if(E_XC_DEVICE1 == psXCInstPri->u32DeviceID)
    {
        MsOS_DetachInterrupt_Shared(E_INT_IRQ_EC_BRIDGE);
        MsOS_DetachInterrupt_Shared(E_INT_IRQ_DISP1);
    }
#else
    if(E_XC_DEVICE0 == psXCInstPri->u32DeviceID)
    {
        MsOS_DetachInterrupt(XC_MAIN_SC_IRQ);
    }
    else if(E_XC_DEVICE1 == psXCInstPri->u32DeviceID)
    {
        MsOS_DetachInterrupt(E_INT_IRQ_EC_BRIDGE);
        MsOS_DetachInterrupt(E_INT_IRQ_DISP1);
    }
#endif

    // Clear internal variable
    mvideo_sc_variable_init(pInstance, TRUE, &pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData);

    _XC_ENTRY(pInstance);
    memset(&pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData, 0 , sizeof(XC_INITDATA) );

    #if 0 //(FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
    MHal_XC_FRCR2_IP_PatchClose(pInstance, MAIN_WINDOW);
    MHal_XC_FRCR2_OP_PatchClose(pInstance, MAIN_WINDOW);
    #endif

    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    // For XC re-init
    MsOS_DeleteMutex(_XC_Mutex);
    _XC_Mutex = -1;

    MsOS_DeleteMutex(_XC_ISR_Mutex);
    _XC_ISR_Mutex = -1;

#if (XC_SUPPORT_IOMMU == TRUE)
    if(pXCResourcePrivate->stdrvXC_MVideo_Context.stMMAfeature.u32Support == MSOS_MMA_SUPPORT)
    {
        if(pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_XC_MAIN] == TRUE)
        {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
            MHal_XC_ReleaseMMA(pInstance,MAIN_WINDOW);
#else
            MS_BOOL bRet = FALSE;

            bRet = Mdrv_XC_Task_WaitEventDone(
            pInstance,
            E_XC_MMA_EVENT_XC_PUT,
            E_XC_MMA_EVENT_XC_PUT_DONE,
            TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
            MAIN_WINDOW);
            if(bRet == FALSE)
            {
                printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
            }
#endif
        }
        if(pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_XC_SUB] == TRUE)
        {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
            MHal_XC_ReleaseMMA(pInstance,SUB_WINDOW);
#else
                MS_BOOL bRet = FALSE;

                bRet = Mdrv_XC_Task_WaitEventDone(
                pInstance,
                E_XC_MMA_EVENT_XC_PUT,
                E_XC_MMA_EVENT_XC_PUT_DONE,
                TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                SUB_WINDOW);
                if(bRet == FALSE)
                {
                    printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                }
#endif
        }
        if(pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_HDMI_DMA] == TRUE)
        {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
            MHal_XC_HdmiDMAReleaseMMA(pInstance,MAIN_WINDOW);
#else
            MS_BOOL bRet = FALSE;

            bRet = Mdrv_XC_Task_WaitEventDone(
            pInstance,
            E_XC_MMA_EVENT_HDMI_DMA_PUT,
            E_XC_MMA_EVENT_HDMI_DMA_PUT_DONE,
            TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
            MAIN_WINDOW);
            if(bRet == FALSE)
            {
                printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
            }
#endif
        }
        if((pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_FRC_RIGHT] == TRUE)
            || (pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_FRC_LEFT] == TRUE)
            || (pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_FRC_PQ] == TRUE))
        {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
            MHal_FRC_ReleaseMMA(pInstance);
#else
                MS_BOOL bRet = FALSE;

                bRet = Mdrv_XC_Task_WaitEventDone(
                pInstance,
                E_XC_MMA_EVENT_FRC_PUT,
                E_XC_MMA_EVENT_FRC_PUT_DONE,
                TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                MAIN_WINDOW);
                if(bRet == FALSE)
                {
                    printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                }

                _Mdrv_XC_MMA_Task_Exit(pInstance,MAIN_WINDOW);
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
                _Mdrv_XC_MMA_Task_Exit(pInstance,SUB_WINDOW);
#endif
#endif
        }

        MDrv_XC_HseReleaseMMA(pInstance, MAIN_WINDOW);

        MsOS_MMA_Exit();
    }
#endif

#ifndef DONT_USE_CMA
#ifndef MSOS_TYPE_NUTTX
#if (XC_SUPPORT_CMA ==TRUE)
#if (HW_DESIGN_4K2K_VER == 7)
    if((psXCInstPri->u32DeviceID == 0)
#if (XC_SUPPORT_IOMMU == TRUE)
    && ((pXCResourcePrivate->stdrvXC_MVideo_Context.stMMAfeature.u32Support == MSOS_MMA_NOT_SUPPORT)
              || (pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_XC_MAIN] == FALSE))
#endif
    )
#endif
    {
        if ((pXCResourcePrivate->sthal_Optee.op_tee_xc[MAIN_WINDOW].isEnable == TRUE) ||
            (pXCResourcePrivate->sthal_Optee.op_tee_xc[SUB_WINDOW].isEnable == TRUE))
        {
            XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
            return TRUE;
        }
        MHal_XC_Release_CMA(pInstance, CMA_XC_SELF_MEM, MAIN_WINDOW);
        MApi_CMA_Pool_Release(gSrcInfo[MAIN_WINDOW].Status2.stXCConfigCMA[CMA_XC_SELF_MEM].u32HeapID);
        if (gSrcInfo[MAIN_WINDOW].Status2.stXCConfigCMA[CMA_XC_COBUFF_MEM].u32HeapID != XC_INVALID_HEAP_ID)
        {
            MHal_XC_Release_CMA(pInstance, CMA_XC_COBUFF_MEM, MAIN_WINDOW);
            MApi_CMA_Pool_Release(gSrcInfo[MAIN_WINDOW].Status2.stXCConfigCMA[CMA_XC_COBUFF_MEM].u32HeapID);
        }
        if (gSrcInfo[MAIN_WINDOW].Status2.stXCConfigCMA[CMA_XC_HDR_DMA_MEM].u32HeapID != XC_INVALID_HEAP_ID)
        {
            MHal_XC_Release_CMA(pInstance, CMA_XC_HDR_DMA_MEM, MAIN_WINDOW);
            MApi_CMA_Pool_Release(gSrcInfo[MAIN_WINDOW].Status2.stXCConfigCMA[CMA_XC_HDR_DMA_MEM].u32HeapID);
        }
    }
#endif
#endif
#endif
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return TRUE;

}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_Exit(void)
{

    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    stXC_EXIT XCArgs;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_EXIT, (void*)&XCArgs) != 0)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif
MS_BOOL MDrv_XC_IsPNLYUVOutput(void *pInstance)
{
    //Pnl output is affected by input signal, so change to xc driver to report it as below
    return MHal_XC_IsPNLYUVOutput(pInstance);
}

MS_BOOL MDrv_XC_Is2K2KToFrc(void *pInstance)
{
    MS_BOOL bRet = FALSE;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
#if FRC_INSIDE
    // For Einstein FRC 4K2K case, for non-4K2K input, need to sent 2Kx2K to FRC
    if ((pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16DefaultHTotal> 3000) &&
        (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16DefaultVTotal> 2000) &&
        (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16DefaultVFreq > 500) &&  // 4K2K 60 out
        (pXCResourcePrivate->stdrvXC_MVideo_Context.enFRC_CurrentInputTiming == E_XC_FRC_InputTiming_2K1K))
    {
        bRet = TRUE;
    }
#endif
    return bRet;
}
#endif
MS_BOOL MDrv_XC_SetIOMapBase_i(void* pInstance)
{
    MS_VIRT _XCRIUBaseAddress = 0, _PMRIUBaseAddress = 0, _XCEXTRIUBaseAddress = 0;
    MS_PHY u32NonPMBankSize = 0, u32PMBankSize = 0;
    #ifdef SUPPORT_XC_EXT_REG
    MS_PHY u32EXTBankSize = 0;
    #endif
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");


    if(MDrv_MMIO_GetBASE( &_XCRIUBaseAddress, &u32NonPMBankSize, MS_MODULE_XC ) != TRUE)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "MApi_XC_Init GetBASE failure\n");
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return FALSE;
    }
    else
    {
        //printf("MApi_XC_Init GetBase success %lx\n", _XCRIUBaseAddress);
    }

    if(MDrv_MMIO_GetBASE( &_PMRIUBaseAddress, &u32PMBankSize, MS_MODULE_PM ) != TRUE)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "MApi_XC_Init Get PM BASE failure\n");
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return FALSE;
    }
    else
    {
        //printf("MApi_XC_Init GetPMBase success %lx\n", _PMRIUBaseAddress);
    }

    #ifdef SUPPORT_XC_EXT_REG
    if(MDrv_MMIO_GetBASE( &_XCEXTRIUBaseAddress, &u32EXTBankSize, MS_MODULE_EXTRIU ) != TRUE)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "MApi_XC_Init Get EXT BASE failure\n");
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        //return FALSE;
    }
    else
    {
        //printf("MApi_XC_Init GetPMBase success %lx\n", _PMRIUBaseAddress);
    }
    #endif

    //printf("XC RIU base=%lx\n", _XCRIUBaseAddress);
    MDrv_XC_init_riu_base(_XCRIUBaseAddress, _PMRIUBaseAddress, _XCEXTRIUBaseAddress);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return TRUE;
}

MS_BOOL MDrv_XC_SetIOMapBase_U2(void* pInstance)
{
    MS_BOOL bReturn = FALSE;

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    if(MDrv_XC_SetIOMapBase_i(pInstance))
    {
        bReturn = TRUE;
    }

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return bReturn;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MDrv_XC_SetIOMapBase(void)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    stXC_CMD_SET_IOMAPBASE XCArgs;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_IOMAPBASE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif

#ifndef MSOS_TYPE_NUTTX
void MApi_XC_PreInit_FRC(void *pInstance, XC_PREINIT_INFO_t *pPanelInfo)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    ///patch
    ///g_XC_Pnl_Misc.FRCInfo.u8FRC3DPanelType = E_XC_3D_PANEL_SHUTTER;
    memcpy(&pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc, pPanelInfo, sizeof(XC_PREINIT_INFO_t));
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}

#ifdef MSOS_TYPE_LINUX_KERNEL
extern MS_BOOL KDrv_XC_HSE_Control(MS_BOOL bEnable);
#endif
E_APIXC_ReturnValue MDrv_XC_HSE_Control(void* pInstance, MS_BOOL bEnable)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    XC_HSE_PREINIT_INFO_t stHSEInfo;

    memset(&stHSEInfo, 0, sizeof(XC_HSE_PREINIT_INFO_t));
    MHal_SC_GetHSEInfo(&stHSEInfo);

    if(bEnable)
    {
        //dynamic get iommu buffer when not cobuffer between frc and hse & not get mem form mmap
        if((MHal_FRC_IsSupportFRC_byEfuse(pInstance) == FALSE) && MHal_SC_IsHSESupported()
             && (stHSEInfo.u32HSE_BufBaseAddress == 0 && stHSEInfo.u64HSE_BufBaseAddress == 0))
        {
            MHal_SC_SetHSEMemory(pInstance,TRUE);
        }

        MHal_FRC_EnableBWCtrl(pInstance, FALSE, E_FRC_BW_CTRL_SOURCE_HSE);
    }

#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    if(0 > _s32FdScaler)
    {
        _s32FdScaler = open("/dev/scaler", O_RDWR);

        if(0 > _s32FdScaler)
        {
            printf("Scaler device not opened!!!!\n");
            return E_APIXC_RET_FAIL;
        }
    }
#endif

#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    if (ioctl(_s32FdScaler, MDRV_XC_IOC_HSE_CONTROL, &bEnable))
    {
        printf("MDRV_XC_IOC_HSE_CONTROL fail!!!!\n");
        return E_APIXC_RET_FAIL;
    }
#endif

#ifdef MSOS_TYPE_LINUX_KERNEL
    if (KDrv_XC_HSE_Control(bEnable) != TRUE)
    {
        return E_APIXC_RET_FAIL;
    }
#endif

    if(!bEnable)
    {
        MHal_FRC_EnableBWCtrl(pInstance, TRUE, E_FRC_BW_CTRL_SOURCE_HSE);

        //dynamic rel iommu buffer when not cobuffer between frc and hse & not get mem form mmap
        if((MHal_FRC_IsSupportFRC_byEfuse(pInstance) == FALSE) && MHal_SC_IsHSESupported()
             && (stHSEInfo.u32HSE_BufBaseAddress == 0 && stHSEInfo.u64HSE_BufBaseAddress == 0))
        {
            MHal_SC_SetHSEMemory(pInstance,FALSE);
        }
    }

    return E_APIXC_RET_OK;
}

E_APIXC_ReturnValue MApi_XC_HSE_Control_U2(void* pInstance, MS_BOOL bEnable)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    E_APIXC_ReturnValue enReturn = E_APIXC_RET_FAIL;
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    enReturn= MDrv_XC_HSE_Control(pInstance, bEnable);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return enReturn;
}

E_APIXC_ReturnValue MApi_XC_HSE_Control(MS_BOOL bEnable)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_HSE_CONTROL XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_HSE_CONTROL, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}

static void _MDrv_XC_HSE_MEMCControl_ISR(SC_INT_SRC eIntNum, void* pParam)
{
    void *pInstance = pu32XCInst_private;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    MHal_FRC_ControlMemcLevelForHSE(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
}

static void _MDrv_XC_HSE_AdjustMEMCLevelWithHSE(void* pInstance)
{
    // HSE HW manipulates FRC buffer read/write
    // Therefore, MEMC must be off when HSE is in effect
    // MEMC will be turned on after a delay(frame number) even after HSE is off
    // MEMC level will be restored according to pre-HSE MEMC level or peri-HSE MEMC level

    if(MHal_FRC_IsSupportFRC_byEfuse(pInstance) && MHal_SC_IsHSESupported())
    {
        if(TRUE == MDrv_XC_InterruptIsAttached(pInstance, SC_INT_VSINT, _MDrv_XC_HSE_MEMCControl_ISR, (void *)NULL))
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SEAMLESSZAP, "already attached ISR\n");
        }
        else
        {
            MDrv_XC_InterruptAttachWithoutMutex(pInstance, SC_INT_VSINT, _MDrv_XC_HSE_MEMCControl_ISR, (void *)NULL);
            XC_LOG_TRACE(XC_DBGLEVEL_SEAMLESSZAP, "attached ISR\n");
        }
    }
}

E_APIXC_ReturnValue MApi_XC_SetEvent_U2(void* pInstance, ST_XC_EVENT *pstXCEvent)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    E_APIXC_ReturnValue enReturn = E_APIXC_RET_FAIL;
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    enReturn= MDrv_XC_SetEvent(pInstance, pstXCEvent);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return enReturn;
}

E_APIXC_ReturnValue MApi_XC_SetEvent(ST_XC_EVENT *pstXCEvent)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_EVENT XCArgs;
    XCArgs.pstXCEvent = pstXCEvent;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_EVENT, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}

E_APIXC_ReturnValue MApi_XC_PreInit_U2( void* pInstance, E_XC_PREINIT_t eType ,void* para, MS_U32 u32Length)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    XC_PREINIT_INFO_t* stFRCInfo = NULL;
    XC_HSE_PREINIT_INFO_t* pstHSEPreInitInfo = NULL;

    switch(eType)
    {
        case E_XC_PREINIT_FRC:
            if(u32Length != sizeof(XC_PREINIT_INFO_t))
            {
                printf("Error size of parameters\n");
                return E_APIXC_RET_FAIL;
            }
            _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
            MApi_XC_PreInit_FRC(pInstance, (XC_PREINIT_INFO_t *)para);
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        break;
        case E_XC_PREINIT_FRC_3D_TYPE:
            if(u32Length != sizeof(XC_PREINIT_INFO_t))
            {
                printf("Error size of parameters\n");
                return E_APIXC_RET_FAIL;
            }
            _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
            UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
            stFRCInfo = (XC_PREINIT_INFO_t *)para;
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u8FRC3DPanelType = stFRCInfo->FRCInfo.u8FRC3DPanelType;
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        break;
        case E_XC_PREINIT_FRC_ADDRESS:
            if(u32Length != sizeof(XC_FRC_ADDRESS_INFO))
            {
                printf("Error size of parameters\n");
                return E_APIXC_RET_FAIL;
            }
            _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
            MDrv_FRC_SetMemoryAddress(pInstance, (XC_FRC_ADDRESS_INFO *)para);
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        break;
        case E_XC_PREINIT_HDMITX_COLORDEPTH:
            #if (HW_DESIGN_HDMITX_VER == 2)
            if(u32Length != sizeof(EN_XC_HDMITX_COLORDEPTH))
            {
                printf("Error size of parameters\n");
                return E_APIXC_RET_FAIL;
            }
            _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
            UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
            pXCResourcePrivate->stdrvXC_MVideo_Context.enHDMITXColorDepth = *(EN_XC_HDMITX_COLORDEPTH *)para;
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            #else
            printf("Not support assign color depth for HDMI Tx \n");
            return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
            #endif
        break;
        case E_XC_PREINIT_HSE:
        {
            if(para == NULL)
            {
                printf("[%s, %d] HSE para is NULL\n", __func__, __LINE__);
                return E_APIXC_RET_FAIL;
            }
            else
            {
                pstHSEPreInitInfo = (XC_HSE_PREINIT_INFO_t *)para;
                XC_LOG_TRACE(XC_DBGLEVEL_SEAMLESSZAP, "[%s][%d] HSE buffer addr: 0x%x, length: %lu, 64 addr: 0x%llx\n", __FUNCTION__, __LINE__, pstHSEPreInitInfo->u32HSE_BufBaseAddress, pstHSEPreInitInfo->u32HSE_BufLength, pstHSEPreInitInfo->u64HSE_BufBaseAddress);
            }

            if(pstHSEPreInitInfo->u32HSE_PreInit_Version < 1)
            {
                printf("[%s, %d][Seamless] ERROR: HSE is not supported, u32HSE_PreInit_Version is %d\n",__func__, __LINE__, pstHSEPreInitInfo->u32HSE_PreInit_Version);
                return E_APIXC_RET_FAIL;
            }

            if(pstHSEPreInitInfo->u32HSE_PreInit_Version >= 2)
            {
                printf("[%s, %d] HSE init, u32HSE_BufBaseAddress : 0x%X, u64HSE_BufBaseAddress : 0x%lX, u32HSE_PreInit_Version is %d\n",__func__, __LINE__,
                    pstHSEPreInitInfo->u32HSE_BufBaseAddress, pstHSEPreInitInfo->u64HSE_BufBaseAddress, pstHSEPreInitInfo->u32HSE_PreInit_Version);
            }
            else
            {
                printf("[%s, %d] HSE init, u32HSE_BufBaseAddress : 0x%X, u32HSE_PreInit_Version is %d\n",__func__, __LINE__,
                    pstHSEPreInitInfo->u32HSE_BufBaseAddress, pstHSEPreInitInfo->u32HSE_PreInit_Version);
            }

            //Save HSE buffer info which comes from upper layer
            MHal_SC_SetHSEInfo(pstHSEPreInitInfo);

            // get iommu addr for HSE if user doesn't specify an address, otherwise use user-specified addr
            MHal_SC_SetHSEMemory(pInstance,FALSE);

            // attach isr in which memc will be turned off/on according to hse status
            _MDrv_XC_HSE_AdjustMEMCLevelWithHSE(pInstance);

            return E_APIXC_RET_OK;
        }
        break;
        default:
        case E_XC_PREINIT_NULL:

        break;
    }
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return E_APIXC_RET_OK;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_PreInit( E_XC_PREINIT_t eType ,void* para, MS_U32 u32Length)
{
    CheckXCInstanceOpen();

#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
     if (pu32XCInst_1 == NULL)
     {
        XC_INSTANT_ATTRIBUTE stXCInstantAttribute;
        INIT_XC_INSTANT_ATTR(stXCInstantAttribute);
        stXCInstantAttribute.u32DeviceID = 1;
        if(UtopiaOpen(MODULE_XC, &pu32XCInst_1, 0, &stXCInstantAttribute) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return E_APIXC_RET_FAIL;
        }
      }
#endif
    stXC_PREINIT XCArgs;
    XCArgs.eType = eType;
    XCArgs.para = para;
    XCArgs.u32Length = u32Length;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_PREINIT, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
static MS_BOOL _MDrv_XC_GetCFDVersion(MS_U8 *pu8Version)
{
    MS_U8 u8CFDVersion = 0;

#ifdef UFO_XC_HDR
#if (UFO_XC_HDR_VERSION == 2)
    /// Send into Kernel CFD
    ST_KDRV_XC_CFD_CONTROL_INFO stCFDCtrlInfo;
    memset(&stCFDCtrlInfo, 0, sizeof(ST_KDRV_XC_CFD_CONTROL_INFO));
    stCFDCtrlInfo.enCtrlType = E_KDRV_XC_CFD_CTRL_GET_CFD_VERSION;
    stCFDCtrlInfo.pParam = &u8CFDVersion;
    stCFDCtrlInfo.u32ParamLen = sizeof(MS_U8);

#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    if(0 > _s32FdScaler)
    {
        _s32FdScaler = open("/dev/scaler", O_RDWR);

        if(0 > _s32FdScaler)
        {
            printf("Scaler device not opened!!!!\n");
            return FALSE;
        }
    }

    if (ioctl(_s32FdScaler, MDRV_XC_IOC_CFDCONCTRL, &stCFDCtrlInfo))
    {
        printf("MDRV_XC_IOC_CFDCONCTRL Get CFD version fail!!!!\n");
        return FALSE;
    }

#endif
#if (defined(MSOS_TYPE_LINUX_KERNEL) && defined(CONFIG_MSTAR_XC_HDR_SUPPORT))
    MDrv_XC_CFDControl(&stCFDCtrlInfo);
#endif
#endif
#endif

    *pu8Version = u8CFDVersion;

    return TRUE;
}

static MS_BOOL _MDrv_XC_SetCFDHashInfo(void)
{
    ST_KDRV_XC_HASH_INFO stHashInfo ;
    memset(&stHashInfo, 0, sizeof(ST_KDRV_XC_HASH_INFO));

#ifdef UFO_XC_HDR
#if (UFO_XC_HDR_VERSION == 2)
    /// Send into Kernel CFD
    ST_KDRV_XC_CFD_CONTROL_INFO stCFDCtrlInfo;
    memset(&stCFDCtrlInfo, 0, sizeof(ST_KDRV_XC_CFD_CONTROL_INFO));
    stCFDCtrlInfo.enCtrlType = E_KDRV_XC_CFD_CTRL_SET_HASH_INFO;
    stCFDCtrlInfo.pParam = &stHashInfo;
    stCFDCtrlInfo.u32ParamLen = sizeof(ST_KDRV_XC_HASH_INFO);

    if(MDrv_AUTH_IPCheck(IPAUTH_DOLBY_HDR_PIN))
    {
        stHashInfo.u32SupportHdrType |= E_XC_CFD_HDR_TYPE_DOLBY;
    }

    if(MDrv_AUTH_IPCheck(IPAUTH_HDR10_PLUS_PIN))
    {
        stHashInfo.u32SupportHdrType |= E_XC_CFD_HDR_TYPE_HDR10_PLUS;
    }

    if(MDrv_AUTH_IPCheck(IPAUTH_TCH_HDR_PIN))
    {
        stHashInfo.u32SupportHdrType |= E_XC_CFD_HDR_TYPE_TCH;
    }

#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    if(0 > _s32FdScaler)
    {
        _s32FdScaler = open("/dev/scaler", O_RDWR);

        if(0 > _s32FdScaler)
        {
            printf("Scaler device not opened!!!!\n");
            return FALSE;
        }
    }

    if (ioctl(_s32FdScaler, MDRV_XC_IOC_CFDCONCTRL, &stCFDCtrlInfo))
    {
        printf("MDRV_XC_IOC_CFDCONCTRL Get CFD version fail!!!!\n");
        return FALSE;
    }

#endif
#if (defined(MSOS_TYPE_LINUX_KERNEL) && defined(CONFIG_MSTAR_XC_HDR_SUPPORT))
    MDrv_XC_CFDControl(&stCFDCtrlInfo);
#endif
#endif
#endif

    return TRUE;
}

static MS_BOOL _MApi_XC_Init_WithoutCreateMutex(void *pInstance, XC_INITDATA *pXC_InitData, MS_U32 u32InitDataLen)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    pXCResourcePrivate->stdrvXC_MVideo.gu16VPixelShiftRange = 0;
    if(pXCResourcePrivate->stdrvXC_MVideo._bSkipSWReset == FALSE)
    {
        _XC_ENTRY(pInstance);
        MDrv_SC_SetSkipWaitVsync(pInstance, MAIN_WINDOW, TRUE);
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
        MDrv_SC_SetSkipWaitVsync(pInstance, SUB_WINDOW , TRUE);
#endif
        MDrv_XC_SoftwareReset(pInstance, REST_IP_ALL, MAIN_WINDOW, INPUT_SOURCE_NONE);

        // adc init
        MDrv_ADC_init(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32XTAL_Clock/1000UL
                      ,pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.bIsShareGround
                      ,pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.eScartIDPort_Sel);

        //mvop init
        //MBoot can not init MVOP here(MBoot does not register MVOP)
#if (!(defined(CONFIG_MBOOT)) && !(defined(MSOS_TYPE_OPTEE)))
        MDrv_MVOP_Init();
#endif

#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
        Hal_SC_enable_window(pInstance, FALSE,SUB_WINDOW);
#endif

        MDrv_XC_DisableInputSource(pInstance, ENABLE, MAIN_WINDOW, INPUT_SOURCE_NONE);
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
        MDrv_XC_DisableInputSource(pInstance, ENABLE, SUB_WINDOW, INPUT_SOURCE_NONE);
#endif
#if 0
#ifndef DONT_USE_CMA
#if (XC_SUPPORT_CMA ==TRUE)
        MS_BOOL ret= FALSE;

        if (
#if (HW_DESIGN_4K2K_VER == 7)
            (psXCInstPri->u32DeviceID == 0) &&
#endif
            pXCResourcePrivate->stdrvXC_MVideo._enPrevPowerState != E_POWER_SUSPEND)
        {
#if (HW_DESIGN_4K2K_VER == 6)
            gSrcInfo[eWindow].Status2.stXCConfigCMA[CMA_XC_SELF_MEM].u32HeapID = ION_XC_HEAP_ID;
#if defined(CHIP_MONET)
            gSrcInfo[eWindow].Status2.stXCConfigCMA[CMA_XC_COBUFF_MEM].u32HeapID = XC_DUAL_MIU_HID;
#else
            gSrcInfo[eWindow].Status2.stXCConfigCMA[CMA_XC_COBUFF_MEM].u32HeapID = XC_INVALID_HEAP_ID;
#endif
            gSrcInfo[eWindow].Status2.stXCConfigCMA[CMA_XC_SELF_MEM].u64AddrHeapOffset = 0;
            gSrcInfo[eWindow].Status2.stXCConfigCMA[CMA_XC_COBUFF_MEM].u64AddrHeapOffset = 0;
#endif
            /* 1. do CMA_POOL_INIT */
            CMA_Pool_Init_PARAM[CMA_XC_SELF_MEM].heap_id     = gSrcInfo[eWindow].Status2.stXCConfigCMA[CMA_XC_SELF_MEM].u32HeapID;
            CMA_Pool_Init_PARAM[CMA_XC_SELF_MEM].flags       = CMA_FLAG_MAP_VMA;
            MsOS_DelayTaskUs(1);
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], CMA self heapid = %d!\n",__func__, __LINE__,CMA_Pool_Init_PARAM[CMA_XC_SELF_MEM].heap_id);

            ret = MApi_CMA_Pool_Init(&CMA_Pool_Init_PARAM[CMA_XC_SELF_MEM]);

            if(ret == FALSE)
            {
                printf("\033[35m   Function = %s, Line = %d, CMA_POOL_INIT ERROR!!\033[m\n", __PRETTY_FUNCTION__, __LINE__);
                while(1);
                ////assert(0);
            }
            else
            {
                //printf("\033[35m  Function = %s, Line = %d, get pool_handle_id is %u\033[m\n", __PRETTY_FUNCTION__, __LINE__, CMA_Pool_Init_PARAM[CMA_XC_SELF_MEM].pool_handle_id);
                //printf("\033[35m  Function = %s, Line = %d, get miu is %u\033[m\n", __PRETTY_FUNCTION__,__LINE__, CMA_Pool_Init_PARAM[CMA_XC_SELF_MEM].miu);
                //printf("\033[35m  Function = %s, Line = %d, get heap_miu_start_offset is 0x%lx\033[m\n", __PRETTY_FUNCTION__, __LINE__, (MS_U32)CMA_Pool_Init_PARAM[CMA_XC_SELF_MEM].heap_miu_start_offset);
                //printf("\033[35m  Function = %s, Line = %d, get heap_length is 0x%X\033[m\n", __PRETTY_FUNCTION__, __LINE__, CMA_Pool_Init_PARAM[CMA_XC_SELF_MEM].heap_length);
            }

            if (gSrcInfo[eWindow].Status2.stXCConfigCMA[CMA_XC_COBUFF_MEM].u32HeapID != XC_INVALID_HEAP_ID)
            {
                /* 1. do CMA_POOL_INIT */
                CMA_Pool_Init_PARAM[CMA_XC_COBUFF_MEM].heap_id     = gSrcInfo[eWindow].Status2.stXCConfigCMA[CMA_XC_COBUFF_MEM].u32HeapID;
                CMA_Pool_Init_PARAM[CMA_XC_COBUFF_MEM].flags       = CMA_FLAG_MAP_VMA;
                MsOS_DelayTaskUs(1);
                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], CMA cobuff heapid = %d!\n",__func__, __LINE__,CMA_Pool_Init_PARAM[CMA_XC_COBUFF_MEM].heap_id);
                ret = MApi_CMA_Pool_Init(&CMA_Pool_Init_PARAM[CMA_XC_COBUFF_MEM]);

                if(ret == FALSE)
                {
                    printf("\033[35m   Function = %s, Line = %d, CMA_POOL_INIT ERROR!!\033[m\n", __PRETTY_FUNCTION__, __LINE__);
                    while(1);
                    //assert(0);
                }
                else
                {
                    //printf("\033[35m  Function = %s, Line = %d, get pool_handle_id is %u\033[m\n", __PRETTY_FUNCTION__, __LINE__, CMA_Pool_Init_PARAM[CMA_XC_SELF_MEM].pool_handle_id);
                    //printf("\033[35m  Function = %s, Line = %d, get miu is %u\033[m\n", __PRETTY_FUNCTION__,__LINE__, CMA_Pool_Init_PARAM[CMA_XC_SELF_MEM].miu);
                    //printf("\033[35m  Function = %s, Line = %d, get heap_miu_start_offset is 0x%lx\033[m\n", __PRETTY_FUNCTION__, __LINE__, (MS_U32)CMA_Pool_Init_PARAM[CMA_XC_SELF_MEM].heap_miu_start_offset);
                    //printf("\033[35m  Function = %s, Line = %d, get heap_length is 0x%X\033[m\n", __PRETTY_FUNCTION__, __LINE__, CMA_Pool_Init_PARAM[CMA_XC_SELF_MEM].heap_length);
                }
            }
        }
#endif
#endif
#endif

#if (XC_SUPPORT_IOMMU == TRUE)
        if(pXCResourcePrivate->stdrvXC_MVideo_Context.stMMAfeature.u32Support == MSOS_MMA_SUPPORT)
        {
            if(MsOS_MMA_Init() == FALSE)
            {
                printf("%s,%d MMA init fail\n",__FUNCTION__,__LINE__);
            }

#if(XC_MMA_ALLOC_USE_THREAD == TRUE)
            if(_Mdrv_XC_MMA_Task_Init(pInstance,MAIN_WINDOW) == FALSE)
            {
                printf("%s,%d Main window MMA tsk init fail\n",__FUNCTION__,__LINE__);
            }
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
            if(_Mdrv_XC_MMA_Task_Init(pInstance,SUB_WINDOW) == FALSE)
            {
                printf("%s,%d Sub window MMA tsk init fail\n",__FUNCTION__,__LINE__);
            }
#endif

#endif

        }
#endif

        //update init buffer size
        pXCResourcePrivate->stdrvXC_MVideo.au32XCSCMIMemSize[MAIN_WINDOW] = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FB_Size;
        pXCResourcePrivate->stdrvXC_MVideo.au32XCSCMIMemSize[SUB_WINDOW] = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FB_Size;

        // default frame buffer address init
        MDrv_XC_SetFrameBufferAddress(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FB_Start_Addr,
                                      pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FB_Size, E_XC_BUFFER_DEFAULT, MAIN_WINDOW);
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
        MDrv_XC_SetFrameBufferAddress(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FB_Start_Addr,
                                      pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FB_Size, E_XC_BUFFER_DEFAULT, SUB_WINDOW);
#endif
        // default frcm frame buffer address init
        MDrv_XC_SetFRCMFrameBufferAddressSilently(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FRCM_FB_Start_Addr,
                                              pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FRCM_FB_Size, MAIN_WINDOW);
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
        MDrv_XC_SetFRCMFrameBufferAddressSilently(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FRCM_FB_Start_Addr,
                                              pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FRCM_FB_Size, SUB_WINDOW);
#endif

#ifdef UFO_XC_SUPPORT_DUAL_MIU
        // default xc dual miu frame buffer address init
        MDrv_XC_SetDualFrameBufferAddress(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Dual_FB_Start_Addr,
                                      pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Dual_FB_Size, MAIN_WINDOW);
#endif
        MDrv_XC_init_fbn_win(pInstance, MAIN_WINDOW);
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
        MDrv_XC_init_fbn_win(pInstance, SUB_WINDOW);
#endif

        if (pXCResourcePrivate->stdrvXC_MVideo._enPrevPowerState != E_POWER_RESUME)
        {
            // in MApi_XC_SetPowerState_U2 function will set mcdi,so here is not needed
            //HW initial settting of MCDi is turned on (the address of MCDi is 0).
            //Driver should turn it off before Fclk on, otherwise it will start write whole memory.
            MDrv_SC_EnableMCDI(pInstance, DISABLE,E_XC_MCDI_BOTH); // Main Window
            MDrv_SC_EnableMCDI(pInstance, DISABLE,E_XC_MCDI_SUB_BOTH); // Sub Window
        }
        if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.bEnableIPAutoCoast)
        {
            // enable IP auto coast
            MDrv_XC_EnableIPAutoCoast(pInstance, ENABLE);
        }

        /**
            * !!! Attention !!! The ISR interface is different from Chakra2 and Utopia.
            * In Chakra2, there are two para needed, MHAL_SavedRegisters and vector.
            * In Utopia, there is no parameter.
            */
        if(pXCResourcePrivate->stdrvXC_MVideo._enPrevPowerState == E_POWER_MECHANICAL
#if 0
#ifndef MSOS_TYPE_LINUX_KERNEL
        || (pXCResourcePrivate->stdrvXC_MVideo._enPrevPowerState == E_POWER_SUSPEND)
#endif
#endif
        )
        {
#ifdef MSOS_TYPE_LINUX_KERNEL
            if(E_XC_DEVICE0 == psXCInstPri->u32DeviceID)
            {
                MsOS_AttachInterrupt_Shared(XC_MAIN_SC_IRQ,(InterruptCb) MDrv_SC_isr);
            }
            else if(E_XC_DEVICE1 == psXCInstPri->u32DeviceID)
            {
                MsOS_AttachInterrupt_Shared(E_INT_IRQ_DISP1,(InterruptCb) MDrv_SC1_isr);
            }
#else
            if(E_XC_DEVICE0 == psXCInstPri->u32DeviceID)
            {
                MsOS_AttachInterrupt(XC_MAIN_SC_IRQ,(InterruptCb) MDrv_SC_isr);
            }
            else if(E_XC_DEVICE1 == psXCInstPri->u32DeviceID)
            {
                MsOS_AttachInterrupt(E_INT_IRQ_DISP1,(InterruptCb) MDrv_SC1_isr);
            }
#endif
        }
        if(E_XC_DEVICE0 == psXCInstPri->u32DeviceID)
        {
            MsOS_EnableInterrupt(XC_MAIN_SC_IRQ);
            if(MDrv_XC_InterruptIsAnyoneAttached(pInstance, SC_INT_VSINT) == TRUE)
            {
                MDrv_SC_set_interrupt(pInstance, SC_INT_VSINT, ENABLE);
            }

        }
        else if(E_XC_DEVICE1 == psXCInstPri->u32DeviceID)
        {
            MsOS_EnableInterrupt(E_INT_IRQ_DISP1);
        }


        // Enable XC interrupts
        //MDrv_WriteByte(BK_SELECT_00, REG_BANK_VOP);
        //MDrv_WriteRegBit(L_BK_VOP(0x25), ENABLE, BIT(7));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK10_25_L, 0x80, BIT(7));

        // Daten FixMe, move from panel init to here, need to check why
        //MDrv_WriteByte(BK_SELECT_00, REG_BANK_S_VOP);
        //MDrv_WriteRegBit(L_BK_S_VOP(0x56), 1, BIT(1));  //Using new ovs_ref
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK0F_56_L, 0x02, BIT(1));

        //MDrv_WriteByte(BK_SELECT_00, REG_BANK_IP1F2);
        //MDrv_WriteByteMask(L_BK_IP1F2(0x09), 0x02, BIT(3) | BIT(2) | BIT(1) | BIT(0));  //YLock
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK01_09_L, 0x02, BIT(3) | BIT(2) | BIT(1) | BIT(0));
#if (XC_SUPPORT_FASTSWITCH_BY_IP1GP==TRUE)
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC0_09_L, 0x02, BIT(3) | BIT(2) | BIT(1) | BIT(0));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC1_09_L, 0x02, BIT(3) | BIT(2) | BIT(1) | BIT(0));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC2_09_L, 0x02, BIT(3) | BIT(2) | BIT(1) | BIT(0));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC3_09_L, 0x02, BIT(3) | BIT(2) | BIT(1) | BIT(0));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC4_09_L, 0x02, BIT(3) | BIT(2) | BIT(1) | BIT(0));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC5_09_L, 0x02, BIT(3) | BIT(2) | BIT(1) | BIT(0));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC6_09_L, 0x02, BIT(3) | BIT(2) | BIT(1) | BIT(0));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC7_09_L, 0x02, BIT(3) | BIT(2) | BIT(1) | BIT(0));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC8_09_L, 0x02, BIT(3) | BIT(2) | BIT(1) | BIT(0));
#endif
        //Extend the pixel of bottom A session at the right side [7:0]
        //Extend the pixel of bottom B session at the left side[15:8] for PIP issue
        SC_W2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK20_14_L, 0x2020);

        //HSYNC Tolerance for Mode Change
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK01_1D_L, 0x02, 0x1F);
#if (XC_SUPPORT_FASTSWITCH_BY_IP1GP==TRUE)
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC0_1D_L, 0x02, 0x1F);
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC1_1D_L, 0x02, 0x1F);
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC2_1D_L, 0x02, 0x1F);
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC3_1D_L, 0x02, 0x1F);
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC4_1D_L, 0x02, 0x1F);
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC5_1D_L, 0x02, 0x1F);
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC6_1D_L, 0x02, 0x1F);
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC7_1D_L, 0x02, 0x1F);
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKC8_1D_L, 0x02, 0x1F);
#endif

        //----------------------------
        // Enable clock
        //----------------------------

        // Enable sub window clock
#if 0
        if(_stXC_ApiInfo.u8MaxWindowNum > 1)
        {
            // Enable FIClk1
            MDrv_WriteByteMask(REG_CKG_FICLK_F1, CKG_FICLK_F1_FLK, CKG_FICLK_F1_MASK);      // select FClk first, if pre-scaling down -> change to CKG_FICLK_F1_IDCLK1
            MDrv_WriteRegBit(REG_CKG_FICLK_F1, DISABLE, CKG_FICLK_F1_INVERT);               // Not Invert
            MDrv_WriteRegBit(REG_CKG_FICLK_F1, DISABLE, CKG_FICLK_F1_GATED);                // Enable clock

            // Enable IDClk1
            MDrv_WriteByteMask(REG_CKG_IDCLK1, CKG_IDCLK1_XTAL, CKG_IDCLK1_MASK);           // initial to XTAL first, will be changed when switch input source
            MDrv_WriteRegBit(REG_CKG_IDCLK1, DISABLE, CKG_IDCLK1_INVERT);                   // Not Invert
            MDrv_WriteRegBit(REG_CKG_IDCLK1, DISABLE, CKG_IDCLK1_GATED);                    // Enable clock
        }
#endif
#ifdef K3_U2
        if(E_XC_DEVICE0 == psXCInstPri->u32DeviceID)
        {
            // Enable FIClk2
            MDrv_WriteByteMask(REG_CKG_FICLK_F2, CKG_FICLK_F2_FLK, CKG_FICLK_F2_MASK);          // select FClk first, if pre-scaling down -> change to CKG_FICLK_F1_IDCLK2
            MDrv_WriteRegBit(REG_CKG_FICLK_F2, DISABLE, CKG_FICLK_F2_INVERT);                   // Not Invert
            MDrv_WriteRegBit(REG_CKG_FICLK_F2, DISABLE, CKG_FICLK_F2_GATED);                    // Enable clock

            // Enable FClk
            MDrv_WriteByteMask(REG_CKG_FCLK, CKG_FCLK_170MHZ, CKG_FCLK_MASK);                   // select 170MHz, if not enough (for example, scaling bandwidth not enough), increase this
            MDrv_WriteRegBit(REG_CKG_FCLK, DISABLE, CKG_FCLK_INVERT);                           // Not Invert
            MDrv_WriteRegBit(REG_CKG_FCLK, DISABLE, CKG_FCLK_GATED);                            // Enable clock

            // Enable IDClk2
            MDrv_WriteByteMask(REG_CKG_IDCLK2, CKG_IDCLK2_XTAL, CKG_IDCLK2_MASK);               // initial to XTAL first, will be changed when switch input source
            MDrv_WriteRegBit(REG_CKG_IDCLK2, DISABLE, CKG_IDCLK2_INVERT);                       // Not Invert
            MDrv_WriteRegBit(REG_CKG_IDCLK2, DISABLE, CKG_IDCLK2_GATED);                        // Enable clock

            // Enable SIDClk2
            MDrv_WriteRegBit(REG_CKG_SIDCLK2, DISABLE, CKG_SIDCLK2_INVERT);
            MDrv_WriteRegBit(REG_CKG_SIDCLK2, DISABLE, CKG_SIDCLK2_GATED);
        }
        else //E_XC_DEVICE1 == pDeviceId->u32Id
        {
            // Enable FIClk2
            MDrv_WriteByteMask(REG_CKG_SC1_FECLK_F2, CKG_SC1_FECLK_F2_SC1_FCLK, CKG_SC1_FECLK_F2_MASK);          // select FClk first, if pre-scaling down -> change to CKG_FICLK_F1_IDCLK2
            MDrv_WriteRegBit(REG_CKG_SC1_FECLK_F2, DISABLE, CKG_SC1_FECLK_F2_INVERT);                   // Not Invert
            MDrv_WriteRegBit(REG_CKG_SC1_FECLK_F2, DISABLE, CKG_SC1_FECLK_F2_GATED);                    // Enable clock

            // Enable FClk
            MDrv_WriteByteMask(REG_CKG_SC1_FCLK, CKG_SC1_FCLK_123MHZ, CKG_SC1_FCLK_MASK);                   // select 170MHz, if not enough (for example, scaling bandwidth not enough), increase this
            MDrv_WriteRegBit(REG_CKG_SC1_FCLK, DISABLE, CKG_SC1_FCLK_INVERT);                           // Not Invert
            MDrv_WriteRegBit(REG_CKG_SC1_FCLK, DISABLE, CKG_SC1_FCLK_GATED);                            // Enable clock

            // Enable IDClk2
            MDrv_WriteByteMask(REG_CKG_SC1_IDCLK2, CKG_SC1_IDCLK2_XTAL, CKG_SC1_IDCLK2_MASK);               // initial to XTAL first, will be changed when switch input source
            MDrv_WriteRegBit(REG_CKG_SC1_IDCLK2, DISABLE, CKG_SC1_IDCLK2_INVERT);                       // Not Invert
            MDrv_WriteRegBit(REG_CKG_SC1_IDCLK2, DISABLE, CKG_SC1_IDCLK2_GATED);                        // Enable clock
            MDrv_Write2ByteMask(REG_CKG_SC1_FECLK2_F2, CKG_SC1_FECLK2_F2_SC1_FCLK, CKG_SC1_FECLK2_F2_MASK);  // enable ficlk2_f2

            // Enable SIDClk2
            MDrv_WriteRegBit(REG_CKG_SC1_SIDCLK2, DISABLE, CKG_SC1_SIDCLK2_INVERT);
            MDrv_WriteRegBit(REG_CKG_SC1_SIDCLK2, DISABLE, CKG_SC1_SIDCLK2_GATED);
        }
#else

#if(HW_DESIGN_4K2K_VER == 7)
        if(E_XC_DEVICE0 == psXCInstPri->u32DeviceID)
        {
            // Enable FIClk2
            MDrv_WriteByteMask(REG_CKG_FICLK_F2, CKG_FICLK_F2_FLK, CKG_FICLK_F2_MASK);          // select FClk first, if pre-scaling down -> change to CKG_FICLK_F1_IDCLK2
            MDrv_WriteRegBit(REG_CKG_FICLK_F2, DISABLE, CKG_FICLK_F2_INVERT);                   // Not Invert
            MDrv_WriteRegBit(REG_CKG_FICLK_F2, DISABLE, CKG_FICLK_F2_GATED);                    // Enable clock

            // Enable FClk
            MDrv_WriteByteMask(REG_CKG_FCLK, CKG_FCLK_DEFAULT, CKG_FCLK_MASK);                   // select 170MHz, if not enough (for example, scaling bandwidth not enough), increase this
            MDrv_WriteRegBit(REG_CKG_FCLK, DISABLE, CKG_FCLK_INVERT);                           // Not Invert
            MDrv_WriteRegBit(REG_CKG_FCLK, DISABLE, CKG_FCLK_GATED);                            // Enable clock

            // Enable IDClk2
            MDrv_WriteByteMask(REG_CKG_IDCLK2, CKG_IDCLK2_XTAL, CKG_IDCLK2_MASK);               // initial to XTAL first, will be changed when switch input source
            MDrv_WriteRegBit(REG_CKG_IDCLK2, DISABLE, CKG_IDCLK2_INVERT);                       // Not Invert
            MDrv_WriteRegBit(REG_CKG_IDCLK2, DISABLE, CKG_IDCLK2_GATED);                        // Enable clock

            // Enable EDClk
            Hal_SC_set_edclk(pInstance, CKG_EDCLK_F2_345MHZ, ENABLE, MAIN_WINDOW);
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
            Hal_SC_set_edclk(pInstance, CKG_EDCLK_F1_345MHZ, ENABLE, SUB_WINDOW);
#endif

            // Enable FMClk
            Hal_SC_set_fmclk(psXCInstPri, ENABLE);

            // Enable FIClk2_f2
            Hal_SC_set_ficlk2(pInstance, CKG_FICLK2_F2_FCLK, ENABLE, MAIN_WINDOW);

            //Enable ODCLK
            MDrv_WriteRegBit(REG_CKG_ODCLK, DISABLE, CKG_ODCLK_INVERT);                       // Not Invert
            MDrv_WriteRegBit(REG_CKG_ODCLK, DISABLE, CKG_ODCLK_GATED);
        }
#else

#if(HW_DESIGN_4K2K_VER == 4 || HW_DESIGN_4K2K_VER == 6)
        if(psXCInstPri->u32DeviceID == E_XC_DEVICE0)
#endif
        {
            // Enable FIClk2
            MDrv_WriteByteMask(REG_CKG_FICLK_F2, CKG_FICLK_F2_FLK, CKG_FICLK_F2_MASK);          // select FClk first, if pre-scaling down -> change to CKG_FICLK_F1_IDCLK2
            MDrv_WriteRegBit(REG_CKG_FICLK_F2, DISABLE, CKG_FICLK_F2_INVERT);                   // Not Invert
            MDrv_WriteRegBit(REG_CKG_FICLK_F2, DISABLE, CKG_FICLK_F2_GATED);                    // Enable clock

            // Enable FClk
            MDrv_WriteByteMask(REG_CKG_FCLK, CKG_FCLK_DEFAULT, CKG_FCLK_MASK);                   // select 170MHz, if not enough (for example, scaling bandwidth not enough), increase this
            MDrv_WriteRegBit(REG_CKG_FCLK, DISABLE, CKG_FCLK_INVERT);                           // Not Invert
            MDrv_WriteRegBit(REG_CKG_FCLK, DISABLE, CKG_FCLK_GATED);                            // Enable clock

            // Enable IDClk2
            MDrv_WriteByteMask(REG_CKG_IDCLK2, CKG_IDCLK2_XTAL, CKG_IDCLK2_MASK);               // initial to XTAL first, will be changed when switch input source
            MDrv_WriteRegBit(REG_CKG_IDCLK2, DISABLE, CKG_IDCLK2_INVERT);                       // Not Invert
            MDrv_WriteRegBit(REG_CKG_IDCLK2, DISABLE, CKG_IDCLK2_GATED);                        // Enable clock

            // Enable EDClk
            Hal_SC_set_edclk(pInstance, CKG_EDCLK_F2_345MHZ, ENABLE, MAIN_WINDOW);
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
            Hal_SC_set_edclk(pInstance, CKG_EDCLK_F1_345MHZ, ENABLE, SUB_WINDOW);
#endif

            // Enable FMClk
            Hal_SC_set_fmclk(psXCInstPri, ENABLE);

            // Enable FIClk2_f2
            Hal_SC_set_ficlk2(pInstance, CKG_FICLK2_F2_FCLK, ENABLE, MAIN_WINDOW);
        }
#if(HW_DESIGN_4K2K_VER == 4 || HW_DESIGN_4K2K_VER == 6)
        else
        {
            // Enable FIClk2
            MDrv_WriteByteMask(REG_CKG_S2_FICLK2_F2, CKG_S2_FICLK2_F2_FCLK, CKG_S2_FICLK2_F2_MASK);          // select FClk first, if pre-scaling down -> change to CKG_S2_FICLK_F1_IDCLK2
            MDrv_WriteRegBit(REG_CKG_S2_FICLK2_F2, DISABLE, CKG_S2_FICLK2_F2_INVERT);                   // Not Invert
            MDrv_WriteRegBit(REG_CKG_S2_FICLK2_F2, DISABLE, CKG_S2_FICLK2_F2_GATED);                    // Enable clock
            // Enable FClk
            MDrv_WriteByteMask(REG_CKG_S2_FCLK, CKG_S2_FCLK_DEFAULT, CKG_S2_FCLK_MASK);                   // select 170MHz, if not enough (for example, scaling bandwidth not enough), increase this
            MDrv_WriteRegBit(REG_CKG_S2_FCLK, DISABLE, CKG_S2_FCLK_INVERT);                           // Not Invert
            MDrv_WriteRegBit(REG_CKG_S2_FCLK, DISABLE, CKG_S2_FCLK_GATED);                            // Enable clock

            // Enable IDClk2
            MDrv_WriteByteMask(REG_CKG_S2_IDCLK2, CKG_S2_IDCLK2_XTAL, CKG_S2_IDCLK2_MASK);               // initial to XTAL first, will be changed when switch input source
            MDrv_WriteRegBit(REG_CKG_S2_IDCLK2, DISABLE, CKG_S2_IDCLK2_INVERT);                       // Not Invert
            MDrv_WriteRegBit(REG_CKG_S2_IDCLK2, DISABLE, CKG_S2_IDCLK2_GATED);                        // Enable clock
#if(HW_DESIGN_4K2K_VER == 6)
            // copy from halpnl.c init xc clk condition, to set the subwindow Odclk.
            if(MDrv_XC_IsSupportPipPatchUsingSc1MainAsSc0Sub())
            {
                if( ( E_XC_PNL_LPLL_VBY1_10BIT_16LANE == (MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type )||
                    ( E_XC_PNL_LPLL_VBY1_8BIT_16LANE == (MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type ) ||
                    ( E_XC_PNL_LPLL_VBY1_10BIT_1LANE == (MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type ) ||
                    ( E_XC_PNL_LPLL_VBY1_8BIT_1LANE == (MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type ) ||
                    ( E_XC_PNL_LPLL_TTL == (MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type ) ||
                    (( E_XC_PNL_LPLL_LVDS == (MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type ) &&
                    ( E_XC_MOD_OUTPUT_SINGLE == pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u8LPLL_Mode )) ||
                    (( E_XC_PNL_LPLL_HS_LVDS == (MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type ) &&
                    ( E_XC_MOD_OUTPUT_SINGLE == pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u8LPLL_Mode)))
                {
                    MDrv_WriteByteMask(REG_CKG_S2_ODCLK, 0x04, CKG_S2_ODCLK_MASK);
                }
                else
                {
                    MDrv_WriteByteMask(REG_CKG_S2_ODCLK, 0x0C, CKG_S2_ODCLK_MASK);
                }
            }
#endif

#ifdef MONACO_SC2_PATCH
            // Enable reg_sc2_odclk_div_en
            MDrv_WriteByteMask(0x101E2C , 0x0E, 0x0E);
            MDrv_WriteByteMask(0x101E30 , 0x11, 0x11);
#endif
        }
#endif
#endif

#ifdef UFO_XC_HDR
#if (UFO_XC_HDR_VERSION == 2)
        // Enable SPT4K_L2
        MDrv_WriteByteMask(REG_CKG_S2_GOP_HDR, CKG_S2_GOP_HDR_EDCLK, CKG_S2_GOP_HDR_MASK);
        MDrv_WriteRegBit(REG_CKG_S2_GOP_HDR, DISABLE, CKG_S2_GOP_HDR_INVERT); // Not Invert
        MDrv_WriteRegBit(REG_CKG_S2_GOP_HDR, DISABLE, CKG_S2_GOP_HDR_GATED);

        MDrv_WriteRegBit(REG_CKG_S2_MECLK, DISABLE, CKG_S2_MECLK_INVERT); // Not Invert
        MDrv_WriteRegBit(REG_CKG_S2_MECLK, DISABLE, CKG_S2_MECLK_GATED);

        MDrv_WriteRegBit(REG_CKG_S2_MGCLK, DISABLE, CKG_S2_MGCLK_INVERT); // Not Invert
        MDrv_WriteRegBit(REG_CKG_S2_MGCLK, DISABLE, CKG_S2_MGCLK_GATED);

        Hal_SC_HDR_CLK_Setting(pInstance);

        MS_U8 u8Version = 0;
        _MDrv_XC_GetCFDVersion(&u8Version);

        pXCResourcePrivate->stdrvXC_Display._u8CFDVersion = u8Version;
#endif
#endif

        MHal_XC_ClockControl(pInstance, ENABLE,CKG_SC_SLOW_DEFAULT);

#endif
        MDrv_SC_SetDisplay_LineBuffer_Mode(pInstance, ENABLE);
        MDrv_SC_SetDisplay_Start_Mode(pInstance, ENABLE);

        //Set line buffer merge address
        //This value will not changed. It is depend on line buffer size of main window.
        MDrv_SC_Set_LB_MergeAddress(pInstance);

#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
        //For pip, need to enable sub line buffer first
        MDrv_XC_FilLineBuffer(pInstance, ENABLE, SUB_WINDOW);
#endif

        // Set PAFRC mixed with noise dither disable---Move the control to pnl mod init
        //SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK24_3F_L, 0x0, 0x8);

        //extra fetch between main and sub
        //if the setting value is too big, hw cannot catch up
        //by CC, it's safe to set 1366x768 panel the same with 1920x1080 panel.
        Hal_SC_Set_extra_fetch_line(pInstance, 0x05);//H:extra fetch line
        Hal_SC_Set_extra_adv_line(pInstance, 0x03);  //V:extra advance line

        MDrv_SC_SetSkipWaitVsync(pInstance, MAIN_WINDOW, FALSE);
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
        MDrv_SC_SetSkipWaitVsync(pInstance, SUB_WINDOW , FALSE);
#endif

        // enable osd layer blending with main window
#if (VIDEO_OSD_SWITCH_VER > 1)
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK10_23_L, BIT(6), BIT(6));
#endif
#ifdef K3_U2
        //Mantis issue of 0266525 . Need to toggle IP write mask field count clear before input V sync .
        //Main window
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_07_L, BIT(4), BIT(4) |BIT(5) |BIT(6));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_07_L, 0, BIT(7));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_07_L, BIT(7), BIT(7));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_07_L, 0, BIT(7));
        //Sub window
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_47_L, BIT(4), BIT(4) |BIT(5) |BIT(6));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_47_L, 0, BIT(7));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_47_L, BIT(7), BIT(7));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_47_L, 0, BIT(7));
#endif
        //if mweEnable not be called, sub win will be open default, we need to black it at first.
        //so that to avoid garbage when opening sub win.
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK10_19_L, BIT(5), BIT(5));
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
        gSrcInfo[SUB_WINDOW].bBlackscreenEnabled = TRUE;
#else
        if(E_XC_DEVICE1 == psXCInstPri->u32DeviceID)
        {
            XC_RESOURCE_PRIVATE* pXCResourcePrivate_1 = NULL;
            UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(1)],(void**)(&pXCResourcePrivate_1));
            gSrcInfo_1[MAIN_WINDOW].bBlackscreenEnabled = TRUE;
        }
        else if(E_XC_DEVICE0 == psXCInstPri->u32DeviceID)
        {
            gSrcInfo[SUB_WINDOW].bBlackscreenEnabled = TRUE;
        }
#endif
#if SUPPORT_SEAMLESS_ZAPPING
        // for seamless zapping, this bit will maximize IP motion detection inside scaler
        // this motion setting will NOT override PQ's setting.
        // no need to be controlled by Qmap
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_45_L, BIT(9) , BIT(9));
#endif

        // this should only turned on when Top Bottom input with PreV scaling down enabled.
        // but the hardware's default value is ON so we turned off when inited.
        HAL_SC_Set_vsd_3D_autofactor_reset(pInstance, DISABLE, MAIN_WINDOW);

        // Enable IPM tune after DS feature
        MDrv_XC_EnableIPMTuneAfterDS(pInstance, ENABLE);

        //enable F2 IPM wbank freeze alginment mode //mantis:0747407
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_06_L, BIT(12) , BIT(12));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK32_06_L, BIT(12) , BIT(12));

        _XC_RETURN(pInstance);
    }
    else
    {
        // default frame buffer address init
        MDrv_XC_SetFrameBufferAddressSilently(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FB_Start_Addr,
                                              pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FB_Size, MAIN_WINDOW);
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
        MDrv_XC_SetFrameBufferAddressSilently(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FB_Start_Addr,
                                              pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FB_Size, SUB_WINDOW);
#endif
        // default frcm frame buffer address init
        MDrv_XC_SetFRCMFrameBufferAddressSilently(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FRCM_FB_Start_Addr,
                                              pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FRCM_FB_Size, MAIN_WINDOW);
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
        MDrv_XC_SetFRCMFrameBufferAddressSilently(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FRCM_FB_Start_Addr,
                                              pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FRCM_FB_Size, SUB_WINDOW);
#endif

#ifdef UFO_XC_SUPPORT_DUAL_MIU
        MDrv_XC_SetDualFrameBufferAddressSilently(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Dual_FB_Start_Addr,
                                              pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Dual_FB_Size, MAIN_WINDOW);
#endif

        //when from mboot to AP, there is an OS switch, so we need to do interrupt handle again.
        /**
         * !!! Attention !!! The ISR interface is different from Chakra2 and Utopia.
         * In Chakra2, there are two para needed, MHAL_SavedRegisters and vector.
         * In Utopia, there is no parameter.
         */
#ifdef MSOS_TYPE_LINUX_KERNEL
        if(E_XC_DEVICE0 == psXCInstPri->u32DeviceID)
        {
            MsOS_AttachInterrupt_Shared(XC_MAIN_SC_IRQ,(InterruptCb) MDrv_SC_isr);
        }
        else if(E_XC_DEVICE1 == psXCInstPri->u32DeviceID)
        {
            MsOS_AttachInterrupt_Shared(E_INT_IRQ_DISP1,(InterruptCb) MDrv_SC1_isr);
        }
#else
        if(E_XC_DEVICE0 == psXCInstPri->u32DeviceID)
        {
            MsOS_AttachInterrupt(XC_MAIN_SC_IRQ,(InterruptCb) MDrv_SC_isr);
        }
        else if(E_XC_DEVICE1 == psXCInstPri->u32DeviceID)
        {
            MsOS_AttachInterrupt(E_INT_IRQ_DISP1,(InterruptCb) MDrv_SC1_isr);
        }
#endif
        if(E_XC_DEVICE0 == psXCInstPri->u32DeviceID)
        {
            MsOS_EnableInterrupt(XC_MAIN_SC_IRQ);
        }
        else if(E_XC_DEVICE1 == psXCInstPri->u32DeviceID)
        {
            MsOS_EnableInterrupt(E_INT_IRQ_DISP1);
        }
    }

#if FRC_INSIDE
    // In A5 new feature,pre-arbiter can support (IPM_W and IPS_R)/(IPM_R and IPS_W)/(OP_R and OPW)  use the same MIU request client
    //Others, total 6 MIU request client

    // Current setting is : Turn off the max request function of pre-arbiter(2 to 1)
    SC_W2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK12_63_L,0x0A00); // 0xFF00 // miu merge arbiter IP0
    SC_W2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK12_68_L,0x0A00); // 0xFF00 // miu merge arbiter IP1
    SC_W2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK12_6A_L,0x1000); // 0xFF00 // miu merge arbiter OP
    MHal_FRC_3DLR_Select(pInstance, FALSE);
#endif

    //for HDMI AV mute, when AVMUTE = 1, HDMI will notice to XC to disable the input source on Main/Sub
    Hal_SC_Enable_AVMute(pInstance, MAIN_WINDOW);
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
    Hal_SC_Enable_AVMute(pInstance, SUB_WINDOW);
#endif

#if (HW_DESIGN_4K2K_VER == 4)
    // for HW Auto No signal can control FRCM freeze
    Hal_SC_set_frcm_to_freeze(pInstance, ENABLE, MAIN_WINDOW);
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0)
    Hal_SC_set_frcm_to_freeze(pInstance, ENABLE, SUB_WINDOW);
#endif

    // for 3D 6Tap
    Hal_SC_3D_set_top_win_6Tap(pInstance, ENABLE);
#endif

    Hal_SC_Init(pInstance);

    Hal_SC_set_T3D_H_size(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width);
#ifdef CONFIG_MSTAR_SRAMPD
    // To avoid mantis 1082875: boot logo flash issue, remove  SRAM PD for LD from XC_init to PNL_init
    //MHal_XC_Set_LD_SRAM_Power_Down(pInstance, TRUE);
    Hal_SC_EnableCLK_for_SUB(pInstance, FALSE);
    Hal_SC_Sub_SRAM_PowerDown_Control(pInstance,FALSE);
    MHal_XC_Set_ADC_SRAM_Power_Down(pInstance, TRUE);
#endif
    if(!(MHal_XC_Init_Patch(pInstance, ENABLE, MAIN_WINDOW)))
    {
        printf("no need to patch\n");
    }
//    MApi_XC_SetCompressionMode(ENABLE, MAIN_WINDOW);

#if defined(SUPPORT_ABF) && (ABF_VERSION == 1)
    MS_BOOL benableABF = TRUE;
    MDrv_XC_ABF_Control(pInstance,E_XC_ABF_CTRL_ENABLE,&benableABF);

    Hal_XC_MLoad_set_opm_lock(pInstance, OPM_LOCK_DS);
#endif
    MHal_XC_EnableDualAIDSource(pInstance,TRUE);

    if (MDrv_XC_IsSupportVRR())
    {
        PNL_ApiExtStatus sPnlExtStatus;
        memset(&sPnlExtStatus, 0, sizeof(PNL_ApiExtStatus));
        sPnlExtStatus.u16ApiStatusEX_Length = sizeof(PNL_ApiExtStatus);

        if (MApi_PNL_GetStatusEx(&sPnlExtStatus))
        {
            if (sPnlExtStatus.bVRR_en == TRUE)
            {
                MHal_SC_VRR_Init(pInstance);
            }
        }
    }

    return TRUE;
}

#if (defined(MSOS_TYPE_LINUX_KERNEL) && defined(CONFIG_MSTAR_XC_HDR_SUPPORT))
extern MS_BOOL KDrv_XC_HwInit(void);
#endif
MS_BOOL MDrv_XC_HwInit(void* pInstance)
{
    UNUSED(pInstance);
#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    if (ioctl(_s32FdScaler, MDRV_XC_IOC_XC_INIT, TRUE))
    {
        printf("MDRV_XC_IOC_XC_INIT fail!!!!\n");
        return E_APIXC_RET_FAIL;
    }
#endif
#if (defined(MSOS_TYPE_LINUX_KERNEL) && defined(CONFIG_MSTAR_XC_HDR_SUPPORT))
    if (KDrv_XC_HwInit() != TRUE)
    {
        printf("KDrv_XC_HwInit fail!!!!\n");
        return E_APIXC_RET_FAIL;
    }
#endif
    return TRUE;
}
#if (defined SUPPORT_BWD) && (!(defined XC_SUPPORT_BWR_USER_MODE))
static void MDrv_SC_BWR_mem_cfg_map_bpp(XC_RESOURCE_PRIVATE* pXCResourcePrivate, SCALER_WIN eWindow)
{
    gSrcInfo[eWindow].Status2.stMemCfgMap[E_XC_BWR_MEM_CONFIG_1].u16BWR_MEM_CFG_VALUE = 0x0;
    gSrcInfo[eWindow].Status2.stMemCfgMap[E_XC_BWR_MEM_CONFIG_1].eBitPerPixelType = E_XC_BWR_MEM_CFG_BPP_16;

    gSrcInfo[eWindow].Status2.stMemCfgMap[E_XC_BWR_MEM_CONFIG_2].u16BWR_MEM_CFG_VALUE = 0x4;
    gSrcInfo[eWindow].Status2.stMemCfgMap[E_XC_BWR_MEM_CONFIG_2].eBitPerPixelType = E_XC_BWR_MEM_CFG_BPP_20;

    gSrcInfo[eWindow].Status2.stMemCfgMap[E_XC_BWR_MEM_CONFIG_3].u16BWR_MEM_CFG_VALUE = 0x5;
    gSrcInfo[eWindow].Status2.stMemCfgMap[E_XC_BWR_MEM_CONFIG_3].eBitPerPixelType = E_XC_BWR_MEM_CFG_BPP_20;

    gSrcInfo[eWindow].Status2.stMemCfgMap[E_XC_BWR_MEM_CONFIG_4].u16BWR_MEM_CFG_VALUE = 0x6;
    gSrcInfo[eWindow].Status2.stMemCfgMap[E_XC_BWR_MEM_CONFIG_4].eBitPerPixelType = E_XC_BWR_MEM_CFG_BPP_20;

    gSrcInfo[eWindow].Status2.stMemCfgMap[E_XC_BWR_MEM_CONFIG_5].u16BWR_MEM_CFG_VALUE = 0x8;
    gSrcInfo[eWindow].Status2.stMemCfgMap[E_XC_BWR_MEM_CONFIG_5].eBitPerPixelType = E_XC_BWR_MEM_CFG_BPP_24;

    gSrcInfo[eWindow].Status2.stMemCfgMap[E_XC_BWR_MEM_CONFIG_6].u16BWR_MEM_CFG_VALUE = 0x9;
    gSrcInfo[eWindow].Status2.stMemCfgMap[E_XC_BWR_MEM_CONFIG_6].eBitPerPixelType = E_XC_BWR_MEM_CFG_BPP_24;

    gSrcInfo[eWindow].Status2.stMemCfgMap[E_XC_BWR_MEM_CONFIG_7].u16BWR_MEM_CFG_VALUE = 0xA;
    gSrcInfo[eWindow].Status2.stMemCfgMap[E_XC_BWR_MEM_CONFIG_7].eBitPerPixelType = E_XC_BWR_MEM_CFG_BPP_24;

    gSrcInfo[eWindow].Status2.stMemCfgMap[E_XC_BWR_MEM_CONFIG_8].u16BWR_MEM_CFG_VALUE = 0xB;
    gSrcInfo[eWindow].Status2.stMemCfgMap[E_XC_BWR_MEM_CONFIG_8].eBitPerPixelType = E_XC_BWR_MEM_CFG_BPP_24;
}
#endif
//-------------------------------------------------------------------------------------------------
/// Initialize the XC
/// If the length is 0, it won't do anything except the mutex and share memory. Usually it is the action of DFB
/// @param  pXC_InitData                  \b IN: the Initialized Data
/// @param  u32InitDataLen                \b IN: the length of the initialized data
/// @return @ref MS_BOOL
//-------------------------------------------------------------------------------------------------
#endif
MS_BOOL MApi_XC_Init_U2(void* pInstance, XC_INITDATA *pXC_InitData, MS_U32 u32InitDataLen)
{
    char wordISR[] = {"_XC_ISR_Mutex"};


#if(XC_CONTEXT_ENABLE)
    MS_BOOL bFirstInstance = TRUE;
#endif
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    MS_U8 u8VerCharIndex = 0 ;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "[XC,Version]\33[0;36m ");
    for (; u8VerCharIndex < 8 ; u8VerCharIndex++)
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "%c",_api_xc_version.DDI.change[u8VerCharIndex]);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "\n \33[m");
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
#ifdef MSOS_TYPE_LINUX_KERNEL
    init_waitqueue_head(&pXCResourcePrivate->stXC_Utility._XC_EventQueue);
#endif

#if(XC_CONTEXT_ENABLE)
    _MApi_XC_Init_Context(pInstance, &bFirstInstance);
    if (!bFirstInstance)
    {
        //The XC instance already exists
        //return TRUE;
    }

    if (&(pXCResourcePrivate->stdrvXC_MVideo._SContext) == NULL || &(pXCResourcePrivate->stdrvXC_MVideo._SCShared) == NULL)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "[MAPI XC][%06d] create context fail\n", __LINE__);
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        return FALSE;
    }

#endif

    if (_XC_Mutex == -1)
    {
        _XC_Mutex = MsOS_CreateMutex(E_MSOS_FIFO, "_XC_Mutex", MSOS_PROCESS_SHARED);
        if (_XC_Mutex == -1)
        {
            printf("[MAPI XC]_XC_Mutex create mutex fail\n");
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "[MAPI XC][%06d] create mutex fail\n", __LINE__);
            XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            return FALSE;
        }
    }

    if (_XC_ISR_Mutex == -1)
    {
        _XC_ISR_Mutex = MsOS_CreateMutex(E_MSOS_FIFO, wordISR, MSOS_PROCESS_SHARED);
        if (_XC_ISR_Mutex == -1)
        {
            printf("[MAPI XC]_XC_ISR_Mutex create mutex fail\n");
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "[MAPI XC][%06d] create ISR mutex fail\n", __LINE__);
            XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            return FALSE;
        }
    }

    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    // This should be the first function. get MMIO base
    if (MDrv_XC_SetIOMapBase_i(pInstance) != TRUE)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "MApi_XC_Init MDrv_XC_SetIOMapBase() failure\n");
    }
#ifndef MSOS_TYPE_NUTTX
    MDrv_XC_SetDeviceOffset(pInstance);
#endif

    // If the length is 0, it won't do anything except the mutex and share memory. Usually it is the action of DFB
    // if the _bDFBInit is TRUE, it also indicates DFB init case
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
#ifndef MSOS_TYPE_OPTEE
    if((0 == u32InitDataLen) || (TRUE == pXCResourcePrivate->stdrvXC_MVideo._bDFBInit))
#endif
    {
        pXCResourcePrivate->stdrvXC_MVideo._bDFBInit = FALSE;
#ifndef MSOS_TYPE_NUTTX
        mvideo_sc_variable_init(pInstance, bFirstInstance, NULL);
#endif
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "MApi_XC_Init(): the u32InitDataLen is zero or DFB init case.\n");
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return TRUE;
    }
#ifndef MSOS_TYPE_NUTTX
    MDrv_XC_FPLLCusReset(pInstance);

    pXCResourcePrivate->stdrvXC_MVideo_Context._bEnableEuro = FALSE;//Euro HDTV support flag
#ifndef MSOS_TYPE_OPTEE
    if(MApi_PNL_Get_TCON_Capability())
    {
        if (pXC_InitData->stPanelInfo.eLPLL_Type >= E_XC_PNL_LPLL_EXT)
        {
            pXC_InitData->stPanelInfo.eLPLL_Type = (E_XC_PNL_LPLL_TYPE)MApi_Pnl_Get_LPLL_Type();
        }
        printf("%s, %d, pXC_InitData->stPanelInfo.eLPLL_Type=%d\n", __FUNCTION__, __LINE__, pXC_InitData->stPanelInfo.eLPLL_Type);
    }
#endif
    #ifdef MSOS_TYPE_LINUX_KERNEL
    if(is_compat_task())
    {
        memcpy(&pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData, pXC_InitData, sizeof(XC_INITDATA));
    }
    else
    {
        memcpy(&pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData, pXC_InitData, ((u32InitDataLen>=sizeof(XC_INITDATA))? sizeof(XC_INITDATA):u32InitDataLen));
    }
    #else
    memcpy(&pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData, pXC_InitData, ((u32InitDataLen>=sizeof(XC_INITDATA))? sizeof(XC_INITDATA):u32InitDataLen));
    #endif
    pXCResourcePrivate->stdrvXC_MVideo_Context.g_u32InitDataLen_Suspend = u32InitDataLen;
#if defined (__aarch64__)
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "Main_FB(start, size)=(%lx, %lu)\n", pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FB_Start_Addr,
                                                                             pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FB_Size);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "Sub_FB (start, size)=(%lx, %lu)\n", pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FB_Start_Addr,
                                                                             pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FB_Size);
#else
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "Main_FB(start, size)=(%tx, %tu)\n", (ptrdiff_t)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FB_Start_Addr,
                                                                             (ptrdiff_t)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FB_Size);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "Sub_FB (start, size)=(%tx, %tu)\n", (ptrdiff_t)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FB_Start_Addr,
                                                                             (ptrdiff_t)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FB_Size);
#endif

    // Scart ID port selection is available after version 1.
    if (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32XC_version < 1)
    {
        pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.eScartIDPort_Sel = E_XC_SCARTID_NONE;
    }

    mvideo_sc_variable_init(pInstance, bFirstInstance, pXC_InitData);

    // For MI multi init case,we should confirm enPrevPowerState = E_POWER_MECHANICAL
    // after STR
    pXCResourcePrivate->stdrvXC_MVideo._enPrevPowerState = E_POWER_MECHANICAL;

    _MApi_XC_Init_WithoutCreateMutex(pInstance, pXC_InitData, u32InitDataLen);
    if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_bEnableCusSTR == TRUE)
    {
        MDrv_XC_HwInit(pInstance);
    }
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    //MApi_XC_SetDbgLevel(XC_DBGLEVEL_OFF);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    //map memory config to bit per pix
#if (defined SUPPORT_BWD) && (!(defined XC_SUPPORT_BWR_USER_MODE))
    MDrv_SC_BWR_mem_cfg_map_bpp(pXCResourcePrivate, MAIN_WINDOW);
#endif
    pXCResourcePrivate->stdrvXC_MVideo.bModuleInited = TRUE;
    PNL_ApiExtStatus sPnlExtStatus;
    memset(&sPnlExtStatus, 0, sizeof(PNL_ApiExtStatus));
    sPnlExtStatus.u16ApiStatusEX_Length = sizeof(PNL_ApiExtStatus);
    if(MApi_PNL_GetStatusEx(&sPnlExtStatus))
    {
        if(sPnlExtStatus.bIsPanelDCLKUpdate == TRUE)
        {
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u32MaxSET = sPnlExtStatus.u32MaxSETfromMaxDCLK;
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u32MinSET = sPnlExtStatus.u32MinSETfromMinDCLK;
        }

        if(sPnlExtStatus.bIsTCONBinEn && IsTCON(sPnlExtStatus.u32TCON_PanelLinkExtType))
        {
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type  = sPnlExtStatus.u32TCON_PanelLinkExtType;

            //If Set is not calculate by Dclk, it needs to copy from bin
            if(sPnlExtStatus.bIsPanelDCLKUpdate == FALSE)
            {
                pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u32MaxSET = sPnlExtStatus.u32TCON_PanelMaxSET;
                pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u32MinSET = sPnlExtStatus.u32TCON_PanelMinSET;
            }
        }
    }
#endif

    //reset HFR_IP2 hw default setting
    if(MHal_XC_Is_Support_HFR_Path())
    {
        MHal_SC_HFR_IP2_Init(pInstance);
    }

    return TRUE;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_Init(XC_INITDATA *pXC_InitData, MS_U32 u32InitDataLen)
{
    CheckXCInstanceOpen();

#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
     if (pu32XCInst_1 == NULL)
     {
        XC_INSTANT_ATTRIBUTE stXCInstantAttribute;
        INIT_XC_INSTANT_ATTR(stXCInstantAttribute);

        stXCInstantAttribute.u32DeviceID = 1;
        if(UtopiaOpen(MODULE_XC, &pu32XCInst_1, 0, &stXCInstantAttribute) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return E_APIXC_RET_FAIL;
        }
      }
#endif
    stXC_INIT XCArgs;
    XCArgs.pXC_InitData = pXC_InitData;
    XCArgs.u32InitDataLen = u32InitDataLen;
    XCArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_INIT, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Get the XC init config
/// @param  pXC_InitData                  \b IN: the Initialized Data
/// @return @ref E_APIXC_ReturnValue
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_GetConfig_U2(void* pInstance, XC_INITDATA *pXC_InitData)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    if(pXCResourcePrivate->stdrvXC_MVideo.bModuleInited)
    {
        memcpy(pXC_InitData, &pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData, sizeof(XC_INITDATA));
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        return E_APIXC_RET_OK;
    }
    else
    {
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        return E_APIXC_RET_FAIL;
    }
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_GetConfig(XC_INITDATA *pXC_InitData)
{
    if (pu32XCInst == NULL)
    {
        if(UtopiaOpen(MODULE_XC, &pu32XCInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return E_APIXC_RET_FAIL;
        }
    }

    stXC_GET_CONFIG XCArgs;
    XCArgs.pXC_InitData = pXC_InitData;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;
    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_CONFIG, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("XC engine GET_CONFIG Ioctl fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
#ifndef MSOS_TYPE_NUTTX
E_APIXC_ReturnValue MDrv_XC_Init_MISC(void *pInstance, XC_INITMISC *pXC_Init_Misc, MS_U32 u32InitMiscDataLen)
{

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    MST_PANEL_INFO_t stFRCPanelInfo;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    if(u32InitMiscDataLen != sizeof(XC_INITMISC))
    {
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return E_APIXC_RET_FAIL;
    }

#if (SUPPORT_IMMESWITCH == 0)
    if (pXC_Init_Misc->u32MISC_A & E_XC_INIT_MISC_A_IMMESWITCH )
    {
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return E_APIXC_RET_FAIL;
    }
#endif

#if (SUPPORT_DVI_AUTO_EQ == 0)
    if (pXC_Init_Misc->u32MISC_A & E_XC_INIT_MISC_A_DVI_AUTO_EQ)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return E_APIXC_RET_FAIL;
    }
#endif

#if (FRC_INSIDE == 0)
    if ((pXC_Init_Misc->u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE) || (pXC_Init_Misc->u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE_60HZ))
    {
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

        return E_APIXC_RET_FAIL;
    }
#endif

#if FRC_INSIDE
    #if (defined(HW_SUPPORT_FRC_DUAL_MIU) && (HW_SUPPORT_FRC_DUAL_MIU == 0))
        if (pXC_Init_Misc->u32MISC_A & E_XC_INIT_MISC_A_FRC_DUAL_MIU)
        {
            pXC_Init_Misc->u32MISC_A &= ~E_XC_INIT_MISC_A_FRC_DUAL_MIU;
            XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "HW NOT support FRC dual MIU! Keep single MIU HERE!\n");
        }
    #endif
#endif

    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    // Start only when all setting ready.
    memset(&stFRCPanelInfo, 0, sizeof(MST_PANEL_INFO_t));

    memcpy(&pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc, pXC_Init_Misc, u32InitMiscDataLen);
    pXCResourcePrivate->stdrvXC_MVideo_Context.g_u32InitMiscDataLen_Suspend = u32InitMiscDataLen;

    if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FAST_GET_VFREQ)
    {
        pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFast_Get_VFreq = TRUE;
    }
    else
    {
        pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFast_Get_VFreq = FALSE;
    }

    #if (FRC_INSIDE)
    if(((pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE)
        || (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE_60HZ)
        || (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE_4K1K_120HZ)
        || (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE_4K_HALFK_240HZ)
        || (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE_KEEP_OP_4K2K)
        || (IsVBY1_16LANE((MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type)))
        #if (HW_DESIGN_4K2K_VER == 6)
        && (TRUE == MHal_FRC_IsSupportFRC_byEfuse(pInstance))
        #endif
        )
    {
        if (FALSE == pXCResourcePrivate->stdrvXC_MVideo_Context.bIsFRCInited)
        {
            MHal_CLKGEN_FRC_Init(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u8LPLL_Mode);
            MDrv_FRC_PNLInfo_Transform(pInstance, &pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo,
                                   &pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc,
                                   &stFRCPanelInfo);

            if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.bFRC) // init ready?! if yes, continue
            {
                MDrv_FRC_Init(pInstance, &stFRCPanelInfo, &(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo));
                gSrcInfo[MAIN_WINDOW].Status2.bInFRCMode = TRUE; //Store FRC enable state in share memory for multi-process
                gSrcInfo[SUB_WINDOW].Status2.bInFRCMode = gSrcInfo[MAIN_WINDOW].Status2.bInFRCMode;
                MHal_FRC_3DLR_Select(pInstance, TRUE);
            }

            // when OUTPUT is 4k2k@60Hz then we turn on FRC
            // in other case , we stil do FRC init but we choose to bypass FRC
            if ((pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16DefaultVFreq < 600)
                && (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width == 3840)
                && (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height == 2160))
            {
                MDrv_FRC_ByPass_Enable(pInstance, TRUE);
            }
            else
            {
                MDrv_FRC_ByPass_Enable(pInstance, FALSE);
            }

            pXCResourcePrivate->stdrvXC_MVideo_Context.bIsFRCInited = TRUE;
        }

        if (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE_KEEP_OP_4K2K)
        {
            // SW patch here, if AP set keep OP as 4K2K, need to adjust DE and Htt before FRC setting is 4K2K
            if (SC_R2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK10_0C_L) < 3000
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB//follow HW_DESIGN_4K2K_VER = 6 rule
               && (psXCInstPri->u32DeviceID == 0)
#endif
            )
            {
                if(MDrv_XC_MLoad_GetStatus(pInstance) == E_MLOAD_ENABLED)
                {
                    _MLOAD_ENTRY(pInstance);

                    MDrv_XC_MLoad_WriteCmd(pInstance, REG_SC_BK10_0C_L, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HTotal, VOP_HTT_MASK);
                    MDrv_XC_MLoad_WriteCmd(pInstance, REG_SC_BK10_05_L, (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HStart+pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width - 1), VOP_DE_HEND_MASK);

                    MDrv_XC_MLoad_Fire(pInstance, TRUE);
                    _MLOAD_RETURN(pInstance);
                }
                else
                {
                    SC_W2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK10_0C_L, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HTotal);
                    SC_W2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK10_05_L, (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HStart+pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width - 1));
                }
            }
        }

        if(MHal_XC_Is_Support4K2K120Hz_2pMode((MS_U16)(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type))
            && ((pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE) == FALSE))
        {
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A |= E_XC_INIT_MISC_A_FRC_INSIDE;
            XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Support E_XC_INIT_MISC_A_FRC_INSIDE when output 120hz!\n");
        }

        //printf("\n## FRC %s mode\n",g_XC_Pnl_Misc.FRCInfo.bFRC?"NORMAL":"BYPASS");
        //printf("## FRC LPLL mode =%d\n\n",g_XC_InitData.stPanelInfo.u8LPLL_Mode);
        //printf("[%s][%d]\n",__FUNCTION__,__LINE__);
    }
    else
    {
        //for need stgen or tgen-lite,but no FRC
#if (FRC_INSIDE)
        #if (HW_DESIGN_4K2K_VER == 6)
        if (FALSE == MHal_FRC_IsSupportFRC_byEfuse(pInstance))
        #endif
        {
            if (FALSE == pXCResourcePrivate->stdrvXC_MVideo_Context.bIsFRCInited)
            {
                MDrv_FRC_PNLInfo_Transform(pInstance, &pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo,
                                           &pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc,
                                           &stFRCPanelInfo);
                MDrv_FRC_Init(pInstance, &stFRCPanelInfo, &(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo));
                pXCResourcePrivate->stdrvXC_MVideo_Context.bIsFRCInited = TRUE;
            }
        }
#endif

        MDrv_FRC_ByPass_Enable(pInstance, TRUE);
    }
    #endif
#if (HW_DESIGN_4K2K_VER == 4)
    if (((psXCInstPri->u32DeviceID == 0) && (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_LEGACY_MODE))
         ||(psXCInstPri->u32DeviceID == 1))
    {
        MDrv_SC_Enable_LegacyMode(pInstance, TRUE);
    }
    else
    {
        MDrv_SC_Enable_LegacyMode(pInstance, FALSE);
    }
#endif
    if (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_SWTICH_MVOP_TO_XC_PATH)
    {
        pXCResourcePrivate->stdrvXC_MVideo_Context.g_bSwitchMVOPToSCPath = TRUE;
    }
    else
    {
        pXCResourcePrivate->stdrvXC_MVideo_Context.g_bSwitchMVOPToSCPath = FALSE;
    }

    if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_ONLY_FREEZE_SCMI)
    {
        pXCResourcePrivate->stdrvXC_MVideo_Context.bOnlyFreezeSCMI = TRUE;
    }
    else
    {
        pXCResourcePrivate->stdrvXC_MVideo_Context.bOnlyFreezeSCMI = FALSE;
    }

#ifdef M7821_BRING_UP_PATCH
    pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_B |= E_XC_INIT_MISC_B_SKIP_SR;
#endif

    if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_B & E_XC_INIT_MISC_B_DISABLE_FSS)
    {
        MDrv_XC_SetFSSEnable(pInstance, DISABLE, MAIN_WINDOW);
    }
    else
    {
        if(pXCResourcePrivate->stdrvXC_MVideo_Context.bIsFSSEnabled == FALSE)
        {
            MDrv_XC_SetFSSEnable(pInstance, ENABLE, MAIN_WINDOW);
        }
    }

    //printf("[%s][%d]\n",__FUNCTION__,__LINE__);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return E_APIXC_RET_OK;
}
#ifndef MSOS_TYPE_NUTTX
E_APIXC_ReturnValue MApi_XC_Init_MISC_U2(void* pInstance, XC_INITMISC *pXC_Init_Misc, MS_U32 u32InitMiscDataLen)
{
    E_APIXC_ReturnValue eReturn = E_APIXC_RET_FAIL;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    eReturn = MDrv_XC_Init_MISC(pInstance, pXC_Init_Misc, u32InitMiscDataLen);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return eReturn;
}
E_APIXC_ReturnValue MApi_XC_Init_MISC(XC_INITMISC *pXC_Init_Misc, MS_U32 u32InitMiscDataLen)
{
    if (pu32XCInst == NULL
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
        || pu32XCInst_1 == NULL
#endif
        )
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_INIT_MISC XCArgs;
    XCArgs.pXC_Init_Misc = pXC_Init_Misc;
    XCArgs.u32InitMiscDataLen = u32InitMiscDataLen;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if((UtopiaIoctl(pu32XCInst, E_XC_CMD_INIT_MISC, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
        || (UtopiaIoctl(pu32XCInst_1, E_XC_CMD_INIT_MISC, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
#endif
        )
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
E_APIXC_ReturnValue MDrv_XC_GetMISCStatus(void* pInstance, XC_INITMISC *pXC_Init_Misc)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    memcpy(pXC_Init_Misc,&pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc,sizeof(XC_INITMISC));

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return E_APIXC_RET_OK;
}
E_APIXC_ReturnValue MApi_XC_GetMISCStatus_U2(void* pInstance, XC_INITMISC *pXC_Init_Misc)
{
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    E_APIXC_ReturnValue eReturn = E_APIXC_RET_FAIL;

    eReturn = MDrv_XC_GetMISCStatus(pInstance, pXC_Init_Misc);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    return eReturn;
}
E_APIXC_ReturnValue MApi_XC_GetMISCStatus(XC_INITMISC *pXC_Init_Misc)
{
    if (pu32XCInst == NULL)
    {
        if(UtopiaOpen(MODULE_XC, &pu32XCInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return E_APIXC_RET_FAIL;
        }
    }
    static XC_INITMISC tmp_Init_Misc;
    stXC_GET_MISC_STATUS XCArgs;
    memcpy(&tmp_Init_Misc,pXC_Init_Misc,sizeof(XC_INITMISC));
    XCArgs.pXC_Init_Misc = &tmp_Init_Misc;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_MISC_STATUS, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        memcpy(pXC_Init_Misc,&tmp_Init_Misc,sizeof(XC_INITMISC));
        return XCArgs.eReturnValue;
    }
}
E_APIXC_ReturnValue MApi_XC_GetChipCaps_U2(void* pInstance, E_XC_CAPS eCapType, MS_U32* pRet, MS_U32 ret_size)
{
    E_APIXC_ReturnValue eReturn = E_APIXC_RET_FAIL;

    eReturn = MDrv_XC_GetChipCaps(pInstance, eCapType, pRet, ret_size);

    return eReturn;
}
E_APIXC_ReturnValue MApi_XC_GetChipCaps(E_XC_CAPS eCapType, MS_U32* pRet, MS_U32 ret_size)
{

    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_GET_CHIP_CAPS XCArgs;
    XCArgs.eCapType = eCapType;
    XCArgs.pRet = pRet;
    XCArgs.ret_size = ret_size;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_CHIPCAPS, (void*)&XCArgs) != 0)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
MS_U32 MApi_XC_GetCapability_U2(void* pInstance, MS_U32 u32Id)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    MS_U32 u32Val = 0;

    switch (u32Id)
    {
        case E_XC_SUPPORT_IMMESWITCH:
            MDrv_XC_GetChipCaps(pInstance, E_XC_IMMESWITCH,&u32Val,sizeof(u32Val));
            break;

        case E_XC_SUPPORT_DVI_AUTO_EQ:
            MDrv_XC_GetChipCaps(pInstance, E_XC_DVI_AUTO_EQ,&u32Val,sizeof(u32Val));
            break;

        case  E_XC_SUPPORT_FRC_INSIDE:
            MDrv_XC_GetChipCaps(pInstance ,E_XC_FRC_INSIDE,&u32Val,sizeof(u32Val));
            break;
        default:
            u32Val = 0;
            break;
    }
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return u32Val;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_U32 MApi_XC_GetCapability(MS_U32 u32Id)
{
    CheckXCInstanceOpen();

#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
     if (pu32XCInst_1 == NULL)
     {
        XC_INSTANT_ATTRIBUTE stXCInstantAttribute;
        INIT_XC_INSTANT_ATTR(stXCInstantAttribute);

        stXCInstantAttribute.u32DeviceID = 1;
        if(UtopiaOpen(MODULE_XC, &pu32XCInst_1, 0, &stXCInstantAttribute) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return E_APIXC_RET_FAIL;
        }
      }
#endif
    stXC_GET_CAPABILITY XCArgs;
    XCArgs.u32Id = u32Id;
    XCArgs.u32ReturnValue = 0;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_CAPABILITY, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return 0;
    }
    else
    {
        return XCArgs.u32ReturnValue;
    }
}
#endif

//-------------------------------------------------------------------------------------------------
/// Enable sub window clock.
//-------------------------------------------------------------------------------------------------
void MApi_XC_EnableCLK_for_SUB(void *pInstance, MS_BOOL bEnable)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    MDrv_XC_EnableCLK_for_SUB(pInstance, bEnable);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}

//-------------------------------------------------------------------------------------------------
/// Enable DIP window clock.
//-------------------------------------------------------------------------------------------------
void MApi_XC_EnableCLK_for_DIP(void *pInstance, MS_BOOL bEnable)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    MDrv_SC_EnableCLK_for_DIP(pInstance, bEnable);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}

//-------------------------------------------------------------------------------------------------
/// This function will change panel type dynamically
/// @param  pstPanelInfo      \b IN: the new panel type
//-------------------------------------------------------------------------------------------------
void MApi_XC_ChangePanelType_U2(void* pInstance, XC_PANEL_INFO *pstPanelInfo)
{
    XC_PANEL_INFO_EX stPanelInfoEx;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    //Reset information used by ex panel info
    memset(&stPanelInfoEx, 0, sizeof(XC_PANEL_INFO_EX));
    stPanelInfoEx.u16VFreq = PANEL_INFO_EX_INVALID_ALL;
    stPanelInfoEx.u32PanelInfoEx_Version = PANEL_INFO_EX_VERSION;
    stPanelInfoEx.u16PanelInfoEX_Length = sizeof(XC_PANEL_INFO_EX);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
    if (psXCInstPri->u32DeviceID == 1)
    {
        /// not support
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        return;
    }
    CHECK_DEVICE1_INSTANCE();
    XC_INSTANCE_PRIVATE *psXCInstPri_1 = NULL;
    UtopiaInstanceGetPrivate(g_pDevice1Instance, (void**)&psXCInstPri_1);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate_1 = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri_1->u32DeviceID)],(void**)(&pXCResourcePrivate_1));
#endif
    MDrv_XC_SetExPanelInfo(pInstance, FALSE, &stPanelInfoEx);
    _XC_ENTRY(pInstance);
    MDrv_XC_GetDefaultHVSyncInfo(pInstance);
    _XC_RETURN(pInstance);

#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
    MDrv_XC_SetExPanelInfo(g_pDevice1Instance, FALSE, &stPanelInfoEx);
    _XC_ENTRY(g_pDevice1Instance);
    MDrv_XC_GetDefaultHVSyncInfo(g_pDevice1Instance);
    _XC_RETURN(g_pDevice1Instance);
#endif

    if (pstPanelInfo->eLPLL_Type == E_XC_PNL_LPLL_EXT)
    {
        pstPanelInfo->eLPLL_Type = (E_XC_PNL_LPLL_TYPE)MApi_Pnl_Get_LPLL_Type();
    }
    //printf("%s, %d, pstPanelInfo->eLPLL_Type=%d\n", __FUNCTION__, __LINE__, pstPanelInfo->eLPLL_Type);

    memcpy(&pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo, pstPanelInfo, sizeof(XC_PANEL_INFO));
    memcpy(&(gSrcInfo[MAIN_WINDOW].Status2.stXCPanelDefaultTiming), &(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo), sizeof(XC_PANEL_TIMING));
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
    memcpy(&pXCResourcePrivate_1->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo, pstPanelInfo, sizeof(XC_PANEL_INFO));
    memcpy(&(gSrcInfo_1[MAIN_WINDOW].Status2.stXCPanelDefaultTiming), &(pXCResourcePrivate_1->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo), sizeof(XC_PANEL_TIMING));
#endif
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_ChangePanelType(XC_PANEL_INFO *pstPanelInfo)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_CHANGE_PANELTYPE XCArgs;
    XCArgs.pstPanelInfo = pstPanelInfo;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_CHANGE_PANELTYPE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// This function will let scaler driver know if to decide best pre-scaling down ratio based on FB size of not.
/// Please set this before call MApi_XC_SetWindow()
/// @param  bEnable      \b IN: enable this feature or not
//-------------------------------------------------------------------------------------------------
void MApi_XC_SetAutoPreScaling_U2(void* pInstance, MS_BOOL bEnable, SCALER_WIN eWindow)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    gSrcInfo[eWindow].Status2.bAutoBestPreScaling = bEnable;
    //printf("MApi_XC_SetAutoPreScaling(): obsolete interface\n");
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_SetAutoPreScaling(MS_BOOL bEnable, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_AUTO_PRESCALING XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.eWindow = eWindow;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_AUTO_PRESCALING, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// This function will enable/diable output black pattern
/// @param  bEnable      \b IN: TRUE: enable, FALSE: otherwise
//-------------------------------------------------------------------------------------------------
void MApi_XC_Set_BLSK_U2(void* pInstance, MS_BOOL bEnable)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);
    MDrv_SC_Set_BLSK(pInstance, bEnable);
    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_Set_BLSK(MS_BOOL bEnable)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_BLACKSCREEN XCArgs;
    XCArgs.bEnable = bEnable;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_BLACKSCREEN, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif

void MApi_XC_GenerateBlackVideoForBothWin_U2( void* pInstance, MS_BOOL bEnable)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    if(HSD_IP2_RESET_PATCH == TRUE)
    {
        // patch for 3M HSD hw bug. only main window
        if(bEnable == FALSE)
        {
            MHal_XC_IP2HSDPatch_DelayUnmute(pInstance, MAIN_WINDOW);
            MHal_XC_IP2HSDPatch_DelayUnmute(pInstance, SUB_WINDOW);
        }
    }

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    _XC_ENTRY(pInstance);
    if(E_MLOAD_ENABLED == MDrv_XC_MLoad_GetStatus(pInstance))
    {
        MDrv_SC_GenerateBlackVideoForBothWin(pInstance, bEnable);
    }
    else
    {
        MDrv_XC_WaitOutputVSync(pInstance, 1, 50, MAIN_WINDOW);
        MDrv_SC_GenerateBlackVideo(pInstance, bEnable, MAIN_WINDOW);
        MDrv_XC_WaitOutputVSync(pInstance, 1, 50, SUB_WINDOW);
        MDrv_SC_GenerateBlackVideo( pInstance, bEnable, SUB_WINDOW);
    }
    gSrcInfo[MAIN_WINDOW].bBlackscreenEnabled = bEnable;
    gSrcInfo[SUB_WINDOW].bBlackscreenEnabled = bEnable;
#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
        MS_U8 u8LPLL_Type = (MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type;
        if (IsVBY1_16LANE(u8LPLL_Type))
        {
            if(!bEnable)
            {
                MS_U16 u16DelayTimeMs = MDrv_XC_GetDelayTimeForFrc(pInstance);
                _XC_RETURN(pInstance);
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                MsOS_DelayTask(u16DelayTimeMs);
                _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
                _XC_ENTRY(pInstance);
            }
            MDrv_SC_GenerateFrcBlackVideo(pInstance, bEnable);
        }
#endif

    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_GenerateBlackVideoForBothWin( MS_BOOL bEnable)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_BOTHWINDOW_BLACKVIDEO XCArgs;
    XCArgs.bEnable = bEnable;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_BOTHWINDOW_BLACKVIDEO, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
#ifdef UFO_XC_SETBLACKVIDEOBYMODE
//-------------------------------------------------------------------------------------------------
/// This function will enable/diable output black pattern by mode
/// @param  bEnable      \b IN: TRUE: enable, FALSE: otherwise
/// @param  eWindow    \b IN: which window we are going to query
/// @param  eMode        \b IN: which black video mode we are going to use
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_GenerateBlackVideoByMode_U2( void* pInstance, MS_BOOL bEnable, SCALER_WIN eWindow, EN_XC_BLACK_VIDEO_MODE eMode )
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    E_APIXC_ReturnValue eRet = E_APIXC_RET_FAIL;

    if(HSD_IP2_RESET_PATCH == TRUE)
    {
        // patch for 3M HSD hw bug
        if(bEnable == FALSE)
        {
            MHal_XC_IP2HSDPatch_DelayUnmute(pInstance, eWindow);
        }
    }

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    _XC_ENTRY(pInstance);

    if(eWindow < WINDOW_NUM)
    {
        eRet = MDrv_SC_GenerateBlackVideoByMode(pInstance, bEnable, eWindow, eMode);
        gSrcInfo[eWindow].bBlackscreenEnabled = bEnable;
    }
    else
    {
        // For now not supporting memsync black for both window
        if(eMode == E_XC_BLACK_VIDEO_MEMSYNC)
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "Not supporting both window memsync black video, using basic mode\n");
        }

        MDrv_SC_GenerateBlackVideoForBothWin(pInstance, bEnable);
        gSrcInfo[MAIN_WINDOW].bBlackscreenEnabled = gSrcInfo[SUB_WINDOW].bBlackscreenEnabled = bEnable;
        eRet = E_APIXC_RET_OK;
    }
#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
        MS_U8 u8LPLL_Type = (MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type;
        if (IsVBY1_16LANE(u8LPLL_Type))
        {
            if((eWindow == MAIN_WINDOW) && (MDrv_SC_Is_Need_Frc_Unmute(pInstance)))
            {
                MS_U16 u16DelayTimeMs = MDrv_XC_GetDelayTimeForFrc(pInstance);
                _XC_RETURN(pInstance);
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                MsOS_DelayTask(u16DelayTimeMs);
                _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
                _XC_ENTRY(pInstance);
                MDrv_SC_GenerateFrcBlackVideo(pInstance, FALSE);
            }
            else
            {
                if(!bEnable)
                {
                    MS_U16 u16DelayTimeMs = MDrv_XC_GetDelayTimeForFrc(pInstance);
                    _XC_RETURN(pInstance);
                    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                    MsOS_DelayTask(u16DelayTimeMs);
                    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
                    _XC_ENTRY(pInstance);
                }
                MDrv_SC_GenerateFrcBlackVideo(pInstance, bEnable);
            }
        }
#endif

    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return eRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_GenerateBlackVideoByMode ( MS_BOOL bEnable, SCALER_WIN eWindow, EN_XC_BLACK_VIDEO_MODE eMode )
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_BLACKVIDEO_BYMODE XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.eWindow = eWindow;
    XCArgs.eMode = eMode;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_BLACKVIDEO_BYMODE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
#endif
//-------------------------------------------------------------------------------------------------
/// This function will enable/diable output black pattern
/// @param  bEnable      \b IN: TRUE: enable, FALSE: otherwise
//-------------------------------------------------------------------------------------------------
void MApi_XC_GenerateBlackVideo_U2( void* pInstance, MS_BOOL bEnable, SCALER_WIN eWindow )
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    if(HSD_IP2_RESET_PATCH == TRUE)
    {
        // patch for 3M HSD hw bug. only main window
        if(bEnable == FALSE)
        {
            MHal_XC_IP2HSDPatch_DelayUnmute(pInstance, eWindow);
        }
    }

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    _XC_ENTRY(pInstance);
    //wait VSync and do setting in Vblanking to avoid broken picture

    if(MDrv_XC_MLoad_GetStatus(pInstance) != E_MLOAD_ENABLED
#ifdef STELLAR //for ac on initialize performance
        && (!MDrv_SC_GetForceWrite((EN_MUTE_TYPE)eWindow))
#endif
        )
    {
        MDrv_XC_WaitOutputVSync(pInstance, 1, 50, MAIN_WINDOW);
    }

    if ( eWindow < WINDOW_NUM )
    {
        MDrv_SC_GenerateBlackVideo(pInstance, bEnable, eWindow );
#ifndef STELLAR
        gSrcInfo[eWindow].bBlackscreenEnabled = bEnable;
#endif
    }
    else
    {
        MDrv_SC_GenerateBlackVideoForBothWin(pInstance, bEnable);
#ifdef STELLAR
        gSrcInfo[MAIN_WINDOW].bBlackscreenEnabled = gSrcInfo[SUB_WINDOW].bBlackscreenEnabled = bEnable;
#endif
    }
#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
    MS_U8 u8LPLL_Type = (MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type;
    if (IsVBY1_16LANE(u8LPLL_Type))
    {
        if((eWindow == MAIN_WINDOW) && (MDrv_SC_Is_Need_Frc_Unmute(pInstance)))
        {
            MS_U16 u16DelayTimeMs = MDrv_XC_GetDelayTimeForFrc(pInstance);
            _XC_RETURN(pInstance);
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            MsOS_DelayTask(u16DelayTimeMs);
            _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
            _XC_ENTRY(pInstance);
            MDrv_SC_GenerateFrcBlackVideo(pInstance, FALSE);
        }
        else
        {
            if(!bEnable)
            {
                MS_U16 u16DelayTimeMs = MDrv_XC_GetDelayTimeForFrc(pInstance);
                _XC_RETURN(pInstance);
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                MsOS_DelayTask(u16DelayTimeMs);
                _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
                _XC_ENTRY(pInstance);
            }
            MDrv_SC_GenerateFrcBlackVideo(pInstance, bEnable);
        }
    }
#endif
    #if TEST_NEW_DYNAMIC_NR   //Why turn on/of DNR here, but not in SetBlueScreen?
    // DNR should be turned on only if video is avaliable.m
    if (mvideo_sc_is_enable_3dnr(pInstance, MAIN_WINDOW))
    {
        //printk("Enable 3DNR %bu (flag is 0x%x\n", !bEnable, g_SrcInfo.u16ColorFinetuneFlag & FINETUNE_3DNR);
        MDrv_Scaler_Enable3DNR(pInstance, !bEnable, MAIN_WINDOW);
    }
    #endif

#if 0
    // Enable FPD function when source is MVOP ouput & "Not" in FBL mode.
    {
        if( (IsSrcTypeDTV(gSrcInfo[eWindow].enInputSourceType) || IsSrcTypeStorage(gSrcInfo[eWindow].enInputSourceType) ) &&
            (!MDrv_XC_IsCurrentFrameBufferLessMode()))
        {
            MApi_XC_set_FD_Mask( !bEnable );
        }
    }
#endif
    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_GenerateBlackVideo ( MS_BOOL bEnable, SCALER_WIN eWindow )
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_BLACKVIDEO XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.eWindow = eWindow;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_BLACKVIDEO, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Query whether current XC is black video enabled or not
/// @return @ref MS_BOOL
//-------------------------------------------------------------------------------------------------
MS_BOOL MApi_XC_IsBlackVideoEnable_U2( void* pInstance, SCALER_WIN eWindow )
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    MS_BOOL bEnable = FALSE;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    bEnable = gSrcInfo[eWindow].bBlackscreenEnabled;
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return bEnable;
}

MS_BOOL MApi_XC_IsBlackVideoEnable( SCALER_WIN eWindow )
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    stXC_CHECK_BLACKVIDEO_ENABLE XCArgs;
    XCArgs.eWindow = eWindow;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_CHECK_BLACKVIDEO_ENABLE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}

//-------------------------------------------------------------------------------------------------
/// wait for input sync
/// @param  u8NumVSyncs        \b IN: the number of VSync we are going to wait
/// @param  u16Timeout         \b IN: time out
/// @return @ref MS_U8 retun the number of left Vsync if timeout
//-------------------------------------------------------------------------------------------------
MS_U8 MApi_XC_WaitInputVSync_U2(void* pInstance, MS_U8 u8NumVSyncs, MS_U16 u16Timeout, SCALER_WIN eWindow)
{
    MS_U8 u8InputVsyncTime;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    _XC_ENTRY(pInstance);
    u8InputVsyncTime = MDrv_XC_wait_input_vsync(pInstance, u8NumVSyncs, u16Timeout, eWindow);
    //return MDrv_XC_wait_input_vsync(u8NumVSyncs, u16Timeout, eWindow);
    _XC_RETURN(pInstance);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return u8InputVsyncTime;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_U8 MApi_XC_WaitInputVSync(MS_U8 u8NumVSyncs, MS_U16 u16Timeout, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return 0xFF;
    }

    stXC_SET_WAIT_INPUT_VSYNC XCArgs;
    XCArgs.u8NumVSyncs = u8NumVSyncs;
    XCArgs.u16Timeout = u16Timeout;
    XCArgs.eWindow = eWindow;
    XCArgs.u8ReturnValue = 0xFF;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_WAIT_INPUT_VSYNC, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return 0xFF;
    }
    else
    {
        return XCArgs.u8ReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// wait for output sync
/// @param  u8NumVSyncs        \b IN: the number of VSync we are going to wait
/// @param  u16Timeout         \b IN: time out
/// @return @ref MS_U8 retun the number of left Vsync if timeout
//-------------------------------------------------------------------------------------------------
MS_U8 MApi_XC_WaitOutputVSync_U2(void* pInstance, MS_U8 u8NumVSyncs, MS_U16 u16Timeout, SCALER_WIN eWindow)
{
    MS_U8 u8OutputVsyncTime;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    _XC_ENTRY(pInstance);
    u8OutputVsyncTime = MDrv_XC_wait_output_vsync(pInstance, u8NumVSyncs, u16Timeout, eWindow);
    //return MDrv_XC_wait_output_vsync(u8NumVSyncs, u16Timeout, eWindow);
    _XC_RETURN(pInstance);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return u8OutputVsyncTime;
}

MS_U8 MApi_XC_WaitOutputVSync(MS_U8 u8NumVSyncs, MS_U16 u16Timeout, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return 0xFF;
    }

    stXC_SET_WAIT_OUTPUT_VSYNC XCArgs;
    XCArgs.u8NumVSyncs = u8NumVSyncs;
    XCArgs.u16Timeout = u16Timeout;
    XCArgs.eWindow = eWindow;
    XCArgs.u8ReturnValue = 0xFF;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_WAIT_OUTPUT_VSYNC, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return 0xFF;
    }
    else
    {
        return XCArgs.u8ReturnValue;
    }
}

#ifdef UFO_XC_SCAN_TYPE
//-------------------------------------------------------------------------------------------------
/// Set different scan type.
/// @param  enScanType      \b IN: scan type. Normal/ForceP/ForceI
/// @return E_APIXC_RET_OK or E_APIXC_RET_FAIL
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_SetScanType_U2(void* pInstance, E_XC_SCAN_TYPE enScanType)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    switch (enScanType)
    {
        case E_XC_NORMAL_MODE:
            pXCResourcePrivate->stdrvXC_MVideo.g_ScanType = E_XC_NORMAL_MODE;
            break;

        case E_XC_FORCE_P_MODE:
            pXCResourcePrivate->stdrvXC_MVideo.g_ScanType = E_XC_FORCE_P_MODE;
            break;

        case E_XC_FORCE_I_MODE: // No Force I yet, switch back to normal scantype
            pXCResourcePrivate->stdrvXC_MVideo.g_ScanType = E_XC_NORMAL_MODE;
            break;

        default:
            pXCResourcePrivate->stdrvXC_MVideo.g_ScanType = E_XC_NORMAL_MODE;
    }

    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    return E_APIXC_RET_OK;
}

E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_SetScanType(E_XC_SCAN_TYPE enScanType)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_SCAN_TYPE XCArgs;
    XCArgs.enScanType = enScanType;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_SCAN_TYPE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return E_APIXC_RET_OK;
    }
}
#endif

//-------------------------------------------------------------------------------------------------
/// wait for output sync
/// @param  u8NumVSyncs        \b IN: the number of VSync we are going to wait
/// @param  u16Timeout         \b IN: time out
/// @return @ref MS_U8 retun the number of left Vsync if timeout
//-------------------------------------------------------------------------------------------------
MS_U8 MDrv_XC_WaitOutputVSync(void *pInstance, MS_U8 u8NumVSyncs, MS_U16 u16Timeout, SCALER_WIN eWindow)
{
    MS_U8 u8OutputVsyncTime;
    u8OutputVsyncTime = MDrv_XC_wait_output_vsync(pInstance, u8NumVSyncs, u16Timeout, eWindow);
    return u8OutputVsyncTime;
}

void msAPI_Scaler_Field_Detect(void *pInstance, INPUT_SOURCE_TYPE_t enInputSrcType, EN_VIDEO_SCAN_TYPE enScanType, SCALER_WIN eWindow)
{

    MS_U8  reg_IP1F2_1D = 0;
    MS_U8  reg_IP1F2_23 = 0;
    MS_U16 reg_IP1F2_21 = 0;
    MS_U16 u16Vtt = 0;
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    // Note:
    // IP1F2_1D[13]: Enable auto no signal filter mode.
    // This functionality is to improve timing detection stability.
#if (XC_SUPPORT_FASTSWITCH_BY_IP1GP==TRUE)
    MHal_XC_SetOfflineSource(pInstance, eWindow, enInputSrcType);
#endif
    if(IsSrcTypeDigitalVD(enInputSrcType))
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "VD \n");
        reg_IP1F2_1D = 0xA1;
        reg_IP1F2_21 = 0x0403;
        reg_IP1F2_23 = 0x30;
    }
    else if(IsSrcTypeDTV(enInputSrcType) || IsSrcTypeStorage(enInputSrcType))
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "DTV ");
        if (enScanType == SCAN_INTERLACE)
        {
            reg_IP1F2_1D = 0xA1;
            reg_IP1F2_21 = 0x0403;
            reg_IP1F2_23 = 0x30;
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "-> Interlace\n");
        }
        else
        {
            reg_IP1F2_1D = 0x21;
            reg_IP1F2_21 = 0x0400;
            reg_IP1F2_23 = 0x00;
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "-> Progressive\n");
        }
    }
    else if (IsSrcTypeHDMI(enInputSrcType))
    {

        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "HDMI ");
        if ((enScanType == SCAN_INTERLACE)
            || (MDrv_SC_GetInterlaceInPModeStatus(pInstance, eWindow, enInputSrcType)
               && (MDrv_XC_GetForceiSupportType(pInstance) == ForceI_SW)))
        {
            //HDMI can get i/p mode info. from header,so we dont have to set i/p mode forecely.
            //reg_IP1F2_21 = 0x0003;      // enable DE -> no field invert, disable DE -> use field invert

            //sw patch: HDMI 720*480i->720*400p , v de will become twice
            //0x20 means that xc ip will detect this timing as i mode if( VTT < (0x20<<4) )
            //it will be more faster to detect i mode
            //But in this case, 720*400p will be treat as i mode, and then v de will become twice
            //So, we decrease the threshold to zero.
            //reg_IP1F2_23 = 0x20;
            reg_IP1F2_1D = 0x21;
            reg_IP1F2_21 = 0x0000;      // enable DE -> no field invert, disable DE -> use field invert
            reg_IP1F2_23 = 0x00;
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "-> Interlace\n");

            u16Vtt = MDrv_SC_ip_get_verticaltotal(pInstance, eWindow);
            if((u16Vtt/2)%2 )
            {
                reg_IP1F2_21 |= BIT(8);
            }
            else
            {
                reg_IP1F2_21 &= ~BIT(8);
            }
        }
        else
        {
            reg_IP1F2_1D = 0x21;
            reg_IP1F2_21 = 0x0000;
            reg_IP1F2_23 = 0x00;
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "-> Progressive\n");
        }

        reg_IP1F2_21 |= BIT(12);

        if( (MDrv_XC_GetHdmiSyncMode(pInstance) == HDMI_SYNC_HV)
          &&(enScanType == SCAN_INTERLACE) )
        {
#if (XC_SUPPORT_FASTSWITCH_BY_IP1GP==TRUE)
            MS_U16 u16Cur_IP1F2_21Val = SC_R2BYTE(psXCInstPri->u32DeviceID, MHal_XC_FSS_BankTransfer(pInstance, REG_SC_BK01_21_L, eWindow));
#else
            MS_U16 u16Cur_IP1F2_21Val = SC_R2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK01_21_L);
#endif
            if( u16Cur_IP1F2_21Val&0xC000 )
            {
                if( reg_IP1F2_21&BIT(8) )
                {
                    reg_IP1F2_21 &= ~BIT(8);
                }
                else
                {
                    reg_IP1F2_21 |= BIT(8);
                }
            }
            else
            {
            }
        }

        // patch for special case to field inverse: if new mode interlace detect and also DE mode, need to inverse field.
        if((pXCResourcePrivate->stapiXC_PCMonitor._bNewModeInterlacedDetect == TRUE) &&
           (MDrv_XC_GetHdmiSyncMode(pInstance) == HDMI_SYNC_DE))
        {
            reg_IP1F2_21 &= ~BIT(8);
        }

        if (MHal_XC_GetVRREnableFromHDMI(pInstance) == TRUE)
        {
            reg_IP1F2_21 |= BIT(0);
            reg_IP1F2_21 &= ~BIT(1);
        }
    }
    else if(IsSrcTypeVga(enInputSrcType))
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "DSUB ");
        if (enScanType == SCAN_INTERLACE)
        {
            reg_IP1F2_1D = 0x21;
            reg_IP1F2_21 = 0x0103;
            reg_IP1F2_23 = 0x10;
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "-> Interlace\n");
        }
        else
        {
            reg_IP1F2_1D = 0x21;
            reg_IP1F2_21 = 0x0000;
            reg_IP1F2_23 = 0x00;
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "-> Progressive\n");
        }
    }
    else if (IsSrcTypeCapture(enInputSrcType)
            ||IsSrcTypeSC0DI(enInputSrcType))
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "DIP ");
        reg_IP1F2_21 = 0x0101;//IP1 sometimes will detect opcapture to interlace,we force it to progressive
        reg_IP1F2_1D = 0xA1;
        reg_IP1F2_23 = 0x08; //0x20  // Vtt will use "IP1F2_23 * 16" as lower bound.
    }
    else // YPbPr
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "YPbPr ");
        if(MDrv_XC_ip_GetEuroHdtvStatus(pInstance, eWindow, enInputSrcType) == EURO_AUS_HDTV_NORMAL)
        {
            reg_IP1F2_21 = 0x0100;
        }
        else
        {
            reg_IP1F2_21 = 0x0000;
        }
        reg_IP1F2_21 |= 0xC000;
        reg_IP1F2_1D = 0xA1;
        reg_IP1F2_23 = 0x08; //0x20  // Vtt will use "IP1F2_23 * 16" as lower bound.
    }

    MDrv_SC_setfield(pInstance, reg_IP1F2_1D, reg_IP1F2_21, reg_IP1F2_23, eWindow);

}

//-------------------------------------------------------------------------------------------------
/// Get the specific window's sync status
/// @param  eCurrentSrc             \b IN: the current input source
/// @param  sXC_Sync_Status         \b OUT:store the sync status
/// @param  eWindow                 \b IN: which window we are going to get
//-------------------------------------------------------------------------------------------------
void MApi_XC_GetSyncStatus_U2(void* pInstance, INPUT_SOURCE_TYPE_t eCurrentSrc, XC_IP_SYNC_STATUS *sXC_Sync_Status, SCALER_WIN eWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);
    MDrv_XC_GetSyncStatus(pInstance, eCurrentSrc, sXC_Sync_Status, eWindow);
    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_GetSyncStatus(INPUT_SOURCE_TYPE_t eCurrentSrc, XC_IP_SYNC_STATUS *sXC_Sync_Status, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_GET_SYNC_STATUS XCArgs;
    XCArgs.eCurrentSrc = eCurrentSrc;
    XCArgs.sXC_Sync_Status = sXC_Sync_Status;
    XCArgs.eWindow = eWindow;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_SYNC_STATUS, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
void MDrv_XC_GetSyncStatus(void *pInstance, INPUT_SOURCE_TYPE_t eCurrentSrc, XC_IP_SYNC_STATUS *sXC_Sync_Status, SCALER_WIN eWindow)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    E_MUX_INPUTPORT _enPort = MDrv_XC_Mux_GetHDMIPort(pInstance, eCurrentSrc);

    MDrv_XC_ip_get_sync_status(pInstance, sXC_Sync_Status, _enPort, eWindow, eCurrentSrc);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}

//-------------------------------------------------------------------------------------------------
/// Set input source type
/// @param  enInputSourceType      \b IN: which input source typewe are going to set
/// @param  eWindow                \b IN: which window we are going to set
/// @return void
//-------------------------------------------------------------------------------------------------
MS_BOOL MApi_XC_SetInputSource_U2( void* pInstance, INPUT_SOURCE_TYPE_t enInputSourceType,SCALER_WIN eWindow )
{
printf("~~~~~~~~~~~~~~~ MApi_XC_SetInputSource_U2, %d\n", enInputSourceType);
#if (XC_SUPPORT_FASTSWITCH_BY_IP1GP == TRUE)
    if(enInputSourceType != INPUT_SOURCE_NONE)
#endif
    {
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
        XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
        UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
        XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
        UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
        if( pXCResourcePrivate->stdrvXC_MVideo.enOldInputSrc_ForScalerSetSource[eWindow] == enInputSourceType)
        {
            printf("MApi_XC_SetInputSource[Win %u] return because old src %d -> %d\n"
                    , eWindow
                    , pXCResourcePrivate->stdrvXC_MVideo.enOldInputSrc_ForScalerSetSource[eWindow], enInputSourceType);
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
            return TRUE;
        }
        else
        {
            pXCResourcePrivate->stdrvXC_MVideo.enOldInputSrc_ForScalerSetSource[eWindow] = enInputSourceType;
        }

#ifdef UFO_XC_HDR
#if (UFO_XC_HDR_VERSION == 2)
        MDrv_XC_HDR_SetInputSource(pInstance, enInputSourceType);

        if (pXCResourcePrivate->stdrvXC_Display._u8CFDVersion == CFD_PLUS_VERSION)
        {
            _MDrv_XC_SetCFDHashInfo();
        }
#endif
#endif

        gSrcInfo[eWindow].enInputSourceType = enInputSourceType;

#if (XC_SUPPORT_FASTSWITCH_BY_IP1GP == TRUE)
        MS_U8 u8InputSrcSel = 0, u8SyncSel = 0, u8VideoSel = 0, u8isYPbPr = 0;
        MHal_XC_SetOfflineSource(pInstance, MAX_WINDOW, INPUT_SOURCE_YPBPR);

        if(MDrv_XC_IsFSSEnabled(pInstance, eWindow))
        {
            if(!IsSrcTypeAnalog(enInputSourceType))
            {
                //patch for fss, fix later
                //for default ypbpr
                u8InputSrcSel = IP_ANALOG1;
                u8SyncSel = (SYNC_ON_GREEN << 1) | SOG;
                u8isYPbPr = TRUE;
                MApi_XC_ADC_SwitchSource(pInstance, 1); // Force ADC to reset after source switch
                MDrv_SC_ip_set_input_source(pInstance, u8InputSrcSel, u8SyncSel, u8VideoSel, u8isYPbPr, MAX_WINDOW);
            }
        }

        MDrv_XC_CheckSubWinForFSS(pInstance, &eWindow);
        MHal_XC_SetOfflineSource(pInstance, eWindow, enInputSourceType);
        gSrcInfo[eWindow].enInputSourceType = enInputSourceType; //write share memory again in case eWindow is replaced.
#endif
        //Disable new mode interlaced detect before set input source
        MDrv_XC_SetNewModeInterlacedDetect(pInstance, DISABLE, 0, DISABLE, FALSE, FALSE, eWindow, enInputSourceType);

        MDrv_SC_ForceInterlaceInPMode(pInstance, DISABLE, eWindow, enInputSourceType);
        //IP1 sometimes will detect opcapture to interlace,we force it to progressive
        Hal_SC_ip_set_user_def_interlace_status(pInstance, DISABLE, DISABLE, eWindow);

        _XC_ENTRY(pInstance);

#if SUPPORT_IP_HDMI_FOR_HV_MODE
        // Should disable "pixel repetition" for other source
        if(!(IsSrcTypeHDMI(enInputSourceType) || IsSrcTypeDVI(enInputSourceType)))
        {
            Hal_SC_ip_set_fir_down_sample_divider(pInstance, DISABLE, 0x00, eWindow); //Reset Pixel repetition, when switch hv mode
        }
#endif

        if(FALSE == MDrv_XC_SetIP1SignalSource(pInstance, enInputSourceType, eWindow))
        {
            _XC_RETURN(pInstance);
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            return FALSE;
        }

        if(IsSrcTypeVga(enInputSourceType))
        {
            MDrv_SC_ip_set_image_wrap(pInstance, DISABLE, DISABLE, eWindow);
        }
        else
        {
            MDrv_SC_ip_set_image_wrap(pInstance, ENABLE, ENABLE, eWindow);
        }

        MDrv_SC_ip_set_de_bypass_mode(pInstance, DISABLE, eWindow); // fix HDMI <-> component switch problems

        if(IsSrcTypeVga(enInputSourceType))
        {
            MDrv_SC_ip_set_input_sync_reference_edge(pInstance, LEADING_EDGE, TAILING_EDGE, eWindow);
            MDrv_SC_ip_set_input_vsync_delay(pInstance, DELAY_QUARTER_HSYNC, eWindow);
            MDrv_SC_ip_set_de_only_mode(pInstance, DISABLE, eWindow);
            MDrv_ip_set_input_10bit(pInstance, ENABLE, eWindow);

            MDrv_SC_ip_set_coast_input(pInstance, PC_SOURCE, eWindow);
            MDrv_SC_ip_set_ms_filter(pInstance, ENABLE, 0, eWindow);
            MDrv_SC_ip_set_coast_window(pInstance, 0x0C, 0x0C, eWindow);
            MDrv_SC_ip_set_input_sync_sample_mode(pInstance, NORMAL_MODE, eWindow);
            MDrv_SC_ip_set_post_glitch_removal(pInstance, DISABLE, 0x00, eWindow);

            // reset fast blanking mux for VGA since VGA vsync can be affected by this setting
            MHal_XC_ADC_ResetFastBlankingMux();
        }
        else if(IsSrcTypeYPbPr(enInputSourceType))
        {
            MDrv_SC_ip_set_input_sync_reference_edge(pInstance, LEADING_EDGE, TAILING_EDGE, eWindow);
            MDrv_SC_ip_set_input_vsync_delay(pInstance, NO_DELAY, eWindow);
            MDrv_SC_ip_set_ms_filter(pInstance, ENABLE, 0, eWindow);
            MDrv_SC_ip_set_de_only_mode(pInstance, DISABLE, eWindow);
            MDrv_ip_set_input_10bit(pInstance, ENABLE, eWindow);

            MDrv_SC_ip_set_coast_input(pInstance, COMPONENT_VIDEO, eWindow);
            MDrv_SC_ip_set_coast_window(pInstance, 0x0C, 0x0C, eWindow);
            MDrv_SC_ip_set_input_sync_sample_mode(pInstance, GLITCH_REMOVAL, eWindow);
            MDrv_SC_ip_set_post_glitch_removal(pInstance, DISABLE, 0x01, eWindow);

        #if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
            MDrv_XC_mux_SetScalerIndexInformationToADC(pInstance, (psXCInstPri->u32DeviceID), enInputSourceType);
        #endif
        }
        else if(IsSrcTypeHDMI(enInputSourceType))
        {
            //Get HDMI 2.1 info to decide de-glitch setting
            E_MUX_INPUTPORT enSrcPort = INPUT_PORT_NONE_PORT;
            enSrcPort = MDrv_XC_Mux_GetHDMIPort(pInstance, enInputSourceType);

            MDrv_SC_ip_set_input_sync_reference_edge(pInstance, LEADING_EDGE, TAILING_EDGE, eWindow);
            MDrv_SC_ip_set_input_vsync_delay(pInstance, DELAY_QUARTER_HSYNC, eWindow);
#ifdef K3_U2
#else
            Hal_HDMI_Set_YUV422to444_Bypass(TRUE);
#endif
            if( MDrv_XC_GetHdmiSyncMode(pInstance) == HDMI_SYNC_DE)
            {
                MDrv_SC_ip_set_input_vsync_delay(pInstance, DELAY_QUARTER_HSYNC, eWindow);
                MDrv_SC_ip_set_de_only_mode(pInstance, ENABLE, eWindow);
                MDrv_SC_ip_set_de_bypass_mode(pInstance, DISABLE, eWindow);
                MDrv_SC_ip_set_input_sync_sample_mode(pInstance, NORMAL_MODE, eWindow);

                if(MDrv_HDMI_GetSourceVersion(enSrcPort) == PCHDMI_SOURCE_VERSION_HDMI21)
                {
                    MDrv_SC_ip_set_post_glitch_removal(pInstance, ENABLE, 0x01, eWindow);
                }
                else
                {
                    MDrv_SC_ip_set_post_glitch_removal(pInstance, ENABLE, 0x07, eWindow);
                }
#ifdef K3_U2
                Hal_HDMI_Set_YUV422to444_Bypass(TRUE);
#endif
            }
            else
            {
                MDrv_SC_ip_set_input_vsync_delay(pInstance, NO_DELAY, eWindow);
                MDrv_SC_ip_set_de_only_mode(pInstance, DISABLE, eWindow);
                // Use DE as capture window
                MDrv_SC_ip_set_de_bypass_mode(pInstance, ENABLE, eWindow);
                // Disable scaling for get correct DE.
                //MDrv_SC_Enable_PreScaling(FALSE, FALSE, eWindow);
                // Set full range capture size for de-bypass mode
                //MDrv_SC_SetFullRangeCapture(eWindow);
                MDrv_SC_ip_set_input_sync_sample_mode(pInstance, GLITCH_REMOVAL, eWindow);
                MDrv_SC_ip_set_post_glitch_removal(pInstance, ENABLE, 0x01, eWindow);
#ifdef K3_U2
                Hal_HDMI_Set_YUV422to444_Bypass(FALSE);
#endif
            }

            MDrv_ip_set_input_10bit(pInstance, ENABLE, eWindow);

            MDrv_SC_ip_set_ms_filter(pInstance, DISABLE, 0, eWindow);

            //MDrv_SC_ip_set_coast_input( PC_SOURCE, eWindow );
            MDrv_SC_ip_set_ms_filter(pInstance, DISABLE, 0, eWindow);
            //MDrv_SC_ip_set_coast_window( 0x0C, 0x0C, eWindow );
            MDrv_SC_ip_set_input_sync_sample_mode(pInstance, NORMAL_MODE, eWindow);
            if(MDrv_HDMI_GetSourceVersion(enSrcPort) == PCHDMI_SOURCE_VERSION_HDMI21)
            {
                MDrv_SC_ip_set_post_glitch_removal(pInstance, ENABLE, 0x01, eWindow);
            }
            else
            {
                MDrv_SC_ip_set_post_glitch_removal(pInstance, ENABLE, 0x07, eWindow);
            }
        }
        else if(IsSrcTypeDigitalVD(enInputSourceType)
        #ifdef __EXTVD
        | IsUseExtVDPort(enInputPortType)
        #endif
        )
        {
            MDrv_SC_ip_set_input_sync_reference_edge(pInstance, TAILING_EDGE, TAILING_EDGE, eWindow);
            MDrv_SC_ip_set_input_vsync_delay(pInstance, DELAY_QUARTER_HSYNC, eWindow);
            MDrv_SC_ip_set_de_only_mode(pInstance, DISABLE, eWindow);

            #ifdef __EXTVD
            if(IsUseExtVDPort(enInputPortType))
            {
                MS_U16 u16tmp;
                MDrv_ip_set_input_10bit(pInstance, DISABLE, eWindow);

                //MDrv_WriteByte(BK_SELECT_00, REG_BANK_IP1F2);
                //MDrv_WriteByte(L_BK_IP1F2(0x08), (MDrv_ReadByte(L_BK_IP1F2(0x08)) & 0x7F));
                //u16tmp = SC_R2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK01_08_L);
                //SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK01_08_L, u16tmp & 0x007F, 0x00FF);
            }
            else
            #endif

            {
                MDrv_ip_set_input_10bit(pInstance, ENABLE, eWindow);
            }
            //MDrv_SC_ip_set_coast_input( PC_SOURCE, eWindow);
            MDrv_SC_ip_set_ms_filter(pInstance, DISABLE, 0, eWindow);
            //MDrv_SC_ip_set_coast_window( 0x0C, 0x0C, eWindow );
            MDrv_SC_ip_set_input_sync_sample_mode(pInstance, NORMAL_MODE, eWindow);
            MDrv_SC_ip_set_post_glitch_removal(pInstance, DISABLE, 0x00, eWindow);
        }
        else //DTV
        {
            //set HSYNC reference to leading edge, the capature start will be 1/2 Hsync when input source is from DC0/DC1
            MDrv_SC_ip_set_input_sync_reference_edge(pInstance, LEADING_EDGE, TAILING_EDGE, eWindow);
            MDrv_SC_ip_set_input_vsync_delay(pInstance, DELAY_QUARTER_HSYNC, eWindow);
            MDrv_SC_ip_set_de_only_mode(pInstance, DISABLE, eWindow);
            MDrv_ip_set_input_10bit(pInstance, ENABLE, eWindow);

            //MDrv_SC_ip_set_coast_input( PC_SOURCE, eWindow);
            MDrv_SC_ip_set_ms_filter(pInstance, DISABLE, 0, eWindow);
            //MDrv_SC_ip_set_coast_window( 0x0C, 0x0C, eWindow );
            MDrv_SC_ip_set_input_sync_sample_mode(pInstance, NORMAL_MODE, eWindow);
            MDrv_SC_ip_set_post_glitch_removal(pInstance, DISABLE, 0x00, eWindow);
        }

        // set  h0027 [2] : true to enable DE only mode Glitch Protect for position
        // to fix HDMI 720P YUV422 HStart error & HDE lack of 1
        if (IsSrcTypeHDMI(enInputSourceType))
        {
            MDrv_SC_ip_set_DE_Mode_Glitch(pInstance, 0x24, eWindow); // VSync glitch removal with line less than 2(DE only)
        }
        else
        {
            MDrv_SC_ip_set_DE_Mode_Glitch(pInstance, 0x04, eWindow); // CHANNEL_ATTRIBUTE function control
        }
        msAPI_Scaler_Field_Detect(pInstance, enInputSourceType, SCAN_AUTO, eWindow);

        if(!MDrv_XC_IsFSSEnabled(pInstance, eWindow) || IsSrcTypeDigitalVD(enInputSourceType) || IsSrcTypeDTV(enInputSourceType) || IsSrcTypeStorage(enInputSourceType))
        {
            MDrv_XC_reset_ip(pInstance, eWindow, enInputSourceType);
        }

        // Inform CFD about Create Path in Fast Source Switch case
        if (MDrv_XC_IsFSSEnabled(pInstance, MAIN_WINDOW) == TRUE)
        {
            XC_CFD_CONTROL_INFO stCfdCtrl;
            ST_XC_MUX_INFO stXcMuxInfo;
            PQ_INPUT_SOURCE_TYPE enPqInputSourceType = PQ_INPUT_SOURCE_NONE;

            if(MDrv_XC_ConvertToPQInputSourceType(enInputSourceType, &enPqInputSourceType) == FALSE)
            {
                XC_LOG_TRACE(XC_DBGLEVEL_PARAMETER_TRACE, "[%s,%5d] Unexpected input source type = %d. \n", __FUNCTION__, __LINE__, enInputSourceType);
            }

            memset(&stXcMuxInfo, 0, sizeof(ST_XC_MUX_INFO));
            stXcMuxInfo.enAction = E_XC_CFD_MUX_CREATE;
            stXcMuxInfo.u8Win = (MS_U8) MAIN_WINDOW;
            stXcMuxInfo.u8InputSource = (MS_U8) enPqInputSourceType;

            memset(&stCfdCtrl, 0, sizeof(XC_CFD_CONTROL_INFO));
            stCfdCtrl.enCtrlType = E_XC_CFD_CTRL_MUX_INFO;
            stCfdCtrl.pParam = &stXcMuxInfo;
            stCfdCtrl.u32ParamLen = sizeof(ST_XC_MUX_INFO);
            MDrv_XC_HDR_Control(pInstance, E_XC_HDR_CTRL_CFD_CONTROL, &stCfdCtrl);
        }

#if 0
        if(g_bIMMESWITCH_DVI_POWERSAVING)
        {
            if((enInputSourceType < INPUT_SOURCE_HDMI || enInputSourceType > INPUT_SOURCE_HDMI4))
                MDrv_DVI_ForceAllPortsEnterPS();
            else
                u8PSDVIStableCount[enInputSourceType - INPUT_SOURCE_HDMI] = 0;
        }
#endif
        /// no delay
        gSrcInfo[eWindow].stStatusnodelay.enInputSourceType = enInputSourceType;
        _XC_RETURN(pInstance);
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    }

    return TRUE;
}

#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_SetInputSource( INPUT_SOURCE_TYPE_t enInputSourceType,SCALER_WIN eWindow )
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_INPUTSOURCE XCArgs;
    XCArgs.enInputSourceType = enInputSourceType;
    XCArgs.eWindow = eWindow;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_INPUTSOURCE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
static MS_BOOL _MDrv_XC_Update_CFDInfo(void *pInstance, INPUT_SOURCE_TYPE_t enInputSourceType, XC_InternalStatus *pSrcInfo, SCALER_WIN eWindow, MS_BOOL bUseYUVSpace)
{

#ifdef UFO_XC_HDR
#if (UFO_XC_HDR_VERSION == 2)

///Add CFD control
///Update Stream info into CFD
    PQ_INPUT_SOURCE_TYPE enPQSourceType=PQ_INPUT_SOURCE_NONE;
    MS_BOOL bisHDmode=FALSE;

    MDrv_XC_ConvertToPQInputSourceType(enInputSourceType, &enPQSourceType);

    /// Trans is HD mode or not
    if(pSrcInfo->stCapWin.height < 600)
    {
        bisHDmode=FALSE;
    }
    else
    {
        bisHDmode=TRUE;
    }

    /// Send into Kernel CFD
    ST_KDRV_XC_CFD_FIRE stUpdateInfo;
    memset(&stUpdateInfo, 0, sizeof(ST_KDRV_XC_CFD_FIRE));
    stUpdateInfo.u32Version=KDRV_XC_CFD_FIRE_VERSION;
    stUpdateInfo.u16Length=sizeof(ST_KDRV_XC_CFD_FIRE);
    stUpdateInfo.u8Win=eWindow;
    stUpdateInfo.u8InputSource=enPQSourceType;
    stUpdateInfo.bIsRgbBypass=(!bUseYUVSpace);
    stUpdateInfo.bIsHdMode=bisHDmode;

#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)

    ST_KDRV_XC_CFD_CONTROL_INFO stCFDCtrlInfo;
    memset(&stCFDCtrlInfo, 0, sizeof(ST_KDRV_XC_CFD_CONTROL_INFO));
    stCFDCtrlInfo.enCtrlType = E_KDRV_XC_CFD_CTRL_UPDATE_INFO;
    stCFDCtrlInfo.pParam = &stUpdateInfo;
    stCFDCtrlInfo.u32ParamLen = sizeof(ST_KDRV_XC_CFD_FIRE);

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

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

    if (ioctl(_s32FdScaler, MDRV_XC_IOC_CFDCONCTRL, &stCFDCtrlInfo))
    {
        printf("MDRV_XC_IOC_CFDCONCTRL UPDATE INFO fail!!!!\n");
    }

    if (stCFDCtrlInfo.u16ErrCode != 0)
    {
        printf("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))

    ST_KDRV_XC_CFD_CONTROL_INFO stCFDCtrlInfo;
    memset(&stCFDCtrlInfo, 0, sizeof(ST_KDRV_XC_CFD_CONTROL_INFO));
    stCFDCtrlInfo.enCtrlType = E_KDRV_XC_CFD_CTRL_UPDATE_INFO;
    stCFDCtrlInfo.pParam = &stUpdateInfo;
    stCFDCtrlInfo.u32ParamLen = sizeof(ST_KDRV_XC_CFD_FIRE);
    MDrv_XC_CFDControl(&stCFDCtrlInfo);
#endif
#endif
#endif
    return TRUE;
}

static void msAPI_Scaler_SetMode(void *pInstance, INPUT_SOURCE_TYPE_t enInputSourceType, XC_InternalStatus *pSrcInfo, SCALER_WIN eWindow )
{
    MS_BOOL bFBL = pSrcInfo->bFBL;
    MS_BOOL bInterlace = pSrcInfo->bInterlace;
    MS_BOOL bUseYUVSpace = FALSE;
    MS_U8 interlace_type = 0;
    XC_INITMISC stXC_Init_Misc;
    memset(&stXC_Init_Misc,0,sizeof(XC_INITMISC));

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    //----------------------------------------------------
    // Setup Memory format
    //----------------------------------------------------
    if(pXCResourcePrivate->stdrvXC_MVideo.s_eMemFmt[eWindow] == E_MS_XC_MEM_FMT_AUTO)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "E_MS_XC_MEM_FMT_AUTO\n");
        if(!bInterlace &&
          ((IsSrcTypeHDMI(enInputSourceType) && !pXCResourcePrivate->stdrv_HDMI.enStatus_info.b_St_HDMI_Mode) || // DVI plugged into HDMI connector
          IsSrcTypeVga(enInputSourceType)  ) )
        {   // always keep memory foramt to 444 for corresponding to Y/C post-scaling filter
            pSrcInfo->bMemFmt422 = FALSE;
        }
        else
        {   // if input source is 422, HW will convert to 444 automatically
            pSrcInfo->bMemFmt422 = TRUE;
        }
    }
    else
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "E_MS_XC_MEM_FMT_422\n");
        if(pXCResourcePrivate->stdrvXC_MVideo.s_eMemFmt[eWindow] == E_MS_XC_MEM_FMT_422)
            pSrcInfo->bMemFmt422 = TRUE;
        else
            pSrcInfo->bMemFmt422 = FALSE;
    }
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR); //K3_PQ

    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "MemFmt422=%u\n", (MS_U16)pSrcInfo->bMemFmt422);
#if ((HW_DESIGN_4K2K_VER == 4)||(HW_DESIGN_4K2K_VER == 6)||(HW_DESIGN_4K2K_VER == 7)||(HW_DESIGN_4K2K_VER == 1))
    if(!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
    {
#ifdef ENABLE_TV_SC2_PQ
        if (psXCInstPri->u32DeviceID == PQ_XC_ID_0)
#endif
        {
            if(s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_csc == NULL)
                bUseYUVSpace = 0;
            else
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
                bUseYUVSpace = s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_csc((psXCInstPri->u32DeviceID == 1)?PQ_SUB_WINDOW:PQ_MAIN_WINDOW,
                                             pSrcInfo->bForceRGBin == ENABLE ? PQ_FOURCE_COLOR_RGB : PQ_FOURCE_COLOR_DEFAULT);
#else
                bUseYUVSpace = s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_csc(eWindow == SUB_WINDOW ? PQ_SUB_WINDOW : PQ_MAIN_WINDOW,
                                             pSrcInfo->bForceRGBin == ENABLE ? PQ_FOURCE_COLOR_RGB : PQ_FOURCE_COLOR_DEFAULT);
#endif
        }
#ifdef ENABLE_TV_SC2_PQ
        else if (psXCInstPri->u32DeviceID == PQ_XC_ID_1)
        {
            if(s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_csc_ex == NULL)
                bUseYUVSpace = 0;
            else
                bUseYUVSpace = s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_csc_ex(psXCInstPri->u32DeviceID, eWindow == SUB_WINDOW ? PQ_SUB_WINDOW : PQ_MAIN_WINDOW,
                                              pSrcInfo->bForceRGBin == ENABLE ? PQ_FOURCE_COLOR_RGB : PQ_FOURCE_COLOR_DEFAULT);
        }
#endif

#ifdef UFO_XC_HDR
#if (UFO_XC_HDR_VERSION == 2)
        if ((pXCResourcePrivate->stdrvXC_Display._bEnableHDR == ENABLE) && IsSrcTypeHDMI(enInputSourceType))
        {
            // since Dolby HDR will fake as RGB but actually YUV content, we have to set our color space as YUV.
            pSrcInfo->bUseYUVSpace = TRUE;
        }
        else
#endif
#endif
        {
            if (bUseYUVSpace)
            {
                pSrcInfo->bUseYUVSpace = TRUE;
             }
            else
            {
                pSrcInfo->bUseYUVSpace = FALSE;
            }
        }

#ifdef ENABLE_TV_SC2_PQ
        if (psXCInstPri->u32DeviceID == PQ_XC_ID_0)
#endif
        {
            if(s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_get_memyuvfmt == NULL)
                pSrcInfo->bMemYUVFmt =  TRUE;
            else
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
                pSrcInfo->bMemYUVFmt = s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_get_memyuvfmt((psXCInstPri->u32DeviceID == 1)?PQ_SUB_WINDOW:PQ_MAIN_WINDOW,
                                          pSrcInfo->bForceRGBin == ENABLE ? PQ_FOURCE_COLOR_RGB : PQ_FOURCE_COLOR_DEFAULT);
#else
                pSrcInfo->bMemYUVFmt = s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_get_memyuvfmt(eWindow == SUB_WINDOW ? PQ_SUB_WINDOW : PQ_MAIN_WINDOW,
                                          pSrcInfo->bForceRGBin == ENABLE ? PQ_FOURCE_COLOR_RGB : PQ_FOURCE_COLOR_DEFAULT);
#endif
        }
#ifdef ENABLE_TV_SC2_PQ
        else if (psXCInstPri->u32DeviceID == PQ_XC_ID_1)
        {
            if(s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_get_memyuvfmt_ex == NULL)
                pSrcInfo->bMemYUVFmt =  TRUE;
            else
                pSrcInfo->bMemYUVFmt = s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_get_memyuvfmt_ex(psXCInstPri->u32DeviceID, eWindow == SUB_WINDOW ? PQ_SUB_WINDOW : PQ_MAIN_WINDOW,
                                          pSrcInfo->bForceRGBin == ENABLE ? PQ_FOURCE_COLOR_RGB : PQ_FOURCE_COLOR_DEFAULT);
        }
#endif

        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "bMemYUVFmt=%u, RGBin=%u\n", pSrcInfo->bMemYUVFmt, pSrcInfo->bForceRGBin);

        if((pSrcInfo->bMemYUVFmt != TRUE) && (eWindow == SUB_WINDOW)) //IP_CSC output is RGB, so skip 444 to 422
        {
            pSrcInfo->bMemFmt422 = FALSE;//Now, only care about Sub case to reduce side effect
        }

        //Set MM RGB mode
        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(IS_INPUT_MM_RGB_MODE(enRGB,eWindow) && (!pSrcInfo->bMemYUVFmt))
            {
                pSrcInfo->bMemFmt422 = FALSE;
            }
        }

        MDrv_XC_GetMISCStatus(pInstance, &stXC_Init_Misc);
        if (stXC_Init_Misc.u32MISC_B & E_XC_INIT_MISC_B_PQ_SKIP_PCMODE_NEWFLOW)
        {
            //if(HDMI/DVI RGB) set to 444
            if(!pSrcInfo->bMemYUVFmt && ( IsSrcTypeHDMI(enInputSourceType) || IsSrcTypeDVI(enInputSourceType) ))
            {
                pSrcInfo->bMemFmt422 = FALSE;
            }
        }
        //-------------------------------------------
        // Set De-interlaced mode and Memory format
        //-------------------------------------------
        //printf("==> Set PQ MEMORY Mode: %d\n", eWindow);

#ifdef K3_U2
        // Tempararily not dumping MADi PQ table in seamless zapping
        if((Hal_SC_GetSeamlessZappingStatus(pInstance, eWindow)) && (Hal_SC_get_freezeimg(pInstance, eWindow)))
        {
            Hal_SC_SetPX2MemFormat(pInstance, eWindow);
            interlace_type = PQ_DEINT_3DDI_HISTORY;
            pSrcInfo->u8BitPerPixel = 24;

            //PQ_IP_MemFormat_Main 422MF
            HAL_SC_set_memory_bit_fmt(pInstance,FALSE,FALSE);
            //444 to 422 (PQ_IP_444To422_Main) (on)
            HAL_SC_ip_set_444to422_filter_mod(pInstance,TRUE);
            //422To444(PQ_IP_422To444_Main on)
            SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_0C_L, 0x80 ,0x0080);
        }
        else
        {
            if(E_XC_DEVICE0 == psXCInstPri->u32DeviceID) // SC0
            {
                if(s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_memformat == NULL)
                    interlace_type = 0;
                else
                    interlace_type = (MS_U8) s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_memformat(
                        eWindow == SUB_WINDOW ?
                        PQ_SUB_WINDOW :
                        PQ_MAIN_WINDOW,
                        pSrcInfo->bMemFmt422,
                        bFBL,
                        &(pSrcInfo->u8BitPerPixel));
            }
            else // SC1
            {
                Hal_SC_SetPX2MemFormat(pInstance, eWindow);
                interlace_type = PQ_DEINT_3DDI_HISTORY;
                pSrcInfo->u8BitPerPixel = 24;
            }
        }
#else
#ifdef ENABLE_TV_SC2_PQ
        if (psXCInstPri->u32DeviceID == PQ_XC_ID_0)
#endif
        {
            if(s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_memformat == NULL)
            {
#if (HW_DESIGN_4K2K_VER == 7)
                // Temperarily add for SC1 to load MADi settings
                if(E_XC_DEVICE1 == psXCInstPri->u32DeviceID)
                {
                    interlace_type = Hal_SC1_LoadMADi(pInstance, eWindow, &(pSrcInfo->u8BitPerPixel));
                }
                else
#endif
                {
                    interlace_type = 0;
                }
            }
            else
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
                interlace_type = (MS_U8) s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_memformat(
                    (psXCInstPri->u32DeviceID == 1)?PQ_SUB_WINDOW:PQ_MAIN_WINDOW,
                    pSrcInfo->bMemFmt422,
                    bFBL,
                    &(pSrcInfo->u8BitPerPixel));
#else
                interlace_type = (MS_U8) s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_memformat(
                    eWindow == SUB_WINDOW ?
                    PQ_SUB_WINDOW :
                    PQ_MAIN_WINDOW,
                    pSrcInfo->bMemFmt422,
                    bFBL,
                    &(pSrcInfo->u8BitPerPixel));
#endif
        }
#ifdef ENABLE_TV_SC2_PQ
        else if (psXCInstPri->u32DeviceID == PQ_XC_ID_1)
        {
            if(s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_memformat_ex == NULL)
                interlace_type = 0;
            else
                interlace_type = (MS_U8) s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_memformat_ex(psXCInstPri->u32DeviceID,
                    eWindow == SUB_WINDOW ?
                    PQ_SUB_WINDOW :
                    PQ_MAIN_WINDOW,
                    pSrcInfo->bMemFmt422,
                    bFBL,
                    &(pSrcInfo->u8BitPerPixel));
        }
#endif
    }
#endif
#if SUPPORT_SEAMLESS_ZAPPING
    Hal_SC_SetPX2MemFormat(pInstance, eWindow);
    if (IsVMirrorMode(eWindow))
    {
        MS_U16 u16IPM3DLRFlags = SC_R2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK12_30_L);
        // 2D mode
        if(MDrv_XC_Get_3D_Output_Mode(pInstance) == E_XC_3D_OUTPUT_MODE_NONE || MDrv_XC_Get_3D_Input_Mode(pInstance, eWindow) == E_XC_3D_INPUT_MODE_NONE)
        {
            if(Hal_SC_IsPX2MemFormat(pInstance, eWindow))
            {
                if((u16IPM3DLRFlags&BIT(4)) == 0)
                {
                    SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_30_L, BIT(4), BIT(4));//F2 IPM 3D LR invert
                }
            }
            else
            {
                if((u16IPM3DLRFlags&BIT(4)) == BIT(4))
                {
                    SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_30_L, 0, BIT(4));//F2 IPM 3D LR invert
                }
            }
        }
    }
#endif
#endif

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR); //K3_PQ
#if ((HW_DESIGN_4K2K_VER == 4)||(HW_DESIGN_4K2K_VER == 6)||(HW_DESIGN_4K2K_VER == 7)||(HW_DESIGN_4K2K_VER == 1))
    if(!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
    {
        pSrcInfo->eDeInterlaceMode = (MS_DEINTERLACE_MODE)interlace_type;
    }
#endif
    //printf("DI mode =%x, bitPerPixel=%d\n",pSrcInfo->eDeInterlaceMode, pSrcInfo->u8BitPerPixel);
    if(bFBL && (pSrcInfo->stDispWin.height != pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height))
    {
        if(SUPPORT_SPECIAL_FRAMELOCK == FALSE)
        {
            MDrv_SC_set_std_display_window(pInstance, eWindow);  //only support full screen
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "[FBL]Set to std_display_window(panel size, full screen) \n");
        }
    }

    _XC_ENTRY(pInstance);

    // set field shift line  // set shift line between even/odd field
    MDrv_SC_set_shift_line(pInstance, pSrcInfo->bFBL, pSrcInfo->eDeInterlaceMode, eWindow);

    //------------------------------------
    // Setup Delay line
    //------------------------------------
    /* move to MDrv_SC_Set_LockFreeze_Point()
    if(bFBL)
    {
        pSrcInfo->u8DelayLines = 1;
        //printf("[FBL]Set delay line\n");
    }
    else if( (pSrcInfo->eDeInterlaceMode == MS_DEINT_2DDI_AVG) ||
             (pSrcInfo->eDeInterlaceMode == MS_DEINT_2DDI_BOB))
    {
        pSrcInfo->u8DelayLines = 1;    // 5
    }
    else
    {
        pSrcInfo->u8DelayLines = 2;
    }

    // Setup delay line
    Hal_SC_set_delayline( pSrcInfo->u8DelayLines, eWindow );           // set delay line for trigger point, for 2DDI
    */
    if(!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
    {
        msAPI_Scaler_Field_Detect(pInstance, enInputSourceType, (pSrcInfo->bInterlace?SCAN_INTERLACE:SCAN_PROGRESSIVE), eWindow );
    }

    //--------------
    // PIP related
    //--------------
    if(PIP_SUPPORTED)
    {
        if(eWindow == MAIN_WINDOW)
        {
            if(pSrcInfo->bInterlace == TRUE)
            {
                SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK20_11_L, BIT(13), BIT(13));
            }
            else
            {
#if SUPPORT_SEAMLESS_ZAPPING
                // If memory format is PX2 progressive mode, set to use interlace line buffer setting
                if(Hal_SC_IsPX2MemFormat(pInstance, eWindow))
                {
                    SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK20_11_L, BIT(13), BIT(13));
                }
                else
#endif
                {
                    SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK20_11_L, 0, BIT(13));
                }
            }
        }
    }

    if(!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
    {
        //Update CFD info into CFD driver
        _MDrv_XC_Update_CFDInfo(pInstance,enInputSourceType,pSrcInfo,eWindow,bUseYUVSpace);
    }

    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
}

//This function is used in internal (as driver), so do not set _XC_SEMAPHORE_ENTRY
void _MDrv_XC_Set_PQ_SourceData(void *pInstance,
    SCALER_WIN eWindow,
    INPUT_SOURCE_TYPE_t enInputSourceType,
    XC_InternalStatus *pstXC_WinTime_Info)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    PQ_WIN ePQWin;
    PQ_INPUT_SOURCE_TYPE enPQSourceType=PQ_INPUT_SOURCE_NONE;
    MS_PQ_Mode_Info stPQModeInfo;

    switch (eWindow)
    {
    default:
    case MAIN_WINDOW:
        ePQWin = PQ_MAIN_WINDOW;
        break;

    case SUB_WINDOW:
        ePQWin = PQ_SUB_WINDOW;
        break;
    }

    if(IsSrcTypeVga(enInputSourceType))
    {
        enPQSourceType = PQ_INPUT_SOURCE_VGA; // VGA
    }
    else if(IsSrcTypeATV(enInputSourceType))
    {
        enPQSourceType = PQ_INPUT_SOURCE_TV; // TV
    }
    else if(IsSrcTypeDTV(enInputSourceType))
    {
        enPQSourceType = PQ_INPUT_SOURCE_DTV; //DTV
    }
    else if(IsSrcTypeAV(enInputSourceType))
    {
        enPQSourceType = PQ_INPUT_SOURCE_CVBS; // AV
    }
    else if(IsSrcTypeScart(enInputSourceType))
    {
        enPQSourceType = PQ_INPUT_SOURCE_SCART; // SCART
    }
    else if(IsSrcTypeSV(enInputSourceType))
    {
        enPQSourceType = PQ_INPUT_SOURCE_SVIDEO; // SV
    }
    else if(IsSrcTypeHDMI(enInputSourceType))
    {
        enPQSourceType = PQ_INPUT_SOURCE_HDMI; // HDMI
    }
    else if(IsSrcTypeYPbPr(enInputSourceType))
    {
        enPQSourceType = PQ_INPUT_SOURCE_YPBPR; // COMP
    }
    else if(IsSrcTypeJpeg(enInputSourceType))
    {
        enPQSourceType = PQ_INPUT_SOURCE_JPEG; // JPEG
    }
    else if(IsSrcTypeStorage(enInputSourceType))
    {
        enPQSourceType = PQ_INPUT_SOURCE_STORAGE; // Storage
    }
    else if(IsSrcTypeCapture(enInputSourceType)
            ||IsSrcTypeSC0DI(enInputSourceType))
    {
        enPQSourceType = PQ_INPUT_SOURCE_DTV; // Temperarily use DTV
    }
    else if(enInputSourceType == INPUT_SOURCE_KTV)
    {
        enPQSourceType = PQ_INPUT_SOURCE_KTV; // KTV
    }
    else
    {
        printf("[%s][%d] Input source select to default when source is %d.\n",__FUNCTION__,__LINE__,enInputSourceType);
    }

    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "PQSetSource:Input src=%u, win=%u,\n", enInputSourceType, eWindow);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "PQSetSource:   PQ src=%u, win=%u\n", enPQSourceType, eWindow);

    stPQModeInfo.bFBL               = pstXC_WinTime_Info->bFBL;
    stPQModeInfo.bInterlace         = pstXC_WinTime_Info->bInterlace;
    stPQModeInfo.u16input_hsize     = pstXC_WinTime_Info->stCapWin.width;
    stPQModeInfo.u16input_vsize     = pstXC_WinTime_Info->stCapWin.height;
    stPQModeInfo.u16input_vfreq     = pstXC_WinTime_Info->u16InputVFreq;
    stPQModeInfo.u16input_vtotal    = pstXC_WinTime_Info->u16InputVTotal;
    stPQModeInfo.u16ouput_vfreq     = pstXC_WinTime_Info->u16InputVFreq;
    stPQModeInfo.u16display_hsize   = pstXC_WinTime_Info->stDispWin.width;
    stPQModeInfo.u16display_vsize   = pstXC_WinTime_Info->stDispWin.height;
    stPQModeInfo.u16cropwin_hsize   = pstXC_WinTime_Info->stCropWin.width;
    stPQModeInfo.u16cropwin_vsize   = pstXC_WinTime_Info->stCropWin.height;

#ifdef ENABLE_TV_SC2_PQ
    if (psXCInstPri->u32DeviceID == PQ_XC_ID_0)
#endif
    {
        if ((s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_modeInfo != NULL) && (!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow)))
        {
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
            s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_modeInfo((psXCInstPri->u32DeviceID == 1)?PQ_SUB_WINDOW:PQ_MAIN_WINDOW, enPQSourceType, &stPQModeInfo);
#else
            s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_modeInfo(ePQWin, enPQSourceType, &stPQModeInfo);
#endif
        }
    }
#ifdef ENABLE_TV_SC2_PQ
    else if (psXCInstPri->u32DeviceID == PQ_XC_ID_1)
    {
        if ((s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_modeInfo_ex != NULL) && (!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow)))
        {
            s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_modeInfo_ex(psXCInstPri->u32DeviceID, ePQWin, enPQSourceType, &stPQModeInfo);
        }
    }
#endif

#ifdef ENABLE_TV_SC2_PQ
    if (psXCInstPri->u32DeviceID == PQ_XC_ID_0)
#endif
    {
        if ((s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_deside_srctype != NULL) && (!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow)))
        {
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
            s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_deside_srctype((psXCInstPri->u32DeviceID == 1)?PQ_SUB_WINDOW:PQ_MAIN_WINDOW, enPQSourceType);
            UNUSED(ePQWin);
#else
            s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_deside_srctype(ePQWin, enPQSourceType);
#endif
        }
    }
#ifdef ENABLE_TV_SC2_PQ
    else if (psXCInstPri->u32DeviceID == PQ_XC_ID_1)
    {
        if ((s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_deside_srctype_ex != NULL) && (!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow)))
        {
            s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_deside_srctype_ex(psXCInstPri->u32DeviceID, ePQWin, enPQSourceType);
        }
    }
#endif

}

///////////////////////////////////////////////////
/// ADC API
///////////////////////////////////////////////////
void MDrv_XC_ADC_GetPQADCSamplingInfo(void *pInstance, INPUT_SOURCE_TYPE_t enSourceType, XC_InternalStatus *pSrcInfo, PQ_ADC_SAMPLING_INFO *pstPQADCSamplingInfo, SCALER_WIN eWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    //MS_U16 u16Width;//, u16RealWidth;
    if ( IsSrcTypeVga(enSourceType) || IsSrcTypeYPbPr(enSourceType) )
    {
        if(pSrcInfo->bHDuplicate)
        {
#ifdef ENABLE_TV_SC2_PQ
            if (psXCInstPri->u32DeviceID == PQ_XC_ID_0)
#endif
            {
                if(s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_ioctl)
                {
                    pstPQADCSamplingInfo->enPQSourceType = IsSrcTypeVga(enSourceType) ? PQ_INPUT_SOURCE_VGA : PQ_INPUT_SOURCE_YPBPR;
                    pstPQADCSamplingInfo->stPQModeInfo.bFBL               = pSrcInfo->bFBL;
                    pstPQADCSamplingInfo->stPQModeInfo.bInterlace         = pSrcInfo->bInterlace;
                    pstPQADCSamplingInfo->stPQModeInfo.u16input_hsize     = pSrcInfo->stCapWin.width;
                    pstPQADCSamplingInfo->stPQModeInfo.u16input_vsize     = pSrcInfo->stCapWin.height;
                    pstPQADCSamplingInfo->stPQModeInfo.u16input_vfreq     = pSrcInfo->u16InputVFreq;
                    pstPQADCSamplingInfo->stPQModeInfo.u16input_vtotal    = pSrcInfo->u16InputVTotal;
                    pstPQADCSamplingInfo->stPQModeInfo.u16ouput_vfreq     = pSrcInfo->u16InputVFreq;
                    pstPQADCSamplingInfo->stPQModeInfo.u16display_hsize   = pSrcInfo->stDispWin.width;
                    pstPQADCSamplingInfo->stPQModeInfo.u16display_vsize   = pSrcInfo->stDispWin.height;
                    pstPQADCSamplingInfo->stPQModeInfo.u16cropwin_hsize   = pSrcInfo->stCropWin.width;
                    pstPQADCSamplingInfo->stPQModeInfo.u16cropwin_vsize   = pSrcInfo->stCropWin.height;

#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
                    if(s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_ioctl((psXCInstPri->u32DeviceID == 1)?PQ_SUB_WINDOW:PQ_MAIN_WINDOW, E_PQ_IOCTL_ADC_SAMPLING, (void *)pstPQADCSamplingInfo, sizeof(PQ_ADC_SAMPLING_INFO)) == FALSE)
#else
                    if(s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_ioctl((PQ_WIN)eWindow, E_PQ_IOCTL_ADC_SAMPLING, (void *)pstPQADCSamplingInfo, sizeof(PQ_ADC_SAMPLING_INFO)) == FALSE)
#endif
                    {
                        pstPQADCSamplingInfo->u16ratio = 2;
                    }
                }
                else
                {
                    pstPQADCSamplingInfo->u16ratio = 2;
                }
            }
#ifdef ENABLE_TV_SC2_PQ
            else if (psXCInstPri->u32DeviceID == PQ_XC_ID_1)
            {
                if(s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_ioctl_ex)
                {
                    pstPQADCSamplingInfo->enPQSourceType = IsSrcTypeVga(enSourceType) ? PQ_INPUT_SOURCE_VGA : PQ_INPUT_SOURCE_YPBPR;
                    pstPQADCSamplingInfo->stPQModeInfo.bFBL               = pSrcInfo->bFBL;
                    pstPQADCSamplingInfo->stPQModeInfo.bInterlace         = pSrcInfo->bInterlace;
                    pstPQADCSamplingInfo->stPQModeInfo.u16input_hsize     = pSrcInfo->stCapWin.width;
                    pstPQADCSamplingInfo->stPQModeInfo.u16input_vsize     = pSrcInfo->stCapWin.height;
                    pstPQADCSamplingInfo->stPQModeInfo.u16input_vfreq     = pSrcInfo->u16InputVFreq;
                    pstPQADCSamplingInfo->stPQModeInfo.u16input_vtotal    = pSrcInfo->u16InputVTotal;
                    pstPQADCSamplingInfo->stPQModeInfo.u16ouput_vfreq     = pSrcInfo->u16InputVFreq;
                    pstPQADCSamplingInfo->stPQModeInfo.u16display_hsize   = pSrcInfo->stDispWin.width;
                    pstPQADCSamplingInfo->stPQModeInfo.u16display_vsize   = pSrcInfo->stDispWin.height;
                    pstPQADCSamplingInfo->stPQModeInfo.u16cropwin_hsize   = pSrcInfo->stCropWin.width;
                    pstPQADCSamplingInfo->stPQModeInfo.u16cropwin_vsize   = pSrcInfo->stCropWin.height;

                    if(s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_ioctl_ex(psXCInstPri->u32DeviceID, (PQ_WIN)eWindow, E_PQ_IOCTL_ADC_SAMPLING, (void *)&stPQADCSamplingInfo, sizeof(PQ_ADC_SAMPLING_INFO)) == FALSE)
                    {
                        pstPQADCSamplingInfo->u16ratio = 2;
                    }
                }
                else
                {
                    pstPQADCSamplingInfo->u16ratio = 2;
                }
            }
#endif
            //u16RealWidth = u16Width / pstPQADCSamplingInfo->u16ratio;
        }
    }
}

static void _MDrv_XC_ADC_SetMode(void *pInstance, INPUT_SOURCE_TYPE_t enSourceType, XC_InternalStatus *pSrcInfo, PQ_ADC_SAMPLING_INFO *pstPQADCSamplingInfo, SCALER_WIN eWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    //MS_U16 u16Width;//, u16RealWidth;
    MS_U16 u16PixClk = 0, u16RealPixClk = 0; // Add the initial value
    MS_U16 u16HorizontalTotal = 0;
    XC_IP_SYNC_STATUS sXC_Sync_Status;

    memset(&sXC_Sync_Status, 0, sizeof(XC_IP_SYNC_STATUS));

    // check SOG Mux status, if there is multi source using the same pad
    // return ture and to do disable online SOG
    if(MDrv_XC_ADC_CheckSOGMux(pInstance, enSourceType))
    {
        // cvbs and ypbpr use the same pad, disable online sog
        MDrv_XC_ADC_DisableSOGMux(pInstance);
    }

    if ( IsSrcTypeVga(enSourceType) || IsSrcTypeYPbPr(enSourceType) )
    {
        // get sync status
        _XC_ENTRY(pInstance);
        MDrv_XC_GetSyncStatus(pInstance, enSourceType, &sXC_Sync_Status, eWindow);
        _XC_RETURN(pInstance);

        // Set Hsync polarity
        MDrv_XC_ADC_hpolarity_setting(pInstance, sXC_Sync_Status.u8SyncStatus & XC_MD_HSYNC_POR_BIT);

        // Set user default phase
        MDrv_XC_ADC_SetPhaseEx(pInstance, pSrcInfo->u16DefaultPhase); // setting ADC phase

        //Sog Filter behavior is before the ADC double sampling, so for Sog Filter, we don't need to use
        //double horizontal total for the bandwidth calculation.
        _XC_ENTRY(pInstance);
        u16RealPixClk = MDrv_XC_cal_pixclk(pInstance, pSrcInfo->u16DefaultHtotal, eWindow, enSourceType);
        _XC_RETURN(pInstance);

        MDrv_XC_ADC_Reload(pInstance, enSourceType, u16RealPixClk, pSrcInfo->u16DefaultHtotal, pstPQADCSamplingInfo->u16ratio);

        // Get Htt
        if(pSrcInfo->bHDuplicate)
        {
            u16HorizontalTotal = pSrcInfo->u16DefaultHtotal * pstPQADCSamplingInfo->u16ratio; // for better quality
        }
        else
        {
            u16HorizontalTotal = pSrcInfo->u16DefaultHtotal;
        }

        // Calculate pixel clock, the unit is MHz
        _XC_ENTRY(pInstance);
        u16PixClk = MDrv_XC_cal_pixclk(pInstance, u16HorizontalTotal, eWindow, enSourceType);
        _XC_RETURN(pInstance);

        if (IsSrcTypeVga(enSourceType))
        {
            // ADC reset, Patch PLL unlock problem.
            MDrv_XC_ADC_reset(pInstance, REST_ADC);
        }
        else
        {
            MDrv_XC_ADC_clamp_placement_setting(pInstance, u16PixClk);
        }

        MDrv_XC_ADC_vco_ctrl(pInstance, IsSrcTypeHDMI(enSourceType), u16PixClk);

        if(pstPQADCSamplingInfo->u16ratio > 0)
        {
            pSrcInfo->stCapWin.x = pSrcInfo->stCapWin.x | (pstPQADCSamplingInfo->u16ratio-1);
        }
    }
    else
    {
        MDrv_XC_ADC_Reload(pInstance, enSourceType, u16RealPixClk, pSrcInfo->u16DefaultHtotal, pstPQADCSamplingInfo->u16ratio);
    }
}

void MDrv_XC_ADC_SetMode(void *pInstance, INPUT_SOURCE_TYPE_t enSourceType, XC_InternalStatus *pSrcInfo, PQ_ADC_SAMPLING_INFO *pstPQADCSamplingInfo, SCALER_WIN eWindow)
{
    PQ_ADC_SAMPLING_INFO stPQADCSamplingInfo;

    // Give a initial setting.
    memset(&stPQADCSamplingInfo,0,sizeof(PQ_ADC_SAMPLING_INFO));
    if ( IsSrcTypeVga(enSourceType) || IsSrcTypeYPbPr(enSourceType) )
    {
        if(pSrcInfo->bHDuplicate)
        {
            stPQADCSamplingInfo = *pstPQADCSamplingInfo;

        }
        else
        {
            stPQADCSamplingInfo.u16ratio = 1;
        }
    }
    _MDrv_XC_ADC_SetMode(pInstance, enSourceType, pSrcInfo, &stPQADCSamplingInfo, eWindow);
}

void MApi_XC_ADC_SwitchSource(void* pInstance, MS_BOOL bUpdateStatus)
{
    MDrv_ADC_SourceSwitch(pInstance,bUpdateStatus);
}

static MS_BOOL _Mdrv_XC_CopySetWinInfo(void *pInstance, XC_SETWIN_INFO *pstXC_SetWin_Info, SCALER_WIN eWindow)
{
    if(pstXC_SetWin_Info == NULL)
    {
        return FALSE;
    }
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    gSrcInfo[eWindow].enInputSourceType     = pstXC_SetWin_Info->enInputSourceType;

    gSrcInfo[eWindow].stCapWin.x            = pstXC_SetWin_Info->stCapWin.x;
    gSrcInfo[eWindow].stCapWin.y            = pstXC_SetWin_Info->stCapWin.y;
    gSrcInfo[eWindow].stCapWin.width        = pstXC_SetWin_Info->stCapWin.width;
    gSrcInfo[eWindow].stCapWin.height       = pstXC_SetWin_Info->stCapWin.height;

    gSrcInfo[eWindow].stDispWin.x           = pstXC_SetWin_Info->stDispWin.x;
    gSrcInfo[eWindow].stDispWin.y           = pstXC_SetWin_Info->stDispWin.y;
    gSrcInfo[eWindow].stDispWin.width       = pstXC_SetWin_Info->stDispWin.width;
    gSrcInfo[eWindow].stDispWin.height      = pstXC_SetWin_Info->stDispWin.height;

    gSrcInfo[eWindow].stCropWin.x           = pstXC_SetWin_Info->stCropWin.x;
    gSrcInfo[eWindow].stCropWin.y           = pstXC_SetWin_Info->stCropWin.y;
    gSrcInfo[eWindow].stCropWin.width       = pstXC_SetWin_Info->stCropWin.width;
    gSrcInfo[eWindow].stCropWin.height      = pstXC_SetWin_Info->stCropWin.height;

    gSrcInfo[eWindow].bInterlace            = pstXC_SetWin_Info->bInterlace;
    gSrcInfo[eWindow].bHDuplicate           = pstXC_SetWin_Info->bHDuplicate;
    gSrcInfo[eWindow].u16InputVFreq         = pstXC_SetWin_Info->u16InputVFreq;
    gSrcInfo[eWindow].u16InputVTotal        = pstXC_SetWin_Info->u16InputVTotal;
    gSrcInfo[eWindow].u16DefaultHtotal      = pstXC_SetWin_Info->u16DefaultHtotal;
    if(pstXC_SetWin_Info->u16DefaultPhase != 0)
    {
        gSrcInfo[eWindow].u16DefaultPhase       = pstXC_SetWin_Info->u16DefaultPhase;
    }
    else
    {
        gSrcInfo[eWindow].u16DefaultPhase       = pstXC_SetWin_Info->u8DefaultPhase;//compatible for old AP code
    }
    gSrcInfo[eWindow].bHCusScaling          = pstXC_SetWin_Info->bHCusScaling;
    gSrcInfo[eWindow].u16HCusScalingSrc     = pstXC_SetWin_Info->u16HCusScalingSrc;
    gSrcInfo[eWindow].u16HCusScalingDst     = pstXC_SetWin_Info->u16HCusScalingDst;
    gSrcInfo[eWindow].bVCusScaling          = pstXC_SetWin_Info->bVCusScaling;
    gSrcInfo[eWindow].u16VCusScalingSrc     = pstXC_SetWin_Info->u16VCusScalingSrc;
    gSrcInfo[eWindow].u16VCusScalingDst     = pstXC_SetWin_Info->u16VCusScalingDst;
    gSrcInfo[eWindow].bDisplayNineLattice   = pstXC_SetWin_Info->bDisplayNineLattice;

    return TRUE;
}

static void _MDrv_XC_CheckCropWin(void *pInstance, XC_SETWIN_INFO *pstXC_SetWin_Info, SCALER_WIN eWindow)
{
    if(pstXC_SetWin_Info == NULL)
    {
        printf("Invalid Crop Window access\n");
        return;
    }

    if(pstXC_SetWin_Info->stCropWin.width > pstXC_SetWin_Info->stCapWin.width)
    {
        pstXC_SetWin_Info->stCropWin.width = pstXC_SetWin_Info->stCapWin.width;
    }

    if(pstXC_SetWin_Info->stCropWin.height > pstXC_SetWin_Info->stCapWin.height)
    {
        pstXC_SetWin_Info->stCropWin.height = pstXC_SetWin_Info->stCapWin.height;
    }

    if(pstXC_SetWin_Info->stCropWin.x > (pstXC_SetWin_Info->stCapWin.width - pstXC_SetWin_Info->stCropWin.width))
    {
        pstXC_SetWin_Info->stCropWin.x = pstXC_SetWin_Info->stCapWin.width - pstXC_SetWin_Info->stCropWin.width;
    }
    if(pstXC_SetWin_Info->stCropWin.y > (pstXC_SetWin_Info->stCapWin.height - pstXC_SetWin_Info->stCropWin.height))
    {
        pstXC_SetWin_Info->stCropWin.y = pstXC_SetWin_Info->stCapWin.height - pstXC_SetWin_Info->stCropWin.height;
    }
}

static void _MDrv_XC_CheckDisplayWin(void *pInstance, XC_SETWIN_INFO *pstXC_SetWin_Info, SCALER_WIN eWindow)
{
    if(eWindow >= WINDOW_NUM)
    {
        printf("Invalid window!\n");
    }
    if(pstXC_SetWin_Info == NULL)
    {
        printf("Invalid Display Window access\n");
        return;
    }

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

#if (XC_SUPPORT_2P_MODE == TRUE)
    if (psXCInstPri->u32DeviceID == E_XC_DEVICE1)
    {
        // sc1 composer sc0 need use 2p in hstart and width
        pstXC_SetWin_Info->stDispWin.x = ((pstXC_SetWin_Info->stDispWin.x + 1) & ~0x01);
        pstXC_SetWin_Info->stDispWin.width = (pstXC_SetWin_Info->stDispWin.width  & ~0x01);
    }
#endif

    if(E_XC_DEVICE1 == psXCInstPri->u32DeviceID && XC_DEVICE1_IS_INTERLACE_OUT)
    {
        pstXC_SetWin_Info->stDispWin.y /= 2;
        pstXC_SetWin_Info->stDispWin.height /= 2;
    }

    // TGEN does not allow display window height <= 5 or it cannot decide VDE_End
    if (pstXC_SetWin_Info->stDispWin.height <= 5)
    {
        pstXC_SetWin_Info->stDispWin.height = 5;
    }


    if ((pstXC_SetWin_Info->stDispWin.x
           + pstXC_SetWin_Info->stDispWin.width
           + pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HStart)
         > pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HTotal)
    {
        pstXC_SetWin_Info->stDispWin.width = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HTotal
                                             - pstXC_SetWin_Info->stDispWin.x
                                             - pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HStart;
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Display Window size is exceeded Htotal, display window horizontal end is limited to Htotal\n");
    }

    if ((pstXC_SetWin_Info->stDispWin.y
           + pstXC_SetWin_Info->stDispWin.height
           + pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VStart)
        > pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VTotal)
    {
        pstXC_SetWin_Info->stDispWin.height = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VTotal
                                             - pstXC_SetWin_Info->stDispWin.y
                                             - pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VStart;
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Display Window size is exceeded Vtotal, display window vertical end is limited to Vtotal\n");
    }

    //patch for monaco,monet 3D SC HW limitation, if input is SBS,PA, CB, capture window's width must be align by 4
    if((E_XC_3D_INPUT_SIDE_BY_SIDE_HALF == MDrv_XC_Get_3D_Input_Mode(pInstance, MAIN_WINDOW))
        || (E_XC_3D_INPUT_PIXEL_ALTERNATIVE == MDrv_XC_Get_3D_Input_Mode(pInstance, MAIN_WINDOW))
        || (E_XC_3D_INPUT_CHECK_BORAD == MDrv_XC_Get_3D_Input_Mode(pInstance, MAIN_WINDOW)))
    {
        pstXC_SetWin_Info->stCapWin.width &= ~0x03;
    }

#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
    // FRC 3D enable: SC transforms to TB, FRC transforms to FA/LA, 3D mode is FA/LA in SDK.
    if(MHal_FRC_IsEnableFRC3D(pInstance,MAIN_WINDOW))
    {
        if((MDrv_XC_Get_3D_Output_Mode(pInstance)== E_XC_3D_OUTPUT_TOP_BOTTOM)
            &&(pXCResourcePrivate->stdrvXC_3D._bLAToTB))
        {
            pstXC_SetWin_Info->stDispWin.y /= 2;
        }
    }
#endif
    MHal_XC_CheckDisplayWin(pstXC_SetWin_Info,eWindow);
}

static void _MDrv_XC_CheckCaptureWin(void *pInstance, XC_SETWIN_INFO *pstXC_SetWin_Info, SCALER_WIN eWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    MS_BOOL bFramePacking = FALSE;
    if(pstXC_SetWin_Info == NULL)
    {
        printf("Invalid Capture Window access\n");
        return;
    }

#if (XC_SUPPORT_2P_MODE == TRUE)
    // 2p mode need width as even
    pstXC_SetWin_Info->stCapWin.width = ((pstXC_SetWin_Info->stCapWin.width + 1) & ~0x01);
#endif

    //patch for monaco 3D SC HW limitation, if input is SBS, 2D_HW, PA, CB, capture window's width must be align by 4
    #if(HW_DESIGN_3D_VER == 5) //monaco mode
        if((E_XC_3D_INPUT_SIDE_BY_SIDE_HALF == MDrv_XC_Get_3D_Input_Mode(pInstance, MAIN_WINDOW))
            || (E_XC_3D_INPUT_PIXEL_ALTERNATIVE == MDrv_XC_Get_3D_Input_Mode(pInstance, MAIN_WINDOW))
            || (E_XC_3D_INPUT_CHECK_BORAD == MDrv_XC_Get_3D_Input_Mode(pInstance, MAIN_WINDOW)))
        {
            pstXC_SetWin_Info->stCapWin.width &= ~0x03;
        }
    #endif

    //if input is LA or TB, capture.height must be even.
    if((E_XC_3D_INPUT_LINE_ALTERNATIVE == MDrv_XC_Get_3D_Input_Mode(pInstance, eWindow))
        || (E_XC_3D_INPUT_TOP_BOTTOM == MDrv_XC_Get_3D_Input_Mode(pInstance, eWindow)))
    {
        bFramePacking = MDrv_SC_3D_IsFramePaking(pInstance,
                                                 pstXC_SetWin_Info->stCapWin.width,
                                                 pstXC_SetWin_Info->stCapWin.height,
                                                 pstXC_SetWin_Info->bInterlace);
        if(!bFramePacking)
        {
            pstXC_SetWin_Info->stCapWin.height &= ~0x01;
        }
    }
#if (XC_SUPPORT_FASTSWITCH_BY_IP1GP==TRUE)
    MHal_XC_SetOfflineSource(pInstance, eWindow, gSrcInfo[eWindow].enInputSourceType);
#endif

#if(HW_DESIGN_4K2K_VER == 7) //Kano Patch
    if((E_XC_DEVICE1 == psXCInstPri->u32DeviceID) &&
        (IsSrcTypeCapture(gSrcInfo[eWindow].enInputSourceType)
        ||IsSrcTypeSC0DI(gSrcInfo[eWindow].enInputSourceType)))
    {
        // H2V2 enabled
        if(SC_R2BYTEMSK(E_XC_DEVICE0, REG_SC_BK6A_20_L, BIT(0)) == BIT(0))
        {
            // When Kano H2V2 is enabled, H sync and DE will randomly shift 1 pixel
            // Need to switch to DE bypass mode to make sure video is always aligned
            // Attention, in DE bypass mode, capture window setting will be useless
            //This is SW patch for H2V2SD, U02 does not need
            if((H2V2_PIXEL_SHIFT_PATCH_USING_IP_DE_BYPASS_MODE == TRUE) && (MDrv_SYS_GetChipRev() == 0x00))
            {
                Hal_SC_ip_set_de_bypass_mode(pInstance, TRUE, eWindow);
            }
            pstXC_SetWin_Info->stCapWin.x = (pstXC_SetWin_Info->stCapWin.x / 2) - 59; // H_DE has 60 pixels delay
            pstXC_SetWin_Info->stCapWin.y /= 2;
            pstXC_SetWin_Info->stCapWin.width /= 2;
            pstXC_SetWin_Info->stCapWin.height /= 2;
        }
        // H2V2 bypassed
        else
        {
            //This is SW patch for H2V2SD, U02 does not need
            if((H2V2_PIXEL_SHIFT_PATCH_USING_IP_DE_BYPASS_MODE == TRUE) && (MDrv_SYS_GetChipRev() == 0x00))
            {
                Hal_SC_ip_set_de_bypass_mode(pInstance, FALSE, eWindow);
            }
            pstXC_SetWin_Info->stCapWin.x += Hal_SC_ip_adjust_capture_h_start(pInstance, eWindow);// H_DE adjusted by chip
        }
    }
#endif
}

MS_BOOL MDrv_XC_Get_VirtualBox_Info(void *pInstance,XC_VBOX_INFO *pstXC_VboxInfo)
{
    MS_U8 u8FrameCnt = 0;

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));


    if(pstXC_VboxInfo)
    {
#ifndef DONT_USE_CMA
#if (XC_SUPPORT_CMA ==TRUE)
#if(HW_DESIGN_4K2K_VER == 7) // Kano
        if((psXCInstPri->u32DeviceID == 0))
#endif
        {
            if ((gSrcInfo[MAIN_WINDOW].u32PreCMAMemSCMSize[CMA_XC_SELF_MEM] + gSrcInfo[MAIN_WINDOW].u32PreCMAMemFRCMSize[CMA_XC_SELF_MEM]) != 0)
                pstXC_VboxInfo->u32FBSize[0] = (gSrcInfo[MAIN_WINDOW].u32PreCMAMemSCMSize[CMA_XC_SELF_MEM] + gSrcInfo[MAIN_WINDOW].u32PreCMAMemFRCMSize[CMA_XC_SELF_MEM]);
            else if ((gSrcInfo[MAIN_WINDOW].u32PreCMAMemSCMSize[CMA_XC_COBUFF_MEM] + gSrcInfo[MAIN_WINDOW].u32PreCMAMemFRCMSize[CMA_XC_COBUFF_MEM]) != 0)
                pstXC_VboxInfo->u32FBSize[0] = (gSrcInfo[MAIN_WINDOW].u32PreCMAMemSCMSize[CMA_XC_COBUFF_MEM] + gSrcInfo[MAIN_WINDOW].u32PreCMAMemFRCMSize[CMA_XC_COBUFF_MEM]);
        }
#else
        pstXC_VboxInfo->u32FBSize[0] = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FB_Size;
#endif
#else
        pstXC_VboxInfo->u32FBSize[0] = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FB_Size;
#endif
        pstXC_VboxInfo->u32FBSize[1] = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FB_Size;
        pstXC_VboxInfo->u16VBox_PanelHsize = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width;
        pstXC_VboxInfo->u16VBox_PanelVsize = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height;

        // temp for sub
        pstXC_VboxInfo->u16VBox_Htotal[1] = XC_FHD_VBOX_H_SIZE;
        pstXC_VboxInfo->u16VBox_Vtotal[1] = XC_FHD_VBOX_V_SIZE;

        if(IsSrcMVOP(pstXC_VboxInfo->enSourceType[0]))
        {
            if(XC_SUPPORT_4K2K)
            {
                //if 2k model, virtual box is 2k
                if(MHal_XC_Is2kModel(pInstance) == TRUE)
                {
                    pstXC_VboxInfo->u16VBox_Htotal[0] = XC_FHD_VBOX_H_SIZE;
                    pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_FHD_VBOX_V_SIZE;
                }
                else
                {
                    if (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_SAVE_MEM_MODE)
                    {
                        // in 4K2K case, the Madi mode is BK12_02 = 0x300 = 16bit/pixels
                        u8FrameCnt = (pstXC_VboxInfo->u32FBSize[0]) / (pstXC_VboxInfo->u16VBox_PanelHsize*pstXC_VboxInfo->u16VBox_PanelVsize * 16/8);
                    }
                    else
                    {
                        // in 4K2K case, the Madi mode is P_Mode10 = 20bit/pixels
                        u8FrameCnt = (pstXC_VboxInfo->u32FBSize[0]) / (pstXC_VboxInfo->u16VBox_PanelHsize*pstXC_VboxInfo->u16VBox_PanelVsize * 20/8);
                    }
                    if(u8FrameCnt >= 2)
                    {
                #if FRC_INSIDE
                    /// FRC Enable
                        if(pXCResourcePrivate->stdrvXC_MVideo_Context.bIsFRCInited)
                        {
                            #if(HW_DESIGN_4K2K_VER == 6)
                                if (pstXC_VboxInfo->bInterface[0] == FALSE)
                                {
                                    pstXC_VboxInfo->u16VBox_Htotal[0] = XC_4K2K_VBOX_H_SIZE;
                                    pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_4K2K_VBOX_V_SIZE;
                                    if(MHal_XC_IsSupportPQDSPlus())
                                    {
                                        if((pstXC_VboxInfo->u32Version >= ST_XC_VBOX_INFO_VERSION_PQDSPLUS) &&
                                           (pstXC_VboxInfo->u32MaxDriftHeight[0] <= XC_FHD_HIGH) && (pstXC_VboxInfo->u32MaxDriftWidth[0] <= XC_FHD_WIDTH) &&
                                           (pstXC_VboxInfo->u32MaxDriftHeight[0] > 0) && (pstXC_VboxInfo->u32MaxDriftWidth[0] > 0))
                                        {
                                            pstXC_VboxInfo->u16VBox_Htotal[0] = XC_FHD_VBOX_H_SIZE;
                                            pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_FHD_VBOX_V_SIZE;
                                        }
                                    }
                                }
                                else
                                {
#if (XC_SUPPORT_4K2K_I_MODE)
                                    if(IS_INPUT_MM_4K2K_I_MODE(gSrcInfo[MAIN_WINDOW].stCapWin.width,gSrcInfo[MAIN_WINDOW].stCapWin.height,gSrcInfo[MAIN_WINDOW].enInputSourceType,gSrcInfo[MAIN_WINDOW].bInterlace) == TRUE)
                                    {
                                        pstXC_VboxInfo->u16VBox_Htotal[0] = XC_4K2K_VBOX_H_SIZE;
                                        pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_4K2K_VBOX_V_SIZE;
                                    }
                                    else
#endif
                                    {
                                        if((pstXC_VboxInfo->u32Version >= ST_XC_VBOX_INFO_VERSION_DTVDS) && (MHal_XC_IsDtv4K2KI(pstXC_VboxInfo->u16SrcHSize[0],
                                            pstXC_VboxInfo->u16SrcVSize[0], pstXC_VboxInfo->bInterface[0], pstXC_VboxInfo->enSourceType[0]) == TRUE))
                                        {
                                            pstXC_VboxInfo->u16VBox_Htotal[0] = XC_4K2K_VBOX_H_SIZE;
                                            pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_4K2K_VBOX_V_SIZE;
                                        }
                                        else
                                        {
                                            pstXC_VboxInfo->u16VBox_Htotal[0] = XC_FHD_VBOX_H_SIZE;
                                            pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_FHD_VBOX_V_SIZE;
                                        }
                                    }
                                }
                            #else
                                if(pXCResourcePrivate->stdrvXC_MVideo_Context.enFRC_CurrentInputTiming == E_XC_FRC_InputTiming_4K2K)
                                {
                                    pstXC_VboxInfo->u16VBox_Htotal[0] = XC_4K2K_VBOX_H_SIZE;
                                    pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_4K2K_VBOX_V_SIZE;
                                }
                                else //if(pXCResourcePrivate->stdrvXC_MVideo_Context.enFRC_CurrentInputTiming == E_XC_FRC_InputTiming_2K1K)) and others
                                {
                                    // 2K2K only for Napoli with FRC_On
                                    pstXC_VboxInfo->u16VBox_Htotal[0] = XC_FHD_VBOX_H_SIZE;
                                    pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_FHD_VBOX_V_SIZE*2;
                                }
                            #endif
                        }
                        else  /// FRC Disable
                        {
                            #if(HW_DESIGN_4K2K_VER == 6) || (HW_DESIGN_4K2K_VER == 7)
                                if (pstXC_VboxInfo->bInterface[0] == FALSE)
                                {
                                    pstXC_VboxInfo->u16VBox_Htotal[0] = XC_4K2K_VBOX_H_SIZE;
                                    pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_4K2K_VBOX_V_SIZE;

                                    if(MHal_XC_IsSupportPQDSPlus())
                                    {
                                        if((pstXC_VboxInfo->u32Version >= ST_XC_VBOX_INFO_VERSION_PQDSPLUS) &&
                                           (pstXC_VboxInfo->u32MaxDriftHeight[0] <= XC_FHD_HIGH) && (pstXC_VboxInfo->u32MaxDriftWidth[0] <= XC_FHD_WIDTH) &&
                                           (pstXC_VboxInfo->u32MaxDriftHeight[0] > 0) && (pstXC_VboxInfo->u32MaxDriftWidth[0] > 0))
                                        {
                                            pstXC_VboxInfo->u16VBox_Htotal[0] = XC_FHD_VBOX_H_SIZE;
                                            pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_FHD_VBOX_V_SIZE;
                                        }
                                    }
                                }
                                else
                                {
#if (XC_SUPPORT_4K2K_I_MODE)
                                    if(IS_INPUT_MM_4K2K_I_MODE(gSrcInfo[MAIN_WINDOW].stCapWin.width,gSrcInfo[MAIN_WINDOW].stCapWin.height,gSrcInfo[MAIN_WINDOW].enInputSourceType,gSrcInfo[MAIN_WINDOW].bInterlace) == TRUE)
                                    {
                                        pstXC_VboxInfo->u16VBox_Htotal[0] = XC_4K2K_VBOX_H_SIZE;
                                        pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_4K2K_VBOX_V_SIZE;
                                    }
                                    else
#endif
                                    {
                                        if((pstXC_VboxInfo->u32Version >= ST_XC_VBOX_INFO_VERSION_DTVDS) && (MHal_XC_IsDtv4K2KI(pstXC_VboxInfo->u16SrcHSize[0],
                                            pstXC_VboxInfo->u16SrcVSize[0], pstXC_VboxInfo->bInterface[0], pstXC_VboxInfo->enSourceType[0]) == TRUE))
                                        {
                                            pstXC_VboxInfo->u16VBox_Htotal[0] = XC_4K2K_VBOX_H_SIZE;
                                            pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_4K2K_VBOX_V_SIZE;
                                        }
                                        else
                                        {
                                            pstXC_VboxInfo->u16VBox_Htotal[0] = XC_FHD_VBOX_H_SIZE;
                                            pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_FHD_VBOX_V_SIZE;
                                        }
                                    }
                                }
                            #else
                                if((pstXC_VboxInfo->u16Vfreq[0] <= 300)&&(pstXC_VboxInfo->bInterface[0] == FALSE))
                                {
                                    pstXC_VboxInfo->u16VBox_Htotal[0] = XC_4K2K_VBOX_H_SIZE;//1920; //3840;
                                    pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_4K2K_VBOX_V_SIZE;//1080; //2160;
                                }
                                else
                                {
                                    pstXC_VboxInfo->u16VBox_Htotal[0] = XC_FHD_VBOX_H_SIZE;
                                    pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_FHD_VBOX_V_SIZE;
                                }
                            #endif
                        }
                #else
                        if(!(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_LEGACY_MODE) && (pstXC_VboxInfo->bInterface[0] == FALSE) )
                        {
                                pstXC_VboxInfo->u16VBox_Htotal[0] = XC_4K2K_VBOX_H_SIZE;
                                pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_4K2K_VBOX_V_SIZE;
                        }
                        else if(!(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_LEGACY_MODE) && (pstXC_VboxInfo->bInterface[0] == TRUE) )// DI 300MHz
                        {
                                pstXC_VboxInfo->u16VBox_Htotal[0] = XC_FHD_VBOX_H_SIZE;
                                pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_FHD_VBOX_V_SIZE;
                        }
                        else // exp : Nike, Monaco_legacy_mode
                        {
                            if((pstXC_VboxInfo->u16Vfreq[0] <= 300)&&(pstXC_VboxInfo->bInterface[0] == FALSE))
                            {
                                pstXC_VboxInfo->u16VBox_Htotal[0] = XC_4K2K_VBOX_H_SIZE;
                                pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_4K2K_VBOX_V_SIZE;
                            }
                            else
                            {
                                pstXC_VboxInfo->u16VBox_Htotal[0] = XC_FHD_VBOX_H_SIZE;
                                pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_FHD_VBOX_V_SIZE;
                            }
                        }
            #endif
                    }
                    else
                    {
                #if (FRC_INSIDE &&(HW_DESIGN_4K2K_VER != 6))
                        if(pXCResourcePrivate->stdrvXC_MVideo_Context.bIsFRCInited
                             &&(pXCResourcePrivate->stdrvXC_MVideo_Context.enFRC_CurrentInputTiming == E_XC_FRC_InputTiming_2K1K))
                        {
                            // 2K2K only for Napoli with FRC_On
                            pstXC_VboxInfo->u16VBox_Htotal[0] = XC_FHD_VBOX_H_SIZE;
                            pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_FHD_VBOX_V_SIZE*2;
                        }
                        else
                #endif

                        #if(HW_DESIGN_4K2K_VER == 6)
                        if(pstXC_VboxInfo->bInterface[0] == FALSE)//p mode//Monet: RFBl/FBL
                        {
                            pstXC_VboxInfo->u16VBox_Htotal[0] = XC_4K2K_VBOX_H_SIZE;
                            pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_4K2K_VBOX_V_SIZE;

                            if(MHal_XC_IsSupportPQDSPlus())
                            {
                                if((pstXC_VboxInfo->u32Version >= ST_XC_VBOX_INFO_VERSION_PQDSPLUS) &&
                                    (pstXC_VboxInfo->u32MaxDriftHeight[0] <= XC_FHD_HIGH) && (pstXC_VboxInfo->u32MaxDriftWidth[0] <= XC_FHD_WIDTH) &&
                                    (pstXC_VboxInfo->u32MaxDriftHeight[0] > 0) && (pstXC_VboxInfo->u32MaxDriftWidth[0] > 0))
                                {
                                    pstXC_VboxInfo->u16VBox_Htotal[0] = XC_FHD_VBOX_H_SIZE;
                                    pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_FHD_VBOX_V_SIZE;
                                }
                            }
                        }
                        else//i mode
                        {
                            if((pstXC_VboxInfo->u32Version >= ST_XC_VBOX_INFO_VERSION_DTVDS) && (MHal_XC_IsDtv4K2KI(pstXC_VboxInfo->u16SrcHSize[0],
                                pstXC_VboxInfo->u16SrcVSize[0], pstXC_VboxInfo->bInterface[0], pstXC_VboxInfo->enSourceType[0]) == TRUE))
                            {
                                pstXC_VboxInfo->u16VBox_Htotal[0] = XC_4K2K_VBOX_H_SIZE;
                                pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_4K2K_VBOX_V_SIZE;
                            }
                            else
                            {
                                pstXC_VboxInfo->u16VBox_Htotal[0] = XC_FHD_VBOX_H_SIZE;
                                pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_FHD_VBOX_V_SIZE;
                            }
                        }
                        #else
                        {
                            pstXC_VboxInfo->u16VBox_Htotal[0] = XC_FHD_VBOX_H_SIZE;
                            pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_FHD_VBOX_V_SIZE;
                        }
                        #endif
                    }
                }
            }
            else
            {
                pstXC_VboxInfo->u16VBox_Htotal[0] = XC_FHD_VBOX_H_SIZE;
                pstXC_VboxInfo->u16VBox_Vtotal[0] = XC_FHD_VBOX_V_SIZE;
            }
        }
        else
        {
            if(MHal_XC_Is2kModel(pInstance) == TRUE)
            {
                pstXC_VboxInfo->u16VBox_Htotal[0] = (gSrcInfo[0].stCapWin.width > XC_FHD_VBOX_H_SIZE)?XC_FHD_VBOX_H_SIZE:gSrcInfo[0].stCapWin.width;
                pstXC_VboxInfo->u16VBox_Vtotal[0] = (gSrcInfo[0].stCapWin.height > XC_FHD_VBOX_H_SIZE)?XC_FHD_VBOX_V_SIZE:gSrcInfo[0].stCapWin.height;
            }
            else
            {
                pstXC_VboxInfo->u16VBox_Htotal[0] = (gSrcInfo[0].stCapWin.width > XC_4K2K_VBOX_H_SIZE)?XC_4K2K_VBOX_H_SIZE:gSrcInfo[0].stCapWin.width;
                pstXC_VboxInfo->u16VBox_Vtotal[0] = (gSrcInfo[0].stCapWin.height > XC_4K2K_VBOX_V_SIZE)?XC_4K2K_VBOX_V_SIZE:gSrcInfo[0].stCapWin.height;
            }
        }

        return TRUE;
    }
    else
    {
        return FALSE;
    }

}

E_APIXC_ReturnValue MApi_XC_Get_VirtualBox_Info_U2(void* pInstance, XC_VBOX_INFO *pstXC_VboxInfo)
{
    MS_BOOL bRet = FALSE;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);

    bRet = MDrv_XC_Get_VirtualBox_Info(pInstance,pstXC_VboxInfo);

    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    if (bRet)
    {
        return E_APIXC_RET_OK;
    }
    else
    {
        return E_APIXC_RET_FAIL;
    }
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_Get_VirtualBox_Info(XC_VBOX_INFO *pstXC_VboxInfo)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_Get_VBOX_Info XCArgs;
    XCArgs.pstXC_VboxInfo = pstXC_VboxInfo;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_VBOX_INFO, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
MS_BOOL MDrv_XC_ConfigCMA(void *pInstance, XC_CMA_CONFIG *pstXC_CMA_Config, XC_CMA_CLIENT enCmaClient, MS_U32 u32DataLen, SCALER_WIN eWindow)
{
#ifndef DONT_USE_CMA
#ifndef MSOS_TYPE_NUTTX
#if (XC_SUPPORT_CMA ==TRUE)
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
#if(HW_DESIGN_4K2K_VER == 7) // Kano
    if(psXCInstPri->u32DeviceID != 0)
    {
        return TRUE;
    }
#endif
    if (enCmaClient >= CMA_XC_MEM_NUM)
    {
        printf("[%s,%5d] XC CMA CMA Client error!\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    if (u32DataLen == sizeof(XC_CMA_CONFIG))
    {
        memcpy(&gSrcInfo[eWindow].Status2.stXCConfigCMA[enCmaClient], pstXC_CMA_Config, u32DataLen);
    }
    else
    {
        printf("[%s,%5d] XC CMA u32DataLen error!\n",__FUNCTION__,__LINE__);
        return FALSE;
    }
#endif
#endif
#endif
    return TRUE;
}

MS_BOOL MApi_XC_ConfigCMA_U2(void *pInstance, XC_CMA_CONFIG *pstXC_CMA_Config, XC_CMA_CLIENT enCmaClient, MS_U32 u32DataLen, SCALER_WIN eWindow)
{
    MS_BOOL bRet = FALSE;
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);

    bRet = MDrv_XC_ConfigCMA(pInstance, pstXC_CMA_Config, enCmaClient, u32DataLen, eWindow);

    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    return bRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_ConfigCMA(XC_CMA_CONFIG *pstXC_CMA_Config, XC_CMA_CLIENT enCmaClient, MS_U32 u32DataLen, SCALER_WIN eWindow)
{
    CheckXCInstanceOpen();

#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
     if (pu32XCInst_1 == NULL)
     {
        XC_INSTANT_ATTRIBUTE stXCInstantAttribute;
        INIT_XC_INSTANT_ATTR(stXCInstantAttribute);

        stXCInstantAttribute.u32DeviceID = 1;
        if(UtopiaOpen(MODULE_XC, &pu32XCInst_1, 0, &stXCInstantAttribute) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return FALSE;
        }
      }
#endif

    stXC_CONFIG_CMA XCArgs;
    XCArgs.pstXC_CMA_Config = pstXC_CMA_Config;
    XCArgs.enCmaClient = enCmaClient;
    XCArgs.u32InitDataLen = u32DataLen;
    XCArgs.eWindow = eWindow;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_CONFIG_CMA, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}
#endif

#if (XC_SUPPORT_IOMMU == TRUE)
static MS_BOOL _MDrv_XC_GetMemoryFromMMA(void *pInstance,SCALER_WIN enWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    if(! MDrv_XC_Is_UpdateDsCmd(pInstance,enWindow))
    {
        if(pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[enWindow] == TRUE)
        {
            MS_PHY u64MMAAddr = 0;
            MS_U32 u32GetMMASize = 0;
            MS_BOOL bSecure = pXCResourcePrivate->stdrvXC_MVideo.abSecure[enWindow];//secure buffer
            u32GetMMASize = MHal_XC_GetMMAUsingBufferSize(pInstance,enWindow);
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][MMA], u32PreMMASize=%x\n",__func__,__LINE__, pXCResourcePrivate->stdrvXC_MVideo.au32PreMMAXCSize[enWindow]);

            if(u32GetMMASize != pXCResourcePrivate->stdrvXC_MVideo.au32PreMMAXCSize[enWindow])
            {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
                MHal_XC_ReleaseMMA(pInstance,enWindow);
                u64MMAAddr = MHal_XC_GetMMAAddr(pInstance,u32GetMMASize,bSecure,enWindow);
                if(u64MMAAddr != XC_INVALID_MEM)
                {
                    pXCResourcePrivate->stdrvXC_MVideo.au32XCSCMIMemSize[enWindow] = u32GetMMASize;
                    pXCResourcePrivate->stdrvXC_MVideo.au32PreMMAXCSize[enWindow] = u32GetMMASize;
                    pXCResourcePrivate->stdrvXC_MVideo.aphyXCMMABaseAddress[enWindow] = u64MMAAddr;
                }
                else
                {
                    printf("[%s][%d] alloc mma fail\n",__FUNCTION__,__LINE__);
                    return FALSE;
                }
#else
                //use thread to release and get memory.
                MS_BOOL bRet = FALSE;

                if(pXCResourcePrivate->stdrvXC_MVideo.aphyXCMMABaseAddress[enWindow] != XC_INVALID_MEM)
                {
                    bRet = Mdrv_XC_Task_WaitEventDone(
                    pInstance,
                    E_XC_MMA_EVENT_XC_PUT,
                    E_XC_MMA_EVENT_XC_PUT_DONE,
                    TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                    enWindow);
                    if(bRet == FALSE)
                    {
                        printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                    }
                }

                bRet = Mdrv_XC_Task_WaitEventDone(
                pInstance,
                E_XC_MMA_EVENT_XC_GET,
                E_XC_MMA_EVENT_XC_GET_DONE,
                TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                enWindow);
                if(bRet == FALSE)
                {
                    printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                }

                if(pXCResourcePrivate->stdrvXC_MVideo.aphyXCMMABaseAddress[enWindow] != XC_INVALID_MEM)
                {
                    u64MMAAddr = pXCResourcePrivate->stdrvXC_MVideo.aphyXCMMABaseAddress[enWindow];
                }
                else
                {
                    printf("[%s][%d] alloc mma fail\n",__FUNCTION__,__LINE__);
                    return FALSE;
                }
#endif

                MDrv_XC_SetFrameBufferAddress(pInstance,u64MMAAddr,u32GetMMASize,E_XC_BUFFER_DEFAULT,enWindow);
                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][MMA], u32MMAAddr = %td\n",__func__, __LINE__, (ptrdiff_t)u64MMAAddr);

#if (HW_DESIGN_4K2K_VER == 4)
                if ((MDrv_XC_Is_DSForceIndexEnabled(pInstance, MAIN_WINDOW)||MDrv_XC_GetDynamicScalingStatus(pInstance))
                    && IsSrcTypeStorage(gSrcInfo[enWindow].enInputSourceType))
                {
                    // MM source and DS on
                    if(!gSrcInfo[enWindow].bInterlace)
                    {
                        //DS ON , 4K2K case
                        Hal_SC_frcmw_Memory_Write_Request(pInstance, DISABLE, MAIN_WINDOW);
                    }
                    else
                    {
                        Hal_SC_frcmw_Memory_Write_Request(pInstance, ENABLE, MAIN_WINDOW);
                    }
                }
                else if ((gSrcInfo[enWindow].stCapWin.width > 3000)
                    &&(gSrcInfo[enWindow].stCapWin.height> 1050))
                {
                    Hal_SC_frcmw_Memory_Write_Request(pInstance, DISABLE, MAIN_WINDOW);
                }
                else
                {
                    Hal_SC_frcmw_Memory_Write_Request(pInstance, ENABLE, MAIN_WINDOW);
                }
#endif
            }
            else
            {
                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][MMA], the size is the same as the size of last time!\n",__func__, __LINE__);
                return TRUE;
            }
        }
    }

    return TRUE;
}

#if FRC_INSIDE
static MS_BOOL _MDrv_FRC_GetMemoryFromMMA(void *pInstance, SCALER_WIN enWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 1)
    if(psXCInstPri->u32DeviceID == 1)
    {
        printf("Error FRC not support sub window\n");
        return FALSE;
    }
#else
    if(enWindow == SUB_WINDOW)
    {
        printf("Error FRC not support sub window\n");
        return FALSE;
    }
#endif

    MS_PHY phyFRCLeftBaseaddress = 0, phyFRCRightBaseaddress = 0;
    MS_U32 u32FRCMmaSize = 0;
    MS_U8 au8FRCLeftBufTag[] = FRC_LEFT_TAG_NAME;
    MS_U8 au8FRCRightBufTag[] = FRC_RIGHT_TAG_NAME;
    MS_U8 au8FRCPQBufTag[] = FRC_PQ_TAG_NAME;
    MS_BOOL bSecure = pXCResourcePrivate->stdrvXC_MVideo.abSecure[enWindow];//FRC memory always secure
    MS_BOOL bFrcRBufTag = pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_FRC_RIGHT];
    MS_BOOL bFrcLBufTag = pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_FRC_LEFT];
    MS_BOOL bFrcPqBufTag = pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_FRC_PQ];
    MS_BOOL bAddrChanged = FALSE;

    if((pXCResourcePrivate->sthal_Optee.op_tee_xc[enWindow].isEnable == TRUE)||(! MDrv_XC_Is_UpdateDsCmd(pInstance,enWindow)))
    {
        //FRC L/R memory
        if(!(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_DUAL_MIU))
        {
            if(bFrcLBufTag == TRUE)
            {
                u32FRCMmaSize = MHal_FRC_GetFrcLMemorySize(pInstance) + MHal_FRC_GetFrcRMemorySize(pInstance);
                if(u32FRCMmaSize != pXCResourcePrivate->stdrvXC_MVideo.u32PreMMAFRC_LSize)
                {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
                    phyFRCLeftBaseaddress = MHal_FRC_GetMMAAddr(pInstance,u32FRCMmaSize,au8FRCLeftBufTag,bSecure,enWindow);
                    if(phyFRCLeftBaseaddress != XC_INVALID_MEM)
                    {
                        pXCResourcePrivate->stdrvXC_MVideo.phyFRCLeftMMABaseAddress = phyFRCLeftBaseaddress;
                        pXCResourcePrivate->stdrvXC_MVideo.u32PreMMAFRC_LSize = u32FRCMmaSize;
                    }
                    else
                    {
                        printf("[%s][%d] FRC alloc mma fail\n",__FUNCTION__,__LINE__);
                        return FALSE;
                    }
#else
                    //use thread to get memory.
                    MS_BOOL bRet = FALSE;
                    bRet = Mdrv_XC_Task_WaitEventDone(
                    pInstance,
                    E_XC_MMA_EVENT_FRC_L_GET,
                    E_XC_MMA_EVENT_FRC_L_GET_DONE,
                    TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                    enWindow);
                    if(bRet == FALSE)
                    {
                        printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                    }

                    if(pXCResourcePrivate->stdrvXC_MVideo.phyFRCLeftMMABaseAddress != XC_INVALID_MEM)
                    {
                        phyFRCLeftBaseaddress = pXCResourcePrivate->stdrvXC_MVideo.phyFRCLeftMMABaseAddress;
                    }
                    else
                    {
                        printf("[%s][%d] FRC alloc mma fail\n",__FUNCTION__,__LINE__);
                        return FALSE;
                    }
#endif
                    bAddrChanged = TRUE;
                }
            }

            phyFRCRightBaseaddress = 0;
            pXCResourcePrivate->stdrvXC_MVideo.phyFRCRightMMABaseAddress = phyFRCRightBaseaddress;
        }
        else
        {
            if((bFrcLBufTag == TRUE) && (bFrcRBufTag == TRUE))
            {
                u32FRCMmaSize = MHal_FRC_GetFrcLMemorySize(pInstance);
                if(u32FRCMmaSize != pXCResourcePrivate->stdrvXC_MVideo.u32PreMMAFRC_LSize)
                {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
                    phyFRCLeftBaseaddress = MHal_FRC_GetMMAAddr(pInstance,u32FRCMmaSize,au8FRCLeftBufTag,bSecure,enWindow);
                    if(phyFRCLeftBaseaddress != XC_INVALID_MEM)
                    {
                        pXCResourcePrivate->stdrvXC_MVideo.phyFRCLeftMMABaseAddress = phyFRCLeftBaseaddress;
                        pXCResourcePrivate->stdrvXC_MVideo.u32PreMMAFRC_LSize = u32FRCMmaSize;
                    }
                    else
                    {
                        printf("[%s][%d] FRC_L alloc mma fail\n",__FUNCTION__,__LINE__);
                        return FALSE;
                    }
#else
                    //use thread to get memory.
                    MS_BOOL bRet = FALSE;
                    bRet = Mdrv_XC_Task_WaitEventDone(
                    pInstance,
                    E_XC_MMA_EVENT_FRC_L_GET,
                    E_XC_MMA_EVENT_FRC_L_GET_DONE,
                    TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                    enWindow);
                    if(bRet == FALSE)
                    {
                        printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                    }
                    if(pXCResourcePrivate->stdrvXC_MVideo.phyFRCLeftMMABaseAddress != XC_INVALID_MEM)
                    {
                        phyFRCLeftBaseaddress = pXCResourcePrivate->stdrvXC_MVideo.phyFRCLeftMMABaseAddress;
                    }
                    else
                    {
                        printf("[%s][%d] FRC_L alloc mma fail\n",__FUNCTION__,__LINE__);
                        return FALSE;
                    }
#endif
                    bAddrChanged = TRUE;
                }
                u32FRCMmaSize = MHal_FRC_GetFrcRMemorySize(pInstance);
                if(u32FRCMmaSize != pXCResourcePrivate->stdrvXC_MVideo.u32PreMMAFRC_RSize)
                {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
                    phyFRCRightBaseaddress = MHal_FRC_GetMMAAddr(pInstance,u32FRCMmaSize,au8FRCRightBufTag,bSecure,enWindow);
                    if(phyFRCRightBaseaddress != XC_INVALID_MEM)
                    {
                        pXCResourcePrivate->stdrvXC_MVideo.phyFRCRightMMABaseAddress = phyFRCRightBaseaddress;
                        pXCResourcePrivate->stdrvXC_MVideo.u32PreMMAFRC_RSize = u32FRCMmaSize;
                    }
                    else
                    {
                        printf("[%s][%d] FRC_R alloc mma fail\n",__FUNCTION__,__LINE__);
                        return FALSE;
                    }
#else
                    //use thread to get memory.
                    MS_BOOL bRet = FALSE;
                    bRet = Mdrv_XC_Task_WaitEventDone(
                    pInstance,
                    E_XC_MMA_EVENT_FRC_R_GET,
                    E_XC_MMA_EVENT_FRC_R_GET_DONE,
                    TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                    enWindow);
                    if(bRet == FALSE)
                    {
                        printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                    }
                    if(pXCResourcePrivate->stdrvXC_MVideo.phyFRCRightMMABaseAddress != XC_INVALID_MEM)
                    {
                        phyFRCRightBaseaddress = pXCResourcePrivate->stdrvXC_MVideo.phyFRCRightMMABaseAddress;
                    }
                    else
                    {
                        printf("[%s][%d] FRC_R alloc mma fail\n",__FUNCTION__,__LINE__);
                        return FALSE;
                    }
#endif
                    bAddrChanged = TRUE;
                }
            }
        }

        //FRC PQ memory
        if(bFrcPqBufTag == TRUE)
        {
            //frc pq co-buffer with HSE depended on HW.
            if(pXCResourcePrivate->stdrvXC_MVideo.phyHSEMMABaseAddress == XC_INVALID_MEM)
            {
                u32FRCMmaSize = MHal_FRC_GetFrcPQMemorySize(pInstance);
                if(u32FRCMmaSize != pXCResourcePrivate->stdrvXC_MVideo.u32PreMMAFRC_PQSize)
                {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
                    pXCResourcePrivate->stdrvXC_MVideo.phyFRCPQMMABaseAddress = MHal_FRC_GetMMAAddr(pInstance,u32FRCMmaSize,au8FRCPQBufTag,bSecure,enWindow);
                    if(pXCResourcePrivate->stdrvXC_MVideo.phyFRCPQMMABaseAddress != XC_INVALID_MEM)
                    {
                        pXCResourcePrivate->stdrvXC_MVideo.u32PreMMAFRC_PQSize = u32FRCMmaSize;
                    }
                    else
                    {
                        printf("[%s][%d] FRC_PQ alloc mma fail\n",__FUNCTION__,__LINE__);
                        //memc levle bypass: no need PQ buffer
                        //return FALSE;
                    }
#else
                    //use thread to get memory.
                    MS_BOOL bRet = FALSE;
                    bRet = Mdrv_XC_Task_WaitEventDone(
                    pInstance,
                    E_XC_MMA_EVENT_FRC_PQ_GET,
                    E_XC_MMA_EVENT_FRC_PQ_GET_DONE,
                    TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                    enWindow);
                    if(bRet == FALSE)
                    {
                        printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                    }
                    if(pXCResourcePrivate->stdrvXC_MVideo.phyFRCPQMMABaseAddress == XC_INVALID_MEM)
                    {
                        printf("[%s][%d] FRC_PQ alloc mma fail\n",__FUNCTION__,__LINE__);
                        //memc levle bypass: no need PQ buffer
                        //return FALSE;
                    }
#endif
                    bAddrChanged = TRUE;
                }
            }
            else
            {
                // if hse iommu buffer exists, use hse buffer
                if(pXCResourcePrivate->stdrvXC_MVideo.phyFRCPQMMABaseAddress != pXCResourcePrivate->stdrvXC_MVideo.phyHSEMMABaseAddress)
                {
                    pXCResourcePrivate->stdrvXC_MVideo.phyFRCPQMMABaseAddress = pXCResourcePrivate->stdrvXC_MVideo.phyHSEMMABaseAddress;
                    pXCResourcePrivate->stdrvXC_MVideo.u32PreMMAFRC_PQSize = XC_HSE_MMA_SIZE;
                    bAddrChanged = TRUE;
                }
            }
        }

        if((bFrcLBufTag == TRUE) || (bFrcRBufTag == TRUE) || (bFrcPqBufTag == TRUE))
        {
            if (bAddrChanged == TRUE)
            {
                MHal_FRC_SetBaseAddr(pInstance, phyFRCLeftBaseaddress,phyFRCRightBaseaddress,pXCResourcePrivate->stdrvXC_MVideo.phyFRCPQMMABaseAddress);
            }
        }
    }
    return TRUE;
}
#endif

static MS_BOOL _MDrv_XC_HDMI_DMA_GetMemoryFromMMA(void *pInstance,SCALER_WIN enWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    MS_PHY u64HdmiDmaBaseaddress = 0;
    MS_BOOL bSecure = pXCResourcePrivate->stdrvXC_MVideo.abSecure[enWindow];
    XC_HDR_DMA_INIT_INFO stDMAInitInfo = {0,};
    MS_U32 u32MmaSize = XC_HDMI_DMA_MMA_SIZE;

    if(! MDrv_XC_Is_UpdateDsCmd(pInstance,enWindow))
    {
        if(pXCResourcePrivate->stdrvXC_MVideo.u32PreMMAHDMIDMASize !=  u32MmaSize)
        {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
            u64HdmiDmaBaseaddress = MHal_XC_HdmiDMAGetMMAAddr(pInstance,u32MmaSize,bSecure,enWindow);

            stDMAInitInfo.phyBaseAddr = u64HdmiDmaBaseaddress;
            stDMAInitInfo.u32Size = u32MmaSize;
            MHal_XC_HDR_Control(pInstance, E_XC_HDR_CTRL_INIT_DMA, (void *)(&stDMAInitInfo));
#else
            //use thread to get memory.
            MS_BOOL bRet = FALSE;
            bRet = Mdrv_XC_Task_WaitEventDone(
            pInstance,
            E_XC_MMA_EVENT_HDMI_DMA_GET,
            E_XC_MMA_EVENT_HDMI_DMA_GET_DONE,
            TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
            enWindow);
            if(bRet == FALSE)
            {
                printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
            }
#endif

        }
    }

    return TRUE;
}

MS_BOOL MDrv_XC_HseGetMMAAddr(void *pInstance, SCALER_WIN enWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    MS_PHY phyBaseaddress = 0;
    MS_BOOL bSecure = pXCResourcePrivate->stdrvXC_MVideo.abSecure[enWindow];
    MS_U32 u32MMASize = XC_HSE_MMA_SIZE;

    if((pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_HSE] == TRUE)
        && (pXCResourcePrivate->stdrvXC_MVideo.phyHSEMMABaseAddress == XC_INVALID_MEM))
    {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
        phyBaseaddress = MHal_XC_HseGetMMAAddr(pInstance, u32MMASize, bSecure, enWindow);
        if(phyBaseaddress != XC_INVALID_MEM)
        {
            pXCResourcePrivate->stdrvXC_MVideo.phyHSEMMABaseAddress = phyBaseaddress;
        }
        else
        {
            printf("[%s][%d] alloc mma fail\n",__FUNCTION__,__LINE__);
            return FALSE;
        }
#else
        //use thread to get memory.
        MS_BOOL bRet = FALSE;
        bRet = Mdrv_XC_Task_WaitEventDone(
               pInstance,
               E_XC_MMA_EVENT_HSE_GET,
               E_XC_MMA_EVENT_HSE_GET_DONE,
               TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
               enWindow);
        if(bRet == FALSE)
        {
            printf("[%s][%d] Wait Event fail\n", __FUNCTION__, __LINE__);
        }
        if(pXCResourcePrivate->stdrvXC_MVideo.phyHSEMMABaseAddress == XC_INVALID_MEM)
        {
            printf("[%s][%d] alloc mma fail\n",__FUNCTION__,__LINE__);
            return FALSE;
        }
#endif
    }

    return TRUE;
}

void MDrv_XC_HseReleaseMMA(void *pInstance, SCALER_WIN enWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    if(pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_HSE] == TRUE)
    {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
        MHal_XC_HseReleaseMMA(pInstance, enWindow);
#else
        MS_BOOL bRet = FALSE;
        bRet = Mdrv_XC_Task_WaitEventDone(
               pInstance,
               E_XC_MMA_EVENT_HSE_PUT,
               E_XC_MMA_EVENT_HSE_PUT_DONE,
               TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
               enWindow);
        if(bRet == FALSE)
        {
            printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
        }
#endif
    }
}

#endif


#if (defined (ANDROID)&& defined(UFO_XC_DS_PQ))
extern void MDrv_PQ_set_imode_PQDS(MS_BOOL imode);
#endif
#ifndef DONT_USE_CMA
#if (XC_SUPPORT_CMA ==TRUE)
MS_BOOL _MDrv_XC_Get_CMA_Addr(void *pInstance, MS_BOOL bIsGetCMABuff[], MS_U32 au32CMAMemSCMSize[],MS_U32 au32CMAMemFRCMSize[], MS_U32* pu32DualMiuMemSize, MS_BOOL bIsCMAChange[] ,MS_PHY u32CMAAddr[] ,MS_U32 u32DataSize ,SCALER_WIN eWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    MS_U32 u32DualMiuMemSize = *pu32DualMiuMemSize;
    MS_U32 u32TimeDiff = 0;
    if(bIsCMAChange[CMA_XC_SELF_MEM] == TRUE)
    {
        u32TimeDiff = MsOS_GetSystemTime();
        u32CMAAddr[CMA_XC_SELF_MEM] = MHal_XC_Get_CMA_Addr(pInstance, CMA_XC_SELF_MEM,  au32CMAMemSCMSize[CMA_XC_SELF_MEM]+au32CMAMemFRCMSize[CMA_XC_SELF_MEM], eWindow);
        u32TimeDiff = MsOS_GetSystemTime() - u32TimeDiff;
        if (u32CMAAddr[CMA_XC_SELF_MEM] != MSOS_INVALID_ADDR)
        {
            gSrcInfo[eWindow].u32PreCMAMemSCMSize[CMA_XC_SELF_MEM] = au32CMAMemSCMSize[CMA_XC_SELF_MEM];
            gSrcInfo[eWindow].u32PreCMAMemFRCMSize[CMA_XC_SELF_MEM] = au32CMAMemFRCMSize[CMA_XC_SELF_MEM];
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], u32CMAAddr[CMA_XC_SELF_MEM]=%x \n",__func__, __LINE__, (MS_U32)u32CMAAddr[CMA_XC_SELF_MEM]);
        }
        else
        {
            printf("[%s,%5d][CMA], cma alloc failed! return here.\n",__func__, __LINE__);
            return FALSE;
        }
    }
    if(bIsCMAChange[CMA_XC_COBUFF_MEM] == TRUE)
    {
        u32TimeDiff = MsOS_GetSystemTime();
        if (u32DualMiuMemSize != 0)
        {
            u32CMAAddr[CMA_XC_COBUFF_MEM] = MHal_XC_Get_CMA_Addr(pInstance, CMA_XC_COBUFF_MEM, u32DualMiuMemSize, eWindow);
        }
        else
        {
            u32CMAAddr[CMA_XC_COBUFF_MEM] = MHal_XC_Get_CMA_Addr(pInstance, CMA_XC_COBUFF_MEM, au32CMAMemSCMSize[CMA_XC_COBUFF_MEM]+au32CMAMemFRCMSize[CMA_XC_COBUFF_MEM], eWindow);
        }

        u32TimeDiff = MsOS_GetSystemTime() - u32TimeDiff;

        if (u32CMAAddr[CMA_XC_COBUFF_MEM] != MSOS_INVALID_ADDR)
        {
            gSrcInfo[eWindow].u32PreCMAMemSCMSize[CMA_XC_COBUFF_MEM] = au32CMAMemSCMSize[CMA_XC_COBUFF_MEM];
            gSrcInfo[eWindow].u32PreCMAMemFRCMSize[CMA_XC_COBUFF_MEM] = au32CMAMemFRCMSize[CMA_XC_COBUFF_MEM];
            gSrcInfo[eWindow].Status2.u32PreCMAMemDualSCMSize = u32DualMiuMemSize;
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], u32CMAAddr[CMA_XC_COBUFF_MEM]=%x \n",__func__, __LINE__, (MS_U32)u32CMAAddr[CMA_XC_COBUFF_MEM]);
        }
        else
        {
            printf("[%s,%5d][CMA], cma alloc failed! return here.\n",__func__, __LINE__);
            return FALSE;
        }
    }
    pXCResourcePrivate->stdrvXC_MVideo.u32CMATimeDiff = u32TimeDiff;
    return TRUE;
}

void _MDrv_XC_Release_CMA(void *pInstance, MS_BOOL bIsCMAChange[] ,MS_U32 u32DataSize ,SCALER_WIN eWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    if(bIsCMAChange[CMA_XC_SELF_MEM] == TRUE)
    {
        MHal_XC_Release_CMA(pInstance, CMA_XC_SELF_MEM, MAIN_WINDOW);
    }
    if(bIsCMAChange[CMA_XC_COBUFF_MEM] == TRUE)
    {
        MHal_XC_Release_CMA(pInstance, CMA_XC_COBUFF_MEM, MAIN_WINDOW);
    }
}

#if (XC_SUPPORT_HDR_DMA_CMA == TRUE)
static MS_BOOL _MDrv_XC_HDMI_DMA_GetMemoryFromCMA(void *pInstance,SCALER_WIN enWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    MS_PHY u64CMAAddr;
    XC_HDR_DMA_INIT_INFO stDMAInitInfo = {0,};
    MS_U32 u32MmaSize = XC_HDMI_DMA_CMA_SIZE;

    if((gSrcInfo[MAIN_WINDOW].Status2.stXCConfigCMA[CMA_XC_HDR_DMA_MEM].u32HeapID != XC_INVALID_HEAP_ID)
        && (pXCResourcePrivate->stdrvXC_MVideo.u32PreCMAHDMIDMASize !=  u32MmaSize))
    {
        u64CMAAddr = MHal_XC_Get_CMA_Addr(pInstance, CMA_XC_HDR_DMA_MEM, u32MmaSize, enWindow);
        if(u64CMAAddr != MSOS_INVALID_ADDR)
        {
            pXCResourcePrivate->stdrvXC_MVideo.phyHDMIDMACMABaseAddress = u64CMAAddr;
            pXCResourcePrivate->stdrvXC_MVideo.u32PreCMAHDMIDMASize = u32MmaSize;
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], u32MmaSize=%x u32CMAAddr[CMA_XC_HDR_DMA_MEM]=%x \n",__func__, __LINE__, u32MmaSize, (MS_U32)u64CMAAddr);

            stDMAInitInfo.phyBaseAddr = u64CMAAddr;
            stDMAInitInfo.u32Size = u32MmaSize;
            MHal_XC_HDR_Control(pInstance, E_XC_HDR_CTRL_INIT_DMA, (void *)(&stDMAInitInfo));
        }
        else
        {
            printf("[%s,%5d][CMA], get  HDR DMA CMA fail,so return failed.\n",__func__, __LINE__);
            return FALSE;
        }
    }

    return TRUE;
}
#endif

#endif
#endif

static void _MDrv_XC_SetFlowControl(void *pInstance, SCALER_WIN enWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    // Set ip flow control
    if(MHal_XC_IsSupportIP2VE(pInstance, enWindow))
    {
        MHal_XC_IP2VE_FlowControl(pInstance, TRUE);
    }
    else
    {
        MHal_XC_IP2VE_FlowControl(pInstance, FALSE); //other source
    }

    // Set UCM flow control
    if(MHal_XC_IsSupportZNR())
    {
        MHal_XC_UCM_FlowControl(pInstance);
    }
    MHal_XC_OPM_FlowControl(pInstance, enWindow);
}
static void _MDrv_XC_SetHFRComposer(void *pInstance, SCALER_WIN enWindow)
{
    //Enable HFR path clock
    if(MHal_XC_Is_Support_HFR_Path())
    {
        XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
        UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
        XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
        UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

        ST_FRC_HFR_COMPOSER_MAIN_ONLY_BYPASS stMainOnlyBypass;
        memset(&stMainOnlyBypass, 0, sizeof(ST_FRC_HFR_COMPOSER_MAIN_ONLY_BYPASS));
        ST_FRC_HFR_COMPOSER_MAIN_SUB_SWAP stMainSubSwap;
        memset(&stMainSubSwap, 0, sizeof(ST_FRC_HFR_COMPOSER_MAIN_SUB_SWAP));
        ST_FRC_HFR_COMPOSER_PATH_ENABLE stPathEnable;
        memset(&stPathEnable, 0, sizeof(ST_FRC_HFR_COMPOSER_PATH_ENABLE));

        //switch composer1,2
        if(gSrcInfo[MAIN_WINDOW].Status2.bEnableHFRPath == TRUE)
        {
            //Composer1.HFR Path+Composer2.original Path

            MHal_FRC_HFR_Set_ComposerSelection(pInstance, E_FRC_COMPOSER_BEFORE_MEMC);
            MHal_FRC_HFR_Set_Hsv_SrcSelection(pInstance, E_FRC_COMPOSER_BEFORE_MEMC);

            //Composer1.HFR Path
            stMainOnlyBypass.eType = E_FRC_COMPOSER_BEFORE_MEMC;
            stMainOnlyBypass.bMainOnlyBypass = TRUE;
            MHal_FRC_HFR_Composer_Set_MainOnlyBypass(pInstance, stMainOnlyBypass);

            stMainSubSwap.eType = E_FRC_COMPOSER_BEFORE_MEMC;
            stMainSubSwap.bMainSubSwap = TRUE;
            MHal_FRC_HFR_Composer_Set_MainSubSwap(pInstance, stMainSubSwap);

            stPathEnable.eType = E_FRC_COMPOSER_BEFORE_MEMC;
            stPathEnable.bEnable = TRUE;
            MHal_FRC_HFR_Composer_Enable_Main(pInstance, stPathEnable);


           //Composer2.original Path
            stMainOnlyBypass.eType = E_FRC_COMPOSER_AFTER_MEMC;
            stMainOnlyBypass.bMainOnlyBypass = TRUE;
            MHal_FRC_HFR_Composer_Set_MainOnlyBypass(pInstance, stMainOnlyBypass);

            stMainSubSwap.eType = E_FRC_COMPOSER_AFTER_MEMC;
            stMainSubSwap.bMainSubSwap = FALSE;
            MHal_FRC_HFR_Composer_Set_MainSubSwap(pInstance, stMainSubSwap);

            stPathEnable.eType = E_FRC_COMPOSER_AFTER_MEMC;
            stPathEnable.bEnable = TRUE;
            MHal_FRC_HFR_Composer_Enable_Main(pInstance, stPathEnable);
        }
        else if(gSrcInfo[SUB_WINDOW].Status2.bEnableHFRPath == FALSE)
        {
            //Composer1.m7632 mode+Composer2.original Path

            MHal_FRC_HFR_Set_ComposerSelection(pInstance, E_FRC_COMPOSER_BEFORE_MEMC);
            MHal_FRC_HFR_Set_Hsv_SrcSelection(pInstance, E_FRC_COMPOSER_BEFORE_MEMC);

            //Composer1.m7632 mode
            stMainOnlyBypass.eType = E_FRC_COMPOSER_BEFORE_MEMC;
            stMainOnlyBypass.bMainOnlyBypass = TRUE;
            MHal_FRC_HFR_Composer_Set_MainOnlyBypass(pInstance, stMainOnlyBypass);

            stMainSubSwap.eType = E_FRC_COMPOSER_BEFORE_MEMC;
            stMainSubSwap.bMainSubSwap = FALSE;
            MHal_FRC_HFR_Composer_Set_MainSubSwap(pInstance, stMainSubSwap);

            stPathEnable.eType = E_FRC_COMPOSER_BEFORE_MEMC;
            stPathEnable.bEnable = TRUE;
            MHal_FRC_HFR_Composer_Enable_Main(pInstance, stPathEnable);


            //Composer2.original Path
            stMainOnlyBypass.eType = E_FRC_COMPOSER_AFTER_MEMC;
            stMainOnlyBypass.bMainOnlyBypass = TRUE;
            MHal_FRC_HFR_Composer_Set_MainOnlyBypass(pInstance, stMainOnlyBypass);

            stMainSubSwap.eType = E_FRC_COMPOSER_AFTER_MEMC;
            stMainSubSwap.bMainSubSwap = FALSE;
            MHal_FRC_HFR_Composer_Set_MainSubSwap(pInstance, stMainSubSwap);

            stPathEnable.eType = E_FRC_COMPOSER_AFTER_MEMC;
            stPathEnable.bEnable = TRUE;
            MHal_FRC_HFR_Composer_Enable_Main(pInstance, stPathEnable);
        }
        else if(gSrcInfo[SUB_WINDOW].Status2.bEnableHFRPath == TRUE)
        {
            //Composer1.m7632 mode+Composer2.PIP

            MHal_FRC_HFR_Set_ComposerSelection(pInstance, E_FRC_COMPOSER_AFTER_MEMC);
            if(enWindow == MAIN_WINDOW)
            {
                MHal_FRC_HFR_Set_Hsv_SrcSelection(pInstance, E_FRC_COMPOSER_AFTER_MEMC);
                 //Composer1.m7632 mode
                stMainOnlyBypass.eType = E_FRC_COMPOSER_BEFORE_MEMC;
                stMainOnlyBypass.bMainOnlyBypass = TRUE;
                MHal_FRC_HFR_Composer_Set_MainOnlyBypass(pInstance, stMainOnlyBypass);

                stMainSubSwap.eType = E_FRC_COMPOSER_BEFORE_MEMC;
                stMainSubSwap.bMainSubSwap = FALSE;
                MHal_FRC_HFR_Composer_Set_MainSubSwap(pInstance, stMainSubSwap);

                stPathEnable.eType = E_FRC_COMPOSER_BEFORE_MEMC;
                stPathEnable.bEnable = TRUE;
                MHal_FRC_HFR_Composer_Enable_Main(pInstance, stPathEnable);
            }
            //Composer2.PIP
            stMainOnlyBypass.eType = E_FRC_COMPOSER_AFTER_MEMC;
            stMainOnlyBypass.bMainOnlyBypass = FALSE;
            MHal_FRC_HFR_Composer_Set_MainOnlyBypass(pInstance, stMainOnlyBypass);

            stMainSubSwap.eType = E_FRC_COMPOSER_AFTER_MEMC;
            stMainSubSwap.bMainSubSwap = FALSE;
            MHal_FRC_HFR_Composer_Set_MainSubSwap(pInstance, stMainSubSwap);

            stPathEnable.eType = E_FRC_COMPOSER_AFTER_MEMC;
            stPathEnable.bEnable = TRUE;
            MHal_FRC_HFR_Composer_Enable_Sub(pInstance, stPathEnable);
        }
    }
    else
    {
        UNUSED(pInstance);
        UNUSED(enWindow);
    }
    return;
}

MS_BOOL _MDrv_XC_SetCFDDispPath(void* pInstance, SCALER_WIN eWindow, EN_KDRV_XC_CFD_DISP_PATH enCFDDispPath)
{
    MS_BOOL bRet = FALSE;
    ST_KDRV_XC_CFD_DISP_PATH stXCCFDSetDispPath;

    //update CFD disp path
    memset(&stXCCFDSetDispPath,0,sizeof(ST_KDRV_XC_CFD_DISP_PATH));
    stXCCFDSetDispPath.u32Version = ST_KDRV_XC_CFD_DISP_PATH_VERSION;
    stXCCFDSetDispPath.u16Length = sizeof(ST_KDRV_XC_CFD_DISP_PATH);
    stXCCFDSetDispPath.u8Win = (MS_U8)eWindow;
    stXCCFDSetDispPath.enDispPath = enCFDDispPath;

    XC_CFD_CONTROL_INFO* pstCfdCtrlInfo = NULL;
    pstCfdCtrlInfo = malloc(sizeof(XC_CFD_CONTROL_INFO));

    if(pstCfdCtrlInfo == NULL)
    {
        printf("_MDrv_XC_SetCFDDispPath, pstCfdCtrlInfo == NULL\n");
        return bRet;
    }
    memset(pstCfdCtrlInfo, 0, sizeof(XC_CFD_CONTROL_INFO));
    pstCfdCtrlInfo->enCtrlType = E_XC_CFD_CTRL_SET_DISP_PATH;
    pstCfdCtrlInfo->pParam = &stXCCFDSetDispPath;
    pstCfdCtrlInfo->u32ParamLen = sizeof(ST_KDRV_XC_CFD_DISP_PATH);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    MApi_XC_HDR_Control(E_XC_HDR_CTRL_CFD_CONTROL, pstCfdCtrlInfo);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    free(pstCfdCtrlInfo);
    pstCfdCtrlInfo = NULL;
    bRet =TRUE;
    return bRet;
}

//-------------------------------------------------------------------------------------------------
/// Set the specific window
/// @param  pstXC_SetWin_Info      \b IN: the information of the window setting
/// @param  u32InitDataLen         \b IN: the length of the pstXC_SetWin_Info
/// @param  eWindow                \b IN: which window we are going to set
/// @return @ref MS_BOOL
//-------------------------------------------------------------------------------------------------
static MS_BOOL _MApi_XC_SetWindow(void *pInstance, XC_SETWIN_INFO *pstXC_SetWin_Info, MS_U32 u32InitDataLen, MS_BOOL bDualWindow, SCALER_WIN eWindow)
{
#ifdef CONFIG_UTOPIA_PROC_DBG_SUPPORT
    u32SetWindowTime = MsOS_GetSystemTime();
#endif
#if (XC_SUPPORT_4K2K_I_MODE)
    if(IS_INPUT_MM_4K2K_I_MODE(pstXC_SetWin_Info->stCapWin.width,pstXC_SetWin_Info->stCapWin.height,pstXC_SetWin_Info->enInputSourceType,pstXC_SetWin_Info->bInterlace) == TRUE)
    {
        pstXC_SetWin_Info->stCapWin.height -= 4;
    }
#endif
#if VERIFY_MVIDEO_FPGA
    MS_BOOL binterlace =FALSE;
#endif

//    MS_BOOL bEnableHighSpeedFclk = FALSE;
    MS_U32 u32DisPOddCheck = 0x00;
    MS_BOOL bRet = TRUE;
    MS_U8 u8OPTEEVersion = 0;
    XC_VBOX_INFO *pstXCVBox = NULL;

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

#ifdef CONFIG_UTOPIA_PROC_DBG_SUPPORT
    gSrcInfo[MAIN_WINDOW].Status2.u32SetWindowTime = 0;
#endif

    if(g_stXCQosCallBackInfo.pfnFunInputSourceInfo != NULL && pstXC_SetWin_Info != NULL)
    {
        pXCResourcePrivate->stXCQosInputSourceInfo.u32Version = ST_XC_QOS_INPUTSOURCE_INFO_VERSION;
        pXCResourcePrivate->stXCQosInputSourceInfo.u32Length = sizeof(ST_XC_QOS_INPUTSOURCE_INFO);

        pXCResourcePrivate->stXCQosInputSourceInfo.enXCQosEvntStatus = E_XC_QOS_EVENT_SETWINDOW_INFO;
        pXCResourcePrivate->stXCQosInputSourceInfo.enWin = eWindow;
        pXCResourcePrivate->stXCQosInputSourceInfo.enInputSourceType = pstXC_SetWin_Info->enInputSourceType;

        memcpy(&pXCResourcePrivate->stXCQosInputSourceInfo.stXC_SetWin_Info,pstXC_SetWin_Info,sizeof(XC_SETWIN_INFO));
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        g_stXCQosCallBackInfo.pfnFunInputSourceInfo(&pXCResourcePrivate->stXCQosInputSourceInfo);
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    }

    //reset sub HFR flag
    if(MHal_XC_Is_Support_HFR_Path() && (eWindow == SUB_WINDOW))
    {
        gSrcInfo[SUB_WINDOW].Status2.bEnableHFRPath = TRUE;
    }

    //global change clock api
    if(MDrv_SC_CheckMuteStatusByRegister(pInstance, eWindow))
    {
        //Enable HFR path clock
        if(MHal_XC_Is_Enable_HFR_Path(pInstance, eWindow))
        {
            MDrv_XC_ChangeClock(pInstance, E_XC_CHANGE_CLOCK_TYPE_HFR_PATH, TRUE);
        }
    }
    if(eWindow == MAIN_WINDOW)
    {
        //CFD disp path & fe_hfr_top
        if(MHal_XC_Is_Enable_HFR_Path(pInstance, eWindow))
        {
            MHal_XC_FE_HFR_TOP_Control(pInstance, EN_FE_HFR_NORMAL_MODE, MHal_XC_IsHDMI420(pInstance, gSrcInfo[eWindow].enInputSourceType));
            _MDrv_XC_SetCFDDispPath(pInstance, eWindow, E_KDRV_XC_CFD_DISP_PATH_HFR_MAIN);
#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
            MHal_FRC_TGEN_Enable_Lock_Source(pInstance, ENABLE, E_FRC_TGEN_LOCK_SOURCE_MODE_FROM_IP, FALSE);
#endif
        }
        else
        {
            //HDMI 2.1 and timing < 4k2k 120Hz need to run main path
            if(IsSrcTypeHDMI(gSrcInfo[eWindow].enInputSourceType) && (gSrcInfo[MAIN_WINDOW].Status2.bHDMI8pMode == TRUE))
            {
                MHal_XC_FE_HFR_TOP_Control(pInstance, EN_FE_HFR_BACK_SC_MODE, MHal_XC_IsHDMI420(pInstance, gSrcInfo[eWindow].enInputSourceType));
            }
            //un-support HFR or run main path case
            _MDrv_XC_SetCFDDispPath(pInstance, eWindow, E_KDRV_XC_CFD_DISP_PATH_XC0_MAIN);

        }
    }
    else if(eWindow == SUB_WINDOW)
    {
        if (MHal_XC_Is_Enable_HFR_Path(pInstance, eWindow))
        {
            _MDrv_XC_SetCFDDispPath(pInstance, eWindow, E_KDRV_XC_CFD_DISP_PATH_HFR_MAIN);
            MHal_XC_SetHfrHdrWindow(pInstance, eWindow, pstXC_SetWin_Info);
        }
    }
    else
    {
        printf("Not support eWindow=%u",eWindow);
    }
    _XC_ENTRY(pInstance);
    _MDrv_XC_SetHFRComposer(pInstance, eWindow);
    _XC_RETURN(pInstance);
    if (MHal_XC_Is_Support_DIP_Sub_Path())
    {

        if(eWindow == SUB_WINDOW)
        {
            if (IsSrcTypeVga(gSrcInfo[eWindow].enInputSourceType)   ||
                IsSrcTypeYPbPr(gSrcInfo[eWindow].enInputSourceType) ||
                IsSrcTypeScart(gSrcInfo[eWindow].enInputSourceType) )
            {
                if(E_XC_3D_INPUT_MODE_NONE == MDrv_XC_Get_3D_Input_Mode(pInstance, eWindow))// don't set adc mode when set sub window at 3d
                {
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"ADC Set Mode \n");
                    PQ_ADC_SAMPLING_INFO stPQADCSamplingInfo;
                    memset(&stPQADCSamplingInfo,0,sizeof(PQ_ADC_SAMPLING_INFO));
                    if ( IsSrcTypeVga(gSrcInfo[eWindow].enInputSourceType) || IsSrcTypeYPbPr(gSrcInfo[eWindow].enInputSourceType) )
                    {
                        if(gSrcInfo[eWindow].bHDuplicate)
                        {
                            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                            MDrv_XC_ADC_GetPQADCSamplingInfo(pInstance, gSrcInfo[eWindow].enInputSourceType, &gSrcInfo[eWindow], &stPQADCSamplingInfo, eWindow);
                            _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
                        }
                    }

                    if(!MDrv_XC_Is_UpdateDsCmd(pInstance,eWindow))
                    {
                        MDrv_XC_ADC_SetMode(pInstance, gSrcInfo[eWindow].enInputSourceType, &gSrcInfo[eWindow], &stPQADCSamplingInfo, eWindow);
                    }
                }
            }
            else
            {
                if (!g_bSWDSGenScenario)
                {
                    MDrv_XC_ADC_SCART_RGB_setMode(pInstance,FALSE);
                }
            }
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            _MDrv_XC_Set_PQ_SourceData(pInstance, eWindow, gSrcInfo[eWindow].enInputSourceType, &gSrcInfo[eWindow]);
            if (!g_bSWDSGenScenario)
            {
                msAPI_Scaler_SetMode(pInstance, gSrcInfo[eWindow].enInputSourceType, &gSrcInfo[eWindow], eWindow);
            }
            return TRUE;
        }
    }

#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
    MS_BOOL bChangeDisplaySize = FALSE;
    MS_U8 u8LPLL_Type =(MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type;
#endif
    gSrcInfo[eWindow].enInputSourceType = pstXC_SetWin_Info->enInputSourceType;

#if (XC_SUPPORT_FASTSWITCH_BY_IP1GP==TRUE)
    MHal_XC_SetOfflineSource(pInstance, eWindow, gSrcInfo[eWindow].enInputSourceType);

    if(IsSrcTypeHDMI(gSrcInfo[eWindow].enInputSourceType))
    {
        MS_BOOL bHDMI420 = FALSE;
        MS_U8 u8Index = 0;
        E_MUX_INPUTPORT enPorts[3] = {INPUT_PORT_NONE_PORT, INPUT_PORT_NONE_PORT, INPUT_PORT_NONE_PORT};
        MS_U8 u8EnablePort = 0;

        bHDMI420 = MHal_XC_IsHDMI420(pInstance, gSrcInfo[eWindow].enInputSourceType);

        MDrv_XC_Mux_GetPort(pInstance, gSrcInfo[eWindow].enInputSourceType, enPorts, &u8EnablePort);
        for(u8Index = 0; u8Index < u8EnablePort; u8Index++)
        {
            MHal_XC_SetFSSSource(pInstance, enPorts[u8Index], bHDMI420);
        }

        MHal_XC_SetFSSHDMI420Enable(pInstance, gSrcInfo[eWindow].enInputSourceType, bHDMI420);
    }
    else
    {
        MHal_XC_SetFSSHDMI420Enable(pInstance, gSrcInfo[eWindow].enInputSourceType, DISABLE);
    }

#endif
    //HDMI 2.1 8p mode width
    if(IsSrcTypeHDMI(gSrcInfo[eWindow].enInputSourceType) && (gSrcInfo[MAIN_WINDOW].Status2.bHDMI8pMode == TRUE))
    {
        pXCResourcePrivate->stdrvXC_MVideo_Context.u16HDMI8pWidth = pstXC_SetWin_Info->stCapWin.width / 8;
    }

#ifdef UFO_XC_SUPPORT_DUAL_MIU
    MS_BOOL bTempEnableDualMIU = FALSE;
    //display window w < 256 pixels,no need enable Dual MIU,so disable dual MIU
    if(gSrcInfo[eWindow].Status2.stMiuDualModCfg.bEnableMiuDualMod && (pstXC_SetWin_Info->stDispWin.width < OFFSET_PIXEL_ALIGNMENT*4))
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Disable MiuDualMod when Disp width is < OFFSET_PIXEL_ALIGNMENT*4 \n");
        bTempEnableDualMIU= gSrcInfo[eWindow].Status2.stMiuDualModCfg.bEnableMiuDualMod;
        gSrcInfo[eWindow].Status2.stMiuDualModCfg.bEnableMiuDualMod = FALSE;
    }
#endif

    // Need clear dual mode enable
#ifdef UFO_XC_SUPPORT_DUAL_MIU
    //MDrv_XC_EnableMiuDualMode(pInstance, FALSE, 2, 2, eWindow);
#endif
#if 0 //jsut for test for maserati dual miu.
#ifndef DONT_USE_CMA
#ifdef UFO_XC_SUPPORT_DUAL_MIU
        if (eWindow == MAIN_WINDOW)
        {
            if (IsSrcTypeStorage(gSrcInfo[eWindow].enInputSourceType)) //&& ((pstXC_SetWin_Info->stCapWin.width > 1920) && (pstXC_SetWin_Info->stCapWin.height > 1080)))
            {
                MDrv_XC_EnableMiuDualMode(pInstance, TRUE, 2, 2, MAIN_WINDOW);
            }
            else
            {
                MDrv_XC_EnableMiuDualMode(pInstance, FALSE, 2, 2, eWindow);
            }
        }
        else
        {
            MDrv_XC_EnableMiuDualMode(pInstance, FALSE, 2, 2, eWindow);
        }
#endif
#endif
#endif

#ifdef STELLAR
    PNL_ApiExtStatus sTCONPnlExtStatus;
    if(stTCONPanelConfig.bUsingTCON)
    {
        sTCONPnlExtStatus.u16ApiStatusEX_Length = sizeof(PNL_ApiExtStatus);
        MApi_PNL_GetStatusEx(&sTCONPnlExtStatus);
    }
#endif

    MDrv_SC_SetPQHSDFlag(pInstance, FALSE); //Reset variable

#ifdef UFO_XC_SCAN_TYPE
    MHal_XC_SetForcepConfig(pInstance, pXCResourcePrivate->stdrvXC_MVideo.g_ScanType);
#endif

#ifdef K3_U2
    // Prepare seamless zapping, reset commands in next DS index
    if((Hal_SC_GetSeamlessZappingStatus(pInstance, eWindow)) && (Hal_SC_get_freezeimg(pInstance, eWindow)))
    {
        Hal_SC_PrepareSeamlessZapping(pInstance, eWindow);
    }
#endif

#if (XC_SUPPORT_IOMMU == TRUE)
    if((pXCResourcePrivate->stdrvXC_MVideo_Context.stMMAfeature.u32Support == MSOS_MMA_SUPPORT)
        && (pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_HDMI_DMA] == TRUE))
    {
        if(IsSrcTypeHDMI(gSrcInfo[eWindow].enInputSourceType))
        {
            _MDrv_XC_HDMI_DMA_GetMemoryFromMMA(pInstance,eWindow);
        }
    }
#endif

#ifndef DONT_USE_CMA
#if (XC_SUPPORT_CMA == TRUE)
#if (XC_SUPPORT_HDR_DMA_CMA == TRUE)
    if(IsSrcTypeHDMI(gSrcInfo[eWindow].enInputSourceType))
    {
        _MDrv_XC_HDMI_DMA_GetMemoryFromCMA(pInstance,eWindow);
    }
#endif
#endif
#endif

#ifdef UFO_XC_HDMI_4K2K_DMA
#if (XC_SUPPORT_IOMMU == TRUE)
    if((pXCResourcePrivate->stdrvXC_MVideo_Context.stMMAfeature.u32Support == MSOS_MMA_NOT_SUPPORT)
        || (pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_HDMI_DMA] == FALSE))
#endif
    {
        if(IsSrcTypeHDMI(gSrcInfo[eWindow].enInputSourceType) && (pstXC_SetWin_Info->stCapWin.width >= 3840) && (pstXC_SetWin_Info->stCapWin.height >= 2160))
        {
            MHal_XC_HDR_Control(pInstance, E_XC_HDR_CTRL_INIT_DMA_HDMI, (void *)(&pXCResourcePrivate->stdrvXC_Display._stDMAInitInfo_Hdmi));
        }
        else
        {
            MHal_XC_HDR_Control(pInstance, E_XC_HDR_CTRL_INIT_DMA, (void *)(&pXCResourcePrivate->stdrvXC_Display._stDMAInitInfo));
        }
    }
#endif
#ifdef VDEC_4K2K_PATCH_MODIFY_INFO
    if(IsSrcTypeStorage(gSrcInfo[eWindow].enInputSourceType)||IsSrcTypeDTV(gSrcInfo[eWindow].enInputSourceType))
    {
        if( pstXC_SetWin_Info->stCapWin.width>=2048 )
        {
            pstXC_SetWin_Info->stCapWin.width=pstXC_SetWin_Info->stCapWin.width /2;
            pstXC_SetWin_Info->stCapWin.x=pstXC_SetWin_Info->stCapWin.x /2;
        }

        if( pstXC_SetWin_Info->stCropWin.width>=2048 )
        {
            pstXC_SetWin_Info->stCropWin.width=pstXC_SetWin_Info->stCropWin.width /2;
            pstXC_SetWin_Info->stCropWin.x=pstXC_SetWin_Info->stCropWin.x /2;
        }
    }
#endif
    if(IsSrcTypeHDMI(gSrcInfo[eWindow].enInputSourceType)||IsSrcTypeDVI(gSrcInfo[eWindow].enInputSourceType))
    {
        //AverageMode??YHDMI2.0 case HDMI will do width/2
        E_MUX_INPUTPORT enInputPortType = MDrv_XC_Mux_GetHDMIPort(pInstance, gSrcInfo[eWindow].enInputSourceType);
        if(Hal_HDMI_GetAverageModeFlag(enInputPortType))
        {
            pstXC_SetWin_Info->stCapWin.width=pstXC_SetWin_Info->stCapWin.width /2;
            pstXC_SetWin_Info->stCropWin.width=pstXC_SetWin_Info->stCropWin.width /2;
            pstXC_SetWin_Info->stCropWin.x=pstXC_SetWin_Info->stCropWin.x /2;
        }
    }

    // we've to support interface changed without re-compile user application
    // please specify new added interface default value here
    //_u16XCDbgSwitch_InternalUseOnly = (XC_DBGLEVEL_SETWINDOW|XC_DGBLEVEL_CROPCALC|XC_DBGLEVEL_SETTIMING);
    if(u32InitDataLen != sizeof(XC_SETWIN_INFO))
    {
        // customer application different version with our driver
        // start from customized pre H/V scaling
        gSrcInfo[eWindow].Status2.bPreHCusScaling = FALSE;
        gSrcInfo[eWindow].Status2.bPreVCusScaling = FALSE;
        gSrcInfo[eWindow].Status2.u16PreHCusScalingSrc = 0;
        gSrcInfo[eWindow].Status2.u16PreHCusScalingDst = 0;
        gSrcInfo[eWindow].Status2.u16PreVCusScalingSrc = 0;
        gSrcInfo[eWindow].Status2.u16PreVCusScalingDst = 0;
    }
    else
    {
        gSrcInfo[eWindow].Status2.bPreHCusScaling = pstXC_SetWin_Info->bPreHCusScaling;
        gSrcInfo[eWindow].Status2.u16PreHCusScalingSrc = pstXC_SetWin_Info->u16PreHCusScalingSrc;
        gSrcInfo[eWindow].Status2.u16PreHCusScalingDst = pstXC_SetWin_Info->u16PreHCusScalingDst;

        if(gSrcInfo[eWindow].Status2.u16PreHCusScalingDst > gSrcInfo[eWindow].Status2.u16PreHCusScalingSrc)
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"H can't do pre scaling up, change to auto prescaling\n");
            gSrcInfo[eWindow].Status2.bPreHCusScaling = FALSE;
            gSrcInfo[eWindow].Status2.u16PreHCusScalingSrc = 0;
            gSrcInfo[eWindow].Status2.u16PreHCusScalingDst = 0;
        }

        if (gSrcInfo[eWindow].Status2.bPreHCusScaling)
        {
            if (gSrcInfo[eWindow].Status2.u16PreHCusScalingSrc == 0 || gSrcInfo[eWindow].Status2.u16PreHCusScalingDst == 0)
            {
                gSrcInfo[eWindow].Status2.bPreHCusScaling = FALSE;
                gSrcInfo[eWindow].Status2.u16PreHCusScalingSrc = 0;
                gSrcInfo[eWindow].Status2.u16PreHCusScalingDst = 0;
                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"PreHCus Src or Dst can not be 0 \n");
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                return FALSE;
            }
        }
        else
        {
            gSrcInfo[eWindow].Status2.u16PreHCusScalingSrc = 0;
            gSrcInfo[eWindow].Status2.u16PreHCusScalingDst = 0;
        }

        gSrcInfo[eWindow].Status2.bPreVCusScaling = pstXC_SetWin_Info->bPreVCusScaling;
        gSrcInfo[eWindow].Status2.u16PreVCusScalingSrc = pstXC_SetWin_Info->u16PreVCusScalingSrc;
        gSrcInfo[eWindow].Status2.u16PreVCusScalingDst = pstXC_SetWin_Info->u16PreVCusScalingDst;

        if(gSrcInfo[eWindow].Status2.u16PreVCusScalingDst > gSrcInfo[eWindow].Status2.u16PreVCusScalingSrc)
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"V can't do pre scaling up, change to auto prescaling\n");
            gSrcInfo[eWindow].Status2.bPreVCusScaling = FALSE;
            gSrcInfo[eWindow].Status2.u16PreVCusScalingSrc = 0;
            gSrcInfo[eWindow].Status2.u16PreVCusScalingDst = 0;
        }

        if (gSrcInfo[eWindow].Status2.bPreVCusScaling)
        {
            if (gSrcInfo[eWindow].Status2.u16PreVCusScalingSrc == 0 || gSrcInfo[eWindow].Status2.u16PreVCusScalingDst == 0  )
            {
                gSrcInfo[eWindow].Status2.bPreVCusScaling = FALSE;
                gSrcInfo[eWindow].Status2.u16PreVCusScalingSrc = 0;
                gSrcInfo[eWindow].Status2.u16PreVCusScalingDst = 0;
                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"PreVCus Src or Dst can not be 0 \n");
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                return FALSE;
            }
        }
        else
        {
            gSrcInfo[eWindow].Status2.u16PreVCusScalingSrc = 0;
            gSrcInfo[eWindow].Status2.u16PreVCusScalingDst = 0;
        }
    }

    // Capture width & height can not be 0 !!
    if (pstXC_SetWin_Info->stCapWin.width == 0 || pstXC_SetWin_Info->stCapWin.height == 0 ||
        pstXC_SetWin_Info->stCropWin.width == 0 || pstXC_SetWin_Info->stCropWin.height == 0 ||
        pstXC_SetWin_Info->stDispWin.width == 0 || pstXC_SetWin_Info->stDispWin.height == 0 )
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Cap/Crop/Disp width or height can not be 0 \n");
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        return FALSE;
    }

    _MDrv_XC_CheckCaptureWin(pInstance, pstXC_SetWin_Info, eWindow);
    _MDrv_XC_CheckCropWin(pInstance, pstXC_SetWin_Info, eWindow);
    _MDrv_XC_CheckDisplayWin(pInstance, pstXC_SetWin_Info, eWindow);

    #if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
    if (IsVBY1_16LANE(u8LPLL_Type))
    {
        // For 120Hz BW request, need to send video size to FRC,
        // FRC would reduce BW by different case
#if (defined (ANDROID))
#else
        Hal_XC_FRC_R2_Set_InputFrameSize(pInstance, pstXC_SetWin_Info->stCropWin.width, pstXC_SetWin_Info->stCropWin.height);
#endif
    }
    #endif

    // copy user data to internal data
    // here is a patch first, will remove after seperate Internal XC status with set window information
    //u32CopyLen = sizeof(XC_SETWIN_INFO) - (sizeof(MS_BOOL)*2) - (sizeof(MS_U16)*4);
    //memcpy(&gSrcInfo[eWindow], pstXC_SetWin_Info, u32CopyLen);
    //do not use memcopy(), strongly depend on structure definition
    _Mdrv_XC_CopySetWinInfo(pInstance, pstXC_SetWin_Info, eWindow);

#if ( HW_SUPPORT_3D_CB_AND_PA  == TRUE)
    bRet = MDrv_SC_3D_FormatValidation(pInstance, &gSrcInfo[eWindow], eWindow);
#endif

#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
        if (((pXCResourcePrivate->stdrvXC_3D._eInput3DMode[eWindow] != E_XC_3D_INPUT_MODE_NONE)
            && (pXCResourcePrivate->stdrvXC_3D._eOutput3DMode != E_XC_3D_OUTPUT_MODE_NONE))
            || (pXCResourcePrivate->stdrvXC_MVideo.gu16HPixelShiftRange==0 && pXCResourcePrivate->stdrvXC_MVideo.gu16VPixelShiftRange==0))
        {
            MHal_XC_PixelShiftStatusChange(pInstance, EN_XC_PIXEL_SHIFT_DISABLE);
        }
#endif

    //for 3D handle invalid display window
    if(E_XC_3D_OUTPUT_CHECKBOARD_HW == MDrv_XC_Get_3D_Output_Mode(pInstance))
    {
        gSrcInfo[eWindow].stDispWin.y &= ~0x1;
        gSrcInfo[eWindow].stDispWin.x &= ~0x1;
        gSrcInfo[eWindow].stDispWin.height &= ~0x1;
        gSrcInfo[eWindow].stDispWin.width &= ~0x1;
    }
    else if((E_XC_3D_OUTPUT_LINE_ALTERNATIVE == MDrv_XC_Get_3D_Output_Mode(pInstance))
            || (E_XC_3D_OUTPUT_LINE_ALTERNATIVE_HW == MDrv_XC_Get_3D_Output_Mode(pInstance))
            || ((E_XC_3D_OUTPUT_TOP_BOTTOM == MDrv_XC_Get_3D_Output_Mode(pInstance)) && MDrv_SC_3D_Is_LR_Sbs2Line(pInstance)))
    {
        gSrcInfo[eWindow].stDispWin.y &= ~0x1;
        gSrcInfo[eWindow].stDispWin.height &= ~0x1;
    }
    else if(E_XC_3D_OUTPUT_PIXEL_ALTERNATIVE_HW == MDrv_XC_Get_3D_Output_Mode(pInstance))
    {
        gSrcInfo[eWindow].stDispWin.x &= ~0x1;
        gSrcInfo[eWindow].stDispWin.width &= ~0x1;
    }
    else if((E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF == MDrv_XC_Get_3D_Output_Mode(pInstance))
            || (E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF_HW == MDrv_XC_Get_3D_Output_Mode(pInstance)))
    {
        gSrcInfo[eWindow].stDispWin.width &= ~0x1;
    }
    else if((E_XC_3D_OUTPUT_TOP_BOTTOM == MDrv_XC_Get_3D_Output_Mode(pInstance))
            || (E_XC_3D_OUTPUT_TOP_BOTTOM_HW == MDrv_XC_Get_3D_Output_Mode(pInstance)))
    {
        gSrcInfo[eWindow].stDispWin.height &= ~0x1;
    }

    // assert check
    MS_ASSERT((gSrcInfo[eWindow].bHCusScaling==TRUE)?((gSrcInfo[eWindow].u16HCusScalingSrc!=0)&&(gSrcInfo[eWindow].u16HCusScalingDst!=0)):1);
    MS_ASSERT((gSrcInfo[eWindow].bVCusScaling==TRUE)?((gSrcInfo[eWindow].u16VCusScalingSrc!=0)&&(gSrcInfo[eWindow].u16VCusScalingDst!=0)):1);
    MS_ASSERT((gSrcInfo[eWindow].Status2.bPreHCusScaling==TRUE)?((gSrcInfo[eWindow].Status2.u16PreHCusScalingSrc!=0)&&(gSrcInfo[eWindow].Status2.u16PreHCusScalingDst!=0)):1);
    MS_ASSERT((gSrcInfo[eWindow].Status2.bPreVCusScaling==TRUE)?((gSrcInfo[eWindow].Status2.u16PreVCusScalingSrc!=0)&&(gSrcInfo[eWindow].Status2.u16PreVCusScalingDst!=0)):1);

    // The total pixel conut of display window can not be odd.
    u32DisPOddCheck = gSrcInfo[eWindow].stDispWin.width * gSrcInfo[eWindow].stDispWin.height;
    // if u32DisPOddCheck is odd, that means both of disp.width and disp.height is odd.
    // we adjust width to even.
    if (u32DisPOddCheck & 0x01)
    {
        if (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width > gSrcInfo[eWindow].stDispWin.width )
        {
            gSrcInfo[eWindow].stDispWin.width = ( gSrcInfo[eWindow].stDispWin.width + 1 ) & ~0x1;
        }
    }

    if (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE_4K_HALFK_240HZ)
    {
        gSrcInfo[eWindow].stDispWin.height = gSrcInfo[eWindow].stDispWin.height/4;
    }
    else if(MDrv_XC_Is2K2KToFrc(pInstance))
    {
        gSrcInfo[eWindow].stDispWin.x = gSrcInfo[eWindow].stDispWin.x/2;
        gSrcInfo[eWindow].stDispWin.width = gSrcInfo[eWindow].stDispWin.width/2;
    }

    #if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
    if (eWindow == MAIN_WINDOW)
    {
        ST_XC_FRCFSCCTRLPOLICY stXCFrcFscCtrlPolicy;
        memset(&stXCFrcFscCtrlPolicy, 0, sizeof(ST_XC_FRCFSCCTRLPOLICY));
        stXCFrcFscCtrlPolicy.u32Version = XC_FRCFSCCTRLPOLICY_VERSION;
        stXCFrcFscCtrlPolicy.u32Length = sizeof(ST_XC_FRCFSCCTRLPOLICY);
        stXCFrcFscCtrlPolicy.enWindow = eWindow;
        stXCFrcFscCtrlPolicy.enInputSourceType = gSrcInfo[MAIN_WINDOW].enInputSourceType;
        stXCFrcFscCtrlPolicy.u16InputHSize = gSrcInfo[MAIN_WINDOW].stCapWin.width;
        stXCFrcFscCtrlPolicy.u16InputVSize = gSrcInfo[MAIN_WINDOW].stCapWin.height;
        stXCFrcFscCtrlPolicy.u16InputVFreq = gSrcInfo[MAIN_WINDOW].u16InputVFreq;
        E_APIXC_ReturnValue enRet = MDrv_XC_GetFrcFscCtrlPolicy(pInstance, &stXCFrcFscCtrlPolicy);
        if(enRet == E_APIXC_RET_OK)
        {
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFRCEnabled = stXCFrcFscCtrlPolicy.bFRCEnabled;
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFSCEnabled = stXCFrcFscCtrlPolicy.bFSCEnabled;

            if (pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFSCEnabled)
            {
                if (MHal_FRC_IsFHDToFSC(pInstance, eWindow))
                {
                    // FHD to FSC case, need to make post-scaling to displayH/2 + displayV/2 first
                    gSrcInfo[eWindow].stDispWin.x = gSrcInfo[eWindow].stDispWin.x/2;
                    gSrcInfo[eWindow].stDispWin.width = gSrcInfo[eWindow].stDispWin.width/2;
                    gSrcInfo[eWindow].stDispWin.y = gSrcInfo[eWindow].stDispWin.y/2;
                    gSrcInfo[eWindow].stDispWin.height = gSrcInfo[eWindow].stDispWin.height/2;
                    bChangeDisplaySize = TRUE;

                    // for custom scaling case
                    if(gSrcInfo[eWindow].bHCusScaling)
                    {
                        gSrcInfo[eWindow].u16HCusScalingDst = gSrcInfo[eWindow].u16HCusScalingDst/2;
                    }
                    if(gSrcInfo[eWindow].bVCusScaling)
                    {
                        gSrcInfo[eWindow].u16VCusScalingDst = gSrcInfo[eWindow].u16VCusScalingDst/2;
                    }
                }
            }
        }
    }
    #endif
 #ifdef MSOS_TYPE_LINUX_KERNEL
    MS_PQ_CAP_INFO stPQCaps;
    memset(&stPQCaps,0,sizeof(MS_PQ_CAP_INFO));
    if(MDrv_PQ_GetGameMode_Status(PQ_MAIN_WINDOW) == TRUE)
    {
        if(IsSrcTypeHDMI(gSrcInfo[eWindow].enInputSourceType)||IsSrcTypeStorage(gSrcInfo[eWindow].enInputSourceType))
        {
            pXCResourcePrivate->stdrvXC_Display._bGameModeStatus = TRUE;
        }
 #if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        if(MDrv_PQ_GetCaps(&stPQCaps))
        {
            if((stPQCaps.u8GameModeVer == 1)&&(IsSrcTypeHDMI(gSrcInfo[eWindow].enInputSourceType))) // m7632 dtv/mm  not disable frc
            {
                pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFRCEnabled = FALSE;
            }
        }
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
#endif
    }
    else
    {
        pXCResourcePrivate->stdrvXC_Display._bGameModeStatus = FALSE;
    }
#endif

    // if V mirror, need to re-cal display window but Ninelattice!!
    if(IsVMirrorMode(eWindow)&&(!gSrcInfo[eWindow].bDisplayNineLattice))
    {
        MS_U32 u32TempHeight = gSrcInfo[eWindow].stDispWin.height;

        if(((MDrv_XC_Get_3D_Output_Mode(pInstance) == E_XC_3D_OUTPUT_TOP_BOTTOM)
                    || (MDrv_XC_Get_3D_Output_Mode(pInstance) == E_XC_3D_OUTPUT_TOP_BOTTOM_HW))
                && ((MDrv_XC_Get_3D_HW_Version() > 1)
                    || ((MDrv_XC_Get_3D_HW_Version() == 1) && (!MDrv_SC_3D_Is_LR_Sbs2Line(pInstance)))))
        {
            gSrcInfo[eWindow].stDispWin.y = gSrcInfo[eWindow].stDispWin.y * 2;
        }

        //for ver3d =1, display window will be half when pass into driver,so here need double it
        if(((MDrv_XC_Get_3D_Output_Mode(pInstance)== E_XC_3D_OUTPUT_TOP_BOTTOM)
                || (MDrv_XC_Get_3D_Output_Mode(pInstance)== E_XC_3D_OUTPUT_TOP_BOTTOM_HW))
            && (MDrv_XC_Get_3D_HW_Version() == 1))
        {
            u32TempHeight *= 2;
        }

#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
        if (pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFSCEnabled && bChangeDisplaySize)
        {
            if( pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height/2 > ( gSrcInfo[eWindow].stDispWin.y + u32TempHeight ) )
            {
                gSrcInfo[eWindow].stDispWin.y =
                    pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height/2 - ( gSrcInfo[eWindow].stDispWin.y + u32TempHeight );
            }
            else
            {
                //pixshift case will do overscan,changed display.y&Height,y + Height >stPanelInfo.u16Height,so need not error handling
                if( !((pXCResourcePrivate->stdrvXC_MVideo.gePixelShiftFeature & PIXEL_SHIFT_DO_OVERSCAN )== PIXEL_SHIFT_DO_OVERSCAN))
                {
                    gSrcInfo[eWindow].stDispWin.y = 0;
                }
            }
        }
        else
#endif
        if( pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height > ( gSrcInfo[eWindow].stDispWin.y + u32TempHeight ) )
        {
            gSrcInfo[eWindow].stDispWin.y =
                pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height - ( gSrcInfo[eWindow].stDispWin.y + u32TempHeight );
        }
        else
        {
            //pixshift case will do overscan,changed display.y&Height,y + Height >stPanelInfo.u16Height,so need not error handling
            if( !((pXCResourcePrivate->stdrvXC_MVideo.gePixelShiftFeature & PIXEL_SHIFT_DO_OVERSCAN )== PIXEL_SHIFT_DO_OVERSCAN))
            {
                gSrcInfo[eWindow].stDispWin.y = 0;
            }
        }

        if(((MDrv_XC_Get_3D_Output_Mode(pInstance) == E_XC_3D_OUTPUT_TOP_BOTTOM)
                    || (MDrv_XC_Get_3D_Output_Mode(pInstance) == E_XC_3D_OUTPUT_TOP_BOTTOM_HW))
                && ((MDrv_XC_Get_3D_HW_Version() > 1)
                    || ((MDrv_XC_Get_3D_HW_Version() == 1) && (!MDrv_SC_3D_Is_LR_Sbs2Line(pInstance)))))
        {
            gSrcInfo[eWindow].stDispWin.y /= 2;

            //adjust main/sub display window position
            //for ver>=2, this logic is at MDrv_XC_3D_LoadReg
            if(MDrv_XC_Get_3D_HW_Version() == 1)
            {
                if(eWindow == MAIN_WINDOW)
                {
                    gSrcInfo[eWindow].stDispWin.y += pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height / 2;
                }
                else
                {
                    if(gSrcInfo[eWindow].stDispWin.y >= pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height / 2)
                    {
                        gSrcInfo[eWindow].stDispWin.y -= pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height / 2;
                    }
                }
            }
        }
    }
    // if H mirror, need to re-cal display window but Ninelattice!!
    if(IsHMirrorMode(eWindow)&&(!gSrcInfo[eWindow].bDisplayNineLattice))
    {
        MS_U32 u32TempWidth = gSrcInfo[eWindow].stDispWin.width;

        if(((MDrv_XC_Get_3D_Output_Mode(pInstance)== E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF)
               || (MDrv_XC_Get_3D_Output_Mode(pInstance)== E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF_HW))
            && (MDrv_XC_Get_3D_HW_Version() >= 1))
        {
            gSrcInfo[eWindow].stDispWin.x = gSrcInfo[eWindow].stDispWin.x * 2;
        }

        //for ver3d =1, display window will be half when pass into driver,so here need double it
        if(((MDrv_XC_Get_3D_Output_Mode(pInstance)== E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF)
                || (MDrv_XC_Get_3D_Output_Mode(pInstance)== E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF_HW))
            && (MDrv_XC_Get_3D_HW_Version() == 1))
        {
            u32TempWidth *= 2;
        }

#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
        if ((IsFSCOrFRCEnable())
            && bChangeDisplaySize)
        {
            if( pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width / 2 > ( gSrcInfo[eWindow].stDispWin.x + u32TempWidth ) )
            {
                gSrcInfo[eWindow].stDispWin.x =
                    pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width / 2 - ( gSrcInfo[eWindow].stDispWin.x + u32TempWidth );
            }
            else
            {
                //pixshift case will do overscan,changed display.x&width,x + width >stPanelInfo.u16Width,so need not error handling
                if( !((pXCResourcePrivate->stdrvXC_MVideo.gePixelShiftFeature & PIXEL_SHIFT_DO_OVERSCAN )== PIXEL_SHIFT_DO_OVERSCAN))
                {
                    gSrcInfo[eWindow].stDispWin.x = 0;
                }
            }
        }
        else
#endif
        if(MDrv_XC_Is2K2KToFrc(pInstance))
        {
            if( pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width / 2 > ( gSrcInfo[eWindow].stDispWin.x + u32TempWidth ) )
            {
                gSrcInfo[eWindow].stDispWin.x =
                    pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width / 2 - ( gSrcInfo[eWindow].stDispWin.x + u32TempWidth );
            }
            else
            {
                //pixshift case will do overscan,changed display.x&width,x + width >stPanelInfo.u16Width,so need not error handling
                if( !((pXCResourcePrivate->stdrvXC_MVideo.gePixelShiftFeature & PIXEL_SHIFT_DO_OVERSCAN )== PIXEL_SHIFT_DO_OVERSCAN))
                {
                    gSrcInfo[eWindow].stDispWin.x = 0;
                }
            }
        }
        else
        {
            if( pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width > ( gSrcInfo[eWindow].stDispWin.x + u32TempWidth ) )
            {
                gSrcInfo[eWindow].stDispWin.x =
                    pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width - ( gSrcInfo[eWindow].stDispWin.x + u32TempWidth );
            }
            else
            {
                //pixshift case will do overscan,changed display.x&width,x + width >stPanelInfo.u16Width,so need not error handling
                if( !((pXCResourcePrivate->stdrvXC_MVideo.gePixelShiftFeature & PIXEL_SHIFT_DO_OVERSCAN )== PIXEL_SHIFT_DO_OVERSCAN))
                {
                    gSrcInfo[eWindow].stDispWin.x = 0;
                }
            }
        }

        if(((MDrv_XC_Get_3D_Output_Mode(pInstance)== E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF)
                || (MDrv_XC_Get_3D_Output_Mode(pInstance)== E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF_HW))
            &&(MDrv_XC_Get_3D_HW_Version() >= 1))
        {
            gSrcInfo[eWindow].stDispWin.x /= 2;

            //adjust main/sub display window position for ver1
            //for ver>=2, this logic is at MDrv_XC_3D_LoadReg
            if(MDrv_XC_Get_3D_HW_Version() == 1)
            {
                if(eWindow == MAIN_WINDOW)
                {
                    gSrcInfo[eWindow].stDispWin.x += pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width / 2;
                }
                else
                {
                    if(gSrcInfo[eWindow].stDispWin.x >= pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width / 2)
                    {
                        gSrcInfo[eWindow].stDispWin.x -= pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width / 2;
                    }
                }
            }
        }
    }

    if ((SUPPORT_3D_DS == 0 && !g_bSWDSGenScenario)
        || SUPPORT_3D_DS == 1)
    {
        MDrv_SC_3D_AdjustHShift(pInstance, &gSrcInfo[eWindow], eWindow);
    }

    if(MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[DS] MDrv_XC_GetDynamicScalingStatus():%d MDrv_XC_Is_DSForceIndexEnabled():%d\n",MDrv_XC_GetDynamicScalingStatus(pInstance),MDrv_XC_Is_DSForceIndexEnabled(pInstance, eWindow));

        if(MDrv_XC_GetDynamicScalingStatus(pInstance) || MDrv_XC_Is_DSForceIndexEnabled(pInstance, eWindow) )
        {
            gSrcInfo[eWindow].stCapWin.width = pstXC_SetWin_Info->stCapWin.width;
            gSrcInfo[eWindow].stCapWin.height = pstXC_SetWin_Info->stCapWin.height;
            gSrcInfo[eWindow].stCropWin.width = pstXC_SetWin_Info->stCropWin.width;
            gSrcInfo[eWindow].stCropWin.height = pstXC_SetWin_Info->stCropWin.height;
        }
    }
    else
    {
        //reset pixel shift preH and preV data,SW DS pixshift need not reset
        pXCResourcePrivate->stdrvXC_MVideo.s8HPixelShift = 0;
        pXCResourcePrivate->stdrvXC_MVideo.s8VPixelShift = 0;
    }

#ifdef STELLAR
    if(stTCONPanelConfig.bUsingTCON)
    {
        PNL_ApiExtStatus sTCONPnlExtStatus;
        sTCONPnlExtStatus.u16ApiStatusEX_Length = sizeof(PNL_ApiExtStatus);
        MApi_PNL_GetStatusEx(&sTCONPnlExtStatus);
        gSrcInfo[eWindow].stDispWin.x += sTCONPnlExtStatus.u16DEHStart ;//pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HStart;
        gSrcInfo[eWindow].stDispWin.y += sTCONPnlExtStatus.u16DEVStart;//pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VStart;
    }
    else
#endif
    {
        gSrcInfo[eWindow].stDispWin.x += pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HStart;
        gSrcInfo[eWindow].stDispWin.y += pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VStart;
    }

    // dump debug msg
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"============= SetWindow Start (Window : %u, src: %u) =============\n",eWindow, gSrcInfo[eWindow].enInputSourceType);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"CapWin  x: %4u  y: %4u  w: %4u  h: %4u \n",gSrcInfo[eWindow].stCapWin.x, gSrcInfo[eWindow].stCapWin.y, gSrcInfo[eWindow].stCapWin.width, gSrcInfo[eWindow].stCapWin.height);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"CropWin x: %4u  y: %4u  w: %4u  h: %4u \n",gSrcInfo[eWindow].stCropWin.x, gSrcInfo[eWindow].stCropWin.y, gSrcInfo[eWindow].stCropWin.width, gSrcInfo[eWindow].stCropWin.height);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"DispWin x: %4u  y: %4u  w: %4u  h: %4u \n",gSrcInfo[eWindow].stDispWin.x,gSrcInfo[eWindow].stDispWin.y, gSrcInfo[eWindow].stDispWin.width, gSrcInfo[eWindow].stDispWin.height);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Panel   x: %4u  y: %4u  w: %4u  h: %4u \n",
                                       pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HStart,
                                       pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VStart,
                                       pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width,
                                       pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"HMirror/VMirror/Interlace/Hdup = %u/%u/%u/%u\n", IsHMirrorMode(eWindow),IsVMirrorMode(eWindow), pstXC_SetWin_Info->bInterlace, pstXC_SetWin_Info->bHDuplicate);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"H/V total = (%u, %u), VFreq = %u\n", pstXC_SetWin_Info->u16DefaultHtotal, pstXC_SetWin_Info->u16InputVTotal, pstXC_SetWin_Info->u16InputVFreq);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Pre  H cus scaling %u (%u -> %u)\n", gSrcInfo[eWindow].Status2.bPreHCusScaling, gSrcInfo[eWindow].Status2.u16PreHCusScalingSrc, gSrcInfo[eWindow].Status2.u16PreHCusScalingDst);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Pre  V cus scaling %u (%u -> %u)\n", gSrcInfo[eWindow].Status2.bPreVCusScaling, gSrcInfo[eWindow].Status2.u16PreVCusScalingSrc, gSrcInfo[eWindow].Status2.u16PreVCusScalingDst);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Post H cus scaling %u (%u -> %u)\n", gSrcInfo[eWindow].bHCusScaling, gSrcInfo[eWindow].u16HCusScalingSrc, gSrcInfo[eWindow].u16HCusScalingDst);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Post V cus scaling %u (%u -> %u)\n", gSrcInfo[eWindow].bVCusScaling, gSrcInfo[eWindow].u16VCusScalingSrc, gSrcInfo[eWindow].u16VCusScalingDst);

#if VERIFY_MVIDEO_FPGA  //non-de-interlace function
    if(gSrcInfo[eWindow].bInterlace)
    {
        gSrcInfo[eWindow].bInterlace = 0;
        binterlace =TRUE;//gSrcInfo[eWindow].u16V_DisSize /= 2;
    }
#endif

#ifdef XC_SUPPORT_2STEP_SCALING
    #ifdef UFO_XC_SUPPORT_2STEP_SCALING
    MDrv_XC_SetIsSupport2StepScalingFlag(FALSE);
    if(MDrv_ReadByte(REG_CHIP_REVISION) >= 1)//Monet U02 fix .
    {
        if((pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HTotal > 3000)          // 4K output
         &&(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VTotal > 2000)
         &&(MDrv_XC_Get_3D_Output_Mode(pInstance) == E_XC_3D_OUTPUT_MODE_NONE)          //3D can not enable 2-step scaling. Hardware limitation
         &&(MDrv_XC_Get_3D_Input_Mode(pInstance, eWindow) == E_XC_3D_INPUT_MODE_NONE))
        {
            if( (gSrcInfo[eWindow].stCapWin.width < 1440)
                && (gSrcInfo[eWindow].stDispWin.width == pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width)
                )
            {
                MDrv_XC_SetIsSupport2StepScalingFlag(TRUE);
            }
            else
            {
                MDrv_XC_SetIsSupport2StepScalingFlag(FALSE);
            }
        }
    }
    #endif
#endif

    gSrcInfo[eWindow].u16InputVFreq = pstXC_SetWin_Info->u16InputVFreq;

    //Since SVP V4, need to set policy before authorize XC buffer
    if(_u32SVPVersion > 3)
    {
        u8OPTEEVersion = MHal_XC_GetOPTeeVersion(_u32SVPVersion);

        XC_LOG_TRACE(XC_DBGLEVEL_SVP,"XC SVP settings, SVPVersion:%u, u8OPTEEVersion:%u, OPTEE isEnable:%s, NeedSetPolicy:%s, NeedPipelineBinding:%s \n",
            _u32SVPVersion,
            u8OPTEEVersion,
            pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].isEnable?("TRUE"):("FALSE"),
            pXCResourcePrivate->stdrvXC_MVideo.abXCNeedSetPolicy[eWindow]?("TRUE"):("FALSE"),
            pXCResourcePrivate->stdrvXC_MVideo.abXCNeedPipelineBinding[eWindow]?("TRUE"):("FALSE"));

        if(pXCResourcePrivate->stdrvXC_MVideo.abXCNeedSetPolicy[eWindow] == TRUE)
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SVP,"XC Setwindow Set Policy Start. Display pipe ID:%u\n", pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].pipeID);
            if (pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].isEnable == FALSE)
            {
                XC_LOG_TRACE(XC_DBGLEVEL_SVP,"XC set policy fail. Wrong flow!! Please call optee enable first.\n");
            }
            else if(pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].pipeID == 0)
            {
                XC_LOG_TRACE(XC_DBGLEVEL_SVP,"XC set policy fail. Invalid display pipe ID = 0\n");
            }
            else
            {
                if(_MDrv_XC_TEE_Send_Cmd(pInstance, E_XC_OPTEE_SET_POLICY, (void*)&pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow], sizeof(XC_OPTEE_HANDLER)) == FALSE)
                {
                    XC_LOG_TRACE(XC_DBGLEVEL_SVP,"_MDrv_XC_TEE_Send_Cmd fail\n");
                }
                pXCResourcePrivate->stdrvXC_MVideo.abXCNeedSetPolicy[eWindow] = FALSE;
            }
        }
    }

#if (ENABLE_CAL_XCMEM_FROM_MADIVR == FALSE)
#if (XC_SUPPORT_IOMMU == TRUE)
    if(pXCResourcePrivate->stdrvXC_MVideo_Context.stMMAfeature.u32Support == MSOS_MMA_SUPPORT)
    {
        if (pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].isEnable == FALSE)
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"MMA: MMA XC Alloc Start.\n");
            if (_MDrv_XC_GetMemoryFromMMA(pInstance,eWindow) == FALSE)
            {
                printf("%s,%d,Get XC MMA memory fail\n",__FUNCTION__,__LINE__);
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                return FALSE;
            }
            else
            {
                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"MMA: MMA XC Alloc Done.\n");
            }
        }
        else
        {
            MHal_XC_AuthorizeMMA(pInstance, eWindow);
        }

#if FRC_INSIDE
        if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFRCEnabled == TRUE)
        {
            if (pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].isEnable == FALSE)
            {
                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"MMA: MMA FRC Alloc Start.\n");
                if(_MDrv_FRC_GetMemoryFromMMA(pInstance, eWindow) == FALSE)
                {
                    printf("%s,%d,Get FRC MMA memory fail\n",__FUNCTION__,__LINE__);
                    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                    return FALSE;
                }
                else
                {
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"MMA: MMA FRC Alloc Done.\n");
                }
            }
            else
            {
                MHal_FRC_AuthorizeMMA(pInstance, eWindow);
            }
        }
        else
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"MMA  no need FRC MMA Alloc.\n");
        }
#endif
    }
#endif
#endif

#if (ENABLE_CAL_XCMEM_FROM_MADIVR == FALSE)
#ifndef DONT_USE_CMA
#if (XC_SUPPORT_CMA ==TRUE)
    if (_MDrv_XC_CheckIfConfigCMA(pInstance, eWindow))
    {
        MS_BOOL bIsGetCMA[CMA_XC_MEM_NUM] = {FALSE, };
        MS_U32 au32CMAMemSCMSize[CMA_XC_MEM_NUM] = {0, };
        MS_U32 au32CMAMemFRCMSize[CMA_XC_MEM_NUM] = {0, };
        MS_U32 u32DualMiuMemSize = 0;
        MS_BOOL bIsCMAChange[CMA_XC_MEM_NUM] = {FALSE, };

        if (MDrv_XC_IsInputSourceDisabled(pInstance, MAIN_WINDOW, INPUT_SOURCE_NONE) == TRUE)
        {
            MHal_XC_CMAPatchClose(pInstance, MAIN_WINDOW);
            bNeedDeattachCMAISR = TRUE;
        }
        else
        {
            MHal_XC_CMAPatch(pInstance, MAIN_WINDOW);
        }

        #ifdef CONFIG_MSTAR_SRAMPD
        // control SRAM PD here
        if (MHal_XC_Is_SRAM_PowerDown(pInstance) == TRUE)
        {
            // enable SRAM here
            MHal_XC_SRAM_PowerDown_Control(pInstance, FALSE);
        }
        #endif

        MHal_XC_Get_CMA_UsingCondition(pInstance, bIsGetCMA, au32CMAMemSCMSize, au32CMAMemFRCMSize, &u32DualMiuMemSize, CMA_XC_MEM_NUM, eWindow);
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], bIsGetCMA[self]=%d bIsGetCMA[cobuff]=%d \n",__func__,__LINE__,bIsGetCMA[CMA_XC_SELF_MEM],bIsGetCMA[CMA_XC_COBUFF_MEM]);
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], au32CMAMemSCMSize[self]=%x au32CMAMemSCMSize[cobuff]=%x u32DualMiuMemSize=%x\n",__func__,__LINE__,au32CMAMemSCMSize[CMA_XC_SELF_MEM],au32CMAMemSCMSize[CMA_XC_COBUFF_MEM],u32DualMiuMemSize);
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], u32PreCMAMemSCMSize[self]=%x u32PreCMAMemSCMSize[cobuff]=%x \n",__func__,__LINE__,gSrcInfo[eWindow].u32PreCMAMemSCMSize[CMA_XC_SELF_MEM],gSrcInfo[eWindow].u32PreCMAMemSCMSize[CMA_XC_COBUFF_MEM]);
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], u32PreCMAMemFRCMSize[self]=%x u32PreCMAMemFRCMSize[cobuff]=%x \n",__func__,__LINE__,gSrcInfo[eWindow].u32PreCMAMemFRCMSize[CMA_XC_SELF_MEM],gSrcInfo[eWindow].u32PreCMAMemFRCMSize[CMA_XC_COBUFF_MEM]);
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], u32PreCMAMemDualSCMSize=%x \n",__func__, __LINE__, gSrcInfo[eWindow].Status2.u32PreCMAMemDualSCMSize);

        if(  ((bIsGetCMA[CMA_XC_SELF_MEM] == TRUE)&&((au32CMAMemSCMSize[CMA_XC_SELF_MEM]!=gSrcInfo[eWindow].u32PreCMAMemSCMSize[CMA_XC_SELF_MEM])||(au32CMAMemFRCMSize[CMA_XC_SELF_MEM]!=gSrcInfo[eWindow].u32PreCMAMemFRCMSize[CMA_XC_SELF_MEM])))
           ||((bIsGetCMA[CMA_XC_COBUFF_MEM] == TRUE)&&((u32DualMiuMemSize!=gSrcInfo[eWindow].Status2.u32PreCMAMemDualSCMSize)||(au32CMAMemSCMSize[CMA_XC_COBUFF_MEM]!=gSrcInfo[eWindow].u32PreCMAMemSCMSize[CMA_XC_COBUFF_MEM])||(au32CMAMemFRCMSize[CMA_XC_COBUFF_MEM]!=gSrcInfo[eWindow].u32PreCMAMemFRCMSize[CMA_XC_COBUFF_MEM]))))
        {
            MS_PHY u32CMAAddr[CMA_XC_MEM_NUM] = {0, };

            if (bIsGetCMA[CMA_XC_SELF_MEM] == TRUE)
            {
                if ((au32CMAMemSCMSize[CMA_XC_SELF_MEM]!=gSrcInfo[eWindow].u32PreCMAMemSCMSize[CMA_XC_SELF_MEM])||(au32CMAMemFRCMSize[CMA_XC_SELF_MEM]!=gSrcInfo[eWindow].u32PreCMAMemFRCMSize[CMA_XC_SELF_MEM]))
                {
                    bIsCMAChange[CMA_XC_SELF_MEM] = TRUE;
                }
            }
            else
            {
                MHal_XC_Release_CMA(pInstance, CMA_XC_SELF_MEM, MAIN_WINDOW);
                bIsCMAChange[CMA_XC_SELF_MEM] = FALSE;
                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], Release self buff!\n",__func__, __LINE__);
            }

            if (bIsGetCMA[CMA_XC_COBUFF_MEM] == TRUE)
            {
                if ((u32DualMiuMemSize!=gSrcInfo[eWindow].Status2.u32PreCMAMemDualSCMSize)
                   ||(au32CMAMemSCMSize[CMA_XC_COBUFF_MEM]!=gSrcInfo[eWindow].u32PreCMAMemSCMSize[CMA_XC_COBUFF_MEM])
                   ||(au32CMAMemFRCMSize[CMA_XC_COBUFF_MEM]!=gSrcInfo[eWindow].u32PreCMAMemFRCMSize[CMA_XC_COBUFF_MEM]))
                {
                    bIsCMAChange[CMA_XC_COBUFF_MEM] = TRUE;
                }
            }
            else
            {
                bIsCMAChange[CMA_XC_COBUFF_MEM] = FALSE;
            }

            _MDrv_XC_Release_CMA(pInstance, bIsCMAChange , CMA_XC_MEM_NUM, eWindow);
            if(_MDrv_XC_Get_CMA_Addr(pInstance, bIsGetCMA, au32CMAMemSCMSize, au32CMAMemFRCMSize, &u32DualMiuMemSize, bIsCMAChange, u32CMAAddr, CMA_XC_MEM_NUM, eWindow) == FALSE)
            {
                printf("[%s,%5d][CMA], cma alloc failed! return here.\n",__func__, __LINE__);
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                return FALSE;
            }

            XC_CMA_CLIENT enXCCMAClient = CMA_XC_SELF_MEM;
            if (u32DualMiuMemSize != 0)
            {
                enXCCMAClient = CMA_XC_SELF_MEM;
            }
            else
            {
                if (bIsGetCMA[CMA_XC_COBUFF_MEM] == TRUE)
                {
                    enXCCMAClient = CMA_XC_COBUFF_MEM;
                }
                else
                {
                    enXCCMAClient = CMA_XC_SELF_MEM;
                }
            }
#if (HW_DESIGN_4K2K_VER == 4)
            if(au32CMAMemSCMSize[enXCCMAClient] == 0)
            {
                // default frcm frame buffer address init
                MDrv_XC_SetFRCMFrameBufferAddress(pInstance, u32CMAAddr[enXCCMAClient], au32CMAMemFRCMSize[enXCCMAClient], eWindow);
                // default frame buffer address init
                MDrv_XC_SetFrameBufferAddress(pInstance, u32CMAAddr[enXCCMAClient], au32CMAMemFRCMSize[enXCCMAClient], E_XC_BUFFER_DEFAULT, eWindow);
            }
            else
#endif
            {
                if(bIsCMAChange[enXCCMAClient] == TRUE)
                {
#if (HW_DESIGN_4K2K_VER == 4)
                    // default frcm frame buffer address init
                    MDrv_XC_SetFRCMFrameBufferAddress(pInstance, u32CMAAddr[enXCCMAClient]+au32CMAMemSCMSize[enXCCMAClient], au32CMAMemFRCMSize[enXCCMAClient], eWindow);
#endif
                    // default frame buffer address init
                    MDrv_XC_SetFrameBufferAddress(pInstance, u32CMAAddr[enXCCMAClient], au32CMAMemSCMSize[enXCCMAClient], E_XC_BUFFER_DEFAULT, eWindow);
                }
            }
#ifdef UFO_XC_SUPPORT_DUAL_MIU
            if (u32DualMiuMemSize != 0)
            {
                // default dual frame buffer address init
                MDrv_XC_SetDualFrameBufferAddress(pInstance, u32CMAAddr[CMA_XC_COBUFF_MEM], u32DualMiuMemSize, eWindow);
            }
#endif
#if (HW_DESIGN_4K2K_VER == 4)
            if ((MDrv_XC_Is_DSForceIndexEnabled(pInstance, MAIN_WINDOW)||MDrv_XC_GetDynamicScalingStatus(pInstance))
                && IsSrcTypeStorage(gSrcInfo[eWindow].enInputSourceType))
            {
                // MM source and DS on
                if(!gSrcInfo[eWindow].bInterlace)
                {
                    //DS ON , 4K2K case
                    Hal_SC_frcmw_Memory_Write_Request(pInstance, DISABLE, MAIN_WINDOW);
                }
                else
                {
                    Hal_SC_frcmw_Memory_Write_Request(pInstance, ENABLE, MAIN_WINDOW);
                }
            }
            else if ((gSrcInfo[eWindow].stCapWin.width > 3000)
                &&(gSrcInfo[eWindow].stCapWin.height> 1050))
            {
                Hal_SC_frcmw_Memory_Write_Request(pInstance, DISABLE, MAIN_WINDOW);
            }
            else
            {
                Hal_SC_frcmw_Memory_Write_Request(pInstance, ENABLE, MAIN_WINDOW);
            }
#endif
            pXCResourcePrivate->stdrvXC_MVideo.au32XCSCMIMemSize[MAIN_WINDOW] = au32CMAMemSCMSize[enXCCMAClient];
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], SCMIMemSize=%x\n",__func__, __LINE__, au32CMAMemSCMSize[enXCCMAClient]);
        }
        else
        {
            MHal_SC_Memory_Request_Force_Off(pInstance, FALSE, E_XC_FORCE_MEMORY_REQUEST_OFF_ALL);
        }
    }
    else
    {
        if((_u32SVPVersion > 3) && (SVP_PARSE_HW_VER(u8OPTEEVersion) != E_XC_OPTEE_VER_1BIT))
        {
            MHal_XC_AuthorizeCMA(pInstance, eWindow);
        }
    }
    if (pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].isEnable == TRUE)
    {
        #ifdef CONFIG_MSTAR_SRAMPD
        // control SRAM PD here
        if (MHal_XC_Is_SRAM_PowerDown(pInstance) == TRUE)
        {
            // enable SRAM here
            MHal_XC_SRAM_PowerDown_Control(pInstance, FALSE);
        }
        #endif
    }
#endif
#endif
#endif

    //Get VB size
    if(!g_bSWDSGenScenario)
    {
        pstXCVBox = malloc(sizeof(XC_VBOX_INFO));
        if(pstXCVBox)
        {
            memset(pstXCVBox, 0, sizeof(XC_VBOX_INFO));
        }
        else
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"pstXCVBox is NULL ! \n");
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            return FALSE;
        }
        pstXCVBox->u32Version = ST_XC_VBOX_INFO_VERSION_DTVDS;
        pstXCVBox->u32Length = sizeof(XC_VBOX_INFO);
        pstXCVBox->u16SrcHSize[0] = gSrcInfo[eWindow].stCapWin.width;
        pstXCVBox->u16SrcVSize[0] = gSrcInfo[eWindow].stCapWin.height;
        pstXCVBox->enSourceType[0] = gSrcInfo[eWindow].enInputSourceType;

        pstXCVBox->bInterface[eWindow] = gSrcInfo[eWindow].bInterlace;
        pstXCVBox->u16Vfreq[eWindow] = pstXC_SetWin_Info->u16InputVFreq;

        if (MDrv_XC_Get_VirtualBox_Info(pInstance,pstXCVBox) == TRUE)
        {
            gSrcInfo[eWindow].Status2.u16VirtualBox_Width = pstXCVBox->u16VBox_Htotal[eWindow];
            gSrcInfo[eWindow].Status2.u16VirtualBox_Height = pstXCVBox->u16VBox_Vtotal[eWindow];
        }
        else
        {
            gSrcInfo[eWindow].Status2.u16VirtualBox_Width = gSrcInfo[eWindow].stCapWin.width;
            gSrcInfo[eWindow].Status2.u16VirtualBox_Height = gSrcInfo[eWindow].stCapWin.height;
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d] [DS] MDrv_XC_Get_VirtualBox_Info error!! u16VBox_Htotal:%d u16VBox_Vtotal:%d  \n",__func__,__LINE__,gSrcInfo[eWindow].Status2.u16VirtualBox_Width,gSrcInfo[eWindow].Status2.u16VirtualBox_Height);
        }
    }

    // Get VB size from MVOP Timing
    if((IsSrcTypeDTV(gSrcInfo[eWindow].enInputSourceType) || IsSrcTypeStorage(gSrcInfo[eWindow].enInputSourceType))
        && !g_bSWDSGenScenario)
    {
        MVOP_Handle stMvopHd = { E_MVOP_MODULE_MAIN };
        MVOP_MFDECRotate Rotate = E_MVOP_MF_ROTATE_NONE;
        MS_BOOL bEnableMFDEC_ROTATE = FALSE;
        MVOP_Timing *pstMVOPTiming = NULL;

        pstMVOPTiming = malloc(sizeof(MVOP_Timing));
        if(pstMVOPTiming)
        {
            memset(pstMVOPTiming, 0, sizeof(MVOP_Timing));
        }
        else
        {
            if(pstXCVBox)
            {
                free(pstXCVBox);
            }

            if(pstMVOPTiming)
            {
                free(pstMVOPTiming);
            }
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"pstXCVBox or pstMVOPTiming is NULL ! \n");
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            return FALSE;
        }

#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
        if(psXCInstPri->u32DeviceID == 0)
#else
        if(eWindow == MAIN_WINDOW)
#endif
        {
            if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_bSwitchMVOPToSCPath) //sub MVOP TO SC0
            {
                MDrv_MVOP_SubGetOutputTiming(pstMVOPTiming);
            }
            else //main MVOP to SC0
            {
                MDrv_MVOP_GetOutputTiming(pstMVOPTiming);
            }
        }
        else
        {
            if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_bSwitchMVOPToSCPath)//main MVOP to SC1
            {
                MDrv_MVOP_GetOutputTiming(pstMVOPTiming);
            }
            else //sub MVOP to SC1
            {
                MDrv_MVOP_SubGetOutputTiming(pstMVOPTiming);
            }
        }

        if (eWindow == MAIN_WINDOW)
        {
            stMvopHd.eModuleNum = E_MVOP_MODULE_MAIN;
        }
        else
        {
            stMvopHd.eModuleNum = E_MVOP_MODULE_SUB;
        }
        MDrv_MVOP_GetCommand(&stMvopHd,E_MVOP_CMD_GET_MFDEC_ROTATE,&Rotate,sizeof(MVOP_MFDECRotate));
        bEnableMFDEC_ROTATE = (Rotate == E_MVOP_MF_ROTATE_90 || E_MVOP_MF_ROTATE_270 == Rotate);

        if(bEnableMFDEC_ROTATE)
        {
            if(pstMVOPTiming->u16Height < pstXCVBox->u16VBox_Htotal[eWindow])
            {
                gSrcInfo[eWindow].Status2.u16VirtualBox_Width = pstMVOPTiming->u16Height;
            }

            if(pstMVOPTiming->u16Width < pstXCVBox->u16VBox_Vtotal[eWindow])
            {
                gSrcInfo[eWindow].Status2.u16VirtualBox_Height = pstMVOPTiming->u16Width;
            }
        }
        else
        {
            if(pstMVOPTiming->u16Width < pstXCVBox->u16VBox_Htotal[eWindow])
            {
                gSrcInfo[eWindow].Status2.u16VirtualBox_Width = pstMVOPTiming->u16Width;
            }

            if(pstMVOPTiming->u16Height < pstXCVBox->u16VBox_Vtotal[eWindow])
            {
                gSrcInfo[eWindow].Status2.u16VirtualBox_Height = pstMVOPTiming->u16Height;
            }
        }

        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d] [DS] u16VBox_Htotal:%d u16VBox_Vtotal:%d  \n",__func__,__LINE__,gSrcInfo[eWindow].Status2.u16VirtualBox_Width,gSrcInfo[eWindow].Status2.u16VirtualBox_Height);
        if(pstMVOPTiming)
        {
            free(pstMVOPTiming);
        }
    }

    if (!g_bSWDSGenScenario)
    {
        if(pstXCVBox)
        {
            free(pstXCVBox);
        }
    }

    if((MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow) || MHal_XC_IsSourceWithoutMvopSuportDS())
        && !IsSrcMVOP(gSrcInfo[eWindow].enInputSourceType))
    {
#ifdef UFO_XC_SCAN_TYPE
        if(pXCResourcePrivate->stdrvXC_MVideo.g_ScanType == E_XC_FORCE_P_MODE)
        {
            MHal_XC_SetVirtualBox(pInstance, &gSrcInfo[eWindow].Status2.u16VirtualBox_Width, &gSrcInfo[eWindow].Status2.u16VirtualBox_Height);
        }
#endif
    }

#if ((HW_DESIGN_4K2K_VER == 4)||(HW_DESIGN_4K2K_VER == 6)||(HW_DESIGN_4K2K_VER == 7))
    if (!g_bSWDSGenScenario)
    {
        XC_InternalStatus *pSrcInfo = NULL;

        pSrcInfo = malloc(sizeof(XC_InternalStatus));
        if(pSrcInfo)
        {
            memcpy(pSrcInfo,&gSrcInfo[eWindow],sizeof(XC_InternalStatus));
        }
        else
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"pSrcInfo is NULL ! \n");
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            return FALSE;
        }
        if((MDrv_XC_GetDynamicScalingStatus(pInstance) || MDrv_XC_Is_DSForceIndexEnabled(pInstance, eWindow)) && IsSrcMVOP(gSrcInfo[eWindow].enInputSourceType))
        {
            if(  !gSrcInfo[eWindow].bInterlace
               ||(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_SAVE_MEM_MODE))
            {
                 if(MDrv_XC_Is_UpdateDsCmd(pInstance,eWindow))
                 {
                     pSrcInfo->stCapWin.width     = gSrcInfo[eWindow].stCapWin.width;
                     pSrcInfo->stCapWin.height    = gSrcInfo[eWindow].stCapWin.height;
                 }
                 else
                 {
                     pSrcInfo->stCapWin.width     = pSrcInfo->Status2.u16VirtualBox_Width;
                     pSrcInfo->stCapWin.height    = pSrcInfo->Status2.u16VirtualBox_Height;
                  }
            }
            else
            {
                pSrcInfo->stCapWin.width     = gSrcInfo[eWindow].stCropWin.width;
                pSrcInfo->stCapWin.height    = gSrcInfo[eWindow].stCropWin.height;
            }
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d] [DS] [PQSrcData] width:%d height:%d  \n",__func__,__LINE__,pSrcInfo->stCapWin.width,pSrcInfo->stCapWin.height);
        }
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        _MDrv_XC_Set_PQ_SourceData(pInstance, eWindow, gSrcInfo[eWindow].enInputSourceType,pSrcInfo);
        if(pSrcInfo)
        {
            free(pSrcInfo);
        }
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    }
#else
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    // Scaler must update source before width / height checking.
    _MDrv_XC_Set_PQ_SourceData(pInstance, eWindow, gSrcInfo[eWindow].enInputSourceType, &gSrcInfo[eWindow]);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
#endif

    //if HW 3d ver > 1 in 3d case, sub PQ source type don't set, because there is no setwin(sub). So load first row in PQ table in quality_map_sub.
    //it need set sub PQ source type the same with Main's source type.
    if((MDrv_XC_Get_3D_Input_Mode(pInstance, eWindow) != E_XC_3D_INPUT_MODE_NONE)
        && (MDrv_XC_Get_3D_HW_Version() > 1)
        && (eWindow == MAIN_WINDOW))
    {
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        _MDrv_XC_Set_PQ_SourceData(pInstance, SUB_WINDOW, gSrcInfo[eWindow].enInputSourceType, &gSrcInfo[eWindow]);
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    }

    if(((MHal_XC_IsSupportZNR() == TRUE) || (MHal_XC_GetCropMode() == E_XC_CROP_MIU_ENHANCED_MODE))
         && !MDrv_XC_Is_UpdateDsCmd(pInstance,eWindow))
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"ZNR + SCMI Buffer Size:%x window:%u \n",pXCResourcePrivate->stdrvXC_MVideo.au32XCSCMIMemSize[eWindow], eWindow);
        MDrv_XC_SetFrameBufferAddress(pInstance, pXCResourcePrivate->stdrvXC_Scaling._u32DNRBaseAddr0[eWindow],
                                      pXCResourcePrivate->stdrvXC_MVideo.au32XCSCMIMemSize[eWindow], E_XC_BUFFER_UCM, eWindow);
    }

    if(eWindow == SUB_WINDOW)
    {
        //For PIP, enable IP Read/write
        _XC_ENTRY(pInstance);
        MDrv_XC_SetScalerMemoryRequest(pInstance, TRUE, E_XC_MEMORY_IP_READ_REQUEST, SUB_WINDOW);
        MDrv_XC_SetScalerMemoryRequest(pInstance, TRUE, E_XC_MEMORY_IP_WRITE_REQUEST, SUB_WINDOW);
        _XC_RETURN(pInstance);
    }

    if (IsSrcTypeVga(gSrcInfo[eWindow].enInputSourceType)   ||
        IsSrcTypeYPbPr(gSrcInfo[eWindow].enInputSourceType) ||
        IsSrcTypeScart(gSrcInfo[eWindow].enInputSourceType) )
    {
        if(!((E_XC_3D_INPUT_MODE_NONE != MDrv_XC_Get_3D_Input_Mode(pInstance, eWindow))
              && (SUB_WINDOW == eWindow)))// don't set adc mode when set sub window at 3d
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"ADC Set Mode \n");
            PQ_ADC_SAMPLING_INFO stPQADCSamplingInfo;
            memset(&stPQADCSamplingInfo,0,sizeof(PQ_ADC_SAMPLING_INFO));
            if ( IsSrcTypeVga(gSrcInfo[eWindow].enInputSourceType) || IsSrcTypeYPbPr(gSrcInfo[eWindow].enInputSourceType) )
            {
                if(gSrcInfo[eWindow].bHDuplicate)
                {
                    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                    MDrv_XC_ADC_GetPQADCSamplingInfo(pInstance, gSrcInfo[eWindow].enInputSourceType, &gSrcInfo[eWindow], &stPQADCSamplingInfo, eWindow);
                    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
                }
            }

            if(!MDrv_XC_Is_UpdateDsCmd(pInstance,eWindow))
            {
                MDrv_XC_ADC_SetMode(pInstance, gSrcInfo[eWindow].enInputSourceType, &gSrcInfo[eWindow], &stPQADCSamplingInfo, eWindow);
            }
        }
    }
    else
    {
        if (!g_bSWDSGenScenario)
        {
            MDrv_XC_ADC_SCART_RGB_setMode(pInstance,FALSE);
        }
    }
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

#ifdef ADC_BYPASS_YPBPR_MACRO_VISION_PATCH
    // patch for china player
    // there are some undefined signal between sync and DE in YPbPr fullHD
    // so we ignore this signal with coast window
    if(IsSrcTypeYPbPr(gSrcInfo[eWindow].enInputSourceType) && (!pstXC_SetWin_Info->bInterlace))
    {
        //MS_U16 u16ADC_PC_Clk = 0;
        //u16ADC_PC_Clk = MDrv_XC_ADC_GetPcClock(pInstance);
        //if(u16ADC_PC_Clk>2635 && u16ADC_PC_Clk<2645 )
        if((1920 == gSrcInfo[eWindow].stCapWin.width) && (1080 == gSrcInfo[eWindow].stCapWin.height))
        {
            MDrv_SC_ip_set_coast_window(pInstance,0x0F, 0x0F, eWindow);
        }
    }
#endif

#if (ENABLE_CAL_XCMEM_FROM_MADIVR == TRUE)
#if (XC_SUPPORT_IOMMU == TRUE)
    if(pXCResourcePrivate->stdrvXC_MVideo_Context.stMMAfeature.u32Support == MSOS_MMA_SUPPORT)
    {
        if (pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].isEnable == FALSE)
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"MMA: MMA XC Alloc Start.\n");
            if (_MDrv_XC_GetMemoryFromMMA(pInstance,eWindow) == FALSE)
            {
                printf("%s,%d,Get XC MMA memory fail\n",__FUNCTION__,__LINE__);
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                return FALSE;
            }
            else
            {
                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"MMA: MMA XC Alloc Done.\n");
            }
        }
        else
        {
            MHal_XC_AuthorizeMMA(pInstance, eWindow);
        }

#if FRC_INSIDE
        if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFRCEnabled == TRUE)
        {
            if (pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].isEnable == FALSE)
            {
                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"MMA: MMA FRC Alloc Start.\n");
                if(_MDrv_FRC_GetMemoryFromMMA(pInstance, eWindow) == FALSE)
                {
                    printf("%s,%d,Get FRC MMA memory fail\n",__FUNCTION__,__LINE__);
                    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                    return FALSE;
                }
                else
                {
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"MMA: MMA FRC Alloc Done.\n");
                }
            }
            else
            {
                MHal_FRC_AuthorizeMMA(pInstance, eWindow);
            }
        }
        else
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"MMA  no need FRC MMA Alloc.\n");
        }
#endif
    }
#endif
#endif

#if (ENABLE_CAL_XCMEM_FROM_MADIVR == TRUE)
#ifndef DONT_USE_CMA
#if (XC_SUPPORT_CMA ==TRUE)
    if (_MDrv_XC_CheckIfConfigCMA(pInstance, eWindow))
    {
        MS_BOOL bIsGetCMA[CMA_XC_MEM_NUM] = {FALSE, };
        MS_U32 au32CMAMemSCMSize[CMA_XC_MEM_NUM] = {0, };
        MS_U32 au32CMAMemFRCMSize[CMA_XC_MEM_NUM] = {0, };
        MS_U32 u32DualMiuMemSize = 0;
        MS_BOOL bIsCMAChange[CMA_XC_MEM_NUM] = {FALSE, };

        if (MDrv_XC_IsInputSourceDisabled(pInstance, MAIN_WINDOW, INPUT_SOURCE_NONE) == TRUE)
        {
            MHal_XC_CMAPatchClose(pInstance, MAIN_WINDOW);
            bNeedDeattachCMAISR = TRUE;
        }
        else
        {
            MHal_XC_CMAPatch(pInstance, MAIN_WINDOW);
        }

        #ifdef CONFIG_MSTAR_SRAMPD
        // control SRAM PD here
        if (MHal_XC_Is_SRAM_PowerDown(pInstance) == TRUE)
        {
            // enable SRAM here
            MHal_XC_SRAM_PowerDown_Control(pInstance, FALSE);
        }
        #endif

        MHal_XC_Get_CMA_UsingCondition(pInstance, bIsGetCMA, au32CMAMemSCMSize, au32CMAMemFRCMSize, &u32DualMiuMemSize, CMA_XC_MEM_NUM, eWindow);
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], bIsGetCMA[self]=%d bIsGetCMA[cobuff]=%d \n",__func__,__LINE__,bIsGetCMA[CMA_XC_SELF_MEM],bIsGetCMA[CMA_XC_COBUFF_MEM]);
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], au32CMAMemSCMSize[self]=%x au32CMAMemSCMSize[cobuff]=%x u32DualMiuMemSize=%x\n",__func__,__LINE__,au32CMAMemSCMSize[CMA_XC_SELF_MEM],au32CMAMemSCMSize[CMA_XC_COBUFF_MEM],u32DualMiuMemSize);
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], u32PreCMAMemSCMSize[self]=%x u32PreCMAMemSCMSize[cobuff]=%x \n",__func__,__LINE__,gSrcInfo[eWindow].u32PreCMAMemSCMSize[CMA_XC_SELF_MEM],gSrcInfo[eWindow].u32PreCMAMemSCMSize[CMA_XC_COBUFF_MEM]);
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], u32PreCMAMemFRCMSize[self]=%x u32PreCMAMemFRCMSize[cobuff]=%x \n",__func__,__LINE__,gSrcInfo[eWindow].u32PreCMAMemFRCMSize[CMA_XC_SELF_MEM],gSrcInfo[eWindow].u32PreCMAMemFRCMSize[CMA_XC_COBUFF_MEM]);
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], u32PreCMAMemDualSCMSize=%x \n",__func__, __LINE__, gSrcInfo[eWindow].Status2.u32PreCMAMemDualSCMSize);

        if(  ((bIsGetCMA[CMA_XC_SELF_MEM] == TRUE)&&((au32CMAMemSCMSize[CMA_XC_SELF_MEM]!=gSrcInfo[eWindow].u32PreCMAMemSCMSize[CMA_XC_SELF_MEM])||(au32CMAMemFRCMSize[CMA_XC_SELF_MEM]!=gSrcInfo[eWindow].u32PreCMAMemFRCMSize[CMA_XC_SELF_MEM])))
           ||((bIsGetCMA[CMA_XC_COBUFF_MEM] == TRUE)&&((u32DualMiuMemSize!=gSrcInfo[eWindow].Status2.u32PreCMAMemDualSCMSize)||(au32CMAMemSCMSize[CMA_XC_COBUFF_MEM]!=gSrcInfo[eWindow].u32PreCMAMemSCMSize[CMA_XC_COBUFF_MEM])||(au32CMAMemFRCMSize[CMA_XC_COBUFF_MEM]!=gSrcInfo[eWindow].u32PreCMAMemFRCMSize[CMA_XC_COBUFF_MEM]))))
        {
            MS_PHY u32CMAAddr[CMA_XC_MEM_NUM] = {0, };

            if (bIsGetCMA[CMA_XC_SELF_MEM] == TRUE)
            {
                if ((au32CMAMemSCMSize[CMA_XC_SELF_MEM]!=gSrcInfo[eWindow].u32PreCMAMemSCMSize[CMA_XC_SELF_MEM])||(au32CMAMemFRCMSize[CMA_XC_SELF_MEM]!=gSrcInfo[eWindow].u32PreCMAMemFRCMSize[CMA_XC_SELF_MEM]))
                {
                    bIsCMAChange[CMA_XC_SELF_MEM] = TRUE;
                }
            }
            else
            {
                MHal_XC_Release_CMA(pInstance, CMA_XC_SELF_MEM, MAIN_WINDOW);
                bIsCMAChange[CMA_XC_SELF_MEM] = FALSE;
                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], Release self buff!\n",__func__, __LINE__);
            }

            if (bIsGetCMA[CMA_XC_COBUFF_MEM] == TRUE)
            {
                if ((u32DualMiuMemSize!=gSrcInfo[eWindow].Status2.u32PreCMAMemDualSCMSize)
                   ||(au32CMAMemSCMSize[CMA_XC_COBUFF_MEM]!=gSrcInfo[eWindow].u32PreCMAMemSCMSize[CMA_XC_COBUFF_MEM])
                   ||(au32CMAMemFRCMSize[CMA_XC_COBUFF_MEM]!=gSrcInfo[eWindow].u32PreCMAMemFRCMSize[CMA_XC_COBUFF_MEM]))
                {
                    bIsCMAChange[CMA_XC_COBUFF_MEM] = TRUE;
                }
            }
            else
            {
                bIsCMAChange[CMA_XC_COBUFF_MEM] = FALSE;
            }

            _MDrv_XC_Release_CMA(pInstance, bIsCMAChange , CMA_XC_MEM_NUM, eWindow);
            if(_MDrv_XC_Get_CMA_Addr(pInstance, bIsGetCMA, au32CMAMemSCMSize, au32CMAMemFRCMSize, &u32DualMiuMemSize, bIsCMAChange, u32CMAAddr, CMA_XC_MEM_NUM, eWindow) == FALSE)
            {
                printf("[%s,%5d][CMA], cma alloc failed! return here.\n",__func__, __LINE__);
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                return FALSE;
            }

            XC_CMA_CLIENT enXCCMAClient = CMA_XC_SELF_MEM;
            if (u32DualMiuMemSize != 0)
            {
                enXCCMAClient = CMA_XC_SELF_MEM;
            }
            else
            {
                if (bIsGetCMA[CMA_XC_COBUFF_MEM] == TRUE)
                {
                    enXCCMAClient = CMA_XC_COBUFF_MEM;
                }
                else
                {
                    enXCCMAClient = CMA_XC_SELF_MEM;
                }
            }
#if (HW_DESIGN_4K2K_VER == 4)
            if(au32CMAMemSCMSize[enXCCMAClient] == 0)
            {
                // default frcm frame buffer address init
                MDrv_XC_SetFRCMFrameBufferAddress(pInstance, u32CMAAddr[enXCCMAClient], au32CMAMemFRCMSize[enXCCMAClient], eWindow);
                // default frame buffer address init
                MDrv_XC_SetFrameBufferAddress(pInstance, u32CMAAddr[enXCCMAClient], au32CMAMemFRCMSize[enXCCMAClient], E_XC_BUFFER_DEFAULT, eWindow);
            }
            else
#endif
            {
                if(bIsCMAChange[enXCCMAClient] == TRUE)
                {
#if (HW_DESIGN_4K2K_VER == 4)
                    // default frcm frame buffer address init
                    MDrv_XC_SetFRCMFrameBufferAddress(pInstance, u32CMAAddr[enXCCMAClient]+au32CMAMemSCMSize[enXCCMAClient], au32CMAMemFRCMSize[enXCCMAClient], eWindow);
#endif
                    // default frame buffer address init
                    MDrv_XC_SetFrameBufferAddress(pInstance, u32CMAAddr[enXCCMAClient], au32CMAMemSCMSize[enXCCMAClient], E_XC_BUFFER_DEFAULT, eWindow);
                }
            }
#ifdef UFO_XC_SUPPORT_DUAL_MIU
            if (u32DualMiuMemSize != 0)
            {
                // default dual frame buffer address init
                MDrv_XC_SetDualFrameBufferAddress(pInstance, u32CMAAddr[CMA_XC_COBUFF_MEM], u32DualMiuMemSize, eWindow);
            }
#endif
#if (HW_DESIGN_4K2K_VER == 4)
            if ((MDrv_XC_Is_DSForceIndexEnabled(pInstance, MAIN_WINDOW)||MDrv_XC_GetDynamicScalingStatus(pInstance))
                && IsSrcTypeStorage(gSrcInfo[eWindow].enInputSourceType))
            {
                // MM source and DS on
                if(!gSrcInfo[eWindow].bInterlace)
                {
                    //DS ON , 4K2K case
                    Hal_SC_frcmw_Memory_Write_Request(pInstance, DISABLE, MAIN_WINDOW);
                }
                else
                {
                    Hal_SC_frcmw_Memory_Write_Request(pInstance, ENABLE, MAIN_WINDOW);
                }
            }
            else if ((gSrcInfo[eWindow].stCapWin.width > 3000)
                &&(gSrcInfo[eWindow].stCapWin.height> 1050))
            {
                Hal_SC_frcmw_Memory_Write_Request(pInstance, DISABLE, MAIN_WINDOW);
            }
            else
            {
                Hal_SC_frcmw_Memory_Write_Request(pInstance, ENABLE, MAIN_WINDOW);
            }
#endif
            pXCResourcePrivate->stdrvXC_MVideo.au32XCSCMIMemSize[MAIN_WINDOW] = au32CMAMemSCMSize[enXCCMAClient];
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][CMA], SCMIMemSize=%x\n",__func__, __LINE__, au32CMAMemSCMSize[enXCCMAClient]);
        }
        else
        {
            MHal_SC_Memory_Request_Force_Off(pInstance, FALSE, E_XC_FORCE_MEMORY_REQUEST_OFF_ALL);
        }
    }
    else
    {
        if((_u32SVPVersion > 3) && (SVP_PARSE_HW_VER(u8OPTEEVersion) != E_XC_OPTEE_VER_1BIT))
        {
            MHal_XC_AuthorizeCMA(pInstance, eWindow);
        }
    }
    if (pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].isEnable == TRUE)
    {
        #ifdef CONFIG_MSTAR_SRAMPD
        // control SRAM PD here
        if (MHal_XC_Is_SRAM_PowerDown(pInstance) == TRUE)
        {
            // enable SRAM here
            MHal_XC_SRAM_PowerDown_Control(pInstance, FALSE);
        }
        #endif
    }
#endif
#endif
#endif

    if (!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
    {
        msAPI_Scaler_SetMode(pInstance, gSrcInfo[eWindow].enInputSourceType, &gSrcInfo[eWindow], eWindow);
    }

//If support SVP V4 & 9bits AID, need to do Pipeline Binding
if((_u32SVPVersion > 3) && (SVP_PARSE_HW_VER(u8OPTEEVersion) != E_XC_OPTEE_VER_1BIT))
{
    if(pXCResourcePrivate->stdrvXC_MVideo.abXCNeedPipelineBinding[eWindow] == TRUE)
    {
        if (pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].isEnable == FALSE)
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SVP,"XC pipeline binding fail. Wrong flow!! Please call optee enable first.\n");
        }
        else if((pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].pipeID == 0) || (pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].u32InputPipeID == 0))
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SVP,"XC pipeline binding fail. Invalid pipe ID! Display pipe ID:%u, Video pipe ID:%u\n", pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].pipeID, pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].u32InputPipeID);
        }
        else
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SVP,"XC Setwindow Pipeline Binding Start. Display pipe ID:%u, Video pipe ID:%u\n", pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].pipeID, pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].u32InputPipeID);
            if(_MDrv_XC_TEE_Send_Cmd(pInstance, E_XC_OPTEE_SET_VIDEO_PIPELINE, (void*)&pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow], sizeof(XC_OPTEE_HANDLER)) == FALSE)
            {
                XC_LOG_TRACE(XC_DBGLEVEL_SVP,"_MDrv_XC_TEE_Send_Cmd fail\n");
            }
            else
            {
                pXCResourcePrivate->stdrvXC_MVideo.abXCNeedPipelineBinding[eWindow] = FALSE;   //Binding done reset flag
            }
        }
    }
    else
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SVP,"No need pipeline binding.\n");
    }
}


_XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);

#if ( HW_SUPPORT_3D_CB_AND_PA  == FALSE)
    //move here in order to set FB/FBL by madi mode.
    bRet = MDrv_SC_3D_FormatValidation(pInstance, &gSrcInfo[eWindow], eWindow);
#endif

#ifdef UFO_XC_SCAN_TYPE
    //Patch for Force P flow. Both P/I set memory type as 2500 mode.
    if(pXCResourcePrivate->stdrvXC_MVideo.g_ScanType == E_XC_FORCE_P_MODE)
    {
        if((psXCInstPri->u32DeviceID == 0) && !MDrv_XC_Is_SupportSWDS(pInstance, MAIN_WINDOW))
        {
            MHal_XC_SetMemoryType_ForceP(pInstance, pstXC_SetWin_Info->bInterlace);
        }
    }
#endif

#if ENABLE_REQUEST_FBL
    if(!(MDrv_XC_GetDynamicScalingStatus(pInstance) || MDrv_XC_Is_DSForceIndexEnabled(pInstance, eWindow)) )
    {
        if ( (MDrv_XC_IsRequestFrameBufferLessMode(pInstance) && MDrv_XC_IsCurrentRequest_FrameBufferLessMode(pInstance))
             && (!MDrv_XC_IsCurrentFrameBufferLessMode(pInstance)) )
        {
            printf("====No Crop for RFBL==\n");

            gSrcInfo[eWindow].stCropWin.x = 0;
            gSrcInfo[eWindow].stCropWin.y = 0;
            gSrcInfo[eWindow].stCropWin.width = gSrcInfo[eWindow].stCapWin.width;
            gSrcInfo[eWindow].stCropWin.height= gSrcInfo[eWindow].stCapWin.height;
        }
    }
#endif

    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"UCNR enabled: %s \n", (MDrv_XC_Get_OPWriteOffEnable(pInstance, eWindow) == TRUE) ? "NO" : "YES");

#ifdef ENABLE_SCALING_WO_MUTE
    if(DISABLE == gSrcInfo[eWindow].bBlackscreenEnabled)
    {
        MDrv_SC_Adjust_Prescaling_Ratio(pInstance, eWindow);
    }
#endif

    //control HSK mode and GenTimingFromXC mode
    if(!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow) && MDrv_SC_CheckMuteStatusByRegister(pInstance, eWindow))
    {
        Hal_SC_MVOP_Gen_Timing_From_XC_Control(pInstance,eWindow);
        Hal_SC_MVOP_HandShake_Control(pInstance,eWindow,&pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg);
    }

    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    MDrv_SC_set_prescaling_ratio(pInstance, gSrcInfo[eWindow].enInputSourceType, &gSrcInfo[eWindow], eWindow );
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"CapWin  x: %4u  y: %4u  w: %4u  h: %4u \n",gSrcInfo[eWindow].stCapWin.x, gSrcInfo[eWindow].stCapWin.y, gSrcInfo[eWindow].stCapWin.width, gSrcInfo[eWindow].stCapWin.height);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"CropWin x: %4u  y: %4u  w: %4u  h: %4u \n",gSrcInfo[eWindow].stCropWin.x, gSrcInfo[eWindow].stCropWin.y, gSrcInfo[eWindow].stCropWin.width, gSrcInfo[eWindow].stCropWin.height);
    // SCMI miu dual mode Phase1: Dual enable under hdmirx 4k2k, dual bw ration 2:2
    // User should judge when enable dual by call dual enable api. So remove auto start dual enable.
#if 0
#ifndef DONT_USE_CMA
#ifdef UFO_XC_SUPPORT_DUAL_MIU
    if (eWindow == MAIN_WINDOW)
    {
        if (IsSrcTypeHDMI(gSrcInfo[eWindow].enInputSourceType) && ((gSrcInfo[(eWindow)].u16H_SizeAfterPreScaling> 1920) && (gSrcInfo[(eWindow)].u16V_SizeAfterPreScaling> 1080)))
        {
            MDrv_XC_EnableMiuDualMode(pInstance, TRUE, 2, 2, MAIN_WINDOW);
        }
    }
#endif
#endif
#endif

    MDrv_SC_set_crop_window(pInstance, &gSrcInfo[eWindow], eWindow );

    //fo_hvsp check condition
    if(eWindow == MAIN_WINDOW)
    {
        if(!MHal_XC_Is_Enable_HFR_Path(pInstance, eWindow) && MHal_XC_IsSupportFOHVSP())
        {
            MHal_XC_FOHVSPCondition(pInstance, eWindow, pstXC_SetWin_Info);
        }
    }

    if(!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
    {
        MDrv_SC_set_2p_mode(pInstance, pstXC_SetWin_Info, eWindow);
    }

    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"CapWin  x: %4u  y: %4u  w: %4u  h: %4u \n",gSrcInfo[eWindow].stCapWin.x, gSrcInfo[eWindow].stCapWin.y, gSrcInfo[eWindow].stCapWin.width, gSrcInfo[eWindow].stCapWin.height);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"CropWin x: %4u  y: %4u  w: %4u  h: %4u \n",gSrcInfo[eWindow].stCropWin.x, gSrcInfo[eWindow].stCropWin.y, gSrcInfo[eWindow].stCropWin.width, gSrcInfo[eWindow].stCropWin.height);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"DispWin x: %4u  y: %4u  w: %4u  h: %4u \n",gSrcInfo[eWindow].stDispWin.x,gSrcInfo[eWindow].stDispWin.y, gSrcInfo[eWindow].stDispWin.width, gSrcInfo[eWindow].stDispWin.height);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    MDrv_SC_set_postscaling_ratio(pInstance, &gSrcInfo[eWindow], eWindow );

#ifdef ENABLE_TV_SC2_PQ
    if (psXCInstPri->u32DeviceID == PQ_XC_ID_0)
#endif
    {
        MS_BOOL bTmpFBL = FALSE;
#ifdef UFO_XC_FB_LEVEL
        if((MDrv_XC_IsFBL(gSrcInfo[eWindow].eFBLevel))||(MDrv_XC_IsRFBL(gSrcInfo[eWindow].eFBLevel)))
        {
            bTmpFBL = TRUE;
        }
#else
        bTmpFBL = gSrcInfo[eWindow].bFBL;
#endif
        if (s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_420upsampling != NULL)
        {
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
            s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_420upsampling(
                            (psXCInstPri->u32DeviceID == 1) ?
                            PQ_SUB_WINDOW :
                            PQ_MAIN_WINDOW,
                            bTmpFBL,
                            gSrcInfo[eWindow].bPreV_ScalingDown,
                            gSrcInfo[eWindow].ScaledCropWin.y);
#else
            s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_420upsampling(
                            eWindow == SUB_WINDOW ?
                            PQ_SUB_WINDOW :
                            PQ_MAIN_WINDOW,
                            bTmpFBL,
                            gSrcInfo[eWindow].bPreV_ScalingDown,
                            gSrcInfo[eWindow].ScaledCropWin.y);
#endif
        }
    }
#ifdef ENABLE_TV_SC2_PQ
    else if (psXCInstPri->u32DeviceID == PQ_XC_ID_1)
    {
            MS_BOOL bTmpFBL = FALSE;
#ifdef UFO_XC_FB_LEVEL
        if((MDrv_XC_IsFBL(gSrcInfo[eWindow].eFBLevel))||(MDrv_XC_IsRFBL(gSrcInfo[eWindow].eFBLevel)))
        {
            bTmpFBL = TRUE;
        }
#else
        bTmpFBL = gSrcInfo[eWindow].bFBL;
#endif

        if (s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_420upsampling_ex != NULL)
        {
            s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_420upsampling_ex(psXCInstPri->u32DeviceID,
                            eWindow == SUB_WINDOW ?
                            PQ_SUB_WINDOW :
                            PQ_MAIN_WINDOW,
                            bTmpFBL,
                            gSrcInfo[eWindow].bPreV_ScalingDown,
                            gSrcInfo[eWindow].ScaledCropWin.y);
        }
    }
#endif
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    // FIXME: direct use variable instead of stDBreg?
    _XC_ENTRY(pInstance);

    //for 3D handle invalid crop window,or else LR status may be inverse.
    if(E_XC_3D_INPUT_CHECK_BORAD == MDrv_XC_Get_3D_Input_Mode(pInstance, eWindow))
    {
        gSrcInfo[eWindow].ScaledCropWin.y &= ~0x1;
        gSrcInfo[eWindow].ScaledCropWin.x &= ~0x1;
        gSrcInfo[eWindow].ScaledCropWin.height &= ~0x1;
        gSrcInfo[eWindow].ScaledCropWin.width &= ~0x1;
    }
    else if(E_XC_3D_INPUT_LINE_ALTERNATIVE == MDrv_XC_Get_3D_Input_Mode(pInstance, eWindow))
    {
        gSrcInfo[eWindow].ScaledCropWin.y &= ~0x1;
        gSrcInfo[eWindow].ScaledCropWin.height &= ~0x1;
    }
    else if(E_XC_3D_INPUT_PIXEL_ALTERNATIVE == MDrv_XC_Get_3D_Input_Mode(pInstance, eWindow))
    {
        gSrcInfo[eWindow].ScaledCropWin.x &= ~0x1;
        gSrcInfo[eWindow].ScaledCropWin.width &= ~0x1;
    }

#if (HW_DESIGN_4K2K_VER == 4)
    // temp solution for monaco
    // MDrv_SC_set_fetch_number_limit will change by MDrv_SC_set_frcm_fetch_number_limit when it stable
    // but function name always is MDrv_SC_set_fetch_number_limit
    MDrv_SC_set_frcm_fetch_number_limit(pInstance, &gSrcInfo[eWindow], eWindow );
#else
    MDrv_SC_set_fetch_number_limit(pInstance, &gSrcInfo[eWindow], eWindow );
#endif
    MDrv_sc_set_capture_window(pInstance, eWindow );

#if VERIFY_MVIDEO_FPGA //non-de-interlace function=> celear second frame half garbage
    if(binterlace)
    {
        gSrcInfo[eWindow].stDispWin.height/= 2;
        gSrcInfo[eWindow].u16InputVTotal /= 2;
    }
#endif

#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
    // For Manhattan, FSC+FRC
    if ((eWindow == MAIN_WINDOW) && (IsFSCOrFRCEnable()))
    {
        if (bChangeDisplaySize)
        {
            // FHD to FSC case, but bk10 display window need to be original size
            #ifdef STELLAR
            if(stTCONPanelConfig.bUsingTCON)
            {
                gSrcInfo[eWindow].stDispWin.x = gSrcInfo[eWindow].stDispWin.x*2 - sTCONPnlExtStatus.u16DEHStart;
                gSrcInfo[eWindow].stDispWin.width = gSrcInfo[eWindow].stDispWin.width*2;
                gSrcInfo[eWindow].stDispWin.y = gSrcInfo[eWindow].stDispWin.y*2 - sTCONPnlExtStatus.u16DEVStart;
                gSrcInfo[eWindow].stDispWin.height = gSrcInfo[eWindow].stDispWin.height*2;
            }
            else
            #endif
            {
                gSrcInfo[eWindow].stDispWin.x = gSrcInfo[eWindow].stDispWin.x*2 - pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HStart;
                gSrcInfo[eWindow].stDispWin.width = gSrcInfo[eWindow].stDispWin.width*2;
                gSrcInfo[eWindow].stDispWin.y = gSrcInfo[eWindow].stDispWin.y*2 - pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VStart;
                gSrcInfo[eWindow].stDispWin.height = gSrcInfo[eWindow].stDispWin.height*2;
            }

            // for custom scaling case
            if(gSrcInfo[eWindow].bHCusScaling)
            {
                gSrcInfo[eWindow].u16HCusScalingDst = gSrcInfo[eWindow].u16HCusScalingDst*2;
            }
            if(gSrcInfo[eWindow].bVCusScaling)
            {
                gSrcInfo[eWindow].u16VCusScalingDst = gSrcInfo[eWindow].u16VCusScalingDst*2;
            }
        }
    }
#endif
    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    MDrv_XC_DLC_CalculateDynamicRange(pInstance,pstXC_SetWin_Info->bInterlace,eWindow,E_XC_DLC_IP);
    MDrv_XC_DLC_CalculateDynamicRange(pInstance,pstXC_SetWin_Info->bInterlace,eWindow,E_XC_DLC_VIP);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);
#ifdef PATCH_TCON_BRING_UP
        if((((MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type) == E_XC_PNL_LPLL_EPI28_12P)
          && !(strcmp(panelName_UD_VB1_8LANE_DRDEPI,MDrv_PNL_GetName())))
        {
            gSrcInfo[eWindow].stDispWin.y = gSrcInfo[eWindow].stDispWin.y+1;
        }
#endif

    // FIXME: direct use variable instead of stDBreg?
    MDrv_SC_set_display_window(pInstance,  eWindow );

    #ifdef MONACO_SC2_PATCH
    // patch!!
    if(psXCInstPri->u32DeviceID == 1)
    {
        Hal_SC_set_de_window(pInstance ,&pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo);
    }
    #endif

    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Physical DispWin HStart: %4u  HEnd: %4u  Vstart: %4u  VEnd: %4u \n",
                                       pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg.u16H_DisStart,
                                       pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg.u16H_DisEnd,
                                       pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg.u16V_DisStart,
                                       pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg.u16V_DisEnd);

    if((!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))&&(Hal_SC_CheckMuteStatusByRegister(pInstance,eWindow) == TRUE))
    {
        //support fhd down clk
        if(MHal_XC_IsSupportFHDDownClk()== TRUE)
        {
            MHal_XC_FHDDownClk(pInstance,eWindow);
        }
        else
        {
            // Adjust FCLK by different case
#if defined(UFO_XC_FB_LEVEL)
            if((MDrv_XC_IsFBL(gSrcInfo[eWindow].eFBLevel))||(MDrv_XC_IsRFBL(gSrcInfo[eWindow].eFBLevel)))//FIXME
#else
            if (gSrcInfo[eWindow].bFBL)
#endif
            {
                MDrv_SC_set_Fclk(pInstance, EN_FCLK_FBL,CKG_FCLK_DEFAULT);
            }
            else
            {
                MDrv_SC_set_Fclk(pInstance, EN_FCLK_NORMAL,CKG_FCLK_DEFAULT);
            }
        }
    }
    // Adjust display line buffer start mode by different case
#ifdef UFO_XC_FB_LEVEL
    if((MDrv_XC_IsFBL(gSrcInfo[eWindow].eFBLevel))||(MDrv_XC_IsRFBL(gSrcInfo[eWindow].eFBLevel)))
#else
    if (gSrcInfo[eWindow].bFBL || gSrcInfo[eWindow].bR_FBL)
#endif
    {
        MDrv_SC_SetDisplay_Start_Mode(pInstance, DISABLE);
    }
    else
    {
        MDrv_SC_SetDisplay_Start_Mode(pInstance, ENABLE);
    }

    //UpDate Utopia XC status to CFD
    MDrv_XC_Update_XCStatus(pInstance, eWindow);

    if (bDualWindow == TRUE)
    {
        if (eWindow == MAIN_WINDOW)
        {
            memcpy(&pXCResourcePrivate->stdrvXC_MVideo_Context.stMainDBreg, &pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg, sizeof(SC_SWDB_INFO));
            pXCResourcePrivate->stdrvXC_MVideo._bSetDualMainWinDone = TRUE;
        }
        else
        {
            memcpy(&pXCResourcePrivate->stdrvXC_MVideo_Context.stSubDBreg
                   , &pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg, sizeof(SC_SWDB_INFO));
            pXCResourcePrivate->stdrvXC_MVideo._bSetDualSubWinDone = TRUE;
        }

        if (pXCResourcePrivate->stdrvXC_MVideo._bSetDualMainWinDone
            && pXCResourcePrivate->stdrvXC_MVideo._bSetDualSubWinDone)
        {
            MDrv_SC_dual_sw_db(pInstance, &pXCResourcePrivate->stdrvXC_MVideo_Context.stMainDBreg
                               , &pXCResourcePrivate->stdrvXC_MVideo_Context.stSubDBreg);
            pXCResourcePrivate->stdrvXC_MVideo._bSetDualMainWinDone = FALSE;
            pXCResourcePrivate->stdrvXC_MVideo._bSetDualSubWinDone = FALSE;
        }
    }
    else
    {

        if(MDrv_SC_CheckMuteStatusByRegister(pInstance, eWindow))
        {
            MHal_XC_Set_LockPoint_DelayLines(pInstance,eWindow,&pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg);
        }
        MHal_XC_IPVsyncTrig_Control(pInstance,pstXC_SetWin_Info->bInterlace,eWindow,&pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg);

        if(!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
        {
            MDrv_SC_sw_db(pInstance, &pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg, eWindow );
        }

//IP2VE setting for BW in MM  && HDMI case
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
        if(psXCInstPri->u32DeviceID == 0)
#else
        if(eWindow == MAIN_WINDOW)
#endif
        {
            _MDrv_XC_SetFlowControl(pInstance, eWindow);
        }
    }

    //update time stamp setting
    MHal_XC_SetTimeStamp(pInstance, MAIN_WINDOW);

#if (SUPPORT_3D_DS == 0)
#if ((HW_DESIGN_4K2K_VER == 1)||(HW_DESIGN_4K2K_VER == 4)||(HW_DESIGN_4K2K_VER == 6)||(HW_DESIGN_4K2K_VER == 7))
    if(MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
    {
#if (defined (ANDROID)&& defined(UFO_XC_DS_PQ))
        MDrv_PQ_set_imode_PQDS(pstXC_SetWin_Info->bInterlace);
#endif
        MDrv_SWDS_AddCmd(pInstance,&pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg,eWindow);
#ifndef ENABLE_XC_WRITE_CMD_API
        MDrv_SWDS_Fire(pInstance,eWindow);
#endif
    }
#endif
#endif

    //For VIP peaking setting
    MDrv_XC_VIP_Peaking_Setting(pInstance, eWindow);

#if SUPPORT_SEAMLESS_ZAPPING
    if(INPUT_SOURCE_DTV == gSrcInfo[eWindow].enInputSourceType)
    {
        //When HDMI 4K2K open compress mode change to PX2 mode need to toggle the compress flag clear
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK08_40_L, BIT(0), BIT(0));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK08_40_L, 0, BIT(0));
    }
#endif

    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    if(bRet == FALSE)
    {
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
        UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
        // coverity issue
        E_XC_3D_INPUT_MODE en3DInputMode = MDrv_XC_Get_3D_Input_Mode(pInstance, eWindow);
        E_XC_3D_OUTPUT_MODE en3DOutputMode = MDrv_XC_Get_3D_Output_Mode(pInstance);
        MDrv_XC_3D_LoadReg(pInstance, en3DInputMode, en3DOutputMode, &gSrcInfo[eWindow], eWindow);
#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
        if(!(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_B & E_XC_INIT_MISC_B_HDMITX_ENABLE)
            && pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFRCEnabled)
        {
            Hal_SC_3D_Enable_FRC_LR_Flag(pInstance, FALSE); //disable FRC LR  flag
            MDrv_XC_FRC_Set_Input3DFormat(pInstance, E_XC_3D_INPUT_MODE_NONE);
            MDrv_XC_FRC_R2_Set_InputFrameSize(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width, \
                        pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height);
            MDrv_XC_FRC_R2_Set_OutputFrameSize(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width, \
                        pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height);
        }
#endif
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    }

#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 3)) // only Napoli need this
    // SW patch for MM 4K BW issue
    if ((pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HTotal > 3000)
        && (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VTotal > 2000)
        && (pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg.u16OPMFetch > 1920)
        && (pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg.u16VLen > 1080))
    {
        /// for BW issue
        if ((IsSrcTypeStorage(gSrcInfo[eWindow].enInputSourceType))||(IsSrcTypeHDMI(gSrcInfo[eWindow].enInputSourceType)))
        {
            // SC
            SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_02_L, BIT(14), BIT(14));  // turn off ip read for bw issue
        }
        else
        {
#ifdef UFO_XC_FB_LEVEL
            if(MDrv_XC_IsFB(gSrcInfo[MAIN_WINDOW].eFBLevel))//FIXME
#else
            if(!gSrcInfo[MAIN_WINDOW].bFBL)
#endif
            {
                SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_02_L, 0x00, BIT(14));
            }
        }
    }
    else
    {
#ifdef UFO_XC_FB_LEVEL
        if(MDrv_XC_IsFB(gSrcInfo[MAIN_WINDOW].eFBLevel))//FIXME
#else
        if(!gSrcInfo[MAIN_WINDOW].bFBL)
#endif
        {
            SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_02_L, 0x00, BIT(14));
        }
    }
    // SC opm line buffer lens
    SC_W2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK12_24_L, 0xA03E); // OP read request
    SC_W2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK12_68_L, 0x3E00); // IPM arbiter
    SC_W2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK12_63_L, 0x3F00); // IPM arbiter
    SC_W2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK12_22_L, 0x2020); // IPM request length

#endif

    #if (HW_DESIGN_4K2K_VER == 4)
    //SW patch for HDMI 4K@60 BW issue, mantis: 782590
    if((pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HTotal > 3000)          // 4K output
        && (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VTotal > 2000)
        && (IsSrcTypeHDMI(gSrcInfo[eWindow].enInputSourceType))                                         // HDMI
        && (pstXC_SetWin_Info->stCapWin.width >= 0x780)                                                 // 4K2K@50,60 input
        && (pstXC_SetWin_Info->stCapWin.height >= 0x870)
        && (pstXC_SetWin_Info->u16InputVFreq >= 490))
    {
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK32_24_L, 0x0008, 0x00FF);  // OP read request
    }
    else
    {
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK32_24_L, 0x0020, 0x00FF);
    }

#if (XC_SUPPORT_FASTSWITCH_BY_IP1GP==TRUE)
        MS_U16 u16Height  = SC_R2BYTEMSK(0, MHal_XC_FSS_BankTransfer(pInstance, REG_SC_BK01_06_L, eWindow), 0x1FFF);
        MS_U16 u16Vtt     = SC_R2BYTEMSK(0, MHal_XC_FSS_BankTransfer(pInstance, REG_SC_BK01_1F_L, eWindow), 0x1FFF);
#else
        MS_U16 u16Height  = SC_R2BYTEMSK(0, REG_SC_BK01_06_L, 0x1FFF);
        MS_U16 u16Vtt     = SC_R2BYTEMSK(0, REG_SC_BK01_1F_L, 0x1FFF);
#endif

    if (  (pstXC_SetWin_Info->enInputSourceType == INPUT_SOURCE_VGA)
        &&(u16Vtt - u16Height <= 5))
    {
        MDrv_XC_MLoad_set_IP_trig_p(pInstance, 0x02, 0x03);
        MDrv_XC_MemControl_Switch_Method_By_Vcnt(pInstance, FALSE, 0x0);
    }
    else if(MDrv_XC_GetDynamicScalingStatus(pInstance) || MDrv_XC_Is_DSForceIndexEnabled(pInstance, eWindow) )
    {
        MDrv_XC_MLoad_set_IP_trig_p(pInstance, 0x01, 0x02);
        MDrv_XC_MemControl_Switch_Method_By_Vcnt(pInstance, FALSE, 0x00);
        MDrv_XC_edclk_pd_control(pInstance, FALSE, 0x00, 0x00);
    }

#if(HW_DESIGN_3D_VER == 5) //monaco/muji mode
   //several lines of garbage on the top FPI to SBSH
   else if ((MApi_XC_Get_3D_Input_Mode(MAIN_WINDOW) == E_XC_3D_INPUT_FRAME_PACKING)
         && (pstXC_SetWin_Info->stCapWin.height == DOUBLEHD_1080X2I_VSIZE))
    {
        MS_U16 u16TrainTriggerPoint = 0;
        MS_U16 u16DispTriggerPoint = 0;
        MDrv_XC_MLoad_get_IP_trig_p(pInstance, &u16TrainTriggerPoint, &u16DispTriggerPoint);
        if((u16TrainTriggerPoint != 0x00) && (u16DispTriggerPoint != 0x01))
        {
            MDrv_XC_MLoad_get_IP_trig_p(pInstance, &pXCResourcePrivate->sthal_SC.u16OldValue[E_STORE_VALUE_IP_AUTO_TUNE_AREA_TRIG], &pXCResourcePrivate->sthal_SC.u16OldValue[E_STORE_VALUE_IP_DISP_AREA_TRIG]);
            MDrv_XC_MLoad_set_IP_trig_p(pInstance, 0x0000, 0x0001);
        }
    }
#endif

    else
    {
        MS_U16 u16TrainTriggerPoint = 0;
        MS_U16 u16DispTriggerPoint = 0;
        MDrv_XC_MLoad_get_IP_trig_p(pInstance, &u16TrainTriggerPoint, &u16DispTriggerPoint);

        if ((pXCResourcePrivate->sthal_SC.u16OldValue[E_STORE_VALUE_IP_AUTO_TUNE_AREA_TRIG] != u16TrainTriggerPoint)
            || (pXCResourcePrivate->sthal_SC.u16OldValue[E_STORE_VALUE_IP_DISP_AREA_TRIG] != u16DispTriggerPoint))
        {
            MDrv_XC_MemControl_Switch_Method_By_Vcnt(pInstance, TRUE, 0x04);
            MDrv_XC_MLoad_set_IP_trig_p(pInstance, pXCResourcePrivate->sthal_SC.u16OldValue[E_STORE_VALUE_IP_AUTO_TUNE_AREA_TRIG], pXCResourcePrivate->sthal_SC.u16OldValue[E_STORE_VALUE_IP_DISP_AREA_TRIG]);
        }
    }

    if( !(MDrv_XC_GetDynamicScalingStatus(pInstance) || MDrv_XC_Is_DSForceIndexEnabled(pInstance, eWindow)) )
    {
        // not need open edclk control when vblanking is little
        if (u16Vtt - u16Height <= 24)
        {
            MDrv_XC_edclk_pd_control(pInstance, FALSE, 0x00, 0x00);
        }
        else
        {
            MDrv_XC_edclk_pd_control(pInstance, TRUE, 0x04, 0x08);
        }
    }

    //DTV 1080i patch for mantis:0735773
    MDrv_XC_DTVPatch(pInstance,eWindow);

#endif

    if((((MDrv_XC_Is_DSForceIndexEnabled(pInstance, MAIN_WINDOW)||MDrv_XC_GetDynamicScalingStatus(pInstance)) && (IS_DS_4K2K(eWindow)) && (gSrcInfo[eWindow].u16InputVFreq > 330)) //UHD DS case
           || (IsSrcMVOP(gSrcInfo[eWindow].enInputSourceType) && IS_INPUT_4K2K(eWindow) && (gSrcInfo[eWindow].u16InputVFreq > 330))))
    {
#if (defined(USE_MVOP_VS_TO_GEN_VS_TO_OPVS) && USE_MVOP_VS_TO_GEN_VS_TO_OPVS == TRUE)
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID,REG_SC_BK02_10_L, BIT(2), BIT(2));
#endif

#if (defined(PATCH_ENABLE_MVOP_HANDSHAKE_MODE_WHEN_BW_NOT_ENOUGH_IN_120HZ_PANEL))
#if (PATCH_ENABLE_MVOP_HANDSHAKE_MODE_WHEN_BW_NOT_ENOUGH_IN_120HZ_PANEL == TRUE)
        if(IsVBY1_16LANE(u8LPLL_Type))
        {
            //MVOP
            MS_BOOL bHSK = TRUE;
            MVOP_Handle stMvopHd = { E_MVOP_MODULE_MAIN };
            MDrv_MVOP_SetCommand(&stMvopHd, E_MVOP_CMD_SET_HANDSHAKE_MODE, &bHSK);
            //XC  0x1302_10 = 0x25
            SC_W2BYTEMSK(psXCInstPri->u32DeviceID,REG_SC_BK02_10_L, 0x0025, 0xFFFF);
        }
#endif
#endif
    }
    else
    {
#if (defined(USE_MVOP_VS_TO_GEN_VS_TO_OPVS) && USE_MVOP_VS_TO_GEN_VS_TO_OPVS == TRUE)
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID,REG_SC_BK02_10_L, 0, BIT(2));
#endif

#if (defined(PATCH_ENABLE_MVOP_HANDSHAKE_MODE_WHEN_BW_NOT_ENOUGH_IN_120HZ_PANEL))
#if (PATCH_ENABLE_MVOP_HANDSHAKE_MODE_WHEN_BW_NOT_ENOUGH_IN_120HZ_PANEL == TRUE)
        //MVOP
        MS_BOOL bHSK = FALSE;
        MVOP_Handle stMvopHd = { E_MVOP_MODULE_MAIN };
        MDrv_MVOP_SetCommand(&stMvopHd, E_MVOP_CMD_SET_HANDSHAKE_MODE, &bHSK);
        //XC  0x1302_10 = 0x00
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID,REG_SC_BK02_10_L, 0x0000, 0xFFFF);
#endif
#endif
    }

#if (HW_DESIGN_4K2K_VER == 6)
    if(IsSrcTypeDTV(gSrcInfo[MAIN_WINDOW].enInputSourceType) || (IsSrcTypeStorage(gSrcInfo[MAIN_WINDOW].enInputSourceType)))//MVOP source
    {
        if(SC_R2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK02_10_L, BIT(2)) == 0)
        {
            SC_W2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK10_5D_L, 0x1018);//return to HW default value
        }
    }
#endif

#if (HW_DESIGN_4K2K_VER == 7)  //Kano Patch
    if((psXCInstPri->u32DeviceID==0) && (eWindow == MAIN_WINDOW))
    {
        //for 60in/24out tearing issue, HW says XC FRC case need enable BK11_05[9]. but need more test to confirm this solution is OK.
        //BK11_05[9] use to protect write too fast.
        if(((gSrcInfo[eWindow].enInputSourceType)==INPUT_SOURCE_STORAGE) || ((gSrcInfo[eWindow].enInputSourceType)==INPUT_SOURCE_STORAGE2)
            || ((gSrcInfo[eWindow].enInputSourceType)==INPUT_SOURCE_HDMI))
        {
            SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK11_05_L, BIT(9) , BIT(9));
        }
        else
        {
            SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK11_05_L, 0 , BIT(9));
        }
    }
#endif
    if (_MDrv_XC_SVP_MirrorModeDetection(pInstance,eWindow))
    {
        memset(&st_xc_handler,0,sizeof(XC_OPTEE_HANDLER));
        st_xc_handler.eWindow = eWindow;
        MDrv_XC_OPTEE_Update(pInstance, E_XC_OPTEE_UPDATE_HANDLER, &st_xc_handler);
    }

#ifdef UFO_XC_SUPPORT_DUAL_MIU
    //display window w < 256, disable dual MIU
    if(bTempEnableDualMIU)
    {
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
        gSrcInfo[eWindow].Status2.stMiuDualModCfg.bEnableMiuDualMod = bTempEnableDualMIU;
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    }
#endif
    //XC fix FIFO bug function
    if((!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))&&(Hal_SC_CheckMuteStatusByRegister(pInstance,eWindow) == TRUE))
    {
        if(MHal_XC_FIFO_Control(pInstance,(gSrcInfo[eWindow].enInputSourceType),eWindow) == FALSE)
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"MHal_XC_FIFO_Control Fail\n");
        }
    }

    if(HSD_IP2_RESET_PATCH == TRUE)
    {
        if(!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow) && MDrv_SC_CheckMuteStatusByRegister(pInstance, eWindow))
        {
            if(gSrcInfo[eWindow].bPreH_ScalingDown == TRUE)
            {
                //ip2 hsd sw patch
                if(MHal_XC_Get_IP2HSDPatchStatus() == FALSE)
                {
                    MHal_XC_IP2HSDPatch(pInstance, TRUE);
                }
            }
        }
    }

#ifdef CONFIG_UTOPIA_PROC_DBG_SUPPORT
    u32SetWindowTime = MsOS_GetSystemTime()-u32SetWindowTime;
    gSrcInfo[MAIN_WINDOW].Status2.u32SetWindowTime = u32SetWindowTime;
#endif
    return bRet;
}


//-------------------------------------------------------------------------------------------------
/// Set the DIP window
/// @param  pstXC_SetWin_Info      \b IN: the information of the window setting
/// @param  u32InitDataLen         \b IN: the length of the pstXC_SetWin_Info
/// @param  eWindow                \b IN: which window we are going to set
/// @return @ref MS_BOOL
//-------------------------------------------------------------------------------------------------

static MS_BOOL _MApi_XC_SetDIPWindow(void* pInstance, XC_SETWIN_INFO *pstXC_SetWin_Info, MS_U32 u32InitDataLen, SCALER_WIN enWindow)
{
    MS_BOOL bRet = TRUE;
    if(MHal_XC_Is_Support_DIP_Sub_Path())
    {
        if(enWindow == SUB_WINDOW)
        {
            MS_BOOL bEnableDipSub= FALSE;
            XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
            UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
            XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
            MS_PHY u32SubFBAddr = 0;
            MS_U32 u32GetMMASize =0;
            _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);

            UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
#if (XC_SUPPORT_IOMMU == TRUE)
            if(pXCResourcePrivate->stdrvXC_MVideo_Context.stMMAfeature.u32Support == MSOS_MMA_SUPPORT)
            {
                if (pXCResourcePrivate->sthal_Optee.op_tee_xc[enWindow].isEnable == FALSE)
                {
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"MMA: MMA XC Alloc Start.\n");
                    if (_MDrv_XC_GetMemoryFromMMA(pInstance,enWindow) == FALSE)
                    {
                        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"%s,%d,Get XC MMA memory fail\n",__FUNCTION__,__LINE__);
                    }
                    else
                    {
                        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"MMA: MMA XC Alloc Done.\n");
                    }
                }
                else
                {
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"%s,%d sub window don't support svp path!\n",__FUNCTION__,__LINE__);
                    //MHal_XC_AuthorizeMMA(pInstance, eWindow);
                }

            }

            u32SubFBAddr = pXCResourcePrivate->stdrvXC_MVideo.aphyXCMMABaseAddress[enWindow];
            u32GetMMASize = pXCResourcePrivate->stdrvXC_MVideo.au32PreMMAXCSize[enWindow];
#else
            u32SubFBAddr = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FB_Start_Addr;
            u32GetMMASize = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FB_Size;
#endif
            MHal_FRC_DisableFRCOPMWrite(pInstance, FALSE, SUB_WINDOW);
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

            if(u32SubFBAddr == 0)
            {
                printf("%s Sub get memory fail\n",__FUNCTION__);
                return FALSE;
            }

            ST_DIP_CAPS_DI_ENGINE* pstDIPCapsBusSize = NULL;
            pstDIPCapsBusSize = (ST_DIP_CAPS_DI_ENGINE*)utopia_malloc(sizeof(ST_DIP_CAPS_DI_ENGINE));
            if(pstDIPCapsBusSize == NULL)
            {
                printf("%s ST_DIP_CAPS_BUS_SIZE alloc fail\n",__FUNCTION__);
                return FALSE;
            }
            pstDIPCapsBusSize->u32Version = ST_DIP_CAPS_DI_ENGINE_VERSION;
            pstDIPCapsBusSize->u32Length = sizeof(ST_DIP_CAPS_DI_ENGINE);
#if 0
            // READ ME:for diff PIP or APVR,PIP need set OUTPUT_SCALER_DIP
            MS_U8 i = 0;
            for ( i = 0 ; i < MAX_DATA_PATH_SUPPORTED; i++)
            {
                if ( pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].dest == OUTPUT_SCALER_DIP &&
                     (pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].u8PathStatus & DATA_PATH_USING)
                   )
                {
                    bEnableDipSub = TRUE;
                }
            }
#endif
            bEnableDipSub = TRUE;//patch
            if(bEnableDipSub)
            {
                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"%s %d u32SubFBAddr=%llx u32GetMMASize =%tu \n",__FUNCTION__,__LINE__,u32SubFBAddr,u32GetMMASize);
                if(gSrcInfo[enWindow].Status2.bDIPOpened == FALSE)
                {
                    if(E_APIXC_RET_OK == MApi_XC_DIP_GetResource(DWIN0_WINDOW))
                    {
                        gSrcInfo[enWindow].Status2.bDIPOpened = TRUE;
                    }
                }

                if(gSrcInfo[enWindow].Status2.bDIPOpened)
                {
                    MApi_XC_DIP_InitByDIP(DWIN0_WINDOW);
                    EN_XC_DIP_DATA_FMT enDIPOutColorFormat = DIP_DATA_FMT_YUV422;
                    MApi_XC_DIP_SetOutputDataFmt(enDIPOutColorFormat, DWIN0_WINDOW);
                    MApi_XC_DIP_GetChipCaps(E_DIP_CAPS_DI_ENGINE,pstDIPCapsBusSize, DWIN0_WINDOW);
                    if(pstDIPCapsBusSize->bDIEngineSupport && (u32GetMMASize >= XC_CMA_24MB))
                    {
                        ST_DIP_Buffer stBuffer;
                        memset(&stBuffer, 0, sizeof(ST_DIP_Buffer));
                        stBuffer.enBufType = E_DIP_DI_DNR_BUFFER;
                        stBuffer.phyBufAddr = u32SubFBAddr+XC_CMA_12MB;
                        stBuffer.u32BufSize = XC_CMA_12MB;
                        _MApi_XC_DIP_SetBuffer(&stBuffer, DWIN0_WINDOW);

                        ST_DIP_SET_PROCESS_MODE_INFO stDISsttings;
                        memset(&stDISsttings, 0, sizeof(ST_DIP_SET_PROCESS_MODE_INFO));
                        stDISsttings.eWindow = DWIN0_WINDOW;
                        if(pstXC_SetWin_Info->bInterlace)
                        {
                            stDISsttings.enMode = EN_DIP_MODE_DI;
                            _MApi_XC_DIP_ProcessMode(DWIN0_WINDOW, EN_DIP_MODE_DI, (void *)&stDISsttings);
                        }
                        else
                        {
                            stDISsttings.enMode = EN_DIP_MODE_PDNR;
                            _MApi_XC_DIP_ProcessMode(DWIN0_WINDOW, EN_DIP_MODE_PDNR, (void *)&stDISsttings);
                        }
                    }
                    ST_DIP_SET_SOURCE_INFO pstSetSrcInfo;
                    memset(&pstSetSrcInfo, 0, sizeof(ST_DIP_SET_SOURCE_INFO));

                    if((pstXC_SetWin_Info->enInputSourceType>=INPUT_SOURCE_TV)&&(pstXC_SetWin_Info->enInputSourceType<INPUT_SOURCE_CVBS_MAX) )
                    {
                        pstSetSrcInfo.enSource = SCALER_DIP_SOURCE_TYPE_VD;
                    }
                    else if((pstXC_SetWin_Info->enInputSourceType>=INPUT_SOURCE_HDMI)&&(pstXC_SetWin_Info->enInputSourceType<INPUT_SOURCE_HDMI_MAX) )
                    {
                        pstSetSrcInfo.enSource = SCALER_DIP_SOURCE_TYPE_HDMI;
                    }
                    else if((pstXC_SetWin_Info->enInputSourceType>=INPUT_SOURCE_DTV)&&(pstXC_SetWin_Info->enInputSourceType<=INPUT_SOURCE_DTV3) )
                    {
                        pstSetSrcInfo.enSource = SCALER_DIP_SOURCE_TYPE_MVOP1;
                    }
                    else if((pstXC_SetWin_Info->enInputSourceType>=INPUT_SOURCE_YPBPR)&&(pstXC_SetWin_Info->enInputSourceType<INPUT_SOURCE_YPBPR_MAX) )
                    {
                        pstSetSrcInfo.enSource = SCALER_DIP_SOURCE_TYPE_COMPONENT;
                    }
                    else
                    {
                        printf("DIP not support!");
                    }
                    pstSetSrcInfo.u16Width = pstXC_SetWin_Info->stCapWin.width;
                    pstSetSrcInfo.u16Height = pstXC_SetWin_Info->stCapWin.height;
                    if(E_APIXC_RET_OK !=MApi_XC_DIP_SetSourceInfo(&pstSetSrcInfo,DWIN0_WINDOW))
                    {
                        printf("MApi_XC_DIP_SetSourceInfo fail !");
                    }
                    if(pstSetSrcInfo.enSource == SCALER_DIP_SOURCE_TYPE_HDMI)
                    {
                        if((MS_HDMI_COLOR_RGB == MDrv_HDMI_Get_ColorFormat()) && (enDIPOutColorFormat == DIP_DATA_FMT_YUV422))
                        {
                            MApi_XC_DIP_EnableR2Y(TRUE, DWIN0_WINDOW);
                        }
                        else
                        {
                            MApi_XC_DIP_EnableR2Y(FALSE, DWIN0_WINDOW);
                        }
                    }
                    else
                    {
                        MApi_XC_DIP_EnableR2Y(FALSE, DWIN0_WINDOW);
                    }

                    ST_XC_DIP_WINPROPERTY pstDIPWinProperty;
                    pstDIPWinProperty.enSource = pstSetSrcInfo.enSource;
                    // Sub display can only support FHD,if size >FHD,need set to FHD
                    if((pstXC_SetWin_Info->stDispWin.width > 1920) || (pstXC_SetWin_Info->stDispWin.height > 1080))
                    {
                        pstDIPWinProperty.u16Width = 1920;
                        pstDIPWinProperty.u16Height= 1080;
                        pstXC_SetWin_Info->stDispWin.width = 1920;
                        pstXC_SetWin_Info->stDispWin.height = 1080;
                    }
                    else
                    {

                        pstDIPWinProperty.u16Width = pstXC_SetWin_Info->stDispWin.width;
                        pstDIPWinProperty.u16Height= pstXC_SetWin_Info->stDispWin.height;
                    }

                    pstDIPWinProperty.u8BufCnt= 3;
                    pstDIPWinProperty.u16Pitch= pstDIPWinProperty.u16Width *2; //RGB888 *4
                    pstDIPWinProperty.u32BufStart = u32SubFBAddr;
                    pstDIPWinProperty.u32BufEnd = pstDIPWinProperty.u32BufStart + XC_CMA_12MB;
                    pstXC_SetWin_Info->bPreHCusScaling = 1;
                    pstXC_SetWin_Info->bPreVCusScaling = 1;
                    pstXC_SetWin_Info->u16PreHCusScalingSrc = pstSetSrcInfo.u16Width;
                    pstXC_SetWin_Info->u16PreVCusScalingSrc = pstSetSrcInfo.u16Height;
                    pstXC_SetWin_Info->u16PreHCusScalingDst = pstDIPWinProperty.u16Width;
                    pstXC_SetWin_Info->u16PreVCusScalingDst = pstDIPWinProperty.u16Height;
                    pstXC_SetWin_Info->bHCusScaling= 1;
                    pstXC_SetWin_Info->bVCusScaling = 1;
                    pstXC_SetWin_Info->u16HCusScalingSrc = pstSetSrcInfo.u16Width;
                    pstXC_SetWin_Info->u16VCusScalingSrc = pstSetSrcInfo.u16Height;
                    pstXC_SetWin_Info->u16HCusScalingDst = pstDIPWinProperty.u16Width;
                    pstXC_SetWin_Info->u16VCusScalingDst = pstDIPWinProperty.u16Height;

                    if(E_APIXC_RET_OK !=MApi_XC_DIP_SetDIPWinProperty(&pstDIPWinProperty, DWIN0_WINDOW))
                    {
                        printf("MApi_XC_DIP_SetDIPWinProperty fail !");
                    }
                    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
#if (XC_SUPPORT_FASTSWITCH_BY_IP1GP==TRUE)
                    // capture window
                    if(!(IsSrcTypeStorage(pstXC_SetWin_Info->enInputSourceType) || IsSrcTypeDTV(pstXC_SetWin_Info->enInputSourceType)))
                    {
                        MS_U16 u16CapWidth = pstXC_SetWin_Info->stCapWin.width;
                        MHal_XC_SetOfflineSource(pInstance, MAX_WINDOW, pstXC_SetWin_Info->enInputSourceType);

                        if(MHal_XC_IsHDMI420(pInstance, pstXC_SetWin_Info->enInputSourceType) == TRUE)
                        {
                            u16CapWidth /= 2;
                        }

                        SC_W2BYTE(0, MHal_XC_FSS_BankTransfer(pInstance, REG_SC_BK01_04_L, MAX_WINDOW), pstXC_SetWin_Info->stCapWin.y); // Capture V start
                        SC_W2BYTE(0, MHal_XC_FSS_BankTransfer(pInstance, REG_SC_BK01_05_L, MAX_WINDOW), pstXC_SetWin_Info->stCapWin.x); // Capture H start
                        SC_W2BYTE(0, MHal_XC_FSS_BankTransfer(pInstance, REG_SC_BK01_06_L, MAX_WINDOW), pstXC_SetWin_Info->stCapWin.height);    // Capture V size
                        SC_W2BYTE(0, MHal_XC_FSS_BankTransfer(pInstance, REG_SC_BK01_07_L, MAX_WINDOW), u16CapWidth);    // Capture H size
                    }
#else
                    printf("%s DIP not support without fastswitch\n",__FUNCTION__);
                    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                    return FALSE;
#endif
                    _Mdrv_XC_CopySetWinInfo(pInstance, pstXC_SetWin_Info, enWindow);
                    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

                    pstXC_SetWin_Info->stCapWin.x = pstXC_SetWin_Info->stCropWin.x;
                    pstXC_SetWin_Info->stCapWin.y = pstXC_SetWin_Info->stCropWin.y;
                    pstXC_SetWin_Info->stCapWin.width = pstXC_SetWin_Info->stCropWin.width;
                    pstXC_SetWin_Info->stCapWin.height = pstXC_SetWin_Info->stCropWin.height;
                    // dump debug msg
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"============= SetWindow Start (Window : %u, XCsrc: %u,DIPsrc:%u) =============\n",enWindow, pstXC_SetWin_Info->enInputSourceType,pstSetSrcInfo.enSource);
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"CapWin  x: %4u      y: %4u      w: %4u    h: %4u \n",pstXC_SetWin_Info->stCapWin.x, pstXC_SetWin_Info->stCapWin.y, pstXC_SetWin_Info->stCapWin.width, pstXC_SetWin_Info->stCapWin.height);
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"CropWin x: %4u      y: %4u      w: %4u    h: %4u \n",pstXC_SetWin_Info->stCropWin.x, pstXC_SetWin_Info->stCropWin.y, pstXC_SetWin_Info->stCropWin.width, pstXC_SetWin_Info->stCropWin.height);
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"DispWin x: %4u      y: %4u      w: %4u    h: %4u \n",pstXC_SetWin_Info->stDispWin.x,pstXC_SetWin_Info->stDispWin.y, pstXC_SetWin_Info->stDispWin.width, pstXC_SetWin_Info->stDispWin.height);

                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"H/V total = (%u, %u), VFreq = %u\n", pstXC_SetWin_Info->u16DefaultHtotal, pstXC_SetWin_Info->u16InputVTotal, pstXC_SetWin_Info->u16InputVFreq);
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Pre  H cus scaling %u (%u -> %u)\n", pstXC_SetWin_Info->bPreHCusScaling, pstXC_SetWin_Info->u16PreHCusScalingSrc, pstXC_SetWin_Info->u16PreHCusScalingDst);
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Pre  V cus scaling %u (%u -> %u)\n", pstXC_SetWin_Info->bPreVCusScaling, pstXC_SetWin_Info->u16PreVCusScalingSrc, pstXC_SetWin_Info->u16PreVCusScalingDst);
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Post H cus scaling %u (%u -> %u)\n", pstXC_SetWin_Info->bHCusScaling, pstXC_SetWin_Info->u16HCusScalingSrc, pstXC_SetWin_Info->u16HCusScalingDst);
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Post V cus scaling %u (%u -> %u)\n", pstXC_SetWin_Info->bVCusScaling, pstXC_SetWin_Info->u16VCusScalingSrc, pstXC_SetWin_Info->u16VCusScalingDst);

                    if(E_APIXC_RET_OK !=MApi_XC_DIP_SetWindow(pstXC_SetWin_Info, u32InitDataLen, DWIN0_WINDOW))
                    {
                        printf("MApi_XC_DIP_SetWindow fail !");
                    }
                    if(s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_load_scalingtable != NULL)
                    {
                        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_load_scalingtable((PQ_WIN)enWindow,
                                            0,
                                            (pstXC_SetWin_Info->stCapWin.width != pstXC_SetWin_Info->stDispWin.width ? TRUE : FALSE),
                                            pstXC_SetWin_Info->bInterlace,
                                            TRUE,
                                            pstXC_SetWin_Info->stCapWin.width,
                                            pstXC_SetWin_Info->stDispWin.width);
                    }

                    if(E_APIXC_RET_OK !=MApi_XC_DIP_SetDIPWinProperty(&pstDIPWinProperty, DWIN0_WINDOW))
                    {
                        printf("MApi_XC_DIP_SetDIPWinProperty fail !");
                    }

                    if(E_APIXC_RET_OK !=MApi_XC_DIP_Ena(TRUE, DWIN0_WINDOW))
                    {
                        printf("MApi_XC_DIP_Ena fail !");
                    }

                    ST_DIP_DIPR_BLENDING_MODE_SETTINGS stDIPRBlending;
                    memset(&stDIPRBlending, 0, sizeof(ST_DIP_DIPR_BLENDING_MODE_SETTINGS));
                    stDIPRBlending.enDIPRSel = E_XC_DIPR0;
                    if(MHal_XC_Is_Support_HFR_Path())
                    {
                        stDIPRBlending.enDIPRDst = E_XC_BLENDING_HFR;
                    }
                    else
                    {
                        stDIPRBlending.enDIPRDst = E_XC_BLENDING_COMPOSER;
                    }

                    stDIPRBlending.bEnable = TRUE;
                    if(E_APIXC_RET_OK != _MApi_XC_DIP_ProcessMode(DWIN0_WINDOW, EN_DIP_MODE_DIPR_BLENDING,(void *)&stDIPRBlending))
                    {
                        printf("_MApi_XC_DIP_ProcessMode fail !");
                    }
                    MApi_XC_DIP_ReleaseResource(DWIN0_WINDOW);

                    MS_WINDOW_TYPE pdspwin;
                    memset(&pdspwin, 0, sizeof(MS_WINDOW_TYPE));
#ifdef STELLAR
                    if(stTCONPanelConfig.bUsingTCON)
                    {
                        PNL_ApiExtStatus sTCONPnlExtStatus;
                        sTCONPnlExtStatus.u16ApiStatusEX_Length = sizeof(PNL_ApiExtStatus);
                        MApi_PNL_GetStatusEx(&sTCONPnlExtStatus);
                        pdspwin.x += sTCONPnlExtStatus.u16DEHStart ;//pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HStart;
                        pdspwin.y += sTCONPnlExtStatus.u16DEVStart;//pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VStart;
                    }
                    else
#endif
                    {
                        pdspwin.x += pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HStart;
                        pdspwin.y += pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VStart;
                    }

                    pdspwin.x += pstXC_SetWin_Info->stDispWin.x;
                    pdspwin.y += pstXC_SetWin_Info->stDispWin.y;
                    pdspwin.width= pstXC_SetWin_Info->stDispWin.width;
                    pdspwin.height = pstXC_SetWin_Info->stDispWin.height;
                    _XC_ENTRY(pInstance);
                    MDrv_XC_set_dispwin_to_reg(pInstance,enWindow,&pdspwin);
                    MDrv_XC_EnableWindow(pInstance, TRUE,SUB_WINDOW);
                    _XC_RETURN(pInstance);
                }
                else
                {
                    printf("MApi_XC_DIP_GetResource fail !!!");
                    bRet = FALSE;
                }
            }
            else
            {
                MApi_XC_DIP_GetChipCaps(E_DIP_CAPS_DI_ENGINE,pstDIPCapsBusSize, DWIN0_WINDOW);
                if(pstDIPCapsBusSize->bDIEngineSupport)
                {
                    if(E_APIXC_RET_OK == MApi_XC_DIP_GetResource(DWIN0_WINDOW))
                    {
                        if(pstXC_SetWin_Info->bInterlace)
                        {
                            ST_DIP_Buffer stBuffer;
                            memset(&stBuffer, 0, sizeof(ST_DIP_Buffer));
                            stBuffer.enBufType = E_DIP_DI_DNR_BUFFER;
                            stBuffer.phyBufAddr = u32SubFBAddr;
                            stBuffer.u32BufSize = XC_CMA_12MB;
                            _MApi_XC_DIP_SetBuffer(&stBuffer, DWIN0_WINDOW);
                        }
                        ST_DIP_XCPathInfo pXCInfo;
                        memset(&pXCInfo, 0, sizeof(ST_DIP_XCPathInfo));
                        pXCInfo.enXCInputSrcType = pstXC_SetWin_Info->enInputSourceType;
                        if((pstXC_SetWin_Info->enInputSourceType>=INPUT_SOURCE_CVBS)&&(pstXC_SetWin_Info->enInputSourceType<INPUT_SOURCE_CVBS_MAX) )
                        {
                            pXCInfo.enDipCapSource = SCALER_DIP_SOURCE_TYPE_VD;
                        }
                        else if((pstXC_SetWin_Info->enInputSourceType>=INPUT_SOURCE_HDMI)&&(pstXC_SetWin_Info->enInputSourceType<INPUT_SOURCE_HDMI_MAX) )
                        {
                            pXCInfo.enDipCapSource = SCALER_DIP_SOURCE_TYPE_HDMI;
                        }
                        else if((pstXC_SetWin_Info->enInputSourceType>=INPUT_SOURCE_DTV)&&(pstXC_SetWin_Info->enInputSourceType<INPUT_SOURCE_DTV3) )
                        {
                            pXCInfo.enDipCapSource = SCALER_DIP_SOURCE_TYPE_MVOP1;
                        }
                        else
                        {
                            printf("DIP not support!");
                        }
                        pXCInfo.stXCCapWin = pstXC_SetWin_Info->stCapWin;
                        pXCInfo.bInterlace = pstXC_SetWin_Info->bInterlace;
                        pXCInfo.bPathEnabled = TRUE;
                        _MApi_XC_DIP_SetXCPathInfo(DWIN0_WINDOW, &pXCInfo);
                        MApi_XC_DIP_ReleaseResource(DWIN0_WINDOW);

                    }
                    else
                    {
                        printf("MApi_XC_DIP_GetResource fail !!!");
                        bRet = FALSE;
                    }
                }
            }
            if(pstDIPCapsBusSize != NULL)
            {
                utopia_free(pstDIPCapsBusSize);
            }
        }
    }
    return bRet;
}
//-------------------------------------------------------------------------------------------------
/// Set the specific window
/// @param  pstXC_SetWin_Info      \b IN: the information of the window setting
/// @param  u32InitDataLen         \b IN: the length of the pstXC_SetWin_Info
/// @param  eWindow                \b IN: which window we are going to set
/// @return @ref MS_BOOL
//-------------------------------------------------------------------------------------------------
MS_BOOL MApi_XC_SetWindow_U2(void* pInstance, XC_SETWIN_INFO *pstXC_SetWin_Info, MS_U32 u32InitDataLen, SCALER_WIN eWindow)
{
    MS_BOOL bRet = FALSE;
    MS_BOOL bEnable8LBMode = FALSE;
    XC_INITMISC stXC_Init_Misc;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    E_XC_3D_INPUT_MODE e3DInput = E_XC_3D_INPUT_MODE_NONE;
    E_XC_3D_OUTPUT_MODE e3DOutput = E_XC_3D_OUTPUT_MODE_NONE;
    if (pstXC_SetWin_Info == NULL)
    {
        printf("[%s,%5d]Invalid parameter !\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    //if only "bDispWinSet is True", means to "MApi_XC_SetDispWinToReg"
    if(pstXC_SetWin_Info->bSetDispWinToReg)
    {
        E_APIXC_ReturnValue enApiXcRet = E_APIXC_RET_FAIL;
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
        enApiXcRet = MDrv_XC_TransferDispWinMirror(pInstance, &pstXC_SetWin_Info->stDispWin, eWindow);
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        MApi_XC_SetDispWinToReg_U2(pInstance, &pstXC_SetWin_Info->stDispWin, eWindow);
        return (enApiXcRet == E_APIXC_RET_OK) ? TRUE : FALSE;
    }

    if (MHal_XC_Is_Support_DIP_Sub_Path())
    {
        if((eWindow == SUB_WINDOW) && (!MDrv_XC_GetMWEStatus(pInstance)))
        {
            _MApi_XC_SetDIPWindow(pInstance, pstXC_SetWin_Info, u32InitDataLen, eWindow);
            if(!MHal_XC_Is_Support_HFR_Path())
            {
                return TRUE;
            }
        }
    }

#ifdef MSOS_TYPE_LINUX_KERNEL
    /*
    ** Save setwindow info for str
    */
    void *pModule = NULL;
    UtopiaInstanceGetModule(pInstance, &pModule);
    XC_REGS_SAVE_AREA *pXCResourceStr = NULL;
    UtopiaModuleGetSTRPrivate(pModule, (void**)&pXCResourceStr);
    if (pstXC_SetWin_Info != NULL)
    {
        memcpy(&pXCResourceStr->pstXC_SetWin_Info[eWindow],pstXC_SetWin_Info,sizeof(XC_SETWIN_INFO));
    }
#endif
    _SETWINDOW_ENTRY();

#ifdef UFO_XC_DS_PQ
#if (UFO_XC_DS_PQ_VERSION==0)
    memset(&PSTXC_DS_CMDCNT, 0, sizeof(XC_DS_CMDCNT));
#endif
#else
    memset(&PSTXC_DS_CMDCNT, 0, sizeof(XC_DS_CMDCNT));
#endif

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

    if(MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
    {
#ifdef CHIP_ENABLE_DMS
        g_bSWDSGenScenario = FALSE;
#else
        g_bSWDSGenScenario = TRUE;
#endif
    }
    else
    {
        g_bSWDSGenScenario = FALSE;
    }

    if (_MApi_XC_SetWindow(pInstance, pstXC_SetWin_Info, sizeof(XC_SETWIN_INFO), FALSE, eWindow) == FALSE)
    {
        goto SETWINDOW_EXIT;
    }

    if ((SUPPORT_3D_DS == 0 && !g_bSWDSGenScenario)
        || SUPPORT_3D_DS == 1)
    {
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
        UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
        e3DInput = MDrv_XC_Get_3D_Input_Mode(pInstance, eWindow);
        e3DOutput = MDrv_XC_Get_3D_Output_Mode(pInstance);
        if(e3DInput == E_XC_3D_INPUT_MODE_NONE)
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Do nothing\n");
        }
        if(e3DOutput == E_XC_3D_OUTPUT_MODE_NONE)
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Do nothing\n");
        }
        MDrv_XC_3D_LoadReg(pInstance, e3DInput, e3DOutput, &gSrcInfo[eWindow], eWindow);
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
        if(!(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_B & E_XC_INIT_MISC_B_HDMITX_ENABLE)
            && pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFRCEnabled
            && !MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
        {
            if(pXCResourcePrivate->stdrvXC_3D._bFRC3DEnabled )//FRC 3D enabled
            {
                MDrv_XC_FRC_Set_Input3DFormat(pInstance, pXCResourcePrivate->stdrvXC_3D._eFrcInput3DMode);

                if(E_XC_3D_OUTPUT_FRAME_ALTERNATIVE == pXCResourcePrivate->stdrvXC_3D._eFrcOutput3DMode)//frc output FA
                {
                    Hal_SC_3D_Enable_FRC_LR_Flag(pInstance, TRUE); //use FRC LR flag
                }
                else
                {
                    Hal_SC_3D_Enable_FRC_LR_Flag(pInstance, FALSE); //use SC LR flag
                }

                if(MHal_FRC_IsEnableFRC3D(pInstance, MAIN_WINDOW))
                {
                    if(IS_OUTPUT_4K1K_120HZ_PANEL() || IS_OUTPUT_4K0_5K_240HZ_PANEL())
                    {
                        MDrv_XC_FRC_R2_Set_InputFrameSize(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width, \
                                    pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height*2);
                        MDrv_XC_FRC_R2_Set_OutputFrameSize(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width, \
                                    pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height);
                    }
                }
            }
            else
            {
                Hal_SC_3D_Enable_FRC_LR_Flag(pInstance, FALSE); //disable FRC LR  flag
                MDrv_XC_FRC_Set_Input3DFormat(pInstance, E_XC_3D_INPUT_MODE_NONE);
                MDrv_XC_FRC_R2_Set_InputFrameSize(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width, \
                            pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height);
                MDrv_XC_FRC_R2_Set_OutputFrameSize(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width, \
                            pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height);
            }
        }
#endif
    }

#if (SUPPORT_3D_DS == 1)
#if ((HW_DESIGN_4K2K_VER == 4)||(HW_DESIGN_4K2K_VER == 6))
#ifdef STELLAR
     if(MDrv_XC_Is_UpdateDsCmd(pInstance) && (MDrv_XC_GetDynamicScalingStatus(pInstance) || MDrv_XC_Is_DSForceIndexEnabled(pInstance, eWindow) ))
#else
    if(MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
#endif
    {
#if (defined (ANDROID)&& defined(UFO_XC_DS_PQ))
        MDrv_PQ_set_imode_PQDS(pstXC_SetWin_Info->bInterlace);
#endif
        MDrv_SWDS_AddCmd(pInstance,&pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg,eWindow);
#ifndef ENABLE_XC_WRITE_CMD_API
        MDrv_SWDS_Fire(pInstance,eWindow);
#endif
    }
#endif
#endif
    bRet = TRUE;

    //all return should come here, because this code will enable opm write limit addr protect
SETWINDOW_EXIT:
    if(g_bSWDSGenScenario)
    {
        g_bSWDSGenScenario = FALSE;
        _SETWINDOW_RETURN();
        return bRet;
    }

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    memcpy(&(gSrcInfo[eWindow].stStatusnodelay.stDispWin), &(gSrcInfo[eWindow].stDispWin), sizeof(MS_WINDOW_TYPE));

    if(eWindow == MAIN_WINDOW && !MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
    {
    #if (ENABLE_OPM_WRITE_SUPPORTED)
        MS_PHY u32OPWLimitBase = 0x00;
        u32OPWLimitBase = ((MDrv_XC_GetIPMBase(pInstance, 0, eWindow)*BYTE_PER_WORD + MDrv_XC_GetDNRBufSize(pInstance, eWindow)) / BYTE_PER_WORD - 1) ;
        _XC_ENTRY(pInstance);
        MDrv_SC_set_opm_write_limit(pInstance, TRUE, 0 , u32OPWLimitBase , eWindow);
        _XC_RETURN(pInstance);
#ifdef UFO_XC_SUPPORT_DUAL_MIU
        _XC_ENTRY(pInstance);
        u32OPWLimitBase = ((MDrv_XC_GetDualIPMBase(pInstance, 0, eWindow)*BYTE_PER_WORD + MDrv_XC_GetDualDNRBufSize(pInstance, eWindow)) / BYTE_PER_WORD - 1) ;
        MDrv_SC_set_dual_opm_write_limit(pInstance, FALSE, 0 , u32OPWLimitBase , eWindow);
        _XC_RETURN(pInstance);
#endif

    #endif
    }

//there is xc performance issue for napoli hw, so need enable 8 LB mode by designer's advice
#if ENABLE_DI_8LB_MODE
    bEnable8LBMode = ((MDrv_XC_Get_3D_Input_Mode(pInstance, eWindow) == E_XC_3D_INPUT_MODE_NONE)
                     && (MDrv_XC_Get_3D_Output_Mode(pInstance) == E_XC_3D_OUTPUT_MODE_NONE)
                     && MDrv_XC_Is2K2KToFrc(pInstance)
                     && (((gSrcInfo[MAIN_WINDOW].stCapWin.width == DOUBLEHD_1080X2P_HSIZE)
                             && (gSrcInfo[MAIN_WINDOW].stCapWin.height > (DOUBLEHD_1080X2P_VSIZE - DOUBLEHD_1080X2P_GARBAGE_VSIZE - 10))
                             && (!IsSrcTypeStorage(gSrcInfo[eWindow].enInputSourceType))) // MM case, enable UC costs too much BW, make video temp garbage, so not enable UC for MM
                         || (MApi_XC_Is_SubWindowEanble()//For dualview case: enable DI8LB to eliminate bubble for scaling performance issue.
                             && (gSrcInfo[SUB_WINDOW].stDispWin.height ==1080)&& (gSrcInfo[SUB_WINDOW].stDispWin.width ==1920))));
#endif

    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);


#if (HW_DESIGN_4K2K_VER == 6)
#ifdef MSOS_TYPE_LINUX_KERNEL
// For HW limitation, we need disable UC feature in Src * 1.5 > Dst(Post Horizontal Scaling)case   (OPW is 1P engine)
    if(((pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width == 3840) && (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height == 2160))&&
       (IsSrcTypeYPbPr(gSrcInfo[eWindow].enInputSourceType) || IsSrcTypeHDMI(gSrcInfo[eWindow].enInputSourceType) || IsSrcTypeDVI(gSrcInfo[eWindow].enInputSourceType) || IsSrcTypeDTV(gSrcInfo[eWindow].enInputSourceType))&&
       (gSrcInfo[eWindow].u16H_SizeAfterPreScaling * 15 >  gSrcInfo[eWindow].stDispWin.width * 10)&&(!MDrv_PQ_GetGameMode_Status(eWindow)))
    {
        if((MDrv_XC_MLoad_GetStatus(pInstance) == E_MLOAD_ENABLED) && (!Hal_SC_CheckMuteStatusByRegister(pInstance, MAIN_WINDOW)))
        {
            _MLOAD_ENTRY(pInstance);
            MDrv_XC_MLoad_WriteCmd(pInstance, REG_SC_BK2A_02_L, 0x0, BIT(15));
            MDrv_XC_MLoad_Fire(pInstance, TRUE);
            _MLOAD_RETURN(pInstance);
        }
        else
        {
            SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK2A_02_L, 0x0 ,BIT(15));//disable UC feature
        }
    }
    else
#endif
#endif
    {
#ifdef ENABLE_TV_SC2_PQ
            if (psXCInstPri->u32DeviceID == PQ_XC_ID_0)
#endif
            {
                if(s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_ioctl && !MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
                {
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
                    s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_ioctl((psXCInstPri->u32DeviceID == 1)?PQ_SUB_WINDOW:PQ_MAIN_WINDOW,
                                       E_PQ_IOCTL_SET_UCFEATURE,
                                       (void *)&bEnable8LBMode,
                                       sizeof(MS_BOOL));
#else
                    s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_ioctl((PQ_WIN)eWindow,
                                               E_PQ_IOCTL_SET_UCFEATURE,
                                               (void *)&bEnable8LBMode,
                                               sizeof(MS_BOOL));
#endif
                }
            }
#ifdef ENABLE_TV_SC2_PQ
            else if (psXCInstPri->u32DeviceID == PQ_XC_ID_1)
            {
                if(s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_ioctl_ex)
                {
                    s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_ioctl_ex(psXCInstPri->u32DeviceID, (PQ_WIN)eWindow,
                                               E_PQ_IOCTL_SET_UCFEATURE,
                                               (void *)&bEnable8LBMode,
                                               sizeof(MS_BOOL));
                }
            }
#endif
    }

#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
    // For Manhattan, FSC+FRC
    // 1. load FSC Qmap here
    // 2. load FRC 3D table here
    // 3. send R2 cmd to FRC
    // 4. control FRC clk
    // 5. control ODCLK2, composer, and TGEN lock_src
    if (eWindow == MAIN_WINDOW)
    {
        if ((IsFSCOrFRCEnable()) && (psXCInstPri->u32DeviceID != 1))
        {
            // for DS case, it would update MDE at DS cmd
            // for other case, if AP only call setwindow without setpaneltiming, need to update MDE here
            if(!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
            {
                static MS_BOOL bIsFhdToFsc = TRUE;
                if (bIsFhdToFsc == MHal_FRC_IsFHDToFSC(pInstance, eWindow))
                {
                    //commands move to mload
                    //MDrv_FRC_UpdateMDE(pInstance, gSrcInfo[MAIN_WINDOW].stDispWin);
                }
                else
                {
                    MS_BOOL bMLoad = (MDrv_XC_MLoad_GetStatus(pInstance) == E_MLOAD_ENABLED)?TRUE:FALSE;
                    if(bMLoad)
                    {
                        _MLOAD_ENTRY(pInstance);
                    }

                    MST_PANEL_INFO_t stFRCPanelInfo;
                    MDrv_FRC_PNLInfo_Transform(pInstance, &pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo,&pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc, &stFRCPanelInfo);
                    MDrv_FRC_Tx_SetTgen(pInstance, &stFRCPanelInfo);

                    MS_BOOL bLockFromIP = Hal_SC_IsLockFromIP(pInstance, eWindow);

                    //for Maserati 120Hz
                    MS_U8 u8LPLL_Type =(MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type;
                    if(IsVBY1_16LANE(u8LPLL_Type))
                    {
                        // case 5: FSC + FRC
                        if (MHal_FRC_IsFHDToFSC(pInstance, eWindow))
                        {
                            // FHD case, ODCLK2 as ODCLK/4
                            MHal_FRC_Set_STGEN_ODCLK(pInstance, E_FRC_STGEN_ODCLK_4, bMLoad);
                        }
                        else
                        {
                            // 4K 50/60 case, ODLCK2 as ODCLK
                            MHal_FRC_Set_STGEN_ODCLK(pInstance, E_FRC_STGEN_ODCLK_1, bMLoad);
                        }
                    }
                    else
                    {
                        if (MHal_FRC_IsFHDToFSC(pInstance, eWindow))
                        {
                            // FHD case, ODCLK2 as ODCLK/4
                            MHal_FRC_Set_STGEN_ODCLK(pInstance, E_FRC_STGEN_ODCLK_4, bMLoad);
                        }
                        else if ((gSrcInfo[eWindow].u16InputVFreq < 330) && bLockFromIP)
                        {
                            // FRC enable, sync from IP case
                            // 4K 24/25/30 case, ODCLK2 as ODCLK/2 for power saving
                            MHal_FRC_Set_STGEN_ODCLK(pInstance, E_FRC_STGEN_ODCLK_2, bMLoad);
                        }
                        else
                        {
                            // 4K 50/60 case, ODLCK2 as ODCLK
                            MHal_FRC_Set_STGEN_ODCLK(pInstance, E_FRC_STGEN_ODCLK_1, bMLoad);
                        }
                    }

                    if (MHal_FRC_IsFHDToFSC(pInstance, MAIN_WINDOW))
                    {
                        if (bMLoad == TRUE)
                        {
                            MDrv_XC_MLoad_WriteCommand_NonXC(pInstance, REG_FSC_BK23, 0x26, 0x1000, 0xFFFF);
                        }
                        else
                        {
                            MDrv_Write2ByteMask(REG_FSC_BK23_26, 0x1000, 0x1000);
                        }
                    }
                    else
                    {
                        if (bMLoad == TRUE)
                        {
                            MDrv_XC_MLoad_WriteCommand_NonXC(pInstance, REG_FSC_BK23, 0x26, 0x0000, 0xFFFF);
                        }
                        else
                        {
                            MDrv_Write2ByteMask(REG_FSC_BK23_26, 0x0000, 0x1000);
                        }
                    }

                    bIsFhdToFsc = MHal_FRC_IsFHDToFSC(pInstance, eWindow);

                    if(bMLoad)
                    {
                        MDrv_XC_MLoad_Fire(pInstance, TRUE);
                        _MLOAD_RETURN(pInstance);
                    }

                }

                MDrv_FRC_Set_3D_QMap(pInstance, E_FRC_PNL_TYPE_2D, E_XC_3D_INPUT_MODE_NONE, E_XC_3D_OUTPUT_MODE_NONE, E_XC_3D_OUTPUT_FI_MODE_NONE); // load FRC 3D table

                //update frc insert, crop
                MHal_FRC_Set_Insert_Crop(pInstance, MAIN_WINDOW);

                if(TRUE == MHal_FRC_IsSupportFRC_ME4XMode(pInstance))
                {
                    FRC_R2_CMD_PARAMETER_t stFRC_R2_Para;
                    memset(&stFRC_R2_Para, 0, sizeof(FRC_R2_CMD_PARAMETER_t));
                    if ((IsSrcTypeStorage(gSrcInfo[eWindow].enInputSourceType)) || (IsSrcTypeDTV(gSrcInfo[eWindow].enInputSourceType)))
                    {
                        //2X
                        stFRC_R2_Para.p1 = 1;
                        MHal_XC_SendCmdToFRC(pInstance, E_XC_FRC_MB_CMD_SET_MFC_ME2X_MODE, 1, stFRC_R2_Para.p1, stFRC_R2_Para.p2, stFRC_R2_Para.p3,
                        stFRC_R2_Para.p4, stFRC_R2_Para.p5, stFRC_R2_Para.p6, stFRC_R2_Para.p7, stFRC_R2_Para.p8);
                    }
                    else
                    {
                        //4X
                        stFRC_R2_Para.p1 = 0;
                        MHal_XC_SendCmdToFRC(pInstance, E_XC_FRC_MB_CMD_SET_MFC_ME2X_MODE, 1, stFRC_R2_Para.p1, stFRC_R2_Para.p2, stFRC_R2_Para.p3,
                        stFRC_R2_Para.p4, stFRC_R2_Para.p5, stFRC_R2_Para.p6, stFRC_R2_Para.p7, stFRC_R2_Para.p8);
                    }
                }

                //HFR mirror by FRC
                if(MHal_XC_Is_Enable_HFR_Path(pInstance, MAIN_WINDOW) && IsVMirrorMode(MAIN_WINDOW))
                {
                   MHal_FRC_SetMirrorControl(pInstance, TRUE);
                }
                else
                {
                   MHal_FRC_SetMirrorControl(pInstance, FALSE);
                }

                //patch to fix FRC getting stuck randomly
                MHal_FRC_Reset(pInstance);
            }
        }
    }
#endif

    if(!MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
    {
        if(pXCResourcePrivate->stdrvXC_MVideo.gu16HPixelShiftRange!=0 && pXCResourcePrivate->stdrvXC_MVideo.gu16VPixelShiftRange!=0)
        {
            MHal_XC_PixelShiftStatusChange(pInstance, EN_XC_PIXEL_SHIFT_FORCE_UPDATE);
        }
    }

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    Hal_SC_ControlMloadTrig(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
#if (HW_DESIGN_4K2K_VER == 7) //Kano Patch
    //[FIXME]If DS case, Kano need to disable REG_SC_BK00_03_L [12]
    //but MDrv_XC_3D_LoadReg will enable REG_SC_BK00_03_L [12]
    //so we disable it here temporary
    if(MDrv_XC_GetDynamicScalingStatus(pInstance))
    {
        SC_W2BYTEMSK(0, REG_SC_BK00_03_L, 0x0000, 0x1000);
    }
#endif

    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"============= SetWindow Done (Window : %u, src: %u) =============\n",eWindow, gSrcInfo[eWindow].enInputSourceType);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    _SETWINDOW_RETURN();

    return bRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_SetWindow(XC_SETWIN_INFO *pstXC_SetWin_Info, MS_U32 u32InitDataLen, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
        || pu32XCInst_1 == NULL
#endif
        )
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }
#if (defined (ANDROID))
    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)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }

    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)
        {
            printf("Obtain XC engine fail\n");
            return E_APIXC_RET_FAIL;
        }
    }
#else
    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)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }

    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 XCArgs2;
        XCArgs2.pXC_Init_Misc = &tmp_Init_Misc;
        XCArgs2.u32InitMiscDataLen = sizeof(XC_INITMISC);
        XCArgs2.eReturnValue = E_APIXC_RET_FAIL;

#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
        if(eWindow == SUB_WINDOW)
        {
            if(UtopiaIoctl(pu32XCInst_1, E_XC_CMD_INIT_MISC, (void*)&XCArgs2) != UTOPIA_STATUS_SUCCESS)
            {
                printf("Obtain XC engine fail\n");
                return E_APIXC_RET_FAIL;
            }
        }
        else
        {
            if(UtopiaIoctl(pu32XCInst, E_XC_CMD_INIT_MISC, (void*)&XCArgs2) != UTOPIA_STATUS_SUCCESS)
            {
                printf("Obtain XC engine fail\n");
                return E_APIXC_RET_FAIL;
            }
        }
#else
        if(UtopiaIoctl(pu32XCInst, E_XC_CMD_INIT_MISC, (void*)&XCArgs2) != UTOPIA_STATUS_SUCCESS)
        {
            printf("Obtain XC engine fail\n");
            return E_APIXC_RET_FAIL;
        }
#endif

    }
#endif
    stXC_SET_WINDOW XCArgs;
    XCArgs.pstXC_SetWin_Info = pstXC_SetWin_Info;
    XCArgs.u32InitDataLen = u32InitDataLen;
    XCArgs.eWindow = eWindow;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_WINDOW, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif
#ifndef DISABLE_3D_FUNCTION
static MS_BOOL _MDrv_XC_3D_LoadReg(void *pInstance, E_XC_3D_INPUT_MODE eInputMode,
                           E_XC_3D_OUTPUT_MODE eOutputMode,
                           XC_InternalStatus *pSrcInfo,
                           SCALER_WIN eWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    if (eWindow == MAIN_WINDOW)
    {
        memcpy(&pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg, &pXCResourcePrivate->stdrvXC_MVideo_Context.stMainDBreg, sizeof(SC_SWDB_INFO));
    }
    else
    {
        memcpy(&pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg, &pXCResourcePrivate->stdrvXC_MVideo_Context.stSubDBreg, sizeof(SC_SWDB_INFO));
    }
    return MDrv_XC_3D_LoadReg(pInstance, eInputMode, eOutputMode, pSrcInfo, eWindow);

}
#endif

MS_BOOL MApi_XC_SetDualWindow_U2(void* pInstance, XC_SETWIN_INFO *pstXC_SetWin_Info_Main, XC_SETWIN_INFO *pstXC_SetWin_Info_Sub)
{
    MS_BOOL bRet = FALSE;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    _SETWINDOW_ENTRY();

    if (_MApi_XC_SetWindow(pInstance, pstXC_SetWin_Info_Main, sizeof(XC_SETWIN_INFO), TRUE, MAIN_WINDOW) == FALSE)
    {
        goto SETWINDOW_EXIT;
    }

    if (_MApi_XC_SetWindow(pInstance, pstXC_SetWin_Info_Sub, sizeof(XC_SETWIN_INFO), TRUE, SUB_WINDOW) == FALSE)
    {
        goto SETWINDOW_EXIT;
    }
#ifndef DISABLE_3D_FUNCTION
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    _MDrv_XC_3D_LoadReg(pInstance, MDrv_XC_Get_3D_Input_Mode(pInstance, MAIN_WINDOW), MDrv_XC_Get_3D_Output_Mode(pInstance), &gSrcInfo[MAIN_WINDOW], MAIN_WINDOW);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
#endif
    bRet = TRUE;
    _SETWINDOW_RETURN();

    //all return should come here, because this code will enable opm write limit addr protect
SETWINDOW_EXIT:
    {
    #if (ENABLE_OPM_WRITE_SUPPORTED)
        MS_PHY u32OPWLimitBase = 0x00;
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
        u32OPWLimitBase = ((MDrv_XC_GetIPMBase(pInstance, 0, MAIN_WINDOW)*BYTE_PER_WORD + MDrv_XC_GetDNRBufSize(pInstance, MAIN_WINDOW)) / BYTE_PER_WORD - 1) ;
        _XC_ENTRY(pInstance);
        MDrv_SC_set_opm_write_limit(pInstance, TRUE, 0 , u32OPWLimitBase , MAIN_WINDOW);
        _XC_RETURN(pInstance);
#ifdef UFO_XC_SUPPORT_DUAL_MIU
        _XC_ENTRY(pInstance);
        if (IsEnableDualMode(MAIN_WINDOW))
        {
            u32OPWLimitBase = ((MDrv_XC_GetDualIPMBase(pInstance, 0, MAIN_WINDOW)*BYTE_PER_WORD + MDrv_XC_GetDualDNRBufSize(pInstance, MAIN_WINDOW)) / BYTE_PER_WORD - 1) ;
            MDrv_SC_set_dual_opm_write_limit(pInstance, TRUE, 0 , u32OPWLimitBase , MAIN_WINDOW);
        }
        _XC_RETURN(pInstance);
#endif
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    #endif
    }

    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"============= Set Dual Window Done =============\n");
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    if (bRet == FALSE)
    {
        _SETWINDOW_RETURN();
    }
    return bRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_SetDualWindow(XC_SETWIN_INFO *pstXC_SetWin_Info_Main, XC_SETWIN_INFO *pstXC_SetWin_Info_Sub)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    stXC_SET_DUALWINDOW XCArgs;
    XCArgs.pstXC_SetWin_Info_Main = pstXC_SetWin_Info_Main;
    XCArgs.pstXC_SetWin_Info_Sub = pstXC_SetWin_Info_Sub;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_DUALWINDOW, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif
#if FRC_INSIDE
void Mapi_XC_FRC_Set_User_MemoryFMT(void *pInstance, MS_U8 u8Mode, MS_BOOL bEnable)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    pXCResourcePrivate->stdrvXC_MVideo.bEnableUserMode = bEnable;
    pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u8IpMode = u8Mode;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}

MS_U8 Mapi_XC_FRC_Get_MemoryFMT(void *pInstance) //NO_USE
{
    MS_U8 u8Val = 0;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    u8Val = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u8IpMode;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return u8Val;
}

void Mapi_XC_FRC_Set_MemoryFMT(void *pInstance, E_XC_3D_INPUT_MODE e3dInputMode,
                                E_XC_3D_OUTPUT_MODE e3dOutputMode,
                                E_XC_3D_PANEL_TYPE e3dPanelType)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    if(pXCResourcePrivate->stdrvXC_MVideo.bEnableUserMode == FALSE)
    {
        // RGB and DVI
        if(0) //(IsSrcTypeVga(enInputSourceType) ||
           //(IsSrcTypeHDMI(enInputSourceType)&(!g_HdmiPollingStatus.bIsHDMIMode)))
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "### is RGB Space\n");
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u8IpMode = FRC_IP_MEM_IP_RGB_10_SPECIAL;
        }
        else
        {
            if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.bFRC)
            {
                if(e3dOutputMode == E_XC_3D_OUTPUT_FRAME_ALTERNATIVE)
                {
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"### E_XC_3D_OUTPUT_FRAME_ALTERNATIVE\n");
                    if((pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u83D_FI_out == E_XC_3D_OUTPUT_FI_960x1080)||
                       (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u83D_FI_out == E_XC_3D_OUTPUT_FI_1280x720))
                    {
                        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"### is YUV Space, 960x1080\n");
                        pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u8IpMode = FRC_IP_MEM_IP_YUV_10_SPECIAL;
                    }
                    //else if((g_XC_Pnl_Misc.FRCInfo.u83D_FI_out == E_XC_3D_OUTPUT_FI_1920x1080)||
                    //   (g_XC_Pnl_Misc.FRCInfo.u83D_FI_out == E_XC_3D_OUTPUT_FI_1920x540))
                    else
                    {
                        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"### is YC Space, 1920x1080/1920x540\n");
                        pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u8IpMode = FRC_IP_MEM_IP_YC_10_SPECIAL;
                    }
                }
                else if(e3dOutputMode == E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF)
                {
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"### E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF\n");
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"### is YUV Space, SBS 960x1080\n");
                    pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u8IpMode = FRC_IP_MEM_IP_YUV_10_SPECIAL;
                }
                else
                {
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"### is YC Space, others\n");
                    pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u8IpMode = FRC_IP_MEM_IP_YC_10_SPECIAL;
                }
            }
            else
            {
                pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u8IpMode = FRC_IP_MEM_IP_YC_10_SPECIAL;
                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"### is YC Space, bypass\n");
            }
        }
    }
    else
    {
        printf("\n\n@@Use User define for debug\n\n");
    }
#if 1
    if((e3dOutputMode == E_XC_3D_OUTPUT_FRAME_ALTERNATIVE)
        &&(e3dInputMode == E_XC_3D_INPUT_FRAME_PACKING)
        &&(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u83D_FI_out == E_XC_3D_OUTPUT_FI_1280x720))
    {
        pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u16PanelWidth = 1280;
        pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u16PanelHeigh= 720;
    }
    else
    {
        pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u16PanelWidth = 1920;
        if (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE_240HZ)
        {
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u16PanelHeigh = 540;
        }
        else
        {
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u16PanelHeigh = 1080;
        }
    }
#endif
    MDrv_FRC_SetMemFormat(pInstance, &pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo);

    if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.bFRC)
    {
        if((pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u83D_FI_out == E_XC_3D_OUTPUT_FI_960x1080)&&
           (e3dOutputMode == E_XC_3D_OUTPUT_FRAME_ALTERNATIVE))
        {
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u16FB_YcountLinePitch /= 2;
        }
        MDrv_FRC_OPM_SetBaseOfset(pInstance, &pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo, e3dOutputMode);
    }
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}

//-------------------------------------------------------------------------------------------------
/// set 3D convert format type
/// @param  e3dInputMode                \b IN: the input format
/// @param  e3dOutputMode               \b IN: the format we want to convert to
/// @param  e3dPanelType                \b IN: the panel type.
//-------------------------------------------------------------------------------------------------
void MApi_XC_FRC_Set_3D_Mode(void *pInstance, E_XC_3D_INPUT_MODE e3dInputMode,
                                       E_XC_3D_OUTPUT_MODE e3dOutputMode,
                                       E_XC_3D_PANEL_TYPE e3dPanelType)
{


    MS_U8 u8FRC_Pnl_type = E_FRC_PNL_TYPE_PASSIVE;
    MS_U8 u83D_FI_out = E_XC_3D_OUTPUT_FI_MODE_NONE;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    if (e3dPanelType == E_XC_3D_PANEL_SHUTTER)
    {
        if (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE_240HZ)
        {
            u8FRC_Pnl_type = E_FRC_PANEL_3D_TYPE_ACTIVE_240_1920_540;
        }
        else
        {
            u8FRC_Pnl_type = E_FRC_PNL_TYPE_ACTIVE;
        }
    }
    else if(e3dPanelType == E_XC_3D_PANEL_PELLICLE)
    {
        u8FRC_Pnl_type = E_FRC_PNL_TYPE_PASSIVE;
    }
    else
    {
        u8FRC_Pnl_type = E_FRC_PNL_TYPE_2D;
    }

    u83D_FI_out = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u83D_FI_out;

    switch(e3dInputMode)
    {
        case E_XC_3D_INPUT_MODE_NONE:
        case E_XC_3D_INPUT_NORMAL_2D_HW:
            MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, e3dInputMode, e3dOutputMode, u83D_FI_out);
        break;

        default:
        case E_XC_3D_INPUT_FRAME_PACKING:
            if(e3dOutputMode == E_XC_3D_OUTPUT_TOP_BOTTOM)
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_FRAME_PACKING, E_XC_3D_OUTPUT_TOP_BOTTOM, u83D_FI_out);
            }
            else if(e3dOutputMode == E_XC_3D_OUTPUT_FRAME_ALTERNATIVE)
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_FRAME_PACKING, E_XC_3D_OUTPUT_FRAME_ALTERNATIVE, u83D_FI_out);
            }
            else if(e3dOutputMode == E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF)
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_FRAME_PACKING, E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF, u83D_FI_out);
            }
            else if((e3dOutputMode == E_XC_3D_OUTPUT_FRAME_L)||(e3dOutputMode == E_XC_3D_OUTPUT_FRAME_R))
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_MODE_NONE, E_XC_3D_OUTPUT_MODE_NONE, E_XC_3D_OUTPUT_FI_1920x1080);
            }
            else
            {
                printf("\n##[%s]1. Please check the 3D input and out mode\n\n", __FUNCTION__);
            }
        break;
        case E_XC_3D_INPUT_SIDE_BY_SIDE_HALF:
            if(e3dOutputMode == E_XC_3D_OUTPUT_TOP_BOTTOM)
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_SIDE_BY_SIDE_HALF, E_XC_3D_OUTPUT_TOP_BOTTOM, u83D_FI_out);

            }
            else if(e3dOutputMode == E_XC_3D_OUTPUT_FRAME_ALTERNATIVE)
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_SIDE_BY_SIDE_HALF, E_XC_3D_OUTPUT_FRAME_ALTERNATIVE, u83D_FI_out);

            }
            else if(e3dOutputMode == E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF)
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_SIDE_BY_SIDE_HALF, E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF, u83D_FI_out);
            }
            else if((e3dOutputMode == E_XC_3D_OUTPUT_FRAME_L)||(e3dOutputMode == E_XC_3D_OUTPUT_FRAME_R))
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_MODE_NONE, E_XC_3D_OUTPUT_MODE_NONE, E_XC_3D_OUTPUT_FI_1920x1080);
            }
            else
            {
                printf("\n##[%s]2. Please check the 3D input and out mode\n\n", __FUNCTION__);
            }

        break;
        case E_XC_3D_INPUT_SIDE_BY_SIDE_FULL:
            if(e3dOutputMode == E_XC_3D_OUTPUT_TOP_BOTTOM)
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_SIDE_BY_SIDE_FULL, E_XC_3D_OUTPUT_TOP_BOTTOM, u83D_FI_out);

            }
            else if(e3dOutputMode == E_XC_3D_OUTPUT_FRAME_ALTERNATIVE)
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_SIDE_BY_SIDE_FULL, E_XC_3D_OUTPUT_FRAME_ALTERNATIVE, u83D_FI_out);
            }
            else if((e3dOutputMode == E_XC_3D_OUTPUT_FRAME_L)||(e3dOutputMode == E_XC_3D_OUTPUT_FRAME_R))
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_MODE_NONE, E_XC_3D_OUTPUT_MODE_NONE, E_XC_3D_OUTPUT_FI_1920x1080);
            }
            else
            {
                printf("\n##[%s]2. Please check the 3D input and out mode\n\n", __FUNCTION__);
            }
        break;
        case E_XC_3D_INPUT_TOP_BOTTOM:
            if(e3dOutputMode == E_XC_3D_OUTPUT_TOP_BOTTOM)
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_TOP_BOTTOM, E_XC_3D_OUTPUT_TOP_BOTTOM, u83D_FI_out);
            }
            else if(e3dOutputMode == E_XC_3D_OUTPUT_FRAME_ALTERNATIVE)
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_TOP_BOTTOM, E_XC_3D_OUTPUT_FRAME_ALTERNATIVE, u83D_FI_out);

            }
            else if(e3dOutputMode == E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF)
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_OUTPUT_TOP_BOTTOM, E_XC_3D_OUTPUT_SIDE_BY_SIDE_HALF, u83D_FI_out);

            }
            else if((e3dOutputMode == E_XC_3D_OUTPUT_FRAME_L)||(e3dOutputMode == E_XC_3D_OUTPUT_FRAME_R))
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_MODE_NONE, E_XC_3D_OUTPUT_MODE_NONE, E_XC_3D_OUTPUT_FI_1920x1080);
            }
            else
            {
                printf("\n##[%s]3. Please check the 3D input and out mode\n\n", __FUNCTION__);
            }
        break;
        case E_XC_3D_INPUT_FRAME_ALTERNATIVE:
            if(e3dOutputMode == E_XC_3D_OUTPUT_TOP_BOTTOM)
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_FRAME_ALTERNATIVE, E_XC_3D_OUTPUT_TOP_BOTTOM, u83D_FI_out);
            }
            else if(e3dOutputMode == E_XC_3D_OUTPUT_FRAME_ALTERNATIVE)
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_FRAME_ALTERNATIVE, E_XC_3D_OUTPUT_FRAME_ALTERNATIVE, u83D_FI_out);
            }
            else if((e3dOutputMode == E_XC_3D_OUTPUT_FRAME_L)||(e3dOutputMode == E_XC_3D_OUTPUT_FRAME_R))
            {
                MDrv_FRC_Set_3D_QMap(pInstance, u8FRC_Pnl_type, E_XC_3D_INPUT_MODE_NONE, E_XC_3D_OUTPUT_MODE_NONE, E_XC_3D_OUTPUT_FI_1920x1080);
            }
            else
            {
                printf("\n##[%s]1. Please check the 3D input and out mode\n\n", __FUNCTION__);
            }
        break;


    }

    switch(e3dOutputMode)
    {
        //for 2D to 3D use

        // ParaCnt=4, P0 = 2D to 3D ConversionMode(0=Disable, 1=Enable)
        //                  P1 = 3D Depth Gain(0~31)
        //                  P2 = 3D Depth Offset(0~127)
        //                  P3 = Artificial Gain(0~15)

        case E_XC_3D_OUTPUT_CHECKBOARD_HW:
        case E_XC_3D_OUTPUT_LINE_ALTERNATIVE_HW:
        case E_XC_3D_OUTPUT_PIXEL_ALTERNATIVE_HW:
        case E_XC_3D_OUTPUT_FRAME_L_HW:
        case E_XC_3D_OUTPUT_FRAME_R_HW:
            if(!Hal_XC_FRC_R2_Set_2DTo3DMode(1, 0x13, 0x69, 0x0E))
            {
                printf("Enable 2DTo3D Failed\n");
            }
        break;

        default:
            if(!Hal_XC_FRC_R2_Set_2DTo3DMode(0, 0x13, 0x69, 0x8))
            {
                printf("Disable 2DTo3D Failed\n");
            }
        break;
    }
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#endif
extern MS_U16 _MDrv_SC_GetOutputVPeriod(void *pInstance);
MS_BOOL MApi_XC_FRC_BypassMFC_U2(void* pInstance, MS_BOOL bEnable)
{
#if FRC_INSIDE
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    MS_U8 u8Mode = FRC_IP_MEM_IP_RGB_10_SPECIAL;
    E_XC_3D_INPUT_MODE e3dInputMode = E_XC_3D_INPUT_MODE_NONE;
    E_XC_3D_OUTPUT_MODE e3dOutputMode = E_XC_3D_OUTPUT_MODE_NONE;

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    E_XC_3D_PANEL_TYPE e3dPanelType = MDrv_XC_Get_3D_Panel_Type(pInstance);
    MS_U16 u16DelayCount = 0;

    FRC_R2_CMD_PARAMETER_t tFRC_R2_Para;

    memset(&tFRC_R2_Para, 0, sizeof(FRC_R2_CMD_PARAMETER_t));

    ///Current spec doesn't have bypass MFC in 3D mode, this is only for PC RGB bypass mode
    if((e3dInputMode != MDrv_XC_Get_3D_Input_Mode(pInstance, MAIN_WINDOW))||(e3dOutputMode != MDrv_XC_Get_3D_Output_Mode(pInstance)))
    {
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        return FALSE;
    }
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    // for Agate, Freeze memory
    W2BYTEMSK(L_FRC_SC134(0x07), BIT(0), BIT(0));

    if(bEnable)
    {
        printf("@@@ MFC Bypass Enable\n");
        printf("@@@1,Set MFC CMD \n");

        tFRC_R2_Para.p1=0x0F;
        tFRC_R2_Para.p2=0;
        tFRC_R2_Para.p3=0;

        MDrv_XC_SendCmdToFRC(pInstance, 0x20, 3, tFRC_R2_Para );
        MDrv_Write2ByteMask(0x3020C0, 0x00, BIT(0));  // IPM CSC oFF
        // for Agate
        MDrv_Write2ByteMask(0x302660, 0x00, BIT(8));  // data flow don't go through the MFC section
        MDrv_Write2ByteMask(0x302C80, 0x00, BIT(0)); // MFC Stage disable
        MDrv_Write2Byte(0x30290E, 0x00); // MFC disable
        W2BYTEMSK(L_FRC_SC134(0x07), BIT(1), BIT(1));
        //Disable 6R mode
        W2BYTEMSK(L_FRC_SC134(0x19), 0x00, BIT(8));


        MDrv_Write2ByteMask(0x303002, 0x00, BIT(3));   // OP2 CSC oFF
        u16DelayCount = 7;
        u8Mode = FRC_IP_MEM_IP_RGB_10_SPECIAL;
    }
    else
    {
        printf("@@@ MFC Bypass Disable\n");
        printf("@@@1,Set MFC CMD \n");

        tFRC_R2_Para.p1=0x02;
        tFRC_R2_Para.p2=0;
        tFRC_R2_Para.p3=0;

        MDrv_XC_SendCmdToFRC(pInstance, 0x20, 3, tFRC_R2_Para );

        MDrv_Write2ByteMask(0x3020C0, BIT(0), BIT(0));
        // for Agate
        MDrv_Write2ByteMask(0x302660, BIT(8), BIT(8));
        MDrv_Write2ByteMask(0x302C80, BIT(0), BIT(0));
        MDrv_Write2Byte(0x30290E, 0x1D2F);
        W2BYTEMSK(L_FRC_SC134(0x07), 0, BIT(1));
        //Enable 6R mode
        W2BYTEMSK(L_FRC_SC134(0x19), BIT(8), BIT(8));

        MDrv_Write2ByteMask(0x303002, BIT(3), BIT(3));
        u16DelayCount = 10;
        u8Mode = FRC_IP_MEM_IP_YUV_10_SPECIAL;
    }
    printf("@@@2,Load MFC table\n");
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    Mapi_XC_FRC_Set_User_MemoryFMT(pInstance, u8Mode, bEnable);
    MDrv_XC_FRC_SetWindow(pInstance, e3dInputMode, e3dOutputMode, e3dPanelType);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    // for Agate, need to wait the memory write finish
    u16DelayCount *= 20;//_MDrv_SC_GetOutputVPeriod(); //get delay time
    printf("Enable Delay=%u\n", u16DelayCount);
    MsOS_DelayTask(u16DelayCount);

    // for Agate, un Freeze memory
    W2BYTEMSK(L_FRC_SC134(0x07), 0x00, BIT(0));

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return TRUE;
#else
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return FALSE;
#endif

}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_FRC_BypassMFC(MS_BOOL bEnable)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    stXC_FRC_BYPASS XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_FRC_BYPASS, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif
void MApi_XC_FRC_Mute_U2(void* pInstance, MS_BOOL bEnable)
{
    MDrv_FRC_Mute(pInstance, bEnable);
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_FRC_Mute(MS_BOOL bEnable)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_FRC_MUTE XCArgs;
    XCArgs.bEnable = bEnable;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_FRC_MUTE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
void MDrv_XC_FRC_SetWindow(void *pInstance, E_XC_3D_INPUT_MODE e3dInputMode,
                           E_XC_3D_OUTPUT_MODE e3dOutputMode,
                           E_XC_3D_PANEL_TYPE e3dPanelType)
{
#if FRC_INSIDE
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    if (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.bFRC &&
        (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16DefaultHTotal> 3000) &&
        (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16DefaultVTotal> 2000) &&
        (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16DefaultVFreq > 500) &&  // 4K2K panel
        (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_FRC_INSIDE_4K1K_120HZ) &&
        (E_XC_3D_OUTPUT_TOP_BOTTOM == MDrv_XC_Get_3D_Output_Mode(pInstance)))
    {
        // For FRC inside chip, ex.Einstein/Napoli
        // 4K1K FA output case, FRC only can handle TB in to FA out
        // FRC input is TB, output is FA
        e3dInputMode = E_XC_3D_INPUT_TOP_BOTTOM;
        e3dOutputMode = E_XC_3D_OUTPUT_FRAME_ALTERNATIVE;
    }

    Mapi_XC_FRC_Set_MemoryFMT(pInstance, e3dInputMode, e3dOutputMode, e3dPanelType);
    MApi_XC_FRC_Set_3D_Mode(pInstance, e3dInputMode, e3dOutputMode, e3dPanelType);
    if( (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width == 3840)
        && (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height == 2160) )
    {
        printf("@@@ %s: 4k2k patch\n", __FUNCTION__);
        //FRC IPM/OPM
        MDrv_FRC_IPM_SetOffset(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width);
        MDrv_FRC_IPM_SetFetchNum(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width);
        MDrv_FRC_OPM_SetOffset(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width);
        MDrv_FRC_OPM_SetFetchNum(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width);
        //FRC OP HSU/VSU
        MDrv_FRC_HSU_SetScalingSize(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width,
                pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width);
        MDrv_FRC_VSU_SetScalingSize(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height,
                pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height);
        //force RGB 444
        MDrv_FRC_CSC_SelectPath(pInstance, E_FRC_COLOR_PATH_RGB_444);
    }
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
#endif
}

void MApi_XC_FRC_SetWindow_U2(void* pInstance,
                             E_XC_3D_INPUT_MODE e3dInputMode,
                             E_XC_3D_OUTPUT_MODE e3dOutputMode,
                             E_XC_3D_PANEL_TYPE e3dPanelType)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    MDrv_XC_FRC_SetWindow(pInstance, e3dInputMode, e3dOutputMode, e3dPanelType);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_FRC_SetWindow(E_XC_3D_INPUT_MODE e3dInputMode,
                           E_XC_3D_OUTPUT_MODE e3dOutputMode,
                           E_XC_3D_PANEL_TYPE e3dPanelType)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_FRC_WINDOW XCArgs;
    XCArgs.e3dInputMode = e3dInputMode;
    XCArgs.e3dOutputMode = e3dOutputMode;
    XCArgs.e3dPanelType = e3dPanelType;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_FRC_WINDOW, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Set report window
/// @param  bEnable        \b IN: enable or disable report window
/// @param  Window         \b IN: the window position and size
/// @param  u8Color        \b IN: report window's color
//-------------------------------------------------------------------------------------------------
void MApi_XC_SetRepWindow_U2(void* pInstance, MS_BOOL bEnable,MS_WINDOW_TYPE Window,MS_U8 u8Color)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    _XC_ENTRY(pInstance);
    MDrv_SC_rep_window(pInstance,bEnable,Window,u8Color);
    memcpy(&pXCResourcePrivate->stdrvXC_MVideo_Context.g_RepWindow, &Window, sizeof(MS_WINDOW_TYPE));
    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_SetRepWindow(MS_BOOL bEnable,MS_WINDOW_TYPE Window,MS_U8 u8Color)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_REPORT_WINDOW XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.Window = Window;
    XCArgs.u8Color = u8Color;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_REPORT_WINDOW, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// update display window registers with input window
/// @param  eWindow                          \b IN: Main or sub window
/// @param  pdspwin                          \b IN: window info that will be setted to registers
//-------------------------------------------------------------------------------------------------
void    MApi_XC_SetDispWinToReg_U2(void* pInstance, MS_WINDOW_TYPE *pstDspwin, SCALER_WIN eWindow)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    if (pstDspwin == NULL)
    {
        printf("[%s,%5d]Invalid parameter !\n",__FUNCTION__,__LINE__);
        return;
    }

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    _XC_ENTRY(pInstance);
    MDrv_XC_set_dispwin_to_reg(pInstance, eWindow, pstDspwin);
    memcpy(&gSrcInfo[eWindow].stDispWin, pstDspwin, sizeof(MS_WINDOW_TYPE));
#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
    if ((IsFSCOrFRCEnable()) && (psXCInstPri->u32DeviceID != 1))
    {
        MDrv_FRC_UpdateMDE(pInstance, gSrcInfo[eWindow].stDispWin);
    }
#endif
    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void    MApi_XC_SetDispWinToReg(MS_WINDOW_TYPE *pstDspwin, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_DISPLAY_WIN_TO_REG XCArgs;
    XCArgs.pstDspwin = pstDspwin;
    XCArgs.eWindow = eWindow;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_DISPLAY_WIN_TO_REG, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// get current display window registers setting
/// @param  eWindow                          \b IN : Main or sub window
/// @param  pdspwin                          \b OUT: Pointer for ouput disp window register
//-------------------------------------------------------------------------------------------------
void    MApi_XC_GetDispWinFromReg_U2(void* pInstance, MS_WINDOW_TYPE *pstDspwin, SCALER_WIN eWindow)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    _XC_ENTRY(pInstance);
    MDrv_XC_get_dispwin_from_reg(pInstance, eWindow, pstDspwin);
    _XC_RETURN(pInstance);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void    MApi_XC_GetDispWinFromReg(MS_WINDOW_TYPE *pstDspwin, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }
    static MS_WINDOW_TYPE tmp;
    memcpy(&tmp,pstDspwin,sizeof(MS_WINDOW_TYPE));
    stXC_GET_DISPLAY_WIN_FROM_REG XCArgs;
    XCArgs.pstDspwin = &tmp;
    XCArgs.eWindow = eWindow;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_DISPLAY_WIN_FROM_REG, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        memcpy(pstDspwin,&tmp,sizeof(MS_WINDOW_TYPE));
        return;
    }
}
#endif
/// MUX API
///////////////////////////////////////////////////

/**
 * InputSource Mux is a controller which handles all of the video path.
 * Before using video source, user have to create a data path between source and sink.
 * For example, DTV -> MainWindow or DTV -> SubWindow.
 */

/////////////////////////////////////////
// Configuration
/////////////////////////////////////////


/********************************************************************************/
/*                 Static Variable and Functions (Private)                      */
/********************************************************************************/

/********************************************************************************/
/*                 Functions                                                    */
/********************************************************************************/

//////////////////////////////////////
//    Data Query Functions
//////////////////////////////////////
void MApi_XC_Mux_GetPortMappingMatrix_U2(void *pInstance,XC_MUX_INPUTSRCTABLE* mapping_tab,MS_U32 length)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    memcpy(pXCResourcePrivate->stdrvXC_MVideo.mapping_tab,mapping_tab,length);
    pXCResourcePrivate->stdrvXC_MVideo.matrix_length = (length / sizeof(XC_MUX_INPUTSRCTABLE));

    if(pXCResourcePrivate->stdrvXC_MVideo.bModuleInited == TRUE)
    {
        if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_B & E_XC_INIT_MISC_B_DISABLE_FSS)
        {
            MDrv_XC_SetFSSEnable(pInstance, DISABLE, MAIN_WINDOW);
        }
        else
        {
            MDrv_XC_SetFSSEnable(pInstance, ENABLE, MAIN_WINDOW);
        }
    }

    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
}

#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_Mux_GetPortMappingMatrix(XC_MUX_INPUTSRCTABLE mapping_tab[50],MS_U32 length)
{
    if (pu32XCInst == NULL)
    {
        if(UtopiaOpen(MODULE_XC, &pu32XCInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return;
        }
    }
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
     if (pu32XCInst_1 == NULL)
     {
        XC_INSTANT_ATTRIBUTE stXCInstantAttribute;
        INIT_XC_INSTANT_ATTR(stXCInstantAttribute);

        stXCInstantAttribute.u32DeviceID = 1;
        if(UtopiaOpen(MODULE_XC, &pu32XCInst_1, 0, &stXCInstantAttribute) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return ;
        }
      }
#endif

    stXC_GET_MUX_MAPPINGTAB XCArgs;
    XCArgs.mapping_tab = mapping_tab;
    XCArgs.length = length;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_MUX_MAPPINGTAB, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Get input port from input source type
/// @param  src                 \b IN: the query based on the input source type
/// @param  port_ids            \b OUT: the port ID we get
/// @param  u8port_count        \b OUT: how many ports we get
//-------------------------------------------------------------------------------------------------
void MDrv_XC_Mux_GetPort(void *pInstance, INPUT_SOURCE_TYPE_t src, E_MUX_INPUTPORT* port_ids , MS_U8* u8port_count )
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    * u8port_count = pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[src].u32EnablePort ;
    port_ids[0] = (E_MUX_INPUTPORT)pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[src].u32Port[0];
    port_ids[1] = (E_MUX_INPUTPORT)pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[src].u32Port[1];
}

//-------------------------------------------------------------------------------------------------
/// Get input port from input HDMI source type
/// @param  src                 \b IN: the query based on the input HDMI type
/// @return @ref E_MUX_INPUTPORT
//-------------------------------------------------------------------------------------------------
E_MUX_INPUTPORT MApi_XC_Mux_GetHDMIPort_U2( void* pInstance, INPUT_SOURCE_TYPE_t src )
{
    E_MUX_INPUTPORT ePort = INPUT_PORT_NONE_PORT;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    ePort = MDrv_XC_Mux_GetHDMIPort(pInstance, src);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return ePort;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_MUX_INPUTPORT MApi_XC_Mux_GetHDMIPort( INPUT_SOURCE_TYPE_t src )
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return INPUT_PORT_NONE_PORT;
    }

    stXC_GET_MUX_HDMIPORT XCArgs;
    XCArgs.src = src;
    XCArgs.eReturnValue = INPUT_PORT_NONE_PORT;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_MUX_HDMIPORT, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return INPUT_PORT_NONE_PORT;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Set Support MHL Path Info
/// @param u8MhlSupportInfo               \b IN:  MHL Support Info
///@return @ref MS_BOOL True if query success.
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_Mux_SetSupportMhlPathInfo_U2(void* pInstance, MS_U8 u8MhlSupportInfo)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    pXCResourcePrivate->stdrvXC_MVideo_Context._u8MhlSupportInfo = u8MhlSupportInfo;
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return E_APIXC_RET_OK;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_Mux_SetSupportMhlPathInfo(MS_U8 u8MhlSupportInfo)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_MUX_SUPPORT_MHL_PATHINFO XCArgs;
    XCArgs.u8MhlSupportInfo = u8MhlSupportInfo;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_MUX_SUPPORT_MHL_PATHINFO, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Set MHL Hot Plug Inverse Info
/// @param bIsMhlHotPlugInverse               \b IN:  MHL Hot Plug Inverse Info
///@return @ref MS_BOOL True if query success.
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_Mux_SetMhlHotPlugInverseInfo_U2(void* pInstance, MS_BOOL bIsMhlHotPlugInverse)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    pXCResourcePrivate->stdrvXC_MVideo_Context.g_bIsMhlHotPlugInverse = bIsMhlHotPlugInverse;
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return E_APIXC_RET_OK;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_Mux_SetMhlHotPlugInverseInfo(MS_BOOL bIsMhlHotPlugInverse)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_MUX_MHL_HOTPLUG_INVERSE_INFO XCArgs;
    XCArgs.bIsMhlHotPlugInverse = bIsMhlHotPlugInverse;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_MUX_MHL_HOTPLUG_INVERSE_INFO, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//////////////////////////////////////
//    Set path & power
//////////////////////////////////////

static void _MApi_XC_Mux_SetPath(void *pInstance, INPUT_SOURCE_TYPE_t enInputSourceType, E_DEST_TYPE enOutputType)
{
    MS_U8 u8Port_count = 0;
    E_MUX_INPUTPORT enPorts[3] = {INPUT_PORT_NONE_PORT, INPUT_PORT_NONE_PORT, INPUT_PORT_NONE_PORT};
    MS_U8 MS_U8index = 0 ;
    MS_U8 dest = (MS_U8)enOutputType;

    MS_U16 u16InputSrc = INPUT_SOURCE_NONE;

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    // Get source ports.

    MDrv_XC_Mux_GetPort(pInstance, enInputSourceType , enPorts , &u8Port_count );

    if((enInputSourceType>=INPUT_SOURCE_HDMI)&&(enInputSourceType<=INPUT_SOURCE_HDMI4))
    {
        u16InputSrc = enInputSourceType - INPUT_SOURCE_HDMI;
        pXCResourcePrivate->u16HDMI_PortInfo[u16InputSrc] = enPorts[0];
    }
    else if((enInputSourceType>=INPUT_SOURCE_DVI)&&(enInputSourceType<=INPUT_SOURCE_DVI4))
    {
        u16InputSrc = enInputSourceType - INPUT_SOURCE_DVI;
        pXCResourcePrivate->u16HDMI_PortInfo[u16InputSrc] = enPorts[0];
    }

    // Initial : Turn off Y/C mux
    // This is only for one VD
    if ( IsSrcTypeDigitalVD(enInputSourceType) && enOutputType != OUTPUT_CVBS1 && enOutputType != OUTPUT_CVBS2 )
    {
        MDrv_XC_mux_turnoff_ymux(pInstance);
        MDrv_XC_mux_turnoff_cmux(pInstance);
    }

    // For backward compatibility ( Remove if app layer ready )
    // Because we can not change the API, we add a check here for old behavior.
    if ( IsSrcTypeVga(enInputSourceType) && u8Port_count == 1)
    {
        MDrv_XC_mux_set_sync_port_by_dataport(pInstance, enPorts[0]);
    }

    for (  ;  MS_U8index < u8Port_count ; MS_U8index++ )
    {
        //printf(" Port index: %d  Port type: %d \n", MS_U8index ,enPorts[MS_U8index]);
        MDrv_XC_mux_dispatch(pInstance, enPorts[MS_U8index],(E_MUX_OUTPUTPORT)dest);
    }

    //printf(" Output port: %d \n", enOutputType);

    // for AV source with the G channel of RGB
    if(!IsSrcTypeVga(enInputSourceType) && !IsSrcTypeYPbPr(enInputSourceType))
    {// VGA and YPbPr have SOG control flow in PCmonitor
        if(MDrv_XC_ADC_CheckSOGMux(pInstance, enInputSourceType))
        {
            // cvbs and ypbpr use the same pad, disable online sog
            MDrv_XC_ADC_DisableSOGMux(pInstance);

            // no Signal in SOG: SOG discharge (E_ADC_ISOG_STANDBY_MODE)
            // signal in SOG   : SOG charge    (E_ADC_ISOG_NORMAL_MODE)
            MDrv_ADC_ISOG_SetDetectMode(pInstance, E_ADC_ISOG_NORMAL_MODE);
        }
        else
        {
            MDrv_ADC_ISOG_SetDetectMode(pInstance, E_ADC_ISOG_STANDBY_MODE);
        }
    }
}

//////////////////////////////////////
//    Path Managerment Functions
//////////////////////////////////////

/*!
 *  Check path is valid or not
 *  @param src The input source of a path
 *  @param dest The destination of a path
 *  @return BOOL true if path is valid.
 */
MS_BOOL _MApi_XC_Mux_CheckValidPath(void *pInstance, INPUT_SOURCE_TYPE_t src , E_DEST_TYPE dest)
{
    // After T3, there is no spec about SV -> CVBS out. (HW design)
    if ( IsSrcTypeSV(src) && ( dest == OUTPUT_CVBS1 || dest == OUTPUT_CVBS2 ) )
    {
        return Hal_XC_SVOutput_GetCaps(pInstance);
    }
    return TRUE;
}

/*!
 *  Search a patch and return the path index if the desire path founded
 *  @param src The input source of a path
 *  @param dest The destination of a path
 *  @return Path Id stored in Mux Controller
 */
static MS_S16 _MApi_XC_Mux_SearchPath(void *pInstance, INPUT_SOURCE_TYPE_t src, E_DEST_TYPE dest)
{
    MS_S16 i;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    for ( i = 0 ; i < MAX_DATA_PATH_SUPPORTED; i++)
    {
        if ( pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].src == src &&
             pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].dest == dest &&
             (pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].u8PathStatus & DATA_PATH_USING)
           )
        {
            return i;
        }
    }
    return -1; // No maching path
}

static MS_S16 _MApi_XC_Mux_GetAvailablePath(void *pInstance, E_PATH_TYPE e_type)
{
    MS_S16 index = 0, boundary = 0;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    if ( e_type == PATH_TYPE_ASYNCHRONOUS )
    {
        index = MAX_SYNC_DATA_PATH_SUPPORTED ;
        boundary = MAX_DATA_PATH_SUPPORTED ;
    }
    else
    {
        boundary = MAX_SYNC_DATA_PATH_SUPPORTED ;
    }

    for (; index < boundary; index++)
    {
        if (pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[index].u8PathStatus & DATA_PATH_USING)
        {
            continue;
        }
        else
        {
            return index;
        }
    }

    return -1; // No maching path

}

// Return sources connected with Main window or Sub window
static MS_U8 _MApi_XC_Mux_GetUsingSources(void *pInstance, INPUT_SOURCE_TYPE_t* src_array )
{
    MS_U8 i = 0, j = 0 , available_source_count = 0;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    for ( ; i < MAX_SYNC_DATA_PATH_SUPPORTED ; i++)
    {
        // Retrieve sources which is using right now.
        if (  ( pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].u8PathStatus & ( DATA_PATH_USING ) ) != ( DATA_PATH_USING ) )
            continue;

        // Check if source on path i already in src_array
        for ( j = 0 ; j < available_source_count ; j++ )
        {
            if ( src_array[j] == pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].src )
                break;
        }

        if ( j >= available_source_count )
        {
            // no same source in src_array
            // only return source which connected with main or sub window
            if ( pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].dest != OUTPUT_CVBS1 &&
                 pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].dest != OUTPUT_CVBS2 &&
                 pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].dest != OUTPUT_SCALER_DWIN)
            {
                src_array[available_source_count] = pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].src ;
                available_source_count++;
            }
        }
    }
    return available_source_count;
}
// Public Functions

//-------------------------------------------------------------------------------------------------
/// Initialize Mux before using Mux controller
/// @param  input_source_to_input_port      \b IN: a function to map from input source to input port
//-------------------------------------------------------------------------------------------------
void MApi_XC_Mux_Init_U2(void* pInstance, void (*input_source_to_input_port)(INPUT_SOURCE_TYPE_t src_ids , E_MUX_INPUTPORT* port_ids , MS_U8* u8port_count ) )
{
    MS_U16 i;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.u8PathCount = 0;
    pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.input_source_to_input_port = input_source_to_input_port;
    for ( i = 0 ; i < MAX_DATA_PATH_SUPPORTED; i++)
    {
        pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].u8PathStatus = 0;
    }

    for(i=0; i<INPUT_SOURCE_NUM; i++)
    {
        pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.u16SrcCreateTime[i] = 0;
    }
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_Mux_Init(void (*input_source_to_input_port)(INPUT_SOURCE_TYPE_t src_ids , E_MUX_INPUTPORT* port_ids , MS_U8* u8port_count ) )
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_MUX_INIT XCArgs;
    XCArgs.input_source_to_input_port = input_source_to_input_port;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_MUX_INIT, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Monitor the source of Mux
/// @param  bRealTimeMonitorOnly      \b IN: define whether do the monitor routine only for real time
//-------------------------------------------------------------------------------------------------
void MApi_XC_Mux_SourceMonitor_U2(void* pInstance, MS_BOOL bRealTimeMonitorOnly)
{
    MS_S16 i;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    for ( i = 0 ; i < MAX_SYNC_DATA_PATH_SUPPORTED ; i++)
    {
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
        UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
        if (  ( pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].u8PathStatus & ( DATA_PATH_ENABLE | DATA_PATH_USING ) )
              == ( DATA_PATH_ENABLE | DATA_PATH_USING ) )
        {
            // execute path_thread()
            if (pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].path_thread != NULL)
            {
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].path_thread(pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].src, bRealTimeMonitorOnly);
                _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
            }

            // execute destination periodic handler
            if(((pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].u8PathStatus & ENABLE_PERIODIC_HANDLER) == ENABLE_PERIODIC_HANDLER) &&
               (pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].dest_periodic_handler != NULL))
            {
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].dest_periodic_handler(pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].src, bRealTimeMonitorOnly );
                _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
            }
        }
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    }

    // This is patch for imme-switch. It can be fix in the feature.
    Hal_DVI_IMMESWITCH_PS_SW_Path();

}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_Mux_SourceMonitor(MS_BOOL bRealTimeMonitorOnly)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_MUX_SOURCE_MONITOR XCArgs;
    XCArgs.bRealTimeMonitorOnly = bRealTimeMonitorOnly;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_MUX_SOURCE_MONITOR, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// A path is disabled after creating it (\ref MApi_XC_Mux_CreatePath)
/// If a path is not enabled,   \ref MApi_XC_Mux_SourceMonitor will bypass it.
/// @param PathId               \b IN: PathId The Path you want to enable
/// @return 1 if enable successfuly. Return -1 if enable fail
//-------------------------------------------------------------------------------------------------
MS_S16 MApi_XC_Mux_EnablePath_U2(void* pInstance, MS_U16 PathId)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    if ( PathId >= MAX_DATA_PATH_SUPPORTED )
    {
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return -1;
    }
    else
    {
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
        UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
        pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[PathId].u8PathStatus |= DATA_PATH_ENABLE;
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return 1;
    }
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_S16 MApi_XC_Mux_EnablePath(MS_U16 PathId)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return -1;
    }

    stXC_SET_MUX_ENABLE_PATH XCArgs;
    XCArgs.PathId = PathId;
    XCArgs.s16ReturnValue = -1;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_MUX_ENABLE_PATH, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return -1;
    }
    else
    {
        return XCArgs.s16ReturnValue;
    }
}
#endif

#if (ENABLE_NONSTD_INPUT_MCNR==1)

extern MS_U8 MDrv_AVD_GetRegValue(MS_U16 u16Addr);
#define BK_AFEC_CC    (0x35CC)

static void _MApi_XC_Sys_Detect_UnStd_Input_Status_Init(void)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    pXCResourcePrivate->stdrvXC_MVideo.bIsUnStdSignal = FALSE;
    pXCResourcePrivate->stdrvXC_MVideo.u8StdSignalStbCnt = 0;
}

static void _MApi_XC_CVBSUnStdISR(SC_INT_SRC eIntNum, void * pParam)
{
    UNUSED(eIntNum); UNUSED(pParam);
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    if((MDrv_AVD_GetRegValue(BK_AFEC_CC) & 0x18) == 0x18)        // non-standard input
    {
        pXCResourcePrivate->stdrvXC_MVideo.u8StdSignalStbCnt = 0;

        if (pXCResourcePrivate->stdrvXC_MVideo.bIsUnStdSignal == FALSE)
        {
            SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_27_L, 0x01, 0x01);
            pXCResourcePrivate->stdrvXC_MVideo.bIsUnStdSignal = TRUE;
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "_MApi_XC_CVBSUnStdISR: unStdSignal\n");
        }
    }
    else
    {
        if(pXCResourcePrivate->stdrvXC_MVideo.bIsUnStdSignal == TRUE)
        {
            pXCResourcePrivate->stdrvXC_MVideo.u8StdSignalStbCnt++;

            if(pXCResourcePrivate->stdrvXC_MVideo.u8StdSignalStbCnt > 10)
            {
                SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_27_L, 0x00, 0x01);
                pXCResourcePrivate->stdrvXC_MVideo.u8StdSignalStbCnt = 0;
                pXCResourcePrivate->stdrvXC_MVideo.bIsUnStdSignal = FALSE;
                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "_MApi_XC_CVBSUnStdISR: StdSignal\n");
            }
        }
    }
}

#endif

static void _Mdrv_XC_Mux_DeletePath(void* pInstance, MS_S16 PathId)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[PathId].u8PathStatus = 0;

    if (pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.u8PathCount > 0)
        pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.u8PathCount--;
}

static void _Mdrv_XC_Mux_DecreaseSrcCreateTime(void* pInstance, INPUT_SOURCE_TYPE_t src, MS_BOOL bIsIncrease)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    if (bIsIncrease)
    {
        // Increate src create time
        if(pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.u16SrcCreateTime[src] < 0xFFFF)
        {
            pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.u16SrcCreateTime[src]++;
        }
    }
    else
    {
        // Decrease src create time
        if(pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.u16SrcCreateTime[src] > 0)
        {
            pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.u16SrcCreateTime[src]--;
        }
    }
}


//-------------------------------------------------------------------------------------------------
/// delete a path from Mux Controller.
/// @param src              \b IN: Type of input source of a path you want to delete
/// @param dest             \b IN: Type of destination of a path you want to delete
/// @return 1 if delete successfuly. Return -1 if delete fail
//-------------------------------------------------------------------------------------------------
MS_S16 MApi_XC_Mux_DeletePath_U2(void* pInstance, INPUT_SOURCE_TYPE_t src, E_DEST_TYPE dest)
{
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
    if (dest == OUTPUT_SCALER_SUB_WINDOW)
    {
        pInstance = g_pDevice1Instance;
        dest = OUTPUT_SCALER2_MAIN_WINDOW;
    }
#endif
    // Maximum number of sources is the number of pathes
    INPUT_SOURCE_TYPE_t _InputSource[MAX_SYNC_DATA_PATH_SUPPORTED] ;
    MS_U8 _source_count = 0;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    _XC_ENTRY(pInstance);

    MS_S16 PathId = _MApi_XC_Mux_SearchPath(pInstance, src,dest);

    XC_LOG_TRACE(XC_DBGLEVEL_MUX,"=========== MApi_XC_Mux_DeletePath(ID 0x%x) is called =========== \n", PathId);
    XC_LOG_TRACE(XC_DBGLEVEL_MUX,"Source : %d   Destination : %d \n",src,dest);

    if ( PathId != -1 )
    {
        MS_U8 u8Index = 0;

        // Initial _InputSource
        for (; u8Index < MAX_SYNC_DATA_PATH_SUPPORTED; u8Index++)
        {
            _InputSource[u8Index] = INPUT_SOURCE_NONE;
        }

        // Clear status.
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
        _Mdrv_XC_Mux_DeletePath(g_pDevice0Instance, PathId);
        _Mdrv_XC_Mux_DeletePath(g_pDevice1Instance, PathId);
#else
        _Mdrv_XC_Mux_DeletePath(pInstance, PathId);
#endif
        // Update ADC setting for multi-source.
        if ( pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.u8PathCount > 0 )
        {
            _source_count = _MApi_XC_Mux_GetUsingSources(pInstance, _InputSource);

            XC_LOG_TRACE(XC_DBGLEVEL_MUX,"Total using source current %d \n",_source_count);

            MDrv_XC_ADC_SetInputSource(pInstance, _InputSource, _source_count);
        }

        if(IsSrcTypeDTV(src) || IsSrcTypeStorage(src))
        {
            MHal_XC_SetHandShakeMode(pInstance, FALSE);
        }
        if ( IsSrcTypeHDMI(src) || IsSrcTypeDVI(src)  )
        {
            // Power down all HDMI power
            // because HDMI cannot PIP with HDMI, so we can power down all the HDMIs
            Hal_SC_mux_set_dvi_mux(pInstance, 0xFF, HAL_OUTPUTPORT_NONE_PORT);
        }

#if (XC_SUPPORT_IOMMU == TRUE)
        if(pXCResourcePrivate->stdrvXC_MVideo_Context.stMMAfeature.u32Support == MSOS_MMA_SUPPORT)
        {
            if ((dest == OUTPUT_SCALER_MAIN_WINDOW) && (pXCResourcePrivate->sthal_Optee.op_tee_xc[MAIN_WINDOW].isEnable == FALSE)
                &&(pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_XC_MAIN] == TRUE)
                )
            {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
                MHal_XC_ReleaseMMA(pInstance,MAIN_WINDOW);
#else
                MS_BOOL bRet = FALSE;

                bRet = Mdrv_XC_Task_WaitEventDone(
                pInstance,
                E_XC_MMA_EVENT_XC_PUT,
                E_XC_MMA_EVENT_XC_PUT_DONE,
                TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                MAIN_WINDOW);

                if(bRet == FALSE)
                {
                    printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                }
#endif
            }
            if ((dest == OUTPUT_SCALER_SUB_WINDOW) && (pXCResourcePrivate->sthal_Optee.op_tee_xc[SUB_WINDOW].isEnable == FALSE)
                && (pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_XC_SUB] == TRUE)
                )
            {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
                MHal_XC_ReleaseMMA(pInstance,SUB_WINDOW);
#else
                MS_BOOL bRet = FALSE;

                bRet = Mdrv_XC_Task_WaitEventDone(
                pInstance,
                E_XC_MMA_EVENT_XC_PUT,
                E_XC_MMA_EVENT_XC_PUT_DONE,
                TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                SUB_WINDOW);
                if(bRet == FALSE)
                {
                    printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                }
#endif

            }
            if((dest == OUTPUT_SCALER_MAIN_WINDOW) && (pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_HDMI_DMA] == TRUE))
            {
                if ( IsSrcTypeHDMI(src))
                {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
                    MHal_XC_HdmiDMAReleaseMMA(pInstance,MAIN_WINDOW);
#else
                    MS_BOOL bRet = FALSE;

                    bRet = Mdrv_XC_Task_WaitEventDone(
                    pInstance,
                    E_XC_MMA_EVENT_HDMI_DMA_PUT,
                    E_XC_MMA_EVENT_HDMI_DMA_PUT_DONE,
                    TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                    MAIN_WINDOW);
                    if(bRet == FALSE)
                    {
                        printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                    }
#endif
                }
            }
            if((dest == OUTPUT_SCALER_MAIN_WINDOW) && (pXCResourcePrivate->sthal_Optee.op_tee_xc[MAIN_WINDOW].isEnable == FALSE)
                &&((pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_FRC_RIGHT] == TRUE)
                  || (pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_FRC_LEFT] == TRUE)
                  || (pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_FRC_PQ] == TRUE)))
            {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
                MHal_FRC_ReleaseMMA(pInstance);
#else
                MS_BOOL bRet = FALSE;

                bRet = Mdrv_XC_Task_WaitEventDone(
                pInstance,
                E_XC_MMA_EVENT_FRC_PUT,
                E_XC_MMA_EVENT_FRC_PUT_DONE,
                TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                MAIN_WINDOW);
                if(bRet == FALSE)
                {
                    printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                }
#endif
            }
        }
#endif

#ifdef UFO_XC_SUPPORT_DUAL_MIU
        if (IsEnableDualMode(MAIN_WINDOW))
        {
            Hal_SC_set_dual_disable(pInstance);
        }
#endif
#ifndef DONT_USE_CMA
#if (XC_SUPPORT_CMA == TRUE)
        if (
#if (XC_SUPPORT_IOMMU == TRUE)
            ((pXCResourcePrivate->stdrvXC_MVideo_Context.stMMAfeature.u32Support == MSOS_MMA_NOT_SUPPORT)
              || (pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_XC_MAIN] == FALSE)) &&
#endif
#if (HW_DESIGN_4K2K_VER == 7)
            (psXCInstPri->u32DeviceID == 0) &&
            (dest == OUTPUT_SCALER_MAIN_WINDOW) &&
#endif
#ifndef UFO_XC_SUPPORT_SUB_CMA
            (dest != OUTPUT_SCALER2_MAIN_WINDOW) &&
#endif
            (pXCResourcePrivate->sthal_Optee.op_tee_xc[MAIN_WINDOW].isEnable == FALSE))
        {
            MHal_XC_Release_CMA(pInstance, CMA_XC_SELF_MEM, MAIN_WINDOW);
            MHal_XC_Release_CMA(pInstance, CMA_XC_COBUFF_MEM, MAIN_WINDOW);
            if (gSrcInfo[MAIN_WINDOW].Status2.stXCConfigCMA[CMA_XC_HDR_DMA_MEM].u32HeapID != XC_INVALID_HEAP_ID)
            {
                MHal_XC_Release_CMA(pInstance, CMA_XC_HDR_DMA_MEM, MAIN_WINDOW);
            }
        }
#endif
#endif
        //Disable SUB_Window CLK
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
        if (dest == OUTPUT_SCALER2_MAIN_WINDOW)
#else
        if (dest == OUTPUT_SCALER_SUB_WINDOW)
#endif
        {
            MApi_XC_EnableCLK_for_SUB(pInstance, DISABLE);
            MDrv_XC_Enable_Extra_Request(pInstance, DISABLE);
        }
        else if(dest == OUTPUT_SCALER_DWIN)
        {
            MApi_XC_EnableCLK_for_DIP(pInstance, DISABLE);
        }

        XC_LOG_TRACE(XC_DBGLEVEL_MUX,"=========== Path deleted success =========== \n");

        // Decrease src create time
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
        _Mdrv_XC_Mux_DecreaseSrcCreateTime(g_pDevice0Instance, src, FALSE);
        _Mdrv_XC_Mux_DecreaseSrcCreateTime(g_pDevice0Instance, src, FALSE);
#else
        _Mdrv_XC_Mux_DecreaseSrcCreateTime(pInstance, src, FALSE);
#endif
        if (pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.u16SrcCreateTime[src] == 0)
        {
            if ( IsSrcTypeYPbPr(src) || IsSrcTypeVga(src)  )
            {
                // ADC have to set as free run
                MDrv_XC_ADC_Set_Freerun(pInstance, TRUE);
            }
        }

#if (ENABLE_NONSTD_INPUT_MCNR == 1)
        MS_U8 i;

        for(i=0; i<_source_count; i++)
        {
            if(IsSrcTypeATV(_InputSource[i]))
            {
                break;
            }
        }

        if(i==_source_count)
        {
            // there is no more ATV input, de-attach ISR
            MDrv_XC_InterruptDeAttach(pInstance, XC_INT_VSINT, _MApi_XC_CVBSUnStdISR, &pXCResourcePrivate->stdrvXC_MVideo.u8UnStdOutput[MAIN_WINDOW]);
        }
#endif

        // We clear Input source variable here. To reset scaler IP again if same source connected again.
        if (dest == OUTPUT_SCALER_MAIN_WINDOW)
        {
            pXCResourcePrivate->stdrvXC_MVideo.enOldInputSrc_ForScalerSetSource[MAIN_WINDOW] = INPUT_SOURCE_NONE;
        }
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
        else if (dest == OUTPUT_SCALER2_MAIN_WINDOW)
#else
        else if (dest == OUTPUT_SCALER_SUB_WINDOW)
#endif
        {
            pXCResourcePrivate->stdrvXC_MVideo.enOldInputSrc_ForScalerSetSource[SUB_WINDOW] = INPUT_SOURCE_NONE;
        }

        //Disable HFR path clock
        if(MHal_XC_Is_Support_HFR_Path())
        {
            ST_FRC_HFR_COMPOSER_PATH_ENABLE stPathEnable;
            memset(&stPathEnable, 0, sizeof(ST_FRC_HFR_COMPOSER_PATH_ENABLE));
            //Disable composer1,2 main,sub path
            if((dest == OUTPUT_SCALER_MAIN_WINDOW) && (gSrcInfo[MAIN_WINDOW].Status2.bEnableHFRPath == TRUE))
            {
                stPathEnable.eType = E_FRC_COMPOSER_BEFORE_MEMC;
                stPathEnable.bEnable = FALSE;
                MHal_FRC_HFR_Composer_Enable_Main(pInstance, stPathEnable);

                stPathEnable.eType = E_FRC_COMPOSER_AFTER_MEMC;
                stPathEnable.bEnable = FALSE;
                MHal_FRC_HFR_Composer_Enable_Main(pInstance, stPathEnable);
            }
            else if((dest == OUTPUT_SCALER_MAIN_WINDOW) && (gSrcInfo[SUB_WINDOW].Status2.bEnableHFRPath == FALSE))
            {
                stPathEnable.eType = E_FRC_COMPOSER_BEFORE_MEMC;
                stPathEnable.bEnable = FALSE;
                MHal_FRC_HFR_Composer_Enable_Main(pInstance, stPathEnable);
                stPathEnable.eType = E_FRC_COMPOSER_AFTER_MEMC;
                stPathEnable.bEnable = FALSE;
                MHal_FRC_HFR_Composer_Enable_Main(pInstance, stPathEnable);
            }
            else if(gSrcInfo[SUB_WINDOW].Status2.bEnableHFRPath == TRUE)
            {
                if(dest == OUTPUT_SCALER_MAIN_WINDOW)
                {
                    stPathEnable.eType = E_FRC_COMPOSER_BEFORE_MEMC;
                    stPathEnable.bEnable = FALSE;
                    MHal_FRC_HFR_Composer_Enable_Main(pInstance, stPathEnable);
                }
                if(dest == OUTPUT_SCALER_SUB_WINDOW)
                {
                    stPathEnable.eType = E_FRC_COMPOSER_AFTER_MEMC;
                    stPathEnable.bEnable = FALSE;
                    MHal_FRC_HFR_Composer_Enable_Sub(pInstance, stPathEnable);
                }
            }
            else
            {
                XC_LOG_TRACE(XC_DBGLEVEL_MUX,"Invalid !!!\n");
            }

            if((dest == OUTPUT_SCALER_MAIN_WINDOW) && (gSrcInfo[MAIN_WINDOW].Status2.bEnableHFRPath == TRUE))
            {
                MHal_SC_HFR_Path_CLK_Setting(pInstance, FALSE);
                MHal_XC_HFR_Disable_CropBeforeFRC(pInstance);
                gSrcInfo[MAIN_WINDOW].Status2.bEnableHFRPath = FALSE;
            }
            else if(dest == OUTPUT_SCALER_SUB_WINDOW)
            {
                MHal_SC_HFR_Path_CLK_Setting(pInstance, FALSE);
                gSrcInfo[SUB_WINDOW].Status2.bEnableHFRPath = FALSE;
                _XC_RETURN(pInstance);
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                if (MHal_XC_Is_Support_DIP_Sub_Path())
                {
                    if(!MDrv_XC_GetMWEStatus(pInstance))
                    {
                        if(gSrcInfo[SUB_WINDOW].Status2.bDIPOpened)
                        {
                            gSrcInfo[SUB_WINDOW].Status2.bDIPOpened = FALSE;
                            MApi_XC_DIP_Ena(FALSE, DWIN0_WINDOW);
                            ST_DIP_DIPR_BLENDING_MODE_SETTINGS stDIPRBlending;
                            memset(&stDIPRBlending, 0, sizeof(ST_DIP_DIPR_BLENDING_MODE_SETTINGS));
                            stDIPRBlending.enDIPRSel = E_XC_DIPR0;
                            if(MHal_XC_Is_Support_HFR_Path())
                            {
                                stDIPRBlending.enDIPRDst = E_XC_BLENDING_HFR;
                            }
                            else
                            {
                                stDIPRBlending.enDIPRDst = E_XC_BLENDING_COMPOSER;
                            }
                            stDIPRBlending.bEnable = FALSE;
                            _MApi_XC_DIP_ProcessMode(DWIN0_WINDOW, EN_DIP_MODE_DIPR_BLENDING,(void *)&stDIPRBlending);
                            MApi_XC_DIP_ReleaseResource(DWIN0_WINDOW);
                        }
                        else
                        {
                            printf("DIP is not opened!!!");
                        }
                        MDrv_XC_EnableWindow(pInstance, FALSE, SUB_WINDOW);
                    }
                  MHal_FRC_DisableFRCOPMWrite(pInstance, TRUE, SUB_WINDOW);
                }
            _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
            _XC_ENTRY(pInstance);
            }
            else
            {
                XC_LOG_TRACE(XC_DBGLEVEL_MUX,"Invalid window type\n");
            }
        }

        // Inform CFD about Delete Path
        XC_CFD_CONTROL_INFO stCfdCtrl;
        ST_XC_MUX_INFO stXcMuxInfo;
        PQ_INPUT_SOURCE_TYPE enPqInputSourceType = PQ_INPUT_SOURCE_NONE;

        if(MDrv_XC_ConvertToPQInputSourceType(src, &enPqInputSourceType) == FALSE)
        {
            XC_LOG_TRACE(XC_DBGLEVEL_PARAMETER_TRACE, "[%s,%5d] Unexpected input source type = %d. \n", __FUNCTION__, __LINE__, src);
        }

        if(dest == OUTPUT_SCALER_MAIN_WINDOW)
        {
            memset(&stXcMuxInfo, 0, sizeof(ST_XC_MUX_INFO));
            stXcMuxInfo.enAction = E_XC_CFD_MUX_DELETE;
            stXcMuxInfo.u8Win = (MS_U8) MAIN_WINDOW;
            stXcMuxInfo.u8InputSource = (MS_U8) enPqInputSourceType;

            memset(&stCfdCtrl, 0, sizeof(XC_CFD_CONTROL_INFO));
            stCfdCtrl.enCtrlType = E_XC_CFD_CTRL_MUX_INFO;
            stCfdCtrl.pParam = &stXcMuxInfo;
            stCfdCtrl.u32ParamLen = sizeof(ST_XC_MUX_INFO);
            MDrv_XC_HDR_Control(pInstance, E_XC_HDR_CTRL_CFD_CONTROL, &stCfdCtrl);
        }

        _XC_RETURN(pInstance);
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

        return TRUE;
    }
    else
    {
        XC_LOG_TRACE(XC_DBGLEVEL_MUX,"This path is not exist\n");
        XC_LOG_TRACE(XC_DBGLEVEL_MUX,"=========== Path deleted fail =========== \n");
        _XC_RETURN(pInstance);
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return -1;
    }
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_S16 MApi_XC_Mux_DeletePath(INPUT_SOURCE_TYPE_t src, E_DEST_TYPE dest)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return -1;
    }

    stXC_SET_MUX_DELETE_PATH XCArgs;
    XCArgs.src = src;
    XCArgs.dest = dest;
    XCArgs.s16ReturnValue = -1;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_MUX_DELETE_PATH, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return -1;
    }
    else
    {
        return XCArgs.s16ReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Trigger Sync Event on Mux Controller.
/// The Mux Controller will pass src and *para to each Sync Event Handler (refer synchronous_event_handler of MUX_DATA_PATH also).
///  ( Delegate Function:
///    Send necessary parameter to SyncEventHandler of each synchronous_path which source is 'src' )
/// @param src          \b IN: The input source which triggers Sync Event
/// @param para         \b IN: A pointer points to parameters which need to pass to Event Handler
//-------------------------------------------------------------------------------------------------
void MApi_XC_Mux_TriggerPathSyncEvent_U2( void* pInstance, INPUT_SOURCE_TYPE_t src , void* para)
{
    MS_S16 i;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    for ( i = 0 ; i < MAX_SYNC_DATA_PATH_SUPPORTED ; i++)
    {
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
        UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
        if (  ( pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].u8PathStatus & ( DATA_PATH_ENABLE | DATA_PATH_USING ) )
              != ( DATA_PATH_ENABLE | DATA_PATH_USING ) )
        {
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            continue;
        }

        // send 'src' to event handler of each path
        if ( pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].src == src &&
             pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].synchronous_event_handler != NULL)
        {
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].synchronous_event_handler(src, para);
            _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
        }
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    }
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_Mux_TriggerPathSyncEvent( INPUT_SOURCE_TYPE_t src , void* para)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_MUX_TRIGGER_PATH_SYNC_EVENT XCArgs;
    XCArgs.src = src;
    XCArgs.para = para;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_MUX_TRIGGER_PATH_SYNC_EVENT, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
/*!
 *  Trigger Sync Event on Mux Controller.
 *  The Mux Controller will pass src and *para to each Sync Event Handler (refer to synchronous_event_handler
 *   of MUX_DATA_PATH also).
 *
 *  ( Delegate Function:
 *    Send necessary parameter to SyncEventHandler of each synchronous_path which source is 'src' )
 *  @param src The input source which triggers Sync Event
 *  @param para A pointer points to parameters which need to pass to Event Handler
 */
//-------------------------------------------------------------------------------------------------
/// Set the specific window
/// @param  src             \b IN: the source type for handler
/// @param para         \b IN: A pointer points to parameters which need to pass to Event Handler
//-------------------------------------------------------------------------------------------------
void MApi_XC_Mux_TriggerDestOnOffEvent_U2( void* pInstance, INPUT_SOURCE_TYPE_t src , void* para)
{
    MS_S16 i;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    for ( i = 0 ; i < MAX_SYNC_DATA_PATH_SUPPORTED ; i++)
    {
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
        UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
        if (  ( pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].u8PathStatus & ( DATA_PATH_ENABLE | DATA_PATH_USING ) )
              != ( DATA_PATH_ENABLE | DATA_PATH_USING ) )
        {
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            continue;
        }

        // send 'src' to event handler of each path
        if ( pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].src == src &&
             pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].dest_on_off_event_handler != NULL)
        {
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].dest_on_off_event_handler(src, para);
            _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
        }
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    }
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_Mux_TriggerDestOnOffEvent( INPUT_SOURCE_TYPE_t src , void* para)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_MUX_TRIGGER_DEST_ONOFF_EVENT XCArgs;
    XCArgs.src = src;
    XCArgs.para = para;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_MUX_TRIGGER_DEST_ONOFF_EVENT, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// This function was used to enable/disable the destination periodic handler
/// After enabled periodic hander, Mux Controller will pass the parameters to this handler and execute it periodically
/// @param  src             \b IN: the source type for handler
/// @param  bEnable         \b IN: Enable/Disable the priodic handler.
/// @return 1 if successfuly. Return -1 if failed.
//-------------------------------------------------------------------------------------------------
MS_S16 MApi_XC_Mux_OnOffPeriodicHandler_U2( void* pInstance, INPUT_SOURCE_TYPE_t src, MS_BOOL bEnable)
{
    MS_S16 i;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    for ( i = 0 ; i < MAX_SYNC_DATA_PATH_SUPPORTED ; i++)
    {
        if (  ( pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].u8PathStatus & ( DATA_PATH_ENABLE | DATA_PATH_USING ) )
              != ( DATA_PATH_ENABLE | DATA_PATH_USING ) )
        {
            continue;
        }

        // send 'src' to event handler of each path
        if(bEnable)
        {
            pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].u8PathStatus |= ENABLE_PERIODIC_HANDLER;
        }
        else
        {
            pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].u8PathStatus &= ~ENABLE_PERIODIC_HANDLER;
        }
    }
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return i;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_S16 MApi_XC_Mux_OnOffPeriodicHandler( INPUT_SOURCE_TYPE_t src, MS_BOOL bEnable)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return -1;
    }

    stXC_SET_MUX_ONOFF_PERIODIC_HANDLER XCArgs;
    XCArgs.src = src;
    XCArgs.bEnable = bEnable;
    XCArgs.s16ReturnValue = -1;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_MUX_ONOFF_PERIODIC_HANDLER, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return -1;
    }
    else
    {
        return XCArgs.s16ReturnValue;
    }
}
#endif
typedef enum
{
    E_OnPathAdd = 0,
    E_OnPathDel= 1,
    E_PathMax,
}E_PathStatus;

static MS_U8 _MApi_XC_Mux_GetPathInfo(void* pInstance, XC_MUX_PATH_INFO* Paths)
{
    MS_U8 u8PathNum = 0;
    MS_U8 u8PathCount = 0;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    for ( u8PathNum = 0, u8PathCount = 0 ; u8PathNum < MAX_SYNC_DATA_PATH_SUPPORTED ; u8PathNum++)
    {
        if (pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[u8PathNum].u8PathStatus & DATA_PATH_USING)
        {
            Paths[u8PathCount].src = pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[u8PathNum].src;
            Paths[u8PathCount].dest = pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[u8PathNum].dest;
            Paths[u8PathCount].path_thread = pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[u8PathNum].path_thread;
            Paths[u8PathCount].SyncEventHandler =  pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[u8PathNum].synchronous_event_handler;
            Paths[u8PathCount].DestOnOff_Event_Handler =  pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[u8PathNum].dest_on_off_event_handler;
            Paths[u8PathCount].dest_periodic_handler =  pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[u8PathNum].dest_periodic_handler;
            u8PathCount++;
            //printf(" Path Id: [%d]  Source: %d   Destination: %d \n",u8PathNum,
            // s_InputSource_Mux_attr.Mux_DataPaths[u8PathNum].src,
            //s_InputSource_Mux_attr.Mux_DataPaths[u8PathNum].dest);
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
            if (Paths[u8PathCount].dest == OUTPUT_SCALER2_MAIN_WINDOW)
            {
                Paths[u8PathCount].dest = OUTPUT_SCALER_SUB_WINDOW;
            }
#endif
        }
    }
    return u8PathCount;
}

static void _Mdrv_XC_Mux_CreatePath(void* pInstance, XC_MUX_PATH_INFO* Path_Info, MS_S16 PathId)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.u8PathCount++;
    pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[PathId].e_PathType = Path_Info->Path_Type;
    pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[PathId].src = Path_Info->src;
    pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[PathId].dest = Path_Info->dest;
    pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[PathId].u8PathStatus = DATA_PATH_USING;
    pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[PathId].path_thread = Path_Info->path_thread;
    pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[PathId].synchronous_event_handler = Path_Info->SyncEventHandler;
    pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[PathId].dest_on_off_event_handler = Path_Info->DestOnOff_Event_Handler;
    pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[PathId].dest_periodic_handler = Path_Info->dest_periodic_handler;

#if (HW_DESIGN_4K2K_VER == 7)
//temp make a default map matrix for Kano
    pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[INPUT_SOURCE_DTV].u32EnablePort = 1;
    pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[INPUT_SOURCE_DTV].u32Port[0] = INPUT_PORT_MVOP;
    pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[INPUT_SOURCE_DTV2].u32EnablePort = 1;
    pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[INPUT_SOURCE_DTV2].u32Port[0] = INPUT_PORT_MVOP2;
    pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[INPUT_SOURCE_STORAGE].u32EnablePort = 1;
    pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[INPUT_SOURCE_STORAGE].u32Port[0] = INPUT_PORT_MVOP;
    pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[INPUT_SOURCE_STORAGE2].u32EnablePort = 1;
    pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[INPUT_SOURCE_STORAGE2].u32Port[0] = INPUT_PORT_MVOP2;
    pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[INPUT_SOURCE_HDMI].u32EnablePort = 1;
    pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[INPUT_SOURCE_HDMI].u32Port[0] = INPUT_PORT_DVI0;
    pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[INPUT_SOURCE_SCALER_OP].u32EnablePort = 1;
    pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[INPUT_SOURCE_SCALER_OP].u32Port[0] = INPUT_PORT_SCALER_OP;
    pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[INPUT_SOURCE_SC0_DI].u32EnablePort = 1;
    pXCResourcePrivate->stdrvXC_MVideo.mapping_tab[INPUT_SOURCE_SC0_DI].u32Port[0] = INPUT_PORT_SCALER_DI;
#ifdef MSOS_TYPE_LINUX_KERNEL
    //for msAPI usage, we set dest_periodic_handler by utopia PQ's MDrv_PQ_AdaptiveTuning
    if(psXCInstPri->u32DeviceID == 0)
    {
        pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[PathId].dest_periodic_handler = MDrv_PQ_AdaptiveTuning;
    }
    else
    {
        pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[PathId].dest_periodic_handler = NULL;
    }
#endif
#endif

}
//-------------------------------------------------------------------------------------------------
/// Create a path in Mux Controller.
/// @param  Path_Info               \b IN: the information of the path
/// @param  u32InitDataLen          \b IN: the length of the Path_Info
/// @return @ref MS_S16 return the path id, or -1 when any error is happened
//-------------------------------------------------------------------------------------------------
MS_S16 MApi_XC_Mux_CreatePath_U2(void* pInstance, XC_MUX_PATH_INFO* Path_Info , MS_U32 u32InitDataLen)
{
    MS_S16 s16PathId = 0;
    MS_U8 u8Index = 0;
    MS_U8 u8SourceCount = 0;
    MS_U8 u8LoopCount = 0;
    // APVR modifications
    MS_U8 u8DigitalVDCount = 0;
    MS_U8 u8PathCount =  0;
    XC_MUX_PATH_INFO SC_Paths[MAX_DATA_PATH_SUPPORTED] = {0};
    // Maximum number of sources is the number of pathes
    INPUT_SOURCE_TYPE_t _InputSource[MAX_SYNC_DATA_PATH_SUPPORTED];
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    // window info
    SCALER_WIN eWindow = MAIN_WINDOW;

#ifdef MSOS_TYPE_LINUX_KERNEL
    if(!is_compat_task())
    {
        if(u32InitDataLen != sizeof(XC_MUX_PATH_INFO) || Path_Info == NULL)
        {
            // ASSERT when driver is not initiallized
            MS_ASSERT(0);
            XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
            return -1;
        }
    }
#else
    if(u32InitDataLen != sizeof(XC_MUX_PATH_INFO) || Path_Info == NULL)
    {
        // ASSERT when driver is not initiallized
        MS_ASSERT(0);
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return -1;
    }
#endif
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
    if (Path_Info->dest == OUTPUT_SCALER_SUB_WINDOW)
    {
        pInstance = g_pDevice1Instance;
        Path_Info->dest = OUTPUT_SCALER2_MAIN_WINDOW;
    }
#endif

    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    _XC_ENTRY(pInstance);

    if((Path_Info->dest == OUTPUT_SCALER2_MAIN_WINDOW) || (Path_Info->dest == OUTPUT_SCALER_SUB_WINDOW))
    {
        eWindow = SUB_WINDOW;
    }
    MDrv_XC_CheckSubWinForFSS(pInstance, &eWindow);
    MHal_XC_SetOfflineSource(pInstance, eWindow, Path_Info->src);

    XC_LOG_TRACE(XC_DBGLEVEL_MUX,"=========== MApi_XC_Mux_CreatePath is called =========== \n");
    XC_LOG_TRACE(XC_DBGLEVEL_MUX,"Source : %d   Destination : %d \n",Path_Info->src,Path_Info->dest);

    if (pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.u8PathCount >= MAX_DATA_PATH_SUPPORTED)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_MUX,"There is no avaliable path. Current Total Path Count : %d ",pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.u8PathCount );
        _XC_RETURN(pInstance);
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return -1; // No avaliable path.
    }

    if ( _MApi_XC_Mux_SearchPath(pInstance, Path_Info->src,Path_Info->dest) != -1)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_MUX,"Path already exist\n");
        _XC_RETURN(pInstance);
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return -1; // Path exist
    }

    if ( _MApi_XC_Mux_CheckValidPath(pInstance, Path_Info->src, Path_Info->dest) == FALSE)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_MUX,"Path is invalid\n");
        _XC_RETURN(pInstance);
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return -1; // Path is invalid
    }

    if(_XC_Mutex == -1)
    {
        MS_ASSERT(0);
        XC_LOG_TRACE(XC_DBGLEVEL_MUX,"XC dirver have to be initiallized first \n");
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        _XC_RETURN(pInstance);
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        return -1;
    }

    //Add one path
    s16PathId= _MApi_XC_Mux_GetAvailablePath(pInstance, Path_Info->Path_Type);

    if ( s16PathId == -1 )
    {
        XC_LOG_TRACE(XC_DBGLEVEL_MUX,"There is no avaliable path. Current Total Path Count : %d ",pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.u8PathCount );
        _XC_RETURN(pInstance);
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return -1; // no available path
    }

    // Path create.
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
    _Mdrv_XC_Mux_CreatePath(g_pDevice0Instance, Path_Info, s16PathId);
    _Mdrv_XC_Mux_CreatePath(g_pDevice1Instance, Path_Info, s16PathId);
#else
    _Mdrv_XC_Mux_CreatePath(pInstance, Path_Info, s16PathId);
#endif
    // Set physical registers after path created.
    _MApi_XC_Mux_SetPath(pInstance, Path_Info->src,Path_Info->dest);
    if (MDrv_XC_OPTEE_IsAnyPipe_Active(pInstance))
    {
        XC_OPTEE_MUX_DATA st_xc_mux_data;
        memset(&st_xc_mux_data, 0, sizeof(XC_OPTEE_MUX_DATA));
        MDrv_XC_OPTEE_Mux(pInstance, E_XC_OPTEE_SET_MUX, &st_xc_mux_data);
    }

    // Initial _InputSource
    for (; u8Index < MAX_SYNC_DATA_PATH_SUPPORTED; u8Index++)
    {
        _InputSource[u8Index] = INPUT_SOURCE_NONE;
    }

    // Update ADC setting for multi-source.
    u8SourceCount = _MApi_XC_Mux_GetUsingSources(pInstance, _InputSource);

    XC_LOG_TRACE(XC_DBGLEVEL_MUX,"Total using source current %d \n",u8SourceCount);

    u8PathCount = _MApi_XC_Mux_GetPathInfo(pInstance, SC_Paths);
    for (  u8LoopCount = 0 ; u8LoopCount < u8PathCount ; ++u8LoopCount )
    {
        if( IsSrcTypeDigitalVD(SC_Paths[u8LoopCount].src) )
        {
            ++u8DigitalVDCount;
        }
    }
    XC_LOG_TRACE(XC_DBGLEVEL_MUX,"Total using source current =%d    Total using path count = %d \n",u8SourceCount , u8PathCount);
    if( u8DigitalVDCount >1)//Another window already connect to
    {
        XC_LOG_TRACE(XC_DBGLEVEL_MUX," Some window already connect  one of ATV/AV/Scart in , no need to set ADC source again ->  DigitalVD_count : %d  \n",u8DigitalVDCount);
    }
    else
    {
        MDrv_XC_ADC_SetInputSource(pInstance, _InputSource, u8SourceCount);
    }
    // Check if need calibration or not
    if(pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.u16SrcCreateTime[Path_Info->src] == 0)
    {
        if (u8DigitalVDCount > 1 )//Another already connect to AVD
        {
            XC_LOG_TRACE(XC_DBGLEVEL_MUX," Some window already connect  one of ATV/AV/Scart in so no need to do calibration again  \n");
        }
        else
        {
            // calibrate input source
            MDrv_XC_ADC_Source_Calibrate(pInstance, MDrv_XC_ADC_ConvertSrcToADCSrc(pInstance, Path_Info->src));

            if(MDrv_XC_IsFSSEnabled(pInstance, eWindow) == FALSE)
            {
                if(IsSrcTypeYPbPr(Path_Info->src))
                {
                    MDrv_XC_ADC_SetSoGCal(pInstance);
                    MsOS_DelayTask(10);
                    MDrv_XC_ADC_Set_Freerun(pInstance, TRUE);
                }
            }

            if (IsSrcTypeVga(Path_Info->src))
            {
                // ADC have to set as free run
                MDrv_XC_ADC_Set_Freerun(pInstance, TRUE);
            }
        }
    }

    // Increate src create time
    if(Path_Info->src < INPUT_SOURCE_NUM)
    {
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
        _Mdrv_XC_Mux_DecreaseSrcCreateTime(g_pDevice0Instance, Path_Info->src, TRUE);
        _Mdrv_XC_Mux_DecreaseSrcCreateTime(g_pDevice0Instance, Path_Info->src, TRUE);
#else
        _Mdrv_XC_Mux_DecreaseSrcCreateTime(pInstance, Path_Info->src, TRUE);
#endif
    }

    if(psXCInstPri->u32DeviceID == E_XC_DEVICE0)
    {
        //Enable Sub_Window CLK
        if(Path_Info->dest == OUTPUT_SCALER_SUB_WINDOW)
        {
            // Disable Pre-Scaling for ATV pink issue
            SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK04_05_L, 0x00 , BIT(15) );
            MApi_XC_EnableCLK_for_SUB(pInstance, ENABLE);
        }
        else if(Path_Info->dest == OUTPUT_SCALER_MAIN_WINDOW)
        {
            // Disable Pre-Scaling for ATV pink issue
            SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK02_05_L, 0x00 , BIT(15) );
        }
        else if(Path_Info->dest == OUTPUT_SCALER_DWIN)
        {
            // Enable DIP window clock
            MApi_XC_EnableCLK_for_DIP(pInstance, ENABLE);
        }
    }

    _XC_RETURN(pInstance);

    XC_LOG_TRACE(XC_DBGLEVEL_MUX,"=========== Path created success =========== \n");

#if (ENABLE_NONSTD_INPUT_MCNR == 1)
    if(IsSrcTypeATV(Path_Info->src) &&
      (!MApi_XC_InterruptIsAttached(XC_INT_VSINT, _MApi_XC_CVBSUnStdISR, &pXCResourcePrivate->stdrvXC_MVideo.u8UnStdOutput[MAIN_WINDOW])))
    {
        // Re-init state machine
        _MApi_XC_Sys_Detect_UnStd_Input_Status_Init();

        // Attach ISR
        MDrv_XC_InterruptAttach(pInstance, XC_INT_VSINT, _MApi_XC_CVBSUnStdISR, &pXCResourcePrivate->stdrvXC_MVideo.u8UnStdOutput[MAIN_WINDOW]);
    }
#endif

    // Inform CFD about Create Path
    if (MDrv_XC_IsFSSEnabled(pInstance, MAIN_WINDOW) == FALSE)
    {
        XC_CFD_CONTROL_INFO stCfdCtrl;
        ST_XC_MUX_INFO stXcMuxInfo;
        PQ_INPUT_SOURCE_TYPE enPqInputSourceType = PQ_INPUT_SOURCE_NONE;

        if(MDrv_XC_ConvertToPQInputSourceType(Path_Info->src, &enPqInputSourceType) == FALSE)
        {
            XC_LOG_TRACE(XC_DBGLEVEL_PARAMETER_TRACE, "[%s,%5d] Unexpected input source type = %d. \n", __FUNCTION__, __LINE__, Path_Info->src);
        }

        memset(&stXcMuxInfo, 0, sizeof(ST_XC_MUX_INFO));
        stXcMuxInfo.enAction = E_XC_CFD_MUX_CREATE;
        stXcMuxInfo.u8Win = (MS_U8) MAIN_WINDOW;
        stXcMuxInfo.u8InputSource = (MS_U8) enPqInputSourceType;

        memset(&stCfdCtrl, 0, sizeof(XC_CFD_CONTROL_INFO));
        stCfdCtrl.enCtrlType = E_XC_CFD_CTRL_MUX_INFO;
        stCfdCtrl.pParam = &stXcMuxInfo;
        stCfdCtrl.u32ParamLen = sizeof(ST_XC_MUX_INFO);
        MDrv_XC_HDR_Control(pInstance, E_XC_HDR_CTRL_CFD_CONTROL, &stCfdCtrl);
    }
    if(g_stXCQosCallBackInfo.pfnFunInputSourceInfo != NULL && Path_Info->src != NULL)
    {
        memset(&pXCResourcePrivate->stXCQosInputSourceInfo, 0, sizeof(ST_XC_QOS_INPUTSOURCE_INFO));
        pXCResourcePrivate->stXCQosInputSourceInfo.u32Version = ST_XC_QOS_INPUTSOURCE_INFO_VERSION;
        pXCResourcePrivate->stXCQosInputSourceInfo.u32Length = sizeof(ST_XC_QOS_INPUTSOURCE_INFO);
        pXCResourcePrivate->stXCQosInputSourceInfo.enXCQosEvntStatus = E_XC_QOS_EVENT_MUX_INFO;
        pXCResourcePrivate->stXCQosInputSourceInfo.enInputSourceType = Path_Info->src;
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        g_stXCQosCallBackInfo.pfnFunInputSourceInfo(&pXCResourcePrivate->stXCQosInputSourceInfo);
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR)
    }

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return s16PathId;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_S16 MApi_XC_Mux_CreatePath(XC_MUX_PATH_INFO* Path_Info , MS_U32 u32InitDataLen)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return -1;
    }

    stXC_SET_MUX_CREATE_PATH XCArgs;
    XCArgs.Path_Info = Path_Info;
    XCArgs.u32InitDataLen = u32InitDataLen;
    XCArgs.s16ReturnValue = -1;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_MUX_CREATE_PATH, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return -1;
    }
    else
    {
        MDrv_HDMI_CreatePathInternal(Path_Info->src);

        return XCArgs.s16ReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Map input source to VD Ymux port
/// @param  u8InputSourceType      \b IN: input source type
/// @return @ref MS_U8
//-------------------------------------------------------------------------------------------------
MS_U8 MApi_XC_MUX_MapInputSourceToVDYMuxPORT_U2( void* pInstance, INPUT_SOURCE_TYPE_t u8InputSourceType )
{
    MS_U8 u8Return = 0;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    u8Return = MDrv_XC_MUX_MapInputSourceToVDYMuxPORT(pInstance, u8InputSourceType );
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return u8Return;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_U8 MApi_XC_MUX_MapInputSourceToVDYMuxPORT( INPUT_SOURCE_TYPE_t u8InputSourceType )
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return 0;
    }

    stXC_GET_MUX_INPUTSOURCE2VDYMUXPORT XCArgs;
    XCArgs.u8InputSourceType = u8InputSourceType;
    XCArgs.u8ReturnValue = 0;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_MUX_INPUTSOURCE2VDYMUXPORT, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return 0;
    }
    else
    {
        return XCArgs.u8ReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Get Paths in driver.
/// @param  Paths      \b OUT: Path information
/// @return @ref MS_U8
//-------------------------------------------------------------------------------------------------
MS_U8 MApi_XC_Mux_GetPathInfo_U2(void* pInstance, XC_MUX_PATH_INFO* Paths)
{
    MS_S16 i;
    MS_U8 count;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    for ( i = 0, count = 0 ; i < MAX_SYNC_DATA_PATH_SUPPORTED ; i++)
    {
        if (pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].u8PathStatus & DATA_PATH_USING)
        {
            Paths[count].src = pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].src;
            Paths[count].dest = pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].dest;
#ifdef MSOS_TYPE_LINUX_KERNEL
            Paths[count].path_thread = NULL;
            Paths[count].SyncEventHandler =  NULL;
            Paths[count].DestOnOff_Event_Handler =  NULL;
            Paths[count].dest_periodic_handler =  NULL;
#else
            Paths[count].path_thread = pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].path_thread;
            Paths[count].SyncEventHandler =  pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].synchronous_event_handler;
            Paths[count].DestOnOff_Event_Handler =  pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].dest_on_off_event_handler;
            Paths[count].dest_periodic_handler =  pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].dest_periodic_handler;
#endif
            count++;
            //printf(" Path Id: [%d]  Source: %d   Destination: %d \n",i, s_InputSource_Mux_attr.Mux_DataPaths[i].src,
            //s_InputSource_Mux_attr.Mux_DataPaths[i].dest);
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
            if (Paths[count].dest == OUTPUT_SCALER2_MAIN_WINDOW)
            {
                Paths[count].dest = OUTPUT_SCALER_SUB_WINDOW;
            }
#endif
        }
    }
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return count;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_U8 MApi_XC_Mux_GetPathInfo(XC_MUX_PATH_INFO* Paths)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return 0;
    }

    stXC_GET_MUX_PATHINFO XCArgs;
    XCArgs.Paths = Paths;
    XCArgs.u8ReturnValue = 0;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_MUX_PATHINFO, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return 0;
    }
    else
    {
        return XCArgs.u8ReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Get XC library version
/// @param  ppVersion                  \b OUT: store the version in this member
/// @return @ref E_APIXC_ReturnValue
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_GetLibVer_U2(void* pInstance, const MSIF_Version **ppVersion)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    if (!ppVersion)
    {
        return E_APIXC_RET_FAIL;
    }

    *ppVersion = &_api_xc_version;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return E_APIXC_RET_OK;
}

E_APIXC_ReturnValue MApi_XC_GetLibVer(const MSIF_Version **ppVersion)
{
    //This function may be called before creating pu32XCInst
    if (!ppVersion)
    {
        return E_APIXC_RET_FAIL;
    }

    *ppVersion = &_api_xc_version;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return E_APIXC_RET_OK;
}

//-------------------------------------------------------------------------------------------------
/// Get XC Information
/// @return @ref XC_ApiInfo returnthe XC information in this member
//-------------------------------------------------------------------------------------------------
XC_ApiInfo * MApi_XC_GetInfo_U2(void* pInstance)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    MDrv_XC_GetInfo(pInstance, &pXCResourcePrivate->stdrvXC_MVideo._stXC_ApiInfo);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return &pXCResourcePrivate->stdrvXC_MVideo._stXC_ApiInfo;
}

const XC_ApiInfo * MApi_XC_GetInfo(void)
{
    stXC_CMD_GET_INFO XCArgs;
    static XC_ApiInfo Getinfo;

    XCArgs.stReturnValue = &Getinfo;

    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return XCArgs.stReturnValue;
    }

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_INFO, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return XCArgs.stReturnValue; // return meaningless value because get instance failed.
    }
    else
    {
        return XCArgs.stReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Extended interface for Getting XC Status of specific window(Main/Sub)
/// @param  pDrvStatusEx                  \b OUT: store the status
/// @param  enWindow                     \b IN: which window(Main/Sub) is going to get status
/// @return @ref MS_U16 return the copied length of input structure
//-------------------------------------------------------------------------------------------------

MS_U16 MDrv_XC_GetStatusEx(void *pInstance, XC_ApiStatusEx *pstDrvStatusEx, SCALER_WIN enWindow)
{
    XC_ApiStatusEx stDrvStatusEx;
    MS_U16 u16CopiedLength = sizeof(XC_ApiStatusEx);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    if((NULL == pstDrvStatusEx) || (pstDrvStatusEx->u16ApiStatusEX_Length == 0))
    {
        //We consider compatible operation from version2 , so reject the info init when version invalid
        XC_LOG_TRACE(XC_DBGLEVEL_SETTIMING, "MApi_XC_GetStatusEx: Null paramter or Wrong u16ApiStatusEX_Length!!\n")

        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return 0;
    }

    // the version control is coming in with version 1
    if(pstDrvStatusEx->u32ApiStatusEx_Version < 1)
    {
        //We consider compatible operation from version1 , so reject the info init when version invalid
        XC_LOG_TRACE(XC_DBGLEVEL_SETTIMING, "MApi_XC_GetStatusEx: please check your u32ApiStatusEx_Version, it should not set to 0!!\n")
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

        return 0;
    }
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    memset(&stDrvStatusEx, 0, sizeof(stDrvStatusEx));
    VERSION_COMPATIBLE_CHECK(pstDrvStatusEx, stDrvStatusEx, u16CopiedLength, u32ApiStatusEx_Version, u16ApiStatusEX_Length, API_STATUS_EX_VERSION, 0);

    stDrvStatusEx.u32ApiStatusEx_Version = API_STATUS_EX_VERSION; //Return the actual version of the returned value for APP to use
    stDrvStatusEx.u16ApiStatusEX_Length = u16CopiedLength; //Return the actual length of the returned value for APP to know whick info is valid

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    _XC_ENTRY(pInstance);
    //----------------------
    // Customer setting
    //----------------------
    stDrvStatusEx.enInputSourceType = gSrcInfo[enWindow].enInputSourceType;

    //----------------------
    // Window
    //----------------------
    memcpy(&stDrvStatusEx.stCapWin, &gSrcInfo[enWindow].stCapWin, sizeof(MS_WINDOW_TYPE));
    memcpy(&stDrvStatusEx.stCropWin, &gSrcInfo[enWindow].stCropWin, sizeof(MS_WINDOW_TYPE));
    /* real crop win in memory */
    memcpy(&stDrvStatusEx.ScaledCropWin, &gSrcInfo[enWindow].ScaledCropWin, sizeof(MS_WINDOW_TYPE));
    //Assign display window for AP layer
    memcpy(&stDrvStatusEx.stDispWin, &gSrcInfo[enWindow].stDispWin, sizeof(MS_WINDOW_TYPE));

    //stXCPanelDefaultTiming.u16HStart only be set to MAIN_WINDOW
    //Sub window do not set this value, use MAIN_WINDOW if set sub.
    if(stDrvStatusEx.stDispWin.x >= gSrcInfo[MAIN_WINDOW].Status2.stXCPanelDefaultTiming.u16HStart)
    {
        stDrvStatusEx.stDispWin.x -= gSrcInfo[MAIN_WINDOW].Status2.stXCPanelDefaultTiming.u16HStart;
    }
    if(stDrvStatusEx.stDispWin.y >= gSrcInfo[MAIN_WINDOW].Status2.stXCPanelDefaultTiming.u16VStart)
    {
        stDrvStatusEx.stDispWin.y -= gSrcInfo[MAIN_WINDOW].Status2.stXCPanelDefaultTiming.u16VStart;
    }

    if(MDrv_XC_Is2K2KToFrc(pInstance))
    {
        stDrvStatusEx.stDispWin.x = stDrvStatusEx.stDispWin.x * 2;
        stDrvStatusEx.stDispWin.width= stDrvStatusEx.stDispWin.width * 2;
    }

    //----------------------
    // Timing
    //----------------------
    stDrvStatusEx.bInterlace = gSrcInfo[enWindow].bInterlace;
    stDrvStatusEx.bHDuplicate = gSrcInfo[enWindow].bHDuplicate;
    stDrvStatusEx.u16InputVFreq = gSrcInfo[enWindow].u16InputVFreq;
    stDrvStatusEx.u16InputVTotal = gSrcInfo[enWindow].u16InputVTotal;
    stDrvStatusEx.u16DefaultHtotal = gSrcInfo[enWindow].u16DefaultHtotal;
    stDrvStatusEx.u8DefaultPhase = (MS_U8)gSrcInfo[enWindow].u16DefaultPhase;
    stDrvStatusEx.u16DefaultPhase = gSrcInfo[enWindow].u16DefaultPhase;

    //----------------------
    // customized scaling
    //----------------------
    stDrvStatusEx.bHCusScaling = gSrcInfo[enWindow].bHCusScaling;
    stDrvStatusEx.u16HCusScalingSrc = gSrcInfo[enWindow].u16HCusScalingSrc;
    stDrvStatusEx.u16HCusScalingDst = gSrcInfo[enWindow].u16HCusScalingDst;
    stDrvStatusEx.bVCusScaling = gSrcInfo[enWindow].bVCusScaling;
    stDrvStatusEx.u16VCusScalingSrc = gSrcInfo[enWindow].u16VCusScalingSrc;
    stDrvStatusEx.u16VCusScalingDst = gSrcInfo[enWindow].u16VCusScalingDst;

    stDrvStatusEx.bPreHCusScaling = gSrcInfo[enWindow].Status2.bPreHCusScaling;
    stDrvStatusEx.u16PreHCusScalingSrc = gSrcInfo[enWindow].Status2.u16PreHCusScalingSrc;
    stDrvStatusEx.u16PreHCusScalingDst = gSrcInfo[enWindow].Status2.u16PreHCusScalingDst;
    stDrvStatusEx.bPreVCusScaling = gSrcInfo[enWindow].Status2.bPreVCusScaling;
    stDrvStatusEx.u16PreVCusScalingSrc = gSrcInfo[enWindow].Status2.u16PreVCusScalingSrc;
    stDrvStatusEx.u16PreVCusScalingDst = gSrcInfo[enWindow].Status2.u16PreVCusScalingDst;

    //--------------
    // 9 lattice
    //--------------
    stDrvStatusEx.bDisplayNineLattice = gSrcInfo[enWindow].bDisplayNineLattice;

    //----------------------
    // XC internal setting
    //----------------------

    /* scaling ratio */
    stDrvStatusEx.u16H_SizeAfterPreScaling = gSrcInfo[enWindow].u16H_SizeAfterPreScaling;
    stDrvStatusEx.u16V_SizeAfterPreScaling = gSrcInfo[enWindow].u16V_SizeAfterPreScaling;
    stDrvStatusEx.bPreV_ScalingDown = gSrcInfo[enWindow].bPreV_ScalingDown;

    /* others */
    stDrvStatusEx.u32Op2DclkSet = gSrcInfo[enWindow].u32Op2DclkSet;

    /* Video screen status */
    stDrvStatusEx.bBlackscreenEnabled = gSrcInfo[enWindow].bBlackscreenEnabled;
    stDrvStatusEx.bBluescreenEnabled = gSrcInfo[enWindow].bBluescreenEnabled;
    stDrvStatusEx.u16VideoDark = gSrcInfo[enWindow].u16VideoDark;

    stDrvStatusEx.u16V_Length = gSrcInfo[enWindow].u16V_Length;
    stDrvStatusEx.u16BytePerWord = gSrcInfo[enWindow].u16BytePerWord;
    stDrvStatusEx.u16OffsetPixelAlignment = gSrcInfo[enWindow].u16OffsetPixelAlignment;
    stDrvStatusEx.u8BitPerPixel = gSrcInfo[enWindow].u8BitPerPixel;

    stDrvStatusEx.eDeInterlaceMode = gSrcInfo[enWindow].eDeInterlaceMode;
    stDrvStatusEx.u8DelayLines = gSrcInfo[enWindow].u8DelayLines;
    stDrvStatusEx.bMemFmt422 = gSrcInfo[enWindow].bMemFmt422;
    stDrvStatusEx.eMemory_FMT = gSrcInfo[enWindow].eMemory_FMT;

    stDrvStatusEx.bForceNRoff = gSrcInfo[enWindow].bForceNRoff;
    stDrvStatusEx.bEn3DNR = gSrcInfo[enWindow].bEn3DNR;
    stDrvStatusEx.bUseYUVSpace = gSrcInfo[enWindow].bUseYUVSpace;
    stDrvStatusEx.bMemYUVFmt = gSrcInfo[enWindow].bMemYUVFmt;
    stDrvStatusEx.bForceRGBin = gSrcInfo[enWindow].bForceRGBin;
    stDrvStatusEx.bLinearMode = gSrcInfo[enWindow].bLinearMode;

    // frame lock related
    // only main can select FB/FBL because panel output timing depends on main window, sub will always use FB
    stDrvStatusEx.bFBL = gSrcInfo[enWindow].bFBL;
    stDrvStatusEx.bFastFrameLock = gSrcInfo[enWindow].bFastFrameLock;
    stDrvStatusEx.bDoneFPLL = MDrv_Scaler_GetFPLLDoneStatus(pInstance);
    stDrvStatusEx.bEnableFPLL = gSrcInfo[enWindow].bEnableFPLL;
    stDrvStatusEx.bFPLL_LOCK = gSrcInfo[enWindow].bFPLL_LOCK;

    // Capture_Memory
    stDrvStatusEx.u32IPMBase0 = gSrcInfo[enWindow].u32IPMBase0;
    stDrvStatusEx.u32IPMBase1 = gSrcInfo[enWindow].u32IPMBase1;
    stDrvStatusEx.u32IPMBase2 = gSrcInfo[enWindow].u32IPMBase2;
    stDrvStatusEx.u16IPMOffset = gSrcInfo[enWindow].u16IPMOffset;
    stDrvStatusEx.u16IPMFetch = gSrcInfo[enWindow].u16IPMFetch;

    //HSizeChangedBeyondPQ status
    stDrvStatusEx.bPQSetHSD = !(gSrcInfo[enWindow].bHCusScaling || gSrcInfo[enWindow].Status2.bPreHCusScaling
                                || MDrv_XC_GetHSizeChangeManuallyFlag(pInstance, enWindow));
    E_XC_3D_INPUT_MODE e3DInput = MDrv_XC_Get_3D_Input_Mode(pInstance, enWindow);
    E_XC_3D_OUTPUT_MODE e3DOutput = MDrv_XC_Get_3D_Output_Mode(pInstance);

    stDrvStatusEx.bIsHWDepthAdjSupported = MDrv_XC_3D_IsHWDepthAdjSupported(pInstance, e3DInput,
                                                                          e3DOutput,
                                                                          enWindow);
    stDrvStatusEx.bIs2LineMode = MDrv_SC_3D_Is2TapModeSupportedFormat(pInstance, &gSrcInfo[enWindow], enWindow);

    stDrvStatusEx.bIsPNLYUVOutput = MDrv_XC_IsPNLYUVOutput(pInstance);

    //if inputSource is HDMI, get the HDMI pixel repetition info.
    if(IsSrcTypeHDMI(stDrvStatusEx.enInputSourceType))
    {
        stDrvStatusEx.u8HDMIPixelRepetition = MDrv_HDMI_avi_infoframe_info(_BYTE_5) & 0x0F;
    }
    else
    {
        stDrvStatusEx.u8HDMIPixelRepetition = 0;
    }

    // FRCM memory info
    stDrvStatusEx.u32FRCMBase0 = MDrv_XC_Get_FRCM_W_BaseAddr(pInstance,0,enWindow);
    stDrvStatusEx.u32FRCMBase1 = MDrv_XC_Get_FRCM_W_BaseAddr(pInstance,1,enWindow);
    stDrvStatusEx.u32FRCMBase2 = MDrv_XC_Get_FRCM_W_BaseAddr(pInstance,2,enWindow);

    #ifdef UFO_XC_ZORDER
    //VIDEO and the relative position of OSD.
    if(E_APIXC_RET_OK != MDrv_SC_GetVideoOnOSD(pInstance, &(stDrvStatusEx.enZOrderIndex), enWindow) )
    {
        printf("%s, %d, MDrv_SC_GetVideoOnOSD fail!\n", __FUNCTION__, __LINE__);
    }
    #endif

    // FSC && FRC settings
    #if FRC_INSIDE
        #if (HW_DESIGN_4K2K_VER == 6) // For Manhattan, FSC+FRC
            stDrvStatusEx.bFSCEnabled = pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFSCEnabled;
            stDrvStatusEx.bFRCEnabled = pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFRCEnabled;
        #else
            stDrvStatusEx.bFSCEnabled = FALSE;
            stDrvStatusEx.bFRCEnabled = FALSE;
        #endif
    #else
        stDrvStatusEx.bFSCEnabled = FALSE;
        stDrvStatusEx.bFRCEnabled = FALSE;
    #endif
    stDrvStatusEx.u16PanelInterfaceType= (MS_U16)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type;

    stDrvStatusEx.bIsHskMode = (SC_R2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK02_10_L,BMASK(7:0)) == 0x25)? TRUE: FALSE;
    stDrvStatusEx.bEnable1pTo2pBypass = (SC_R2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK02_0A_L) & BIT(15))? TRUE: FALSE;
    if(enWindow==MAIN_WINDOW)
    {
        stDrvStatusEx.u16OPMOffset = SC_R2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_16_L,BMASK(13:0));
        stDrvStatusEx.u16OPMFetch = SC_R2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_17_L,BMASK(13:0));
     }
     else if (enWindow==SUB_WINDOW)
     {
         stDrvStatusEx.u16OPMOffset = SC_R2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_56_L,BMASK(13:0));
         stDrvStatusEx.u16OPMFetch = SC_R2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_57_L,BMASK(13:0));
     }
     else
     {
         stDrvStatusEx.u16OPMOffset = SC_R2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_16_L,BMASK(13:0));
         stDrvStatusEx.u16OPMFetch = SC_R2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK12_17_L,BMASK(13:0));
     }

    stDrvStatusEx.u16HPostScalingSrc = gSrcInfo[enWindow].Status2.u16HPostScalingSrc;
    stDrvStatusEx.u16HPostScalingDst = gSrcInfo[enWindow].Status2.u16HPostScalingDst;
#if defined(UFO_XC_HDR) && defined(UFO_XC_HDR_VERSION) && (UFO_XC_HDR_VERSION == 2)
    if (pstDrvStatusEx->u32ApiStatusEx_Version >= 12)
    {
        ST_KDRV_XC_CFD_STATUS stCfdStatus;
        ST_XC_CFD_POINT_INFO *pstCfdPointInfo = pstDrvStatusEx->pstCfdPointInfo;
        ST_KDRV_XC_HDR_CFD_POINT_INFO stCfdPointInfo[E_KDRV_XC_CFD_POINT_MAX];
        memset(&stCfdStatus, 0, sizeof(ST_KDRV_XC_CFD_STATUS));

        if (pstCfdPointInfo != NULL)
        {
            MS_U8 u8Count = 0;
            MS_U16 u16PointInfoSize = sizeof(ST_KDRV_XC_HDR_CFD_POINT_INFO);
            MS_U8 u8PointNum = 0;

            VERSION_COMPATIBLE_CHECK(pstCfdPointInfo, ST_XC_CFD_POINT_INFO, u16PointInfoSize, u32Version, u32Length, ST_XC_CFD_POINT_INFO_VERSION, E_APIXC_RET_FAIL);

            if (u16PointInfoSize != 0)
            {
                u8PointNum = (pstDrvStatusEx->u16DataSize) / u16PointInfoSize;
            }

            stCfdPointInfo[E_KDRV_XC_CFD_POINT_IP1_INPUT].u32Version = ST_KDRV_XC_HDR_CFD_POINT_INFOR_VERSION;
            stCfdPointInfo[E_KDRV_XC_CFD_POINT_IP1_INPUT].u32Length = sizeof(ST_KDRV_XC_HDR_CFD_POINT_INFO);
            stCfdStatus.u32Version = ST_KDRV_XC_CFD_STATUS_VERSION;
            stCfdStatus.u16Length = sizeof(ST_KDRV_XC_CFD_STATUS);
            stCfdStatus.u8Win = enWindow;
            stCfdStatus.pstPointInfo = stCfdPointInfo;
            stCfdStatus.u16DataSize = sizeof(ST_KDRV_XC_HDR_CFD_POINT_INFO) * E_KDRV_XC_CFD_POINT_MAX;

            ST_KDRV_XC_CFD_CONTROL_INFO stXCCfdControlInfo;
            memset(&stXCCfdControlInfo, 0, sizeof(ST_KDRV_XC_CFD_CONTROL_INFO));
            stXCCfdControlInfo.enCtrlType = E_KDRV_XC_CFD_CTRL_GET_STATUS;
            stXCCfdControlInfo.pParam = &stCfdStatus;
            stXCCfdControlInfo.u32ParamLen = sizeof(ST_KDRV_XC_CFD_STATUS);
#ifdef UFO_XC_HDR
#if (UFO_XC_HDR_VERSION == 2)
#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
            MS_S32 s32Result = 0;
            s32Result = ioctl(_s32FdScaler, MDRV_XC_IOC_CFDCONCTRL, &stXCCfdControlInfo);
            if (s32Result)
            {
                printf("MDRV_XC_IOC_CFDCONCTRL fail!!!! iResult is "MPRI_DEC"\n", s32Result);
                _XC_RETURN(pInstance);
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                return E_APIXC_RET_FAIL;
            }
#endif
#endif
#endif

#if (defined(MSOS_TYPE_LINUX_KERNEL) && defined(CONFIG_MSTAR_XC_HDR_SUPPORT))
            if (MDrv_XC_CFDControl(&stXCCfdControlInfo) != TRUE)
            {
                _XC_RETURN(pInstance);
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                return E_APIXC_RET_FAIL;
            }
#endif

            for (u8Count=0; (u8Count<u8PointNum) && (u8Count<E_KDRV_XC_CFD_POINT_MAX); u8Count++)
            {
                /// when struct ST_XC_CFD_POINT_INFO add new member, it need to add version control and new member
                ST_XC_CFD_POINT_INFO *pstCfdSourcePointInfo = (ST_XC_CFD_POINT_INFO *)(((MS_U8 *)pstCfdPointInfo) + (u8Count * pstCfdPointInfo->u32Length));
                pstCfdSourcePointInfo->u8Format = stCfdPointInfo[u8Count].u8Format;
                pstCfdSourcePointInfo->u8DataFormat = stCfdPointInfo[u8Count].u8DataFormat;
                pstCfdSourcePointInfo->u8IsFullRange = stCfdPointInfo[u8Count].u8IsFullRange;
                pstCfdSourcePointInfo->u8ColorPriamries = stCfdPointInfo[u8Count].u8ColorPriamries;
                pstCfdSourcePointInfo->u8TransferCharacterstics = stCfdPointInfo[u8Count].u8TransferCharacterstics;
                pstCfdSourcePointInfo->u8MatrixCoefficients = stCfdPointInfo[u8Count].u8MatrixCoefficients;
            }
            stDrvStatusEx.pstCfdPointInfo = pstCfdPointInfo;
        }
    }
#endif
    Hal_SC_get_disp_de_window(pInstance, &stDrvStatusEx.stDispDEWin);

    //Get HFR path enable status
    stDrvStatusEx.bIsHFRPathEnable = MHal_XC_Is_Enable_HFR_Path(pInstance, enWindow);
    //get lock point for zero frame delay mode
    MS_U16 u16ConvertToPanelDE = 0;
    MS_U16 u16NoDelayLockPoint = 0;
    MS_U16 u16SignalVblank = 0;

    u16SignalVblank= WINDOWST.u16InputVTotal-WINDOWST.stCapWin.height; // 1125 - 1080 = 45

    u16ConvertToPanelDE = (WINDOWST.stCropWin.y+u16SignalVblank)*PanelVTT/ WINDOWST.stCapWin.height;  // (0+45)*2260/1080 = 94
    u16NoDelayLockPoint = PanelVTT - (u16ConvertToPanelDE + NoDelayLockPointDelta + NoDelayLockPointAfa);   //2260-(94+100+20)=2046
    if(pstDrvStatusEx->u32ApiStatusEx_Version >= 14)
    {
        if(MDrv_XC_GetNoDelayMode(pInstance))
        {
            stDrvStatusEx.u16LockPointAuto = u16NoDelayLockPoint;
        }
    }

    if(pstDrvStatusEx->u32ApiStatusEx_Version >= 16)
    {
        stDrvStatusEx.bPreH_ScalingDown = gSrcInfo[enWindow].bPreH_ScalingDown;
    }

    //get Vx1 16 lane status
    stDrvStatusEx.bIs120HzOutput = (IsVBY1_16LANE((MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type)) ? TRUE : FALSE;

#if FRC_INSIDE
    stDrvStatusEx.u16MemcLevelStatus = pXCResourcePrivate->sthal_FRC.g_u16MemcLevelStatus;
#endif

    if(pstDrvStatusEx->u32ApiStatusEx_Version >= 17)
    {
        stDrvStatusEx.bEnableIREPattern = gSrcInfo[enWindow].Status2.bEnableIREPattern;
    }

    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    //shorter Ap structure with longer lib structure, we should handle it to avoid access-violation.
    memcpy(pstDrvStatusEx, &stDrvStatusEx, u16CopiedLength);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return u16CopiedLength;
}

MS_U16 MApi_XC_GetStatusEx_U2(void* pInstance, XC_ApiStatusEx *pstDrvStatusEx, SCALER_WIN enWindow)
{
    MS_U16 u16Return = 0;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    u16Return = MDrv_XC_GetStatusEx(pInstance, pstDrvStatusEx, enWindow);
    return u16Return;
}

MS_U16 MApi_XC_GetStatusEx(XC_ApiStatusEx *pDrvStatusEx, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return 0;
    }

    stXC_GET_STATUS_EX XCArgs;
    XCArgs.pDrvStatusEx = pDrvStatusEx;
    XCArgs.eWindow = eWindow;
    XCArgs.u16ReturnValue = 0;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_STATUS_EX, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return 0;
    }
    else
    {
        return XCArgs.u16ReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Get XC Status of specific window(Main/Sub)
/// @param  pDrvStatus                  \b OUT: store the status
/// @param  eWindow                     \b IN: which window(Main/Sub) is going to get status
/// @return @ref MS_BOOL
//-------------------------------------------------------------------------------------------------
MS_BOOL MDrv_XC_GetStatus(void *pInstance, XC_ApiStatus *pDrvStatus, SCALER_WIN eWindow)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    //----------------------
    // Customer setting
    //----------------------
#ifndef MSOS_TYPE_NUTTX
    pDrvStatus->enInputSourceType = gSrcInfo[eWindow].enInputSourceType;
#else
    MS_U8 u8InputSourceData = 0;
    MS_U8 u8InputSoureceClk = 0;
    MS_U16 u16InputSourceType = 0;
    Hal_SC_mux_get_mainwin_ip_mux(pInstance,&u8InputSourceData,&u8InputSoureceClk);
    if(u8InputSourceData == SC_MAINWIN_IPMUX_VD)
    {
            // scart/av/atv
            u16InputSourceType = INPUT_SOURCE_CVBS;
    }
    else if(u8InputSourceData == SC_MAINWIN_IPMUX_HDMI_DVI)
    {
            // hdmi
            u16InputSourceType = INPUT_SOURCE_HDMI;
    }
    else if(u8InputSourceData == SC_MAINWIN_IPMUX_MVOP)
    {
            // dtv/MM
            u16InputSourceType = INPUT_SOURCE_DTV;
    }
    else if(u8InputSourceData == SC_MAINWIN_IPMUX_ADC_A)
    {
            // Component/pc-rbg
            u16InputSourceType = INPUT_SOURCE_YPBPR;
    }
    else
    {
    }
    pDrvStatus->enInputSourceType = u16InputSourceType;
#endif

#ifndef MSOS_TYPE_NUTTX
    //----------------------
    // Window
    //----------------------
    memcpy(&pDrvStatus->stCapWin, &gSrcInfo[eWindow].stCapWin, sizeof(MS_WINDOW_TYPE));
    memcpy(&pDrvStatus->stCropWin, &gSrcInfo[eWindow].stCropWin, sizeof(MS_WINDOW_TYPE));
    /* real crop win in memory */
    memcpy(&pDrvStatus->ScaledCropWin, &gSrcInfo[eWindow].ScaledCropWin, sizeof(MS_WINDOW_TYPE));
    //Assign display window for AP layer
    memcpy(&pDrvStatus->stDispWin, &gSrcInfo[eWindow].stDispWin, sizeof(MS_WINDOW_TYPE));

    //stXCPanelDefaultTiming.u16HStart only be set to MAIN_WINDOW
    //Sub window do not set this value, use MAIN_WINDOW if set sub.
    if(pDrvStatus->stDispWin.x >= gSrcInfo[MAIN_WINDOW].Status2.stXCPanelDefaultTiming.u16HStart)
    {
        pDrvStatus->stDispWin.x -= gSrcInfo[MAIN_WINDOW].Status2.stXCPanelDefaultTiming.u16HStart;
    }
    if(pDrvStatus->stDispWin.y >= gSrcInfo[MAIN_WINDOW].Status2.stXCPanelDefaultTiming.u16VStart)
    {
        pDrvStatus->stDispWin.y -= gSrcInfo[MAIN_WINDOW].Status2.stXCPanelDefaultTiming.u16VStart;
    }

    //----------------------
    // Timing
    //----------------------
    pDrvStatus->bInterlace = gSrcInfo[eWindow].bInterlace;
    pDrvStatus->bHDuplicate = gSrcInfo[eWindow].bHDuplicate;
    pDrvStatus->u16InputVFreq = gSrcInfo[eWindow].u16InputVFreq;
    pDrvStatus->u16InputVTotal = gSrcInfo[eWindow].u16InputVTotal;
    pDrvStatus->u16DefaultHtotal = gSrcInfo[eWindow].u16DefaultHtotal;
    pDrvStatus->u8DefaultPhase = (MS_U8)gSrcInfo[eWindow].u16DefaultPhase;

    //----------------------
    // customized scaling
    //----------------------
    pDrvStatus->bHCusScaling = gSrcInfo[eWindow].bHCusScaling;
    pDrvStatus->u16HCusScalingSrc = gSrcInfo[eWindow].u16HCusScalingSrc;
    pDrvStatus->u16HCusScalingDst = gSrcInfo[eWindow].u16HCusScalingDst;
    pDrvStatus->bVCusScaling = gSrcInfo[eWindow].bVCusScaling;
    pDrvStatus->u16VCusScalingSrc = gSrcInfo[eWindow].u16VCusScalingSrc;
    pDrvStatus->u16VCusScalingDst = gSrcInfo[eWindow].u16VCusScalingDst;

    //--------------
    // 9 lattice
    //--------------
    pDrvStatus->bDisplayNineLattice = gSrcInfo[eWindow].bDisplayNineLattice;

    //----------------------
    // XC internal setting
    //----------------------

    /* scaling ratio */
    pDrvStatus->u16H_SizeAfterPreScaling = gSrcInfo[eWindow].u16H_SizeAfterPreScaling;
    pDrvStatus->u16V_SizeAfterPreScaling = gSrcInfo[eWindow].u16V_SizeAfterPreScaling;
    pDrvStatus->bPreV_ScalingDown = gSrcInfo[eWindow].bPreV_ScalingDown;


    /* others */
    pDrvStatus->u32Op2DclkSet = gSrcInfo[eWindow].u32Op2DclkSet;

    /* Video screen status */
    pDrvStatus->bBlackscreenEnabled = gSrcInfo[eWindow].bBlackscreenEnabled;
    pDrvStatus->bBluescreenEnabled = gSrcInfo[eWindow].bBluescreenEnabled;
    pDrvStatus->u16VideoDark = gSrcInfo[eWindow].u16VideoDark;

    pDrvStatus->u16V_Length = gSrcInfo[eWindow].u16V_Length;
    pDrvStatus->u16BytePerWord = gSrcInfo[eWindow].u16BytePerWord;
    pDrvStatus->u16OffsetPixelAlignment = gSrcInfo[eWindow].u16OffsetPixelAlignment;
    pDrvStatus->u8BitPerPixel = gSrcInfo[eWindow].u8BitPerPixel;

    pDrvStatus->eDeInterlaceMode = gSrcInfo[eWindow].eDeInterlaceMode;
    pDrvStatus->u8DelayLines = gSrcInfo[eWindow].u8DelayLines;
    pDrvStatus->bMemFmt422 = gSrcInfo[eWindow].bMemFmt422;
    pDrvStatus->eMemory_FMT = gSrcInfo[eWindow].eMemory_FMT;

    pDrvStatus->bForceNRoff = gSrcInfo[eWindow].bForceNRoff;
    pDrvStatus->bEn3DNR = gSrcInfo[eWindow].bEn3DNR;
    pDrvStatus->bUseYUVSpace = gSrcInfo[eWindow].bUseYUVSpace;
    pDrvStatus->bMemYUVFmt = gSrcInfo[eWindow].bMemYUVFmt;
    pDrvStatus->bForceRGBin = gSrcInfo[eWindow].bForceRGBin;
    pDrvStatus->bLinearMode = gSrcInfo[eWindow].bLinearMode;

    // frame lock related
    // only main can select FB/FBL because panel output timing depends on main window, sub will always use FB
    pDrvStatus->bFBL = gSrcInfo[eWindow].bFBL;
    pDrvStatus->bFastFrameLock = gSrcInfo[eWindow].bFastFrameLock;
    pDrvStatus->bDoneFPLL = MDrv_Scaler_GetFPLLDoneStatus(pInstance);
    pDrvStatus->bEnableFPLL = gSrcInfo[eWindow].bEnableFPLL;
    pDrvStatus->bFPLL_LOCK = gSrcInfo[eWindow].bFPLL_LOCK;

    // Capture_Memory
    pDrvStatus->u32IPMBase0 = gSrcInfo[eWindow].u32IPMBase0;
    pDrvStatus->u32IPMBase1 = gSrcInfo[eWindow].u32IPMBase1;
    pDrvStatus->u32IPMBase2 = gSrcInfo[eWindow].u32IPMBase2;
    pDrvStatus->u16IPMOffset = gSrcInfo[eWindow].u16IPMOffset;
    pDrvStatus->u16IPMFetch = gSrcInfo[eWindow].u16IPMFetch;

#ifdef STELLAR
    pDrvStatus->u16OutputVFreqAfterFRC =gSrcInfo[eWindow].Status2.u16OutputVFreqAfterFRC;
#endif

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
#endif
    return TRUE;
}
MS_BOOL MApi_XC_GetStatus_U2(void* pInstance, XC_ApiStatus *pDrvStatus, SCALER_WIN eWindow)
{
    MS_BOOL bReturn = FALSE;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);
    bReturn = MDrv_XC_GetStatus(pInstance, pDrvStatus, eWindow);
#ifndef MSOS_TYPE_NUTTX
    if(MDrv_XC_Is2K2KToFrc(pInstance))
    {
        pDrvStatus->stDispWin.x = pDrvStatus->stDispWin.x * 2;
        pDrvStatus->stDispWin.width= pDrvStatus->stDispWin.width * 2;
    }
#endif
    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return bReturn;
}

MS_BOOL MApi_XC_GetStatus(XC_ApiStatus *pDrvStatus, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    stXC_GET_STATUS XCArgs;
    XCArgs.pDrvStatus = pDrvStatus;
    XCArgs.eWindow = eWindow;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_STATUS, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#ifndef MSOS_TYPE_NUTTX
MS_BOOL MApi_XC_GetStatusNodelay_U2(void* pInstance, ST_XC_APISTATUSNODELAY *pDrvStatus, SCALER_WIN eWindow)
{
    MS_U16 u16CopiedLength = sizeof(ST_XC_APISTATUSNODELAY);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
#if 0
    if((NULL == pDrvStatus) || (pDrvStatus->u16ApiStatusEX_Length == 0))
    {
        //We consider compatible operation from version2 , so reject the info init when version invalid
        XC_LOG_TRACE(XC_DBGLEVEL_SETTIMING, "MApi_XC_GetStatusEx: Null paramter or Wrong u16ApiStatusEX_Length!!\n")

        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return 0;
    }

    // the version control is coming in with version 1
    if(pDrvStatus->u32ApiStatusEx_Version < 1)
    {
        //We consider compatible operation from version1 , so reject the info init when version invalid
        XC_LOG_TRACE(XC_DBGLEVEL_SETTIMING, "MApi_XC_GetStatusEx: please check your u32ApiStatusEx_Version, it should not set to 0!!\n")
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

        return 0;
    }
#endif
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    //----------------------
    // Customer setting
    //----------------------
    pDrvStatus->enInputSourceType = gSrcInfo[eWindow].stStatusnodelay.enInputSourceType;

    //----------------------
    // Window
    //----------------------
    //Assign display window for AP layer
    memcpy(&pDrvStatus->stDispWin, &gSrcInfo[eWindow].stStatusnodelay.stDispWin, sizeof(MS_WINDOW_TYPE));

    if(pDrvStatus->stDispWin.x >= gSrcInfo[eWindow].Status2.stXCPanelDefaultTiming.u16HStart)
    {
        pDrvStatus->stDispWin.x -= gSrcInfo[eWindow].Status2.stXCPanelDefaultTiming.u16HStart;
    }
    if(pDrvStatus->stDispWin.y >= gSrcInfo[eWindow].Status2.stXCPanelDefaultTiming.u16VStart)
    {
        pDrvStatus->stDispWin.y -= gSrcInfo[eWindow].Status2.stXCPanelDefaultTiming.u16VStart;
    }
    /* Video screen status */
    pDrvStatus->bBlackscreenEnabled = gSrcInfo[eWindow].bBlackscreenEnabled;

    //pixel shift H/V offset
    pDrvStatus->s8HOffset = pXCResourcePrivate->stdrvXC_MVideo.s8HPixelShift;
    pDrvStatus->s8VOffset = pXCResourcePrivate->stdrvXC_MVideo.s8VPixelShift;

    // op size
    if(pDrvStatus->u32ApiStatusEx_Version >= 1)
    {
        pDrvStatus->u32OPSizeH = gSrcInfo[eWindow].ScaledCropWin.width;
        pDrvStatus->u32OPSizeV = gSrcInfo[eWindow].ScaledCropWin.height;
    }

    // read/write diff
    if(pDrvStatus->u32ApiStatusEx_Version >= 2)
    {
        pDrvStatus->u8UCMRwDiff = MHal_XC_GetUCMRwDiff(pInstance);
        pDrvStatus->u8FRCRwDiff = MHal_FRC_GetRwDiff(pInstance);
    }

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return u16CopiedLength;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_GetStatusNodelay(ST_XC_APISTATUSNODELAY *pDrvStatus, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    ST_XC_GET_STATUS_NODELAY XCArgs;
    XCArgs.pDrvStatus = pDrvStatus;
    XCArgs.eWindow = eWindow;
    XCArgs.u16ReturnValue = 0;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_STATUS_NODELAY, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.u16ReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Set XC debug level
/// @param  u16DbgSwitch                \b IN: turn on/off debug switch
//-------------------------------------------------------------------------------------------------
MS_BOOL MApi_XC_SetDbgLevel_U2(void* pInstance, MS_U16 u16DbgSwitch)
{
    _u16XCDbgSwitch_InternalUseOnly = u16DbgSwitch;

    return TRUE;
}
MS_BOOL MApi_XC_SetDbgLevel(MS_U16 u16DbgSwitch)
{
    XC_INSTANT_ATTRIBUTE stXCInstantAttribute;
    INIT_XC_INSTANT_ATTR(stXCInstantAttribute);

    CheckXCInstanceOpen();

#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
     if (pu32XCInst_1 == NULL)
     {
        stXCInstantAttribute.u32DeviceID = 1;
        if(UtopiaOpen(MODULE_XC, &pu32XCInst_1, 0, &stXCInstantAttribute) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return E_APIXC_RET_FAIL;
        }
      }
#endif
    stXC_SET_DBG_LEVEL XCArgs;
    XCArgs.u16DbgSwitch = u16DbgSwitch;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_DBG_LEVEL, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
//-------------------------------------------------------------------------------------------------
/// Get scaler output vertical frequency
/// @return @ref MS_U16 return output vertical frequency x 100
//-------------------------------------------------------------------------------------------------
MS_U16 MApi_XC_GetOutputVFreqX100_U2(void* pInstance)
{
    MS_U16 u16OutputVfreqX100;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    u16OutputVfreqX100 = MDrv_SC_GetOutputVFreqX100(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return u16OutputVfreqX100;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_U16 MApi_XC_GetOutputVFreqX100(void)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return 0;
    }

    stXC_GET_OUTPUT_VFREQX100 XCArgs;
    XCArgs.u16ReturnValue = 0;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_OUTPUT_VFREQX100, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return 0;
    }
    else
    {
        return XCArgs.u16ReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Get OP1 output vertical frequency
/// @return @ref MS_U16 return output vertical frequency x 100
//-------------------------------------------------------------------------------------------------
MS_U16 MApi_XC_GetOP1OutputVFreqX100_U2(void* pInstance)
{
    MS_U16 u16OutputVfreqX100;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    u16OutputVfreqX100 = MDrv_SC_GetOP1OutputVFreqX100(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return u16OutputVfreqX100;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_U16 MApi_XC_GetOP1OutputVFreqX100(void)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return 0;
    }

    stXC_GET_OP1_OUTPUT_VFREQX100 XCArgs;
    XCArgs.u16ReturnValue = 0;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_OP1_OUTPUT_VFREQX100, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return 0;
    }
    else
    {
        return XCArgs.u16ReturnValue;
    }
}
#endif

/// set SC1 CaptureMode on/off
/// @param bEnable              \b IN: enable/disable SC1 capture mode
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_EnableCaptureMode(MS_BOOL bEnable)
{
    void *pInstance = pu32XCInst_1_private;

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);
    pXCResourcePrivate->stdrvXC_MVideo.bCaptureMode = bEnable;
    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    return E_APIXC_RET_OK;
}

//-------------------------------------------------------------------------------------------------
/// set NR on/off
/// @param bEn              \b IN: enable/disable NR
/// @param eWindow          \b IN: which window(Main/Sub) is going to get status
//-------------------------------------------------------------------------------------------------
void MApi_XC_Set_NR_U2(void* pInstance, MS_BOOL bEn, SCALER_WIN eWindow)
{
#if TEST_NEW_DYNAMIC_NR
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);
    MDrv_Scaler_EnableNR(pInstance, bEn, eWindow);
    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
#endif
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_Set_NR(MS_BOOL bEn, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_NR XCArgs;
    XCArgs.bEn = bEn;
    XCArgs.eWindow = eWindow;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_NR, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
#ifndef MSOS_TYPE_LINUX_KERNEL
extern __attribute__((weak)) void MDrv_PQ_DesideSrcType(PQ_WIN eWindow, PQ_INPUT_SOURCE_TYPE enInputSourceType);
extern __attribute__((weak)) void MDrv_PQ_DisableFilmMode(PQ_WIN eWindow, MS_BOOL bOn);
extern __attribute__((weak)) MS_BOOL 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);
extern __attribute__((weak)) void MDrv_PQ_Set420upsampling(PQ_WIN eWindow,
                                                            MS_BOOL bFBL,
                                                            MS_BOOL bPreV_ScalingDown,
                                                            MS_U16 u16V_CropStart);
extern __attribute__((weak)) MS_BOOL MDrv_PQ_SetCSC(PQ_WIN eWindow, PQ_FOURCE_COLOR_FMT enForceColor);
extern __attribute__((weak)) MS_BOOL MDrv_PQ_Get_MemYUVFmt(PQ_WIN eWindow, PQ_FOURCE_COLOR_FMT enForceColor);
extern __attribute__((weak)) PQ_DEINTERLACE_MODE MDrv_PQ_SetMemFormat(PQ_WIN eWindow, MS_BOOL bMemFmt422, MS_BOOL bFBL, MS_U8 *pu8BitsPerPixel);
extern __attribute__((weak)) void MDrv_PQ_Set_ModeInfo(PQ_WIN eWindow, PQ_INPUT_SOURCE_TYPE enInputSourceType, MS_PQ_Mode_Info *pstPQModeInfo);
extern __attribute__((weak)) MS_BOOL MDrv_PQ_IOCTL(PQ_WIN eWindow, MS_U32 u32Flag, void *pBuf, MS_U32 u32BufSize);
#endif
//-------------------------------------------------------------------------------------------------
/// Create a function for PQ in SC.
/// @param  PQ_Function_Info               \b IN: the information of the function
/// @param  u32InitDataLen          \b IN: the length of the PQ_Function_Info
//-------------------------------------------------------------------------------------------------
void MApi_XC_PQ_LoadFunction_U2(void* pInstance, PQ_Function_Info* function_Info , MS_U32 u32InitDataLen)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

#if (HW_DESIGN_4K2K_VER == 7)
//Kano sc1 do not load pq function now
    if(psXCInstPri->u32DeviceID == 1)
    {
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_deside_srctype = NULL;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_disable_filmmode = NULL;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_load_scalingtable = NULL;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_420upsampling = NULL;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_csc = NULL;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_get_memyuvfmt= NULL;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_memformat = NULL;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_modeInfo = NULL;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_ioctl = NULL;
        return;
    }
#endif

        // Path create.
#ifdef MSOS_TYPE_LINUX_KERNEL
        UNUSED(function_Info);
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_deside_srctype = MDrv_PQ_DesideSrcType;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_disable_filmmode = MDrv_PQ_DisableFilmMode;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_load_scalingtable = MDrv_PQ_LoadScalingTable;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_420upsampling = MDrv_PQ_Set420upsampling;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_csc = MDrv_PQ_SetCSC;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_get_memyuvfmt= MDrv_PQ_Get_MemYUVFmt;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_memformat = MDrv_PQ_SetMemFormat;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_modeInfo = MDrv_PQ_Set_ModeInfo;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_ioctl = MDrv_PQ_IOCTL;
#else
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_deside_srctype = function_Info->pq_deside_srctype;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_disable_filmmode = function_Info->pq_disable_filmmode;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_load_scalingtable = function_Info->pq_load_scalingtable;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_420upsampling = function_Info->pq_set_420upsampling;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_csc = function_Info->pq_set_csc;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_get_memyuvfmt= function_Info->pq_get_memyuvfmt;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_memformat = function_Info->pq_set_memformat;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_modeInfo = function_Info->pq_set_modeInfo;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_ioctl = function_Info->pq_ioctl;
#endif

#ifdef ENABLE_TV_SC2_PQ
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_deside_srctype_ex = function_Info->pq_deside_srctype_ex;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_disable_filmmode_ex = function_Info->pq_disable_filmmode_ex;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_load_scalingtable_ex = function_Info->pq_load_scalingtable_ex;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_420upsampling_ex = function_Info->pq_set_420upsampling_ex;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_csc_ex = function_Info->pq_set_csc_ex;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_get_memyuvfmt_ex = function_Info->pq_get_memyuvfmt_ex;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_memformat_ex = function_Info->pq_set_memformat_ex;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_set_modeInfo_ex = function_Info->pq_set_modeInfo_ex;
        s_PQ_Function_Info[psXCInstPri->u32DeviceID].pq_ioctl_ex = function_Info->pq_ioctl_ex;
#endif

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}

void MApi_XC_PQ_LoadFunction(PQ_Function_Info* function_Info , MS_U32 u32InitDataLen)
{
    if (pu32XCInst == NULL)
    {
        if(UtopiaOpen(MODULE_XC, &pu32XCInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return;
        }
    }

    stXC_SET_PQ_LOADFUNCTION XCArgs;
    XCArgs.function_Info = function_Info;
    XCArgs.u32InitDataLen = u32InitDataLen;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_PQ_LOADFUNCTION, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}

//-------------------------------------------------------------------------------------------------
/// @ingroup XC
/// @param  enMemoryType                        \b IN: memory type
/// @param phyBaseAddr                          \b IN: memory address
/// @param u32Size                              \b IN: memory size
/// @return  E_APIXC_ReturnValue:               \b OUT: Return status
///@Reference file     MultiWindow xc auto_download Feature Design And Specification.docx
/// For general use to set memory address
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue  MApi_XC_SetMemoryAddress_U2(void* pInstance, ST_XC_MEMORY_INFO *pstMemory_Info)
{
    E_APIXC_ReturnValue eRet = E_APIXC_RET_FAIL;
    MS_U32 u32ValidLength = 0;
    ST_XC_MEMORY_INFO stMemInfo;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    // version handling
    if ((pstMemory_Info == NULL) || (pstMemory_Info->u32Length == 0))
    {
        //We cosider compatible operation form version2 , so reject the info init when version invalid
        XC_LOG_TRACE(XC_DBGLEVEL_PARAMETER_TRACE, "MApi_XC_SetMemoryAddress: Null paramter or Wrong u32Length!!\n");

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

        return eRet;
    }

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

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

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);

    XC_LOG_TRACE(XC_DBGLEVEL_PARAMETER_TRACE, "enType: %d.\n", stMemInfo.enType);
    eRet = MDrv_XC_SetMemoryAddress(pInstance,&stMemInfo);

    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return eRet;
}

E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_SetMemoryAddress(ST_XC_MEMORY_INFO *pstMemory_Info)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    //wraper code to call MApi_XC_SetMemoryAddress_U2
    stXC_SET_MEMORY_ADDRESS XCArgs;
    XCArgs.pstMemory_Info = pstMemory_Info;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    // call MApi_XC_SetMemoryAddress_U2 by UtopiaIoctl
    if (UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_MEMORY_ADDRESS, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return UTOPIA_STATUS_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}

E_APIXC_ReturnValue MApi_XC_ConnectDisplay_U2(void* pInstance,ST_XC_CONNECTDISPLAY *pstXCDispaly,SCALER_WIN enWindow)
{
    ST_XC_CONNECTDISPLAY stConnectDisplayInfo;
    MS_U32 u32CopiedLength = sizeof(ST_XC_CONNECTDISPLAY);
    memset(&stConnectDisplayInfo, 0, sizeof(ST_XC_CONNECTDISPLAY));
    if((NULL == pstXCDispaly) || (pstXCDispaly->u32Length == 0))
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETTIMING, "MApi_XC_ConnectDisplay: Null paramter or Wrong u16Length!!\n");
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return E_APIXC_RET_FAIL;
    }
    if(pstXCDispaly->u32Version < ST_XC_CONNECTDISPLAY_VERSION)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETTIMING, "MApi_XC_ConnectDisplay: please check your u32Version, it should not set to 0!!\n");
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return E_APIXC_RET_FAIL;
    }

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

    //old AP + new lib, driver shouldn't access to the space which doesn't exist in old structure
    if((pstXCDispaly->u32Version < ST_XC_CONNECTDISPLAY_VERSION) || (pstXCDispaly->u32Length < sizeof(ST_XC_CONNECTDISPLAY)))
    {
        //We cosider compatible operation form version1 , so reject the info init when version invalid
        XC_LOG_TRACE(XC_DBGLEVEL_SETTIMING, "MApi_XC_GetStatusEx: new version XC lib shouldn't access to the space which doesn't exist in old structure!!\n")
        u32CopiedLength = pstXCDispaly->u32Length;
    }

    pstXCDispaly->u32Length = u32CopiedLength; //Return the actual length of the returned value for APP to know whick info is valid

    memcpy(&stConnectDisplayInfo, pstXCDispaly, u32CopiedLength);

    MDrv_XC_ConnectDisplay(pInstance,&stConnectDisplayInfo,enWindow);
    return E_APIXC_RET_OK;

}
E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_ConnectDisplay(ST_XC_CONNECTDISPLAY *pstXCDispaly,SCALER_WIN enWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_ConnectDisplay() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }
    stXC_CONNECTDISPLAY XCArgs;
    XCArgs.pstXCDispaly = pstXCDispaly;
    XCArgs.enWindow = enWindow;
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;
    if(UtopiaIoctl(pu32XCInst, E_XC_CONNECTDISPLAY, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.enReturnValue;
    }

}

void MApi_XC_SetOSD2VEMode_U2(void* pInstance, EN_VOP_SEL_OSD_XC2VE_MUX  eVOPSelOSD_MUX)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    MDrv_XC_SetOSD2VEmode(pInstance, eVOPSelOSD_MUX);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_SetOSD2VEMode(EN_VOP_SEL_OSD_XC2VE_MUX  eVOPSelOSD_MUX)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_OSD2VEMODE XCArgs;
    XCArgs.eVOPSelOSD_MUX = eVOPSelOSD_MUX;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_OSD2VEMODE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
void MApi_XC_IP2_PreFilter_Enable_U2(void* pInstance, MS_BOOL bEnable)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    _XC_ENTRY(pInstance);
    MDrv_SC_IP2_PreFilter_Enable(pInstance, bEnable);
    _XC_RETURN(pInstance);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_IP2_PreFilter_Enable(MS_BOOL bEnable)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_IP2_PREFILTER_ENABLE XCArgs;
    XCArgs.bEnable = bEnable;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_IP2_PREFILTER_ENABLE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
void MApi_XC_KeepPixelPointerAppear_U2(void* pInstance, MS_BOOL bEnable)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    _XC_ENTRY(pInstance);
    Hal_SC_enable_cursor_report(pInstance, bEnable);
    pXCResourcePrivate->stdrvXC_MVideo.s_bKeepPixelPointerAppear = bEnable;
    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_KeepPixelPointerAppear(MS_BOOL bEnable)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_KEEP_PIXELPOINTER_APPEAR XCArgs;
    XCArgs.bEnable = bEnable;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_KEEP_PIXELPOINTER_APPEAR, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
E_APIXC_GET_PixelRGB_ReturnValue MApi_XC_Get_Pixel_RGB_U2(void* pInstance, XC_Get_Pixel_RGB *pData, SCALER_WIN eWindow)
{
    E_APIXC_GET_PixelRGB_ReturnValue enRet;

    MS_WINDOW_TYPE DispWin;
    MS_WINDOW_TYPE DEWin;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);

    Hal_SC_get_disp_de_window(pInstance, &DEWin);

    if(pData->enStage == E_XC_GET_PIXEL_STAGE_AFTER_DLC)
        MDrv_XC_get_dispwin_from_reg(pInstance, eWindow,&DispWin );
    else
        MDrv_XC_get_dispwin_from_reg(pInstance, MAIN_WINDOW, &DispWin);

    pData->u16x += DEWin.x;
    pData->u16y += DEWin.y;

    if(pData->u16x > DEWin.x + DEWin.width ||
       pData->u16y > DEWin.y + DEWin.height)
    {
        enRet = E_GET_PIXEL_RET_OUT_OF_RANGE;
    }
    else
    {
        if(Hal_SC_get_pixel_rgb(pInstance, pData))
            enRet = E_GET_PIXEL_RET_OK;
        else
            enRet = E_GET_PIXEL_RET_FAIL;
    }

    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return enRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_GET_PixelRGB_ReturnValue MApi_XC_Get_Pixel_RGB(XC_Get_Pixel_RGB *pData, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_GET_PIXEL_RET_OUT_OF_RANGE;
    }

    stXC_GET_PIXELRGB XCArgs;
    XCArgs.pData = pData;
    XCArgs.eWindow = eWindow;
    XCArgs.eReturnValue = E_GET_PIXEL_RET_OUT_OF_RANGE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_PIXELRGB, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_GET_PIXEL_RET_OUT_OF_RANGE;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
MS_BOOL MApi_XC_ReportPixelInfo_U2(void* pInstance, MS_XC_REPORT_PIXELINFO *pstRepPixInfo)
{
    MS_BOOL enRet = FALSE;
    MS_WINDOW_TYPE stDEWin;
    MS_WINDOW_TYPE stRepWin;
    memset(&stDEWin, 0, sizeof(MS_WINDOW_TYPE));
    memset(&stRepWin, 0, sizeof(MS_WINDOW_TYPE));
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    if(NULL == pstRepPixInfo)
    {
        printf("MApi_XC_ReportPixelInfo: pstRepPixInfo is NULL!!\n");
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        _XC_SEMAPHORE_RETURN(pInstance, E_XC_ID_VAR);
        return FALSE;
    }

    _XC_ENTRY(pInstance);

    //backup
    stRepWin.x = pstRepPixInfo->u16XStart;
    stRepWin.width = pstRepPixInfo->u16XEnd - pstRepPixInfo->u16XStart + 1;
    stRepWin.y = pstRepPixInfo->u16YStart;
    stRepWin.height = pstRepPixInfo->u16YEnd - pstRepPixInfo->u16YStart + 1;

    Hal_SC_get_disp_de_window(pInstance, &stDEWin);

    if((pstRepPixInfo->u16XStart > pstRepPixInfo->u16XEnd)
        || (pstRepPixInfo->u16XStart >= stDEWin.width)
        || (pstRepPixInfo->u16XEnd >= stDEWin.width)
        || (pstRepPixInfo->u16YStart > pstRepPixInfo->u16YEnd)
        || (pstRepPixInfo->u16YStart >= stDEWin.height)
        || (pstRepPixInfo->u16YEnd >= stDEWin.height)
      )
    {
        printf("Attention! MApi_XC_ReportPixelInfo: Invalid Param! Report Window=(%u,%u,%u,%u), DE Window=(%u,%u,%u,%u)\n",
                pstRepPixInfo->u16XStart, pstRepPixInfo->u16XEnd, pstRepPixInfo->u16YStart, pstRepPixInfo->u16YEnd,
                stDEWin.x, stDEWin.x + stDEWin.width - 1, stDEWin.y, stDEWin.y + stDEWin.height - 1);
        _XC_RETURN(pInstance);
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        _XC_SEMAPHORE_RETURN(pInstance, E_XC_ID_VAR);
        return FALSE;
    }

    //add de
    /*printf("! MApi_XC_ReportPixelInfo: Report Window=(%u,%u,%u,%u), DE Window=(%u,%u,%u,%u)\n",
                pstRepPixInfo->u16XStart, pstRepPixInfo->u16XEnd, pstRepPixInfo->u16YStart, pstRepPixInfo->u16YEnd,
                stDEWin.x, stDEWin.x + stDEWin.width - 1, stDEWin.y, stDEWin.y + stDEWin.height - 1);*/

    //if H mirror
    if(IsHMirrorMode(MAIN_WINDOW) || (MDrv_ReadRegBit(REG_MVOP_MIRROR, BIT(1)) == BIT(1)))
    {
        pstRepPixInfo->u16XStart = stDEWin.width - pstRepPixInfo->u16XEnd - 1;
        pstRepPixInfo->u16XEnd = stRepWin.width + pstRepPixInfo->u16XStart - 1;
    }

    pstRepPixInfo->u16XStart += stDEWin.x;
    pstRepPixInfo->u16XEnd += stDEWin.x;

    //if V mirror
    if(IsVMirrorMode(MAIN_WINDOW) || (MDrv_ReadRegBit(REG_MVOP_MIRROR, BIT(0)) == BIT(0)))
    {
        pstRepPixInfo->u16YStart = stDEWin.height - pstRepPixInfo->u16YEnd -1;
        pstRepPixInfo->u16YEnd = stRepWin.height + pstRepPixInfo->u16YStart - 1;
    }

    pstRepPixInfo->u16YStart += stDEWin.y;
    pstRepPixInfo->u16YEnd += stDEWin.y;

    /*printf("%s[%d]! MApi_XC_ReportPixelInfo: Report Window=(%u,%u,%u,%u), DE Window=(%u,%u,%u,%u)\n",
                __FUNCTION__,__LINE__, pstRepPixInfo->u16XStart, pstRepPixInfo->u16XEnd, pstRepPixInfo->u16YStart, pstRepPixInfo->u16YEnd,
                stDEWin.x, stDEWin.x + stDEWin.width - 1, stDEWin.y, stDEWin.y + stDEWin.height - 1);*/

    enRet = Hal_XC_SetPixelInfo(pInstance, pstRepPixInfo, TRUE);
    if(enRet)
    {
        _XC_RETURN(pInstance);
        _XC_SEMAPHORE_RETURN(pInstance, E_XC_ID_VAR);

        MsOS_DelayTask(50);  // Must wait .
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
        _XC_ENTRY(pInstance);
        Hal_XC_GetPixelInfo(pInstance, pstRepPixInfo);
    }
    Hal_XC_SetPixelInfo(pInstance, pstRepPixInfo, FALSE);

    //restore for user
    pstRepPixInfo->u16XStart = stRepWin.x;
    pstRepPixInfo->u16XEnd = stRepWin.x + stRepWin.width - 1;
    pstRepPixInfo->u16YStart = stRepWin.y;
    pstRepPixInfo->u16YEnd = stRepWin.y + stRepWin.height -1;

    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance, E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return enRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_ReportPixelInfo(MS_XC_REPORT_PIXELINFO *pstRepPixInfo)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    stXC_GET_REPORT_PIXEL_INFO XCArgs;
    XCArgs.pstRepPixInfo = pstRepPixInfo;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_REPORT_PIXEL_INFO, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}
#endif
///This is an Obsolete functions, try use MApi_XC_Set_MemFmtEx
MS_BOOL MApi_XC_Set_MemFmt_U2(void* pInstance, MS_XC_MEM_FMT eMemFmt)
{
    MS_BOOL bret = FALSE;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    if(TRUE == MDrv_XC_Set_MemFmtEx(pInstance, eMemFmt, MAIN_WINDOW))
    {
        bret = MDrv_XC_Set_MemFmtEx(pInstance, eMemFmt, SUB_WINDOW);
    }
    else
    {
        bret = FALSE;
    }
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return bret;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_Set_MemFmt(MS_XC_MEM_FMT eMemFmt)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    stXC_SET_MEMORY_FORMAT XCArgs;
    XCArgs.eMemFmt = eMemFmt;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_MEMORY_FORMAT, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif
MS_BOOL MDrv_XC_Set_MemFmtEx(void *pInstance, MS_XC_MEM_FMT eMemFmt, SCALER_WIN eWindow)
{
    MS_BOOL bret = FALSE;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    if(eMemFmt <= E_MS_XC_MEM_FMT_422)
    {
        pXCResourcePrivate->stdrvXC_MVideo.s_eMemFmt[eWindow] = eMemFmt;
        bret = TRUE;
    }
    else
    {
        bret = FALSE;
    }
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return bret;
}

MS_BOOL MApi_XC_Set_MemFmtEx_U2(void* pInstance, MS_XC_MEM_FMT eMemFmt, SCALER_WIN eWindow)
{
    MS_BOOL bReturn = FALSE;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    bReturn = MDrv_XC_Set_MemFmtEx(pInstance, eMemFmt, eWindow);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return bReturn;
}

MS_BOOL MApi_XC_Set_MemFmtEx(MS_XC_MEM_FMT eMemFmt, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        if(UtopiaOpen(MODULE_XC, &pu32XCInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return FALSE;
        }
    }

    stXC_SET_MEMORY_FORMAT_EX XCArgs;
    XCArgs.eMemFmt = eMemFmt;
    XCArgs.eWindow = eWindow;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_MEMORY_FORMAT_EX, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
MS_BOOL MApi_XC_IsRequestFrameBufferLessMode_U2(void* pInstance)
{
    MS_BOOL bret = FALSE;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    bret = MDrv_XC_IsRequestFrameBufferLessMode(pInstance);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return bret;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_IsRequestFrameBufferLessMode(void)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    stXC_CHECK_REQUEST_FRAMEBUFFERLESS_MODE XCArgs;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_CHECK_REQUEST_FRAMEBUFFERLESS_MODE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Set Flag to bypass software reset in MApi_XC_Init()
/// @param  bFlag               \b IN: TRUE: initialize XC by skipping SW reset; FALSE: initialize XC in normal case
/// @return E_APIXC_RET_OK or E_APIXC_RET_FAIL
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_SkipSWReset_U2(void* pInstance, MS_BOOL bFlag)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    pXCResourcePrivate->stdrvXC_MVideo._bSkipSWReset = bFlag;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return E_APIXC_RET_OK;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_SkipSWReset(MS_BOOL bFlag)
{
    XC_INSTANT_ATTRIBUTE stXCInstantAttribute;
    INIT_XC_INSTANT_ATTR(stXCInstantAttribute);

    CheckXCInstanceOpen();

#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
     if (pu32XCInst_1 == NULL)
     {
        stXCInstantAttribute.u32DeviceID = 1;
        if(UtopiaOpen(MODULE_XC, &pu32XCInst_1, 0, &stXCInstantAttribute) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return E_APIXC_RET_FAIL;
        }
      }
#endif
    stXC_SET_SKIP_SWRESET XCArgs;
    XCArgs.bFlag = bFlag;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_SKIP_SWRESET, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Select which DE to vop
/// @param  bFlag               \b IN: @ref E_OP2VOP_DE_SEL
/// @return E_APIXC_RET_OK or E_APIXC_RET_FAIL
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_OP2VOPDESel_U2(void* pInstance, E_OP2VOP_DE_SEL eVopDESel)
{
    E_APIXC_ReturnValue eRet = E_APIXC_RET_FAIL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    _XC_ENTRY(pInstance);
    eRet = Hal_SC_OP2VOPDESel(pInstance, eVopDESel);
    _XC_RETURN(pInstance);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return eRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_OP2VOPDESel(E_OP2VOP_DE_SEL eVopDESel)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_OP2VOP_DE_SELECTION XCArgs;
    XCArgs.eVopDESel = eVopDESel;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_OP2VOP_DE_SELECTION, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Set Video and OSD Layer at Scaler VOP
/// @return E_APIXC_RET_OK or E_APIXC_RET_FAIL
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_SetOSDLayer_U2(void* pInstance, E_VOP_OSD_LAYER_SEL  eVOPOSDLayer, SCALER_WIN eWindow)
{
    E_APIXC_ReturnValue eRet = E_APIXC_RET_FAIL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    _XC_ENTRY(pInstance);

    eRet = MDrv_XC_SetOSDLayer(pInstance, eVOPOSDLayer, eWindow);
    if(eRet == E_APIXC_RET_OK)
    {
        gSrcInfo[eWindow].u16OSDLayer = eVOPOSDLayer;
    }

    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return eRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_SetOSDLayer(E_VOP_OSD_LAYER_SEL  eVOPOSDLayer, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_OSDLAYER XCArgs;
    XCArgs.eVOPOSDLayer = eVOPOSDLayer;
    XCArgs.eWindow = eWindow;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_OSDLAYER, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Get Video and OSD Layer Enum at Scaler VOP
/// @return Enum value
//-------------------------------------------------------------------------------------------------
E_VOP_OSD_LAYER_SEL MApi_XC_GetOSDLayer_U2(void* pInstance, SCALER_WIN eWindow)
{
    E_VOP_OSD_LAYER_SEL eSelect = E_VOP_LAYER_VIDEO_MUX1_MUX2;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    _XC_ENTRY(pInstance);

    eSelect = ((E_VOP_OSD_LAYER_SEL)gSrcInfo[eWindow].u16OSDLayer == E_VOP_LAYER_RESERVED) ? MDrv_XC_GetOSDLayer(pInstance, eWindow) : (E_VOP_OSD_LAYER_SEL)gSrcInfo[eWindow].u16OSDLayer;

    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return eSelect;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_VOP_OSD_LAYER_SEL MApi_XC_GetOSDLayer(SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_VOP_LAYER_RESERVED;
    }

    stXC_GET_OSDLAYER XCArgs;
    XCArgs.eWindow = eWindow;
    XCArgs.eReturnValue = E_VOP_LAYER_RESERVED;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_OSDLAYER, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_VOP_LAYER_RESERVED;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Set Constant Alpha Value of Video
/// @return E_APIXC_RET_OK or E_APIXC_RET_FAIL
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_SetVideoAlpha_U2(void* pInstance, MS_U8 u8Val, SCALER_WIN eWindow)
{
    E_APIXC_ReturnValue eRet = E_APIXC_RET_FAIL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    eRet = MDrv_XC_SetVideoAlpha(pInstance, u8Val, eWindow);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return eRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_SetVideoAlpha(MS_U8 u8Val, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_VIDEO_ALPHA XCArgs;
    XCArgs.u8Val = u8Val;
    XCArgs.eWindow = eWindow;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_VIDEO_ALPHA, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Get Constant Alpha Value of Video
/// @return E_APIXC_RET_OK or E_APIXC_RET_FAIL
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_GetVideoAlpha_U2(void* pInstance, MS_U8 *pu8Val, SCALER_WIN eWindow)
{
    E_APIXC_ReturnValue eRet = E_APIXC_RET_FAIL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    eRet = MDrv_XC_GetVideoAlpha(pInstance, pu8Val, eWindow);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return eRet;

}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_GetVideoAlpha(MS_U8 *pu8Val, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_GET_VIDEO_ALPHA XCArgs;
    XCArgs.pu8Val = pu8Val;
    XCArgs.eWindow = eWindow;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_VIDEO_ALPHA, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Get field packing mode support status
/// @return TRUE(success) or FALSE(fail)
//-------------------------------------------------------------------------------------------------
MS_BOOL MApi_XC_IsFieldPackingModeSupported_U2(void* pInstance)
{
    MS_BOOL bRet = FALSE;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    bRet = MDrv_XC_Get_SCMI_Type();
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return bRet;
}

MS_BOOL MApi_XC_IsFieldPackingModeSupported(void)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    stXC_CHECK_FIELDPACKINGMODE_SUPPORTED XCArgs;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_CHECK_FIELDPACKINGMODE_SUPPORTED, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}

//-------------------------------------------------------------------------------------------------
// Skip Wait Vsync
/// @param eWindow               \b IN: Enable
/// @param Skip wait Vsync      \b IN: Disable wait Vsync
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_SkipWaitVsync_U2( void* pInstance, MS_BOOL eWindow,MS_BOOL bIsSkipWaitVsyn)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    MDrv_SC_SetSkipWaitVsync(pInstance, eWindow, bIsSkipWaitVsyn);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return E_APIXC_RET_OK;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_SkipWaitVsync( MS_BOOL eWindow,MS_BOOL bIsSkipWaitVsyn)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SKIP_WAIT_VSYNC XCArgs;
    XCArgs.eWindow = eWindow;
    XCArgs.bIsSkipWaitVsyn = bIsSkipWaitVsyn;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SKIP_WAIT_VSYNC, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Set XC CMA Heap ID
/// @param  u8CMAHeapID            \b IN: CMA Heap ID
/// @param  eWindow                 \b IN: which window we are going to set
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_SetCMAHeapID_U2( void* pInstance, MS_U8 u8CMAHeapID, SCALER_WIN eWindow)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    MDrv_SC_SetCMAHeapID(pInstance, u8CMAHeapID, eWindow);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return E_APIXC_RET_OK;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_SetCMAHeapID(MS_U8 u8CMAHeapID, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_CMA_HEAP_ID XCArgs;
    XCArgs.u8CMAHeapID = u8CMAHeapID;
    XCArgs.eWindow = eWindow;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_CMA_HEAP_ID, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Set the VGA SOG on or off
/// @param  bEnable                \b IN: bEnable =1, Turn on the VGA SOG; bEnable =0, Turn off the VGA SOG
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_SetVGASogEn_U2(void* pInstance, MS_BOOL bVGASogEn)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    MDrv_XC_SetVGASogEn(pInstance, bVGASogEn);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return E_APIXC_RET_OK;
}

E_APIXC_ReturnValue MApi_XC_SetVGASogEn(MS_BOOL bVGASogEn)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_VGA_SOG_ENABLE XCArgs;
    XCArgs.bVGASogEn = bVGASogEn;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_VGA_SOG_ENABLE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}

//-------------------------------------------------------------------------------------------------
/// Get pixel shift
/// @param  ps8HOffset               \b OUT: pixel shift H
/// @param  ps8VOffset               \b OUT: pixel shift V
/// @return E_APIXC_RET_OK
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_GetPixelShift_U2(void* pInstance, MS_S8 *ps8HOffset, MS_S8 *ps8VOffset)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    *ps8HOffset = pXCResourcePrivate->stdrvXC_MVideo.s8HPixelShift;
    *ps8VOffset = pXCResourcePrivate->stdrvXC_MVideo.s8VPixelShift;

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return E_APIXC_RET_OK;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue  MApi_XC_GetPixelShift(MS_S8 *ps8HOffset, MS_S8 *ps8VOffset)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_GET_PIXEL_SHIFT XCArgs;
    XCArgs.ps8HOffset = ps8HOffset;
    XCArgs.ps8VOffset = ps8VOffset;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_PIXEL_SHIFT, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }

}
#endif
//-------------------------------------------------------------------------------------------------
/// Set pixel shift
/// @param  s8H               \b IN: pixel shift H
/// @param  s8V               \b IN: pixel shift V
/// @return E_APIXC_RET_OK or E_APIXC_RET_FAIL
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_SetPixelShift_U2(void* pInstance, MS_S8 s8H, MS_S8 s8V)
{
    E_APIXC_ReturnValue eRet = E_APIXC_RET_FAIL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    _XC_ENTRY(pInstance);

    MS_S8 s8CheckHData = 0;
    MS_S8 s8CheckVData = 0;
    MS_S8 s8HSftPixOdd = 0;

    s8CheckHData = (MS_S8)pXCResourcePrivate->stdrvXC_MVideo.gu16HPixelShiftRange / 2;
    s8CheckVData = (MS_S8)pXCResourcePrivate->stdrvXC_MVideo.gu16VPixelShiftRange / 2;

    if((pXCResourcePrivate->stdrvXC_MVideo.gu16HPixelShiftRange == 0) && (pXCResourcePrivate->stdrvXC_MVideo.gu16VPixelShiftRange == 0))
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[pixel shift][%s,%5d]Not support or has not been enabled!!!\n",__FUNCTION__,__LINE__);
        _XC_RETURN(pInstance);
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        return E_APIXC_RET_FAIL;
    }
    else
    {
        if((s8H < (-1)*(s8CheckHData))|| (s8H > s8CheckHData)|| (s8V  < (-1)*s8CheckVData)|| (s8V > s8CheckVData))
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "[pixel shift] [%s,%5d]ERROR,shift pixel out of range,please check!!!\n", __FUNCTION__, __LINE__);
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "[pixel shift] shift H=%d (%d ~ %d)\n",s8H, (-1)*(s8CheckHData), s8CheckHData);
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "[pixel shift] shift V=%d (%d ~ %d)\n",s8V, ((-1)*s8CheckVData), s8CheckVData);
            _XC_RETURN(pInstance);
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            return E_APIXC_RET_FAIL;
        }

        // save pixshift offset for SW DS.
        if(MDrv_XC_Is_UpdateDsCmd(pInstance, MAIN_WINDOW) && (MDrv_XC_GetDynamicScalingStatus(pInstance) || MDrv_XC_Is_DSForceIndexEnabled(pInstance, MAIN_WINDOW)))
        {
            pXCResourcePrivate->stdrvXC_MVideo.s8HPixelShift = s8H;
            pXCResourcePrivate->stdrvXC_MVideo.s8VPixelShift = s8V;
            _XC_RETURN(pInstance);
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            return E_APIXC_RET_OK;
        }

        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[pixel shift] do pixel shift...\n");

        //2p mode need H even
        s8HSftPixOdd = s8H % 2;
        if(s8H > 0)
        {
            s8H = s8H + s8HSftPixOdd;
        }
        else
        {
            s8H = s8H - s8HSftPixOdd;
        }
        if(s8HSftPixOdd != 0)
        {
            MHal_XC_Set1PShift(pInstance, TRUE);
        }
        else
        {
            MHal_XC_Set1PShift(pInstance, FALSE);
        }
        eRet = MDrv_XC_SetPixelShift(pInstance, s8H, s8V);

        _XC_RETURN(pInstance);
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        return eRet;
    }
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_SetPixelShift(MS_S8 s8H, MS_S8 s8V)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_PIXEL_SHIFT XCArgs;
    XCArgs.s8H = s8H;
    XCArgs.s8V = s8V;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_PIXEL_SHIFT, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
E_APIXC_ReturnValue MApi_XC_SetPixelShiftFeatures_U2(void* pInstance,
                                                     MS_U16 u16HPixelShiftRange,
                                                     MS_U16 u16VPixelShiftRange,
                                                     PIXEL_SHIFT_FEATURE ePixelShiftFeature)
{
#if (XC_SUPPORT_PIXSHIFT_2ND_SDE==TRUE)
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    if((ePixelShiftFeature & PIXEL_SHIFT_MOVE_MAIN_WINDOW) == PIXEL_SHIFT_MOVE_MAIN_WINDOW)
    {
        if(pXCResourcePrivate->stdrvXC_MVideo.gePixelShiftFeature == ePixelShiftFeature)
        {
            printf("[%s,%5d]Pixel shift function has been enabled, please no not enable again\n",__FUNCTION__,__LINE__);
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            return E_APIXC_RET_FAIL;
        }
        else
        {
            _XC_ENTRY(pInstance);
            if(u16HPixelShiftRange > HPixelShiftRangeMax)
            {
                u16HPixelShiftRange = HPixelShiftRangeMax;
            }
            if((ePixelShiftFeature & PIXEL_SHIFT_DO_OVERSCAN) == PIXEL_SHIFT_DO_OVERSCAN)
            {
                if(u16VPixelShiftRange > VPixelShiftOverscanRangeMax)
                {
                    u16VPixelShiftRange = VPixelShiftOverscanRangeMax;
                }
            }
            else
            {
                if(u16VPixelShiftRange > VPixelShiftJustscanRangeMax)
                {
                    u16VPixelShiftRange = VPixelShiftJustscanRangeMax;
                }
            }
            pXCResourcePrivate->stdrvXC_MVideo.gu16HPixelShiftRange = 2 * u16HPixelShiftRange;
            pXCResourcePrivate->stdrvXC_MVideo.gu16VPixelShiftRange = 2 * u16VPixelShiftRange;
            pXCResourcePrivate->stdrvXC_MVideo.gePixelShiftFeature = ePixelShiftFeature;

            MHal_XC_UpdateFdeWindow(pInstance, TRUE);

            _XC_RETURN(pInstance);
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            return E_APIXC_RET_OK;
        }
    }
    else
    {
        if((pXCResourcePrivate->stdrvXC_MVideo.gu16HPixelShiftRange == 0) && (pXCResourcePrivate->stdrvXC_MVideo.gu16VPixelShiftRange == 0))
        {
            printf("[%s,%5d]Pixel shift function has not been enabled, can not disable it\n",__FUNCTION__,__LINE__);
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            return E_APIXC_RET_FAIL;
        }
        else
        {
            _XC_ENTRY(pInstance);
            pXCResourcePrivate->stdrvXC_MVideo.gePixelShiftFeature = 0;
            MHal_XC_PixelShiftStatusChange(pInstance, EN_XC_PIXEL_SHIFT_DISABLE);
            MHal_XC_UpdateFdeWindow(pInstance, FALSE);

            _XC_RETURN(pInstance);
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            return E_APIXC_RET_OK;
        }
    }
#else
    printf("[%s,%5d]Pixel shift function do not support!!!\n",__FUNCTION__,__LINE__);
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_SetPixelShiftFeatures(MS_U16 u16HPixelShiftRange,
                                                  MS_U16 u16VPixelShiftRange,
                                                  PIXEL_SHIFT_FEATURE ePixelShiftFeature)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_PIXELSHIFT_FEATURES XCArgs;
    XCArgs.u16HPixelShiftRange = u16HPixelShiftRange;
    XCArgs.u16VPixelShiftRange = u16VPixelShiftRange;
    XCArgs.ePixelShiftFeature = ePixelShiftFeature;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_PIXEL_SHIFT_FEATURES, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Set the specific window for traveling mode(VE capture function)
/// @param  pstXC_SetWin_Info      \b IN: the information of the window setting
/// @param  u32InitDataLen         \b IN: the length of the pstXC_SetWin_Info
/// @param  eWindow                \b IN: which window we are going to set
/// @return @ref MS_BOOL
//-------------------------------------------------------------------------------------------------
MS_BOOL MApi_XC_SetTravelingWindow_U2(void* pInstance, XC_SETWIN_INFO *pstXC_SetWin_Info, MS_U32 u32InitDataLen, SCALER_WIN eWindow)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    //The component 1080i can't frame lock when capture start is odd.
    if(pstXC_SetWin_Info->bInterlace)
    {
        // the Capture start should be even
        if(pstXC_SetWin_Info->stCapWin.y%2)
        {
            pstXC_SetWin_Info->stCapWin.y += 1;
        }
        pstXC_SetWin_Info->stCapWin.height = pstXC_SetWin_Info->stCapWin.height & (~0x01);
    }
    pstXC_SetWin_Info->stCapWin.width = pstXC_SetWin_Info->stCapWin.width & (~0x01);

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
#if (XC_SUPPORT_FASTSWITCH_BY_IP1GP==TRUE)
    MHal_XC_SetOfflineSource(pInstance, eWindow, gSrcInfo[eWindow].enInputSourceType);
#endif
    // copy user data to internal data
    // here is a patch first, will remove after seperate Internal XC status with set window information
    //u32CopyLen = sizeof(XC_SETWIN_INFO) - (sizeof(MS_BOOL)*2) - (sizeof(MS_U16)*4);
    //memcpy(&gSrcInfo[eWindow], pstXC_SetWin_Info, u32CopyLen);
    //do not use memcpy(), strongly depend on structure definition
    _Mdrv_XC_CopySetWinInfo(pInstance, pstXC_SetWin_Info, eWindow);

    gSrcInfo[eWindow].stDispWin.x += gSrcInfo[eWindow].Status2.stXCPanelDefaultTiming.u16HStart;
    gSrcInfo[eWindow].stDispWin.y += gSrcInfo[eWindow].Status2.stXCPanelDefaultTiming.u16VStart;

    // dump debug msg
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"============= SetTravelingWindow Start (Window : %u, src: %u) =============\n",eWindow, gSrcInfo[eWindow].enInputSourceType);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"CapWin  x: %4u  y: %4u  w: %4u  h: %4u \n",gSrcInfo[eWindow].stCapWin.x, gSrcInfo[eWindow].stCapWin.y, gSrcInfo[eWindow].stCapWin.width, gSrcInfo[eWindow].stCapWin.height);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"CropWin x: %4u  y: %4u  w: %4u  h: %4u \n",gSrcInfo[eWindow].stCropWin.x, gSrcInfo[eWindow].stCropWin.y, gSrcInfo[eWindow].stCropWin.width, gSrcInfo[eWindow].stCropWin.height);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"DispWin x: %4u  y: %4u  w: %4u  h: %4u \n",gSrcInfo[eWindow].stDispWin.x,gSrcInfo[eWindow].stDispWin.y, gSrcInfo[eWindow].stDispWin.width, gSrcInfo[eWindow].stDispWin.height);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Panel   x: %4u  y: %4u  w: %4u  h: %4u \n",
                                       pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HStart,
                                       pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VStart,
                                       pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width,
                                       pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Mirror/Interlace/Hdup = %u/%u/%u\n", IsVMirrorMode(eWindow), pstXC_SetWin_Info->bInterlace, pstXC_SetWin_Info->bHDuplicate);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"H/V total = (%u, %u), VFreq = %u\n", pstXC_SetWin_Info->u16DefaultHtotal, pstXC_SetWin_Info->u16InputVTotal, pstXC_SetWin_Info->u16InputVFreq);

    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    // Scaler must update source before width / height checking.
    _MDrv_XC_Set_PQ_SourceData(pInstance, eWindow, gSrcInfo[eWindow].enInputSourceType, &gSrcInfo[eWindow]);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);

    // Capture width & height can not be 0 !!
    if ((gSrcInfo[eWindow].stCapWin.width < 0x20) || (gSrcInfo[eWindow].stCapWin.height < 0x20) ||
        (gSrcInfo[eWindow].stCapWin.x > 0x7FF)    || (gSrcInfo[eWindow].stCapWin.y > 0x7FF))
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Cap Width/Height can not be smaller than 0x20, or X/Y can not bigger than 0x7FF \n");
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        return FALSE;
    }
/* //Useless code
#if ENABLE_REQUEST_FBL
    if(s_PQ_Function_Info.pq_ioctl)
    {
        s_PQ_Function_Info.pq_ioctl((PQ_WIN)eWindow, E_PQ_IOCTL_RFBL_CTRL, NULL, NULL);
    }
#endif
*/
    _XC_ENTRY(pInstance);
    MDrv_sc_set_capture_window(pInstance, eWindow );//Fine tune capture window
    Hal_SC_ip_set_capture_h_start(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg.u16H_CapStart, eWindow); //Set the tuned capture window
    Hal_SC_ip_set_capture_v_start(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg.u16V_CapStart, eWindow);
    Hal_SC_ip_set_capture_h_size (pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg.u16H_CapSize , eWindow);
    Hal_SC_ip_set_capture_v_size (pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg.u16V_CapSize , eWindow);
    _XC_RETURN(pInstance);

    if (IsSrcTypeVga(gSrcInfo[eWindow].enInputSourceType)   ||
        IsSrcTypeYPbPr(gSrcInfo[eWindow].enInputSourceType) ||
        IsSrcTypeScart(gSrcInfo[eWindow].enInputSourceType) )
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"ADC Set Mode \n");
        PQ_ADC_SAMPLING_INFO stPQADCSamplingInfo;
        memset(&stPQADCSamplingInfo,0,sizeof(PQ_ADC_SAMPLING_INFO));
        if ( IsSrcTypeVga(gSrcInfo[eWindow].enInputSourceType) || IsSrcTypeYPbPr(gSrcInfo[eWindow].enInputSourceType) )
        {
            if(gSrcInfo[eWindow].bHDuplicate)
            {
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                MDrv_XC_ADC_GetPQADCSamplingInfo(pInstance, gSrcInfo[eWindow].enInputSourceType, &gSrcInfo[eWindow], &stPQADCSamplingInfo, eWindow);
                _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
            }
        }

        MDrv_XC_ADC_SetMode(pInstance, gSrcInfo[eWindow].enInputSourceType, &gSrcInfo[eWindow], &stPQADCSamplingInfo, eWindow);
    }
    else
    {
        MDrv_XC_ADC_SCART_RGB_setMode(pInstance,FALSE);
    }
    memcpy(&gSrcInfo[eWindow].stStatusnodelay.stDispWin, &gSrcInfo[eWindow].stDispWin, sizeof(MS_WINDOW_TYPE));
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    msAPI_Scaler_SetMode(pInstance, gSrcInfo[eWindow].enInputSourceType, &gSrcInfo[eWindow], eWindow);
/*
    if((s_PQ_Function_Info.pq_set_csc == NULL) || (s_PQ_Function_Info.pq_set_memformat == NULL))
    {
        Hal_SC_set_csc(TRUE, eWindow);
        Hal_SC_set_444To422(ENABLE, eWindow);
        gSrcInfo[eWindow].bUseYUVSpace = TRUE;
        gSrcInfo[eWindow].bMemYUVFmt = TRUE;
    }
*/
    MDrv_SC_set_2p_mode(pInstance, pstXC_SetWin_Info, eWindow);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"============= SetTravelingWindow Done (Window : %u, src: %u) =============\n",eWindow, gSrcInfo[eWindow].enInputSourceType);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return TRUE;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_SetTravelingWindow(XC_SETWIN_INFO *pstXC_SetWin_Info, MS_U32 u32InitDataLen, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

#if (defined (ANDROID))
    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)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }

    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)
        {
            printf("Obtain XC engine fail\n");
            return E_APIXC_RET_FAIL;
        }
    }
#else
    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)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }

    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 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)
        {
            printf("Obtain XC engine fail\n");
            return E_APIXC_RET_FAIL;
        }
    }
#endif

    stXC_SET_TRAVELING_WINDOW XCArgs;
    XCArgs.pstXC_SetWin_Info = pstXC_SetWin_Info;
    XCArgs.u32InitDataLen = u32InitDataLen;
    XCArgs.eWindow = eWindow;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_TRAVELING_WINDOW, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_Combine_MLoadEn
/// @param  bEnable               \b IN: whether to menuload
/// Do an example when you want to menuload OSDLayer :
/// MApi_XC_Combine_MLoadEn(TRUE)
/// call xc function
/// MApi_XC_Combine_MLoadEn(FALSE)
/// @return E_APIXC_RET_OK or E_APIXC_RET_FAIL
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_Combine_MLoadEn_U2(void* pInstance, MS_BOOL bEn)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    MDrv_XC_Combine_MLoadEn(pInstance, bEn);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return E_APIXC_RET_OK;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_Combine_MLoadEn(MS_BOOL bEn)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_MLOAD_COMBINE stXCArgs;
    stXCArgs.bEn = bEn;
    stXCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_MLOAD_COMBINE, (void*)&stXCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return stXCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_SetVideoOnOSD
/// @param  E_VDO_ON_OSD_LAYER               \b IN: set video show on which osd layer
/// @param  eWindow                                     \b IN: set main or sub video data to memory
/// @return E_APIXC_RET_OK or E_APIXC_RET_FAIL
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_SetVideoOnOSD_U2(void* pInstance, E_VIDEO_ON_OSD_LAYER enlayer, SCALER_WIN eWindow)
{
    E_APIXC_ReturnValue bRet = E_APIXC_RET_FAIL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    _XC_ENTRY(pInstance);
    bRet = MDrv_SC_SetVideoOnOSD(pInstance, enlayer, eWindow);
    _XC_RETURN(pInstance);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return bRet;

}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_SetVideoOnOSD(E_VIDEO_ON_OSD_LAYER enlayer, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_VIDEO_ON_OSD XCArgs;
    XCArgs.enlayer = enlayer;
    XCArgs.eWindow = eWindow;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_VIDEO_ON_OSD, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_SetOSDLayerBlending
/// @param  u8Layer               \b IN: which osd layer will blend
/// @param  bEnable               \b IN: whether to blend
/// @param  eWindow               \b IN: which window blend with
/// @return E_APIXC_RET_OK or E_APIXC_RET_FAIL
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_SetOSDLayerBlending_U2(void* pInstance, MS_U8 u8Layer, MS_BOOL bEnable, SCALER_WIN eWindow)
{
    E_APIXC_ReturnValue bRet = E_APIXC_RET_FAIL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    _XC_ENTRY(pInstance);
    bRet = MDrv_SC_SetOSDLayerBlending(pInstance, u8Layer, bEnable, eWindow);
    _XC_RETURN(pInstance);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return bRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_SetOSDLayerBlending(MS_U8 u8Layer, MS_BOOL bEnable, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_OSD_LAYER_BLENDING stXCArgs;
    stXCArgs.u8Layer = u8Layer;
    stXCArgs.bEnable = bEnable;
    stXCArgs.eWindow = eWindow;
    stXCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_OSD_LAYER_BLEANDING, (void*)&stXCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return stXCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_SetOSDLayerAlpha
/// @param  u8Layer                 \b IN: which osd layer will blend
/// @param  u16Alpha                \b IN: blending alpha
/// @return E_APIXC_RET_OK or E_APIXC_RET_FAIL
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_SetOSDLayerAlpha_U2(void* pInstance, MS_U8 u8Layer, MS_U8 u8Alpha)
{
    E_APIXC_ReturnValue bRet = E_APIXC_RET_FAIL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    _XC_ENTRY(pInstance);
    bRet = MDrv_SC_SetOSDLayerAlpha(pInstance, u8Layer, u8Alpha);
    _XC_RETURN(pInstance);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return bRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_SetOSDLayerAlpha(MS_U8 u8Layer, MS_U8 u8Alpha)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_OSD_LAYER_ALPHA stXCArgs;
    stXCArgs.u8Layer = u8Layer;
    stXCArgs.u8Alpha = u8Alpha;
    stXCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_OSD_LAYER_ALPHA, (void*)&stXCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return stXCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_SetOSDBlendingFormula
/// @param  enOsdIndex                              \b IN: osd index
/// @param  enType                                  \b IN: osd blending type
/// @param  eWindow                                 \b IN: blending with main or sub video
/// @return E_APIXC_RET_OK or E_APIXC_RET_FAIL
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_SetOSDBlendingFormula_U2(void* pInstance, E_XC_OSD_INDEX enOsdIndex, E_XC_OSD_BlENDING_TYPE enType, SCALER_WIN eWindow)
{
    E_APIXC_ReturnValue bRet = E_APIXC_RET_FAIL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    _XC_ENTRY(pInstance);
    bRet = MDrv_SC_SetOSDBlendingFormula(pInstance, enOsdIndex, enType, eWindow);
    _XC_RETURN(pInstance);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return bRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_SetOSDBlendingFormula(E_XC_OSD_INDEX enOsdIndex, E_XC_OSD_BlENDING_TYPE enType, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_OSD_BLENDING_FORMULA XCArgs;
    XCArgs.enOsdIndex = enOsdIndex;
    XCArgs.enType = enType;
    XCArgs.eWindow = eWindow;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_OSD_BLENDING_FORMULA, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_SetScaling
/// @param  bEnable                               \b IN: set enable or disable
/// @param  eScaling_type                      \b IN: set scaling type, pre-scaling, post-scaling, both-scaling
/// @param  eVector_type                       \b IN: set H vector, v vector, and HV vector
/// @param  eWindow                             \b IN: set main or sub video data to memory
//-------------------------------------------------------------------------------------------------
void MApi_XC_SetScaling_U2( void* pInstance, MS_BOOL bEnable, E_XC_SCALING_TYPE eScaling_type, E_XC_VECTOR_TYPE eVector_type, SCALER_WIN eWindow )
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    if(eScaling_type == E_XC_PRE_SCALING)
    {
        if(eWindow == MAIN_WINDOW)
        {
            switch(eVector_type)
            {
                case E_XC_H_VECTOR:
                    SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK02_05_L, bEnable<<15 , BIT(15) );
                    break;
                case E_XC_V_VECTOR:
                    SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK02_09_L, bEnable<<15 , BIT(15) );
                    break;
                default:
                case E_XC_HV_VECTOR:
                    SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK02_05_L, bEnable<<15 , BIT(15) );
                    SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK02_09_L, bEnable<<15 , BIT(15) );
                    break;
            }
        }
        else
        {
            switch(eVector_type)
            {
                case E_XC_H_VECTOR:
                    SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK04_05_L, bEnable<<15 , BIT(15) );
                    break;
                case E_XC_V_VECTOR:
                    SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK04_09_L, bEnable<<15 , BIT(15) );
                    break;
                default:
                case E_XC_HV_VECTOR:
                    SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK04_05_L, bEnable<<15 , BIT(15) );
                    SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK04_09_L, bEnable<<15 , BIT(15) );
                    break;
            }
        }
    }
    else if(eScaling_type == E_XC_POST_SCALING)
    {
        if(eWindow == MAIN_WINDOW)
        {
            switch(eVector_type)
            {
                case E_XC_H_VECTOR:
#if defined(SUPPORT_SCALER_EXT_REG) && (SUPPORT_SCALER_EXT_REG == 1)
                    if(MHal_XC_Is_Enable_HFR_Path(pInstance, eWindow))
                    {
                        if(MHal_XC_IsSwitchHFRSubBank() == TRUE)
                        {
                            SC_EXT_SUB_BANK_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_EXT_BKC9_08_L, bEnable<<8 , BIT(8));
                        }
                        else
                        {
                            SC_EXT_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_EXT_BKC9_08_L, bEnable<<8 , BIT(8));
                        }
                    }
                    else
#endif
                    {
                        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_08_L, bEnable<<8 , BIT(8) );
                    }
                    break;
                case E_XC_V_VECTOR:
#if defined(SUPPORT_SCALER_EXT_REG) && (SUPPORT_SCALER_EXT_REG == 1)
                    if(MHal_XC_Is_Enable_HFR_Path(pInstance, eWindow))
                    {
                        if(MHal_XC_IsSwitchHFRSubBank() == TRUE)
                        {
                            SC_EXT_SUB_BANK_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_EXT_BKC9_0A_L, bEnable<<8 , BIT(8));
                        }
                        else
                        {
                            SC_EXT_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_EXT_BKC9_0A_L, bEnable<<8 , BIT(8));
                        }
                    }
                    else
#endif
                    {
                        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_0A_L, bEnable<<8 , BIT(8) );
                    }
                    break;
                default:
                case E_XC_HV_VECTOR:
#if defined(SUPPORT_SCALER_EXT_REG) && (SUPPORT_SCALER_EXT_REG == 1)
                    if(MHal_XC_Is_Enable_HFR_Path(pInstance, eWindow))
                    {
                        if(MHal_XC_IsSwitchHFRSubBank() == TRUE)
                        {
                            SC_EXT_SUB_BANK_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_EXT_BKC9_08_L, bEnable<<8 , BIT(8));
                            SC_EXT_SUB_BANK_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_EXT_BKC9_0A_L, bEnable<<8 , BIT(8));
                        }
                        else
                        {
                            SC_EXT_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_EXT_BKC9_08_L, bEnable<<8 , BIT(8));
                            SC_EXT_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_EXT_BKC9_0A_L, bEnable<<8 , BIT(8));
                        }
                    }
                    else
#endif
                    {
                        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_08_L, bEnable<<8 , BIT(8) );
                        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_0A_L, bEnable<<8 , BIT(8) );
                    }
                    break;
            }
        }
        else
        {
            switch(eVector_type)
            {
                case E_XC_H_VECTOR:
                    SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_28_L, bEnable<<8 , BIT(8) );
                    break;
                case E_XC_V_VECTOR:
                    SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_2A_L, bEnable<<8 , BIT(8) );
                    break;
                default:
                case E_XC_HV_VECTOR:
                    SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_28_L, bEnable<<8 , BIT(8) );
                    SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_2A_L, bEnable<<8 , BIT(8) );
                    break;
            }
        }
    }
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_SetScaling( MS_BOOL bEnable, E_XC_SCALING_TYPE eScaling_type, E_XC_VECTOR_TYPE eVector_type, SCALER_WIN eWindow )
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_SCALING XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.eScaling_type = eScaling_type;
    XCArgs.eVector_type = eVector_type;
    XCArgs.eWindow = eWindow;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_SCALING, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_SetMCDIBufferAddress
/// @param  u32FBAddress                              \b IN: frame buffer base addr
/// @param  u32FBSize                                   \b IN: frame buffer size
/// @param  eType                                         \b IN: set frame buffer for MCDI ME1 or ME2
//-------------------------------------------------------------------------------------------------
void MApi_XC_SetMCDIBufferAddress_U2(void* pInstance, MS_PHY u32FBAddress, MS_PHY u32FBSize, E_XC_MCDI_TYPE eType)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);
    MDrv_SC_SetMCDIBufferAddress(pInstance, u32FBAddress, u32FBSize, eType);
    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_SetMCDIBufferAddress(MS_PHY u32FBAddress, MS_PHY u32FBSize, E_XC_MCDI_TYPE eType)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_MCDI_BUFFERADDRESS XCArgs;
    XCArgs.u32FBAddress = u32FBAddress;
    XCArgs.u32FBSize = u32FBSize;
    XCArgs.eType = eType;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_MCDI_BUFFERADDRESS, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_EnableMCDI
/// @param  bEnable                                      \b IN: enable/disable MCDI
/// @param  eType                                         \b IN: control MCDI ME1 or ME2 or both
//-------------------------------------------------------------------------------------------------
void MApi_XC_EnableMCDI_U2(void* pInstance, MS_BOOL bEnable, E_XC_MCDI_TYPE eType)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);
    MDrv_SC_EnableMCDI(pInstance, bEnable, eType);
    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_EnableMCDI(MS_BOOL bEnable, E_XC_MCDI_TYPE eType)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_MCDI_ENABLE XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.eType = eType;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_MCDI_ENABLE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_SendCmdToFRC
/// @param  u8Cmd                                      \b IN: CMD
/// @param  count                                         \b IN: parameter Counter
/// @param  pFRC_R2_Para                           \b IN: input parameter
/// @return  TRUE if succeed, FALSE if failed
//-------------------------------------------------------------------------------------------------
MS_BOOL MApi_XC_SendCmdToFRC_U2( void* pInstance, MS_U8 u8Cmd, MS_U8 count, FRC_R2_CMD_PARAMETER_t pFRC_R2_Para )
{
    MS_BOOL bret = FALSE;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

#ifdef MSOS_TYPE_LINUX_KERNEL
    if(u8Cmd == E_XC_FRC_MB_CMD_SET_GAME_MODE)
    {
        MS_PQ_CAP_INFO stPQCaps;
        memset(&stPQCaps,0,sizeof(MS_PQ_CAP_INFO));
        if(MDrv_PQ_GetCaps(&stPQCaps))
        {
            if(stPQCaps.u8GameModeVer == 1)
            {
                printf("Does not support FRC GAMEMODE\n");
                return TRUE;
            }
        }
    }
#endif
    bret = MDrv_XC_SendCmdToFRC(pInstance, u8Cmd, count, pFRC_R2_Para);

#if (HW_DESIGN_3D_VER == 4)
    // for 2Dto3D case, the FRC FICLK need to be changed
    //E_MAPI_FRC_MB_CMD_SET_2D_TO_3D_MODE = 0x40,  ParaCnt=5,
    //          P0 = 2D to 3D ConversionMode(0=Disable, 1=Enable)
    //          P1 = 3D Depth Gain(0~31)
    //          P2 = 3D Depth Offset(0~127)
    //          P3 = Artificial Gain(0~15)
    //          P4 = L/R Swap (0=Normal, 1=L/R Swap)
    if(u8Cmd == 0x40)
    {
        if(pFRC_R2_Para.p1==ENABLE)
        {
            W2BYTEMSK(L_CLKGEN1(0x35), 0x0004, 0x0004); // clk_idclk_frc
            W2BYTEMSK(L_CLKGEN1(0x35), 0x0000, 0x0002); // Not Invert
            W2BYTEMSK(L_CLKGEN1(0x35), 0x0000, 0x0001); // Enable clock
        }
        else
        {
            W2BYTEMSK(L_CLKGEN1(0x35), 0x0000, 0x0004); // clk_fdclk_frc
            W2BYTEMSK(L_CLKGEN1(0x35), 0x0000, 0x0002); // Not Invert
            W2BYTEMSK(L_CLKGEN1(0x35), 0x0000, 0x0001); // Enable clock
        }
    }
#endif
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return bret;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_SendCmdToFRC( MS_U8 u8Cmd, MS_U8 count, FRC_R2_CMD_PARAMETER_t pFRC_R2_Para )
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    stXC_SEND_CMD2FRC XCArgs;
    XCArgs.u8Cmd = u8Cmd;
    XCArgs.count = count;
    XCArgs.pFRC_R2_Para = pFRC_R2_Para;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SEND_CMD2FRC, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_GetMsgFromFRC
/// @param  u8Cmd                                      \b IN: CMD
/// @param  pu8ParaCount                            \b IN: Counter
/// @param  pu8Para                                     \b IN: input parameter
/// @return  TRUE if succeed, FALSE if failed
//-------------------------------------------------------------------------------------------------
MS_BOOL MApi_XC_GetMsgFromFRC_U2(void* pInstance, MS_U8* pu8Cmd, MS_U8* pu8ParaCount, MS_U8* pu8Para)
{
    MS_BOOL bret = FALSE;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    bret = MDrv_XC_GetMsgFromFRC(pInstance, pu8Cmd, pu8ParaCount, pu8Para);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return bret;

}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_GetMsgFromFRC(MS_U8* pu8Cmd, MS_U8* pu8ParaCount, MS_U8* pu8Para)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    stXC_GET_MSG_FROM_FRC XCArgs;
    XCArgs.pu8Cmd = pu8Cmd;
    XCArgs.pu8ParaCount = pu8ParaCount;
    XCArgs.pu8Para = pu8Para;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_MSG_FROM_FRC, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif

//-------------------------------------------------------------------------------------------------
/// MApi_XC_GetFrcSwVersion
/// @ingroup XC_FRC
/// @param  @param  pstXC_FRCSWVERSION               \b OUT: the pointer to the struct of ST_XC_FRCSWVERSION.
/// @return  E_APIXC_ReturnValue
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_GetFrcSwVersion_U2(void * pInstance, ST_XC_FRCSWVERSION *pstXCFrcSwVersion)
{
    E_APIXC_ReturnValue enReturn = E_APIXC_RET_FAIL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    ST_XC_FRCSWVERSION stXCFrcSwVersion;
    MS_U16 u16CopiedLength = sizeof(ST_XC_FRCSWVERSION);
    memset(&stXCFrcSwVersion, 0, sizeof(ST_XC_FRCSWVERSION));

    VERSION_COMPATIBLE_CHECK(pstXCFrcSwVersion, stXCFrcSwVersion, u16CopiedLength, u32Version, u32Length, XC_FRCSWVERSION_VERSION, E_APIXC_RET_FAIL);
    pstXCFrcSwVersion->u32Version = XC_FRCSWVERSION_VERSION; /*Return the actual version of the returned value for APP to use*/
    pstXCFrcSwVersion->u32Length = u16CopiedLength; /*Return the actual length of the returned value for APP to know whick info is valid*/

    memcpy(&stXCFrcSwVersion, pstXCFrcSwVersion, u16CopiedLength);
    enReturn = MDrv_XC_GetFrcSwVersion(pInstance, &stXCFrcSwVersion);
    memcpy(pstXCFrcSwVersion, &stXCFrcSwVersion, u16CopiedLength);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return enReturn;
}

#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_GetFrcSwVersion(ST_XC_FRCSWVERSION *pstXCFrcSwVersion)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_FRCSWVERSION XCArgs;
    XCArgs.pstXCFrcSwVersion = pstXCFrcSwVersion;
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;

    if (UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_FRCSWVERSION, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.enReturnValue;
    }

}
#endif

//-------------------------------------------------------------------------------------------------
/// MApi_XC_GetFrcFscCtrlPolicy
/// @ingroup XC
/// @param  @param  pstXCFrcFscCtrlPolicy               \b OUT: the pointer to the struct of ST_XC_FRCFSCCTRLPOLICY.
/// @return  E_APIXC_ReturnValue
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_GetFrcFscCtrlPolicy_U2(void * pInstance, ST_XC_FRCFSCCTRLPOLICY *pstXCFrcFscCtrlPolicy)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    E_APIXC_ReturnValue enReturn = E_APIXC_RET_FAIL;
    ST_XC_FRCFSCCTRLPOLICY stXCFrcFscCtrlPolicy;
    MS_U16 u16CopiedLength = sizeof(ST_XC_FRCFSCCTRLPOLICY);
    memset(&stXCFrcFscCtrlPolicy, 0, sizeof(ST_XC_FRCFSCCTRLPOLICY));

    VERSION_COMPATIBLE_CHECK(pstXCFrcFscCtrlPolicy, stXCFrcFscCtrlPolicy, u16CopiedLength, u32Version, u32Length, XC_FRCFSCCTRLPOLICY_VERSION, E_APIXC_RET_FAIL);
    pstXCFrcFscCtrlPolicy->u32Version = XC_FRCFSCCTRLPOLICY_VERSION; /*Return the actual version of the returned value for APP to use*/
    pstXCFrcFscCtrlPolicy->u32Length = u16CopiedLength; /*Return the actual length of the returned value for APP to know whick info is valid*/

    memcpy(&stXCFrcFscCtrlPolicy, pstXCFrcFscCtrlPolicy, u16CopiedLength);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    enReturn = MDrv_XC_GetFrcFscCtrlPolicy(pInstance, &stXCFrcFscCtrlPolicy);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    memcpy(pstXCFrcFscCtrlPolicy, &stXCFrcFscCtrlPolicy, u16CopiedLength);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return enReturn;
}

#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_GetFrcFscCtrlPolicy(ST_XC_FRCFSCCTRLPOLICY *pstXCFrcFscCtrlPolicy)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_FRCFSCCTRLPOLICY XCArgs;
    XCArgs.pstXCFrcFscCtrlPolicy = pstXCFrcFscCtrlPolicy;
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;

    if (UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_FRCFSCCTRLPOLICY, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.enReturnValue;
    }
}
#endif

//-------------------------------------------------------------------------------------------------
/// set XC bandwidth saving X-mode
/// @param  bEnable                   \b IN: enable or disable
/// @param  eWindow                 \b IN: which window we are going to enable or disable
//-------------------------------------------------------------------------------------------------
void MApi_XC_Set_BWS_Mode_U2(void* pInstance, MS_BOOL bEnable, SCALER_WIN eWindow)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    Hal_SC_set_bws_mode(pInstance, bEnable, eWindow);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_Set_BWS_Mode(MS_BOOL bEnable, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_BWS_MODE XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.eWindow = eWindow;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_BWS_MODE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// set xc FRC ColorPathCtrl
/// @param  epath_type               \b IN: Select the path
/// @param  bEnable                    \b IN: enable or disable
//-------------------------------------------------------------------------------------------------
void MApi_XC_FRC_ColorPathCtrl_U2( void* pInstance, MS_U16 u16Path_sel, MS_BOOL bEnable)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    if(u16Path_sel & FRC_PATHCTRL_OP2COLORMATRIX)
    {
        MDrv_FRC_OP2_ColorMatrixEn(pInstance, bEnable);
    }
    if(u16Path_sel & FRC_PATHCTRL_OP2CSCDITHER)
    {
        MDrv_FRC_OP2_CscDitherEn(pInstance, bEnable);
    }
    if(u16Path_sel & FRC_PATHCTRL_OP2BRIGHTNESS)
    {
        MDrv_FRC_OP2_BrightnessEn(pInstance, bEnable);
    }
    if(u16Path_sel & FRC_PATHCTRL_OP2CONTRAST)
    {
        MDrv_FRC_OP2_ContrastEn(pInstance, bEnable);
    }
    if(u16Path_sel & FRC_PATHCTRL_OP2NOISEROUND)
    {
        MDrv_FRC_OP2_NoiseRoundEn(pInstance, bEnable);
    }

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_FRC_ColorPathCtrl( MS_U16 u16Path_sel, MS_BOOL bEnable)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_FRC_COLOR_PATH_CONTROL XCArgs;
    XCArgs.u16Path_sel = u16Path_sel;
    XCArgs.bEnable = bEnable;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_FRC_COLOR_PATH_CONTROL, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// set XC FRC_OP2 SetRGBGain
/// @param  u16RedGain                 \b IN: Red Gain
/// @param  u16GreenGain              \b IN: Green Gain
/// @param  u16BlueGain                \b IN: Blue Gain
//-------------------------------------------------------------------------------------------------
void MApi_XC_FRC_OP2_SetRGBGain_U2(void* pInstance, MS_U16 u16RedGain, MS_U16 u16GreenGain, MS_U16 u16BlueGain)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    MDrv_FRC_OP2_SetRGBGain(pInstance, u16RedGain,  u16GreenGain,  u16BlueGain);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_FRC_OP2_SetRGBGain(MS_U16 u16RedGain, MS_U16 u16GreenGain, MS_U16 u16BlueGain)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_FRC_OP2_RGBGAIN XCArgs;
    XCArgs.u16RedGain = u16RedGain;
    XCArgs.u16GreenGain = u16GreenGain;
    XCArgs.u16BlueGain = u16BlueGain;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_FRC_OP2_RGBGAIN, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// set XC FRC_OP2 SetRGBOffset
/// @param  u16RedOffset                 \b IN: Red Offset
/// @param  u16GreenOffset              \b IN: Green Offset
/// @param  u16BlueOffset                \b IN: Blue Offset
//-------------------------------------------------------------------------------------------------
void MApi_XC_FRC_OP2_SetRGBOffset_U2(void* pInstance, MS_U16 u16RedOffset, MS_U16 u16GreenOffset, MS_U16 u16BlueOffset)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    MDrv_FRC_OP2_SetRGBOffset(pInstance, u16RedOffset,  u16GreenOffset,  u16BlueOffset);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_FRC_OP2_SetRGBOffset(MS_U16 u16RedOffset, MS_U16 u16GreenOffset, MS_U16 u16BlueOffset)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_FRC_OP2_RGBOFFSET XCArgs;
    XCArgs.u16RedOffset = u16RedOffset;
    XCArgs.u16GreenOffset = u16GreenOffset;
    XCArgs.u16BlueOffset = u16BlueOffset;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_FRC_OP2_RGBOFFSET, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// set XC FRC_OP2 SetDither
/// @param  u16dither                 \b IN: Dither setting
//-------------------------------------------------------------------------------------------------
void MApi_XC_FRC_OP2_SetDither_U2(void* pInstance, MS_U16 u16dither)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    MDrv_FRC_OP2_SetDither(pInstance, u16dither);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_FRC_OP2_SetDither(MS_U16 u16dither)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_FRC_OP2_DITHER XCArgs;
    XCArgs.u16dither = u16dither;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_FRC_OP2_DITHER, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_ForceReadFrame
/// @param  bEnable                                 \b IN: enable/disable keep specified frame
/// @param  u16FrameIndex                           \b IN: control specified frame to show
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_ForceReadFrame_U2(void* pInstance, MS_BOOL bEnable, MS_U16 u16FrameIndex)
{
    E_APIXC_ReturnValue bRet = E_APIXC_RET_FAIL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    _XC_ENTRY(pInstance);

    bRet = MDrv_SC_ForceReadFrame(pInstance, bEnable, u16FrameIndex);

    _XC_RETURN(pInstance);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return bRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_ForceReadFrame(MS_BOOL bEnable, MS_U16 u16FrameIndex)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_FORCE_READ_FRAME XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.u16FrameIndex = u16FrameIndex;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_FORCE_READ_FRAME, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif

E_APIXC_ReturnValue MApi_XC_OSDC_InitSetting_U2(void* pInstance,
                                               E_XC_OSDC_TGEN_Type e_osdctgen_type,
                                               MS_XC_OSDC_TGEN_INFO *pstOC_Tgen_Cus,
                                               MS_XC_OSDC_CTRL_INFO *pstOC_Ctrl)
{

    if(!SUPPORT_OSD_HSLVDS_PATH)
    {
        printf("Please check the osdc capability \n");
        return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
    }
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    gSrcInfo[MAIN_WINDOW].Status2.bIsInitOSDC = TRUE;

    if(e_osdctgen_type == E_XC_OSDC_TGEN_MANUAL)
    {
        if(NULL == pstOC_Tgen_Cus)
        {
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            return E_APIXC_RET_FAIL_INVALID_PARAMETER;
        }

        memcpy(&gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING, pstOC_Tgen_Cus, sizeof(MS_XC_OSDC_TGEN_INFO));
        memcpy(&gSrcInfo[MAIN_WINDOW].Status2.stXCOSDC_CTRL,  pstOC_Ctrl,     sizeof(MS_XC_OSDC_CTRL_INFO));
    }
    else
    {
        switch(e_osdctgen_type)
        {
            case E_XC_OSDC_TGEN_1366x768:
                gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HTotal        = 0x618;
                gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VTotal        = 0x326;

                gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Hsync_Start   = 0x04;
                gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Hsync_End     = 0x06;
                gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HframDE_Start = 0x34;
                gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HframDE_End   = 0x589;

                gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Vsync_Start   = 0x04;
                gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Vsync_End     = 0x06;
                gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VframDE_Start = 0x10;
                gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VframDE_End   = 0x309;
                break;

            case E_XC_OSDC_TGEN_1920x1080:
                if(pstOC_Ctrl->u16OC_Lpll_type == E_XC_PNL_LPLL_HS_LVDS)
                {
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HTotal        = 0x7B4; //0x800;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VTotal        = 0x448; //0x500;

                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Hsync_Start   = 0x04;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Hsync_End     = 0x06;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HframDE_Start = 0x34;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HframDE_End   = 0x7B3;

                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Vsync_Start   = 0x04;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Vsync_End     = 0x06;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VframDE_Start = 0x10;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VframDE_End   = 0x447;
                }
                else
                {
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HTotal        = 0x7FF; //should be odd(total cycle: htt+1),
                                                                                                   //vby one tx need even
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VTotal        = 0x500;

                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Hsync_Start   = 0x04;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Hsync_End     = 0x06;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HframDE_Start = 0x34;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HframDE_End   = 0x7B3;

                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Vsync_Start   = 0x04;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Vsync_End     = 0x06;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VframDE_Start = 0x10;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VframDE_End   = 0x447;

                }
                break;
            case E_XC_OSDC_TGEN_3840x2160:
                if(pstOC_Ctrl->u16OC_Lpll_type == E_XC_PNL_LPLL_HS_LVDS)
                {
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HTotal        = 0xFFF;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VTotal        = 0x982;//0xA00;

                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Hsync_Start   = 0x04;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Hsync_End     = 0x06;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HframDE_Start = 0x34;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HframDE_End   = 0xF33;

                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Vsync_Start   = 0x04;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Vsync_End     = 0x06;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VframDE_Start = 0x10;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VframDE_End   = 0x87F;
                }
                else
                {
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HTotal        = 0x10FF;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VTotal        = 0x900;//0xA00;

                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Hsync_Start   = 0x04;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Hsync_End     = 0x06;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HframDE_Start = 0x34;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HframDE_End   = 0xF33;

                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Vsync_Start   = 0x04;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Vsync_End     = 0x06;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VframDE_Start = 0x10;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VframDE_End   = 0x87F;
                }
                break;

            case E_XC_OSDC_TGEN_3840x1080:
                if(pstOC_Ctrl->u16OC_Lpll_type == E_XC_PNL_LPLL_HS_LVDS)
                {
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HTotal        = 0xFFF;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VTotal        = 0x982/2;//0xA00;

                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Hsync_Start   = 0x04;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Hsync_End     = 0x06;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HframDE_Start = 0x34;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HframDE_End   = 0xF33;

                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Vsync_Start   = 0x04;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Vsync_End     = 0x06;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VframDE_Start = 0x10;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VframDE_End   = (0x87F+1)/2-1;
                }
                else
                {
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HTotal        = 0x10FF;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VTotal        = 0x900/2;//0xA00;

                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Hsync_Start   = 0x04;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Hsync_End     = 0x06;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HframDE_Start = 0x34;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_HframDE_End   = 0xF33;

                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Vsync_Start   = 0x04;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_Vsync_End     = 0x06;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VframDE_Start = 0x10;
                    gSrcInfo[MAIN_WINDOW].Status2.stXCOSDCTIMING.u16OC_Tgen_VframDE_End   = (0x87F+1)/2-1+8;
                }
                break;

            case E_XC_OSDC_TGEN_MAX:
            default:
                break;
        }
        memcpy(&gSrcInfo[MAIN_WINDOW].Status2.stXCOSDC_CTRL, pstOC_Ctrl, sizeof(MS_XC_OSDC_CTRL_INFO));
    }
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return E_APIXC_RET_OK;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_OSDC_InitSetting(E_XC_OSDC_TGEN_Type e_osdctgen_type,
                                             MS_XC_OSDC_TGEN_INFO *pstOC_Tgen_Cus,
                                             MS_XC_OSDC_CTRL_INFO *pstOC_Ctrl)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_OSDC_INITSETTING XCArgs;
    XCArgs.e_osdctgen_type = e_osdctgen_type;
    XCArgs.pstOC_Tgen_Cus = pstOC_Tgen_Cus;
    XCArgs.pstOC_Ctrl = pstOC_Ctrl;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_OSDC_INITSETTING, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
void MApi_XC_OSDC_SetOutVfreqx10_U2(void* pInstance, MS_U16 u16Vfreq)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    pXCResourcePrivate->stdrvXC_MVideo.OSDC_FREQ = u16Vfreq;
    printf("OSDC_FREQ=%u\n",pXCResourcePrivate->stdrvXC_MVideo.OSDC_FREQ);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_OSDC_SetOutVfreqx10(MS_U16 u16Vfreq)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_OSDC_SET_OUTPUTVFREQX10 XCArgs;
    XCArgs.u16Vfreq = u16Vfreq;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_OSDC_SET_OUTPUTVFREQX10, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
E_APIXC_ReturnValue MApi_XC_OSDC_Control_U2(void* pInstance, MS_U32 eCtrl_type)
{
    E_APIXC_ReturnValue eReturn = E_APIXC_RET_FAIL;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
//    MS_U8 u8LPLL_Type = 0;
//    u8LPLL_Type =(MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type;

    eReturn  = MDrv_XC_OSDC_Control(pInstance, eCtrl_type);

    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return eReturn;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_OSDC_Control(MS_U32 eCtrl_type)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_OSDC_CONTROL XCArgs;
    XCArgs.eCtrl_type = eCtrl_type;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_OSDC_CONTROL, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
E_APIXC_ReturnValue MApi_XC_OSDC_GetDstInfo_U2(void* pInstance, MS_OSDC_DST_DispInfo *pDstInfo, MS_U32 u32SizeofDstInfo)
{
    MS_XC_OSDC_TGEN_INFO stOC_Tgen;
    MS_XC_OSDC_CTRL_INFO stOC_Ctrl;

    E_APIXC_ReturnValue eRet = E_APIXC_RET_FAIL;

    if(NULL == pDstInfo)
    {
        printf("MApi_XC_OSDC_GetDstInfo():pDstInfo is NULL\n");
        return E_APIXC_RET_FAIL;
    }
    else if(u32SizeofDstInfo != sizeof(MS_OSDC_DST_DispInfo))
    {
        printf("MApi_XC_OSDC_GetDstInfo():u16SizeofDstInfo is different from the MS_XC_OSDC_DST_DispInfo defined, check header file!\n");
        return E_APIXC_RET_FAIL;
    }

    // the version control is coming in with version 1
    if(pDstInfo->ODSC_DISPInfo_Version < 1)
    {
        //We consider compatible operation form version1 , so reject the info init when version invalid
        XC_LOG_TRACE(XC_DBGLEVEL_SETTIMING, "MApi_XC_OSDC_GetDstInfo: please check your ODSC_DISPInfo_Version, it should not set to 0!!\n")
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

        return E_APIXC_RET_FAIL;
    }

    if(MDrv_XC_OSDC_GetDataFromRegister(pInstance, &stOC_Tgen, &stOC_Ctrl))
    {
        pDstInfo->VDTOT                = stOC_Tgen.u16OC_Tgen_VTotal;
        pDstInfo->DEVST                = stOC_Tgen.u16OC_Tgen_VframDE_Start;
        pDstInfo->DEVEND               = stOC_Tgen.u16OC_Tgen_VframDE_End;
        pDstInfo->HDTOT                = stOC_Tgen.u16OC_Tgen_HTotal;
        pDstInfo->DEHST                = stOC_Tgen.u16OC_Tgen_HframDE_Start;
        pDstInfo->DEHEND               = stOC_Tgen.u16OC_Tgen_HframDE_End;

        pDstInfo->SYNCHST              = stOC_Tgen.u16OC_Tgen_Hsync_Start;
        pDstInfo->SYNCHEND             = stOC_Tgen.u16OC_Tgen_Hsync_End;
        pDstInfo->SYNCVST              = stOC_Tgen.u16OC_Tgen_Vsync_Start;
        pDstInfo->SYNCVEND             = stOC_Tgen.u16OC_Tgen_Vsync_End;

        pDstInfo->bCLK_EN              = stOC_Ctrl.bOC_ClK_En;
        pDstInfo->bMIXER_BYPASS_EN     = stOC_Ctrl.bOC_Mixer_Bypass_En;
        pDstInfo->bMIXER_INVALPHA_EN   = stOC_Ctrl.bOC_Mixer_InvAlpha_En;
        pDstInfo->bMIXER_HSTVFDEOUT_EN = stOC_Ctrl.bOC_Mixer_Hsync_Vfde_Out;
        pDstInfo->bMIXER_HFDEVFDEOUT_EN= stOC_Ctrl.bOC_Mixer_Hfde_Vfde_Out;

        eRet = E_APIXC_RET_OK;
    }
    else
    {
        pDstInfo->VDTOT                = 0;
        pDstInfo->HDTOT                = 0;
        pDstInfo->DEHST                = 0;
        pDstInfo->DEHEND               = 0;
        pDstInfo->DEVST                = 0;
        pDstInfo->DEVEND               = 0;

        pDstInfo->SYNCHST              = 0;
        pDstInfo->SYNCHEND             = 0;
        pDstInfo->SYNCVST              = 0;
        pDstInfo->SYNCVEND             = 0;

        pDstInfo->bCLK_EN              = DISABLE;
        pDstInfo->bMIXER_BYPASS_EN     = DISABLE;
        pDstInfo->bMIXER_INVALPHA_EN   = DISABLE;
        pDstInfo->bMIXER_HSTVFDEOUT_EN = DISABLE;
        pDstInfo->bMIXER_HFDEVFDEOUT_EN= DISABLE;

        eRet = E_APIXC_RET_FAIL;
    }

    return eRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_OSDC_GetDstInfo(MS_OSDC_DST_DispInfo *pDstInfo, MS_U32 u32SizeofDstInfo)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_OSDC_GET_DESTINATION_INFO XCArgs;
    XCArgs.pDstInfo = pDstInfo;
    XCArgs.u32SizeofDstInfo = u32SizeofDstInfo;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_OSDC_GET_DESTINATION_INFO, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif

E_APIXC_ReturnValue MApi_XC_GetVideoLatency_U2(void* pInstance, ST_XC_VIDEO_LATENCY_INFO* pstVideoLatencyInfo, SCALER_WIN enWindow)
{
    E_APIXC_ReturnValue enRet = E_APIXC_RET_FAIL;
    MS_U16 u16ValidLength = 0;
    ST_XC_VIDEO_LATENCY_INFO stVideoLatencyInfo;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    // version handling
    if ((pstVideoLatencyInfo == NULL) || (pstVideoLatencyInfo->u16Length == 0))
    {
        //We cosider compatible operation form version2 , so reject the info init when version invalid
        XC_LOG_TRACE(XC_DBGLEVEL_PARAMETER_TRACE, "MApi_XC_GetVideoLatency: Null paramter or Wrong u32Length!!\n");

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

        return enRet;
    }

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

    //old AP + new lib, driver shouldn't access to the space which doesn't exist in old structure
    if((pstVideoLatencyInfo->u32Version < ST_XC_GET_VIDEO_LATENCY_VERSION) || (pstVideoLatencyInfo->u16Length < sizeof(*pstVideoLatencyInfo)))
    {
        //We cosider compatible operation form version1 , so reject the info init when version invalid
        XC_LOG_TRACE(XC_DBGLEVEL_PARAMETER_TRACE, "MApi_XC_GetVideoLatency: new version XC lib shouldn't access to the space which doesn't exist in old structure!!\n")
        u16ValidLength = pstVideoLatencyInfo->u16Length;
    }
    else
    {
        u16ValidLength = sizeof(*pstVideoLatencyInfo);
    }

    memcpy(&stVideoLatencyInfo, pstVideoLatencyInfo, u16ValidLength);
    stVideoLatencyInfo.u16Length = u16ValidLength;

    _XC_ENTRY(pInstance);

    enRet = MDrv_XC_GetVideoLatency(pInstance,&stVideoLatencyInfo, enWindow);
    memcpy(pstVideoLatencyInfo, &stVideoLatencyInfo, u16ValidLength);

    _XC_RETURN(pInstance);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return enRet;
}

E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_GetVideoLatency(ST_XC_VIDEO_LATENCY_INFO* pstVideoLatencyInfo, SCALER_WIN enWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    //wraper code to call MApi_XC_GetVideoLatency_U2
    stXC_VIDEO_LATENCY_INFO XCArgs;
    XCArgs.pstVideoLatencyInfo = pstVideoLatencyInfo;
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;
    if(enWindow >= MAX_WINDOW)
    {
        XCArgs.enWindow = MAIN_WINDOW;
    }
    else
    {
        XCArgs.enWindow = enWindow;
    }

    // call MApi_XC_GetVideoLatency_U2 by UtopiaIoctl
    if (UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_VIDEO_LATENCY, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return UTOPIA_STATUS_FAIL;
    }
    else
    {
        return XCArgs.enReturnValue;
    }
}

#define LOGTIMECOST  FALSE
XC_PCMONITOR_STATUS _MApi_XC_PCMonitor_Exe(void* pInstance, SCALER_WIN eWindow, INPUT_SOURCE_TYPE_t enInputSourceType)
{
    MApi_XC_PCMonitor_Restart_U2(pInstance,eWindow);
    MS_U32 u32maxRetryTime = XC_STR_PCMONITOR_MAX_TETRY_TIME;
    XC_PCMONITOR_STATUS enPCMonitorStatus = E_XC_PCMONITOR_STABLE_NOSYNC;
    while ( enPCMonitorStatus != E_XC_PCMONITOR_STABLE_SYNC )
    {
        if(u32maxRetryTime == 0)
        {
            printf("\033[1;31mRetry Time out.Signal Unstable or No Signal!!\033[0m\n");
            break;
        }

        enPCMonitorStatus = MApi_XC_PCMonitor_U2(pInstance,enInputSourceType, eWindow);

        if(enPCMonitorStatus == E_XC_PCMONITOR_STABLE_SYNC)
        {
            printf("Signal Stable!!\n");
            break;
        }

        MsOS_DelayTask(10);
        u32maxRetryTime--;
    }
    return enPCMonitorStatus;
}

#ifdef MSOS_TYPE_LINUX_KERNEL
MS_BOOL _MApi_XC_GetStrStatus(MS_U8 u8WinID,MS_BOOL *pbSingnalStable,MS_BOOL *pbMute)
{
 #ifndef MSOS_TYPE_OPTEE
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
#else
    XC_RESOURCE_PRIVATE_FOR_TEE* pXCResourcePrivate = NULL;
#endif

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pu32XCInst_private, (void**)&psXCInstPri);

    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));


    *pbSingnalStable = pXCResourcePrivate->stdrvXC_MVideo.bSignalStableForStr;
    *pbMute = gSrcInfo[u8WinID].bBlackscreenEnabled;
    return TRUE;
}

#if (XC_STR_DTV_ENABLE_THREAD ==1)
static void _XC_PowerResume_Task(MS_U32 u32DeviceId)
{
    void *pInstance  = NULL;
    MS_U8 u8Win = 0;

    if(u32DeviceId == 0)
    {
        pInstance  = pu32XCInst_private;
    }
    else
    {
        pInstance  = pu32XCInst_1_private;
    }

    if(UTOPIA_STATUS_FAIL == UtopiaStrWaitCondition("xc", E_POWER_RESUME, 0))
    {
        bResumeThreadDone[u32DeviceId] = TRUE;
        UtopiaStrSendCondition("xc",E_POWER_RESUME ,0);
        printf("[%s][%d]  XC PowerState:Resume wait Condition failed!!!!\n",__FUNCTION__,__LINE__);
        return;
    }

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
#ifndef MSOS_TYPE_OPTEE
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
#else
    XC_RESOURCE_PRIVATE_FOR_TEE* pXCResourcePrivate = NULL;
#endif
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(u32DeviceId)],(void**)(&pXCResourcePrivate));

    void *pModule;
    //****************to do :  add
    /* Get Str Resource*/
    UtopiaInstanceGetModule(pInstance, &pModule);
    XC_REGS_SAVE_AREA *pXCResourceStr = NULL;
    UtopiaModuleGetSTRPrivate(pModule, (void**)&pXCResourceStr);
    /*2.set_window,*/
    MApi_XC_SetWindow_U2(pInstance,&(pXCResourceStr->pstXC_SetWin_Info[MAIN_WINDOW]), sizeof(XC_SETWIN_INFO), MAIN_WINDOW);
    /*3.set_paneltiming,*/
    MApi_XC_SetPanelTiming_U2(pInstance,&(pXCResourceStr->pTimingInfo), MAIN_WINDOW);
    //pq load Grule need setting after set window
    UtopiaStrSendCondition("xc",E_POWER_RESUME ,0);
#if (SUPPORT_OP2_TEST_PATTERN >0)
    SC_W2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK10_2F_L,pXCResourcePrivate->stdrvXC_MVideo.u16OP2_color_3x3);
#endif
    /*5.fire cfd,*/
///Update CFD Setting when STR on
#if defined(UFO_XC_HDR) && defined(UFO_XC_HDR_VERSION) && (UFO_XC_HDR_VERSION == 2)
    PQ_INPUT_SOURCE_TYPE enPQSourceType=PQ_INPUT_SOURCE_NONE;
    MDrv_XC_ConvertToPQInputSourceType(gSrcInfo[MAIN_WINDOW].enInputSourceType, &enPQSourceType);
    XC_CFD_FIRE stCfdFire;
    memset(&stCfdFire, 0, sizeof(XC_CFD_FIRE));
    if(u32DeviceId == E_XC_DEVICE0)
    {
        stCfdFire.u8Win = MAIN_WINDOW;
    }
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
    else
    {
        stCfdFire.u8Win = SUB_WINDOW;
    }
#endif
    stCfdFire.u32Version = 0;
    stCfdFire.u16Length = sizeof(XC_CFD_FIRE);
    stCfdFire.u8UpdateType = E_XC_CFD_UPDATE_FORCE;
    stCfdFire.bIsRgbBypass = gSrcInfo[MAIN_WINDOW].bForceRGBin;
    stCfdFire.u8InputSource = enPQSourceType;

    XC_CFD_CONTROL_INFO stCfdCtrl;
    memset(&stCfdCtrl, 0, sizeof(XC_CFD_CONTROL_INFO));
    stCfdCtrl.enCtrlType = E_XC_CFD_CTRL_TYPE_FIRE;
    stCfdCtrl.pParam = &stCfdFire;
    stCfdCtrl.u32ParamLen = sizeof(XC_CFD_FIRE);
    MDrv_XC_HDR_Control(pInstance, E_XC_HDR_CTRL_CFD_CONTROL, &stCfdCtrl);
#endif
    /*6.pq delay,*/
    MS_U16 u16PQdelay = 0;
    u16PQdelay=MDrv_PQ_GetDelayTime(PQ_MAIN_WINDOW);
    MsOS_DelayTask(u16PQdelay);

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);

    for(u8Win = 0; u8Win < MAX_WINDOW; u8Win++)
    {
        MDrv_SC_GenerateBlackVideo(pInstance, gSrcInfo[u8Win].bBlackscreenEnabled, (SCALER_WIN)u8Win);
    }

    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    bResumeThreadDone[u32DeviceId] = TRUE;
}

static MS_BOOL _XC_PowerResumeThread_Create(MS_U32 u32DeviceId)
{
    MS_S32 s32TaskId = MsOS_CreateTask(
                (TaskEntry)_XC_PowerResume_Task,
                (MS_VIRT)u32DeviceId,
                E_TASK_PRI_MEDIUM,
                TRUE,
                NULL,
                32,
                "XC_Resume");

    if (s32TaskId < 0)
    {
        printf("Pp Task create failed\n");

        return FALSE;
    }

    return TRUE;
}
#endif
#endif//#ifdef MSOS_TYPE_LINUX_KERNEL

MS_U32 MApi_XC_SetPowerState_U2(void* pInstance, EN_POWER_MODE enPowerState)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
#ifndef MSOS_TYPE_OPTEE
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
#else
    XC_RESOURCE_PRIVATE_FOR_TEE* pXCResourcePrivate = NULL;
#endif

#if LOGTIMECOST
    MS_U32 u32Begin = MsOS_GetSystemTime();
#endif
#ifndef MSOS_TYPE_OPTEE
    MS_U32 u32Ret = UTOPIA_STATUS_FAIL;
    MS_U8 i = 0;
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    if(pXCResourcePrivate->stdrvXC_MVideo.bModuleInited == false)
    {
        printf("[%s,%5d]XC not initial, return.\033[0m\n",__FUNCTION__,__LINE__);
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        return UTOPIA_STATUS_FAIL;
    }

    if (enPowerState == E_POWER_SUSPEND)
    {
        if(pXCResourcePrivate->stdrvXC_MVideo._enPrevPowerState == E_POWER_SUSPEND)
        {
            // Because of MsOS_DisableInterrupt will cause desc->depth + 1
            // MsOS_DisableInterrupt twice will cause kernel desc->depth can not balance.
            // To aviod this case we return here when suspend twice
            printf("[%s,%5d] Can not suspend twice for kernel IRQ depth issue.\n",__FUNCTION__,__LINE__);
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            return UTOPIA_STATUS_FAIL;
        }

        pXCResourcePrivate->stdrvXC_MVideo._enPrevPowerState = enPowerState;
        if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_bEnableCusSTR == FALSE)
        {
            pXCResourcePrivate->stdrvXC_MVideo._bSkipSWReset = FALSE;
#ifdef ADC_AV_CALIBRATION_ONLY_ONCE
            pXCResourcePrivate->sthal_ADC._bIsAVCalibrationDone = FALSE;
#endif


#ifdef MSOS_TYPE_LINUX_KERNEL
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            if(UTOPIA_STATUS_FAIL == UtopiaStrWaitCondition("xc", enPowerState, 0))
            {
                printf("[%s][%d]  XC PowerState:%td wait Condition failed!!!!\n",__FUNCTION__,__LINE__,(ptrdiff_t)enPowerState);
            }
            _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);

            /*
            ** Save setwindow info for str
            */
            void *pModule = NULL;
            UtopiaInstanceGetModule(pInstance, &pModule);
            XC_REGS_SAVE_AREA *pXCResourceStr = NULL;
            UtopiaModuleGetSTRPrivate(pModule, (void**)&pXCResourceStr);
#if (XC_STR_DTV_ENABLE_THREAD ==1)
            if(IsSrcTypeDTV(gSrcInfo[MAIN_WINDOW].enInputSourceType))
            {
                MS_U16 u16ThreadWaitTime = 0;
                while((bResumeThreadStart[psXCInstPri->u32DeviceID] == TRUE)&&(bResumeThreadDone[psXCInstPri->u32DeviceID] == FALSE)&&(u16ThreadWaitTime < 1000))
                {
                    MsOS_DelayTask(1);
                    u16ThreadWaitTime++;
                    if(u16ThreadWaitTime == THREADWAITTIME)
                    {
                        printf("%s,%d kernel thread wait timeout!\n",__FUNCTION__,__LINE__);
                        break;
                    }
                }

                bResumeThreadStart[psXCInstPri->u32DeviceID] = FALSE;
                bResumeThreadDone[psXCInstPri->u32DeviceID] = FALSE;
            }
#endif
#if (SUPPORT_OP2_TEST_PATTERN >0)
            pXCResourcePrivate->stdrvXC_MVideo.u16OP2_color_3x3 = SC_R2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK10_2F_L,0xFFFF);
#endif
            if(MDrv_XC_ADC_GetGainSetting(pInstance, &(pXCResourceStr->stADCSetting)) == FALSE)
            {
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                return UTOPIA_STATUS_FAIL;
            }

            pXCResourcePrivate->stdrvXC_MVideo.bSignalStableForStr = TRUE;

            // Because of in linux kernel mode, XC can do STR without XC_init.
            // Get IO_Base again for protecting
            // (NG flow: without XC init => do STR => kernel call XCStr callback
            // => call MApi_XC_SetPowerState_U2
            // => No _XC_RIU_BASE)
            if (_XC_RIU_BASE == 0)
            {
                if (MDrv_XC_SetIOMapBase_i(pInstance) != TRUE)
                {
                    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "MApi_XC_Init MDrv_XC_SetIOMapBase() failure\n");
                }
                MDrv_XC_SetDeviceOffset(pInstance);
            }
#endif

            #if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6) && MANHATTAN_GOP_HW_BUG_PATCH)
            // For manhattan
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFSCEnabled = FALSE;
            pXCResourcePrivate->stdrvXC_MVideo_Context.g_bFRCEnabled = FALSE;
            _MLOAD_ENTRY(pInstance);
            MHal_FRC_AdjustGOPPosition(pInstance);
            MDrv_XC_MLoad_Fire(pInstance,TRUE);
            _MLOAD_RETURN(pInstance);
            #endif
            MDrv_XC_MLoad_Enable(pInstance,  FALSE);
            MApi_XC_MLoad_Cus_Enable_U2(pInstance,E_MLCLIENT_MAIN_HDR,FALSE);
#if FRC_INSIDE
            // disable FRC_PQ client during suspend flow to avoid MIU hit
            MHal_FRC_Set_TimingStable(pInstance, FALSE);
#endif

            // disable FRC during suspend flow to avoid FRC_R2 MIU hit
            MDrv_FRC_ByPass_Enable(pInstance,  TRUE);

            //DISABLE Interrupt
            if(E_XC_DEVICE0 == psXCInstPri->u32DeviceID)
            {
                MsOS_DisableInterrupt(XC_MAIN_SC_IRQ);
            }
            else if(E_XC_DEVICE1 == psXCInstPri->u32DeviceID)
            {
                MsOS_DisableInterrupt(E_INT_IRQ_EC_BRIDGE);
                MsOS_DisableInterrupt(E_INT_IRQ_DISP1);
            }

            if(TRUE == MDrv_SC_check_interruptVSINT())
            {
                MDrv_SC_set_interrupt(pInstance, SC_INT_VSINT, DISABLE); //Mask Output Vsync for Hanging issue for CPU to bussy (Mantis 1180314)
            }

#if 0
#ifndef MSOS_TYPE_LINUX_KERNEL
            // Release ISR
            if(E_XC_DEVICE0 == psXCInstPri->u32DeviceID)
            {
            MsOS_DetachInterrupt(XC_MAIN_SC_IRQ);
            }
            else if(E_XC_DEVICE1 == psXCInstPri->u32DeviceID)
            {
                MsOS_DetachInterrupt(E_INT_IRQ_EC_BRIDGE);
                MsOS_DetachInterrupt(E_INT_IRQ_DISP1);
            }
#endif
#endif

#ifdef MSOS_TYPE_LINUX_KERNEL

#ifndef DONT_USE_CMA
#if (XC_SUPPORT_CMA ==TRUE)
            if (  ((gSrcInfo[MAIN_WINDOW].u32PreCMAMemSCMSize[CMA_XC_SELF_MEM]+gSrcInfo[MAIN_WINDOW].u32PreCMAMemFRCMSize[CMA_XC_SELF_MEM])!=0)
                &&((gSrcInfo[MAIN_WINDOW].u32PreCMAMemSCMSize[CMA_XC_COBUFF_MEM]+gSrcInfo[MAIN_WINDOW].u32PreCMAMemFRCMSize[CMA_XC_COBUFF_MEM])!=0))
            {
                // signal stable,but cma buff is not use
                // setwindow will be done when str resume.
                pXCResourcePrivate->stdrvXC_MVideo.bSignalStableForStr = FALSE;
            }

#endif
#endif

#endif
        }
        u32Ret = UTOPIA_STATUS_SUCCESS;
    }
    else if (enPowerState == E_POWER_RESUME)
    {
        if (pXCResourcePrivate->stdrvXC_MVideo._enPrevPowerState == E_POWER_SUSPEND)
        {
#ifdef CONFIG_UTOPIA_PROC_DBG_SUPPORT
            if(gbStrPatternEnable == TRUE)
            {
                _XCMdbloctl_TestPattern(pInstance, gu8PatternIP, TRUE, MAIN_WINDOW, 0, 0, 0);
            }
#endif
            pXCResourcePrivate->stdrvXC_Scaling._au32PreFBAddress[MAIN_WINDOW] = 0xFFFFFFFF;
            pXCResourcePrivate->stdrvXC_Scaling._au32PreFBSize[MAIN_WINDOW] = 0x0;
            pXCResourcePrivate->stdrvXC_Scaling._au32PreFBAddress[SUB_WINDOW] = 0xFFFFFFFF;
            pXCResourcePrivate->stdrvXC_Scaling._au32PreFBSize[SUB_WINDOW] = 0x0;
            pXCResourcePrivate->stdrvXC_Scaling._u32PreDualFBAddress[MAIN_WINDOW] = 0xFFFFFFFF;
            pXCResourcePrivate->stdrvXC_Scaling._u32PreDualFBSize[MAIN_WINDOW] = 0x0;
            pXCResourcePrivate->stdrvXC_Scaling._u32PreDualFBAddress[SUB_WINDOW] = 0xFFFFFFFF;
            pXCResourcePrivate->stdrvXC_Scaling._u32PreDualFBSize[SUB_WINDOW] = 0x0;

            #if FRC_INSIDE
            pXCResourcePrivate->stdrvXC_MVideo_Context.bIsFRCInited = FALSE;
            #endif

            if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_bEnableCusSTR == FALSE)
            {
                for(i = 0; i < WINDOW_NUM; i++)
                {
                    MDrv_SC_GenerateBlackVideo(pInstance, TRUE, (SCALER_WIN)i);
                }

                _MApi_XC_Init_WithoutCreateMutex(pInstance, &pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData, pXCResourcePrivate->stdrvXC_MVideo_Context.g_u32InitDataLen_Suspend);

                //start offline monitor when system init
                //dump mux val for every ip1
                MDrv_XC_SetFSSEnable(pInstance, ENABLE, MAIN_WINDOW);

                MDrv_XC_OSDC_Control(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_u32OSDCCtrltype_Suspend);
            }

            MDrv_XC_Init_MISC(pInstance, &pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc, pXCResourcePrivate->stdrvXC_MVideo_Context.g_u32InitMiscDataLen_Suspend);

            if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_bEnableCusSTR == FALSE)
            {
                if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_u32MLoadBufByteLen_Suspend > 0)
                {
#if (SUPPORT_KERNEL_MLOAD == 1)
                    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                    //dc on should do menuload init
                    pXCResourcePrivate->stdrvXC_MVideo_Context.stdrvXC_Menuload[psXCInstPri->u32DeviceID].g_bMLoadInitted = FALSE;
                    MApi_XC_MLoad_Init_U2(pInstance,
                    pXCResourcePrivate->stdrvXC_MVideo_Context.g_u32MLoadPhyAddr_Suspend,
                    pXCResourcePrivate->stdrvXC_MVideo_Context.g_u32MLoadBufByteLen_Suspend);
                    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
#else
                    _MLOAD_ENTRY(pInstance);
                    //dc on should do menuload init
                    pXCResourcePrivate->stdrvXC_MVideo_Context.stdrvXC_Menuload[psXCInstPri->u32DeviceID].g_bMLoadInitted = FALSE;
                    MDrv_XC_MLoad_Init(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_u32MLoadPhyAddr_Suspend);
                    _MLOAD_RETURN(pInstance);
#endif
                }

                for(i = 0; i < E_XC_MCDI_MAX; i++)
                {
                    if((pXCResourcePrivate->stdrvXC_MVideo_Context.g_u32MCDIFBSize_Suspend[i] > 0) && (i < E_XC_MCDI_SUB_BOTH))
                    {
                        pXCResourcePrivate->stdrvXC_Display._u32MCDIFBAddress[i]=0xFFFFFFFF;
                        pXCResourcePrivate->stdrvXC_Display._u32MCDIFBSize[i]=0x0;
                        MDrv_SC_SetMCDIBufferAddress(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_u32MCDIFBAddress_Suspend[i],
                                                     pXCResourcePrivate->stdrvXC_MVideo_Context.g_u32MCDIFBSize_Suspend[i],
                                                     (E_XC_MCDI_TYPE)i);
                    }
                    if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_bEnableMCDI_Suspend[i])
                    {
                        MDrv_SC_EnableMCDI(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_bEnableMCDI_Suspend[i], (E_XC_MCDI_TYPE)i);
                    }
                }

                for(i = 0; i < WINDOW_NUM; i++)
                {
                    if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_u8DispWindowColor_Suspend[i] > 0)
                    {
                        MDrv_XC_SetDispWindowColor(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_u8DispWindowColor_Suspend[i], (SCALER_WIN)i);
                    }
                }

                if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_u32FrameColor_Suspend > 0)
                {
                    MDrv_XC_SetFrameColor(pInstance, pXCResourcePrivate->stdrvXC_MVideo_Context.g_u32FrameColor_Suspend);
                }

#ifndef MSOS_TYPE_LINUX_KERNEL
                MDrv_SC_SetSkipWaitVsync(pInstance, MAIN_WINDOW, TRUE);
                MDrv_SC_SetSkipWaitVsync(pInstance, SUB_WINDOW , TRUE);
#endif

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

#ifdef MSOS_TYPE_LINUX_KERNEL
                    MDrv_SC_GenerateBlackVideo(pInstance, TRUE, (SCALER_WIN)i);
#else
                    MDrv_SC_GenerateBlackVideo(pInstance, gSrcInfo[i].bBlackscreenEnabled, (SCALER_WIN)i);
#endif
                }

#ifndef MSOS_TYPE_LINUX_KERNEL
                MDrv_SC_SetSkipWaitVsync(pInstance, MAIN_WINDOW, FALSE);
                MDrv_SC_SetSkipWaitVsync(pInstance, SUB_WINDOW , FALSE);
#endif

                #if (LD_ENABLE == 1)
                // Local Dimming Recovery procedure
                // In Local Dimming situation, we don't know if AP has a mmap for Local Dimming
                // we can only judge it by checking if it is inited so we will try to resume.
                if (pXCResourcePrivate->stdrv_LD._bMDrvLD_InitFinished == TRUE)
                {
                    MDrv_XC_LD_Set_MIUSel(pInstance, pXCResourcePrivate->stdrv_LD.gsLDDramBaseAddr.u8MIU);

                    MDrv_XC_LD_SetMemoryAddress(pInstance, pXCResourcePrivate->stdrv_LD.gsLDDramBaseAddr.u32LDFReadBuf0Base,
                                                pXCResourcePrivate->stdrv_LD.gsLDDramBaseAddr.u32LDFReadBuf1Base,
                                                pXCResourcePrivate->stdrv_LD.gsLDDramBaseAddr.u32LDBWriteBuf0Base,
                                                pXCResourcePrivate->stdrv_LD.gsLDDramBaseAddr.u32LDBWriteBuf1Base,
                                                pXCResourcePrivate->stdrv_LD.gsLDDramBaseAddr.u32Edge2DCoeffBase,
                                                (pXCResourcePrivate->stdrv_LD.gsLDDramBaseAddr.u32SPIDataBuf0Base
                                                 -pXCResourcePrivate->stdrv_LD.gsLDDramBaseAddr.u32LDBWriteBuf0Base));

                    MDrv_XC_LD_Init(pInstance, pXCResourcePrivate->stdrv_LD._enLDPanelType);

                    // Must call or otherwise our local dimming report value will be only 1 pixel
                    // but actually we want it to be the whole screen so we divided it into 32*18 areas of the panel screen.
                    MS_U8 u8LDData[576] = {0};
                    MDrv_LD_Get_Value(pInstance, u8LDData, 32, 18);
                }
#endif
#ifdef UFO_XC_HDR
#if (UFO_XC_HDR_VERSION == 2)
                if(pXCResourcePrivate->stdrvXC_Display._stDMAInitInfo.phyBaseAddr != 0)
                {
                    MDrv_XC_HDR_Control(pInstance, E_XC_HDR_CTRL_INIT_DMA, (void*) &pXCResourcePrivate->stdrvXC_Display._stDMAInitInfo);
                }
#endif
#endif
                //pXCResourcePrivate->stdrvXC_MVideo._enPrevPowerState = enPowerState;

#ifdef MSOS_TYPE_LINUX_KERNEL
                if(E_XC_DEVICE0 == psXCInstPri->u32DeviceID)
                {
                    void *pModule;
                    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                    //****************to do :  add
                    /* Get Str Resource*/
                    UtopiaInstanceGetModule(pInstance, &pModule);
                    XC_REGS_SAVE_AREA *pXCResourceStr = NULL;
                    UtopiaModuleGetSTRPrivate(pModule, (void**)&pXCResourceStr);
                    /*1.create_mux,*/
                    MS_S16 s16PathId;
                    INPUT_SOURCE_TYPE_t currentSrc[WINDOW_NUM] = {INPUT_SOURCE_NONE};
                    //get path data from shm
                    memset(&pXCResourceStr->Path_Info[0],0,sizeof(XC_MUX_PATH_INFO)*MAX_DATA_PATH_SUPPORTED);
                    for(i=0;i<MAX_DATA_PATH_SUPPORTED;i++)
                    {
                        if(pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].u8PathStatus & DATA_PATH_USING)
                        {
                            pXCResourceStr->Path_Info[i].Path_Type                 = pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].e_PathType;
                            pXCResourceStr->Path_Info[i].src                       = pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].src;
                            pXCResourceStr->Path_Info[i].dest                      = pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].dest;
                            pXCResourceStr->Path_Info[i].path_thread               = pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].path_thread;
                            pXCResourceStr->Path_Info[i].SyncEventHandler          = pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].synchronous_event_handler;
                            pXCResourceStr->Path_Info[i].DestOnOff_Event_Handler   = pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].dest_on_off_event_handler;
                            pXCResourceStr->Path_Info[i].dest_periodic_handler     = pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].dest_periodic_handler;
                            pXCResourceStr->u8PathStatus[i]                         =  pXCResourcePrivate->stdrvXC_MVideo.s_InputSource_Mux_attr.Mux_DataPaths[i].u8PathStatus;
                            s16PathId = MApi_XC_Mux_DeletePath_U2(pInstance,pXCResourceStr->Path_Info[i].src, pXCResourceStr->Path_Info[i].dest);
                            if(s16PathId >=0)
                            {
                                XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE,"Delete Path Success !!\n");
                                s16PathId = MApi_XC_Mux_CreatePath_U2(pInstance,&(pXCResourceStr->Path_Info[i]), sizeof(XC_MUX_PATH_INFO) );
                                if(s16PathId != -1)
                                {
                                    if( MApi_XC_Mux_EnablePath_U2(pInstance,s16PathId) != -1 )
                                    {
                                        if(pXCResourceStr->Path_Info[i].dest == OUTPUT_SCALER_MAIN_WINDOW)
                                        {
                                            pXCResourcePrivate->stdrvXC_MVideo.enOldInputSrc_ForScalerSetSource[MAIN_WINDOW] = INPUT_SOURCE_NONE;
                                            MApi_XC_SetInputSource_U2(pInstance,pXCResourceStr->Path_Info[i].src, MAIN_WINDOW);
                                            currentSrc[MAIN_WINDOW] = pXCResourceStr->Path_Info[i].src;
                                        }
                                        else if((pXCResourceStr->Path_Info[i].dest == OUTPUT_SCALER_SUB_WINDOW)&&pXCResourceStr->bPIP_enable)
                                        {
                                            pXCResourcePrivate->stdrvXC_MVideo.enOldInputSrc_ForScalerSetSource[SUB_WINDOW] = INPUT_SOURCE_NONE;
                                            MApi_XC_SetInputSource_U2(pInstance,pXCResourceStr->Path_Info[i].src, SUB_WINDOW);
                                            currentSrc[SUB_WINDOW] = pXCResourceStr->Path_Info[i].src;
                                        }
                                        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE," Enable Path Success \n");
                                    }
                                    else
                                    {
                                        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE," Enable Path Error \n");
                                        u32Ret = UTOPIA_STATUS_FAIL;
                                    }
                                }else
                                {
                                    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE,"Create Path Error !!\n");
                                    u32Ret = UTOPIA_STATUS_FAIL;
                                }
                            }
                            else
                            {
                                XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE,"Delete Path Error !!\n");
                            }


                        }
                    }
                    MApi_XC_DisableInputSource_U2(pInstance,DISABLE, MAIN_WINDOW);

                    if(IsSrcTypeYPbPr(gSrcInfo[MAIN_WINDOW].enInputSourceType))
                    {
                        // sw patch, enable power because it was disabled in adc init
                        MDrv_XC_ADC_SetPLLPD(pInstance, TRUE);
                        _MApi_XC_Mux_SetPath(pInstance, INPUT_SOURCE_YPBPR, OUTPUT_SCALER_MAIN_WINDOW);
                    }

                    //if(pXCResourceStr->bPIP_enable)
                    //{
                    //    MApi_XC_DisableInputSource_U2(pInstance,DISABLE, SUB_WINDOW);
                    //}
#ifndef DONT_USE_CMA
#if (XC_SUPPORT_CMA ==TRUE)
                    if (  ((gSrcInfo[MAIN_WINDOW].u32PreCMAMemSCMSize[CMA_XC_SELF_MEM]+gSrcInfo[MAIN_WINDOW].u32PreCMAMemFRCMSize[CMA_XC_SELF_MEM])==0)
                        &&((gSrcInfo[MAIN_WINDOW].u32PreCMAMemSCMSize[CMA_XC_COBUFF_MEM]+gSrcInfo[MAIN_WINDOW].u32PreCMAMemFRCMSize[CMA_XC_COBUFF_MEM])==0))
                    {
                        // setwindow will be done when str resume.
                        pXCResourcePrivate->stdrvXC_MVideo.bSignalStableForStr = FALSE;
                    }
#endif
#endif
                    //enable mload lastly for saving  time when power resume
                    //when you need to add code,put them ahead of here
                    //dtv resume doing in thread,put mload ahead in case influence pq,ace etc
                    if((pXCResourcePrivate->stdrvXC_MVideo_Context.g_u32MLoadBufByteLen_Suspend > 0) &&
                        IsSrcTypeDTV(gSrcInfo[MAIN_WINDOW].enInputSourceType))
                    {
                        _MLOAD_ENTRY(pInstance);
                        MDrv_XC_MLoad_Enable(pInstance,  TRUE);
                        MApi_XC_MLoad_Cus_Enable_U2(pInstance,E_MLCLIENT_MAIN_HDR,TRUE);
                        _MLOAD_RETURN(pInstance);
                    }

                    if(pXCResourcePrivate->stdrvXC_MVideo.bSignalStableForStr)
                    {
                        pXCResourcePrivate->stdrvXC_MVideo.bSignalStableForStr = FALSE;
                        // wait xc front module resume done
                        if(IsSrcTypeYPbPr(gSrcInfo[MAIN_WINDOW].enInputSourceType) ||IsSrcTypeVga(gSrcInfo[MAIN_WINDOW].enInputSourceType)
                            ||IsSrcTypeHDMI(gSrcInfo[MAIN_WINDOW].enInputSourceType))
                        {
                            //get signal status
                            XC_IP_SYNC_STATUS  sXC_Sync_Status = {0};
                            MS_U16 u16DelayTime = 0;
                            MS_U16 u16DelayTimeTemp = 0;
                            MS_U16 u16PreVototal = 0,u16PreHperiod = 0;

                            if(UTOPIA_STATUS_FAIL == UtopiaStrWaitCondition("xc", enPowerState, 0))
                            {
                                printf("[%s][%d]  XC PowerState:%td wait Condition failed!!!!\n",__FUNCTION__,__LINE__,(ptrdiff_t)enPowerState);
                            }

                            MDrv_XC_GetSyncStatus(pInstance,gSrcInfo[MAIN_WINDOW].enInputSourceType,&sXC_Sync_Status,MAIN_WINDOW);
                            if(gSrcInfo[MAIN_WINDOW].u16InputVFreq > 0)
                            {
                                u16DelayTime = (DELAY_FRAME_NUM)*(1000/(gSrcInfo[MAIN_WINDOW].u16InputVFreq/10));
                            }
                            else
                            {
                                XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE,"InputVFreq is zero, attention!!\n");
                                u16DelayTime = 0;
                            }

                            while(((sXC_Sync_Status.u8SyncStatus & XC_MD_HSYNC_LOSS_BIT)||(sXC_Sync_Status.u8SyncStatus & XC_MD_VSYNC_LOSS_BIT))
                                        &&(u16DelayTimeTemp < u16DelayTime))
                            {
                                MsOS_DelayTask(1);
                                u16DelayTimeTemp++;
                                MDrv_XC_GetSyncStatus(pInstance,gSrcInfo[MAIN_WINDOW].enInputSourceType,&sXC_Sync_Status,MAIN_WINDOW);
                            }

                            if(u16DelayTimeTemp > u16DelayTime)
                            {
                                    printf("[%s,%d] Signal detect  Time Out!\n",__FUNCTION__,__LINE__);
                                    pXCResourcePrivate->stdrvXC_MVideo.bSignalStableForStr = FALSE;
                            }
                            else
                            {
                                u16PreVototal = pXCResourcePrivate->stdrvXC_IP.sPCMONITOR_status[MAIN_WINDOW].u16Vtotal;
                                u16PreHperiod = pXCResourcePrivate->stdrvXC_IP.sPCMONITOR_status[MAIN_WINDOW].u16Hperiod;

                                if(_MApi_XC_PCMonitor_Exe(pInstance,MAIN_WINDOW, gSrcInfo[MAIN_WINDOW].enInputSourceType) == E_XC_PCMONITOR_STABLE_SYNC)
                                {
                                    if((abs(u16PreVototal -pXCResourcePrivate->stdrvXC_IP.sPCMONITOR_status[MAIN_WINDOW].u16Vtotal)>MD_VTOTAL_TORLANCE)
                                      ||((abs(u16PreHperiod -pXCResourcePrivate->stdrvXC_IP.sPCMONITOR_status[MAIN_WINDOW].u16Hperiod)>MD_HPERIOD_TORLANCE)))
                                    {
                                        printf("[%s,%d] Timing has changed!\n",__FUNCTION__,__LINE__);
                                        pXCResourcePrivate->stdrvXC_MVideo.bSignalStableForStr = FALSE;
                                        pXCResourcePrivate->stdrvXC_MVideo.bTimingUnstableForStr = TRUE;
                                    }
                                    else
                                    {
                                        pXCResourcePrivate->stdrvXC_MVideo.bSignalStableForStr = TRUE;
                                    }
                                }
                                else
                                {
                                    pXCResourcePrivate->stdrvXC_MVideo.bSignalStableForStr = FALSE;
                                    pXCResourcePrivate->stdrvXC_MVideo.bTimingUnstableForStr = TRUE;
                                }
                            }
                        }
                        else if(IsSrcTypeDigitalVD(gSrcInfo[MAIN_WINDOW].enInputSourceType))
                        {
                            pXCResourcePrivate->stdrvXC_MVideo.bSignalStableForStr = TRUE;

                            if(UTOPIA_STATUS_FAIL == UtopiaStrWaitCondition("xc", enPowerState, 0))
                            {
                                printf("[%s][%d]  XC PowerState:%td wait Condition failed!!!!\n",__FUNCTION__,__LINE__,(ptrdiff_t)enPowerState);
                            }
                            const char Vd_Key[] = "timing change";
                            char Value[255];

                            if(UTOPIA_STATUS_SUCCESS == (UtopiaStrGetData((char*)&Vd_Key,(char*)&Value)))
                            {
                                if(0 == strcmp(Value,"yes" ))
                                {
                                    printf("[%s,%d] Timing has changed!\n",__FUNCTION__,__LINE__);
                                    pXCResourcePrivate->stdrvXC_MVideo.bSignalStableForStr = FALSE;
                                }
                            }
                        }
                        else if(IsSrcTypeDTV(gSrcInfo[MAIN_WINDOW].enInputSourceType))
                        {
#if (XC_STR_DTV_ENABLE_THREAD ==1)
                            bResumeThreadStart[psXCInstPri->u32DeviceID] = TRUE;
                            _XC_PowerResumeThread_Create(psXCInstPri->u32DeviceID);
                            u32Ret = UTOPIA_STATUS_SUCCESS;
                            pXCResourcePrivate->stdrvXC_MVideo._enPrevPowerState = enPowerState;
                            return u32Ret;
#else
                            if(UTOPIA_STATUS_FAIL == UtopiaStrWaitCondition("xc", E_POWER_RESUME, 0))
                            {
                                printf("[%s][%d]  XC PowerState:Resume wait Condition failed!!!!\n",__FUNCTION__,__LINE__);
                                pXCResourcePrivate->stdrvXC_MVideo.bSignalStableForStr = FALSE;
                            }
                            else
                            {
                                pXCResourcePrivate->stdrvXC_MVideo.bSignalStableForStr = TRUE;
                            }
#endif
                        }

                        if(pXCResourcePrivate->stdrvXC_MVideo.bSignalStableForStr)
                        {
                            /*2.set_window,*/
                            if(MApi_XC_SetWindow_U2(pInstance,&(pXCResourceStr->pstXC_SetWin_Info[MAIN_WINDOW]), sizeof(XC_SETWIN_INFO), MAIN_WINDOW) ==FALSE)
                            {
                                XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE," Set Window Error \n");
                            }
                            /*3.set_paneltiming,*/
                            MApi_XC_SetPanelTiming_U2(pInstance,&(pXCResourceStr->pTimingInfo), MAIN_WINDOW);
#if (SUPPORT_OP2_TEST_PATTERN >0)
                            SC_W2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK10_2F_L,pXCResourcePrivate->stdrvXC_MVideo.u16OP2_color_3x3);
#endif
                        }
                    }

                    //if(pXCResourceStr->bPIP_enable)
                    //{
                    //    if(MApi_XC_SetWindow_U2(pInstance,&(pXCResourceStr->pstXC_SetWin_Info[SUB_WINDOW]), sizeof(XC_SETWIN_INFO), SUB_WINDOW) ==FALSE)
                    //    {
                    //        u32Ret = UTOPIA_STATUS_FAIL;
                    //    }
                    //}
                    if(currentSrc[MAIN_WINDOW] == INPUT_SOURCE_VGA)
                        {
                        MApi_XC_ADC_AdjustGainOffset_U2(pInstance, &(pXCResourceStr->stADCSetting));
                    }

                    /// For CVBS out, recover ADC table
                    if(pXCResourcePrivate->stdrvXC_MVideo.bSignalStableForStr)
                    {
                        if(IsSrcTypeATV(gSrcInfo[MAIN_WINDOW].enInputSourceType) || IsSrcTypeDTV(gSrcInfo[MAIN_WINDOW].enInputSourceType) || IsSrcTypeSV(gSrcInfo[MAIN_WINDOW].enInputSourceType))
                        {
                            MApi_XC_ADC_SetCVBSOut_U2(pInstance, TRUE, OUTPUT_CVBS1 , gSrcInfo[MAIN_WINDOW].enInputSourceType, TRUE);
                        }
                    }

                    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
                }
#endif
                u32Ret = UTOPIA_STATUS_SUCCESS;
                //enable mload lastly for saving  time when power resume
                //when you need to add code,put them ahead of here
                if(pXCResourcePrivate->stdrvXC_MVideo_Context.g_u32MLoadBufByteLen_Suspend > 0)
                {
                    _MLOAD_ENTRY(pInstance);
                    MDrv_XC_MLoad_Enable(pInstance,  TRUE);
                    MApi_XC_MLoad_Cus_Enable_U2(pInstance,E_MLCLIENT_MAIN_HDR,TRUE);
                    _MLOAD_RETURN(pInstance);
                }
#if ((PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB == 0) && (MAX_XC_DEVICE_NUM == 2))
                if(psXCInstPri->u32DeviceID == E_XC_DEVICE1)
                {
                    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                    XC_SETWIN_INFO XC_SetWin_Info;
                    MS_S16 s16PathId;
                    XC_MUX_PATH_INFO PathInfo;
                    XC_MUX_PATH_INFO AllPathInfo[MAX_SYNC_DATA_PATH_SUPPORTED];
                    memset(&PathInfo, 0, sizeof(XC_MUX_PATH_INFO));
                    memset(AllPathInfo, 0, sizeof(XC_MUX_PATH_INFO)*MAX_SYNC_DATA_PATH_SUPPORTED);

                    gSrcInfo[MAIN_WINDOW].stDispWin.x          = 0;
                    gSrcInfo[MAIN_WINDOW].stDispWin.y          = 0;

                    XC_SetWin_Info.enInputSourceType           = gSrcInfo[MAIN_WINDOW].enInputSourceType;

                    XC_SetWin_Info.stCapWin.x                  = gSrcInfo[MAIN_WINDOW].stCapWin.x;
                    XC_SetWin_Info.stCapWin.y                  = gSrcInfo[MAIN_WINDOW].stCapWin.y;
                    XC_SetWin_Info.stCapWin.width              = gSrcInfo[MAIN_WINDOW].stCapWin.width;
                    XC_SetWin_Info.stCapWin.height             = gSrcInfo[MAIN_WINDOW].stCapWin.height;

                    XC_SetWin_Info.stDispWin.x                 = gSrcInfo[MAIN_WINDOW].stDispWin.x;
                    XC_SetWin_Info.stDispWin.y                 = gSrcInfo[MAIN_WINDOW].stDispWin.y;
                    XC_SetWin_Info.stDispWin.width             = gSrcInfo[MAIN_WINDOW].stDispWin.width;
                    XC_SetWin_Info.stDispWin.height            = gSrcInfo[MAIN_WINDOW].stDispWin.height;

                    XC_SetWin_Info.stCropWin.x                 = gSrcInfo[MAIN_WINDOW].stCropWin.x;
                    XC_SetWin_Info.stCropWin.y                 = gSrcInfo[MAIN_WINDOW].stCropWin.y;
                    XC_SetWin_Info.stCropWin.width             = gSrcInfo[MAIN_WINDOW].stCropWin.width;
                    XC_SetWin_Info.stCropWin.height            = gSrcInfo[MAIN_WINDOW].stCropWin.height;

                    XC_SetWin_Info.bInterlace                  = gSrcInfo[MAIN_WINDOW].bInterlace;
                    XC_SetWin_Info.bHDuplicate                 = gSrcInfo[MAIN_WINDOW].bHDuplicate;
                    XC_SetWin_Info.u16InputVFreq               = gSrcInfo[MAIN_WINDOW].u16InputVFreq;
                    XC_SetWin_Info.u16InputVTotal              = gSrcInfo[MAIN_WINDOW].u16InputVTotal;
                    XC_SetWin_Info.u16DefaultHtotal            = gSrcInfo[MAIN_WINDOW].u16DefaultHtotal;
                    XC_SetWin_Info.u16DefaultPhase             = gSrcInfo[MAIN_WINDOW].u16DefaultPhase;

                    XC_SetWin_Info.bHCusScaling                = gSrcInfo[MAIN_WINDOW].bHCusScaling;
                    XC_SetWin_Info.u16HCusScalingSrc           = gSrcInfo[MAIN_WINDOW].u16HCusScalingSrc;
                    XC_SetWin_Info.u16HCusScalingDst           = gSrcInfo[MAIN_WINDOW].u16HCusScalingDst;
                    XC_SetWin_Info.bVCusScaling                = gSrcInfo[MAIN_WINDOW].bVCusScaling;
                    XC_SetWin_Info.u16VCusScalingSrc           = gSrcInfo[MAIN_WINDOW].u16VCusScalingSrc;
                    XC_SetWin_Info.u16VCusScalingDst           = gSrcInfo[MAIN_WINDOW].u16VCusScalingDst;
                    XC_SetWin_Info.bDisplayNineLattice         = gSrcInfo[MAIN_WINDOW].bDisplayNineLattice;

                    XC_SetWin_Info.bPreHCusScaling             = gSrcInfo[MAIN_WINDOW].Status2.bPreHCusScaling;
                    XC_SetWin_Info.bPreVCusScaling             = gSrcInfo[MAIN_WINDOW].Status2.bPreVCusScaling;
                    XC_SetWin_Info.u16PreHCusScalingSrc        = gSrcInfo[MAIN_WINDOW].Status2.u16PreHCusScalingSrc;
                    XC_SetWin_Info.u16PreHCusScalingDst        = gSrcInfo[MAIN_WINDOW].Status2.u16PreHCusScalingDst;
                    XC_SetWin_Info.u16PreVCusScalingSrc        = gSrcInfo[MAIN_WINDOW].Status2.u16PreVCusScalingSrc;
                    XC_SetWin_Info.u16PreVCusScalingDst        = gSrcInfo[MAIN_WINDOW].Status2.u16PreVCusScalingDst;
                    XC_SetWin_Info.u16DefaultPhase             = gSrcInfo[MAIN_WINDOW].u16DefaultPhase;

                    XC_SetWin_Info.bSetDispWinToReg            = FALSE;

                    PathInfo.Path_Type = PATH_TYPE_SYNCHRONOUS;
                    PathInfo.src = INPUT_SOURCE_SCALER_OP;
                    PathInfo.dest = OUTPUT_SCALER_MAIN_WINDOW;
                    PathInfo.SyncEventHandler = NULL;
                    PathInfo.DestOnOff_Event_Handler = NULL;
                    PathInfo.path_thread = NULL;

                    s16PathId = MApi_XC_Mux_DeletePath_U2(pInstance,PathInfo.src, PathInfo.dest);
                    if(s16PathId >=0)
                    {
                        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE,"Delete Path Success !!\n");
                        s16PathId = MApi_XC_Mux_CreatePath_U2(pInstance,&PathInfo, sizeof(XC_MUX_PATH_INFO) );
                        if(s16PathId != -1)
                        {
                            XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE," Create Path Success \n");
                            if( MApi_XC_Mux_EnablePath_U2(pInstance,s16PathId) != -1 )
                            {
                                if(PathInfo.dest == OUTPUT_SCALER_MAIN_WINDOW)
                                {
                                    pXCResourcePrivate->stdrvXC_MVideo.enOldInputSrc_ForScalerSetSource[MAIN_WINDOW] = INPUT_SOURCE_NONE;
                                    MApi_XC_SetInputSource_U2(pInstance,PathInfo.src, MAIN_WINDOW);
                                }
                                XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE," Enable Path Success \n");
                            }
                            else
                            {
                                XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE," Enable Path Error \n");
                                u32Ret = UTOPIA_STATUS_FAIL;
                            }
                        }
                        else
                        {
                            XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE,"Create Path Error !!\n");
                            u32Ret = UTOPIA_STATUS_FAIL;
                        }
                    }
                    else
                    {
                        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE,"Delete Path Error !!\n");
                    }

                    MApi_XC_DisableInputSource_U2(pInstance,DISABLE, MAIN_WINDOW);
                    MApi_XC_SetWindow_U2(pInstance, &XC_SetWin_Info, sizeof(XC_SETWIN_INFO), MAIN_WINDOW);

                    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
                }
#endif

                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"============= XC PowerState CFD Resume Start  =============\n");
    ///Update CFD Setting when STR on
#if defined(UFO_XC_HDR) && defined(UFO_XC_HDR_VERSION) && (UFO_XC_HDR_VERSION == 2)
                PQ_INPUT_SOURCE_TYPE enPQSourceType=PQ_INPUT_SOURCE_NONE;
                MDrv_XC_ConvertToPQInputSourceType(gSrcInfo[MAIN_WINDOW].enInputSourceType, &enPQSourceType);
                XC_CFD_FIRE stCfdFire;
                memset(&stCfdFire, 0, sizeof(XC_CFD_FIRE));
                if(psXCInstPri->u32DeviceID == E_XC_DEVICE0)
                {
                    stCfdFire.u8Win = MAIN_WINDOW;
                }
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
                else
                {
                    stCfdFire.u8Win = SUB_WINDOW;
                }
#endif
                stCfdFire.u32Version = 0;
                stCfdFire.u16Length = sizeof(XC_CFD_FIRE);
                stCfdFire.u8UpdateType = E_XC_CFD_UPDATE_FORCE;
                stCfdFire.bIsRgbBypass = gSrcInfo[MAIN_WINDOW].bForceRGBin;
                stCfdFire.u8InputSource = enPQSourceType;

                XC_CFD_CONTROL_INFO stCfdCtrl;
                memset(&stCfdCtrl, 0, sizeof(XC_CFD_CONTROL_INFO));
                stCfdCtrl.enCtrlType = E_XC_CFD_CTRL_TYPE_FIRE;
                stCfdCtrl.pParam = &stCfdFire;
                stCfdCtrl.u32ParamLen = sizeof(XC_CFD_FIRE);
                MDrv_XC_HDR_Control(pInstance, E_XC_HDR_CTRL_CFD_CONTROL, &stCfdCtrl);
#endif
                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"============= XC PowerState CFD Resume End  =============\n");
            }
        }
        else
        {
            printf("[%s,%5d]It is not suspended yet. We shouldn't resume\n",__FUNCTION__,__LINE__);
            u32Ret = UTOPIA_STATUS_FAIL;
        }
        pXCResourcePrivate->stdrvXC_MVideo._enPrevPowerState = enPowerState;
    }
    else
    {
        printf("[%s,%5d]Do Nothing: %u\n",__FUNCTION__,__LINE__,enPowerState);
        u32Ret = UTOPIA_STATUS_FAIL;
    }

    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
#if LOGTIMECOST
    printf("=========[%s,%5d] cost %lu ms========\n", __FUNCTION__, __LINE__, MsOS_GetSystemTime() - u32Begin);
#endif

    return u32Ret;

#else  ////For Optee STR
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));


    if (enPowerState == E_POWER_SUSPEND)
    {
        ///Do nothing for optee suspend
    }
    else if (enPowerState == E_POWER_RESUME)
    {
        if(pXCResourcePrivate->sthal_Optee.op_tee_xc[MAIN_WINDOW].isEnable == TRUE)
        {
            if((E_XC_DEVICE0 == psXCInstPri->u32DeviceID)
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
              ||(E_XC_DEVICE1 == psXCInstPri->u32DeviceID)
#endif
              )
            {
#if 0 //which will be locked by seal side
                Hal_SC_secure_lock(pInstance, MAIN_WINDOW, TRUE, TRUE);
#endif
                /// recorver for FB adress REG

                MDrv_XC_OPTEE_Control(pInstance,E_XC_OPTEE_UPDATE_HANDLER,&pXCResourcePrivate->sthal_Optee.op_tee_xc[MAIN_WINDOW]);
            }
        }
        else if(pXCResourcePrivate->sthal_Optee.op_tee_xc[SUB_WINDOW].isEnable == TRUE)
        {
#if 0 //which will be locked by seal side
            Hal_SC_secure_lock(pInstance, SUB_WINDOW, TRUE, TRUE);
#endif
            MDrv_XC_OPTEE_Control(pInstance,E_XC_OPTEE_UPDATE_HANDLER,&pXCResourcePrivate->sthal_Optee.op_tee_xc[SUB_WINDOW]);
        }
    }

    return UTOPIA_STATUS_SUCCESS;

#endif
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_U32 MApi_XC_SetPowerState(EN_POWER_MODE enPowerState)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return UTOPIA_STATUS_FAIL;
    }

    stXC_SET_POWERSTATE XCArgs;
    XCArgs.enPowerState = enPowerState;
    XCArgs.u32ReturnValue = UTOPIA_STATUS_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_POWERSTATE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return UTOPIA_STATUS_FAIL;
    }

#if (MAX_XC_DEVICE_NUM == 2) // Support SC1
    if (pu32XCInst_1 == NULL)
    {
        printf("[%s,%5d] No SC1 instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return UTOPIA_STATUS_FAIL;
    }

    XCArgs.enPowerState = enPowerState;
    XCArgs.u32ReturnValue = UTOPIA_STATUS_FAIL;

    if(UtopiaIoctl(pu32XCInst_1, E_XC_CMD_SET_POWERSTATE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return UTOPIA_STATUS_FAIL;
    }
#endif

    return XCArgs.u32ReturnValue;
}
#endif

//-------------------------------------------------------------------------------------------------
/// MApi_XC_SetByPassOSDVsyncPos
/// @param  u16VsyncStart                              \b IN: OSD vsync start
/// @param  u16VsyncEnd                                \b IN: OSD vsync end
//-------------------------------------------------------------------------------------------------
void MApi_XC_BYPASS_SetOSDVsyncPos_U2(void* pInstance, MS_U16 u16VsyncStart, MS_U16 u16VsyncEnd)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    _XC_ENTRY(pInstance);

    MHal_XC_ByPass_SetOSDVsyncPos(pInstance, u16VsyncStart, u16VsyncEnd);

    _XC_RETURN(pInstance);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_BYPASS_SetOSDVsyncPos(MS_U16 u16VsyncStart, MS_U16 u16VsyncEnd)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_BYPASS_OSDVSYNC_POS XCArgs;
    XCArgs.u16VsyncStart = u16VsyncStart;
    XCArgs.u16VsyncEnd = u16VsyncEnd;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_BYPASS_OSDVSYNC_POS, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_BYPASS_SetInputSrc
/// @param  bEnable                              \b IN: ENABLE/DISABLE bypass mode
/// @param  input                                \b IN: Select the input source for bypass mode
//-------------------------------------------------------------------------------------------------
void MApi_XC_BYPASS_SetInputSrc_U2(void* pInstance, MS_BOOL bEnable, E_XC_BYPASS_InputSource input)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    MHal_XC_BYPASS_Setinputclk(pInstance, bEnable,input);

    _XC_ENTRY(pInstance);
    MHal_XC_Bypass_SetinputSrc(pInstance, input);
    _XC_RETURN(pInstance);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_BYPASS_SetInputSrc(MS_BOOL bEnable, E_XC_BYPASS_InputSource input)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_SET_BYPASS_INPUTSRC XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.input = input;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_BYPASS_INPUTSRC, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_SetSeamlessZapping
/// @param  eWindow                 \b IN: which window we are going to enable or disable
/// @param  bEnable                 \b IN: ENABLE/DISABLE seamless zapping
/// @return E_APIXC_RET_OK or E_APIXC_RET_FAIL
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_SetSeamlessZapping_U2(void* pInstance, SCALER_WIN eWindow, MS_BOOL bEnable)
{
    E_APIXC_ReturnValue eRet = E_APIXC_RET_FAIL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    if(!SUPPORT_SEAMLESS_ZAPPING)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return E_APIXC_RET_FAIL;
    }
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    if(!MDrv_XC_GetDynamicScalingStatus(pInstance))
    {
        if(!bEnable)
        {
            eRet = E_APIXC_RET_OK;
        }
        else
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SEAMLESSZAP, "[%s][%06d] Dynamic scaling is not initialized\n", __FUNCTION__, __LINE__);
            eRet = E_APIXC_RET_FAIL;
        }
    }
    else
    {
        _XC_ENTRY(pInstance);
        if(bEnable)
        {
            MDrv_XC_Set_DSIndexSourceSelect(pInstance, E_XC_DS_INDEX_SCALER, eWindow);
        }
        else
        {
            MDrv_XC_Set_DSIndexSourceSelect(pInstance, E_XC_DS_INDEX_MVOP, eWindow);
        }

        eRet = (E_APIXC_ReturnValue)Hal_SC_SetSeamlessZapping(pInstance, eWindow, bEnable);
        XC_LOG_TRACE(XC_DBGLEVEL_SEAMLESSZAP, "[%s][%06d] set Seamless Zapping[%s] = %x\n", __FUNCTION__, __LINE__, (eWindow==SUB_WINDOW)?"SUB":"MAIN", bEnable);

        _XC_RETURN(pInstance);
    }
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return eRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_SetSeamlessZapping(SCALER_WIN eWindow, MS_BOOL bEnable)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_SEAMLESS_ZAPPING XCArgs;
    XCArgs.eWindow = eWindow;
    XCArgs.bEnable = bEnable;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_SEAMLESS_ZAPPING, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif

E_APIXC_ReturnValue MApi_XC_GetSeamlessZappingStatus_U2(void* pInstance, SCALER_WIN eWindow, MS_BOOL* pbEnable)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    E_APIXC_ReturnValue eReturnValue = E_APIXC_RET_FAIL;

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    *pbEnable = Hal_SC_GetSeamlessZappingStatus(pInstance, eWindow);
    XC_LOG_TRACE(XC_DBGLEVEL_SEAMLESSZAP, "[%s][%06d] get Seamless Zapping[%s] = %x\n", __FUNCTION__, __LINE__, (eWindow == SUB_WINDOW)?"SUB":"MAIN", *pbEnable);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    eReturnValue = E_APIXC_RET_OK;

    return eReturnValue;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_GetSeamlessZappingStatus(SCALER_WIN eWindow, MS_BOOL* pbEnable)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_GET_SEAMLESS_ZAPPING_STATUS XCArgs;
    XCArgs.eWindow = eWindow;
    XCArgs.pbEnable = pbEnable;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_SEAMLESS_ZAPPING_STATUS, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_Vtrack_SetPayloadData
/// @param  u16Timecode                       \b IN: The time code is coded on 16 bits. It is calculated based on the number of days from January 1st.
/// @param  u8ID                              \b IN: Operator ID
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_Vtrack_SetPayloadData_U2(void* pInstance, MS_U16 u16Timecode, MS_U8 u8OperatorID)
{
    E_APIXC_ReturnValue eReturn = E_APIXC_RET_FAIL;
    _MLOAD_ENTRY(pInstance);
    eReturn = MHal_XC_Vtrack_SetPayloadData(pInstance, u16Timecode, u8OperatorID);
    _MLOAD_RETURN(pInstance);
    return eReturn;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_Vtrack_SetPayloadData(MS_U16 u16Timecode, MS_U8 u8OperatorID)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_VTRACK_PAYLOADDATA XCArgs;
    XCArgs.u16Timecode = u16Timecode;
    XCArgs.u8OperatorID = u8OperatorID;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_VTRACK_PAYLOADDATA, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_Vtrack_SetAllPayloadData
/// @param  vtrack_payload                       \b IN: Vtrack payload data
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_Vtrack_SetAllPayloadData_U2(void* pInstance, XC_VTRACK_PAYLOAD *vtrack_payload)
{
    E_APIXC_ReturnValue eReturn = E_APIXC_RET_FAIL;
    _MLOAD_ENTRY(pInstance);
    eReturn = MHal_XC_Vtrack_SetAllPayloadData(pInstance, vtrack_payload);
    _MLOAD_RETURN(pInstance);
    return eReturn;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_Vtrack_SetAllPayloadData(XC_VTRACK_PAYLOAD *vtrack_payload)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }
    stXC_SET_VTRACK_ALLPAYLOADDATA XCArgs;
    XCArgs.vtrack_payload = vtrack_payload;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_VTRACK_ALLPAYLOADDATA, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_Vtrack_SetVtrackKey
/// @param  u64VtrackKey                       \b IN: Vtrack Key
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_Vtrack_SetVtrackKey_U2(void* pInstance, MS_U64 u64VtrackKey)
{
    E_APIXC_ReturnValue eReturn = E_APIXC_RET_FAIL;
    _MLOAD_ENTRY(pInstance);
    eReturn = MHal_XC_Vtrack_SetVtrackKey(pInstance, u64VtrackKey);
    _MLOAD_RETURN(pInstance);
    return eReturn;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_Vtrack_SetVtrackKey(MS_U64 u64VtrackKey)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_VTRACK_VTRACKKEY XCArgs;
    XCArgs.u64VtrackKey= u64VtrackKey;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_VTRACK_VTRACKKEY, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_Vtrack_SetUserDefindedSetting
/// @param  bEnable                              \b IN: ENABLE/DISABLE Customized Setting
/// @param  pu8Setting                           \b IN: pointer to targert user definded setting data (include setting1 to setting3)
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_Vtrack_SetUserDefindedSetting_U2(void *pInstance, MS_BOOL bUserDefinded, MS_U8 *pu8Setting)
{
    E_APIXC_ReturnValue eReturn = E_APIXC_RET_FAIL;
    _MLOAD_ENTRY(pInstance);
    eReturn = MHal_XC_Vtrack_SetUserDefindedSetting(pInstance, bUserDefinded, pu8Setting);
    _MLOAD_RETURN(pInstance);
    return eReturn;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_Vtrack_SetUserDefindedSetting(MS_BOOL bUserDefinded, MS_U8 *pu8Setting)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_VTRACK_USERDEFINED_SETTING XCArgs;
    XCArgs.bUserDefinded = bUserDefinded;
    XCArgs.pu8Setting = pu8Setting;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_VTRACK_USERDEFINED_SETTING, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_Vtrack_Enable
/// @param  u8FrameRate                          \b IN: ENABLE/DISABLE The FrameRateIn is based on the output format of the box
/// @param  bEnable                              \b IN: ENABLE/DISABLE Vtrack
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_Vtrack_Enable_U2(void* pInstance, MS_U8 u8FrameRate, MS_BOOL bEnable)
{
    E_APIXC_ReturnValue eReturn = E_APIXC_RET_FAIL;
    _MLOAD_ENTRY(pInstance);
    eReturn = MHal_XC_Vtrack_Enable(pInstance, u8FrameRate, bEnable);
    _MLOAD_RETURN(pInstance);
    return eReturn;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_Vtrack_Enable(MS_U8 u8FrameRate, MS_BOOL bEnable)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_VTRACK_ENABLE XCArgs;
    XCArgs.u8FrameRate = u8FrameRate;
    XCArgs.bEnable = bEnable;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_VTRACK_ENABLE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Pre-set the specific window with PQ info
/// @param  pstXC_SetWin_Info      \b IN: the information of the window setting
/// @param  eWindow                \b IN: which window we are going to set
//-------------------------------------------------------------------------------------------------
void MApi_XC_PreSetPQInfo_U2(void* pInstance, XC_SETWIN_INFO *pstXC_SetWin_Info, SCALER_WIN eWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    MS_U32 u32DisPOddCheck = 0x00;
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    MDrv_SC_SetPQHSDFlag(pInstance, FALSE); //Reset variable

    if(pstXC_SetWin_Info->bInterlace)
    {
        //The component 1080i can't frame lock when capture start is odd.
#ifdef K3_U2
        if(!IsSrcTypeDTV( pstXC_SetWin_Info->enInputSourceType ))
#endif
        {
            // the Capture start should be even
            if(pstXC_SetWin_Info->stCapWin.y%2)
            {
                pstXC_SetWin_Info->stCapWin.y += 1;
                pstXC_SetWin_Info->stCapWin.height -= 1; //Y+1, so height-1 to keep not capture outside
            }
        }

        // the Capture height should be even
        if(pstXC_SetWin_Info->stCapWin.height%2)
        {
            pstXC_SetWin_Info->stCapWin.height -= 1;
        }
    }

    // copy user data to internal data
    // here is a patch first, will remove after seperate Internal XC status with set window information
    //u32CopyLen = sizeof(XC_SETWIN_INFO) - (sizeof(MS_BOOL)*2) - (sizeof(MS_U16)*4);
    //memcpy(&gSrcInfo[eWindow], pstXC_SetWin_Info, u32CopyLen);
    //do not use memcopy(), strongly depend on structure definition
    _Mdrv_XC_CopySetWinInfo(pInstance, pstXC_SetWin_Info, eWindow);

    // The total pixel conut of display window can not be odd.
    u32DisPOddCheck = gSrcInfo[eWindow].stDispWin.width * gSrcInfo[eWindow].stDispWin.height;
    // if u32DisPOddCheck is odd, that means both of disp.width and disp.height is odd.
    // we adjust width to even.
    if (u32DisPOddCheck & 0x01)
    {
        if (pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width > gSrcInfo[eWindow].stDispWin.width )
        {
            gSrcInfo[eWindow].stDispWin.width = ( gSrcInfo[eWindow].stDispWin.width + 1 ) & ~0x1;
        }
    }

    // if mirror, need to re-cal display window but Ninelattice!!
    if((IsVMirrorMode(eWindow) || IsHMirrorMode(eWindow)) &&(!gSrcInfo[eWindow].bDisplayNineLattice))
    {
        MS_U32 u32TempWidth = gSrcInfo[eWindow].stDispWin.width;
        MS_U32 u32TempHeight = gSrcInfo[eWindow].stDispWin.height;

        if(IsHMirrorMode(eWindow))
        {
            if( pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width > ( gSrcInfo[eWindow].stDispWin.x + u32TempWidth ) )
            {
                gSrcInfo[eWindow].stDispWin.x =
                    pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width - ( gSrcInfo[eWindow].stDispWin.x + u32TempWidth );
            }
            else
            {
                gSrcInfo[eWindow].stDispWin.x = 0;
            }
        }

        if(IsVMirrorMode(eWindow))
        {
            if( pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height > ( gSrcInfo[eWindow].stDispWin.y + u32TempHeight ) )
            {
                gSrcInfo[eWindow].stDispWin.y =
                    pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height - ( gSrcInfo[eWindow].stDispWin.y + u32TempHeight );
            }
            else
            {
                gSrcInfo[eWindow].stDispWin.y = 0;
            }
        }
    }

    MDrv_SC_3D_FormatValidation(pInstance, &gSrcInfo[eWindow], eWindow);
    MDrv_SC_3D_AdjustHShift(pInstance, &gSrcInfo[eWindow], eWindow);

    gSrcInfo[eWindow].stDispWin.x += pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HStart;
    gSrcInfo[eWindow].stDispWin.y += pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VStart;

    // dump debug msg
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"============= SetWindow Start (Window : %u, src: %u) =============\n",eWindow, gSrcInfo[eWindow].enInputSourceType);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"CapWin  x: %4u  y: %4u  w: %4u  h: %4u \n",gSrcInfo[eWindow].stCapWin.x, gSrcInfo[eWindow].stCapWin.y, gSrcInfo[eWindow].stCapWin.width, gSrcInfo[eWindow].stCapWin.height);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"CropWin x: %4u  y: %4u  w: %4u  h: %4u \n",gSrcInfo[eWindow].stCropWin.x, gSrcInfo[eWindow].stCropWin.y, gSrcInfo[eWindow].stCropWin.width, gSrcInfo[eWindow].stCropWin.height);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"DispWin x: %4u  y: %4u  w: %4u  h: %4u \n",gSrcInfo[eWindow].stDispWin.x,gSrcInfo[eWindow].stDispWin.y, gSrcInfo[eWindow].stDispWin.width, gSrcInfo[eWindow].stDispWin.height);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Panel   x: %4u  y: %4u  w: %4u  h: %4u \n", pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16HStart
                                                                                  , pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16VStart
                                                                                  , pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width
                                                                                  , pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Mirror/Interlace/Hdup = %u/%u/%u\n", IsVMirrorMode(eWindow), pstXC_SetWin_Info->bInterlace, pstXC_SetWin_Info->bHDuplicate);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"H/V total = (%u, %u), VFreq = %u\n", pstXC_SetWin_Info->u16DefaultHtotal, pstXC_SetWin_Info->u16InputVTotal, pstXC_SetWin_Info->u16InputVFreq);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Pre  H cus scaling %u (%u -> %u)\n", gSrcInfo[eWindow].Status2.bPreHCusScaling, gSrcInfo[eWindow].Status2.u16PreHCusScalingSrc, gSrcInfo[eWindow].Status2.u16PreHCusScalingDst);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Pre  V cus scaling %u (%u -> %u)\n", gSrcInfo[eWindow].Status2.bPreVCusScaling, gSrcInfo[eWindow].Status2.u16PreVCusScalingSrc, gSrcInfo[eWindow].Status2.u16PreVCusScalingDst);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Post H cus scaling %u (%u -> %u)\n", gSrcInfo[eWindow].bHCusScaling, gSrcInfo[eWindow].u16HCusScalingSrc, gSrcInfo[eWindow].u16HCusScalingDst);
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"Post V cus scaling %u (%u -> %u)\n", gSrcInfo[eWindow].bVCusScaling, gSrcInfo[eWindow].u16VCusScalingSrc, gSrcInfo[eWindow].u16VCusScalingDst);
    memcpy(&gSrcInfo[eWindow].stStatusnodelay.stDispWin, &gSrcInfo[eWindow].stDispWin, sizeof(MS_WINDOW_TYPE));
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    // Scaler must update source before width / height checking.
    _MDrv_XC_Set_PQ_SourceData(pInstance, eWindow, gSrcInfo[eWindow].enInputSourceType, &gSrcInfo[eWindow]);
    msAPI_Scaler_SetMode(pInstance, gSrcInfo[eWindow].enInputSourceType, &gSrcInfo[eWindow], eWindow);

}
#ifndef UTOPIAXP_REMOVE_WRAPPER
void MApi_XC_PreSetPQInfo(XC_SETWIN_INFO *pstXC_SetWin_Info, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return;
    }

    stXC_PRESET_PQINFO XCArgs;
    XCArgs.pstXC_SetWin_Info = pstXC_SetWin_Info;
    XCArgs.eWindow = eWindow;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_PRESET_PQINFO, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return;
    }
    else
    {
        return;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// Mapi_XC_Is_OP1_TestPattern_Enabled
/// @return MS_BOOL: TRUE - enabled, FALSED - disabled
//-------------------------------------------------------------------------------------------------
MS_BOOL MApi_XC_Is_OP1_TestPattern_Enabled_U2(void* pInstance)
{
#ifndef DISABLE_HW_PATTERN_FUNCTION
    #ifdef SUPPORT_HW_TESTPATTERN
        XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
        UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
        XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
        MS_BOOL bReturn = FALSE;
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
        UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
        bReturn = pXCResourcePrivate->stdrvXC_MVideo.bIsHWPatternEnabled;
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        return bReturn;
    #else
        return FALSE;
    #endif
#else
    return FALSE;
#endif
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_Is_OP1_TestPattern_Enabled(void)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    stXC_GET_OP1_TESTPATTERN_ENABLED XCArgs;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_GET_OP1_TESTPATTERN_ENABLED, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_Set_OP1_TestPattern
/// @param  ePattern                                      \b IN: select test pattern
/// @param  eMode                                         \b IN: choose the patgen mode
/// @return E_APIXC_RET_OK or E_APIXC_RET_FAIL or E_APIXC_RET_FAIL_FUN_NOT_SUPPORT
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_Set_OP1_TestPattern_U2(void* pInstance, EN_XC_OP1_PATTERN ePattern, EN_XC_OP1_PATGEN_MODE eMode)
{
    E_APIXC_ReturnValue enResult = E_APIXC_RET_FAIL;

#ifndef DISABLE_HW_PATTERN_FUNCTION

#ifdef SUPPORT_HW_TESTPATTERN
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    _XC_ENTRY(pInstance);
    if (FALSE == pXCResourcePrivate->stdrvXC_MVideo.bIsHWPatternEnabled)
    {
        pXCResourcePrivate->stdrvXC_MVideo.bIsInputSrcDisabled = Hal_SC_Is_InputSource_Disable(pInstance, MAIN_WINDOW) >> 7;
        pXCResourcePrivate->stdrvXC_MVideo.bIsMute = SC_R2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK10_19_L, BIT(1)) >> 1;
#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
        if (IsVBY1_16LANE((MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type))
        {
            pXCResourcePrivate->stdrvXC_MVideo_Context.bIsFRCMute = SC_R2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKCB_17_L, BIT(1)) >> 1;
        }
#endif
    }

    if (E_XC_OP1_PATTERN_OFF == ePattern)
    {
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK10_19_L, BIT(1),BIT(1)); //window color en : on
#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
        if (IsVBY1_16LANE((MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type))
        {
            SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKCB_17_L, BIT(1),BIT(1));
        }
#endif

        MDrv_SC_OP1_PatGen_Disable(pInstance);

        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK10_19_L, (pXCResourcePrivate->stdrvXC_MVideo.bIsMute << 1),BIT(1));
#if (FRC_INSIDE && (HW_DESIGN_4K2K_VER == 6))
        if (IsVBY1_16LANE((MS_U8)pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.eLPLL_Type))
        {
            SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BKCB_17_L, (pXCResourcePrivate->stdrvXC_MVideo_Context.bIsFRCMute << 1),BIT(1));
        }
#endif
#if (XC_SUPPORT_FASTSWITCH_BY_IP1GP==TRUE)
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, MHal_XC_FSS_BankTransfer(pInstance, REG_SC_BK01_02_L, MAIN_WINDOW), (pXCResourcePrivate->stdrvXC_MVideo.bIsInputSrcDisabled << 7),BIT(7));
#else
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK01_02_L, (pXCResourcePrivate->stdrvXC_MVideo.bIsInputSrcDisabled << 7),BIT(7));
#endif
        pXCResourcePrivate->stdrvXC_MVideo.bIsHWPatternEnabled = FALSE;
    }
    else
    {
        MDrv_SC_Set_OP1_TestPattern(pInstance, ePattern, eMode);
        pXCResourcePrivate->stdrvXC_MVideo.bIsHWPatternEnabled = TRUE;
    }
    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    enResult = E_APIXC_RET_OK;
#else
    enResult = E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

#else
    enResult = E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif


    return enResult;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_Set_OP1_TestPattern(EN_XC_OP1_PATTERN ePattern, EN_XC_OP1_PATGEN_MODE eMode)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_OP1_TESTPATTERN XCArgs;
    XCArgs.ePattern = ePattern;
    XCArgs.eMode = eMode;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_SET_OP1_TESTPATTERN, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif

//-------------------------------------------------------------------------------------------------
/// MApi_XC_Set_OP2_Pattern
/// @param  bEnable               \b IN: enable/disable OP2  pattern
/// @param  u16R_Data             \b IN: when enable OP2  patter ,Data Generator for r_cr constant
/// @param  u16G_Data             \b IN: when enable OP2  patter ,Data Generator for g_y constant
/// @param  u16B_Data             \b IN: when enable OP2  patter ,Data Generator for b_cb constant
/// @return E_APIXC_RET_OK or E_APIXC_RET_FAIL or E_APIXC_RET_FAIL_FUN_NOT_SUPPORT
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_Set_OP2_Pattern_U2(void* pInstance, MS_BOOL bEnable, MS_U16 u16R_Data, MS_U16 u16G_Data ,MS_U16 u16B_Data)
{
    E_APIXC_ReturnValue enResult = E_APIXC_RET_OK;
    #if (SUPPORT_OP2_TEST_PATTERN >0)
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    _XC_ENTRY(pInstance);
    Hal_SC_vop2_Set_TestPattern(pInstance, bEnable,u16R_Data,u16G_Data,u16B_Data);
    _XC_RETURN(pInstance);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Eixt\n");
    #endif
    return enResult;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_Set_OP2_Pattern(MS_BOOL bEnable, MS_U16 u16R_Data, MS_U16 u16G_Data ,MS_U16 u16B_Data)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_OP2_TESTPATTERN  XCArgs;
    XCArgs.bEnable = bEnable ;
    XCArgs.u16R_Data = u16R_Data ;
    XCArgs.u16G_Data = u16G_Data;
    XCArgs.u16B_Data = u16B_Data;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_SET_OP2_TESTPATTERN, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
//-------------------------------------------------------------------------------------------------
/// MApi_XC_CheckWhiteBalancePatternModeSupport
/// @param  eWBPatternMode   \b IN: White balance Pattern gen Mode
/// @return MS_BOOL: TRUE - support, FALSED - unsupport
//-------------------------------------------------------------------------------------------------
MS_BOOL MApi_XC_CheckWhiteBalancePatternModeSupport_U2(void* pInstance, EN_XC_WB_PATTERN_MODE enWBPatternMode)
{
#ifndef DISABLE_HW_PATTERN_FUNCTION
    #ifdef SUPPORT_HW_TESTPATTERN
    if(enWBPatternMode == E_XC_OP1_PATTERN_MODE)
    {
        return TRUE;
    }
    else
    #endif
    if(SUPPORT_OP2_TEST_PATTERN)
    {
        if(enWBPatternMode == E_XC_OP2_PATTERN_MODE)
        {
            return TRUE;
        }
    }
    return FALSE;
#else
    if(SUPPORT_OP2_TEST_PATTERN)
    {
        if(enWBPatternMode == E_XC_OP2_PATTERN_MODE)
        {
            return TRUE;
        }
    }
    return FALSE;
 #endif
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_CheckWhiteBalancePatternModeSupport(EN_XC_WB_PATTERN_MODE enWBPatternMode)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_CHECK_WB_TESTPATTERN  XCArgs;
    XCArgs.eWBPattern = enWBPatternMode;
    XCArgs.eReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CHECK_WHITEBALANCE_PATTERN_MODE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
E_APIXC_ReturnValue MApi_XC_SetHLinearScaling_U2(void* pInstance, MS_BOOL bEnable, MS_BOOL bSign, MS_U16 u16Delta, SCALER_WIN eWindow)
{
#if XC_HLINEARSCALING_VER > 0
    MS_U16 u16DeltaInit = 0;

    _XC_ENTRY(pInstance);

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    if(bEnable)
    {
        if(0 == SC_R2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_08_L, BIT(8)))
        {
            printf("Waring: there is no H_postscaling enabled, the HLinearScaling won't work!\n");
        }

        u16DeltaInit = (960 * u16Delta) / 512;

        if((u16DeltaInit < 2) || (u16Delta > 0x7F) || (eWindow != MAIN_WINDOW))
        {
            _XC_RETURN(pInstance);
            XC_LOG_TRACE(XC_DGBLEVEL_CROPCALC,"MApi_XC_SetHLinearScaling(): u16Delta=%u, u16DeltaInit=%u, eWindow=%u\n", u16Delta, u16DeltaInit, eWindow);
            return E_APIXC_RET_FAIL_INVALID_PARAMETER;
        }

        XC_LOG_TRACE(XC_DGBLEVEL_CROPCALC,"MApi_XC_SetHLinearScaling(): u16DeltaInit=%u\n", u16DeltaInit);

        //enable
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_13_L, BIT(6), BIT(6));

        //zone 0 width: 0x3C0
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_13_L, 1<<12, BIT(12));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_14_L, 0xE0, 0x00FF);
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_13_L, 1<<8, BIT(8));

        //zone 1 width: 0x3C0
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_13_L, 1<<13, BIT(13));
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_14_L, 0xE0<<8, 0xFF00);
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_13_L, 1<<9, BIT(9));

        //zone 0 1 Delta
        SC_W2BYTE(psXCInstPri->u32DeviceID, REG_SC_BK23_16_L, (u16Delta<<8) | (u16Delta));

        //zone 0 1 Delta init
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_15_L, u16DeltaInit<<8, 0x7F00);

        //Delta init Signed
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_15_L, bSign<<15, BIT(15));
    }
    else
    {
        SC_W2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK23_13_L, 0, BIT(6));
    }

    _XC_RETURN(pInstance);

    return E_APIXC_RET_OK;
#else
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_SetHLinearScaling(MS_BOOL bEnable, MS_BOOL bSign, MS_U16 u16Delta, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_HLINEARSCALING XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.bSign = bSign;
    XCArgs.u16Delta = u16Delta;
    XCArgs.eWindow = eWindow;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_SET_HLINEARSCALING, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
E_APIXC_ReturnValue MApi_XC_EnableT3D_U2(void* pInstance, MS_BOOL bEnable)
{
    MS_BOOL bRet = FALSE;
#ifdef ENABLE_CHIP_BRINGUP
    bEnable = FALSE;
#endif
    _XC_ENTRY(pInstance);
    bRet = MDrv_XC_EnableT3D(pInstance, bEnable);
    _XC_RETURN(pInstance);

    if (bRet)
    {
        return E_APIXC_RET_OK;
    }
    else
    {
        return E_APIXC_RET_FAIL;
    }
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_EnableT3D(MS_BOOL bEnable)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_EnableT3D XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_SET_ENABLE_T3D, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
E_APIXC_ReturnValue MApi_XC_Set_FRC_InputTiming_U2(void* pInstance, E_XC_FRC_InputTiming enFRC_InputTiming)
{
#if FRC_INSIDE
    MS_BOOL bRet = FALSE;

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    bRet = MDrv_XC_Set_FRC_InputTiming(pInstance, enFRC_InputTiming);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    if (bRet)
    {
        return E_APIXC_RET_OK;
    }
    else
    {
        return E_APIXC_RET_FAIL;
    }
#else
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_Set_FRC_InputTiming(E_XC_FRC_InputTiming enFRC_InputTiming)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_Set_FRC_InputTiming XCArgs;
    XCArgs.enFRC_InputTiming = enFRC_InputTiming;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_SET_FRC_INPUTTIMING, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
E_APIXC_ReturnValue MApi_XC_Get_FRC_InputTiming_U2(void* pInstance, E_XC_FRC_InputTiming *penFRC_InputTiming)
{
#if FRC_INSIDE
    MS_BOOL bRet = FALSE;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);

    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    if(penFRC_InputTiming)
    {
        *penFRC_InputTiming = pXCResourcePrivate->stdrvXC_MVideo_Context.enFRC_CurrentInputTiming;
        bRet = TRUE;
    }
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    if (bRet)
    {
        return E_APIXC_RET_OK;
    }
    else
    {
        return E_APIXC_RET_FAIL;
    }
#else
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_Get_FRC_InputTiming(E_XC_FRC_InputTiming *penFRC_InputTiming)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_Get_FRC_InputTiming XCArgs;
    XCArgs.penFRC_InputTiming = penFRC_InputTiming;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_GET_FRC_INPUTTIMING, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
#if 0
E_APIXC_ReturnValue MApi_XC_Get_VirtualBox_Info_U2(void* pInstance, XC_VBOX_INFO *pstXC_VboxInfo)
{
    MS_BOOL bRet = FALSE;
    MS_U8 u8FrameCnt = 0;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    if(pstXC_VboxInfo)
    {
        pstXC_VboxInfo->u32FBSize[0] = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FB_Size;
        pstXC_VboxInfo->u32FBSize[1] = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FB_Size;
        pstXC_VboxInfo->u16VBox_PanelHsize = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Width;
        pstXC_VboxInfo->u16VBox_PanelVsize = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.stPanelInfo.u16Height;

        // temp for sub
        pstXC_VboxInfo->u16VBox_Htotal[1] = 1920;
        pstXC_VboxInfo->u16VBox_Vtotal[1] = 1080;

        if(XC_SUPPORT_4K2K)
        {
            // in 4K2K case, the Madi mode is P_Mode10 = 20bit/pixels
            u8FrameCnt = (pstXC_VboxInfo->u32FBSize[0]) / (pstXC_VboxInfo->u16VBox_PanelHsize*pstXC_VboxInfo->u16VBox_PanelVsize * 20/8);
            if(u8FrameCnt >= 2)
            {
        #if FRC_INSIDE
                /// FRC Enable
                if(pXCResourcePrivate->stdrvXC_MVideo_Context.bIsFRCInited)
                {
                    if(pXCResourcePrivate->stdrvXC_MVideo_Context.enFRC_CurrentInputTiming == E_XC_FRC_InputTiming_4K2K)
                    {
                        pstXC_VboxInfo->u16VBox_Htotal[0] = 3840;
                        pstXC_VboxInfo->u16VBox_Vtotal[0] = 2160;
                    }
                    else //if(pXCResourcePrivate->stdrvXC_MVideo_Context.enFRC_CurrentInputTiming == E_XC_FRC_InputTiming_2K1K)) and others
                    {
                        // 2K2K only for Napoli with FRC_On
                        pstXC_VboxInfo->u16VBox_Htotal[0] = 1920;
                        pstXC_VboxInfo->u16VBox_Vtotal[0] = 1080*2;
                    }
                }
                else  /// FRC Disable
                {
                    if((pstXC_VboxInfo->u16Vfreq[0] <= 300)&&(pstXC_VboxInfo->bInterface[0] == FALSE))
                    {
                        pstXC_VboxInfo->u16VBox_Htotal[0] = 3840;
                        pstXC_VboxInfo->u16VBox_Vtotal[0] = 2160;
                    }
                    else
                    {
                        pstXC_VboxInfo->u16VBox_Htotal[0] = 1920;
                        pstXC_VboxInfo->u16VBox_Vtotal[0] = 1080;
                    }
                }
            #else
                if(0)//(pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Init_Misc.u32MISC_A & E_XC_INIT_MISC_A_MONACO_MODE)
                {
                        pstXC_VboxInfo->u16VBox_Htotal[0] = 3840;
                        pstXC_VboxInfo->u16VBox_Vtotal[0] = 2160;
                }
                else // exp : Nike, Monaco_legacy_mode
                {
                    if((pstXC_VboxInfo->u16Vfreq[0] <= 300)&&(pstXC_VboxInfo->bInterface[0] == FALSE))
                    {
                        pstXC_VboxInfo->u16VBox_Htotal[0] = 3840;
                        pstXC_VboxInfo->u16VBox_Vtotal[0] = 2160;
                    }
                    else
                    {
                        pstXC_VboxInfo->u16VBox_Htotal[0] = 1920;
                        pstXC_VboxInfo->u16VBox_Vtotal[0] = 1080;
                    }
                }
        #endif
            }
            else
            {
        #if FRC_INSIDE
                if(pXCResourcePrivate->stdrvXC_MVideo_Context.bIsFRCInited
                 &&(pXCResourcePrivate->stdrvXC_MVideo_Context.enFRC_CurrentInputTiming == E_XC_FRC_InputTiming_2K1K))
                {
                    // 2K2K only for Napoli with FRC_On
                    pstXC_VboxInfo->u16VBox_Htotal[0] = 1920;
                    pstXC_VboxInfo->u16VBox_Vtotal[0] = 1080*2;
                }
                else
        #endif
                {
                    pstXC_VboxInfo->u16VBox_Htotal[0] = 1920;
                    pstXC_VboxInfo->u16VBox_Vtotal[0] = 1080;
                }
            }
        }
        else
        {
            pstXC_VboxInfo->u16VBox_Htotal[0] = 1920;
            pstXC_VboxInfo->u16VBox_Vtotal[0] = 1080;
        }
        bRet = TRUE;
    }
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    if (bRet)
    {
        return E_APIXC_RET_OK;
    }
    else
    {
        return E_APIXC_RET_FAIL;
    }
}

E_APIXC_ReturnValue MApi_XC_Get_VirtualBox_Info(XC_VBOX_INFO *pstXC_VboxInfo)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_Get_VBOX_Info XCArgs;
    XCArgs.pstXC_VboxInfo = pstXC_VboxInfo;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_VBOX_INFO, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
E_APIXC_ReturnValue MApi_XC_Set_OSD_Detect_U2(void* pInstance,MS_BOOL bEnable, MS_U32 Threhold)
{
    MS_BOOL bRet = FALSE;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    bRet = Hal_SC_SetOSDDetect(pInstance, bEnable,Threhold);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    if (bRet)
    {
        return E_APIXC_RET_OK;
    }
    else
    {
        return E_APIXC_RET_FAIL;
    }
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_Set_OSD_Detect(MS_BOOL bEnable, MS_U32 Threhold)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_Set_OSD_DETECT XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.Threhold = Threhold;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_SET_OSD_DETECT, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
E_APIXC_ReturnValue MApi_XC_Get_OSD_Detect_U2(void* pInstance,MS_BOOL *pbOSD)
{
    MS_BOOL bRet = FALSE;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    bRet = Hal_SC_GetOSDDetect(pInstance, pbOSD);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    if (bRet)
    {
        return E_APIXC_RET_OK;
    }
    else
    {
        return E_APIXC_RET_FAIL;
    }
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_Get_OSD_Detect(MS_BOOL *pbOSD)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_Get_OSD_DETECT XCArgs;
    XCArgs.pbOSD = pbOSD;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_GET_OSD_DETECT, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
MS_BOOL MApi_XC_Is2K2KToFrcMode_U2(void* pInstance)
{
    MS_BOOL bRet = FALSE;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    bRet = MDrv_XC_Is2K2KToFrc(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return bRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_Is2K2KToFrcMode(void)
{
    //return MDrv_XC_Is2K2KToFrc();
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_Is2K2KToFrcMode  XCArgs;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_IS2K2KTOFRCMODE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif
MS_BOOL MApi_XC_Set_XC_VOP_U2(void * pInstance, MS_XC_VOP_Data stXCVOPData)
{

    MS_BOOL bResult = TRUE;

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);

    printf("[%s][%d]pPNLInstancePrivate->u32DeviceID= %tx\n", __FUNCTION__, __LINE__, (ptrdiff_t)psXCInstPri->u32DeviceID);
    if(psXCInstPri->u32DeviceID == 0)
    {
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        return FALSE;
    }

    MDrv_XC_Set_VOP(pInstance, &stXCVOPData);

    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);

    return bResult;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_Set_XC_VOP(MS_XC_VOP_Data stXCVOPData)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SetXC_VOP XCArgs;
    XCArgs.stXC_VOPData = stXCVOPData;
    XCArgs.bReturn = FALSE;
    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_XC_VOP, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain PNL engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturn;
    }

}
#endif
//K3_U2
#if 0
//-------------------------------------------------------------------------------------------------
/// MApi_XC_EX_Set_WhiteBalance_Pattern
/// @param  bEnable                  \b IN: enable/disable white balance test pattern
/// @param  u16R_CR_Data             \b IN: when enable white balance test pattern ,Data Generator for r_cr constant
/// @param  u16G_Y_Data              \b IN: when enable white balance test pattern ,Data Generator for g_y constant
/// @param  u16B_CB_Data             \b IN: when enable white balance test pattern ,Data Generator for b_cb constant
/// @return E_APIXC_RET_OK or E_APIXC_RET_FAIL or E_APIXC_RET_FAIL_FUN_NOT_SUPPORT
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_EX_Set_WhiteBalance_Pattern_U2(XC_DEVICE_ID *pDeviceId, MS_BOOL bEnable, MS_U16 u16R_CR_Data, MS_U16 u16G_Y_Data ,MS_U16 u16B_CB_Data)
{
    XC_EX_RETURN_VALUE enResult = E_XC_EX_RET_OK;

    W2BYTEMSK(REG_IPMUX_10_L, (bEnable << 9) , BIT(9));
    W2BYTEMSK(REG_IPMUX_10_L, (bEnable << 12), BIT(12));
    W2BYTEMSK(REG_IPMUX_10_L, (bEnable << 13), BIT(13));
    W2BYTEMSK(REG_IPMUX_10_L, (bEnable << 14), BIT(14));

    if(bEnable == TRUE)
    {
        W2BYTEMSK(REG_IPMUX_1D_L, u16R_CR_Data, BMASK(9:0));
        W2BYTEMSK(REG_IPMUX_1E_L, u16G_Y_Data,  BMASK(9:0));
        W2BYTEMSK(REG_IPMUX_1F_L, u16B_CB_Data, BMASK(9:0));
    }

    return enResult;
}

E_APIXC_ReturnValue MApi_XC_Get_FRC_InputTiming(E_XC_FRC_InputTiming *penFRC_InputTiming)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_Get_FRC_InputTiming XCArgs;
    XCArgs.penFRC_InputTiming = penFRC_InputTiming;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_GET_FRC_INPUTTIMING, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif

//-------------------------------------------------------------------------------------------------
/// Get SWDSIndex
/// @return @ref MS_U8 return SWDSIndex
//-------------------------------------------------------------------------------------------------
MS_U8 MApi_XC_GetSWDSIndex_U2(void* pInstance, SCALER_WIN eWindow)
{
    MS_U8 u8SWDSIndex;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
#if (SUPPORT_KERNEL_DS == 0)
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
#endif
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    if(MDrv_XC_Is_UpdateDsCmd(pInstance, eWindow))
    {
#if (SUPPORT_KERNEL_DS == 1)
        u8SWDSIndex = MDrv_XC_GetSWDSIndex(pInstance, eWindow);
#else
        u8SWDSIndex = pXCResourcePrivate->sthal_SC.u8DynamicScalingNextIndex[eWindow];
#endif
    }
    else
    {
        u8SWDSIndex = 0;
    }
#if (SUPPORT_KERNEL_DS == 0)
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
#endif
    return u8SWDSIndex;

}
#ifndef UTOPIAXP_REMOVE_WRAPPER
#ifdef MSOS_TYPE_LINUX_KERNEL
MS_U8 MApi_XC_GetSWDSIndex(SCALER_WIN eWindow)
#else
__attribute__((weak)) MS_U8 MApi_XC_GetSWDSIndex(SCALER_WIN eWindow)
#endif
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return 0;
    }

    stXC_GET_SWDSIndex XCArgs;
    XCArgs.u8ReturnValue = 0;
    XCArgs.eWindow = eWindow;
    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_SWDSIndex, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)  //E_XC_CMD_GET_SWDSIndex
    {
        printf("Obtain XC engine fail\n");
        return 0;
    }
    else
    {
        return XCArgs.u8ReturnValue;
    }

}
#endif
//-------------------------------------------------------------------------------------------------
/// Set DNR base0 address
/// @param  u32FBAddress      \b IN: the framebuffer address
/// @param  u32FBSize         \b IN: the framebuffer size
//-------------------------------------------------------------------------------------------------
MS_BOOL MApi_XC_SetDualFrameBufferAddress_U2(void* pInstance, MS_PHY u32FBAddress, MS_PHY u32FBSize, SCALER_WIN eWindow)
{
    MS_BOOL bReturn = FALSE;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);
    bReturn = MDrv_XC_SetDualFrameBufferAddress(pInstance, u32FBAddress, u32FBSize, eWindow);
    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return bReturn;
}

#ifdef UFO_XC_SUPPORT_DUAL_MIU
MS_BOOL MApi_XC_EnableMiuDualMode_U2(void* pInstance, MS_BOOL bEnable, MS_U32 u32LeftFactor, MS_U32 u32RightFactor, SCALER_WIN eWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
#ifndef XC_DUAL_MIU_SUPPORT_SUBWIN
    if (eWindow == MAIN_WINDOW)
    {
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
        if (psXCInstPri->u32DeviceID != 0)
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "XC SCMI Dual miu just support main window only.\n");
            return FALSE;
        }
#endif
    }
    else
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "XC SCMI Dual miu just support main window only.\n");
        return FALSE;
    }
#endif

    if(!((u32RightFactor <= 3*u32LeftFactor) && (u32LeftFactor <= 3*u32RightFactor)))
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "error params u32LeftFactor: %u, u32RightFactor: %u.\n", u32LeftFactor, u32RightFactor);
        return FALSE;
    }

    gSrcInfo[eWindow].Status2.stMiuDualModCfg.bEnableMiuDualMod = bEnable;
    gSrcInfo[eWindow].Status2.stMiuDualModCfg.u32LeftFactor = u32LeftFactor;
    gSrcInfo[eWindow].Status2.stMiuDualModCfg.u32RightFactor = u32RightFactor;
    XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "u32LeftFactor: %u, u32RightFactor: %u.\n", u32LeftFactor, u32RightFactor);
    return TRUE;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL  MApi_XC_EnableMiuDualMode(MS_BOOL bEnable, MS_U32 u32LeftFactor, MS_U32 u32RightFactor, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_SetBwdConfig() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    stXC_EnableDualMode XCArgs;
    XCArgs.bEnable       = bEnable;
    XCArgs.u32LeftFactor = u32LeftFactor;
    XCArgs.u32RightFactor= u32RightFactor;
    XCArgs.eWindow = eWindow;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_ENABLE_DUAL_MODE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif
#endif

#ifdef SUPPORT_BWD
MS_BOOL MApi_XC_SetBwdConfig_U2(void* pInstance, E_XC_BWD_CONFIG_TYPE eType, void *pstParam, SCALER_WIN eWindow )
{
    MS_BOOL bResult = FALSE;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);
    bResult = MDrv_SC_setBwdConfig(pInstance, eType, pstParam, eWindow);
      _XC_RETURN(pInstance);
      _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return bResult;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_SetBwdConfig(E_XC_BWD_CONFIG_TYPE eType, void *pstParam, SCALER_WIN eWindow )
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_SetBwdConfig() first\n",__FUNCTION__,__LINE__);
        return FALSE;
    }

    stXC_SetBwdConfig XCArgs;
    XCArgs.eType = eType;
    XCArgs.pstParam = pstParam;
    XCArgs.eWindow = eWindow;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_BWD_CONFIG, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif
#endif
MS_BOOL MApi_XC_IsSupport2StepScaling_U2(void* pInstance)
{
    return MDrv_XC_IsSupport2StepScaling();
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_IsSupport2StepScaling(void)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_IsSupport2StepScaling XCArgs;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_IS_SUPPORT_2_STEP_SCALING, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif


E_APIXC_ReturnValue MApi_XC_SetForceWrite_U2( void* pInstance, EN_MUTE_TYPE enMuteType, MS_BOOL bIsForceWrite)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
   _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    MDrv_SC_SetForceWrite( enMuteType, bIsForceWrite);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return E_APIXC_RET_OK;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue MApi_XC_SetForceWrite( EN_MUTE_TYPE enMuteType, MS_BOOL bIsForceWrite)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_FORCE_WRITE XCArgs;
    XCArgs.enMuteType = enMuteType;
    XCArgs.bIsForceWrite = bIsForceWrite;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_FORCE_WRITE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");

        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }

}
#endif

MS_BOOL MApi_XC_GetForceWrite_U2( void* pInstance, EN_MUTE_TYPE enMuteType)
{
    MS_BOOL bret = FALSE;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
   _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    bret = MDrv_SC_GetForceWrite( enMuteType);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return bret;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_GetForceWrite( EN_MUTE_TYPE enMuteType)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_GET_FORCE_WRITE XCArgs;
    XCArgs.enMuteType = enMuteType;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_FORCE_WRITE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        printf("G eReturnValue = %d\n",(XCArgs.eReturnValue == TRUE));
        return XCArgs.eReturnValue;
    }

}
#endif
E_APIXC_ReturnValue MApi_XC_VideoPlaybackCtrl_U2(void* pInstance,MS_U32 u32Cmd,void *pCmdArgs,MS_U32 u32CmdArgsSize)
{
    E_APIXC_ReturnValue eReturnValue = E_APIXC_RET_FAIL;
    switch(u32Cmd)
    {
        case E_XC_VPC_MULTI_SPEED_PLAYBACK_CTRL:
            if(u32CmdArgsSize == sizeof(ST_XC_VPC_MULTI_SPEED_PLAYBACK_CTRL))
            {
                ST_XC_VPC_MULTI_SPEED_PLAYBACK_CTRL *stXCVpcMultiSpeedPlaybackCtrl = (ST_XC_VPC_MULTI_SPEED_PLAYBACK_CTRL *)pCmdArgs;

                _XC_ENTRY(pInstance);
                MDrv_XC_SetForceCurrentReadBank(pInstance, stXCVpcMultiSpeedPlaybackCtrl ->bEnable, stXCVpcMultiSpeedPlaybackCtrl ->eWindow);
                _XC_RETURN(pInstance);
                eReturnValue = E_APIXC_RET_OK;
            }
            else
            {
                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"E_XC_VPC_MULTI_SPEED_PLAYBACK_I_Mode Struct Size No Match\n");
                return eReturnValue;
            }
            break;

        default:
            printf("Unknow Handle ID!\n");
            break;
    }

    return eReturnValue;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_VideoPlaybackCtrl(MS_U32 u32Cmd,void *pCmdArgs,MS_U32 u32CmdArgsSize)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_VIDEO_PLAYBACK_CTRL XCArgs;
    XCArgs.u32Cmd = u32Cmd;
    XCArgs.pCmdArgs = pCmdArgs;
    XCArgs.u32CmdArgsSize = u32CmdArgsSize;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;
    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_VIDEO_PLAYBACK_CTRL, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain PNL engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
#ifdef UFO_XC_AUTO_DOWNLOAD
E_APIXC_ReturnValue MApi_XC_AutoDownload_Config_U2(void* pInstance, XC_AUTODOWNLOAD_CONFIG_INFO *pstConfigInfo)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    E_APIXC_ReturnValue eRet = E_APIXC_RET_FAIL;
    XC_AUTODOWNLOAD_CONFIG_INFO stConfigInfo;
    MS_U16 u16CopiedLength = sizeof(stConfigInfo);

    memset(&stConfigInfo, 0, sizeof(stConfigInfo));
    VERSION_COMPATIBLE_CHECK(pstConfigInfo, stConfigInfo, u16CopiedLength, u32ConfigInfo_Version, u16ConfigInfo_Length, AUTODOWNLOAD_CONFIG_INFO_VERSION, E_APIXC_RET_FAIL);
    pstConfigInfo->u32ConfigInfo_Version = AUTODOWNLOAD_CONFIG_INFO_VERSION; /*Return the actual version of the returned value for APP to use*/
    pstConfigInfo->u16ConfigInfo_Length = u16CopiedLength; /*Return the actual length of the returned value for APP to know whick info is valid*/
    memcpy(&stConfigInfo, pstConfigInfo, u16CopiedLength);

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);

    eRet = MDrv_XC_AutoDownload_Config(pInstance, &stConfigInfo);

    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return eRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_AutoDownload_Config(XC_AUTODOWNLOAD_CONFIG_INFO *pstConfigInfo)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_AUTODOWNLOAD_CONFIG_CTRL XCArgs;
    XCArgs.pstConfigInfo = pstConfigInfo;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;
    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_AUTODOWNLOAD_CONFIG_CTRL, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
E_APIXC_ReturnValue MApi_XC_AutoDownload_Write_U2(void* pInstance, XC_AUTODOWNLOAD_DATA_INFO *pstDataInfo)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_AUTODOWNLOAD_DATA_INFO stDataInfo;
    MS_U16 u16CopiedLength = sizeof(stDataInfo);
    E_APIXC_ReturnValue eRet = E_APIXC_RET_FAIL;

#if (defined(CONFIG_PURE_SN) || defined(CONFIG_PURE_SN_64))
    XC_LOG_TRACE(XC_DBGLEVEL_PARAMETER_TRACE, "Write auto download(client: %d, size: %ld).\n",
        pstDataInfo->enClient, pstDataInfo->u32Size);
#else
    XC_LOG_TRACE(XC_DBGLEVEL_PARAMETER_TRACE, "Write auto download(client: %d, size: %d).\n",
        pstDataInfo->enClient, pstDataInfo->u32Size);
#endif

    XC_LOG_TRACE(XC_DBGLEVEL_PARAMETER_TRACE, "Write auto download(client: %d).\n", pstDataInfo->enClient);

    memset(&stDataInfo, 0, sizeof(stDataInfo));
    VERSION_COMPATIBLE_CHECK(pstDataInfo, stDataInfo, u16CopiedLength, u32DataInfo_Version, u16DataInfo_Length, AUTODOWNLOAD_DATA_INFO_VERSION, E_APIXC_RET_FAIL);
    pstDataInfo->u32DataInfo_Version = AUTODOWNLOAD_DATA_INFO_VERSION; /*Return the actual version of the returned value for APP to use*/
    pstDataInfo->u16DataInfo_Length = u16CopiedLength; /*Return the actual length of the returned value for APP to know whick info is valid*/
    memcpy(&stDataInfo, pstDataInfo, u16CopiedLength);

    XC_AUTODOWNLOAD_FORMAT_INFO stFormatInfo;
    u16CopiedLength = sizeof(stFormatInfo);
    memset(&stFormatInfo, 0, sizeof(stFormatInfo));
    if (stDataInfo.enClient == E_XC_AUTODOWNLOAD_CLIENT_HDR)
    {
        XC_AUTODOWNLOAD_FORMAT_INFO* pstFormatInfo = (XC_AUTODOWNLOAD_FORMAT_INFO *)stDataInfo.pParam;
#if (defined(CONFIG_PURE_SN) || defined(CONFIG_PURE_SN_64))
        XC_LOG_TRACE(XC_DBGLEVEL_PARAMETER_TRACE, "Write auto download, Size: %ld, SubClient: %d, AddrRange(enable, start, end) = (%d, %td, %td).\n",
            stDataInfo.u32Size, pstFormatInfo->enSubClient, pstFormatInfo->bEnableRange, (ptrdiff_t)pstFormatInfo->u16StartAddr, (ptrdiff_t)pstFormatInfo->u16EndAddr);
#else
        XC_LOG_TRACE(XC_DBGLEVEL_PARAMETER_TRACE, "Write auto download, Size: %d, SubClient: %d, AddrRange(enable, start, end) = (%d, %td, %td).\n",
            stDataInfo.u32Size, pstFormatInfo->enSubClient, pstFormatInfo->bEnableRange, (ptrdiff_t)pstFormatInfo->u16StartAddr, (ptrdiff_t)pstFormatInfo->u16EndAddr);
#endif

        VERSION_COMPATIBLE_CHECK(pstFormatInfo, stFormatInfo, u16CopiedLength, u32FormatInfo_Version, u16FormatInfo_Length, AUTODOWNLOAD_FORMAT_INFO_VERSION, E_APIXC_RET_FAIL);
        pstFormatInfo->u32FormatInfo_Version = AUTODOWNLOAD_FORMAT_INFO_VERSION; /*Return the actual version of the returned value for APP to use*/
        pstFormatInfo->u16FormatInfo_Length = u16CopiedLength; /*Return the actual length of the returned value for APP to know whick info is valid*/
        memcpy(&stFormatInfo, pstFormatInfo, u16CopiedLength);

        stDataInfo.pParam = &stFormatInfo;
    }

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);

    eRet = MDrv_XC_AutoDownload_Write(pInstance, &stDataInfo);

    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return eRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_AutoDownload_Write(XC_AUTODOWNLOAD_DATA_INFO *pstDataInfo)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }
    stXC_AUTODOWNLOAD_WRITE_CTRL XCArgs;
    XCArgs.pstDataInfo= pstDataInfo;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;
    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_AUTODOWNLOAD_WRITE_CTRL, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
E_APIXC_ReturnValue MApi_XC_AutoDownload_Fire_U2(void* pInstance, EN_XC_AUTODOWNLOAD_CLIENT enClient)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    E_APIXC_ReturnValue eRet = E_APIXC_RET_FAIL;
    XC_LOG_TRACE(XC_DBGLEVEL_PARAMETER_TRACE, "Fire auto download(client: %d).\n", enClient);

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);

    eRet = MDrv_XC_AutoDownload_Fire(pInstance, enClient);

    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return eRet;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_AutoDownload_Fire(EN_XC_AUTODOWNLOAD_CLIENT enClient)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_AUTODOWNLOAD_FIRE_CTRL XCArgs;
    XCArgs.enClient = enClient;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;
    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_AUTODOWNLOAD_FIRE_CTRL, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
#endif
#ifdef UFO_XC_HDR
#if (UFO_XC_HDR_VERSION == 2)

static E_APIXC_ReturnValue _MDrv_XC_GetFodInfo(void* pInstance, void *pParam)
{
    XC_FOD_INFO* pstFodInfo = (XC_FOD_INFO *)pParam;
    ST_KDRV_XC_FOD_INFO stKdrvFodInfo;
    memset(&stKdrvFodInfo, 0, sizeof(ST_KDRV_XC_FOD_INFO));
    stKdrvFodInfo.u16Length = pstFodInfo->u16Length;
    stKdrvFodInfo.u8Win = pstFodInfo->u8Win;
    stKdrvFodInfo.u8Bff = pstFodInfo->u8Bff;
    stKdrvFodInfo.s32Width= pstFodInfo->s32Width;
    stKdrvFodInfo.s32Height= pstFodInfo->s32Height;
    stKdrvFodInfo.bFieldOrderChange = pstFodInfo->bFieldOrderChange;

#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    if(0 > _s32FdScaler)
    {
        _s32FdScaler = open("/dev/scaler", O_RDWR);
        if(0 > _s32FdScaler)
        {
            printf("Scaler device not opened!!!!\n");
            return E_APIXC_RET_FAIL;
        }
    }
    if (ioctl(_s32FdScaler, MDRV_XC_IOC_GET_FOD_INFO, &stKdrvFodInfo))
    {
        printf("MDRV_XC_IOC_GET_FOD_INFO fail!!!!\n");
        return E_APIXC_RET_FAIL;
    }
#endif

#if (defined(MSOS_TYPE_LINUX_KERNEL) && defined(CONFIG_MSTAR_XC_HDR_SUPPORT))
    if (MDrv_XC_DetectFieldOrder(&stKdrvFodInfo) != TRUE)
    {
        return E_APIXC_RET_FAIL;
    }
#endif
    pstFodInfo->bFieldOrderChange = stKdrvFodInfo.bFieldOrderChange;
    return E_APIXC_RET_OK;
}

E_APIXC_ReturnValue MApi_XC_HDR_Control_U2(void* pInstance, EN_XC_HDR_CTRL_TYPE enCtrlType, void *pParam)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    E_APIXC_ReturnValue eRet = E_APIXC_RET_FAIL;
    XC_LOG_TRACE(XC_DBGLEVEL_PARAMETER_TRACE, "HDR contrl type: %d.\n", enCtrlType);

    //no need to get xc seamphore
    if(enCtrlType == E_XC_HDR_CTRL_GET_FOD_INFO)
    {
        eRet = _MDrv_XC_GetFodInfo(pInstance, pParam);
    }
    else
    {
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
        _XC_ENTRY(pInstance);

        eRet = MDrv_XC_HDR_Control(pInstance, enCtrlType, pParam);

        _XC_RETURN(pInstance);
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    }
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return eRet;
}

E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_HDR_Control(EN_XC_HDR_CTRL_TYPE enCtrlType, void *pParam)
{
    if (pu32XCInst == NULL)
    {
        if(UtopiaOpen(MODULE_XC, &pu32XCInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return E_APIXC_RET_FAIL;
        }
    }

    stXC_HDR_CTRL XCArgs;
    XCArgs.enCtrlType= enCtrlType;
    XCArgs.pParam = pParam;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;
    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_HDR_CTRL, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}
#endif
#endif

#ifdef UFO_XC_PQ_PATH
MS_BOOL MApi_XC_GetPQPathStatus_U2(void* pInstance, E_XC_PQ_Path_Type ePqPathType,  MS_U16 u16Width, MS_U16 u16Height)
{
    return MDrv_XC_GetPQPathStatus(pInstance, ePqPathType, u16Width, u16Height);
}

MS_BOOL MApi_XC_GetPQPathStatus(E_XC_PQ_Path_Type ePqPathType, MS_U16 u16Width, MS_U16 u16Height)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_GetPqPathStatus XCArgs;
    XCArgs.ePqPathType = ePqPathType;
    XCArgs.u16Height = u16Height;
    XCArgs.u16Width = u16Width;
    XCArgs.bReturnValue = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_PQ_PATH, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif

////#define MSTAR_TA_NAME       "mstara_internal_tests.ta"
#define MSTAR_INTERNAL_XC_UUID {0x4dd53ca0, 0x0248, 0x11e6, \
      {0x86, 0xc0, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b} }
//#define SYS_TEEC_OPERATION_INITIALIZER { 0, 0, { { { 0 } } } }
#define SYS_TEEC_OPERATION_INITIALIZER { 0 }
extern MS_U32 MDrv_SYS_TEEC_InitializeContext(const char *name, TEEC_Context *context);;
extern MS_U32 MDrv_SYS_TEEC_Open(TEEC_Context *context, TEEC_Session *session, const TEEC_UUID *destination, MS_U32 connection_method, const void *connection_data, TEEC_Operation *operation, MS_U32 *error_origin);
extern MS_U32 MDrv_SYS_TEEC_InvokeCmd(TEEC_Session *session, MS_U32 cmd_id, TEEC_Operation *operation, MS_U32 *error_origin);
extern void MDrv_SYS_TEEC_Close(TEEC_Session *session);
extern void MDrv_SYS_TEEC_FinalizeContext(TEEC_Context *context);

MS_BOOL _MDrv_XC_TEE_Send_Cmd(void *pInstance,EN_XC_OPTEE_ACTION action,void *para,MS_U32 u32size)
{
    MS_BOOL bret = FALSE;

#if defined(MSOS_TYPE_LINUX) || defined(MSOS_TYPE_LINUX_KERNEL)
#if (defined (UTOPIA_CA_WRAPPER))
        const MS_U32 u32TEETimeOut = 3000;
        SYS_TEEWRP_DATA      stData;
        SYS_TEEWRP_FUNC_RET  eRet = E_SYS_TEEWRP_FUNC_RET_SUCCESS;
        /* Init */
        memset(&stData, 0, sizeof(SYS_TEEWRP_DATA));
        strncpy(stData.pR2teeName, "XC_TEE", SYS_TEEWRP_NAME_LENGTH);
        strncpy(stData.pOpteeName,_xc_device, SYS_TEEWRP_NAME_LENGTH);
        stData.u32CmdTimeout = (stData.phySize / MAX_MBX_PARAM_SIZE ) * u32TEETimeOut;//Total time out
        memcpy(&stData.stUuid, &uuid, sizeof(TEEC_UUID));
        bret = MDrv_SYS_TEEWRP_Init(&stData);
        eRet = stData.u32FuncRet;
        if (eRet != E_SYS_TEEWRP_FUNC_RET_SUCCESS)
        {
            printf("[%s %d]:eRet = %u  eRet != E_SYS_TEEWRP_FUNC_RET_SUCCESS\n",__func__,__LINE__,eRet);
            return bret;
        }
        /* Ree send to Tee. (repeat) */
        stData.u8Cmd = action;
        stData.phySize = (size_t)u32size;
        stData.virtAddr = (MS_VIRT)(para);
        bret = MDrv_SYS_TEEWRP_Comm(&stData);
        eRet = stData.u32FuncRet;
        if (eRet != E_SYS_TEEWRP_FUNC_RET_SUCCESS)
        {
            printf("[%s %d]:eRet = %u  eRet != E_SYS_TEEWRP_FUNC_RET_SUCCESS\n",__func__,__LINE__,eRet);
            return bret;
        }
        /* Deinit. */
        bret = MDrv_SYS_TEEWRP_Destroy(&stData);
        eRet = stData.u32FuncRet;
        if (eRet != E_SYS_TEEWRP_FUNC_RET_SUCCESS)
        {
            printf("[%s %d]:eRet = %u  eRet != E_SYS_TEEWRP_FUNC_RET_SUCCESS\n",__func__,__LINE__,eRet);
            return bret;
        }
        bret = TRUE;
#else
    SYS_TEEINFO stTeeMode;
    MDrv_SYS_ReadKernelCmdLine();
    MDrv_SYS_GetTEEInfo(&stTeeMode);
    if(stTeeMode.OsType == SYS_TEEINFO_OSTYPE_NUTTX)
    {
        if(_MDrv_XC_MBX_Init(pInstance)!=TRUE)
        {
            printf("%s %d: _MDrv_XC_MBX_Init failed\n",__func__,__LINE__);
            bret = FALSE;
            return bret;
        }
        if(_MDrv_XC_REE_SendMBX(pInstance,action,para)!=TRUE)
        {
            printf("%s %d: _MDrv_XC_REE_SendMBX failed\n",__func__,__LINE__);
            bret = FALSE;
            return bret;
        }
        bret = TRUE;
    }
    else
    {
        TEEC_UUID uuid = MSTAR_INTERNAL_XC_UUID;
        TEEC_Operation op = SYS_TEEC_OPERATION_INITIALIZER;
        static const char optee_xc[] = "opteearmtz00";
        char *_xc_device = (char *)optee_xc;
        TEEC_Context* pstMstar_teec_ctx = NULL;
        pstMstar_teec_ctx = malloc(sizeof(TEEC_Context));
        if(pstMstar_teec_ctx == NULL)
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SVP, "pstMstar_teec_ctx malloc fail!\n");
            return FALSE;
        }
        memset(pstMstar_teec_ctx,0,sizeof(TEEC_Context));

        TEEC_Session* pstSession = NULL;
        pstSession = malloc(sizeof(TEEC_Session));
        if(pstSession == NULL)
        {
            XC_LOG_TRACE(XC_DBGLEVEL_SVP, "pstMstar_teec_ctx malloc fail!\n");
            if(pstMstar_teec_ctx!= NULL)
            {
              free(pstMstar_teec_ctx);
            }
            return FALSE;
        }
        memset(pstSession,0,sizeof(TEEC_Session));

        MS_U32 ret_orig= 0;
        if (MDrv_SYS_TEEC_InitializeContext(_xc_device, pstMstar_teec_ctx) != TEEC_SUCCESS)
        {
            printf("%s %d: Init Context failed\n",__func__,__LINE__);
            MDrv_SYS_TEEC_FinalizeContext(pstMstar_teec_ctx);
            free(pstMstar_teec_ctx);
            free(pstSession);
            return bret;
        }

        if (MDrv_SYS_TEEC_Open(pstMstar_teec_ctx, pstSession, &uuid, TEEC_LOGIN_PUBLIC, NULL, NULL, &ret_orig) != TEEC_SUCCESS)
        {
            printf("%s %d: TEEC Open pstSession failed\n",__func__,__LINE__);
            MDrv_SYS_TEEC_Close(pstSession);
            MDrv_SYS_TEEC_FinalizeContext(pstMstar_teec_ctx);
            free(pstMstar_teec_ctx);
            free(pstSession);
            return bret;
        }
        op.params[0].tmpref.buffer = para;
        op.params[0].tmpref.size = u32size;
        op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
        if (MDrv_SYS_TEEC_InvokeCmd(pstSession, action, &op, &ret_orig) != TEEC_SUCCESS)
        {
            printf("%s %d: TEEC Invoke command failed\n",__func__,__LINE__);
        }
        else
        {
            bret = TRUE;
        }
        MDrv_SYS_TEEC_Close(pstSession);
        MDrv_SYS_TEEC_FinalizeContext(pstMstar_teec_ctx);
        free(pstMstar_teec_ctx);
        free(pstSession);
    }
#endif
#endif
    return bret;
}
MS_BOOL _MDrv_XC_MBX_Init(void* pInstance)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    MS_BOOL bRet = TRUE;
    const MS_U16 u16MsgQueueSize = 10;
    const MS_U32 u32TimeoutMillSecs = 1000;
    if(MApi_MBX_Init(E_MBX_CPU_MIPS, E_MBX_ROLE_HK, u32TimeoutMillSecs)!=E_MBX_SUCCESS)
    {
        printf("XC_REE MApi_MBX_Init fail\n");
        bRet = FALSE;
        return bRet;
    }
    if(MApi_MBX_Enable(TRUE)!=E_MBX_SUCCESS)
    {
        printf("XC_REE MApi_MBX_Enable fail\n");
        bRet = FALSE;
        return bRet;
    }
    if(MApi_MBX_QueryDynamicClass(E_MBX_ROLE_AEON,"XC_TEE",(MS_U8 *)&pXCResourcePrivate->u8REEClassNum))
    {
        printf("XC_REE MApi_MBX_QueryDynamicClass fail\n");
        bRet = FALSE;
        return bRet;
    }
    if(MApi_MBX_RegisterMSG((MBX_Class)pXCResourcePrivate->u8REEClassNum, u16MsgQueueSize)!=E_MBX_SUCCESS)
    {
        printf("XC_REE MApi_MBX_RegisterMSG fail\n");
        bRet = FALSE;
        return bRet;
    }
    //init variable
    pXCResourcePrivate->u32TotalMailNum = 0;
    MS_U32 u32XcOpteeHandlerSize = (unsigned int)sizeof(XC_OPTEE_HANDLER);
    //data size
    pXCResourcePrivate->u32DataSize = u32XcOpteeHandlerSize;
    const MS_U8 u8MBXParaNum = 10;
    //calculate for mailNum
    MS_U32 u32SizeCounter = 0;
    while(u32SizeCounter<u32XcOpteeHandlerSize)
    {
        u32SizeCounter+=u8MBXParaNum;
        pXCResourcePrivate->u32TotalMailNum++;
    }

    //initial
    pXCResourcePrivate->u32ResidentDataSize = pXCResourcePrivate->u32DataSize;


    return bRet;
}

MS_BOOL _MDrv_XC_REE_SendMBX(void* pInstance,EN_XC_OPTEE_ACTION enAction,void *pPara)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    MS_BOOL bRet = FALSE;
    MBX_Msg mbxMsg;
    mbxMsg.eRoleID          = E_MBX_ROLE_AEON;
    mbxMsg.eMsgType         = E_MBX_MSG_TYPE_NORMAL;
    mbxMsg.u8Ctrl           = 0; // Useless?
    mbxMsg.u8MsgClass       = pXCResourcePrivate->u8REEClassNum;
    mbxMsg.u8Index          = enAction;
    mbxMsg.u8S0             = 0;//??
    mbxMsg.u8S1             = 0;//??
    mbxMsg.u8ParameterCount = 0;
    ///
    switch(enAction)
    {
        case E_XC_OPTEE_GET_PIPE_ID:
        {
            bRet = _MDrv_XC_REE_MBX_CheckSendMSG(&mbxMsg);
            break;
        }
        case E_XC_OPTEE_ENABLE:
        {
            bRet = _MDrv_XC_REE_MBX_CheckSendMSG(&mbxMsg);
            break;
        }
        case E_XC_OPTEE_DISABLE:
        {
            bRet = _MDrv_XC_REE_MBX_CheckSendMSG(&mbxMsg);
            break;
        }
        case E_XC_OPTEE_SET_HANDLER:
        {
            bRet = _MDrv_XC_REE_MBX_CheckSendMSG(&mbxMsg);
            break;
        }
        case E_XC_OPTEE_SYNC_HANDLER:       //todo
        {
            bRet = _MDrv_XC_REE_MBX_SendMSG(pInstance,&mbxMsg,pPara);
            break;
        }
        case E_XC_OPTEE_GET_HANDLER:        //todo
        {
            bRet = _MDrv_XC_REE_MBX_CheckSendMSG(&mbxMsg);
            break;
        }
        case E_XC_OPTEE_UPDATE_HANDLER:     //todo
        {
            bRet = _MDrv_XC_REE_MBX_SendMSG(pInstance,&mbxMsg,pPara);
            break;
        }
        case E_XC_OPTEE_SET_MUX:
        {
            bRet = _MDrv_XC_REE_MBX_CheckSendMSG(&mbxMsg);
            break;
        }
        case E_XC_OPTEE_SYNC_MUX:           //todo
        {
            bRet = _MDrv_XC_REE_MBX_SendMSG(pInstance,&mbxMsg,pPara);
            break;
        }
        case E_XC_OPTEE_GET_MUX:            //todo
        {
            bRet = _MDrv_XC_REE_MBX_CheckSendMSG(&mbxMsg);
            break;
        }
        default:
        {
            bRet = _MDrv_XC_REE_MBX_CheckSendMSG(&mbxMsg);
            break;
        }
    }
    return bRet;
}
MS_BOOL _MDrv_XC_REE_MBX_CheckSendMSG(MBX_Msg *pMsg)
{
    if(pMsg==NULL)
    {
        printf("[XC]%s,pMsg is a null pointer\n",__FUNCTION__);
        return FALSE;
    }
    MS_U32 u32Time = 0;
    MBX_Result enMbxResult = E_MBX_SUCCESS;
    MS_BOOL bRet = TRUE;
    const MS_U32 u32TimeConstraints = 3000;
    //Send Mail
    u32Time = MsOS_GetSystemTime();
    do
    {
        enMbxResult = MApi_MBX_SendMsg(pMsg);

        switch(enMbxResult)
        {
            case E_MBX_SUCCESS:
                // Do nothing
                bRet = TRUE;
                break;
            default:
                printf("[XC]Ree Send Cmd fail=%u\n", enMbxResult);
                break;
        }

        if( (MsOS_GetSystemTime() - u32Time) > u32TimeConstraints )
        {
            printf("[XC]Ree Send Cmd timeout!!\n");
            bRet = FALSE;
            break;
        }
    }while(E_MBX_SUCCESS != enMbxResult);
    return bRet;
}
//Use for construct header
MS_BOOL _MDrv_XC_REE_MBX_SendMSG(void* pInstance,MBX_Msg *pMsg,void *pPara)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    MS_BOOL bRet = TRUE;
    MS_U32 u32Index =0;
    const MS_U32 u32MaxMBXParaNum = 10;
    MS_U8* u8pPara = pPara;
    for(u32Index = 0;u32Index<pXCResourcePrivate->u32TotalMailNum;u32Index++)
    {
        pMsg->u8ParameterCount = (pXCResourcePrivate->u32ResidentDataSize>= u32MaxMBXParaNum) ? u32MaxMBXParaNum : pXCResourcePrivate->u32ResidentDataSize;
        memset(pMsg->u8Parameters, 0, u32MaxMBXParaNum);
        memcpy(pMsg->u8Parameters, (void *)u8pPara, pMsg->u8ParameterCount);
        //Send Mail
        if(_MDrv_XC_REE_MBX_CheckSendMSG(pMsg)==FALSE)
        {
            bRet = FALSE;
            return bRet;
        }
        u8pPara+= pMsg->u8ParameterCount;
        pXCResourcePrivate->u32ResidentDataSize-=u32MaxMBXParaNum;
    }
    //after sendMSG init global variable
    pXCResourcePrivate->u32ResidentDataSize = pXCResourcePrivate->u32DataSize;
    return bRet;
}
MS_BOOL MDrv_XC_OPTEE_IsAnyPipe_Active(void *pInstance)
{
    //ipmux bank (0x102E) is share with Main/Main2/Sub

    MS_BOOL bRet = FALSE;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(0)],(void**)(&pXCResourcePrivate));
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB==1)
    XC_RESOURCE_PRIVATE* pXCResourcePrivate1 = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(1)],(void**)(&pXCResourcePrivate1));
#endif
    ////pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_cnt = 0;
    if ((pXCResourcePrivate->sthal_Optee.op_tee_xc[MAIN_WINDOW].isEnable == TRUE) ||
        (pXCResourcePrivate->sthal_Optee.op_tee_xc[SUB_WINDOW].isEnable == TRUE)
#if (PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB==1)
        ||
        (pXCResourcePrivate1->sthal_Optee.op_tee_xc[MAIN_WINDOW].isEnable == TRUE) ||
        (pXCResourcePrivate1->sthal_Optee.op_tee_xc[SUB_WINDOW].isEnable == TRUE)
#endif
        )
    {
        bRet = TRUE;
    }

    return bRet;
}

static E_APIXC_ReturnValue _MDrv_XC_SetMVOPAID(void* pInstance,MS_BOOL bSecure,SCALER_WIN enWindow)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    E_APIXC_ReturnValue enReturn = E_APIXC_RET_FAIL;
    if((MDrv_ReadByte(REG_IPMUX_3F_L) &0x01)==FALSE)//check aid select table enable
    {
        printf("%s,%d dual aid path not enable\n",__FUNCTION__,__LINE__);

        return enReturn;
    }
    else
    {
        EN_XC_DRAM_AID enDramAid = E_XC_DRAM_AID_MAX;
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
        if(psXCInstPri->u32DeviceID == 1)
        {
            enDramAid = E_XC_DRAM_AID_DC1;
        }
        else if(psXCInstPri->u32DeviceID == 0)
        {
            enDramAid = E_XC_DRAM_AID_DC0;
        }
#else
        if(enWindow == SUB_WINDOW)
        {
            enDramAid = E_XC_DRAM_AID_DC1;
        }
        else if(enWindow == MAIN_WINDOW)
        {
            enDramAid = E_XC_DRAM_AID_DC0;
        }
#endif

        MHal_XC_SetDualAID(pInstance,bSecure,enDramAid);
        enReturn = E_APIXC_RET_OK;
    }
    return enReturn;
}
MS_BOOL MDrv_XC_OPTEE_Control(void *pInstance,EN_XC_OPTEE_ACTION action,XC_OPTEE_HANDLER* pstxc_handler)
{
    MS_BOOL bopteeenable = FALSE;
    ///MS_U32 u32tmp_address = 0;
    ///MS_U32 u32offset = 0;
    MS_U32 u32Index = 0;
    MS_U32 u32Cnt=0;
    MS_U32 u32TmpMemSize = 0;
    MS_PHY phyTmpMemAddr = 0;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);

    if(pstxc_handler == NULL)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_SVP,"Receive NULL pointer!\n");
        return FALSE;
    }

#ifndef MSOS_TYPE_OPTEE
#ifndef DONT_USE_CMA
#if (XC_SUPPORT_CMA ==TRUE)
    static MS_BOOL bIsGetCMA[CMA_XC_MEM_NUM] = {FALSE,};
#endif
#endif
#endif

#ifndef MSOS_TYPE_OPTEE
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
#else
    XC_RESOURCE_PRIVATE_FOR_TEE* pXCResourcePrivate = NULL;
#endif
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    /*
    ** if SC2 - main as SC0 - sub ,take care this as sub , therefore eWindow id has to be take case specilly
    */
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
    if (psXCInstPri->u32DeviceID == 1 && pstxc_handler->eWindow == MAIN_WINDOW)
    {
        pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].eWindow = SUB_WINDOW; //if PIP_SUB special case
    }else
#endif
    {
        pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].eWindow = pstxc_handler->eWindow; //if PIP_SUB special case
    }

    XC_LOG_TRACE(XC_DBGLEVEL_SVP,"XC OPTEE Control Action=%d\n", action);

    //Check XC_OPTEE_HANDLER structure size and version
    pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].version = XC_OPTEE_HANDLER_VERSION;
    pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].length = sizeof(XC_OPTEE_HANDLER);
    printf("Size of XC_OPTEE_HANDLER in REE = %u, xc_handler = %u\n", (unsigned int)sizeof(XC_OPTEE_HANDLER), (unsigned int)(pstxc_handler->length));
    printf("Verion in REE = %d, xc_handler =%d\n", XC_OPTEE_HANDLER_VERSION, pstxc_handler->version);
    if((pstxc_handler->length != sizeof(XC_OPTEE_HANDLER)) || (pstxc_handler->version != XC_OPTEE_HANDLER_VERSION))
    {
        printf("REE and xc_handler(input parameter) not match!\n");
    }

    switch (action)
    {
//********relay SN*********//
        case E_XC_OPTEE_GET_PIPE_ID:
            //get pipe in into shm'
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
            if (psXCInstPri->u32DeviceID == 1 && pstxc_handler->eWindow == MAIN_WINDOW)
            {
                pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].pipeID =pipeID_SC1_main;//?
                pstxc_handler->pipeID = pipeID_SC1_main;
            }else if(psXCInstPri->u32DeviceID == 0)
#endif
            {
                if(pstxc_handler->eWindow == MAIN_WINDOW)
                {
                    pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].pipeID = pipeID_SC0_main;
                    pstxc_handler->pipeID = pipeID_SC0_main;
                }
                else
                {
                    pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].pipeID = pipeID_SC0_sub;
                    pstxc_handler->pipeID = pipeID_SC0_sub;
                }
            }
            break;
        case E_XC_OPTEE_ENABLE:
#if (XC_SUPPORT_IOMMU == TRUE)
            if(pXCResourcePrivate->stdrvXC_MVideo_Context.stMMAfeature.u32Support == MSOS_MMA_SUPPORT)
            {
                pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].isEnable = pstxc_handler->isEnable;
                //clean drams cnt
                pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].drams_cnt = 0;
                pXCResourcePrivate->sthal_Optee.op_tee_xc[MAIN_WINDOW].u8OPTEEVersion = MHal_XC_GetOPTeeVersion(_u32SVPVersion);
                if (SVP_PARSE_HW_VER(MHal_XC_GetOPTeeVersion(_u32SVPVersion)) == E_XC_OPTEE_VER_9BIT)
                {
                    // Get CMA:
                    if (pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[pstxc_handler->eWindow] == TRUE)
                    {
                        MS_PHY u64MMAAddr = 0;
                        MS_U32 u32GetMMASize = 0;
                        MS_BOOL bSecure = pstxc_handler->isEnable;//secure buffer
                        u32GetMMASize = MHal_XC_GetMMAUsingBufferSize(pInstance,pstxc_handler->eWindow);
                        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][MMA], u32PreMMASize = %x u32GetMMASize = %x.\n",__func__,__LINE__,pXCResourcePrivate->stdrvXC_MVideo.au32PreMMAXCSize[pstxc_handler->eWindow], u32GetMMASize);

                        if ((pXCResourcePrivate->stdrvXC_MVideo.abXCMMANeedAuthorize[pstxc_handler->eWindow] == FALSE) ||
                            ((pXCResourcePrivate->stdrvXC_MVideo.abXCMMANeedAuthorize[pstxc_handler->eWindow] == TRUE)
                              && (u32GetMMASize != pXCResourcePrivate->stdrvXC_MVideo.au32PreMMAXCSize[pstxc_handler->eWindow]))
                        )
                        {
                            Hal_SC_set_freezeimg(pInstance, ENABLE, MAIN_WINDOW);
                            if (pXCResourcePrivate->stdrvXC_MVideo.au32PreMMAXCSize[pstxc_handler->eWindow] != 0)
                            {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
                                MHal_XC_ReleaseMMA(pInstance,pstxc_handler->eWindow);
#else
                                MS_BOOL bRet = FALSE;

                                bRet = Mdrv_XC_Task_WaitEventDone(
                                pInstance,
                                E_XC_MMA_EVENT_XC_PUT,
                                E_XC_MMA_EVENT_XC_PUT_DONE,
                                TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                                pstxc_handler->eWindow);
                                if(bRet == FALSE)
                                {
                                    printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                                }
#endif
                             }
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
                            u64MMAAddr = MHal_XC_GetMMAAddr(pInstance,u32GetMMASize,bSecure,pstxc_handler->eWindow);
                            pXCResourcePrivate->stdrvXC_MVideo.au32PreMMAXCSize[pstxc_handler->eWindow] = u32GetMMASize;
                            pXCResourcePrivate->stdrvXC_MVideo.aphyXCMMABaseAddress[pstxc_handler->eWindow] = u64MMAAddr;
#else
                            //use thread to get memory.
                            MS_BOOL bRet = FALSE;

                            bRet = Mdrv_XC_Task_WaitEventDone(
                            pInstance,
                            E_XC_MMA_EVENT_XC_GET,
                            E_XC_MMA_EVENT_XC_GET_DONE,
                            TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                            pstxc_handler->eWindow);
                            if(bRet == FALSE)
                            {
                                printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                            }
                            u64MMAAddr = pXCResourcePrivate->stdrvXC_MVideo.aphyXCMMABaseAddress[pstxc_handler->eWindow];
#endif
                            Hal_SC_add_dram_to_shm(pInstance,pstxc_handler->eWindow,u64MMAAddr,u32GetMMASize);
                            MDrv_XC_SetFrameBufferAddress(pInstance,u64MMAAddr, pXCResourcePrivate->stdrvXC_MVideo.au32PreMMAXCSize[pstxc_handler->eWindow], E_XC_BUFFER_DEFAULT, pstxc_handler->eWindow);
                            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d][MMA], u64MMAAddr = %td\n",__func__, __LINE__, (ptrdiff_t)u64MMAAddr);

#if (HW_DESIGN_4K2K_VER == 4)
                            if ((MDrv_XC_Is_DSForceIndexEnabled(pInstance, MAIN_WINDOW)||MDrv_XC_GetDynamicScalingStatus(pInstance))
                                && IsSrcTypeStorage(gSrcInfo[pstxc_handler->eWindow].enInputSourceType))
                            {
                                // MM source and DS on
                                if(!gSrcInfo[pstxc_handler->eWindow].bInterlace)
                                {
                                    //DS ON , 4K2K case
                                    Hal_SC_frcmw_Memory_Write_Request(pInstance, DISABLE, MAIN_WINDOW);
                                }
                                else
                                {
                                    Hal_SC_frcmw_Memory_Write_Request(pInstance, ENABLE, MAIN_WINDOW);
                                }
                            }
                            else if ((gSrcInfo[pstxc_handler->eWindow].stCapWin.width > 3000)
                                &&(gSrcInfo[pstxc_handler->eWindow].stCapWin.height> 1050))
                            {
                                Hal_SC_frcmw_Memory_Write_Request(pInstance, DISABLE, MAIN_WINDOW);
                            }
                            else
                            {
                                Hal_SC_frcmw_Memory_Write_Request(pInstance, ENABLE, MAIN_WINDOW);
                            }
#endif
                            Hal_SC_set_freezeimg(pInstance, DISABLE, MAIN_WINDOW);

                            pXCResourcePrivate->stdrvXC_Scaling.abIsTeeGetMMA[pstxc_handler->eWindow] = TRUE;
                            pXCResourcePrivate->stdrvXC_MVideo.abXCMMANeedAuthorize[pstxc_handler->eWindow] = TRUE;

#if (FRC_INSIDE == TRUE)
                            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"MMA: MMA FRC Alloc Start.\n");
                            if(_MDrv_FRC_GetMemoryFromMMA(pInstance, MAIN_WINDOW) == FALSE)
                            {
                                printf("%s,%d,Get FRC MMA memory fail\n",__FUNCTION__,__LINE__);
                            }
                            else
                            {
                                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"MMA: %s,%d MMA FRC Alloc Done.\n",__FUNCTION__,__LINE__);
                            }
                             Hal_SC_add_dram_to_shm(pInstance,pstxc_handler->eWindow,pXCResourcePrivate->stdrvXC_MVideo.phyFRCLeftMMABaseAddress, pXCResourcePrivate->stdrvXC_MVideo.u32PreMMAFRC_LSize);
#endif
                        }
                        else
                        {
                            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "[%s,%5d][MMA], the size is the same as the size of last time!\n",__func__, __LINE__);
                        }
                    }
                    else
                    {
                        pXCResourcePrivate->stdrvXC_Scaling.abIsTeeGetMMA[pstxc_handler->eWindow] = FALSE;
                    }

                    pXCResourcePrivate->stdrvXC_MVideo.abSecure[pstxc_handler->eWindow] = pstxc_handler->isEnable;
                    if(E_APIXC_RET_OK != _MDrv_XC_SetMVOPAID(pInstance,pstxc_handler->isEnable,pstxc_handler->eWindow))
                    {
                        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "[OPTEE] [MMA] E_XC_OPTEE_ENABLE set AID fail!\n");
                        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                        return FALSE;
                    }
                    else
                    {
                        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "[OPTEE] [MMA] E_XC_OPTEE_ENABLE set mvop AID OK!\n");
                    }
                    pXCResourcePrivate->sthal_Optee.enOpTeeStatus[pstxc_handler->eWindow] = E_XC_OPTEE_STATUS_ENABLE;
                    _MDrv_XC_TEE_Send_Cmd(pInstance,E_XC_OPTEE_SYNC_HANDLER,&pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow],sizeof(XC_OPTEE_HANDLER));
                }

            }
            else
#endif
            {
                //reset forst
                pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].drams_cnt = 0;
                pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].regs_cnt = 0;

                if (pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].isEnable == TRUE)
                {
                    bopteeenable = TRUE;
                }
                pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].isEnable = TRUE;
                if(_u32SVPVersion >3)
                {
                    pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].u8OPTEEVersion = MHal_XC_GetOPTeeVersion(_u32SVPVersion);
                    pXCResourcePrivate->stdrvXC_MVideo.abXCCMANeedAuthorize[pstxc_handler->eWindow] = TRUE;
                    XC_LOG_TRACE(XC_DBGLEVEL_SVP,"u8OPTEEVersion = %d\n",pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].u8OPTEEVersion);
                }
                //on flag , get cma , set_handler
#ifndef MSOS_TYPE_OPTEE
#ifndef DONT_USE_CMA
#if (XC_SUPPORT_CMA ==TRUE)
                if(psXCInstPri->u32DeviceID == 0 && pstxc_handler->eWindow == MAIN_WINDOW)
                {
                    MS_U32* pu32CMAMemSCMSize = NULL;
                    MS_U32* pu32CMAMemFRCMSize= NULL;
                    MS_U32 u32DualMiuMemSize = 0;
                    XC_CMA_CLIENT enXCCMAClient = CMA_XC_SELF_MEM;
                    static MS_PHY u32CMAAddr[CMA_XC_MEM_NUM] = {0, };

                    pu32CMAMemSCMSize = malloc(sizeof(MS_U32)*CMA_XC_MEM_NUM);
                    if(pu32CMAMemSCMSize == NULL)
                    {
                        XC_LOG_TRACE(XC_DBGLEVEL_SVP,"[%s,%5d]pu32CMAMemSCMSize malloc fail\n",__func__, __LINE__);
                        return FALSE;
                    }
                    memset(pu32CMAMemSCMSize,0,(sizeof(MS_U32)*CMA_XC_MEM_NUM));
                    pu32CMAMemFRCMSize= malloc(sizeof(MS_U32)*CMA_XC_MEM_NUM);
                    if(pu32CMAMemFRCMSize == NULL)
                    {
                        XC_LOG_TRACE(XC_DBGLEVEL_SVP,"[%s,%5d]pu32CMAMemFRCMSize malloc fail\n",__func__, __LINE__);
                        if(pu32CMAMemSCMSize)
                        {
                            free(pu32CMAMemSCMSize);
                        }
                        return FALSE;
                    }
                    memset(pu32CMAMemFRCMSize,0,(sizeof(MS_U32)*CMA_XC_MEM_NUM));

                    //get cma
                    MHal_XC_Get_CMA_UsingCondition(pInstance, bIsGetCMA, pu32CMAMemSCMSize, pu32CMAMemFRCMSize, &u32DualMiuMemSize, CMA_XC_MEM_NUM, pstxc_handler->eWindow);
                    if (u32DualMiuMemSize != 0)
                    {
                        enXCCMAClient = CMA_XC_SELF_MEM;
                    }
                    else
                    {
                        if (bIsGetCMA[CMA_XC_COBUFF_MEM] == TRUE)
                        {
                            enXCCMAClient = CMA_XC_COBUFF_MEM;
                        }
                        else
                        {
                            enXCCMAClient = CMA_XC_SELF_MEM;
                        }
                    }

                    if (((bopteeenable == FALSE)&&(pXCResourcePrivate->stdrvXC_Scaling._au32PreFBSize[pstxc_handler->eWindow] == 0))
                        ||(!IsSrcTypeStorage(gSrcInfo[pstxc_handler->eWindow].enInputSourceType)))
                    {
                        pXCResourcePrivate->stdrvXC_MVideo.au32XCSCMIMemSize[MAIN_WINDOW] = pu32CMAMemSCMSize[enXCCMAClient]+pu32CMAMemFRCMSize[enXCCMAClient];
                        XC_LOG_TRACE(XC_DBGLEVEL_SVP,"[%s,%5d]SVP Memsize = %x\n",__func__, __LINE__, pXCResourcePrivate->stdrvXC_MVideo.au32XCSCMIMemSize[MAIN_WINDOW]);
                        XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW,"[%s,%5d]SVP Memsize = %x\n",__func__, __LINE__, pXCResourcePrivate->stdrvXC_MVideo.au32XCSCMIMemSize[MAIN_WINDOW]);
                        u32CMAAddr[enXCCMAClient] = MHal_XC_Get_CMA_Addr(pInstance, enXCCMAClient,  pu32CMAMemSCMSize[enXCCMAClient]+pu32CMAMemFRCMSize[enXCCMAClient], pstxc_handler->eWindow);
                    }
                    else if((bopteeenable == FALSE)&&(pXCResourcePrivate->stdrvXC_Scaling._au32PreFBSize[pstxc_handler->eWindow] != 0))
                    {
                        u32CMAAddr[enXCCMAClient] = pXCResourcePrivate->stdrvXC_Scaling._au32PreFBAddress[pstxc_handler->eWindow];
                        pu32CMAMemSCMSize[enXCCMAClient] = pXCResourcePrivate->stdrvXC_Scaling._au32PreFBSize[pstxc_handler->eWindow];
                        bIsGetCMA[CMA_XC_SELF_MEM] = FALSE;
                        bIsGetCMA[CMA_XC_COBUFF_MEM] = FALSE;
                        MHal_SC_Memory_Request_Force_Off(pInstance, FALSE, E_XC_FORCE_MEMORY_REQUEST_OFF_ALL);
                    }
                    Hal_SC_add_dram_to_shm(pInstance,pstxc_handler->eWindow,u32CMAAddr[enXCCMAClient], pu32CMAMemSCMSize[enXCCMAClient]);
#if (HW_DESIGN_4K2K_VER == 4)
                    Hal_SC_add_dram_to_shm(pInstance,pstxc_handler->eWindow,u32CMAAddr[enXCCMAClient]+pu32CMAMemSCMSize[enXCCMAClient], pu32CMAMemFRCMSize[enXCCMAClient]);
#endif
#if (HW_DESIGN_4K2K_VER == 4)
                    if(pu32CMAMemSCMSize[enXCCMAClient] == 0)
                    {
                        // default frcm frame buffer address init
                        MDrv_XC_SetFRCMFrameBufferAddress(pInstance, u32CMAAddr[enXCCMAClient], pu32CMAMemFRCMSize[enXCCMAClient], pstxc_handler->eWindow);
                        // default frame buffer address init
                        MDrv_XC_SetFrameBufferAddress(pInstance, u32CMAAddr[enXCCMAClient], pu32CMAMemFRCMSize[enXCCMAClient],E_XC_BUFFER_DEFAULT, pstxc_handler->eWindow);
                    }
                    else
#endif
                    {
#if (HW_DESIGN_4K2K_VER == 4)
                        // default frcm frame buffer address init
                        MDrv_XC_SetFRCMFrameBufferAddress(pInstance, u32CMAAddr[enXCCMAClient]+pu32CMAMemSCMSize[enXCCMAClient], pu32CMAMemFRCMSize[enXCCMAClient], pstxc_handler->eWindow);
#endif
                        // default frame buffer address init
                        MDrv_XC_SetFrameBufferAddress(pInstance, u32CMAAddr[enXCCMAClient], pu32CMAMemSCMSize[enXCCMAClient],E_XC_BUFFER_DEFAULT, pstxc_handler->eWindow);
                    }
                    gSrcInfo[pstxc_handler->eWindow].u32PreCMAMemSCMSize[enXCCMAClient] = pu32CMAMemSCMSize[enXCCMAClient];
                    gSrcInfo[pstxc_handler->eWindow].u32PreCMAMemFRCMSize[enXCCMAClient] = 0;
                    ///u32offset = _XC_Device_Offset[psXCInstPri->u32DeviceID];
                    ///u32tmp_address = u32CMAAddr[enXCCMAClient];
                    free(pu32CMAMemSCMSize);
                    free(pu32CMAMemFRCMSize);
                }
                else
#endif
#endif
                {
                    pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].isEnable = TRUE;
                    if (pstxc_handler->eWindow == MAIN_WINDOW)
                    {
#if (FRC_INSIDE == TRUE)
                        //XC
                        phyTmpMemAddr = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FB_Start_Addr;
                        u32TmpMemSize = pXCResourcePrivate->stdrvXC_MVideo.au32XCSCMIMemSize[pstxc_handler->eWindow];
                        Hal_SC_add_dram_to_shm(pInstance, pstxc_handler->eWindow, phyTmpMemAddr, u32TmpMemSize);
                        XC_LOG_TRACE(XC_DBGLEVEL_SVP,"[%s,%5d] u32Main_FB_Start_Addr=0x%tx, au32XCSCMIMemSize=0x%tx\n",
                                     __func__, __LINE__, (ptrdiff_t)phyTmpMemAddr, (ptrdiff_t)u32TmpMemSize);

                        //FRC_L + FRC_R
                        phyTmpMemAddr = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u32FRC_MEMC_MemAddr;
                        u32TmpMemSize = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u32FRC_MEMC_MemSize
                                        + pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_Pnl_Misc.FRCInfo.u32FRC_R2_MemSize;
                        Hal_SC_add_dram_to_shm(pInstance, pstxc_handler->eWindow, phyTmpMemAddr, u32TmpMemSize);
                        XC_LOG_TRACE(XC_DBGLEVEL_SVP,"[%s,%5d] u32FRC_MEMC_MemAddr=0x%tx, u32FRC_MEMC_MemSize+u32FRC_R2_MemSize=0x%tx\n",
                                     __func__, __LINE__, (ptrdiff_t)phyTmpMemAddr, (ptrdiff_t)u32TmpMemSize);
#else
                        //XC
                        phyTmpMemAddr = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FB_Start_Addr;
                        u32TmpMemSize = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FB_Size;
                        Hal_SC_add_dram_to_shm(pInstance,pstxc_handler->eWindow, phyTmpMemAddr, u32TmpMemSize);
                        XC_LOG_TRACE(XC_DBGLEVEL_SVP,"[%s,%5d] u32Main_FB_Start_Addr=0x%tx, u32Main_FB_Size=0x%tx\n",
                                     __func__, __LINE__, (ptrdiff_t)phyTmpMemAddr, (ptrdiff_t)u32TmpMemSize);
#endif
#if (HW_DESIGN_4K2K_VER == 4)
                        Hal_SC_add_dram_to_shm(pInstance,pstxc_handler->eWindow,pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FRCM_FB_Start_Addr, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Main_FRCM_FB_Size);
#endif
                    }
                    else if (pstxc_handler->eWindow == SUB_WINDOW)
                    {
                        ///u32tmp_address = pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FB_Start_Addr;
                        Hal_SC_add_dram_to_shm(pInstance,pstxc_handler->eWindow,pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FB_Start_Addr, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FB_Size);
#if (HW_DESIGN_4K2K_VER == 4)
                        Hal_SC_add_dram_to_shm(pInstance,pstxc_handler->eWindow,pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FRCM_FB_Start_Addr, pXCResourcePrivate->stdrvXC_MVideo_Context.g_XC_InitData.u32Sub_FRCM_FB_Size);
#endif
                    }
                    ///u32offset = _XC_Device_Offset[psXCInstPri->u32DeviceID];
                }
#endif
                ///u32tmp_address /=BYTE_PER_WORD;
                Hal_SC_update_to_shm(pInstance, pstxc_handler->eWindow);
#if 0
                if (pstxc_handler->eWindow == MAIN_WINDOW)
                {
                Hal_SC_add_reg_to_shm(pInstance,pstxc_handler->eWindow,REG_SC_BK0D_38_L,(BIT(0) | BIT(4)), (BIT(0) | BIT(4)));
                Hal_SC_add_reg_to_shm(pInstance,pstxc_handler->eWindow,REG_SC_BK12_08_L, (u32tmp_address & 0xFFFF), 0xFFFF);
                Hal_SC_add_reg_to_shm(pInstance,pstxc_handler->eWindow,REG_SC_BK12_09_L, (u32tmp_address >> 16), 0xFFFF);
                Hal_SC_add_reg_to_shm(pInstance,pstxc_handler->eWindow,REG_SC_BK12_1C_L, 0x0000, 0xFFFF);
                }
                else if (pstxc_handler->eWindow == SUB_WINDOW)
                {
                    Hal_SC_add_reg_to_shm(pInstance,pstxc_handler->eWindow,REG_SC_BK0D_38_L,(BIT(1) | BIT(6)), (BIT(1) | BIT(6)));
                    Hal_SC_add_reg_to_shm(pInstance,pstxc_handler->eWindow,REG_SC_BK12_48_L, (u32tmp_address & 0xFFFF), 0xFFFF);
                    Hal_SC_add_reg_to_shm(pInstance,pstxc_handler->eWindow,REG_SC_BK12_49_L, (u32tmp_address >> 16), 0xFFFF);
                    Hal_SC_add_reg_to_shm(pInstance,pstxc_handler->eWindow,REG_SC_BK12_5C_L, 0x0000, 0xFFFF);
                }
#endif
                if((_u32SVPVersion >3) &&
                   (SVP_PARSE_HW_VER(MHal_XC_GetOPTeeVersion(_u32SVPVersion))!= E_XC_OPTEE_VER_1BIT))
                {
                    if(E_APIXC_RET_OK != _MDrv_XC_SetMVOPAID(pInstance,TRUE,pstxc_handler->eWindow))
                    {
                        printf("%s @ line %d [OPTEE]E_XC_OPTEE_ENABLE set AID fail!\n", __FUNCTION__,__LINE__);
                        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                        return FALSE;
                    }
                }
                _MDrv_XC_TEE_Send_Cmd(pInstance,E_XC_OPTEE_SYNC_HANDLER,&pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow],sizeof(XC_OPTEE_HANDLER));
            }
            break;
        case E_XC_OPTEE_DISABLE:
#if (XC_SUPPORT_IOMMU == TRUE)
            if(pXCResourcePrivate->stdrvXC_MVideo_Context.stMMAfeature.u32Support == MSOS_MMA_SUPPORT)
            {
                if(SVP_PARSE_HW_VER(MHal_XC_GetOPTeeVersion(_u32SVPVersion))== E_XC_OPTEE_VER_9BIT)
                {
                    pXCResourcePrivate->stdrvXC_MVideo.abSecure[pstxc_handler->eWindow] = FALSE;
                    if(E_APIXC_RET_OK != _MDrv_XC_SetMVOPAID(pInstance,FALSE,pstxc_handler->eWindow))
                    {
                        printf("%s @ line %d [OPTEE]E_XC_OPTEE_DISABLE set AID fail!\n", __FUNCTION__,__LINE__);
                        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                        return FALSE;
                    }

                    bopteeenable = TRUE;
                    if (pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].isEnable == FALSE)
                    {
                        bopteeenable = FALSE;
                    }

                    if(pXCResourcePrivate->stdrvXC_MVideo_Context.stMMAfeature.u32Support == MSOS_MMA_SUPPORT)
                    {
                        if ((pstxc_handler->eWindow == MAIN_WINDOW)
                            && (pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_XC_MAIN] == TRUE))
                        {
                            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "[OPTEE] [MMA] E_XC_OPTEE_DISABLE Unauthorize_MMA Main.\n");
                            if (pXCResourcePrivate->sthal_Optee.enOpTeeStatus[pstxc_handler->eWindow] != E_XC_OPTEE_STATUS_UNAUTHORIZE)
                            {
                                if (MHal_XC_UnauthorizeMMA(pInstance,pstxc_handler->eWindow) == FALSE)
                                {
                                    printf("%s @ line %d [OPTEE] [MMA] E_XC_OPTEE_DISABLE MHal_XC_UnauthorizeMMA fail!\n", __FUNCTION__,__LINE__);
                                }
                                else
                                {
                                    pXCResourcePrivate->sthal_Optee.enOpTeeStatus[pstxc_handler->eWindow] = E_XC_OPTEE_STATUS_UNAUTHORIZE;
                                }
                            }

                            // release MMA
                            if ((bopteeenable == TRUE) && (pXCResourcePrivate->stdrvXC_Scaling.abIsTeeGetMMA[pstxc_handler->eWindow] == TRUE))
                            {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
                                MHal_XC_ReleaseMMA(pInstance,MAIN_WINDOW);
#else
                                MS_BOOL bRet = FALSE;

                                bRet = Mdrv_XC_Task_WaitEventDone(
                                pInstance,
                                E_XC_MMA_EVENT_XC_PUT,
                                E_XC_MMA_EVENT_XC_PUT_DONE,
                                TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                                pstxc_handler->eWindow);
                                if(bRet == FALSE)
                                {
                                    printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                                }
#endif
                                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "[OPTEE] [MMA] E_XC_OPTEE_DISABLE release_MMA Main Done.\n");
                                pXCResourcePrivate->stdrvXC_Scaling.abIsTeeGetMMA[pstxc_handler->eWindow] = FALSE;
                            }
                        }

                        if ((pstxc_handler->eWindow == SUB_WINDOW)
                            && (pXCResourcePrivate->stdrvXC_MVideo_Context.au8BufTagInfoSupport[E_MMA_BUF_TAG_NAME_XC_SUB] == TRUE))
                        {
                            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "[OPTEE] [MMA] E_XC_OPTEE_DISABLE Unauthorize_MMA Sub.\n");
                            if (pXCResourcePrivate->sthal_Optee.enOpTeeStatus[pstxc_handler->eWindow] != E_XC_OPTEE_STATUS_UNAUTHORIZE)
                            {
                                if (MHal_XC_UnauthorizeMMA(pInstance,pstxc_handler->eWindow) == FALSE)
                                {
                                    printf("%s @ line %d [OPTEE] [MMA] E_XC_OPTEE_DISABLE MHal_XC_UnauthorizeMMA fail!\n", __FUNCTION__,__LINE__);
                                }
                                else
                                {
                                    pXCResourcePrivate->sthal_Optee.enOpTeeStatus[pstxc_handler->eWindow] = E_XC_OPTEE_STATUS_UNAUTHORIZE;
                                }
                            }

                            // release MMA
                            if ((bopteeenable == TRUE) && (pXCResourcePrivate->stdrvXC_Scaling.abIsTeeGetMMA[pstxc_handler->eWindow] == TRUE))
                            {
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
                                MHal_XC_ReleaseMMA(pInstance, SUB_WINDOW);
#else
                                MS_BOOL bRet = FALSE;

                                bRet = Mdrv_XC_Task_WaitEventDone(
                                pInstance,
                                E_XC_MMA_EVENT_XC_PUT,
                                E_XC_MMA_EVENT_XC_PUT_DONE,
                                TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                                pstxc_handler->eWindow);
                                if(bRet == FALSE)
                                {
                                    printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                                }
#endif
                                XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "[OPTEE] [MMA] E_XC_OPTEE_DISABLE release_MMA Sub Done.\n");
                                pXCResourcePrivate->stdrvXC_Scaling.abIsTeeGetMMA[pstxc_handler->eWindow] = FALSE;
                            }
                        }

#if (FRC_INSIDE == TRUE)
                        MHal_FRC_UnauthorizeMMA(pInstance,pstxc_handler->eWindow);
#if (XC_MMA_ALLOC_USE_THREAD == FALSE)
                        MHal_FRC_ReleaseMMA(pInstance);
#else
                        MS_BOOL bRet = FALSE;

                        bRet = Mdrv_XC_Task_WaitEventDone(
                        pInstance,
                        E_XC_MMA_EVENT_FRC_PUT,
                        E_XC_MMA_EVENT_FRC_PUT_DONE,
                        TICK_PER_ONE_MS*MMA_ACTION_TIMEOUT,
                        pstxc_handler->eWindow);
                        if(bRet == FALSE)
                        {
                            printf("[%s][%d] Wait Event fail\n",__FUNCTION__,__LINE__);
                        }
#endif
#endif
                    }

                    pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].isEnable = FALSE;
                }
            }
            else
#endif
            {
                //off flag
                bopteeenable = TRUE;
                if (pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].isEnable == FALSE)
                {
                    bopteeenable = FALSE;
                }
                pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].isEnable = FALSE;
                if((_u32SVPVersion >3) &&
                   (SVP_PARSE_HW_VER(MHal_XC_GetOPTeeVersion(_u32SVPVersion))!= E_XC_OPTEE_VER_1BIT))
                {
                    if(E_APIXC_RET_OK != _MDrv_XC_SetMVOPAID(pInstance,FALSE,pstxc_handler->eWindow))
                    {
                        printf("%s @ line %d [OPTEE]E_XC_OPTEE_DISABLE set AID fail!\n", __FUNCTION__,__LINE__);
                        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                        return FALSE;
                    }
                }
#ifndef MSOS_TYPE_OPTEE
#ifndef MSOS_TYPE_NUTTX
                if(psXCInstPri->u32DeviceID == 0 && pstxc_handler->eWindow == MAIN_WINDOW)
                {
#ifndef DONT_USE_CMA
#if (XC_SUPPORT_CMA ==TRUE)
                    if (bopteeenable == TRUE)
                    {
                        if (bIsGetCMA[CMA_XC_COBUFF_MEM] == TRUE)
                        {
                            MHal_XC_Release_CMA(pInstance, CMA_XC_COBUFF_MEM, MAIN_WINDOW);
                        }
                        else if(bIsGetCMA[CMA_XC_SELF_MEM] == TRUE)
                        {
                            MHal_XC_Release_CMA(pInstance, CMA_XC_SELF_MEM, MAIN_WINDOW);
                        }
                    }
#endif
#endif
                }
#else
                UNUSED(bopteeenable);
#endif
#endif
            }
            break;
//*******pipe only********//
        case E_XC_OPTEE_SET_HANDLER:
            _MDrv_XC_TEE_Send_Cmd(pInstance,E_XC_OPTEE_SYNC_HANDLER,&pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow],sizeof(XC_OPTEE_HANDLER));
            break;
        case E_XC_OPTEE_SYNC_HANDLER:
            memcpy(&pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow],pstxc_handler,sizeof(XC_OPTEE_HANDLER));
#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
            if (psXCInstPri->u32DeviceID == 1 && pstxc_handler->eWindow == MAIN_WINDOW)
            {
                pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].eWindow = SUB_WINDOW; //if PIP_SUB special case
            }else
#endif
            {
                pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].eWindow = pstxc_handler->eWindow; //if PIP_SUB special case
            }
            break;
        case E_XC_OPTEE_GET_HANDLER:
            //get from shm
            memcpy(pstxc_handler,&pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow],sizeof(XC_OPTEE_HANDLER));
            break;
        case E_XC_OPTEE_UPDATE_HANDLER:
            //****set seal directly****//
            memcpy(&pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow],pstxc_handler,sizeof(XC_OPTEE_HANDLER));
            u32Cnt = pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].regs_cnt;
            for(u32Index = 0 ; u32Index < u32Cnt ; u32Index++)
            {
                        if (u32Index>= OP_TEE_XC_REG_MAX_CNT)
            {
                    printf("array size overflow for xc part");
                    break;
                }
                MDrv_Write2ByteMask(pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].regs_data[u32Index].bk
                                  , pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].regs_data[u32Index].value
                                  , pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].regs_data[u32Index].mask);
            }
            break;
        case E_XC_OPTEE_SET_PIPEID:
            //****set seal directly****//
            pXCResourcePrivate->stdrvXC_MVideo.au32pipeID[pstxc_handler->eWindow] = pstxc_handler->pipeID;
            pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].pipeID = pstxc_handler->pipeID;
            XC_LOG_TRACE(XC_DBGLEVEL_SETWINDOW, "MMA set u32pipeID[%u]: %x.\n", pstxc_handler->eWindow, pstxc_handler->pipeID);
            break;
        case E_XC_OPTEE_GET_VERSION:
            pstxc_handler->u8OPTEEVersion =(MS_U8)MHal_XC_GetOPTeeVersion(_u32SVPVersion);
            break;

        case E_XC_OPTEE_GET_SECURE_INPUT:
            if(_MDrv_XC_TEE_Send_Cmd(pInstance,E_XC_OPTEE_GET_SECURE_INPUT,pstxc_handler,sizeof(XC_OPTEE_HANDLER)) == FALSE)
            {
                XC_LOG_TRACE(XC_DBGLEVEL_SVP,"_MDrv_XC_TEE_Send_Cmd fail\n");
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                return FALSE;
            }
            XC_LOG_TRACE(XC_DBGLEVEL_SVP,"Get secure input done. enSecureInput=%d, u32SecureInputInfo=%u\n", pstxc_handler->enSecureInput, pstxc_handler->u32SecureInputInfo);
            break;

        case E_XC_OPTEE_SET_SECURE_INPUT:
            XC_LOG_TRACE(XC_DBGLEVEL_SVP,"OPTEE control failed. Can't set secure input in REE (write protect)\n");
            break;

        case E_XC_OPTEE_CONFIG_INPUT_PIPE:
            _u32SVPVersion = 4;
            pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].u32InputPipeID = pstxc_handler->u32InputPipeID;
            pXCResourcePrivate->stdrvXC_MVideo.abXCNeedPipelineBinding[pstxc_handler->eWindow] = TRUE;
            XC_LOG_TRACE(XC_DBGLEVEL_SVP, "Video pipeline ID: %x.\n", pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].u32InputPipeID);

            //Since SVP V4, XC need to set policy in setwindow
            pXCResourcePrivate->stdrvXC_MVideo.abXCNeedSetPolicy[pstxc_handler->eWindow] = TRUE;
            break;

        case E_XC_OPTEE_UNSET_VIDEO_PIPELINE:
            if((_u32SVPVersion > 3) && (pXCResourcePrivate->stdrvXC_MVideo.abXCNeedPipelineBinding[pstxc_handler->eWindow] == FALSE))  //Binding done
            {
                if(pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].u32InputPipeID != pstxc_handler->u32InputPipeID)
                {
                    XC_LOG_TRACE(XC_DBGLEVEL_SVP, "Pipeline Unbinding fail!! The Video PipeID is different, XC share mem:%u, input:%u\n", pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].u32InputPipeID, pstxc_handler->u32InputPipeID);
                    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                    return FALSE;
                }
                else if(pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].pipeID != pstxc_handler->pipeID)
                {
                    XC_LOG_TRACE(XC_DBGLEVEL_SVP, "Pipeline Unbinding fail!! The Display PipeID is different, XC share mem:%u, input:%u\n", pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].pipeID, pstxc_handler->pipeID);
                    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                    return FALSE;
                }
                else
                {
                    XC_LOG_TRACE(XC_DBGLEVEL_SVP, "Pipeline Unbinding start!! Video PipeID:%u, Display PipeID:%u\n", pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].u32InputPipeID, pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].pipeID);
                    if(_MDrv_XC_TEE_Send_Cmd(pInstance,E_XC_OPTEE_UNSET_VIDEO_PIPELINE, (void*)&pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow],sizeof(XC_OPTEE_HANDLER)) == FALSE)
                    {
                        XC_LOG_TRACE(XC_DBGLEVEL_SVP,"_MDrv_XC_TEE_Send_Cmd fail\n");
                        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                        return FALSE;
                    }
                    else
                    {
                        pXCResourcePrivate->sthal_Optee.op_tee_xc[pstxc_handler->eWindow].u32InputPipeID = 0;        //Clear video pipeline
                    }
                }
            }
            else
            {
                XC_LOG_TRACE(XC_DBGLEVEL_SVP, "Pipeline Unbinding fail!! Wrong flow, pipeline not binding yet.\n");
                _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
                return FALSE;
            }
            break;

        default:
            XC_LOG_TRACE(XC_DBGLEVEL_SVP, "OPTEE Control fail!! Invalid action: %d.\n", action);
            break;
    }
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return TRUE;
}
MS_BOOL MApi_XC_OPTEE_Control_U2(void *pInstance,EN_XC_OPTEE_ACTION action,XC_OPTEE_HANDLER* xc_handler)
{
    return MDrv_XC_OPTEE_Control(pInstance,action,xc_handler);
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_OPTEE_Control(EN_XC_OPTEE_ACTION action,XC_OPTEE_HANDLER* xc_handler)
{
    CheckXCInstanceOpen();

#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
     if (pu32XCInst_1 == NULL)
     {
        XC_INSTANT_ATTRIBUTE stXCInstantAttribute;
        INIT_XC_INSTANT_ATTR(stXCInstantAttribute);

        stXCInstantAttribute.u32DeviceID = 1;
        if(UtopiaOpen(MODULE_XC, &pu32XCInst_1, 0, &stXCInstantAttribute) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return E_APIXC_RET_FAIL;
        }
      }
#endif
    stXC_OPTEE_CTRL XCArgs;
    XCArgs.action = action;
    XCArgs.xc_handler = xc_handler;
    XCArgs.bReturn = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_OPTEE_CTL, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturn;
    }
}
#endif
MS_BOOL MDrv_XC_OPTEE_Update(void *pInstance,EN_XC_OPTEE_ACTION action,XC_OPTEE_HANDLER* xc_handler)
{
    ///MS_U32 u32tmp_address;
    ///MS_U32 u32offset = 0;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    if(xc_handler == NULL)
    {
        printf("Invalid OPTEE Update access\n");
        return FALSE;
    }

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);

    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    if(action != E_XC_OPTEE_UPDATE_HANDLER)
        return FALSE;
    //reset first
    // only update register setting, dram buffer lock max
    pXCResourcePrivate->sthal_Optee.op_tee_xc[xc_handler->eWindow].regs_cnt = 0;

   /// u32tmp_address = pXCResourcePrivate->stdrvXC_MVideo_Context.stDBreg.u32DNRBase0*BYTE_PER_WORD;

#if 0
    if(psXCInstPri->u32DeviceID == 0 && xc_handler->eWindow == MAIN_WINDOW)
    {
        //get cma
        u32offset = _XC_Device_Offset[psXCInstPri->u32DeviceID];
    }
    else
    {
        u32offset = _XC_Device_Offset[psXCInstPri->u32DeviceID];
    }
#endif
#if 0
    Hal_SC_add_reg_to_shm(pInstance,xc_handler->eWindow,REG_SC_BK0D_38_L,(BIT(0) | BIT(4)), (BIT(0) | BIT(4)));
    Hal_SC_add_reg_to_shm(pInstance,xc_handler->eWindow,REG_SC_BK12_08_L, (u32tmp_address & 0xFFFF), 0xFFFF);
    Hal_SC_add_reg_to_shm(pInstance,xc_handler->eWindow,REG_SC_BK12_09_L, (u32tmp_address >> 16), 0xFFFF);
    Hal_SC_add_reg_to_shm(pInstance,xc_handler->eWindow,REG_SC_BK12_1C_L, 0x0000, 0xFFFF);
#endif
    Hal_SC_update_to_shm(pInstance, xc_handler->eWindow);
    //************************PRINT DATA*******************************//
    //MS_U32 tmp_x=0;
    //for(tmp_x=0;tmp_x < pXCResourcePrivate->sthal_Optee.op_tee_xc[xc_handler->eWindow].regs_cnt;tmp_x++)
    //{
    //    printf("\033[1;32m[%s:%d][OPTEE][REG:%d] 0x%x:0x%x:0x%x\033[m\n",__FUNCTION__,__LINE__,tmp_x
    //        ,pXCResourcePrivate->sthal_Optee.op_tee_xc[xc_handler->eWindow].regs_data[tmp_x].bk
    //        ,pXCResourcePrivate->sthal_Optee.op_tee_xc[xc_handler->eWindow].regs_data[tmp_x].value
    //        ,pXCResourcePrivate->sthal_Optee.op_tee_xc[xc_handler->eWindow].regs_data[tmp_x].mask);
    //}
    //************************END PRINT DATA*******************************//
    _MDrv_XC_TEE_Send_Cmd(pInstance,E_XC_OPTEE_UPDATE_HANDLER,&pXCResourcePrivate->sthal_Optee.op_tee_xc[xc_handler->eWindow],sizeof(XC_OPTEE_HANDLER));
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return TRUE;
    //****go on
}
MS_BOOL MDrv_XC_OPTEE_Mux(void *pInstance,EN_XC_OPTEE_ACTION action,XC_OPTEE_MUX_DATA* xc_mux_data)
{
    MS_U32 u32Index = 0;
    MS_U32 u32Cnt=0;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    if(xc_mux_data == NULL)
    {
        printf("Invalid OPTEE Mux access\n");
        return FALSE;
    }

#ifndef MSOS_TYPE_OPTEE
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
#else
    XC_RESOURCE_PRIVATE_FOR_TEE* pXCResourcePrivate = NULL;
#endif
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    switch (action)
    {
        case E_XC_OPTEE_SET_MUX:
            pXCResourcePrivate->sthal_Optee.op_tee_mux.bfire = TRUE;
            _MDrv_XC_TEE_Send_Cmd(pInstance,E_XC_OPTEE_SYNC_MUX,&pXCResourcePrivate->sthal_Optee.op_tee_mux,sizeof(XC_OPTEE_MUX_DATA));
            pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_cnt = 0;
            pXCResourcePrivate->sthal_Optee.op_tee_mux.bfire = FALSE;
            //printf("\033[1;32m[%s:%d][NORMAL]XC_OPTEE_MUX_DATA=%d\033[m\n",__FUNCTION__,__LINE__,sizeof(XC_OPTEE_MUX_DATA));
            break;
        case E_XC_OPTEE_SYNC_MUX:
            memcpy(&pXCResourcePrivate->sthal_Optee.op_tee_mux,xc_mux_data,sizeof(XC_OPTEE_MUX_DATA));
            //************************PRINT DATA*******************************//
            //for(tmp_x=0;tmp_x < pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_cnt;tmp_x++)
           // {
           //     printf("\033[1;32m[%s:%d][OPTEE][REG:%d] 0x%x:0x%x:0x%x\033[m\n",__FUNCTION__,__LINE__,tmp_x
           //         ,pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_data[tmp_x].bk
           //         ,pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_data[tmp_x].value
           //         ,pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_data[tmp_x].mask);
           // }
            u32Cnt = pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_cnt;
            for(u32Index = 0 ; u32Index < u32Cnt ; u32Index++)
            {

                if (u32Index>= OP_TEE_XC_REG_MAX_CNT)
                {
                    printf("array size overflow for ipmux part");
                    break;
                }
                MDrv_Write2ByteMask(pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_data[u32Index].bk
                                  , pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_data[u32Index].value
                                  , pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_data[u32Index].mask);
            }
            //************************END PRINT DATA*******************************//
            break;
        case E_XC_OPTEE_GET_MUX:
            break;
        default:
            break;
    }
    return TRUE;
}
//CID:238768, Big parameter passed by value for xc_mux_data change to use pointer @Crixus 20180517
MS_BOOL MApi_XC_OPTEE_Mux_U2(void *pInstance,EN_XC_OPTEE_ACTION action,XC_OPTEE_MUX_DATA xc_mux_data)
{
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    MDrv_XC_OPTEE_Mux(pInstance,action, &xc_mux_data);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return TRUE;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
//CID:238767, Big parameter passed by value for xc_mux_data change to use pointer @Crixus 20180517
MS_BOOL MApi_XC_OPTEE_Mux(EN_XC_OPTEE_ACTION action,XC_OPTEE_MUX_DATA xc_mux_data)
{
    CheckXCInstanceOpen();

#if PIP_PATCH_USING_SC1_MAIN_AS_SC0_SUB
     if (pu32XCInst_1 == NULL)
     {
        XC_INSTANT_ATTRIBUTE stXCInstantAttribute;
        INIT_XC_INSTANT_ATTR(stXCInstantAttribute);

        stXCInstantAttribute.u32DeviceID = 1;
        if(UtopiaOpen(MODULE_XC, &pu32XCInst_1, 0, &stXCInstantAttribute) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return E_APIXC_RET_FAIL;
        }
      }
#endif
    stXC_OPTEE_MUX XCArgs;
    XCArgs.action = action;
    XCArgs.xc_mux_data = xc_mux_data;
    XCArgs.bReturn = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_OPTEE_MUX, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturn;
    }
}
#endif
#ifdef MSOS_TYPE_OPTEE
static EN_XC_OPTEE_TIMER_STATUS status=E_XC_OPTEE_TIMER_TEE_DISABLE;
static MS_U8 u8OpteeErrorCnt = 0;
#endif
static MS_BOOL _Mdrv_XC_OPTEE_CheckRBase_Handler(void * pParam,SCALER_WIN eWindow)
{
#if defined(MSOS_TYPE_OPTEE) && defined(SUPPORT_READLIMIT_1BIT_NS_SW)
    MS_BOOL bRBaseLegal = TRUE;
    void *pInstance = pu32XCInst_private;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    XC_RESOURCE_PRIVATE_FOR_TEE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    if(!pXCResourcePrivate->sthal_Optee.op_tee_xc[MAIN_WINDOW].isEnable)
    {
        status = E_XC_OPTEE_TIMER_TEE_DISABLE;
        u8OpteeErrorCnt = 0;
    }

    switch (status)
    {
        case E_XC_OPTEE_TIMER_TEE_DISABLE:
            if(pXCResourcePrivate->sthal_Optee.op_tee_xc[MAIN_WINDOW].isEnable)
            {
                status = E_XC_OPTEE_TIMER_TEE_ENABLE;
            }
            break;
        case E_XC_OPTEE_TIMER_TEE_ENABLE:
            if(Hal_SC_secure_Check_RBase(pInstance) == FALSE)
            {
                u8OpteeErrorCnt++;
                status = E_XC_OPTEE_TIMER_TEE_RBASE_ABNORMAL;
            }
            break;
        case E_XC_OPTEE_TIMER_TEE_RBASE_ABNORMAL:
            if(Hal_SC_secure_Check_RBase(pInstance) == FALSE)
            {
                u8OpteeErrorCnt++;
                if(u8OpteeErrorCnt > MAX_OPTEE_ERROR_CNT)
                {
                    status = E_XC_OPTEE_TIMER_TEE_CRASH;
                }
            }
            else
            {
                status = E_XC_OPTEE_TIMER_TEE_ENABLE;
                u8OpteeErrorCnt = 0;
            }
            break;
        case E_XC_OPTEE_TIMER_TEE_CRASH:
            bRBaseLegal = FALSE;
            break;
        default:
            break;
    }
    return bRBaseLegal;
#else
#ifdef MSOS_TYPE_OPTEE
    UNUSED(status);
    UNUSED(u8OpteeErrorCnt);
#endif
    return TRUE;
#endif
}

MS_BOOL MApi_XC_OPTEE_CheckRBase_U2(void* pInstance,SCALER_WIN eWindow)
{
    return _Mdrv_XC_OPTEE_CheckRBase_Handler(pInstance,eWindow);
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_OPTEE_CheckRBase(SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_OPTEE_CheckRBase XCArgs;
    XCArgs.eWindow = eWindow;
    XCArgs.bReturn = FALSE;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_OPTEE_CHECKRBASE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bReturn;
    }
}
#endif
#ifdef MSOS_TYPE_OPTEE
static MS_U32 _MDrv_XC_PipID_Trans(MS_U32 u32PipeID)
{
    if(u32PipeID == pipeID_SC0_main)
        return MAIN_WINDOW;
    else if (u32PipeID == pipeID_SC0_sub)
        return SUB_WINDOW;
    else if (u32PipeID == pipeID_SC1_main)
        return MAIN_WINDOW;
    else
        return 0xFFFF;
}
#endif
MS_U32 MApi_XC_GetResourceByPipeID_U2(void* pInstance,MS_U32 u32PipeID, MS_U32*  U32RetResNum, RESOURCE* pResource)
{
    /// pResource: return scaler memory information
    /// U32RetResNum: check resource number max
#ifdef MSOS_TYPE_OPTEE
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE_FOR_TEE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    MS_U32 eWindow = _MDrv_XC_PipID_Trans(u32PipeID);
    MS_U32 u32TmpRetResNum = 0;
    //id[3]: 0, id[2]=address_h, id[1]=address_l, id[0]=length
    // 0: SCM Dram buffer
    pResource[0].ID[0] = pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].drams_data[0].length;
    pResource[0].ID[1] = pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].drams_data[0].address & 0xFFFFFFFF;
    pResource[0].ID[2] = 0;
    pResource[0].ID[3] = 0;
    u32TmpRetResNum++;
    #if (HW_DESIGN_4K2K_VER == 4)
    //id[3]: 0, id[2]=address_h, id[1]=address_l, id[0]=length
    // 1: FRCM Dram buffer
    pResource[1].ID[0] = pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].drams_data[1].length;
    pResource[1].ID[1] = pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].drams_data[1].address & 0xFFFFFFFF;
    pResource[1].ID[2] = 0;
    pResource[1].ID[3] = 0;
    u32TmpRetResNum++;
    #else
    UNUSED(eWindow);
    #endif
    *U32RetResNum = u32TmpRetResNum;
    if(pResource[0].ID[0]==0 ) //If FBL ,DO not lock
        *U32RetResNum=0;
#endif
    return UTOPIA_STATUS_SUCCESS;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_U32 MApi_XC_GetResourceByPipeID(MS_U32 u32PipeID, MS_U32*  U32RetResNum, RESOURCE* pResource)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_GetResourceByPipeID XCArgs;
    XCArgs.u32PipeID = u32PipeID;
    XCArgs.U32RetResNum = U32RetResNum;
    XCArgs.pResource = pResource;
    XCArgs.u32ReturnValue = UTOPIA_STATUS_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_RES_BY_PIPE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return UTOPIA_STATUS_FAIL;
    }
    else
    {
        return XCArgs.u32ReturnValue;
    }

}
#endif
MS_U32 MApi_XC_ConfigPipe_U2(void* pInstance,MS_U32 u32PipeID, MS_U32 u32SecureDMA, MS_U32 u32OperationMode)
{
#ifdef MSOS_TYPE_OPTEE
    /// u32SecureDMA: memory lock
    /// u32OperationMode: RIU register lock
    MS_U32 u32Index = 0;
    MS_U32 u32Cnt=0;
    MS_U32 eWindow = _MDrv_XC_PipID_Trans(u32PipeID);
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE_FOR_TEE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    if(Hal_SC_secure_lock(pInstance, eWindow, u32SecureDMA, u32OperationMode) == FALSE)
    {
        return UTOPIA_STATUS_FAIL;
    }
    u32Cnt = pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].regs_cnt;

    for(u32Index = 0 ; u32Index < u32Cnt ; u32Index++)
    {
        if (u32Index>= OP_TEE_XC_REG_MAX_CNT)
        {
            printf("array size overflow for xc part");
            return UTOPIA_STATUS_FAIL;
        }
        MDrv_Write2ByteMask(pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].regs_data[u32Index].bk
                          , pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].regs_data[u32Index].value
                          , pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].regs_data[u32Index].mask);
    }
    u32Cnt = pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_cnt;
    for(u32Index = 0 ; u32Index < u32Cnt ; u32Index++)
    {
        if (u32Index>= OP_TEE_XC_REG_MAX_CNT)
        {
            printf("array size overflow for ipmux part");
            return UTOPIA_STATUS_FAIL;
        }
        MDrv_Write2ByteMask(pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_data[u32Index].bk
                          , pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_data[u32Index].value
                          , pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_data[u32Index].mask);
    }

#endif
    return UTOPIA_STATUS_SUCCESS;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_U32 MApi_XC_ConfigPipe(MS_U32 u32PipeID, MS_U32 u32SecureDMA, MS_U32 u32OperationMode)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_ConfigPipe XCArgs;
    XCArgs.u32PipeID = u32PipeID;
    XCArgs.u32SecureDMA = u32SecureDMA;
    XCArgs.u32OperationMode = u32OperationMode;
    XCArgs.u32ReturnValue = UTOPIA_STATUS_FAIL;
    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_CONFIG_PIPE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return UTOPIA_STATUS_FAIL;
    }
    else
    {
        return XCArgs.u32ReturnValue;
    }
}
#endif
MS_U32 MApi_XC_CheckPipe_U2(void* pInstance,MS_U32 u32PipeID, MS_U32 u32SecureDMA, MS_U32 u32OperationMode)
{
#ifdef MSOS_TYPE_OPTEE
    /// check PipeID is correct or not
    /// u32SecureDMA: memory lock
    /// u32OperationMode: RIU register lock
    ///MS_U32 u32Index = 0;
    ///MS_U32 u32Cnt=0;
    MS_U32 eWindow = _MDrv_XC_PipID_Trans(u32PipeID);
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);

    XC_RESOURCE_PRIVATE_FOR_TEE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    if(pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].isEnable == FALSE)
    {
        // if there is not enable return fail
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        return UTOPIA_STATUS_FAIL;
    }
#if 0
    u32Cnt = pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].regs_cnt;
    //How to get data correctly
    for(u32Index = 0 ; u32Index < u32Cnt ; u32Cnt++)
    {
        if(pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].regs_data[u32Cnt].value != (MDrv_Read2Byte( pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].regs_data[u32Cnt].bk)| pXCResourcePrivate->sthal_Optee.op_tee_xc[eWindow].regs_data[u32Cnt].mask ))
        {
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            return UTOPIA_STATUS_FAIL;
        }
    }
    u32Cnt = pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_cnt;
    for(u32Index = 0 ; u32Index < u32Cnt ; u32Cnt++)
    {
        if( pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_data[u32Cnt].value != (MDrv_Read2Byte( pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_data[u32Cnt].bk )| pXCResourcePrivate->sthal_Optee.op_tee_mux.regs_data[u32Cnt].mask ))
        {
            _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
            return UTOPIA_STATUS_FAIL;
        }
    }
#endif
#endif
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return UTOPIA_STATUS_SUCCESS; // success
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_U32 MApi_XC_CheckPipe(MS_U32 u32PipeID, MS_U32 u32SecureDMA, MS_U32 u32OperationMode)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_CheckPipe XCArgs;
    XCArgs.u32PipeID = u32PipeID;
    XCArgs.u32SecureDMA = u32SecureDMA;
    XCArgs.u32OperationMode = u32OperationMode;
    XCArgs.u32ReturnValue = UTOPIA_STATUS_FAIL;
    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_CHECK_PIPE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return UTOPIA_STATUS_FAIL;
    }
    else
    {
        return XCArgs.u32ReturnValue;
    }
}
#endif
MS_BOOL MApi_XC_SetSWDRInfo_U2(void* pInstance, XC_SWDR_INFO *pSWDR_INFO)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);

    MDrv_XC_SetSWDRInfo(pInstance,pSWDR_INFO);

    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return TRUE;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_SetSWDRInfo(XC_SWDR_INFO *pSWDR_INFO)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SWDR_INFO XCArgs;
    XCArgs.pSWDR_INFO = pSWDR_INFO;
    XCArgs.bReturnValue = UTOPIA_STATUS_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_SWDR_INFO, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return UTOPIA_STATUS_FAIL;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif
MS_BOOL MApi_XC_GetSWDRInfo_U2(void* pInstance, XC_SWDR_INFO *pSWDR_INFO)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);

    MDrv_XC_GetSWDRInfo(pInstance,pSWDR_INFO);

    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return TRUE;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_BOOL MApi_XC_GetSWDRInfo(XC_SWDR_INFO *pSWDR_INFO)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SWDR_INFO XCArgs;
    XCArgs.pSWDR_INFO = pSWDR_INFO;
    XCArgs.bReturnValue = UTOPIA_STATUS_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_SWDR_INFO, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return UTOPIA_STATUS_FAIL;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#endif
MS_BOOL MApi_XC_SetSWDRInputData_U2(void* pInstance, XC_SWDR_INPUT* pSWDRInput, SCALER_WIN eWindow)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    MDrv_XC_SetSWDRInput(pInstance,pSWDRInput,eWindow);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return TRUE;
}
MS_BOOL MApi_XC_SetSWDRInputData(XC_SWDR_INPUT* pSWDRInput, SCALER_WIN eWindow)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }
    stXC_SWDR_INPUTDATA XCArgs;
    XCArgs.pSWDRInput = pSWDRInput;
    XCArgs.eWindow = eWindow;
    XCArgs.bReturnValue = UTOPIA_STATUS_FAIL;
    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_SWDR_INPUTDATA, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return UTOPIA_STATUS_FAIL;
    }
    else
    {
        return XCArgs.bReturnValue;
    }
}
#ifdef UFO_XC_SUPPORT_HDMI_DOWNSCALE_OUTPUT_POLICY
MS_U32 MApi_XC_GetHDMIPolicy_U2(void* pInstance)
{
    MS_U32 u32HDMIPolicy = 0;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    _XC_ENTRY(pInstance);
    u32HDMIPolicy = MDrv_SC_GetHDMIPolicy(pInstance);
    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return u32HDMIPolicy;
}
#ifndef UTOPIAXP_REMOVE_WRAPPER
MS_U32 MApi_XC_GetHDMIPolicy(void)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_GET_HDMIPOLICY XCArgs;
    XCArgs.u32HDMIPolicyInfo = 0;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_HDMIPOLICY, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return UTOPIA_STATUS_FAIL;
    }
    else
    {
        return XCArgs.u32HDMIPolicyInfo;
    }
}
#endif
#endif

void MApi_XC_SetPQBinPath_U2(void* pInstance,E_XC_PQ_BIN_PATH eXCPqBinPath,MS_U8 u8size,char* b_XCPQBinFilePath)
{

#if (defined(MSOS_TYPE_LINUX_KERNEL) && defined(CONFIG_MSTAR_XC_SET_PQBINPATH))
    return KApi_XC_SetPQBinPath_Ext((EN_KDRV_XC_PQ_BIN_PATH)eXCPqBinPath,u8size,b_XCPQBinFilePath);
#else
#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    int fd = -1;
    fd = open("/dev/scaler", O_WRONLY | O_NDELAY);
    if (-1 == fd)
    {
        printf("\nUnable to open /dev/scaler\n");
        //return E_APIXC_RET_FAIL;
    }
    else
    {
        ST_KDRV_XC_SET_PQBINPATH k_set_pqbinpath;
        k_set_pqbinpath.enXCPqBinPath= (EN_KDRV_XC_PQ_BIN_PATH)eXCPqBinPath;
        k_set_pqbinpath.u8size= u8size;
        k_set_pqbinpath.b_PQBinFilePath= b_XCPQBinFilePath;
        if (-1 == ioctl(fd, MDRV_XC_IOC_SET_PQBIN_PATH,&k_set_pqbinpath))
        {
            perror("ioctl");
        }
        close(fd); //CID:307272, Double close for close(fd); @Crixus 20180516
    }
#endif
#endif
}

MS_BOOL MApi_XC_SetPQBinPath(E_XC_PQ_BIN_PATH eXCPqBinPath,MS_U8 u8size,char* b_XCPQBinFilePath)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return UTOPIA_STATUS_FAIL;
    }

    stXC_SET_PQBINPATH XCArgs;
    XCArgs.eXCPqBinPath = eXCPqBinPath;
    XCArgs.u8size = u8size;
    XCArgs.b_PQBinFilePath = b_XCPQBinFilePath;
    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_PQBIN_PATH, (void*)&XCArgs) != 0)
    {
        printf("Obtain PQ engine fail\n");
        return UTOPIA_STATUS_FAIL;
    }
    return UTOPIA_STATUS_SUCCESS;
}

E_APIXC_ReturnValue MDrv_XC_GetDNRBufferSize(void* pInstance, SCALER_WIN eWindow, MS_U32 *pu32BufferSize)
{
    *pu32BufferSize = MHal_XC_GetDNRBufSize(pInstance, eWindow);
    return E_APIXC_RET_OK;
}

E_APIXC_ReturnValue MApi_XC_GetDNRBufferSize_U2(void* pInstance, SCALER_WIN eWindow, MS_U32 *pu32BufferSize)
{
    E_APIXC_ReturnValue eRet = E_APIXC_RET_FAIL;
    eRet = MDrv_XC_GetDNRBufferSize(pInstance, eWindow, pu32BufferSize);
    return eRet;
}

E_APIXC_ReturnValue MApi_XC_GetDNRBufferSize(SCALER_WIN eWindow, MS_U32 *pu32BufferSize)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_GETDNRBUFFERSIZE XCArgs;
    XCArgs.eWindow = eWindow;
    XCArgs.pu32BufferSize = pu32BufferSize;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;
    if(UtopiaIoctl(pu32XCInst, E_XC_GET_DNRBUFFERSIZE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}

E_APIXC_ReturnValue MApi_XC_SetFrameInfo_U2(void* pInstance, ST_XC_FRAME_INFO* pFrameInfo, SCALER_WIN eWindow)
{
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    if((NULL == pFrameInfo) || (pFrameInfo->u32Length == 0) || (pFrameInfo->u32Version < 1))
    {
        //We cosider compatible operation form version 1 , so reject the info init when version invalid
        XC_LOG_TRACE(XC_DBGLEVEL_SETTIMING, "MApi_XC_SetFrameInfo: Null paramter or Wrong u32Length or Wrong u32Version!!\n");

        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return E_APIXC_RET_FAIL_INVALID_PARAMETER;
    }

    if((pFrameInfo->u32Version > FRAME_INFO_STRUCTURE_VERSION) || (pFrameInfo->u32Length > sizeof(ST_XC_FRAME_INFO)))
    {
#if defined (__aarch64__)
        XC_LOG_TRACE(XC_DBGLEVEL_SETTIMING, "MApi_XC_SetFrameInfo: this old version XC(ver:%d) lib has only length:%lu driver status!!\n",
                     FRAME_INFO_STRUCTURE_VERSION, sizeof(ST_XC_FRAME_INFO));
#else
        XC_LOG_TRACE(XC_DBGLEVEL_SETTIMING, "MApi_XC_SetFrameInfo: this old version XC(ver:%d) lib has only length:%u driver status!!\n",
                     FRAME_INFO_STRUCTURE_VERSION, sizeof(ST_XC_FRAME_INFO));
#endif

        pFrameInfo->u32Version = FRAME_INFO_STRUCTURE_VERSION;
        pFrameInfo->u32Length = sizeof(ST_XC_FRAME_INFO);
    }

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    E_APIXC_ReturnValue eRet = E_APIXC_RET_FAIL;
    eRet = MDrv_XC_SetFrameInfo(pInstance,pFrameInfo,eWindow);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return eRet;
}

#ifdef MSOS_TYPE_LINUX_KERNEL
E_APIXC_ReturnValue MApi_XC_SetFrameInfo(ST_XC_FRAME_INFO* pFrameInfo, SCALER_WIN eWindow)
#else
__attribute__((weak)) E_APIXC_ReturnValue MApi_XC_SetFrameInfo(ST_XC_FRAME_INFO* pFrameInfo, SCALER_WIN eWindow)
#endif
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }
    stXC_FRAME_INFO XCArgs;
    XCArgs.pstFrameInfo = pFrameInfo;
    XCArgs.eWindow = eWindow;
    XCArgs.bReturnValue = FALSE;
    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_FRAME_INFO, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return (E_APIXC_ReturnValue)XCArgs.bReturnValue;
    }
}

#if defined(SUPPORT_ABF) && (ABF_VERSION == 1)
E_APIXC_ReturnValue MDrv_XC_ABF_Control(void* pInstance, EN_XC_ABF_CTRL_TYPE enCtrlType, void *pParam)
{
    if(enCtrlType >= E_XC_ABF_CTRL_MAX)
    {
        XC_LOG_TRACE(XC_DBGLEVEL_PARAMETER_TRACE, "ABF ctrl fail.\n");
        return E_APIXC_RET_FAIL;
    }

    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    switch(enCtrlType)
    {
        case E_XC_ABF_CTRL_ENABLE:
        {
            MS_BOOL bEnable = *(MS_BOOL *)pParam;

            MHal_XC_ABF_Enable(pInstance,bEnable,MAIN_WINDOW);

            break;
        }
        default:
            break;
    }

    return E_APIXC_RET_OK;
}
#endif

#ifdef MSOS_TYPE_LINUX_KERNEL
extern MS_BOOL KDrv_XC_PQ_SetGamma(ST_KDRV_XC_PQ_GAMMA_IN *pstPqGammaIn);
#endif
E_APIXC_ReturnValue MDrv_XC_PQ_Gamma(ST_XC_PQ_GAMMA_IN *pstGammaIn)
{
    static ST_KDRV_XC_PQ_GAMMA_IN _stKdrvPqGammaIn;
    memset(&_stKdrvPqGammaIn, 0, sizeof(ST_KDRV_XC_PQ_GAMMA_IN));

    if(pstGammaIn == NULL)
    {
        printf("Warning : pstGammaIn is Empty!");
        return E_APIXC_RET_FAIL;
    }

    _stKdrvPqGammaIn.u32Version = pstGammaIn->u32Version;
    _stKdrvPqGammaIn.u32Length = pstGammaIn->u32Length;
    _stKdrvPqGammaIn.bInit = pstGammaIn->bInit;
    _stKdrvPqGammaIn.enMainSubSel = (EN_KDRV_XC_PQ_VIDEO_HW_CTRL_SEL) pstGammaIn->enMainSubSel;
    memcpy(_stKdrvPqGammaIn.astGainOff1, pstGammaIn->astGainOff1, sizeof(ST_XC_PQ_GAIN_OFFSET)*PQ_GAMMA_CHANNEL);
    memcpy(_stKdrvPqGammaIn.astGainOff2, pstGammaIn->astGainOff2, sizeof(ST_XC_PQ_GAIN_OFFSET)*PQ_GAMMA_CHANNEL);
    _stKdrvPqGammaIn.bOne_Drv3 = pstGammaIn->bOne_Drv3;
    _stKdrvPqGammaIn.bEnable = pstGammaIn->bEnable;
    _stKdrvPqGammaIn.au32_u20p12MaxBase[0] = pstGammaIn->au32_u20p12MaxBase[0];
    _stKdrvPqGammaIn.au32_u20p12MaxBase[1] = pstGammaIn->au32_u20p12MaxBase[1];
    _stKdrvPqGammaIn.au32_u20p12MaxBase[2] = pstGammaIn->au32_u20p12MaxBase[2];
    memcpy(_stKdrvPqGammaIn.au32_u20p12Lut, pstGammaIn->au32_u20p12Lut, sizeof(MS_U32)*PQ_GAMMA_LUT_ENTRY*PQ_GAMMA_CHANNEL);

#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    if(0 > _s32FdScaler)
    {
        _s32FdScaler = open("/dev/scaler", O_RDWR);

        if(0 > _s32FdScaler)
        {
            printf("Scaler device not opened!!!!\n");
            return E_APIXC_RET_FAIL;
        }
    }
#endif

#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    if (ioctl(_s32FdScaler, MDRV_XC_IOC_PQ_GAMMA, &_stKdrvPqGammaIn))
    {
        printf("MDRV_XC_IOC_PQ_GAMMA fail!!!!\n");
        return E_APIXC_RET_FAIL;
    }
#endif

#if defined(MSOS_TYPE_LINUX_KERNEL)
    if (KDrv_XC_PQ_SetGamma(&_stKdrvPqGammaIn) != TRUE)
    {
        return E_APIXC_RET_FAIL;
    }
#endif

    return E_APIXC_RET_OK;
}

E_APIXC_ReturnValue MApi_XC_PQ_Gamma_U2(void * pInstance, ST_XC_PQ_GAMMA_IN *pstGammaIn)
{
    E_APIXC_ReturnValue bReturn = E_APIXC_RET_FAIL;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    MS_U32 u32CopiedLength = sizeof(ST_XC_PQ_GAMMA_IN);
    ST_XC_PQ_GAMMA_IN *pstGammaInTemp = NULL;
    VERSION_COMPATIBLE_CHECK(pstGammaIn, ST_XC_PQ_GAMMA_IN, u32CopiedLength, u32Version, u32Length, ST_XC_PQ_GAMMA_IN_VERSION, E_APIXC_RET_FAIL);
    MS_BOOL bRet = TRUE;
    XC_MALLOC_MEM(pstGammaInTemp, u32CopiedLength, bRet);

    if(bRet == FALSE)
    {
        return bReturn;
    }
    if(pstGammaInTemp != NULL && pstGammaIn != NULL)
    {
        pstGammaIn->u32Version = ST_XC_PQ_GAMMA_IN_VERSION; /*Return the actual version of the returned value for APP to use*/
        pstGammaIn->u32Length = u32CopiedLength; /*Return the actual length of the returned value for APP to know whick info is valid*/
        memcpy(pstGammaInTemp, pstGammaIn, u32CopiedLength);
        bReturn = MDrv_XC_PQ_Gamma(pstGammaInTemp);
    }

    XC_FREE_MEM(pstGammaInTemp, u32CopiedLength);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return bReturn;
}

E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_PQ_Gamma(ST_XC_PQ_GAMMA_IN *pstGammaIn)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_PQ_GAMMA_IN XCArgs;
    XCArgs.pstGammaIn = pstGammaIn;
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;

    if (UtopiaIoctl(pu32XCInst, E_XC_CMD_PQ_GAMMA, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.enReturnValue;
    }
}

#ifdef MSOS_TYPE_LINUX_KERNEL
extern MS_BOOL KDrv_XC_PQ_SetRGBPattern(ST_KDRV_XC_PQ_RGB_PATTERN_IN *pstRGBPatternIn);
#endif
E_APIXC_ReturnValue MDrv_XC_PQ_RGBPattern(ST_XC_PQ_RGB_PATTERN_IN *pstRGBPatternIn)
{
    ST_KDRV_XC_PQ_RGB_PATTERN_IN stKdrvRGBPatternIn;
    memset(&stKdrvRGBPatternIn, 0, sizeof(ST_KDRV_XC_PQ_RGB_PATTERN_IN));

    if(pstRGBPatternIn == NULL)
    {
        printf("Warning : pstRGBPatternIn is Empty!");
        return E_APIXC_RET_FAIL;
    }

    stKdrvRGBPatternIn.u32Version = pstRGBPatternIn->u32Version;
    stKdrvRGBPatternIn.u32Length = pstRGBPatternIn->u32Length;
    stKdrvRGBPatternIn.bEnable = pstRGBPatternIn->bEnable;
    stKdrvRGBPatternIn.u16_u0p16R = pstRGBPatternIn->u16_u0p16R;
    stKdrvRGBPatternIn.u16_u0p16G = pstRGBPatternIn->u16_u0p16G;
    stKdrvRGBPatternIn.u16_u0p16B = pstRGBPatternIn->u16_u0p16B;

#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    if(0 > _s32FdScaler)
    {
        _s32FdScaler = open("/dev/scaler", O_RDWR);

        if(0 > _s32FdScaler)
        {
            printf("Scaler device not opened!!!!\n");
            return E_APIXC_RET_FAIL;
        }
    }
#endif

#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    if (ioctl(_s32FdScaler, MDRV_XC_IOC_PQ_RGB_PATTERN, &stKdrvRGBPatternIn))
    {
        printf("MDRV_XC_IOC_PQ_RGB_PATTERN fail!!!!\n");
        return E_APIXC_RET_FAIL;
    }
#endif

#ifdef MSOS_TYPE_LINUX_KERNEL
    if (KDrv_XC_PQ_SetRGBPattern(&stKdrvRGBPatternIn) != TRUE)
    {
        return E_APIXC_RET_FAIL;
    }
#endif

    return E_APIXC_RET_OK;
}

E_APIXC_ReturnValue MApi_XC_PQ_RGBPattern_U2(void * pInstance, ST_XC_PQ_RGB_PATTERN_IN *pstRGBPatternIn)
{
    E_APIXC_ReturnValue bReturn = E_APIXC_RET_FAIL;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    ST_XC_PQ_RGB_PATTERN_IN stRGBPatternIn;
    MS_U32 u32CopiedLength = sizeof(ST_XC_PQ_RGB_PATTERN_IN);
    memset(&stRGBPatternIn, 0, sizeof(ST_XC_PQ_RGB_PATTERN_IN));

    VERSION_COMPATIBLE_CHECK(pstRGBPatternIn, stRGBPatternIn, u32CopiedLength, u32Version, u32Length, ST_XC_PQ_RGB_PATTERN_IN_VERSION, E_APIXC_RET_FAIL);
    pstRGBPatternIn->u32Version = ST_XC_PQ_RGB_PATTERN_IN_VERSION; /*Return the actual version of the returned value for APP to use*/
    pstRGBPatternIn->u32Length = u32CopiedLength; /*Return the actual length of the returned value for APP to know whick info is valid*/

    memcpy(&stRGBPatternIn, pstRGBPatternIn, u32CopiedLength);

    bReturn = MDrv_XC_PQ_RGBPattern(&stRGBPatternIn);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return bReturn;
}

E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_PQ_RGBPattern(ST_XC_PQ_RGB_PATTERN_IN *pstRGBPatternIn)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_PQ_RGB_PATTERN_IN XCArgs;
    XCArgs.pstRGBPatternIn = pstRGBPatternIn;
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;

    if (UtopiaIoctl(pu32XCInst, E_XC_CMD_PQ_RGB_PATTERN_IN, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.enReturnValue;
    }

}

#ifdef MSOS_TYPE_LINUX_KERNEL
extern MS_BOOL KDrv_XC_PQ_SetBlueStretchIn(ST_KDRV_XC_PQ_BLUE_STRETCH_IN *pstBlueStretchIn);
#endif
E_APIXC_ReturnValue MDrv_XC_PQ_BlueStretchIn(ST_XC_PQ_BLUE_STRETCH_IN *pstBlueStretchIn)
{
    ST_KDRV_XC_PQ_BLUE_STRETCH_IN stKdrvPqBlueStretchIn;
    memset(&stKdrvPqBlueStretchIn, 0, sizeof(ST_KDRV_XC_PQ_BLUE_STRETCH_IN));

    if(pstBlueStretchIn == NULL)
    {
        printf("Warning : pstBlueStretchIn is Empty!");
        return E_APIXC_RET_FAIL;
    }

    stKdrvPqBlueStretchIn.u32Version = pstBlueStretchIn->u32Version;
    stKdrvPqBlueStretchIn.u32Length = pstBlueStretchIn->u32Length;
    stKdrvPqBlueStretchIn.u8Strength = pstBlueStretchIn->u8Strength;
    stKdrvPqBlueStretchIn.u8SatLevel = pstBlueStretchIn->u8SatLevel;
    stKdrvPqBlueStretchIn.bEn = pstBlueStretchIn->bEn;
#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    if(0 > _s32FdScaler)
    {
        _s32FdScaler = open("/dev/scaler", O_RDWR);

        if(0 > _s32FdScaler)
        {
            printf("Scaler device not opened!!!!\n");
            return E_APIXC_RET_FAIL;
        }
    }
#endif

#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    if (ioctl(_s32FdScaler, MDRV_XC_IOC_PQ_BLUE_STRETCH, &stKdrvPqBlueStretchIn))
    {
        printf("MDRV_XC_IOC_PQ_BLUE_STRETCH fail!!!!\n");
        return E_APIXC_RET_FAIL;
    }
#endif

#ifdef MSOS_TYPE_LINUX_KERNEL
    if (KDrv_XC_PQ_SetBlueStretchIn(&stKdrvPqBlueStretchIn) != TRUE)
    {
        return E_APIXC_RET_FAIL;
    }
#endif

    return E_APIXC_RET_OK;
}

MS_U32 MDrv_SC_GetIPCLK(void *pInstance, SCALER_WIN eWindow)
{
    return MHal_SC_GetIPCLK(pInstance, eWindow);
}

MS_U32 MDrv_SC_GetOPCLK(void *pInstance, SCALER_WIN eWindow)
{
    return MHal_SC_GetOPCLK(pInstance, eWindow);
}

MS_BOOL MDrv_SC_IsDistractHBlanking(void * pInstance, SCALER_WIN eWindow)
{
    return MHal_SC_IsDistractHBlanking(pInstance, eWindow);
}

E_APIXC_ReturnValue MApi_XC_PQ_BlueStretchIn_U2(void * pInstance, ST_XC_PQ_BLUE_STRETCH_IN *pstBlueStretchIn)
{
    E_APIXC_ReturnValue bReturn = E_APIXC_RET_FAIL;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    ST_XC_PQ_BLUE_STRETCH_IN stBlueStretchIn;
    MS_U32 u32CopiedLength = sizeof(ST_XC_PQ_BLUE_STRETCH_IN);
    memset(&stBlueStretchIn, 0, sizeof(ST_XC_PQ_BLUE_STRETCH_IN));

    VERSION_COMPATIBLE_CHECK(pstBlueStretchIn, stBlueStretchIn, u32CopiedLength, u32Version, u32Length, ST_XC_PQ_BLUE_STRETCH_IN_VERSION, E_APIXC_RET_FAIL);
    pstBlueStretchIn->u32Version = ST_XC_PQ_BLUE_STRETCH_IN_VERSION; /*Return the actual version of the returned value for APP to use*/
    pstBlueStretchIn->u32Length = u32CopiedLength; /*Return the actual length of the returned value for APP to know whick info is valid*/

    memcpy(&stBlueStretchIn, pstBlueStretchIn, u32CopiedLength);

    bReturn = MDrv_XC_PQ_BlueStretchIn(&stBlueStretchIn);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return bReturn;
}

E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_PQ_BlueStretchIn(ST_XC_PQ_BLUE_STRETCH_IN *pstBlueStretchIn)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_PQ_BLUE_STRETCH_IN XCArgs;
    XCArgs.pstBlueStretchIn = pstBlueStretchIn;
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;

    if (UtopiaIoctl(pu32XCInst, E_XC_CMD_PQ_BLUE_STRETCH_IN, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.enReturnValue;
    }

}

MS_U16 MApi_XC_GetVideoCaptureInfo_U2(void* pInstance, ST_XC_VIDEO_CAPTURE_INFO *pDrvStatus, SCALER_WIN eWindow)
{
    ST_XC_VIDEO_CAPTURE_INFO DrvStatus;
    MS_U16 u16CopiedLength = sizeof(ST_XC_VIDEO_CAPTURE_INFO);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    if((NULL == pDrvStatus) || (pDrvStatus->u16ApiStatusEX_Length == 0))
    {
        //We cosider compatible operation form version2 , so reject the info init when version invalid
        XC_LOG_TRACE(XC_DBGLEVEL_SETTIMING, "MApi_XC_GetVideoCaptureInfo_U2: Null paramter or Wrong u16ApiStatusEX_Length!!\n")

        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
        return 0;
    }

    // the version control is coming in with version 1
    if(pDrvStatus->u32ApiStatusEx_Version < 1)
    {
        //We cosider compatible operation form version1 , so reject the info init when version invalid
        XC_LOG_TRACE(XC_DBGLEVEL_SETTIMING, "MApi_XC_GetVideoCaptureInfo_U2: please check your u32ApiStatusEx_Version, it should not set to 0!!\n")
        XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

        return 0;
    }
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;

    memset(&DrvStatus, 0, sizeof(ST_XC_VIDEO_CAPTURE_INFO));
    VERSION_COMPATIBLE_CHECK(pDrvStatus, DrvStatus, u16CopiedLength, u32ApiStatusEx_Version, u16ApiStatusEX_Length, ST_XC_VIDEO_CAPTURE_INFO_VERSION, 0);

    DrvStatus.u32ApiStatusEx_Version = ST_XC_VIDEO_CAPTURE_INFO_VERSION; //Return the actual version of the returned value for APP to use
    DrvStatus.u16ApiStatusEX_Length = u16CopiedLength; //Return the actual length of the returned value for APP to know whick info is valid

    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
    _XC_ENTRY(pInstance);
    //----------------------
    // Customer setting
    //----------------------
    DrvStatus.enInputSourceType = gSrcInfo[eWindow].enInputSourceType;

    //----------------------
    // Window
    //----------------------
    memcpy(&DrvStatus.stCapWin, &gSrcInfo[eWindow].stCapWin, sizeof(MS_WINDOW_TYPE));
    //Assign display window for AP layer
    memcpy(&DrvStatus.stDispWin, &gSrcInfo[eWindow].stDispWin, sizeof(MS_WINDOW_TYPE));

    if(DrvStatus.stDispWin.x >= gSrcInfo[eWindow].Status2.stXCPanelDefaultTiming.u16HStart)
    {
        DrvStatus.stDispWin.x -= gSrcInfo[eWindow].Status2.stXCPanelDefaultTiming.u16HStart;
    }
    if(DrvStatus.stDispWin.y >= gSrcInfo[eWindow].Status2.stXCPanelDefaultTiming.u16VStart)
    {
        DrvStatus.stDispWin.y -= gSrcInfo[eWindow].Status2.stXCPanelDefaultTiming.u16VStart;
    }

    if(MDrv_XC_Is2K2KToFrc(pInstance))
    {
        DrvStatus.stDispWin.x = DrvStatus.stDispWin.x * 2;
        DrvStatus.stDispWin.width= DrvStatus.stDispWin.width * 2;
    }

    DrvStatus.bIsIP2PMode = SC_R2BYTEMSK(psXCInstPri->u32DeviceID, REG_SC_BK02_0A_L, BIT(15));
    #if XC_SUPPORT_4K2K // 4K2K chip always output 2P
        DrvStatus.bIsOP2PMode = TRUE;
    #else
        DrvStatus.bIsOP2PMode = FALSE;
    #endif

    DrvStatus.u32IPCLK = MDrv_SC_GetIPCLK(pInstance, eWindow);
    DrvStatus.u32OPCLK = MDrv_SC_GetOPCLK(pInstance, eWindow);
    DrvStatus.bDistractHBlanking = MDrv_SC_IsDistractHBlanking(pInstance, eWindow);

    _XC_RETURN(pInstance);
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    //shorter Ap structure with longer lib structure, we should handle it to avoid access-violation.
    memcpy(pDrvStatus, &DrvStatus, u16CopiedLength);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return u16CopiedLength;
}

#ifdef MSOS_TYPE_LINUX_KERNEL
MS_U16 MApi_XC_GetVideoCaptureInfo(ST_XC_VIDEO_CAPTURE_INFO *pDrvStatus, SCALER_WIN eWindow)
#else
__attribute__((weak)) MS_U16 MApi_XC_GetVideoCaptureInfo(ST_XC_VIDEO_CAPTURE_INFO *pDrvStatus, SCALER_WIN eWindow)
#endif
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return 0;
    }

    stXC_VIDEO_CAPTURE_INFO XCArgs;
    XCArgs.pDrvStatus = pDrvStatus;
    XCArgs.eWindow = eWindow;
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_VIDEO_CAPTURE_INFO, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return 0;
    }
    else
    {
        return XCArgs.enReturnValue;
    }
}

#ifdef MSOS_TYPE_LINUX_KERNEL
extern MS_BOOL KDrv_XC_PQ_SetHNonLinearScaling(ST_KDRV_XC_PQ_NONLINEARSCALING_API_PARAM_IN *pstHNLAPIParamIn);
#endif
E_APIXC_ReturnValue MDrv_XC_H_NonLinearScaling(void * pInstance, ST_XC_H_NONLINEAR_SCALING_IN *pstHNLIn)
{
    ST_KDRV_XC_PQ_NONLINEARSCALING_API_PARAM_IN stKdrvPqHNLIn;
    memset(&stKdrvPqHNLIn, 0, sizeof(ST_KDRV_XC_PQ_NONLINEARSCALING_API_PARAM_IN));

    if(pstHNLIn == NULL)
    {
        printf("Warning : pstHNLIn is Empty!");
        return E_APIXC_RET_FAIL;
    }

    XC_ApiStatusEx stXCStatusEx;
    memset(&stXCStatusEx, 0, sizeof(XC_ApiStatusEx));
    stXCStatusEx.u32ApiStatusEx_Version = API_XCSTATUS_EX_VERSION;
    stXCStatusEx.u16ApiStatusEX_Length = sizeof(XC_ApiStatusEx);
    MDrv_XC_GetStatusEx(pInstance, &stXCStatusEx,MAIN_WINDOW);

    stKdrvPqHNLIn.u32Version = pstHNLIn->u32Version;
    stKdrvPqHNLIn.u32Length = pstHNLIn->u32Length;
    stKdrvPqHNLIn.u8Win = pstHNLIn->u8Win;
    stKdrvPqHNLIn.u8HNLEn = pstHNLIn->u8HNL_En;
    stKdrvPqHNLIn.u16SourceWidth = stXCStatusEx.u16HPostScalingSrc;
    stKdrvPqHNLIn.u16TargetWidth = stXCStatusEx.u16HPostScalingDst;
    stKdrvPqHNLIn.u32TargetRatioH = pstHNLIn->u32HNL_Target_Ratio_h;
    stKdrvPqHNLIn.u32TargetRatioHStart = pstHNLIn->u32HNL_Target_Ratio_h_start;
    stKdrvPqHNLIn.u16Width0 = pstHNLIn->u16HNL_Width0;
    stKdrvPqHNLIn.u16Width1 = pstHNLIn->u16HNL_Width1;

    stKdrvPqHNLIn.u8ParamFromVREn = pstHNLIn->u8ParamFromVREn;

#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    if(0 > _s32FdScaler)
    {
        _s32FdScaler = open("/dev/scaler", O_RDWR);

        if(0 > _s32FdScaler)
        {
            printf("Scaler device not opened!!!!\n");
            return E_APIXC_RET_FAIL;
        }
    }
#endif

#if (defined MSOS_TYPE_LINUX) || (defined ANDROID)
    if (ioctl(_s32FdScaler, MDRV_XC_IOC_SET_H_NONLINEAR_SCALING, &stKdrvPqHNLIn))
    {
        printf("MDRV_XC_IOC_SET_H_NONLINEAR_SCALING fail!!!!\n");
        return E_APIXC_RET_FAIL;
    }
#endif

#ifdef MSOS_TYPE_LINUX_KERNEL
    if (KDrv_XC_PQ_SetHNonLinearScaling(&stKdrvPqHNLIn) != TRUE)
    {
        return E_APIXC_RET_FAIL;
    }
#endif

    return E_APIXC_RET_OK;
}


E_APIXC_ReturnValue MApi_XC_H_NonLinearScaling_U2(void * pInstance, ST_XC_H_NONLINEAR_SCALING_IN *pstHNLIn)
{
    E_APIXC_ReturnValue bReturn = E_APIXC_RET_FAIL;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    ST_XC_H_NONLINEAR_SCALING_IN stHNLIn;
    MS_U32 u32CopiedLength = sizeof(ST_XC_H_NONLINEAR_SCALING_IN);
    memset(&stHNLIn, 0, sizeof(ST_XC_H_NONLINEAR_SCALING_IN));

    VERSION_COMPATIBLE_CHECK(pstHNLIn, stHNLIn, u32CopiedLength, u32Version, u32Length, ST_XC_H_NONLINEAR_SCALING_IN_VERSION, E_APIXC_RET_FAIL);
    pstHNLIn->u32Version = ST_XC_H_NONLINEAR_SCALING_IN_VERSION; /*Return the actual version of the returned value for APP to use*/
    pstHNLIn->u32Length = u32CopiedLength; /*Return the actual length of the returned value for APP to know whick info is valid*/

    memcpy(&stHNLIn, pstHNLIn, u32CopiedLength);

    bReturn = MDrv_XC_H_NonLinearScaling(pInstance,&stHNLIn);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return bReturn;
}

E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_H_NonLinearScaling(ST_XC_H_NONLINEAR_SCALING_IN *pstHNLIn)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_H_NONLINEAR_SCALING_IN XCArgs;
    XCArgs.pstHNLIn = pstHNLIn;
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;

    if (UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_H_NONLINEAR_SCALING, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.enReturnValue;
    }

}

#endif

E_APIXC_ReturnValue MDrv_XC_Memory_Enable(void* pInstance, ST_XC_MEM_ENABLE_CTRL* pstMemEnableCtrl)
{
    XC_LOG_TRACE(XC_DBGLEVEL_SVP, "Enter MDrv_XC_Memory_Enable\n");
    E_APIXC_ReturnValue enRet = E_APIXC_RET_FAIL;
    enRet = MHal_XC_Memory_Enable(pInstance,pstMemEnableCtrl);
    return enRet;
}

E_APIXC_ReturnValue MApi_XC_Memory_Enable_U2(void* pInstance, ST_XC_MEM_ENABLE_CTRL* pstMemEnableCtrl)
{
    XC_LOG_TRACE(XC_DBGLEVEL_SVP, "Enter MApi_XC_Memory_Enable_U2\n");
    E_APIXC_ReturnValue enRet = E_APIXC_RET_FAIL;
    enRet = MDrv_XC_Memory_Enable(pInstance,pstMemEnableCtrl);
    return enRet;
}

E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_Memory_Enable(ST_XC_MEM_ENABLE_CTRL* pstMemEnableCtrl)
{
    XC_LOG_TRACE(XC_DBGLEVEL_SVP, "Enter MApi_XC_Memory_Enable\n");
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }
    stXC_MEM_ENABLE XCArgs;
    XCArgs.pstMemEnableCtrl = pstMemEnableCtrl;
    XCArgs.eReturnValue = E_APIXC_RET_FAIL;
    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_MEM_ENABLE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.eReturnValue;
    }
}

MS_BOOL _MDrv_XC_SVP_MirrorModeDetection(void* pInstance,SCALER_WIN eWindow)
{
    MS_BOOL bRet = FALSE;
    MS_BOOL bIsMirr = FALSE;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    if(((gSrcInfo[eWindow].Status2.eMirrorMode == MIRROR_H_ONLY) ||
         (gSrcInfo[eWindow].Status2.eMirrorMode == MIRROR_V_ONLY) ||
         (gSrcInfo[eWindow].Status2.eMirrorMode == MIRROR_HV)))
    {
        bIsMirr = TRUE;
    }
    if ((MDrv_XC_OPTEE_IsAnyPipe_Active(pInstance)) &&(bIsMirr)&&
         (!(MDrv_XC_GetDynamicScalingStatus(pInstance) || MDrv_XC_Is_DSForceIndexEnabled(pInstance, eWindow))))
    {
        bRet = TRUE;
    }
    _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
    return bRet;
}

E_APIXC_ReturnValue MApi_XC_TestFunction_U2(void* pInstance, ST_XC_TEST_FUNCTION *pstXCTestFunction)
{
    ST_XC_TEST_FUNCTION stXCTestFunction;
    ds_reg_ip_op_sel eIPOP_Sel = DS_IP;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));

    memset(&stXCTestFunction, 0, sizeof(ST_XC_TEST_FUNCTION));
    stXCTestFunction.enTestFunction = pstXCTestFunction->enTestFunction;
    stXCTestFunction.stXCWriteCmd.eWindow = pstXCTestFunction->stXCWriteCmd.eWindow;
    stXCTestFunction.stXCWriteCmd.u32CmdRegAddr = pstXCTestFunction->stXCWriteCmd.u32CmdRegAddr;
    stXCTestFunction.stXCWriteCmd.u16CmdRegValue = pstXCTestFunction->stXCWriteCmd.u16CmdRegValue;
    if(pstXCTestFunction->stXCWriteCmd.IPOP_Sel == E_DS_IP)
    {
        eIPOP_Sel = DS_IP;
    }
    else
    {
        eIPOP_Sel = DS_OP;
    }
    stXCTestFunction.stXCWriteCmd.Source_Select = pstXCTestFunction->stXCWriteCmd.Source_Select;

    if(stXCTestFunction.enTestFunction == E_XC_TEST_WRITE_DSCMD)
    {
        Hal_SC_WriteSWDSCommand(pInstance, stXCTestFunction.stXCWriteCmd.eWindow,stXCTestFunction.stXCWriteCmd.u32CmdRegAddr, stXCTestFunction.stXCWriteCmd.u16CmdRegValue, eIPOP_Sel, (ds_reg_source_sel)stXCTestFunction.stXCWriteCmd.Source_Select, &pXCResourcePrivate->stXC_DS_CmdCnt);
    }
    return E_APIXC_RET_OK;
}

E_APIXC_ReturnValue MApi_XC_TestFunction(ST_XC_TEST_FUNCTION *pstXCTestFunction)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_TEST_FUNCTION XCArgs;
    XCArgs.pstXCTestFunction = pstXCTestFunction;
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;

    if(UtopiaIoctl(pu32XCInst, E_XC_CMD_TEST_FUNCTION, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.enReturnValue;
    }
}
E_APIXC_ReturnValue MApi_XC_SetScalerBWLevel_U2(void * pInstance, ST_XC_QOS_SCALER_BW_INFO *pstQosBWInfo)
{
    E_APIXC_ReturnValue bReturn = E_APIXC_RET_FAIL;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    ST_XC_QOS_SCALER_BW_INFO stQosBWInfo;
    MS_U32 u32CopiedLength = sizeof(ST_XC_QOS_SCALER_BW_INFO);
    memset(&stQosBWInfo, 0, sizeof(ST_XC_QOS_SCALER_BW_INFO));

    VERSION_COMPATIBLE_CHECK(pstQosBWInfo, stQosBWInfo, u32CopiedLength, u32Version, u32Length, ST_XC_QOS_SCALER_BW_INFO_VERSION, E_APIXC_RET_FAIL);
    pstQosBWInfo->u32Version = ST_XC_QOS_SCALER_BW_INFO_VERSION; /*Return the actual version of the returned value for APP to use*/
    pstQosBWInfo->u32Length = u32CopiedLength; /*Return the actual length of the returned value for APP to know whick info is valid*/

    memcpy(&stQosBWInfo, pstQosBWInfo, u32CopiedLength);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");

    return bReturn;
}
E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_SetScalerBWLevel(ST_XC_QOS_SCALER_BW_INFO *pstQosBWInfo)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_QOS_SCALER_BW_INFO XCArgs;
    XCArgs.pstQosBWInfo = pstQosBWInfo;
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;

    if (UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_SCALER_BW_LEVEL, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.enReturnValue;
    }
}

E_APIXC_ReturnValue MApi_XC_SetFRCBWLevel_U2(void * pInstance, ST_XC_QOS_FRC_BW_INFO *pstQosBWInfo)
{
    E_APIXC_ReturnValue bReturn = E_APIXC_RET_FAIL;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    ST_XC_QOS_FRC_BW_INFO stQosBWInfo;
    MS_U32 u32CopiedLength = sizeof(ST_XC_QOS_FRC_BW_INFO);
    memset(&stQosBWInfo, 0, sizeof(ST_XC_QOS_FRC_BW_INFO));
    VERSION_COMPATIBLE_CHECK(pstQosBWInfo, stQosBWInfo, u32CopiedLength, u32Version, u32Length, ST_XC_QOS_FRC_BW_INFO_VERSION, E_APIXC_RET_FAIL);

    pstQosBWInfo->u32Version = ST_XC_QOS_FRC_BW_INFO_VERSION; /*Return the actual version of the returned value for APP to use*/
    pstQosBWInfo->u32Length = sizeof(ST_XC_QOS_FRC_BW_INFO); /*Return the actual length of the returned value for APP to know whick info is valid*/

    memcpy(&stQosBWInfo, pstQosBWInfo, sizeof(ST_XC_QOS_FRC_BW_INFO));
    FRC_R2_CMD_PARAMETER_t stFRC_R2_Para;
    memset(&stFRC_R2_Para,0,sizeof(FRC_R2_CMD_PARAMETER_t));
    if(stQosBWInfo.enFRCBWmode < E_XC_FRC_BW_MAX)
    {
        stFRC_R2_Para.p1 = (MS_U8)stQosBWInfo.enFRCBWmode;
        _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR);
        UtopiaResourceGetPrivate(g_pXCResource[_XC_SELECT_INTERNAL_VARIABLE(psXCInstPri->u32DeviceID)],(void**)(&pXCResourcePrivate));
        pXCResourcePrivate->stdrvXC_MVideo_Context.enFRCBWmode = stQosBWInfo.enFRCBWmode;
        _XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
        if(MDrv_XC_SendCmdToFRC(pInstance,E_XC_FRC_MB_CMD_SET_MFC_BANDWIDTH_MODE,1,stFRC_R2_Para) == TRUE)
        {
            bReturn = E_APIXC_RET_OK;
        }
    }

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return bReturn;
}
E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_SetFRCBWLevel(ST_XC_QOS_FRC_BW_INFO *pstQosBWInfo)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_QOS_FRC_BW_INFO XCArgs;
    XCArgs.pstQosBWInfo = pstQosBWInfo;
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;

    if (UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_FRC_BW_LEVEL, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.enReturnValue;
    }
}
E_APIXC_ReturnValue MApi_XC_QOS_CallBackFun_U2(void * pInstance, ST_XC_QOS_CALLBACK_INFO *pstQosCallbackInfo)
{
    E_APIXC_ReturnValue bReturn = E_APIXC_RET_OK;
    XC_INSTANCE_PRIVATE *psXCInstPri = NULL;
    UtopiaInstanceGetPrivate(pInstance, (void**)&psXCInstPri);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    ST_XC_QOS_CALLBACK_INFO stQosCallbackInfo;
    MS_U32 u32CopiedLength = sizeof(ST_XC_QOS_CALLBACK_INFO);
    memset(&stQosCallbackInfo, 0, sizeof(ST_XC_QOS_CALLBACK_INFO));

    VERSION_COMPATIBLE_CHECK(pstQosCallbackInfo, stQosCallbackInfo, u32CopiedLength, u32Version, u32Length, ST_XC_QOS_CALLBACK_INFO_VERSION, E_APIXC_RET_FAIL);
    pstQosCallbackInfo->u32Version = ST_XC_QOS_CALLBACK_INFO_VERSION; /*Return the actual version of the returned value for APP to use*/
    pstQosCallbackInfo->u32Length = u32CopiedLength; /*Return the actual length of the returned value for APP to know whick info is valid*/

    if(pstQosCallbackInfo->pfnFunInputSourceInfo != NULL)
    {
        g_stXCQosCallBackInfo.pfnFunInputSourceInfo = pstQosCallbackInfo->pfnFunInputSourceInfo;
    }

    return bReturn;
}
E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_QOS_CallBackFun(ST_XC_QOS_CALLBACK_INFO *pstQosCallBackInfo)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n",__FUNCTION__,__LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_QOS_CALLBACK_INFO XCArgs;
    XCArgs.pstCallBack = pstQosCallBackInfo;
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;

    if (UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_QOS_CALLBACKFUN, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.enReturnValue;
    }
}

E_APIXC_ReturnValue MApi_XC_SetVRROn_U2(void* pInstance, MS_BOOL bEnable)
{
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[E_XC_POOL_ID_INTERNAL_VARIABLE],(void**)(&pXCResourcePrivate));

    _XC_SEMAPHORE_ENTRY(pInstance, E_XC_ID_VAR);
    pXCResourcePrivate->stdrvXC_MVideo_Context.g_bSetVRROn = bEnable;
    _XC_SEMAPHORE_RETURN(pInstance, E_XC_ID_VAR);
    return E_APIXC_RET_OK;
}

E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_SetVRROn(MS_BOOL bEnable)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n", __FUNCTION__, __LINE__);
        return E_APIXC_RET_FAIL;
    }

    stXC_SET_VRRON XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;

    if (UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_VRRON, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.enReturnValue;
    }
}

E_APIXC_ReturnValue MApi_XC_Enable_CusSTR_U2(void* pInstance, MS_BOOL bEnable)
{
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[E_XC_POOL_ID_INTERNAL_VARIABLE],(void**)(&pXCResourcePrivate));

    _XC_SEMAPHORE_ENTRY(pInstance, E_XC_ID_VAR);
    pXCResourcePrivate->stdrvXC_MVideo_Context.g_bEnableCusSTR = bEnable;
    _XC_SEMAPHORE_RETURN(pInstance, E_XC_ID_VAR);
    return E_APIXC_RET_OK;
}

E_APIXC_ReturnValue SYMBOL_WEAK MApi_XC_Enable_CusSTR(MS_BOOL bEnable)
{
    if (pu32XCInst == NULL)
    {
        if(UtopiaOpen(MODULE_XC, &pu32XCInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return E_APIXC_RET_FAIL;
        }
    }

    stXC_CUSSTR_ENABLE XCArgs;
    XCArgs.bEnable = bEnable;
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;

    if (UtopiaIoctl(pu32XCInst, E_XC_CMD_CUSSTR_ENABLE, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.enReturnValue;
    }
}

MS_BOOL MApi_XC_Get_CusSTRStatus_U2(void* pInstance)
{
    MS_BOOL bEnable = FALSE;
    XC_RESOURCE_PRIVATE* pXCResourcePrivate = NULL;
    UtopiaResourceGetPrivate(g_pXCResource[E_XC_POOL_ID_INTERNAL_VARIABLE],(void**)(&pXCResourcePrivate));

    _XC_SEMAPHORE_ENTRY(pInstance, E_XC_ID_VAR);
    bEnable = pXCResourcePrivate->stdrvXC_MVideo_Context.g_bEnableCusSTR;
    _XC_SEMAPHORE_RETURN(pInstance, E_XC_ID_VAR);
    return bEnable;
}

MS_BOOL SYMBOL_WEAK MApi_XC_Get_CusSTRStatus(void)
{
    if (pu32XCInst == NULL)
    {
        printf("[%s,%5d]No instance existed, please get an instance by calling MApi_XC_Init() first\n", __FUNCTION__, __LINE__);
        return FALSE;
    }

    stXC_GET_CUSSTRSTATUS XCArgs;
    XCArgs.bEnable = FALSE;

    if (UtopiaIoctl(pu32XCInst, E_XC_CMD_GET_CUSSTRSTATUS, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return FALSE;
    }
    else
    {
        return XCArgs.bEnable;
    }
}

E_APIXC_ReturnValue MDrv_XC_Set_RWPointDiff(void* pInstance, ST_XC_SET_RW_POINT_DIFF stRWPointDiff)
{
    E_APIXC_ReturnValue enRet = E_APIXC_RET_FAIL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    enRet = MHal_XC_Set_RWPointDiff(pInstance,stRWPointDiff);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return enRet;
}

E_APIXC_ReturnValue MApi_XC_Set_RWPointDiff_U2(void* pInstance, ST_XC_SET_RW_POINT_DIFF stRWPointDiff)
{
    E_APIXC_ReturnValue enRet = E_APIXC_RET_FAIL;

    ST_XC_SET_RW_POINT_DIFF _stRWPointDiff;
    MS_U32 u32CopiedLength = sizeof(ST_XC_SET_RW_POINT_DIFF);
    memset(&_stRWPointDiff, 0, sizeof(ST_XC_SET_RW_POINT_DIFF));

    VERSION_COMPATIBLE_CHECK(&stRWPointDiff, _stRWPointDiff, u32CopiedLength, u32Version, u32Length, ST_XC_SET_RW_POINT_DIFF_VERSION, E_APIXC_RET_FAIL);
    stRWPointDiff.u32Version = ST_XC_SET_RW_POINT_DIFF_VERSION; /*Return the actual version of the returned value for APP to use*/
    stRWPointDiff.u32Length = u32CopiedLength; /*Return the actual length of the returned value for APP to know whick info is valid*/

    memcpy(&_stRWPointDiff, &stRWPointDiff, u32CopiedLength);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");
    enRet = MDrv_XC_Set_RWPointDiff(pInstance,_stRWPointDiff);
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return enRet;
}

E_APIXC_ReturnValue MApi_XC_SourceSignal_Control_U2(void* pInstance, ST_XC_SRC_SIG_CTL *pstSourceSignalCtl)
{
    E_APIXC_ReturnValue enRet = E_APIXC_RET_FAIL;
    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Entry\n");

    enRet = MDrv_XC_SourceSignal_Control(pInstance, pstSourceSignalCtl);

    XC_LOG_TRACE(XC_DBGLEVEL_FUNCTION_TRACE, "Exit\n");
    return enRet;
}

E_APIXC_ReturnValue MApi_XC_Set_RWPointDiff(ST_XC_SET_RW_POINT_DIFF stRWPointDiff)
{
    stXC_SET_RWPOINTDIFF XCArgs;

    if (pu32XCInst == NULL)
    {
        if(UtopiaOpen(MODULE_XC, &pu32XCInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return E_APIXC_RET_FAIL;
        }
    }

    memcpy(&XCArgs.stRWPointDiff, &stRWPointDiff, sizeof(ST_XC_SET_RW_POINT_DIFF));
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;

    if (UtopiaIoctl(pu32XCInst, E_XC_CMD_SET_RW_POINT_DIFF, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.enReturnValue;
    }
}

E_APIXC_ReturnValue MApi_XC_SourceSignal_Control(ST_XC_SRC_SIG_CTL *pstSourceSignalCtl)
{
    stXC_SRCSIG_CTL XCArgs;
    memset(&XCArgs, 0, sizeof(stXC_SRCSIG_CTL));

    if (pu32XCInst == NULL)
    {
        if(UtopiaOpen(MODULE_XC, &pu32XCInst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            printf("UtopiaOpen XC failed\n");
            return E_APIXC_RET_FAIL;
        }
    }

    if(pstSourceSignalCtl == NULL)
    {
        printf("[%s,%d] pstSourceSignalCtl is NULL!!! \n", __FUNCTION__, __LINE__);
        return E_APIXC_RET_FAIL;
    }

    XCArgs.pstSrcSigCTL = pstSourceSignalCtl;
    XCArgs.enReturnValue = E_APIXC_RET_FAIL;

    if (UtopiaIoctl(pu32XCInst, E_XC_CMD_SRC_SIG_CONTROL, (void*)&XCArgs) != UTOPIA_STATUS_SUCCESS)
    {
        printf("Obtain XC engine fail\n");
        return E_APIXC_RET_FAIL;
    }
    else
    {
        return XCArgs.enReturnValue;
    }
}

