//<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  apiXC_Adc.c
/// brief  Scaler API layer Interface
/// author MStar Semiconductor Inc.
///////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef UTOPIAXP_REMOVE_WRAPPER
#ifndef _API_XC_DWIN_C_
#define _API_XC_DWIN_C_
#endif
//-------------------------------------------------------------------------------------------------
//  Include Files
//-------------------------------------------------------------------------------------------------
// Common Definition
#ifdef MSOS_TYPE_LINUX_KERNEL
#include <linux/string.h>
#include <linux/slab.h>
#else
#include <string.h>
#endif
#include "MsCommon.h"
#include "MsOS.h"
#ifdef UTOPIA_V2
#include "utopia.h"
#include "utopia_driver_id.h"
#include "apiXC_DWIN_v2.h"
#ifdef MFD_MODULE_ENABLE
#include "drvMFD.h"
#endif
#endif
#include "drvXC_IOPort.h"
#include "apiXC.h"
#include "apiXC_DWIN.h"
#include "mvideo_context.h"
#include "drv_sc_DIP_scaling.h"
#include "mhal_dip.h"
#ifdef GOPD_DIP
#include "apiGOP.h"
#ifdef MSOS_TYPE_LINUX
#include <pthread.h>
#endif
#endif

#include "XC_DWIN_private.h"
#include "drvXC_HDMI_if.h"
// Internal Definition

//-------------------------------------------------------------------------------------------------
//  Driver Compiler Options
//-------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------
//  Local Defines
//-------------------------------------------------------------------------------------------------
#if defined(MSOS_TYPE_LINUX) || defined(MSOS_TYPE_LINUX_KERNEL)
#ifndef DIP_UTOPIA2K
#define DIP_UTOPIA2K
#endif
#endif

#define DIPR_WIDTH_PATCH (1)

#ifndef UNUSED
#define UNUSED(x) (void)(x)
#endif
//#define TRACE_ADC_INPUT(x) //x
#define msg_video(x) //x

#define DIPDRIVER_BASE 0

#if(defined(CONFIG_MLOG))
#include "ULog.h"

// Debug Logs, level form low(INFO) to high(FATAL, always show)
// Function information, ex function entry
#ifndef XC_INFO
#define XC_INFO(x, args...) ULOGI("DIP API", x, ##args)
#endif
// Warning, illegal paramter but can be self fixed in functions
#ifndef XC_WARN
#define XC_WARN(x, args...) ULOGW("DIP API", x, ##args)
#endif
//  Need debug, illegal paramter.
#ifndef XC_DBUG
#define XC_DBUG(x, args...) ULOGD("DIP API", x, ##args)
#endif
// Error, function will be terminated but system not crash
#ifndef XC_ERR
#define XC_ERR(x, args...) ULOGE("DIP API", x, ##args)
#endif
// Critical, system crash. (ex. assert)
#ifndef XC_FATAL
#define XC_FATAL(x, args...) ULOGF("DIP API", x, ##args)
#endif

#else
// Debug Logs, level form low(INFO) to high(FATAL, always show)
// Function information, ex function entry
#ifndef XC_INFO
#define XC_INFO(x, args...) printf("DIP API", x, ##args)
#endif
// Warning, illegal paramter but can be self fixed in functions
#ifndef XC_WARN
#define XC_WARN(x, args...) printf("DIP API", x, ##args)
#endif
//  Need debug, illegal paramter.
#ifndef XC_DBUG
#define XC_DBUG(x, args...) printf("DIP API", x, ##args)
#endif
// Error, function will be terminated but system not crash
#ifndef XC_ERR
#define XC_ERR(x, args...) printf("DIP API", x, ##args)
#endif
// Critical, system crash. (ex. assert)
#ifndef XC_FATAL
#define XC_FATAL(x, args...) printf("DIP API", x, ##args)
#endif

#endif

#if DIPR_WIDTH_PATCH
#ifdef DIP_UTOPIA2K
#define DIP_INTERNAL_IRQ           (1)
#else
#define DIP_INTERNAL_IRQ           (0)
#endif
#define DIP_TIMEOUT                (100)  //(ms)
#define DIP_FIRE_DONE              (1)
#endif
static SCALER_DIP_SOURCE_TYPE _genSource[MAX_DIP_WINDOW] =  {[0 ... (MAX_DIP_WINDOW-1)] = SCALER_DIP_SOURCE_TYPE_SUB2};
//-------------------------------------------------------------------------------------------------
//  Local Structurs
//-------------------------------------------------------------------------------------------------
#if DIPR_WIDTH_PATCH
typedef struct DLL_PACKED
{
    ST_XC_DIPR_PROPERTY   stDIPRProperty;
    ST_DIP_MFDEC_INFO     stDIPR_MFDecInfo;
    XC_SETWIN_INFO        stXC_SetWin_Info;
    ST_XC_DIP_WINPROPERTY stDIPWinProperty;
    EN_XC_DIP_DATA_FMT    enDIPWFmt;
    EN_XC_DIP_TILE_BLOCK  enRdTileFmt;
    MS_U16                u16OffsetWidth;
    MS_U16                u16WriteWidth;
}ST_DIPR_PATCH_INFO;

typedef struct DLL_PACKED
{
    EN_XC_DIP_DATA_FMT enDataFmt;
    EN_XC_DIP_TILE_BLOCK enRdTileFmt;
    MS_BOOL bMfdEna;
}ST_DIPR_FORMAT_INFO;
#endif

#define HDMI_SOURCE_VERSION_HDMI2P1     (3)
//-------------------------------------------------------------------------------------------------
//  Global Variables
//-------------------------------------------------------------------------------------------------
#ifdef UTOPIA_V2
void* pInstantDip[MAX_DIP_WINDOW] = {NULL};
#endif

#ifdef MFD_MODULE_ENABLE
MFD_RES_HANDLE Mfdec_Res = MFD_RES_TYPE_IDX_NONE;
MS_U32 Mfdec_DIP_WIN = MAX_DIP_WINDOW;
#endif

#define CheckDIPInstanceOpen(eWin,eReturn) \
    if(pInstantDip[eWin] == NULL) \
    { \
        if(UtopiaOpen(MODULE_DIP,(void**)&pInstantDip[eWin], 0, NULL) != UTOPIA_STATUS_SUCCESS) \
        { \
            XC_ERR("Open DIP fail\n"); \
            return eReturn; \
        } \
    } \

//-------------------------------------------------------------------------------------------------
//  Local Variables
//-------------------------------------------------------------------------------------------------
//MS_XC_MEM_FMT s_DIPeMemFmt[WINDOW_NUM] = {E_MS_XC_MEM_FMT_AUTO, E_MS_XC_MEM_FMT_AUTO};
#ifdef GOPD_DIP
GOP_DwinProperty GopD_property;
EN_XC_DIP_DATA_FMT DataFmt = DIP_DATA_FMT_YUV422;
extern SCALER_DIP_WIN Gopd_dip;
static InterruptCb pGopd_CB;
#ifdef MSOS_TYPE_LINUX
#define FRAME_DELAY 15
static MS_BOOL bDeAttached = TRUE;
static pthread_t id;
#endif
#endif

#ifdef STELLAR
#define DIP_STACK_SIZE 4096
typedef struct
{
    MS_BOOL        bInstall[MAX_DIP_WINDOW];
    MS_S32         s32TaskId[MAX_DIP_WINDOW];
    InterruptCb dip_callback[MAX_DIP_WINDOW];
}DIP_CbData;

DIP_CbData _stDIPCB = {.bInstall={0,0,0},.s32TaskId = {-1,-1,-1}};
MS_U8  _u8DIP_CallbackStack[MAX_DIP_WINDOW][DIP_STACK_SIZE];
static char* DipTaskName[MAX_DIP_WINDOW]={"DIP_WINDOW_TASK","DWIN0_WINDOW_TASK","DWIN1_WINDOW_TASK"};
#endif

#ifdef DIP_UTOPIA2K
static InterruptCb _gpAPIDipIntCb;
static InterruptCb _gpAPIDwin0IntCb;
static InterruptCb _gpAPIDwin1IntCb;
static InterruptCb _gpdip2IntIndepCb;
static MS_BOOL _gbAPIDipIsr = FALSE, _gbAPIDwin0Isr = FALSE, _gbAPIDwin1Isr = FALSE, _gbAPIAttached = FALSE;
static MS_BOOL _gbDIP2IndepIsr = FALSE;
#endif

#if DIPR_WIDTH_PATCH
static ST_XC_DIPR_PROPERTY _gstDiprProperty[MAX_DIP_WINDOW] = {[0 ... (MAX_DIP_WINDOW-1)] = {}};
static ST_DIP_MFDEC_INFO _gstMfdInfo[MAX_DIP_WINDOW] = {[0 ... (MAX_DIP_WINDOW-1)] = {}};
static MS_BOOL _gbDIEna[MAX_DIP_WINDOW] = {[0 ... (MAX_DIP_WINDOW-1)] = (FALSE)};
static XC_SETWIN_INFO _gstXCSetWinInfo[MAX_DIP_WINDOW] = {[0 ... (MAX_DIP_WINDOW-1)] = {}};
static ST_XC_DIP_WINPROPERTY _gstDipwProperty[MAX_DIP_WINDOW] = {[0 ... (MAX_DIP_WINDOW-1)] = {}};
static EN_XC_DIP_DATA_FMT _genDIPWFmt[MAX_DIP_WINDOW] = {[0 ... (MAX_DIP_WINDOW-1)] = (DIP_DATA_FMT_MAX)};
static EN_XC_DIP_TILE_BLOCK _genRdTileFmt[MAX_DIP_WINDOW] = {[0 ... (MAX_DIP_WINDOW-1)] = (DIP_TILE_BLOCK_R_NONE)};
static MS_BOOL _gbDIPRPatchFlowEna[MAX_DIP_WINDOW] = {[0 ... (MAX_DIP_WINDOW-1)] = (FALSE)};

#if DIP_INTERNAL_IRQ
static MS_S32 _gs32InternalIsrEventId[MAX_DIP_WINDOW] = {[0 ... (MAX_DIP_WINDOW-1)] = (-1)};
static MS_BOOL _gbInternalIsrEna[MAX_DIP_WINDOW] = {[0 ... (MAX_DIP_WINDOW-1)] = (FALSE)};
MS_BOOL gbInternalIsrAttached = FALSE;
#endif
#endif
//-------------------------------------------------------------------------------------------------
//  Debug Functions
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
//  Local Functions
//-------------------------------------------------------------------------------------------------
#ifdef STELLAR
void _DIP_Callback_Proc(SCALER_DIP_WIN eWindow)
{
    if(eWindow < MAX_DIP_WINDOW)
    {
        DIP_INTER_STUS* pIntrStus = (DIP_INTER_STUS*)utopia_malloc(sizeof(DIP_INTER_STUS));
        while(1)
        {
            if(pInstantDip[eWindow] && pIntrStus)
            {
                pIntrStus->IntrStautus = 0;
                pIntrStus->eFlag = DIP_INTR_QUERY;
                pIntrStus->eWindow = eWindow;
                if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_INTERRUPT,(void*)pIntrStus) == UTOPIA_STATUS_SUCCESS)
                {
                    _stDIPCB.dip_callback[eWindow](pIntrStus->IntrStautus);
                }
            }
            else
            {
                MsOS_DelayTask(100);
            }
        }
        if(pIntrStus)
        {
            utopia_free(pIntrStus);
        }
    }
}

void _DIP_Callback_task(InterruptCb pIntCb,SCALER_DIP_WIN eWindow)
{
    if(eWindow < MAX_DIP_WINDOW)
    {
        if(_stDIPCB.bInstall[eWindow] == FALSE && _stDIPCB.s32TaskId[eWindow] == -1)
        {
            _stDIPCB.s32TaskId[eWindow] = MsOS_CreateTask((TaskEntry)_DIP_Callback_Proc,
                                              (MS_U32)(eWindow),
                                              E_TASK_PRI_SYS,
                                              TRUE,
                                              (void*)_u8DIP_CallbackStack[eWindow],
                                              DIP_STACK_SIZE,
                                              DipTaskName[eWindow]);

            if (_stDIPCB.s32TaskId[eWindow] >= 0)
            {
                _stDIPCB.bInstall[eWindow] = TRUE;
            }
        }
        _stDIPCB.dip_callback[eWindow] = pIntCb;
    }
}
#endif

#ifdef DIP_UTOPIA2K
static void _MApi_XC_DIP_CallBack(InterruptNum eIntNum)
{
#if (DIPR_WIDTH_PATCH&&DIP_INTERNAL_IRQ)
    MS_U8 u8IntStus[MAX_DIP_WINDOW] = {[0 ... (MAX_DIP_WINDOW-1)] = 0};
    SCALER_DIP_WIN eWindow = DIP_WINDOW;

    u8IntStus[DIP_WINDOW] = MApi_XC_DIP_GetIntStatus(DIP_WINDOW);
    if(u8IntStus[DIP_WINDOW] > 0)
    {
        if(_gbInternalIsrEna[DIP_WINDOW] == TRUE)
        {
            MApi_XC_DIP_ClearInt(0xFF, DIP_WINDOW);
        }
        else
        {
            if(_gpAPIDipIntCb != NULL)
            {
                (void)(_gpAPIDipIntCb)(eIntNum);
            }
        }
    }

    u8IntStus[DWIN0_WINDOW] = MApi_XC_DIP_GetIntStatus(DWIN0_WINDOW);
    if(u8IntStus[DWIN0_WINDOW] > 0)
    {
        if(_gbInternalIsrEna[DWIN0_WINDOW] == TRUE)
        {
            MApi_XC_DIP_ClearInt(0xFF, DWIN0_WINDOW);
        }
        else
        {
            if(_gpAPIDwin0IntCb != NULL)
            {
                (void)(_gpAPIDwin0IntCb)(eIntNum);
            }
        }
    }

    u8IntStus[DWIN1_WINDOW] = MApi_XC_DIP_GetIntStatus(DWIN1_WINDOW);
    if(u8IntStus[DWIN1_WINDOW] > 0)
    {
        if(_gbInternalIsrEna[DWIN1_WINDOW] == TRUE)
        {
            MApi_XC_DIP_ClearInt(0xFF, DWIN1_WINDOW);
        }
        else
        {
            if(_gpAPIDwin1IntCb != NULL)
            {
                (void)(_gpAPIDwin1IntCb)(eIntNum);
            }
        }
    }
    MsOS_EnableInterrupt(E_INT_IRQ_DIPW);

    for(eWindow = DIP_WINDOW; eWindow < MAX_DIP_WINDOW; eWindow++)
    {
        if(u8IntStus[eWindow] > 0)
        {
            MsOS_SetEvent(_gs32InternalIsrEventId[eWindow], DIP_FIRE_DONE);
        }
    }
#else
    if( (MApi_XC_DIP_GetIntStatus(DIP_WINDOW) > 0) && (_gpAPIDipIntCb != NULL) )
    {
        (void)(_gpAPIDipIntCb)(eIntNum);
    }
    if( (MApi_XC_DIP_GetIntStatus(DWIN0_WINDOW) > 0) && (_gpAPIDwin0IntCb != NULL) )
    {
        (void)(_gpAPIDwin0IntCb)(eIntNum);
    }
    if( (MApi_XC_DIP_GetIntStatus(DWIN1_WINDOW) > 0) && (_gpAPIDwin1IntCb != NULL) )
    {
        (void)(_gpAPIDwin1IntCb)(eIntNum);
    }
    MsOS_EnableInterrupt(E_INT_IRQ_DIPW);
#endif
}

static void _MApi_XC_DIP2_CallBack(InterruptNum eIntNum)
{
    if(_gpdip2IntIndepCb != NULL)
    {
        (void)(_gpdip2IntIndepCb)(eIntNum);
    }
    MsOS_EnableInterrupt(E_INT_IRQ_DIP2);
}
#endif

E_APIXC_ReturnValue MApi_XC_DIP_GetChipCaps(EN_DIP_CHIPCAPS enDIPCaps, void* pRet, SCALER_DIP_WIN eWindow)
{
    ST_DIP_GET_CHIP_CAPS* pstDIPChipCaps = NULL;
    ST_DIP_CAPS_BUS_SIZE* pstDIPBusSize = NULL;
    ST_DIP_CAPS_DIPR_WIDTH_MAX* pstDIPRCapsWidthMax = NULL;
    ST_DIP_CAPS_DIPR_WIDTH_MIN* pstDIPRCapsWidthMin = NULL;
    ST_DIP_CAPS_IRQ* pstDIPCapsIrqSupport = NULL;
    ST_DIP_CAPS_VSCALING_MAX_WIDTH* pstDIPCapsVscalingMaxWidth = NULL;
    ST_DIP_CAPS_DIPW_WIDTH_MAX* pstDIPWCapsWidthMax = NULL;
    ST_DIP_CAPS_DI_ENGINE *pstDIEnagineSupport = NULL;
    MS_U32 u32Version = 0;
    MS_U32 u32Length = 0;

    CheckDIPInstanceOpen(eWindow,E_APIXC_RET_FAIL);

    pstDIPChipCaps = (ST_DIP_GET_CHIP_CAPS*)utopia_malloc(sizeof(ST_DIP_GET_CHIP_CAPS));
    if (pstDIPChipCaps == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }
    pstDIPChipCaps->u32Version = ST_DIP_GET_CHIP_CAPS_VERSION;
    pstDIPChipCaps->u32Length = sizeof(ST_DIP_GET_CHIP_CAPS);
    pstDIPChipCaps->enDIPCaps = enDIPCaps;
    pstDIPChipCaps->eWindow = eWindow;
    pstDIPChipCaps->pRet = pRet;

    /* version check */
    if(enDIPCaps == E_DIP_CAPS_BUS_SIZE)
    {
        pstDIPBusSize = (ST_DIP_CAPS_BUS_SIZE*)pRet;
        u32Version = pstDIPBusSize->u32Version;
        u32Length = pstDIPBusSize->u32Length;
        if((u32Version < ST_DIP_CAPS_BUS_SIZE_VERSION)||(u32Length < sizeof(ST_DIP_CAPS_BUS_SIZE)))
        {
            XC_ERR("%s,%d - invalid Version %td or invalid version Length %td\n",__func__,__LINE__,(ptrdiff_t)u32Version,(ptrdiff_t)u32Length);
            if(pstDIPChipCaps != NULL)
            {
                utopia_free(pstDIPChipCaps);
            }
            return E_APIXC_RET_FAIL;
        }
    }
    else if(enDIPCaps == E_DIP_CAPS_DIPR_MAX_WIDTH)
    {
        pstDIPRCapsWidthMax = (ST_DIP_CAPS_DIPR_WIDTH_MAX*)pRet;
        u32Version = pstDIPRCapsWidthMax->u32Version;
        u32Length = pstDIPRCapsWidthMax->u32Length;
        if((u32Version < ST_DIP_CAPS_DIPR_WIDTH_MAX_VERSION)||(u32Length < sizeof(ST_DIP_CAPS_DIPR_WIDTH_MAX)))
        {
            XC_ERR("%s,%d - invalid Version %td or invalid version Length %td\n",__func__,__LINE__,(ptrdiff_t)u32Version,(ptrdiff_t)u32Length);
            if(pstDIPChipCaps != NULL)
            {
                utopia_free(pstDIPChipCaps);
            }
            return E_APIXC_RET_FAIL;
        }
    }
    else if(enDIPCaps == E_DIP_CAPS_DIPR_MIN_WIDTH)
    {
        pstDIPRCapsWidthMin = (ST_DIP_CAPS_DIPR_WIDTH_MIN*)pRet;
        u32Version = pstDIPRCapsWidthMin->u32Version;
        u32Length = pstDIPRCapsWidthMin->u32Length;
        if((u32Version < ST_DIP_CAPS_DIPR_WIDTH_MIN_VERSION)||(u32Length < sizeof(ST_DIP_CAPS_DIPR_WIDTH_MIN)))
        {
            XC_ERR("%s,%d - invalid Version %td or invalid version Length %td\n",__func__,__LINE__,(ptrdiff_t)u32Version,(ptrdiff_t)u32Length);
            if(pstDIPChipCaps != NULL)
            {
                utopia_free(pstDIPChipCaps);
            }
            return E_APIXC_RET_FAIL;
        }
    }
    else if(enDIPCaps == E_DIP_CAPS_DIP_IRQ_SUPPORT)
    {
        pstDIPCapsIrqSupport = (ST_DIP_CAPS_IRQ*)pRet;
        u32Version = pstDIPCapsIrqSupport->u32Version;
        u32Length = pstDIPCapsIrqSupport->u32Length;
        if((u32Version < ST_DIP_CAPS_IRQ_VERSION)||(u32Length < sizeof(ST_DIP_CAPS_IRQ)))
        {
            XC_ERR("%s,%d - invalid Version %td or invalid version Length %td\n",__func__,__LINE__,(ptrdiff_t)u32Version,(ptrdiff_t)u32Length);
            if(pstDIPChipCaps != NULL)
            {
                utopia_free(pstDIPChipCaps);
            }
            return E_APIXC_RET_FAIL;
        }
    }
    else if(enDIPCaps == E_DIP_CAPS_VSCALING_MAX_WIDTH)
    {
        pstDIPCapsVscalingMaxWidth = (ST_DIP_CAPS_VSCALING_MAX_WIDTH*)pRet;
        u32Version = pstDIPCapsVscalingMaxWidth->u32Version;
        u32Length = pstDIPCapsVscalingMaxWidth->u32Length;
        if((u32Version < ST_DIP_CAPS_VSCALING_MAX_WIDTH_VERSION)||(u32Length < sizeof(ST_DIP_CAPS_VSCALING_MAX_WIDTH)))
        {
            XC_ERR("%s,%d - invalid Version %td or invalid version Length %td\n",__func__,__LINE__,(ptrdiff_t)u32Version,(ptrdiff_t)u32Length);
            if(pstDIPChipCaps != NULL)
            {
                utopia_free(pstDIPChipCaps);
            }
            return E_APIXC_RET_FAIL;
        }
    }
    else if(enDIPCaps == E_DIP_CAPS_DIPW_MAX_WIDTH)
    {
        pstDIPWCapsWidthMax = (ST_DIP_CAPS_DIPW_WIDTH_MAX*)pRet;
        u32Version = pstDIPWCapsWidthMax->u32Version;
        u32Length = pstDIPWCapsWidthMax->u32Length;
        if((u32Version < ST_DIP_CAPS_DIPW_WIDTH_MAX_VERSION)||(u32Length < sizeof(ST_DIP_CAPS_DIPW_WIDTH_MAX)))
        {
            XC_ERR("%s,%d - invalid Version %td or invalid version Length %td\n",__func__,__LINE__,(ptrdiff_t)u32Version,(ptrdiff_t)u32Length);
            if(pstDIPChipCaps != NULL)
            {
                utopia_free(pstDIPChipCaps);
            }
            return E_APIXC_RET_FAIL;
        }
    }
    else if(enDIPCaps == E_DIP_CAPS_DI_ENGINE)
    {
        pstDIEnagineSupport = (ST_DIP_CAPS_DI_ENGINE*)pRet;
        u32Version = pstDIEnagineSupport->u32Version;
        u32Length = pstDIEnagineSupport->u32Length;
        if((u32Version < ST_DIP_CAPS_DI_ENGINE_VERSION)||(u32Length < sizeof(ST_DIP_CAPS_DI_ENGINE)))
        {
            XC_ERR("%s,%d - invalid Version %td or invalid version Length %td\n",__func__,__LINE__,(ptrdiff_t)u32Version,(ptrdiff_t)u32Length);
            if(pstDIPChipCaps != NULL)
            {
                utopia_free(pstDIPChipCaps);
            }
            return E_APIXC_RET_FAIL;
        }
    }
    else
    {
        XC_ERR("%s,%d - invalid enDIPCaps %d \n",__func__,__LINE__,enDIPCaps);
        if(pstDIPChipCaps != NULL)
        {
            utopia_free(pstDIPChipCaps);
        }
        return E_APIXC_RET_FAIL;
    }

    if(UtopiaIoctl(pInstantDip[eWindow], MDrv_CMD_DIP_GET_CHIPCAPS, (void*)pstDIPChipCaps) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_GET_CHIPCAPS(%d) fail\n",enDIPCaps);
        if(pstDIPChipCaps != NULL)
        {
            utopia_free(pstDIPChipCaps);
        }
        return E_APIXC_RET_FAIL;
    }

    utopia_free(pstDIPChipCaps);

    return E_APIXC_RET_OK;
}

#if DIPR_WIDTH_PATCH
MS_BOOL _MApi_XC_DIP_GetBPP(EN_XC_DIP_DATA_FMT enDIPWFmt, MS_U16 *u16bpp)
{
    switch(enDIPWFmt)
    {
        case DIP_DATA_FMT_YUV422:
        case DIP_DATA_FMT_RGB565:
        case DIP_DATA_FMT_YC422:
            *u16bpp = 2;
            break;
        case DIP_DATA_FMT_ARGB8888:
            *u16bpp = 4;
            break;
        default:
            *u16bpp = 1;
            break;
    }

    return TRUE;
}

MS_BOOL _MApi_XC_DIP_GetAddrOffset(MS_U16 u16OffsetWidth, EN_XC_DIP_TILE_BLOCK enRdTileFmt, MS_PHY *phyAddrOffset, SCALER_DIP_WIN eWindow)
{
    MS_U16 u16BusSize=0;
    ST_DIP_CAPS_BUS_SIZE* pstDIPCapsBusSize = NULL;
    pstDIPCapsBusSize = (ST_DIP_CAPS_BUS_SIZE*)utopia_malloc(sizeof(ST_DIP_CAPS_BUS_SIZE));
    if(pstDIPCapsBusSize == NULL)
    {
        XC_ERR("%s ST_DIP_CAPS_BUS_SIZE alloc fail\n",__FUNCTION__);
        return FALSE;
    }
    pstDIPCapsBusSize->u32Version = ST_DIP_CAPS_BUS_SIZE_VERSION;
    pstDIPCapsBusSize->u32Length = sizeof(ST_DIP_CAPS_BUS_SIZE);
    MApi_XC_DIP_GetChipCaps(E_DIP_CAPS_BUS_SIZE, (void*)pstDIPCapsBusSize, eWindow);
    u16BusSize = pstDIPCapsBusSize->u16BusSize;

    switch(enRdTileFmt)
    {
        case DIP_TILE_BLOCK_R_16_32:
        case DIP_TILE_BLOCK_R_32_32:
            *phyAddrOffset = ((MS_PHY)u16OffsetWidth*(MS_PHY)u16BusSize);
            break;
        case DIP_TILE_BLOCK_R_32_16:
            *phyAddrOffset = ((MS_PHY)u16OffsetWidth*(MS_PHY)u16BusSize)/2;
            break;
        default:
            *phyAddrOffset = ((MS_PHY)u16OffsetWidth*(MS_PHY)u16BusSize);
            break;
    }
    if(pstDIPCapsBusSize != NULL)
    {
        utopia_free(pstDIPCapsBusSize);
    }

    return TRUE;
}

MS_BOOL _MApi_XC_DIP_GetCapsDIPRMaxWidth(ST_DIPR_FORMAT_INFO stDiprFormat, MS_U16 *u16WidthHWMax, SCALER_DIP_WIN eWindow)
{
    ST_DIP_CAPS_DIPR_WIDTH_MAX* pstDIPGetWidthCaps = NULL;
    pstDIPGetWidthCaps = (ST_DIP_CAPS_DIPR_WIDTH_MAX*)utopia_malloc(sizeof(ST_DIP_CAPS_DIPR_WIDTH_MAX));
    if (pstDIPGetWidthCaps == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return FALSE;
    }
    pstDIPGetWidthCaps->u32Version = ST_DIP_CAPS_DIPR_WIDTH_MAX_VERSION;
    pstDIPGetWidthCaps->u32Length = sizeof(ST_DIP_CAPS_DIPR_WIDTH_MAX);
    pstDIPGetWidthCaps->enDataFmt = stDiprFormat.enDataFmt;
    pstDIPGetWidthCaps->enRdTileFmt = stDiprFormat.enRdTileFmt;
    pstDIPGetWidthCaps->bMfdEna = stDiprFormat.bMfdEna;
    MApi_XC_DIP_GetChipCaps(E_DIP_CAPS_DIPR_MAX_WIDTH, (void*)pstDIPGetWidthCaps, eWindow);
    *u16WidthHWMax = pstDIPGetWidthCaps->u16Width;
    if(pstDIPGetWidthCaps != NULL)
    {
        utopia_free(pstDIPGetWidthCaps);
    }

    return TRUE;
}

MS_BOOL _MApi_XC_DIP_GetCapsDIPWMaxWidth(EN_XC_DIP_DATA_FMT enDipwDataFmt, MS_U16 *u16WidthHWMax, SCALER_DIP_WIN eWindow)
{
    ST_DIP_CAPS_DIPW_WIDTH_MAX* pstDIPGetWidthCaps = NULL;
    pstDIPGetWidthCaps = (ST_DIP_CAPS_DIPW_WIDTH_MAX*)utopia_malloc(sizeof(ST_DIP_CAPS_DIPW_WIDTH_MAX));
    if (pstDIPGetWidthCaps == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return FALSE;
    }
    pstDIPGetWidthCaps->u32Version = ST_DIP_CAPS_DIPW_WIDTH_MAX_VERSION;
    pstDIPGetWidthCaps->u32Length = sizeof(ST_DIP_CAPS_DIPW_WIDTH_MAX);
    pstDIPGetWidthCaps->enDataFmt = enDipwDataFmt;
    MApi_XC_DIP_GetChipCaps(E_DIP_CAPS_DIPW_MAX_WIDTH, (void*)pstDIPGetWidthCaps, eWindow);
    *u16WidthHWMax = pstDIPGetWidthCaps->u16Width;
    if(pstDIPGetWidthCaps != NULL)
    {
        utopia_free(pstDIPGetWidthCaps);
    }

    return TRUE;
}

MS_BOOL _MApi_XC_DIP_GetDIPRCapsMinWidth(MS_U16 u16Bpp, MS_U16 *u16CapsMinWidth, SCALER_DIP_WIN eWindow)
{
    ST_DIP_CAPS_DIPR_WIDTH_MIN* pstDIPCapsMinWidth = NULL;

    pstDIPCapsMinWidth = (ST_DIP_CAPS_DIPR_WIDTH_MIN*)utopia_malloc(sizeof(ST_DIP_CAPS_DIPR_WIDTH_MIN));
    if(pstDIPCapsMinWidth == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return FALSE;
    }
    pstDIPCapsMinWidth->u32Version = ST_DIP_CAPS_DIPR_WIDTH_MIN_VERSION;
    pstDIPCapsMinWidth->u32Length = sizeof(ST_DIP_CAPS_DIPR_WIDTH_MIN);
    pstDIPCapsMinWidth->u16Bpp = u16Bpp;
    MApi_XC_DIP_GetChipCaps(E_DIP_CAPS_DIPR_MIN_WIDTH, (void*)pstDIPCapsMinWidth, eWindow);
    *u16CapsMinWidth = pstDIPCapsMinWidth->u16Width;

    if(pstDIPCapsMinWidth != NULL)
    {
        utopia_free(pstDIPCapsMinWidth);
    }

    return TRUE;
}

MS_BOOL _MApi_XC_DIP_IsReadTileFmt(EN_XC_DIP_TILE_BLOCK enTileBlock, MS_BOOL *bIsReadTile)
{
    switch(enTileBlock)
    {
        case DIP_TILE_BLOCK_R_NONE:
        case DIP_TILE_BLOCK_R_16_32:
        case DIP_TILE_BLOCK_R_32_16:
        case DIP_TILE_BLOCK_R_32_32:
        case DIP_TILE_BLOCK_R_10Bit_LSB_32_16:
        case DIP_TILE_BLOCK_R_10Bit_LSB_128_64:
            *bIsReadTile = TRUE;
            break;
        case DIP_TILE_BLOCK_W_NONE:
        case DIP_TILE_BLOCK_W_16_32:
        case DIP_TILE_BLOCK_W_32_16:
        case DIP_TILE_BLOCK_W_32_32:
            *bIsReadTile = FALSE;
            break;
        default:
            *bIsReadTile = TRUE;
            break;
    }

    return TRUE;
}

MS_BOOL _MApi_XC_DIP_SaveTileFmt(EN_XC_DIP_TILE_BLOCK enTileBlock, SCALER_DIP_WIN eWindow)
{
    MS_BOOL bIsReadTile= FALSE;

    if(eWindow >= MAX_DIP_WINDOW)
    {
        return E_APIXC_RET_FAIL;
    }

    _MApi_XC_DIP_IsReadTileFmt(enTileBlock, &bIsReadTile);

    if(bIsReadTile == TRUE)
    {
        _genRdTileFmt[eWindow] = enTileBlock;
    }

    return TRUE;
}

MS_BOOL _MApi_XC_DIP_DIPR_InternalFire(SCALER_DIP_WIN eWindow)
{
    DIP_TRIGGER_MODE* pTrigMode = NULL;

    pTrigMode = (DIP_TRIGGER_MODE*)utopia_malloc(sizeof(DIP_TRIGGER_MODE));
    if (pTrigMode == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }
    pTrigMode->eTrigMode = DIP_TRIG_ONESHOTFAST;
    pTrigMode->eWindow = eWindow;

    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_TRIGGER,(void*)pTrigMode) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_TRIGGER DIP_TRIG_ONESHOTFAST fail\n");
        utopia_free(pTrigMode);
        return FALSE;
    }

    if(pTrigMode != NULL)
    {
        utopia_free(pTrigMode);
    }

    return TRUE;
}

MS_BOOL _MApi_XC_DIP_DIPR_WidthSWPatchPartialFire(ST_DIPR_PATCH_INFO* pstDIPRWidthPatchInfo, SCALER_DIP_WIN eWindow)
{
    ST_XC_DIPR_PROPERTY_EX stDIPRProperty_ex = {};
    ST_DIP_DI_SETTING stDIP_DI_setting = {};
    ST_XC_DIPR_PROPERTY stDIPRProperty = pstDIPRWidthPatchInfo->stDIPRProperty;
    ST_DIP_MFDEC_INFO stDIPR_MFDecInfo = pstDIPRWidthPatchInfo->stDIPR_MFDecInfo;
    XC_SETWIN_INFO stXC_SetWin_Info = pstDIPRWidthPatchInfo->stXC_SetWin_Info;
    ST_XC_DIP_WINPROPERTY stDIPWinProperty = pstDIPRWidthPatchInfo->stDIPWinProperty;
    EN_XC_DIP_DATA_FMT enDIPWFmt = pstDIPRWidthPatchInfo->enDIPWFmt;
    EN_XC_DIP_TILE_BLOCK enRdTileFmt = pstDIPRWidthPatchInfo->enRdTileFmt;
    MS_U16 u16OffsetWidth = pstDIPRWidthPatchInfo->u16OffsetWidth;
    MS_U16 u16WriteWidth = pstDIPRWidthPatchInfo->u16WriteWidth;
    MS_PHY phyReadAddrOffset = 0;
    MS_PHY phyWriteAddrOffset = 0;
    MS_U32 u32BufSize = 0;
    MS_U16 u16bpp = 0;

    MApi_XC_DIP_SetOutputDataFmt(enDIPWFmt, eWindow);

    stDIPRProperty.u16Width = u16WriteWidth;
    if(stDIPR_MFDecInfo.bMFDec_Enable == TRUE)
    {
        stDIPR_MFDecInfo.u16StartX = u16OffsetWidth;
        stDIPR_MFDecInfo.u16HSize = u16WriteWidth;
    }
    else
    {
        if(pstDIPRWidthPatchInfo->stDIPRProperty.enDataFmt < DIP_DATA_FMT_YUV420)
        {
            _MApi_XC_DIP_GetBPP(pstDIPRWidthPatchInfo->stDIPRProperty.enDataFmt, &u16bpp);
            phyReadAddrOffset = ((MS_PHY)u16OffsetWidth*(MS_PHY)u16bpp);
        }
        else
        {
            _MApi_XC_DIP_GetAddrOffset(u16OffsetWidth, enRdTileFmt,&phyReadAddrOffset,eWindow);
        }
        stDIPRProperty.u32YBufAddr += phyReadAddrOffset;
        stDIPRProperty.u32CBufAddr += phyReadAddrOffset;
    }
    stDIPRProperty_ex.stDIPRProperty = stDIPRProperty;
    stDIPRProperty_ex.stDIPR_MFDecInfo = stDIPR_MFDecInfo;
    stDIPRProperty_ex.stDIPR3DDISetting.stDipr3DDI_VerCtl.u32version = 1;
    stDIPRProperty_ex.stDIPR3DDISetting.stDipr3DDI_VerCtl.u32size = sizeof(ST_DIP_DI_SETTING);
    stDIP_DI_setting.bEnableDI = FALSE;
    stDIPRProperty_ex.stDIPR3DDISetting.pDIPR3DDISetting = &stDIP_DI_setting;
    MApi_XC_DIP_SetDIPRProperty_EX(&stDIPRProperty_ex, eWindow);
    stDIPWinProperty.u16Width = u16WriteWidth;
    _MApi_XC_DIP_GetBPP(enDIPWFmt, &u16bpp);
    phyWriteAddrOffset = ((MS_PHY)u16OffsetWidth*(MS_PHY)u16bpp);
    stDIPWinProperty.u32BufStart += phyWriteAddrOffset;
    u32BufSize = (stDIPWinProperty.u16Pitch)*(stDIPWinProperty.u16Height)*u16bpp;
    stDIPWinProperty.u32BufEnd = stDIPWinProperty.u32BufStart + u32BufSize;
    MApi_XC_DIP_SetDIPWinProperty(&stDIPWinProperty, eWindow);

    stXC_SetWin_Info.stCapWin.x = 0;
    stXC_SetWin_Info.stCapWin.width = u16WriteWidth;
    stXC_SetWin_Info.bPreHCusScaling = FALSE;
    stXC_SetWin_Info.u16PreHCusScalingSrc = u16WriteWidth;
    stXC_SetWin_Info.u16PreHCusScalingDst = u16WriteWidth;
    MApi_XC_DIP_SetWindow(&stXC_SetWin_Info, sizeof(XC_SETWIN_INFO), eWindow);

    MApi_XC_DIP_SetDIPWinProperty(&stDIPWinProperty, eWindow);

    MApi_XC_DIP_Set420TileBlock(enRdTileFmt, eWindow);

    _MApi_XC_DIP_DIPR_InternalFire(eWindow);

    return TRUE;
}

static MS_BOOL _MApi_XC_DIP_DIPR_WaitWriteDone(SCALER_DIP_WIN eWindow)
{
    MS_U32 u32StartTime = 0;
#if DIP_INTERNAL_IRQ
    MS_S32 s32Ret = 0;
    MS_U32 u32DummyEvent =0;
#endif

    u32StartTime = MsOS_GetSystemTime();

#if DIP_INTERNAL_IRQ
    if((_gbInternalIsrEna[eWindow] == TRUE)&&(_gs32InternalIsrEventId[eWindow]>0))
    {
        s32Ret = MsOS_WaitEvent_Interrupt(_gs32InternalIsrEventId[eWindow], DIP_FIRE_DONE, &u32DummyEvent, E_OR_CLEAR, DIP_TIMEOUT);
        if(s32Ret < 0)
        {
            XC_ERR("\033[1;31m[%s,%d] DIP[%d] Timeout !!! \033[0m\n",__FUNCTION__,__LINE__,eWindow);
            return FALSE;
        }
    }
    else
#endif
    {
        while(MApi_XC_DIP_GetIntStatus(eWindow) == 0)
        {
            if(MsOS_GetSystemTime() - u32StartTime > DIP_TIMEOUT)
            {
                XC_ERR("\033[1;31m[%s] DIP[%d] Timeout !!! \033[0m\n",__FUNCTION__,eWindow);
                return FALSE;
            }
            MsOS_DelayTask(1);
        }
    }

    return TRUE;
}

MS_BOOL _MApi_XC_DIP_DIPR_WidthSWPatchCheck(MS_BOOL *bSWPatchSupport, XC_SETWIN_INFO stXC_SetWin_Info, MS_BOOL bDIEna)
{
    MS_BOOL bScaling = FALSE;
    MS_BOOL bClip = FALSE;

    if(stXC_SetWin_Info.u16PreHCusScalingSrc != stXC_SetWin_Info.u16PreHCusScalingDst)
    {
        bScaling = TRUE;
    }
    else
    {
        bScaling = FALSE;
    }

    if(stXC_SetWin_Info.stCapWin.x != 0 )
    {
        bClip = TRUE;
    }

    if((bScaling == TRUE)||(bClip == TRUE)||(bDIEna == TRUE))
    {
        *bSWPatchSupport = FALSE;
    }
    else
    {
        *bSWPatchSupport = TRUE;
    }

    return TRUE;
}

MS_BOOL _MApi_XC_DIP_DIPW_SizeCheck(MS_BOOL *bBufferSizeCheck, ST_XC_DIP_WINPROPERTY stDIPWinProperty)
{
    MS_U16 u16Height =0;
    MS_U16 u16Pitch =0;
    MS_PHY phyBufStart =0;
    MS_PHY phyBufEnd = 0 ;
    MS_PHY phyBufSize = 0;
    MS_PHY phyNeedSize =0;

    u16Height = stDIPWinProperty.u16Height;
    u16Pitch = stDIPWinProperty.u16Pitch;
    phyBufStart = stDIPWinProperty.u32BufStart;
    phyBufEnd = stDIPWinProperty.u32BufEnd;
    phyBufSize = phyBufEnd - phyBufStart;

    phyNeedSize = ((MS_PHY)u16Pitch*(MS_PHY)u16Height);

    if(phyNeedSize > phyBufSize)
    {
        XC_ERR("[%s] Size is not enough, NeedSize(0x%tx) > BufSize(0x%tx)\n",__FUNCTION__,(ptrdiff_t)phyNeedSize,(ptrdiff_t)phyBufSize);
        *bBufferSizeCheck = FALSE;
    }
    else
    {
        *bBufferSizeCheck = TRUE;
    }

    return TRUE;
}

MS_BOOL _MApi_XC_DIP_DIPR_WidthSWPatch(SCALER_DIP_WIN eWindow)
{
    ST_DIPR_PATCH_INFO stDIPRWidthPatchInfo = {};
    MS_U16 u16DiprWidthHWMax = 0;
    MS_U16 u16DipwWidthHWMax = 0;
    MS_U16 u16WidthHWMax = 0;
    MS_U16 u16NeedPatchWidth = 0;
    MS_U16 u16LoopCnt = 0;
    MS_U16 u16Remainder = 0;
    MS_U16 u16idx = 0;
    MS_U16 u16BusSize = 0;
    MS_U16 u16CapsMinWidth = 0;
    ST_DIP_CAPS_BUS_SIZE* pstDIPCapsBusSize = NULL;
    MS_U16 u16bpp = 0;
    MS_BOOL bSWPatchSupport = FALSE;
    MS_BOOL bBufferSizeCheck = FALSE;
    ST_DIPR_FORMAT_INFO stDiprFormat = {};
    MS_BOOL bDIEna = FALSE;
#if DIP_INTERNAL_IRQ
    MS_S32 s32IrqStus = 0;
#endif

    stDIPRWidthPatchInfo.stDIPRProperty = _gstDiprProperty[eWindow];
    stDIPRWidthPatchInfo.stDIPR_MFDecInfo = _gstMfdInfo[eWindow];
    stDIPRWidthPatchInfo.stXC_SetWin_Info = _gstXCSetWinInfo[eWindow];
    stDIPRWidthPatchInfo.stDIPWinProperty = _gstDipwProperty[eWindow];
    stDIPRWidthPatchInfo.enDIPWFmt = _genDIPWFmt[eWindow];
    stDIPRWidthPatchInfo.enRdTileFmt = _genRdTileFmt[eWindow];
    bDIEna = _gbDIEna[eWindow];

    _MApi_XC_DIP_DIPR_WidthSWPatchCheck(&bSWPatchSupport,stDIPRWidthPatchInfo.stXC_SetWin_Info, bDIEna);
    if(bSWPatchSupport == FALSE)
    {
        XC_ERR("[%s] HW Not Support DIPR width(%d) is too large\n",__FUNCTION__,stDIPRWidthPatchInfo.stDIPRProperty.u16Width);
        return FALSE;
    }

    _MApi_XC_DIP_DIPW_SizeCheck(&bBufferSizeCheck,stDIPRWidthPatchInfo.stDIPWinProperty);
    if(bBufferSizeCheck == FALSE)
    {
        return FALSE;
    }
    _gbDIPRPatchFlowEna[eWindow] = TRUE;
#if DIP_INTERNAL_IRQ
    if(_gbAPIAttached == FALSE)
    {
        if(gbInternalIsrAttached == FALSE)
        {
            MsOS_AttachInterrupt(E_INT_IRQ_DIPW,_MApi_XC_DIP_CallBack);
            gbInternalIsrAttached = TRUE;
        }
    }
    _gbInternalIsrEna[eWindow] = TRUE;
#endif
    stDiprFormat.enDataFmt = _gstDiprProperty[eWindow].enDataFmt;
    stDiprFormat.enRdTileFmt = stDIPRWidthPatchInfo.enRdTileFmt;
    stDiprFormat.bMfdEna = stDIPRWidthPatchInfo.stDIPR_MFDecInfo.bMFDec_Enable;
    _MApi_XC_DIP_GetCapsDIPRMaxWidth(stDiprFormat, &u16DiprWidthHWMax, eWindow);
    _MApi_XC_DIP_GetCapsDIPWMaxWidth(_genDIPWFmt[eWindow], &u16DipwWidthHWMax, eWindow);
    u16WidthHWMax = MIN(u16DiprWidthHWMax,u16DipwWidthHWMax);
    stDIPRWidthPatchInfo.u16WriteWidth = u16WidthHWMax;
    u16NeedPatchWidth = stDIPRWidthPatchInfo.stDIPWinProperty.u16Width;
    u16LoopCnt = u16NeedPatchWidth/u16WidthHWMax;
    u16Remainder = u16NeedPatchWidth%u16WidthHWMax;
#if DIP_INTERNAL_IRQ
    if(_gbAPIAttached == FALSE)
    {
        s32IrqStus = MsOS_CheckInterrupt(E_INT_IRQ_DIPW);
        if(s32IrqStus == 0)
        {
            MsOS_EnableInterrupt(E_INT_IRQ_DIPW);
        }
        else
        {
            XC_ERR("[%s,%d]IRQ Already Enabled, IRQ status conflict \n",__FUNCTION__,__LINE__);
        }
    }
#endif

    for(u16idx = 0; u16idx<u16LoopCnt ; u16idx++)
    {
        MApi_XC_DIP_ClearInt(0xFF, eWindow);
        stDIPRWidthPatchInfo.u16OffsetWidth = u16WidthHWMax*u16idx;
#if DIP_INTERNAL_IRQ
        MsOS_ClearEvent(_gs32InternalIsrEventId[eWindow], DIP_FIRE_DONE);
        if((u16idx == (u16LoopCnt-1)) && (u16Remainder == 0))
        {
            _gbInternalIsrEna[eWindow] = FALSE;
            if(_gbAPIAttached == FALSE)
            {
                MsOS_DisableInterrupt(E_INT_IRQ_DIPW);
            }
        }
#endif
        _MApi_XC_DIP_DIPR_WidthSWPatchPartialFire(&stDIPRWidthPatchInfo, eWindow);
        _MApi_XC_DIP_DIPR_WaitWriteDone(eWindow);
        if((u16idx == (u16LoopCnt-1)) && (u16Remainder == 0))
        {
            _gbDIPRPatchFlowEna[eWindow] = FALSE;
        }
    }

    if(u16Remainder != 0)
    {
        MApi_XC_DIP_ClearInt(0xFF, eWindow);
#if DIP_INTERNAL_IRQ
        MsOS_ClearEvent(_gs32InternalIsrEventId[eWindow], DIP_FIRE_DONE);
#endif
        pstDIPCapsBusSize = (ST_DIP_CAPS_BUS_SIZE*)utopia_malloc(sizeof(ST_DIP_CAPS_BUS_SIZE));
        if(pstDIPCapsBusSize == NULL)
        {
            XC_ERR("%s ST_DIP_CAPS_BUS_SIZE alloc fail\n",__FUNCTION__);
#if DIP_INTERNAL_IRQ
            _gbInternalIsrEna[eWindow] = FALSE;
            if(_gbAPIAttached == FALSE)
            {
                MsOS_DisableInterrupt(E_INT_IRQ_DIPW);
            }
#endif
            _gbDIPRPatchFlowEna[eWindow] = FALSE;
            return FALSE;
        }
        pstDIPCapsBusSize->u32Version = ST_DIP_CAPS_BUS_SIZE_VERSION;
        pstDIPCapsBusSize->u32Length = sizeof(ST_DIP_CAPS_BUS_SIZE);
        MApi_XC_DIP_GetChipCaps(E_DIP_CAPS_BUS_SIZE, (void*)pstDIPCapsBusSize, eWindow);
        u16BusSize = pstDIPCapsBusSize->u16BusSize;
        stDIPRWidthPatchInfo.u16WriteWidth = (u16Remainder+(u16BusSize-1))&(~(u16BusSize-1));
        _MApi_XC_DIP_GetBPP(stDIPRWidthPatchInfo.stDIPRProperty.enDataFmt, &u16bpp);
        _MApi_XC_DIP_GetDIPRCapsMinWidth(u16bpp, &u16CapsMinWidth, eWindow);
        if(stDIPRWidthPatchInfo.u16WriteWidth < u16CapsMinWidth)
        {
            stDIPRWidthPatchInfo.u16WriteWidth = u16CapsMinWidth;
        }
        stDIPRWidthPatchInfo.u16OffsetWidth = u16NeedPatchWidth - stDIPRWidthPatchInfo.u16WriteWidth;
#if DIP_INTERNAL_IRQ
        _gbInternalIsrEna[eWindow] = FALSE;
        if(_gbAPIAttached == FALSE)
        {
            MsOS_DisableInterrupt(E_INT_IRQ_DIPW);
        }
#endif
        _MApi_XC_DIP_DIPR_WidthSWPatchPartialFire(&stDIPRWidthPatchInfo, eWindow);
        _MApi_XC_DIP_DIPR_WaitWriteDone(eWindow);
        _gbDIPRPatchFlowEna[eWindow] = FALSE;
        if(pstDIPCapsBusSize != NULL)
        {
            utopia_free(pstDIPCapsBusSize);
        }
    }

    return TRUE;
}

MS_BOOL _MApi_XC_DIP_DIPR_WidthHWSupport(MS_BOOL *bHWSupport, SCALER_DIP_WIN eWindow)
{
    MS_U16 u16DiprWidthHWMax = 0;
    MS_U16 u16DipwWidthHWMax = 0;
    MS_U16 u16DIPRPatchWidth = 0;
    MS_U16 u16DIPWPatchWidth = 0;
    SCALER_DIP_SOURCE_TYPE enSource = SCALER_DIP_SOURCE_TYPE_SUB2;
    ST_DIPR_FORMAT_INFO stDiprFormat;

    enSource = _gstDipwProperty[eWindow].enSource;
    if(enSource != SCALER_DIP_SOURCE_TYPE_DRAM)
    {
        *bHWSupport = TRUE;
        return TRUE;
    }
    //DIPR
    u16DIPRPatchWidth = _gstDiprProperty[eWindow].u16Width;

    memset(&stDiprFormat, 0, sizeof(ST_DIPR_FORMAT_INFO));
    stDiprFormat.enDataFmt = _gstDiprProperty[eWindow].enDataFmt;
    stDiprFormat.enRdTileFmt = _genRdTileFmt[eWindow];
    stDiprFormat.bMfdEna = _gstMfdInfo[eWindow].bMFDec_Enable;
    _MApi_XC_DIP_GetCapsDIPRMaxWidth(stDiprFormat, &u16DiprWidthHWMax, eWindow);
    //DIPW
    u16DIPWPatchWidth = _gstDipwProperty[eWindow].u16Width;

    _MApi_XC_DIP_GetCapsDIPWMaxWidth(_genDIPWFmt[eWindow], &u16DipwWidthHWMax, eWindow);

    if((u16DIPRPatchWidth > u16DiprWidthHWMax) || (u16DIPWPatchWidth > u16DipwWidthHWMax))
    {
        *bHWSupport = FALSE;
    }
    else
    {
        *bHWSupport = TRUE;
    }

    return TRUE;
}
#endif


MS_BOOL _Mapi_XC_DIP_GetXCStatus(ST_DIP_GET_XC_STATUS *pstGetXCStatus)
{
    MS_BOOL bRet = FALSE, bMainStatus = FALSE, bSubStatus = FALSE;
    XC_ApiStatusEx stXCStatusMain;
    XC_ApiStatusEx stXCStatusSub;

    memset(&stXCStatusMain, 0, sizeof(XC_ApiStatusEx));
    memset(&stXCStatusSub, 0, sizeof(XC_ApiStatusEx));

    stXCStatusMain.u32ApiStatusEx_Version =API_STATUS_EX_VERSION;
    stXCStatusMain.u16ApiStatusEX_Length = sizeof(stXCStatusMain);
    if(MApi_XC_GetStatusEx(&stXCStatusMain, MAIN_WINDOW) == sizeof(XC_ApiStatusEx))
    {
        pstGetXCStatus->u32Version = ST_DIP_GET_XC_STATUS_VERSION;
        pstGetXCStatus->u32Length = sizeof(ST_DIP_GET_XC_STATUS);
        pstGetXCStatus->u16MainCapWidth = stXCStatusMain.stCapWin.width;
        pstGetXCStatus->u16MainCapHeight = stXCStatusMain.stCapWin.height;
        pstGetXCStatus->bMainHskMode = stXCStatusMain.bIsHskMode;
        pstGetXCStatus->bMain2pEna = stXCStatusMain.bEnable1pTo2pBypass;

        pstGetXCStatus->u16MainDispDEWidth = stXCStatusMain.stDispDEWin.width;
        pstGetXCStatus->u16MainDispDEHeight = stXCStatusMain.stDispDEWin.height;
        pstGetXCStatus->u16MainOPMFetch = stXCStatusMain.u16OPMFetch;
        pstGetXCStatus->bMainHFRPathEn = stXCStatusMain.bIsHFRPathEnable;
        pstGetXCStatus->u16MainCropWidth = stXCStatusMain.stCropWin.width;
        pstGetXCStatus->u16MainCropHeight = stXCStatusMain.stCropWin.height;
        pstGetXCStatus->bIsOP120Hz = stXCStatusMain.bIs120HzOutput;
        bMainStatus = TRUE;
    }
    else
    {
        bMainStatus = FALSE;
        XC_ERR("%s Get XC Main Win Status fail\n",__FUNCTION__);
    }

    stXCStatusSub.u32ApiStatusEx_Version =API_STATUS_EX_VERSION;
    stXCStatusSub.u16ApiStatusEX_Length = sizeof(stXCStatusSub);
    if(MApi_XC_GetStatusEx(&stXCStatusSub, SUB_WINDOW) == sizeof(XC_ApiStatusEx))
    {
        pstGetXCStatus->u16SubCapWidth = stXCStatusSub.stCapWin.width;
        pstGetXCStatus->u16SubCapHeight = stXCStatusSub.stCapWin.height;

        pstGetXCStatus->u16SubDispDEWidth = stXCStatusSub.stDispDEWin.width;
        pstGetXCStatus->u16SubDispDEHeight = stXCStatusSub.stDispDEWin.height;
        pstGetXCStatus->u16SubOPMFetch = stXCStatusSub.u16OPMFetch;
        pstGetXCStatus->bSubHFRPathEn = stXCStatusMain.bIsHFRPathEnable;
        bSubStatus = TRUE;
    }
    else
    {
        bSubStatus = FALSE;
        XC_ERR("%s Get XC Sub Win Status fail\n",__FUNCTION__);
    }
    if((bMainStatus == FALSE)||(bSubStatus == FALSE))
    {
        bRet = FALSE;
    }
    else
    {
        bRet = TRUE;
    }

    return bRet;
}


MS_BOOL _MApi_XC_DIP_SetBuffer(ST_DIP_Buffer *pstBuf,SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
    ST_DIP_SET_BUFFER_INFO *pSetSetBuf = NULL;

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetSetBuf = (ST_DIP_SET_BUFFER_INFO *)utopia_malloc(sizeof(ST_DIP_SET_BUFFER_INFO));
    if (pSetSetBuf == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }
    pSetSetBuf->enBufType = pstBuf->enBufType;
    pSetSetBuf->phyBufAddr = pstBuf->phyBufAddr;
    pSetSetBuf->u32BufSize = pstBuf->u32BufSize;
    pSetSetBuf->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_BUFFER,(void*)pSetSetBuf) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_DATAFMT fail\n");
        utopia_free(pSetSetBuf);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetSetBuf);
#else

#endif
    return E_APIXC_RET_OK;
#else
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif
}

MS_BOOL _MApi_XC_DIP_ProcessMode(SCALER_DIP_WIN eWindow, EN_DIP_MODE enMode,void *pModeSettings)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
    ST_DIP_SET_PROCESS_MODE_INFO *pSetMode = NULL;

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetMode = (ST_DIP_SET_PROCESS_MODE_INFO *)utopia_malloc(sizeof(ST_DIP_SET_PROCESS_MODE_INFO));
    if (pSetMode == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }
    pSetMode->eWindow = eWindow;
    pSetMode->enMode = enMode;
    pSetMode->pModeSettings = pModeSettings;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_PROCESS_MODE,(void*)pSetMode) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_DATAFMT fail\n");
        utopia_free(pSetMode);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetMode);
#else

#endif
    return E_APIXC_RET_OK;
#else
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif
}

MS_BOOL _MApi_XC_DIP_SetXCPathInfo(SCALER_DIP_WIN eWindow,ST_DIP_XCPathInfo *pstXCInfo)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
    ST_DIP_SET_XCPATH_INFO *pstSetXCInfo = NULL;

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pstSetXCInfo = (ST_DIP_SET_XCPATH_INFO *)utopia_malloc(sizeof(ST_DIP_SET_XCPATH_INFO));
    if (pstSetXCInfo == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }
    pstSetXCInfo->eWindow = eWindow;
    memcpy(&(pstSetXCInfo->stXCInfo),pstXCInfo, sizeof(ST_DIP_XCPathInfo));
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_XC_PATH_INFO,(void*)pstSetXCInfo) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_DATAFMT fail\n");
        utopia_free(pstSetXCInfo);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pstSetXCInfo);
#else

#endif
    return E_APIXC_RET_OK;
#else
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif
}
MS_BOOL _MApi_XC_DIP_DIPW_SetHDMIInfo(INPUT_SOURCE_TYPE_t enInputSourceType, SCALER_DIP_WIN eWindow)
{
    ST_DIP_HDMI_Info *pstHDMIInfo = NULL;
    stCMD_HDMI_GET_TIMING_INFO stGetTiming;
    ST_XC_APISTATUSNODELAY stXCStatus;
    stCMD_HDMI_GET_TMDS_RATES_KHZ stGetHDMIRates;

    pstHDMIInfo = (ST_DIP_HDMI_Info*)utopia_malloc(sizeof(ST_DIP_HDMI_Info));
    if (pstHDMIInfo == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return FALSE;
    }
    memset(pstHDMIInfo , 0 ,sizeof(ST_DIP_HDMI_Info));

    if((enInputSourceType >= INPUT_SOURCE_HDMI) && (enInputSourceType<=INPUT_SOURCE_HDMI4))
    {
        pstHDMIInfo->enInputSourceType = enInputSourceType;
    }
    else
    {
        //XC Main window
        memset(&stXCStatus, 0 ,sizeof(ST_XC_APISTATUSNODELAY));
        MApi_XC_GetStatusNodelay(&stXCStatus,MAIN_WINDOW);
        pstHDMIInfo->enInputSourceType = stXCStatus.enInputSourceType;
    }
    //Get HDMI width
    memset(&stGetTiming, 0 ,sizeof(stCMD_HDMI_GET_TIMING_INFO));
    stGetTiming.enInfoType = E_HDMI_GET_HDE;
    stGetTiming.enInputPortType = (E_MUX_INPUTPORT)((pstHDMIInfo->enInputSourceType - INPUT_SOURCE_HDMI)+ INPUT_PORT_DVI0);
    MDrv_HDMI_Ctrl(E_HDMI_INTERFACE_CMD_GET_TIMING_INFO,&stGetTiming ,sizeof(stCMD_HDMI_GET_TIMING_INFO));
    pstHDMIInfo->u16Width = stGetTiming.u16TimingInfo;

    stGetTiming.enInfoType = E_HDMI_GET_VDE;
    stGetTiming.enInputPortType = (E_MUX_INPUTPORT)((pstHDMIInfo->enInputSourceType - INPUT_SOURCE_HDMI)+ INPUT_PORT_DVI0);
    MDrv_HDMI_Ctrl(E_HDMI_INTERFACE_CMD_GET_TIMING_INFO,&stGetTiming ,sizeof(stCMD_HDMI_GET_TIMING_INFO));
    pstHDMIInfo->u16Height = stGetTiming.u16TimingInfo;

    //Get HDMI clock rates
    memset(&stGetHDMIRates, 0 ,sizeof(stCMD_HDMI_GET_TMDS_RATES_KHZ));
    stGetHDMIRates.enInputPortType = (E_MUX_INPUTPORT)((pstHDMIInfo->enInputSourceType - INPUT_SOURCE_HDMI)+ INPUT_PORT_DVI0);
    stGetHDMIRates.enType = E_HDMI_GET_PIXEL_CLOCK_RATES;
    MDrv_HDMI_Ctrl(E_HDMI_INTERFACE_CMD_GET_TMDS_RATES_KHZ,&stGetHDMIRates ,sizeof(stCMD_HDMI_GET_TMDS_RATES_KHZ));

    //pstHDMIInfo->u16VsyncPos = MApi_XC_GetOffLineInfo(pstHDMIInfo->enInputSourceType);
    pstHDMIInfo->bIsHDMI2P1 = (MDrv_HDMI_GetSourceVersion(stGetHDMIRates.enInputPortType) == HDMI_SOURCE_VERSION_HDMI2P1);
    pstHDMIInfo->u16VsyncPos = FALSE;
    pstHDMIInfo->eWindow = eWindow;
    pstHDMIInfo->enSource = _genSource[eWindow];
    pstHDMIInfo->u32HDMIClkRate = stGetHDMIRates.ulRetRates;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_HDMI_INFO,(void*)pstHDMIInfo) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_HDMI_INFO fail\n");
        utopia_free(pstHDMIInfo);
        return FALSE;
    }
    utopia_free(pstHDMIInfo);

    return TRUE;
}

/////////////////////////////////////////
// Configuration
/////////////////////////////////////////
#if 0
// Path
typedef struct s_Data_Path
{

    //////////////////////////////
    // Path Type
    //////////////////////////////
    E_PATH_TYPE e_PathType;

    //////////////////////////////
    // Path Status
    //////////////////////////////

    /*
        Make the signal go through the path. If this flag is not enabled,
        synchronous_event_handler and source_monitor are useless.
     */
    #define DATA_PATH_ENABLE BIT(0)

    /*
        Used to identify a path is created or not. ( 1: path created )
     */
    #define DATA_PATH_USING BIT(1)

    /*
        Used to identify the priodic handler enabled or disabled
     */
    #define ENABLE_PERIODIC_HANDLER BIT(2)

    MS_U8 u8PathStatus;

    //////////////////////////////
    // A path has a format like Src -> Dest
    //////////////////////////////
    INPUT_SOURCE_TYPE_t src;
    E_DEST_TYPE dest;

    //////////////////////////////
    // thread for source.
    //////////////////////////////
    void (*path_thread)(INPUT_SOURCE_TYPE_t src, MS_BOOL bRealTimeMonitorOnly);

    ///////////////////////////////////////
    // Customize function.
    // This will be executed automatically when synchronous event triggered.
    ///////////////////////////////////////
    void (* synchronous_event_handler )(INPUT_SOURCE_TYPE_t src, void* para);

    ///////////////////////////////////////
    // Customize function.
    // This will be executed automatically when destination on/off event triggered.
    ///////////////////////////////////////
    void (* dest_on_off_event_handler )(INPUT_SOURCE_TYPE_t src, void* para);

    ///////////////////////////////////////
    // Customize function.
    // This will be executed periodically after enabled destinatio period handler
    ///////////////////////////////////////
    void (* dest_periodic_handler )(INPUT_SOURCE_TYPE_t src, MS_BOOL bRealTimeMonitorOnly);
}MUX_DATA_PATH;
#endif

#if 0
// Path Collection
typedef struct s_Mux_InputSource_Attr
{
    // The number of pathes which is used right now.
    MS_U8  u8PathCount;

    void (*input_source_to_input_port)(INPUT_SOURCE_TYPE_t src_ids , E_MUX_INPUTPORT* port_ids , MS_U8* u8port_count );
    // Pathes.
    // Path array in MAPP_MUX is constructed [Sync path, Sync path, ..., Async path, Async path]
    MUX_DATA_PATH Mux_DataPaths[MAX_DATA_PATH_SUPPORTED];

    // indicate how many times the src created
    MS_U16 u16SrcCreateTime[INPUT_SOURCE_NUM];
}InputSource_Mux_Attr;

#endif
E_APIXC_ReturnValue MApi_XC_DIP_QueryResource(SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
    DIP_RESOURCE* pResource = NULL;
    pResource = (DIP_RESOURCE*)utopia_malloc(sizeof(DIP_RESOURCE));
    if (pResource == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }
    if(pInstantDip[eWindow] == NULL)
    {
        if(UtopiaOpen(MODULE_DIP|DIPDRIVER_BASE,(void**)&pInstantDip[eWindow], 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            XC_ERR("Open DIP fail\n");
            utopia_free(pResource);
            return E_APIXC_RET_FAIL;
        }
    }
    //Query Resource
    pResource->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_QUERY_RESOURCE,(void* )pResource) == UTOPIA_STATUS_NO_RESOURCE)
    {
        utopia_free(pResource);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pResource);
#else

#endif
    return E_APIXC_RET_OK;
#else
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

E_APIXC_ReturnValue MApi_XC_DIP_GetResource(SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
    DIP_RESOURCE* pResource = NULL;
    pResource = (DIP_RESOURCE*)utopia_malloc(sizeof(DIP_RESOURCE));
    if (pResource == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }
    if(pInstantDip[eWindow] == NULL)
    {
        if(UtopiaOpen(MODULE_DIP|DIPDRIVER_BASE,(void**)&pInstantDip[eWindow], 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            XC_ERR("Open DIP fail\n");
            utopia_free(pResource);
            return E_APIXC_RET_FAIL;
        }
    }
    //Get Resource
    pResource->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_GET_RESOURCE,(void*)pResource) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_GET_RESOURCE fail\n");
        utopia_free(pResource);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pResource);
#else

#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif
}

E_APIXC_ReturnValue MApi_XC_DIP_ReleaseResource(SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
    DIP_RESOURCE* pResource = NULL;
    pResource = (DIP_RESOURCE*)utopia_malloc(sizeof(DIP_RESOURCE));
    if (pResource == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }
    if(pInstantDip[eWindow] == NULL)
    {
        if(UtopiaOpen(MODULE_DIP|DIPDRIVER_BASE,(void**)&pInstantDip[eWindow], 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            XC_ERR("Open DIP fail\n");
            utopia_free(pResource);
            return E_APIXC_RET_FAIL;
        }
    }
    //Free Resource
    pResource->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_RELEASE_RESOURCE,(void*)pResource) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_GET_RESOURCE fail\n");
        utopia_free(pResource);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pResource);

#ifdef MFD_MODULE_ENABLE
    if( ((Mfdec_Res & MFD_RES_TYPE_IDX_MASK) != MFD_RES_TYPE_IDX_NONE) && (Mfdec_DIP_WIN == eWindow) )
    {
        MDrv_MFD_ResFree(Mfdec_Res);
        Mfdec_Res = MFD_RES_TYPE_IDX_NONE;
        Mfdec_DIP_WIN = MAX_DIP_WINDOW;
    }
#endif

#else

#endif
    return E_APIXC_RET_OK;
#else
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

E_APIXC_ReturnValue MApi_XC_DIP_CMDQ_SetAction(EN_XC_DIP_CMDQ_ACTION eAction, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
    DIP_CMDQ_SET_ACTION* pResource = NULL;
    pResource = (DIP_CMDQ_SET_ACTION*)utopia_malloc(sizeof(DIP_CMDQ_SET_ACTION));
    if (pResource == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }

    if(pInstantDip[eWindow] == NULL)
    {
        if(UtopiaOpen(MODULE_DIP,(void**)&pInstantDip[eWindow], 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            XC_ERR("Open DIP fail\n");
            utopia_free(pResource);
            return E_APIXC_RET_FAIL;
        }
    }

    pResource->eWindow = eWindow;
    pResource->eAction = eAction;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_CMDQ_SETACTION,(void*)pResource) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_GET_RESOURCE fail\n");
        utopia_free(pResource);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pResource);
#else
    MDrv_XC_DIP_CMDQ_SetAction(NULL, eAction, eWindow);
#endif
    return E_APIXC_RET_OK;
#else
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//MS_BOOL PreScaling = FALSE;
//-------------------------------------------------------------------------------------------------
/// Reset DIP for restart capture from buffer 0
/// @return E_APIXC_RET_OK - Success
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_DIP_SWReset(SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_TRIGGER_MODE* pTrigMode = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        return E_APIXC_RET_OK;
    }
#endif
    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }

    pTrigMode = (DIP_TRIGGER_MODE*)utopia_malloc(sizeof(DIP_TRIGGER_MODE));
    if (pTrigMode == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pTrigMode->eTrigMode = DIP_TRIG_RESET;
	pTrigMode->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_TRIGGER,(void*)pTrigMode) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_TRIGGER DIP_TRIG_RESET fail\n");
		utopia_free(pTrigMode);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pTrigMode);
#else
	MDrv_XC_DIP_SWReset(NULL,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}


//-------------------------------------------------------------------------------------------------
/// Init the status for DIP path
/// @return E_APIXC_RET_OK - Success
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_DIP_InitByDIP(SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_RESOURCE* pResource = NULL;
#if DIPR_WIDTH_PATCH
    _MApi_XC_DIP_SaveTileFmt(DIP_TILE_BLOCK_R_16_32,eWindow);
    _MApi_XC_DIP_SaveTileFmt(DIP_TILE_BLOCK_W_NONE,eWindow);
#endif

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        MApi_GOP_DWIN_Init();
        return E_APIXC_RET_OK;
    }
#endif
    pResource = (DIP_RESOURCE*)utopia_malloc(sizeof(DIP_RESOURCE));
    if (pResource == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    if(pInstantDip[eWindow] == NULL)
    {
        if(UtopiaOpen(MODULE_DIP|DIPDRIVER_BASE,(void**)&pInstantDip[eWindow], 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            XC_ERR("Open DIP fail\n");
            utopia_free(pResource);
            return E_APIXC_RET_FAIL;
        }
    }
	//init
	pResource->eWindow = eWindow;
	if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_INIT,(void*)pResource) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_INIT fail\n");
		utopia_free(pResource);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pResource);

#if (DIPR_WIDTH_PATCH&&DIP_INTERNAL_IRQ)
    if(eWindow >= MAX_DIP_WINDOW)
    {
        return E_APIXC_RET_FAIL;
    }

    if(eWindow == DIP_WINDOW)
    {
        if(_gs32InternalIsrEventId[eWindow] == (-1))
        {
            _gs32InternalIsrEventId[eWindow] = MsOS_CreateEventGroup("InternalIsr_DIP_WINDOW");

        }
    }
    else if(eWindow == DWIN0_WINDOW)
    {
        if(_gs32InternalIsrEventId[eWindow] == (-1))
        {
            _gs32InternalIsrEventId[eWindow] = MsOS_CreateEventGroup("InternalIsr_DWIN0_WINDOW");
        }
    }
    else if(eWindow == DWIN1_WINDOW)
    {
        if(_gs32InternalIsrEventId[eWindow] == (-1))
        {
            _gs32InternalIsrEventId[eWindow] = MsOS_CreateEventGroup("InternalIsr_DWIN1_WINDOW");
        }
    }
    else
    {
        XC_ERR("%s,Invalid eWindow %d \n",__func__,eWindow);
        return E_APIXC_RET_FAIL;
    }
    if(_gs32InternalIsrEventId[eWindow] < (0))
    {
        XC_ERR("\033[1;31m[%s] DIP[%d] Create IRQ Event Fail !!! \033[0m\n",__FUNCTION__,eWindow);
        return E_APIXC_RET_FAIL;
    }
#endif

#else
	MDrv_XC_DIP_Init(NULL,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//-------------------------------------------------------------------------------------------------
///Set the frame buffer address for specific window
/// @param  u32FBAddress      \b IN: the frame buffer address
/// @param  u32FBSize           \b IN: the frame buffer size
/// @param  eWindow             \b IN: set address for which window(DIP_WINDOW)
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_DIP_SetFrameBufferAddress(MS_PHY u32FBAddress, MS_PHY u32FBSize, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2

#else
    MDrv_XC_DWIN_Disable_IPM_ReadWriteRequest(NULL,DISABLE, eWindow);
    MDrv_XC_DWIN_SetFrameBufferAddress(NULL,u32FBAddress, u32FBSize, eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//******************************************************************************
/// API for specify input source type for DIP path
/// @param enInputSourceType                  \b IN: the inputsource type
/// @param  eWindow                                \b IN:  for which window(DIP_WINDOW)
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_SetInputSource(INPUT_SOURCE_TYPE_t enInputSourceType, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2

#else
	MDrv_XC_DWIN_SetInputSource(NULL,enInputSourceType, eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//******************************************************************************
/// API for get input source type for DIP path
/// @param enInputSourceType                  \b OUT: the inputsource type
/// @param  eWindow                                \b IN:  for which window(DIP_WINDOW)
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_GetInputSource(INPUT_SOURCE_TYPE_t *enInputSourceType, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2

#else
    MDrv_XC_DWIN_GetInputSource(NULL,enInputSourceType,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//******************************************************************************
/// API for enable or disable input source for DIP path
/// @param bEnable                  \b IN: enable or disable inputsource
/// @param  eWindow               \b IN:  for which window(DIP_WINDOW)
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_DisableInputSource(MS_BOOL bDisable, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2

#else
    MDrv_XC_DWIN_DisableInputSource(NULL,bDisable, eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

E_APIXC_ReturnValue MApi_XC_DIP_SetSourceInfo(ST_DIP_SET_SOURCE_INFO *pstSetSrcInfo, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
    DIP_SET_SRC_INFO_WINDOW* pstSetSrcInfoWindow = NULL;

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pstSetSrcInfoWindow = (DIP_SET_SRC_INFO_WINDOW*)utopia_malloc(sizeof(DIP_SET_SRC_INFO_WINDOW));
    if (pstSetSrcInfoWindow == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }

    memcpy(&(pstSetSrcInfoWindow->stDIPSetSrcInfo),pstSetSrcInfo, sizeof(ST_DIP_SET_SOURCE_INFO));
    pstSetSrcInfoWindow->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_SRC_INFO,(MS_U32* )pstSetSrcInfoWindow) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_SRC_INFO fail\n");
        utopia_free(pstSetSrcInfoWindow);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pstSetSrcInfoWindow);

    return E_APIXC_RET_OK;
#else
    MDrv_XC_DIP_SetSourceInfo(NULL,pstSetSrcInfo,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//-------------------------------------------------------------------------------------------------
/// 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 E_APIXC_ReturnValue
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_DIP_SetWindow(XC_SETWIN_INFO *pstXC_SetWin_Info, MS_U32 u32InitDataLen, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION
    MS_U32 u32CopiedLength = 0;

#ifdef UTOPIA_V2
	DIP_SET_WINDOW* pSetWindow = NULL;
    ST_DIP_GET_XC_STATUS* pstGetXCStatus = NULL;
#if DIPR_WIDTH_PATCH
    if(eWindow >= MAX_DIP_WINDOW)
    {
        return E_APIXC_RET_FAIL;
    }
    if(_gbDIPRPatchFlowEna[eWindow] == FALSE)
    {
        _gstXCSetWinInfo[eWindow] = *pstXC_SetWin_Info;
    }
#endif
#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        GopD_property.u16x = pstXC_SetWin_Info->stCapWin.x;
        GopD_property.u16y = pstXC_SetWin_Info->stCapWin.y;
        if( pstXC_SetWin_Info->u16PreHCusScalingSrc != pstXC_SetWin_Info->u16PreHCusScalingDst ){
            XC_ERR("eWindow[%d] doesnt support Hscaling src[%d] dst[%d]\n",eWindow,pstXC_SetWin_Info->u16PreHCusScalingSrc,pstXC_SetWin_Info->u16PreHCusScalingDst);
            return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
        }
        if( pstXC_SetWin_Info->u16PreVCusScalingSrc != pstXC_SetWin_Info->u16PreVCusScalingDst ){
            XC_ERR("eWindow[%d] doesnt support Vscaling src[%d] dst[%d]\n",eWindow,pstXC_SetWin_Info->u16PreVCusScalingSrc,pstXC_SetWin_Info->u16PreVCusScalingDst);
            return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
        }
        return E_APIXC_RET_OK;
    }
#endif
    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }

    pstGetXCStatus = (ST_DIP_GET_XC_STATUS*)utopia_malloc(sizeof(ST_DIP_GET_XC_STATUS));
    if (pstGetXCStatus == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }
    _Mapi_XC_DIP_GetXCStatus(pstGetXCStatus);
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_FILL_XC_API_INFO,(void*)pstGetXCStatus) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_FILL_XC_API_INFO fail\n");
        utopia_free(pstGetXCStatus);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pstGetXCStatus);


    if(_genSource[eWindow] == SCALER_DIP_SOURCE_TYPE_HDMI)
    {
        if(_MApi_XC_DIP_DIPW_SetHDMIInfo(pstXC_SetWin_Info->enInputSourceType,eWindow) == FALSE)
        {
            XC_WARN("[%s][%d] _MApi_XC_DIP_DIPW_SetHDMIInfo fail\n",__FUNCTION__,__LINE__);
        }
    }


    pSetWindow = (DIP_SET_WINDOW*)utopia_malloc(sizeof(DIP_SET_WINDOW));
    if (pSetWindow == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }

    memset(pSetWindow, 0, sizeof(DIP_SET_WINDOW));
    u32CopiedLength = sizeof(XC_SETWIN_INFO);

    if(u32InitDataLen < sizeof(XC_SETWIN_INFO))
    {
#if defined (__aarch64__)
        XC_WARN("[%s]XC_SETWIN_INFO length:%lu. New version of DIP lib shouldn't access the space which doesn't exist in old structure\n",__FUNCTION__,sizeof(XC_SETWIN_INFO));
#else
        XC_WARN("[%s]XC_SETWIN_INFO length:%u. New version of DIP lib shouldn't access the space which doesn't exist in old structure\n",__FUNCTION__,sizeof(XC_SETWIN_INFO));
#endif
        u32CopiedLength = u32InitDataLen;
    }

    if(u32InitDataLen > sizeof(XC_SETWIN_INFO))
    {
#if defined (__aarch64__)
        XC_WARN("[%s] The length of structure XC_SETWIN_INFO in old version of DIP lib only has %lu\n",__FUNCTION__,sizeof(XC_SETWIN_INFO));
#else
        XC_WARN("[%s] The length of structure XC_SETWIN_INFO in old version of DIP lib only has %u\n",__FUNCTION__,sizeof(XC_SETWIN_INFO));
#endif
    }

    pSetWindow->u32DataLen = u32CopiedLength;
    memcpy(&(pSetWindow->WinInfo),pstXC_SetWin_Info, u32CopiedLength);
	pSetWindow->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_WINDOW,(void*)pSetWindow) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_WINDOW fail\n");
		utopia_free(pSetWindow);
        return E_APIXC_RET_FAIL;
    }
    pstXC_SetWin_Info->u16PreHCusScalingDst=pSetWindow->WinInfo.u16PreHCusScalingDst;
    utopia_free(pSetWindow);

    return E_APIXC_RET_OK;
#else
    XC_SETWIN_INFO* pstSetDIPWindow = NULL;
    pstSetDIPWindow = (XC_SETWIN_INFO*)utopia_malloc(sizeof(XC_SETWIN_INFO));
    memset(pstSetDIPWindow, 0, sizeof(XC_SETWIN_INFO));
    u32CopiedLength = sizeof(XC_SETWIN_INFO);

    if(u32InitDataLen < sizeof(XC_SETWIN_INFO))
    {
#if defined (__aarch64__)
        XC_WARN("[%s]XC_SETWIN_INFO length:%lu. New version of DIP lib shouldn't access the space which doesn't exist in old structure\n",__FUNCTION__,sizeof(XC_SETWIN_INFO));
#else
        XC_WARN("[%s]XC_SETWIN_INFO length:%u. New version of DIP lib shouldn't access the space which doesn't exist in old structure\n",__FUNCTION__,sizeof(XC_SETWIN_INFO));
#endif
        u32CopiedLength = u32InitDataLen;
    }

    if(u32InitDataLen > sizeof(XC_SETWIN_INFO))
    {
#if defined (__aarch64__)
        XC_WARN("[%s] The length of structure XC_SETWIN_INFO in old version of DIP lib only has %lu\n",__FUNCTION__,sizeof(XC_SETWIN_INFO));
#else
        XC_WARN("[%s] The length of structure XC_SETWIN_INFO in old version of DIP lib only has %u\n",__FUNCTION__,sizeof(XC_SETWIN_INFO));
#endif
    }

    memcpy(pstSetDIPWindow,pstXC_SetWin_Info, u32CopiedLength);
    MDrv_XC_DIP_SetWindow(NULL,pstSetDIPWindow,u32CopiedLength,eWindow);
    pstXC_SetWin_Info->u16PreHCusScalingDst=pstSetDIPWindow->u16PreHCusScalingDst;
    utopia_free(pstSetDIPWindow);
#endif
	return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//******************************************************************************
/// API to get DE window for DIP path
/// @param psWin                  \b OUT: the DE window info to get
/// @param  eWindow             \b IN:  for which window(DIP_WINDOW)
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_GetDEWindow(MS_WINDOW_TYPE *psWin, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2

#else
    MDrv_XC_DWIN_get_DE_Window(NULL,psWin, eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//---------------------------------

//---- Aileen
//-------------------------------------------------------------------------------------------------
/// Set the Vertical start of capture window for specific window
/// @param  u16Vstart        \b IN: the window info to get
/// @param  eWindow          \b IN: for which window(DIP_WINDOW) to set
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_DIP_SetCaptureWindowVstart(MS_U16 u16Vstart , SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2

#else
    MDrv_XC_DWIN_set_capture_v_start(NULL,u16Vstart, eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//-------------------------------------------------------------------------------------------------
/// Set the horizontal start of capture window for specific window
/// @param  u16Hstart        \b IN: the window info to get
/// @param  eWindow          \b IN: which window(DIP_WINDOW) to set
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_DIP_SetCaptureWindowHstart(MS_U16 u16Hstart , SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2

#else
    MDrv_XC_DWIN_set_capture_h_start(NULL,u16Hstart, eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//-------------------------------------------------------------------------------------------------
/// Set the Vertical size of capture window for specific window
/// @param  u16Vsize         \b IN: the window info to get
/// @param  eWindow          \b IN: which window(DIP_WINDOW) to set
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_DIP_SetCaptureWindowVsize(MS_U16 u16Vsize , SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2

#else
    MDrv_XC_DWIN_set_capture_v_size(NULL,u16Vsize, eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//-------------------------------------------------------------------------------------------------
/// Set the Horizontal size of capture window for specific window
/// @param  u16Hsize         \b IN: the window info to get
/// @param  eWindow          \b IN: which window(DIP_WINDOW) to set
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_DIP_SetCaptureWindowHsize(MS_U16 u16Hsize , SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2

#else
    MDrv_XC_DWIN_set_capture_h_size(NULL,u16Hsize, eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//-------------------------------------------------------------------------------------------------
/// Get the capture window for specific window
/// @param  capture_win      \b OUT: the window info to get
/// @param  eWindow          \b IN: which window(DIP_WINDOW) to get
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_DIP_GetCaptureWindow(MS_WINDOW_TYPE* capture_win, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2

#else
    MDrv_XC_DWIN_get_capture_window(NULL,capture_win, eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//******************************************************************************
/// API for enable or disable RGB to YUV color space convertion
/// @param bEnable \b IN: enable or disable
/// @param  eWindow          \b IN: which window(DIP_WINDOW) to set
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_EnableR2Y(MS_BOOL benable, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_SET_PROPERTY* pSetProperty = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        MApi_GOP_DWIN_EnableR2YCSC(benable);
        return E_APIXC_RET_OK;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetProperty = (DIP_SET_PROPERTY*)utopia_malloc(sizeof(DIP_SET_PROPERTY));
    if (pSetProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pSetProperty->Property.bR2Y = benable;
    pSetProperty->eFlag = DIP_PRO_R2Y;
	pSetProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_PROPERTY,(void*)pSetProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_R2Y fail\n");
		utopia_free(pSetProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetProperty);
#else
    MDrv_XC_DWIN_EnableR2YCSC(NULL,benable, eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//=============== DIP =====================//
//-------------------------------------------------------------------------------------------------
/// Set  FrameRateCtrl
/// @param  bEnable           \b IN: FrameRateCtrl enable or not
/// @param  u16Src             \b IN: Src Frame
/// @param  u16Dst            \b IN: Dst Frame
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_DIP_FrameRateCtrl(MS_BOOL bEnable,MS_U16 u16In,MS_U16 u16Out, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_SET_PROPERTY* pSetProperty = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        XC_ERR("eWindow[%d] doesnt support frame rate cnt\n",eWindow);
        return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetProperty = (DIP_SET_PROPERTY*)utopia_malloc(sizeof(DIP_SET_PROPERTY));
    if (pSetProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pSetProperty->Property.stFrameRateControl.bFrameRateCtrl = bEnable;
    pSetProperty->Property.stFrameRateControl.u16In = u16In;
    pSetProperty->Property.stFrameRateControl.u16Out = u16Out;
    pSetProperty->eFlag = DIP_PRO_FRC;
	pSetProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_PROPERTY,(void*)pSetProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_FRC fail\n");
		utopia_free(pSetProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetProperty);
#else
	MDrv_XC_DIP_SetFRC(NULL,bEnable,u16In,u16Out,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//-------------------------------------------------------------------------------------------------
/// Set DIP win property
/// @param  pstDIPWinProperty     \b IN: @ref ST_DIP_WINPROPERTY
/// @param  eWindow         \b IN: the window
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_DIP_SetDIPWinProperty(ST_XC_DIP_WINPROPERTY *pstDIPWinProperty, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_SET_WIN_PROPERTY* pSetWinProperty = NULL;
    ST_DIP_GET_XC_STATUS* pstGetXCStatus = NULL;
#if DIPR_WIDTH_PATCH
    if(eWindow >= MAX_DIP_WINDOW)
    {
        return E_APIXC_RET_FAIL;
    }
    if(_gbDIPRPatchFlowEna[eWindow] == FALSE)
    {
        _gstDipwProperty[eWindow] = *pstDIPWinProperty;
    }
#endif

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        GopD_property.u16w = pstDIPWinProperty->u16Width;
        GopD_property.u16h = pstDIPWinProperty->u16Height;
        if( (DataFmt == DIP_DATA_FMT_YUV422) || (DataFmt == DIP_DATA_FMT_RGB565))
            GopD_property.u16fbw = pstDIPWinProperty->u16Pitch/2;
        else if(DataFmt == DIP_DATA_FMT_ARGB8888)
            GopD_property.u16fbw = pstDIPWinProperty->u16Pitch/4;
        GopD_property.u32fbaddr0 = pstDIPWinProperty->u32BufStart;
        GopD_property.u32fbaddr1 = pstDIPWinProperty->u32BufEnd;

        if(pstDIPWinProperty->enSource == SCALER_DIP_SOURCE_TYPE_MAIN)
            MApi_GOP_DWIN_SetSourceSel(DWIN_SRC_IP);
        else if(pstDIPWinProperty->enSource == SCALER_DIP_SOURCE_TYPE_OP_CAPTURE)
            MApi_GOP_DWIN_SetSourceSel(DWIN_SRC_OP);
        else{
            XC_ERR("eWindow[%d] doesnt support capture source[%d]\n",eWindow,pstDIPWinProperty->enSource);
            return E_APIXC_RET_FAIL_ENUM_NOT_SUPPORT;
        }
        MApi_GOP_DWIN_SetWinProperty(&GopD_property);
        return E_APIXC_RET_OK;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }

    pstGetXCStatus = (ST_DIP_GET_XC_STATUS*)utopia_malloc(sizeof(ST_DIP_GET_XC_STATUS));
    if (pstGetXCStatus == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }
    _Mapi_XC_DIP_GetXCStatus(pstGetXCStatus);
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_FILL_XC_API_INFO,(void*)pstGetXCStatus) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_FILL_XC_API_INFO fail\n");
        utopia_free(pstGetXCStatus);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pstGetXCStatus);


    _genSource[eWindow] = pstDIPWinProperty->enSource;


    pSetWinProperty = (DIP_SET_WIN_PROPERTY*)utopia_malloc(sizeof(DIP_SET_WIN_PROPERTY));
    if (pSetWinProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    memcpy(&(pSetWinProperty->WinProperty),pstDIPWinProperty, sizeof(ST_XC_DIP_WINPROPERTY));
	pSetWinProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_WINPROPERTY,(void*)pSetWinProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_WINPROPERTY fail\n");
		utopia_free(pSetWinProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetWinProperty);
#else
	MDrv_XC_DIP_SetWinProperty(NULL,pstDIPWinProperty, eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//-------------------------------------------------------------------------------------------------
/// Get DWIN GetBufferInfo
//-------------------------------------------------------------------------------------------------
BUFFER_INFO MApi_XC_DIP_GetBufInfo(SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	BUFFER_INFO BufInfo;
    DIP_BUF_INFO* pBufInfo = NULL;
	DIP_RESOURCE* pResource = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        BufInfo.u8BufCnt = 1;
        BufInfo.u32YBuf[0] = GopD_property.u32fbaddr0;
        return BufInfo;
    }
#endif

    memset(&BufInfo, 0, sizeof(BUFFER_INFO));
    if(pInstantDip[eWindow] == NULL)
    {
    	pResource = (DIP_RESOURCE*)utopia_malloc(sizeof(DIP_RESOURCE));
		if(UtopiaOpen(MODULE_DIP|DIPDRIVER_BASE,(void**)&pInstantDip[eWindow], 0, NULL) != UTOPIA_STATUS_SUCCESS)
		{
			XC_ERR("Open DIP fail\n");
			utopia_free(pResource);
			return BufInfo;
		}
		utopia_free(pResource);
    }

    pBufInfo = (DIP_BUF_INFO*)utopia_malloc(sizeof(DIP_BUF_INFO));
    if (pBufInfo == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return BufInfo;
    }
	memset(&(pBufInfo->BufInfo), 0, sizeof(BUFFER_INFO));
	pBufInfo->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_GET_BUFINFO,(void*)pBufInfo) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_GET_BUFINFO fail\n");
		utopia_free(pBufInfo);
        return BufInfo;
    }
    memcpy(&BufInfo,&(pBufInfo->BufInfo), sizeof(BUFFER_INFO));
    utopia_free(pBufInfo);
    return BufInfo;
#else
	return MDrv_XC_DIP_GetBufferInfo(NULL,eWindow);
#endif
#else
    BUFFER_INFO BufInfo;
    memset(&BufInfo, 0, sizeof(BUFFER_INFO));
	return BufInfo;
#endif

}

/********************************************************************************/
/// Set gop dwin capture one frame
/// @return E_APIXC_RET_OK - Success
/********************************************************************************/
E_APIXC_ReturnValue MApi_XC_DIP_CapOneFrame(SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_TRIGGER_MODE* pTrigMode = NULL;
#if DIPR_WIDTH_PATCH
    MS_BOOL bHWSupport = FALSE;
    MS_BOOL bRet = FALSE;
#endif

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        MApi_GOP_DWIN_CaptureOneFrame();
        return E_APIXC_RET_OK;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }

    pTrigMode = (DIP_TRIGGER_MODE*)utopia_malloc(sizeof(DIP_TRIGGER_MODE));
    if (pTrigMode == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pTrigMode->eTrigMode = DIP_TRIG_ONESHOT;
	pTrigMode->eWindow = eWindow;
#if DIPR_WIDTH_PATCH
    _MApi_XC_DIP_DIPR_WidthHWSupport(&bHWSupport, eWindow);
    if(bHWSupport == TRUE)
    {
        if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_TRIGGER,(void*)pTrigMode) != UTOPIA_STATUS_SUCCESS)
        {
            XC_ERR("Ioctl MDrv_CMD_DIP_TRIGGER DIP_TRIG_ONESHOT fail\n");
            utopia_free(pTrigMode);
            return E_APIXC_RET_FAIL;
        }
    }
    else
    {
        bRet = _MApi_XC_DIP_DIPR_WidthSWPatch(eWindow);
        if(bRet == FALSE)
        {
            if(pTrigMode != NULL)
            {
                utopia_free(pTrigMode);
            }
            return E_APIXC_RET_FAIL_INVALID_PARAMETER;
        }
    }
#else
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_TRIGGER,(void*)pTrigMode) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_TRIGGER DIP_TRIG_ONESHOT fail\n");
		utopia_free(pTrigMode);
        return E_APIXC_RET_FAIL;
    }
#endif
    utopia_free(pTrigMode);
#else
    MDrv_XC_DIP_CaptureOneFrame(NULL,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

/********************************************************************************/
/// Set XC dwin capture one frame
/// @return E_APIXC_RET_OK - Success
/********************************************************************************/
E_APIXC_ReturnValue MApi_XC_DIP_CapOneFrameFast(SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_TRIGGER_MODE* pTrigMode = NULL;
#if DIPR_WIDTH_PATCH
    MS_BOOL bHWSupport = FALSE;
    MS_BOOL bRet = FALSE;
#endif
#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        MApi_GOP_DWIN_CaptureOneFrame2();
        return E_APIXC_RET_OK;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }

    pTrigMode = (DIP_TRIGGER_MODE*)utopia_malloc(sizeof(DIP_TRIGGER_MODE));
    if (pTrigMode == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pTrigMode->eTrigMode = DIP_TRIG_ONESHOTFAST;
	pTrigMode->eWindow = eWindow;
#if DIPR_WIDTH_PATCH
    _MApi_XC_DIP_DIPR_WidthHWSupport(&bHWSupport, eWindow);
    if(bHWSupport == TRUE)
    {
        if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_TRIGGER,(void*)pTrigMode) != UTOPIA_STATUS_SUCCESS)
        {
            XC_ERR("Ioctl MDrv_CMD_DIP_TRIGGER DIP_TRIG_ONESHOTFAST fail\n");
            utopia_free(pTrigMode);
            return E_APIXC_RET_FAIL;
        }
    }
    else
    {
        bRet = _MApi_XC_DIP_DIPR_WidthSWPatch(eWindow);
        if(bRet == FALSE)
        {
            if(pTrigMode != NULL)
            {
                utopia_free(pTrigMode);
            }
            return E_APIXC_RET_FAIL_INVALID_PARAMETER;
        }
    }
#else
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_TRIGGER,(void*)pTrigMode) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_TRIGGER DIP_TRIG_ONESHOTFAST fail\n");
		utopia_free(pTrigMode);
        return E_APIXC_RET_FAIL;
    }
#endif
    utopia_free(pTrigMode);
#else
    MDrv_XC_DIP_CaptureOneFrame2(NULL,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//******************************************************************************
/// clear DWIN interrupt
/// @param u8mask \b IN: interrup mask
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_ClearInt(MS_U16 u8mask, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_INTER_STUS* pIntrStus = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        MApi_GOP_DWIN_ClearIntr(u8mask);
        return E_APIXC_RET_OK;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_OK;
    }

    pIntrStus = (DIP_INTER_STUS*)utopia_malloc(sizeof(DIP_INTER_STUS));
    if (pIntrStus == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pIntrStus->IntrStautus = u8mask;
    pIntrStus->eFlag = DIP_INTR_CLEAR;
	pIntrStus->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_INTERRUPT,(void*)pIntrStus) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_INTERRUPT DIP_INTR_CLEAR fail\n");
		utopia_free(pIntrStus);
        return E_APIXC_RET_OK;
    }
    utopia_free(pIntrStus);
#else
    MDrv_XC_DIP_ClearIntr(NULL,u8mask,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//******************************************************************************
/// API for enable or disable DWIN
/// @param bEnable \b IN DWIN enable or not
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_Ena(MS_BOOL bEnable, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_TRIGGER_MODE* pTrigMode = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        MApi_GOP_DWIN_Enable(bEnable);
        return E_APIXC_RET_OK;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }

    pTrigMode = (DIP_TRIGGER_MODE*)utopia_malloc(sizeof(DIP_TRIGGER_MODE));
    if (pTrigMode == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }
    if(bEnable)
        pTrigMode->eTrigMode = DIP_TRIG_ON;
    else
        pTrigMode->eTrigMode = DIP_TRIG_OFF;
    pTrigMode->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_TRIGGER,(void*)pTrigMode) != UTOPIA_STATUS_SUCCESS)
    {
        if(bEnable)
        {
            XC_ERR("Ioctl MDrv_CMD_DIP_TRIGGER DIP_TRIG_ON fail\n");
        }
        else
        {
            XC_ERR("Ioctl MDrv_CMD_DIP_TRIGGER DIP_TRIG_OFF fail\n");
        }
        utopia_free(pTrigMode);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pTrigMode);

#else
	MDrv_XC_DIP_EnableCaptureStream(NULL,bEnable,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

/******************************************************************************/
/// Set interrupt mask of XC DWIN.
/// @param u8mask \b IN bitmask of interrupt type
///   - bit7 DWIN VSYNC interrupt mask
///   - bit6 Dump Window interlace Bottom Field ACK Interrupt Mask
///   - bit5 Dump Window interlace Top Field ACK Interrupt Mask
///   - bit4 Dump Window Progressive ACK Interrupt Mask
///   - bit3:1 Reserved
///   - bit0 GWIN VSYNC Interrupt Mask
/// @param bEnable \b IN
///   - # TRUE enable interrupts specified by u8mask
///   - # FALSE disable interrupts specified by u8mask
/// @return E_APIXC_RET_OK - Success
/******************************************************************************/
E_APIXC_ReturnValue MApi_XC_DIP_EnaInt(MS_U16 u8mask, MS_BOOL bEnable, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_INTER_STUS* pIntrStus = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        MApi_GOP_DWIN_EnableIntr(u8mask,bEnable);
        return E_APIXC_RET_OK;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_OK;
    }

    pIntrStus = (DIP_INTER_STUS*)utopia_malloc(sizeof(DIP_INTER_STUS));
    if (pIntrStus == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pIntrStus->IntrStautus = u8mask;
    if(bEnable)
        pIntrStus->eFlag = DIP_INTR_ENABLE;
    else
        pIntrStus->eFlag = DIP_INTR_DISABLE;
	pIntrStus->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_INTERRUPT,(void*)pIntrStus) != UTOPIA_STATUS_SUCCESS)
    {
        if(bEnable)
        {
            XC_ERR("Ioctl MDrv_CMD_DIP_INTERRUPT DIP_INTR_ENABLE fail\n");
        }
        else
        {
            XC_ERR("Ioctl MDrv_CMD_DIP_INTERRUPT DIP_INTR_DISABLE fail\n");
        }

        utopia_free(pIntrStus);
        return E_APIXC_RET_OK;
    }
    utopia_free(pIntrStus);
#else
    MDrv_XC_DIP_EnableIntr(NULL,u8mask, bEnable,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

/********************************************************************************/
/// Get XC Dwin interrupt status
/// @return XC Dwin interrupt status
/********************************************************************************/
MS_U16 MApi_XC_DIP_GetIntStatus(SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	MS_U16 u16IntrStus = 0;
    DIP_INTER_STUS* pIntrStus = NULL;
	DIP_RESOURCE* pResource = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        u16IntrStus = MApi_GOP_DWIN_GetIntrStatus();
        return u16IntrStus;
    }
#endif
	if(pInstantDip[eWindow] == NULL)
	{
		pResource = (DIP_RESOURCE*)utopia_malloc(sizeof(DIP_RESOURCE));
		if(UtopiaOpen(MODULE_DIP|DIPDRIVER_BASE,(void**)&pInstantDip[eWindow], 0, NULL) != UTOPIA_STATUS_SUCCESS)
		{
			XC_ERR("Open DIP fail\n");
			utopia_free(pResource);
			return u16IntrStus;
		}

		utopia_free(pResource);
	}
    pIntrStus = (DIP_INTER_STUS*)utopia_malloc(sizeof(DIP_INTER_STUS));
    if (pIntrStus == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return u16IntrStus;
    }
    pIntrStus->eFlag = DIP_INTR_GET;
	pIntrStus->IntrStautus=0;
	pIntrStus->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_INTERRUPT,(void*)pIntrStus) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_INTERRUPT DIP_INTR_GET fail\n");
		utopia_free(pIntrStus);
        return u16IntrStus;
    }
    u16IntrStus = pIntrStus->IntrStautus;
    utopia_free(pIntrStus);

    return u16IntrStus;
#else
    return MDrv_XC_DIP_GetIntrStatus(NULL,eWindow);
#endif
#else
    MS_U16 u16IntrStus = 0;
	return u16IntrStus;
#endif

}

/********************************************************************************/
/// Get DIP interrupt Independent or share
/// @return DIP interrupt type  FALSE: Share  TRUE: Independent
/********************************************************************************/
MS_BOOL MApi_XC_DIP_CheckIntIndependent(SCALER_DIP_WIN eWindow)
{
    MS_BOOL bRet = 0;
    DIP_INTER_STUS* pIntrStus = NULL;
    DIP_RESOURCE* pResource = NULL;

    if(pInstantDip[eWindow] == NULL)
    {
        pResource = (DIP_RESOURCE*)utopia_malloc(sizeof(DIP_RESOURCE));
        if(UtopiaOpen(MODULE_DIP|DIPDRIVER_BASE,(void**)&pInstantDip[eWindow], 0, NULL) != UTOPIA_STATUS_SUCCESS)
        {
            XC_ERR("Open DIP fail\n");
            utopia_free(pResource);
            return bRet;
        }
        utopia_free(pResource);
    }
    pIntrStus = (DIP_INTER_STUS*)utopia_malloc(sizeof(DIP_INTER_STUS));
    if (pIntrStus == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return bRet;
    }
    pIntrStus->eFlag = DIP_INTR_INDEP;
    pIntrStus->IntrStautus=0;
    pIntrStus->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_INTERRUPT,(void*)pIntrStus) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_INTERRUPT DIP_INTR_GET fail\n");
        utopia_free(pIntrStus);
        return bRet;
    }

    if(pIntrStus->IntrStautus)
    {
        bRet = TRUE;
    }
    else
    {
        bRet = FALSE;
    }
    utopia_free(pIntrStus);

    return bRet;
}

//******************************************************************************
/// Set DWIN data source scan type
/// @param enScanMode \b IN: EN_GOP_DWIN_SCAN_MODE
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_SetSourceScanType(EN_XC_DIP_SCAN_MODE enScanMode, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_SET_PROPERTY* pSetProperty = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        MApi_GOP_DWIN_SelectSourceScanType((EN_GOP_DWIN_SCAN_MODE)enScanMode);
        return E_APIXC_RET_OK;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetProperty = (DIP_SET_PROPERTY*)utopia_malloc(sizeof(DIP_SET_PROPERTY));
    if (pSetProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pSetProperty->Property.eScanMode = enScanMode;
    pSetProperty->eFlag = DIP_PRO_SCANMODE;
	pSetProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_PROPERTY,(void*)pSetProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_SCANMODE fail\n");
		utopia_free(pSetProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetProperty);
#else
    MDrv_XC_DIP_SelectSourceScanType(NULL,(EN_XC_DWIN_SCAN_TYPE)enScanMode,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//******************************************************************************
/// API for enable or disable DWIN Interlace write
/// @param bEnable \b IN DWIN enable or not
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_EnaInterlaceWrite(MS_BOOL bEnable, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_SET_PROPERTY* pSetProperty = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        XC_ERR("eWindow[%d] doesnt support interlace write\n",eWindow);
        return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetProperty = (DIP_SET_PROPERTY*)utopia_malloc(sizeof(DIP_SET_PROPERTY));
    if (pSetProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pSetProperty->Property.bInterlaceW = bEnable;
    pSetProperty->eFlag = DIP_PRO_INTERLACE_W;
	pSetProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_PROPERTY,(void*)pSetProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_INTERLACE_W fail\n");
		utopia_free(pSetProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetProperty);
#else
    MDrv_XC_DIP_SetInterlaceWrite(NULL,bEnable,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}
//******************************************************************************
/// API for enable or disable DWIN Y2R
/// @param bEnable \b IN DWIN enable or not
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_EnableY2R(MS_BOOL bEnable, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_SET_PROPERTY* pSetProperty = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        XC_ERR("eWindow[%d] doesnt support Y2R\n",eWindow);
        return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetProperty = (DIP_SET_PROPERTY*)utopia_malloc(sizeof(DIP_SET_PROPERTY));
    if (pSetProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pSetProperty->Property.bY2R = bEnable;
    pSetProperty->eFlag = DIP_PRO_Y2R;
	pSetProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_PROPERTY,(void*)pSetProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_Y2R fail\n");
		utopia_free(pSetProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetProperty);
#else
    MDrv_XC_DIP_SetY2R(NULL,bEnable,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}
//******************************************************************************
/// Set DWIN Capture format
/// @param fmt \b IN: buffer format to store DWIN content
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_SetOutputDataFmt(EN_XC_DIP_DATA_FMT fmt, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_SET_PROPERTY* pSetProperty = NULL;
#if DIPR_WIDTH_PATCH
    if(eWindow >= MAX_DIP_WINDOW)
    {
        return E_APIXC_RET_FAIL;
    }
    if(_gbDIPRPatchFlowEna[eWindow] == FALSE)
    {
        _genDIPWFmt[eWindow] = fmt;
    }
#endif

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        DataFmt = fmt;
        if(fmt == DIP_DATA_FMT_YUV422)
            MApi_GOP_DWIN_SetDataFmt(DWIN_DATA_FMT_UV8Y8);
        else if(fmt == DIP_DATA_FMT_RGB565)
            MApi_GOP_DWIN_SetDataFmt(DWIN_DATA_FMT_RGB565);
        else if(fmt == DIP_DATA_FMT_ARGB8888)
            MApi_GOP_DWIN_SetDataFmt(DWIN_DATA_FMT_ARGB8888);
        else{
            XC_ERR("eWindow[%d] doesnt support fmt[%d]\n",eWindow,fmt);
            return E_APIXC_RET_FAIL_ENUM_NOT_SUPPORT;
        }
        return E_APIXC_RET_OK;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetProperty = (DIP_SET_PROPERTY*)utopia_malloc(sizeof(DIP_SET_PROPERTY));
    if (pSetProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pSetProperty->Property.eDataFmt = fmt;
    pSetProperty->eFlag = DIP_PRO_DATAFMT;
	pSetProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_PROPERTY,(void*)pSetProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_DATAFMT fail\n");
		utopia_free(pSetProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetProperty);
#else
    MDrv_XC_DIP_SetDataFmt(NULL,(EN_DRV_XC_DWIN_DATA_FMT)fmt,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}
/*****************************************************************************/
/// Set DWIN Capture constant alpha value for format ARGB8888
/// @param u8AlphaVal \b IN: constant alpha value
/// @return E_APIXC_RET_OK - Success
/*****************************************************************************/
E_APIXC_ReturnValue MApi_XC_DIP_SetAlpha(MS_U8 u8AlphaVal, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_SET_PROPERTY* pSetProperty = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        MApi_GOP_DWIN_SetAlphaValue(u8AlphaVal);
        return E_APIXC_RET_OK;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetProperty = (DIP_SET_PROPERTY*)utopia_malloc(sizeof(DIP_SET_PROPERTY));
    if (pSetProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pSetProperty->Property.u8Alpha = u8AlphaVal;
    pSetProperty->eFlag = DIP_PRO_ALPHA;
	pSetProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_PROPERTY,(void*)pSetProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_ALPHA fail\n");
		utopia_free(pSetProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetProperty);
#else
    MDrv_XC_DIP_SetAlphaValue(NULL,u8AlphaVal,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}
//******************************************************************************
/// API for enable or disable DWIN UV SWAP
/// @param bEnable \b IN DWIN enable or not
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_SwapUV(MS_BOOL bEnable, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_SET_PROPERTY* pSetProperty = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        MApi_GOP_DWIN_SetUVSwap(bEnable);
        return E_APIXC_RET_OK;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetProperty = (DIP_SET_PROPERTY*)utopia_malloc(sizeof(DIP_SET_PROPERTY));
    if (pSetProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pSetProperty->Property.bSwapUV = bEnable;
    pSetProperty->eFlag = DIP_PRO_SWAPUV;
	pSetProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_PROPERTY,(void*)pSetProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_SWAPUV fail\n");
		utopia_free(pSetProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetProperty);
#else
	MDrv_XC_DIP_SetUVSwap(NULL,bEnable,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}
//******************************************************************************
/// API for enable or disable DWIN YC SWAP
/// @param bEnable \b IN DWIN enable or not
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_SwapYC(MS_BOOL bEnable, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_SET_PROPERTY* pSetProperty = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        XC_ERR("eWindow[%d] doesnt support YC swap\n",eWindow);
        return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetProperty = (DIP_SET_PROPERTY*)utopia_malloc(sizeof(DIP_SET_PROPERTY));
    if (pSetProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pSetProperty->Property.bSwapYC = bEnable;
    pSetProperty->eFlag = DIP_PRO_SWAPYC;
	pSetProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_PROPERTY,(void*)pSetProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_SWAPYC fail\n");
		utopia_free(pSetProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetProperty);
#else
	MDrv_XC_DIP_SetYCSwap(NULL,bEnable,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}
//******************************************************************************
/// API for enable or disable DWIN RGB SWAP
/// @param bEnable \b IN DWIN enable or not
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_SwapRGB(MS_BOOL bEnable, EN_XC_DIP_RGB_SWAP_TYPE enSwapType, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_SET_PROPERTY* pSetProperty = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        XC_ERR("eWindow[%d] doesnt support RGB swap\n",eWindow);
        return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetProperty = (DIP_SET_PROPERTY*)utopia_malloc(sizeof(DIP_SET_PROPERTY));
    if (pSetProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pSetProperty->Property.stSwapRGB.bSwap = bEnable;
    pSetProperty->Property.stSwapRGB.eSwapType = enSwapType;
    pSetProperty->eFlag = DIP_PRO_SWAPRGB;
	pSetProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_PROPERTY,(void*)pSetProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_SWAPRGB fail\n");
		utopia_free(pSetProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetProperty);
#else
	MDrv_XC_DIP_SetRGBSwap(NULL,bEnable,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}
//******************************************************************************
/// API for set output capture mux
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_SetOutputCapture(MS_BOOL bEnable,EN_XC_DIP_OP_CAPTURE eOpCapture,SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_SET_PROPERTY* pSetProperty = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        if( eOpCapture == E_XC_DIP_OP2 )
            MApi_XC_OP2VOPDESel(E_OP2VOPDE_WHOLEFRAME_WITHOSD);
        else
            MApi_XC_OP2VOPDESel(E_OP2VOPDE_WHOLEFRAME);
        return E_APIXC_RET_OK;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetProperty = (DIP_SET_PROPERTY*)utopia_malloc(sizeof(DIP_SET_PROPERTY));
    if (pSetProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pSetProperty->Property.stOpCapture.bOpCapture = bEnable;
    pSetProperty->Property.stOpCapture.eOpCapture = eOpCapture;
    pSetProperty->eFlag = DIP_PRO_OP_CAPTURE;
	pSetProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_PROPERTY,(void*)pSetProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_OP_CAPTURE fail\n");
		utopia_free(pSetProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetProperty);
#else
	MDrv_XC_DIP_SetOutputCapture(NULL,bEnable,eOpCapture,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}
//******************************************************************************
/// API for set DIPW H & V Mirror
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_SetMirror(MS_BOOL bHMirror,MS_BOOL bVMirror,SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
	DIP_SET_PROPERTY* pSetProperty = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        XC_ERR("eWindow[%d] doesnt support Mirror\n",eWindow);
        return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetProperty = (DIP_SET_PROPERTY*)utopia_malloc(sizeof(DIP_SET_PROPERTY));
    if (pSetProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pSetProperty->Property.stMirror.bHMirror = bHMirror;
    pSetProperty->Property.stMirror.bVMirror = bVMirror;
    pSetProperty->eFlag = DIP_PRO_MIRROR;
	pSetProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_PROPERTY,(void*)pSetProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_MIRROR fail\n");
		utopia_free(pSetProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetProperty);
#else
    MDrv_XC_DIP_SetMirror(NULL,bHMirror,bVMirror,eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}
//******************************************************************************
/// API for set DIPR Property
/// @return E_APIXC_RET_OK - Success
//******************************************************************************

E_APIXC_ReturnValue MApi_XC_DIP_SetDIPRProperty(ST_XC_DIPR_PROPERTY *pstDIPRProperty,SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
    DIP_SET_DIPR_PROPERTY_EX* pSetDIPRProperty_ex = NULL;
    ST_DIP_DI_SETTING stDIPRDISetting = {NULL};
    MS_U32 u32MFDecInfo = 0;
#if DIPR_WIDTH_PATCH
    if(eWindow >= MAX_DIP_WINDOW)
    {
        return E_APIXC_RET_FAIL;
    }
    if(_gbDIPRPatchFlowEna[eWindow] == FALSE)
    {
        _gstDiprProperty[eWindow] = *pstDIPRProperty;
    }
#endif

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        XC_ERR("eWindow[%d] doesnt support DIPR\n",eWindow);
        return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetDIPRProperty_ex = (DIP_SET_DIPR_PROPERTY_EX*)utopia_malloc(sizeof(DIP_SET_DIPR_PROPERTY_EX));
    if (pSetDIPRProperty_ex == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }
    memcpy(&(pSetDIPRProperty_ex->stDIPRProperty_ex.stDIPRProperty),pstDIPRProperty, sizeof(ST_XC_DIPR_PROPERTY));
    pSetDIPRProperty_ex->stDIPRProperty_ex.stDIPR3DDISetting.stDipr3DDI_VerCtl.u32version = 1;
    pSetDIPRProperty_ex->stDIPRProperty_ex.stDIPR3DDISetting.stDipr3DDI_VerCtl.u32size = sizeof(ST_DIP_DI_SETTING);
    stDIPRDISetting.bEnableDI = FALSE;
    pSetDIPRProperty_ex->stDIPRProperty_ex.stDIPR3DDISetting.pDIPR3DDISetting = &stDIPRDISetting;
    pSetDIPRProperty_ex->stDIPRProperty_ex.stDIPR_MFDecInfo.bMFDec_Enable = FALSE;
    pSetDIPRProperty_ex->stDIPRProperty_ex.stDIPR_MFDecInfo.pMFDecInfo = &u32MFDecInfo;
    pSetDIPRProperty_ex->enDIPRFlag = E_DIP_SET_DIPR_NORMAL;
    pSetDIPRProperty_ex->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_DIPR_PROPERTY,(void*)pSetDIPRProperty_ex) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_DIPR_PROPERTY fail\n");
        utopia_free(pSetDIPRProperty_ex);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetDIPRProperty_ex);
#else
    MDrv_XC_DIP_SetDIPRProperty(NULL,pstDIPRProperty, eWindow);
#endif
    return E_APIXC_RET_OK;
#else
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

E_APIXC_ReturnValue MApi_XC_DIP_SetDIPRProperty_EX(ST_XC_DIPR_PROPERTY_EX *pstDIPRProperty_ex, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
        DIP_SET_DIPR_PROPERTY_EX* pSetDIPRProperty_ex = NULL;
#if DIPR_WIDTH_PATCH
    ST_DIP_DI_SETTING* pstDIPRDISetting = NULL;
    ST_DIP_DI_SETTING_V2* pstDIPRDISetting_V2 = NULL;
#endif
#ifdef MFD_MODULE_ENABLE
        ST_DRV_MFD_MVOP_DIP_INFO stMFD_MVOP_DIP_Info;
        MFD_RES_HANDLE u32Mfdec_Res = MFD_RES_TYPE_IDX_NONE;
        EN_DRV_MFD_BIT_DEPTH enMFDBitDepth = E_MFD_8BIT;
        memset(&stMFD_MVOP_DIP_Info, 0, sizeof(ST_DRV_MFD_MVOP_DIP_INFO));
#endif
#ifdef GOPD_DIP
        if(eWindow == Gopd_dip){
            XC_ERR("eWindow[%d] doesnt support DIPR\n",eWindow);
            return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
        }
#endif

        if(pInstantDip[eWindow] == NULL)
        {
            XC_ERR("DIP not Opened\n");
            return E_APIXC_RET_FAIL;
        }
#if DIPR_WIDTH_PATCH
    if(eWindow >= MAX_DIP_WINDOW)
    {
        return E_APIXC_RET_FAIL;
    }
    if(_gbDIPRPatchFlowEna[eWindow] == FALSE)
    {
        _gstDiprProperty[eWindow] = pstDIPRProperty_ex->stDIPRProperty;
        _gstMfdInfo[eWindow] = pstDIPRProperty_ex->stDIPR_MFDecInfo;
        if((pstDIPRProperty_ex->stDIPR3DDISetting.stDipr3DDI_VerCtl.u32version==1) && (pstDIPRProperty_ex->stDIPR3DDISetting.stDipr3DDI_VerCtl.u32size==sizeof(ST_DIP_DI_SETTING)))
        {
            pstDIPRDISetting  = (ST_DIP_DI_SETTING*)pstDIPRProperty_ex->stDIPR3DDISetting.pDIPR3DDISetting;
            _gbDIEna[eWindow] = pstDIPRDISetting->bEnableDI;
        }
        else if((pstDIPRProperty_ex->stDIPR3DDISetting.stDipr3DDI_VerCtl.u32version==2)&&(pstDIPRProperty_ex->stDIPR3DDISetting.stDipr3DDI_VerCtl.u32size==sizeof(ST_DIP_DI_SETTING_V2)))
        {
            pstDIPRDISetting_V2  = (ST_DIP_DI_SETTING_V2*)pstDIPRProperty_ex->stDIPR3DDISetting.pDIPR3DDISetting;
            _gbDIEna[eWindow] = pstDIPRDISetting_V2->stDIP_DI_setting.bEnableDI;
        }
        else
        {
            _gbDIEna[eWindow] = FALSE;
            if(pstDIPRProperty_ex->stDIPR3DDISetting.stDipr3DDI_VerCtl.u32version > 2)
            {
                XC_ERR("DIP DI version Invalid\n");
            }
        }
    }
#endif

#ifdef MFD_MODULE_ENABLE
    if(pstDIPRProperty_ex->stDIPR_MFDecInfo.bMFDec_Enable == TRUE)
    {
        if(Mfdec_Res == MFD_RES_TYPE_IDX_MASK)
        {
            MDrv_MFD_ResAllocate((MFD_RES_TYPE_DIP|MFD_RES_TYPE_OPT_MFDEC), &u32Mfdec_Res);
            Mfdec_Res = u32Mfdec_Res;
        }

        if((Mfdec_Res & MFD_RES_TYPE_IDX_MASK) != MFD_RES_TYPE_IDX_NONE)
        {
            Mfdec_DIP_WIN = eWindow;
            stMFD_MVOP_DIP_Info.u32DrvMFDInfo_Version = pstDIPRProperty_ex->stDIPR_MFDecInfo.stMFDec_VerCtl.u32version;
            stMFD_MVOP_DIP_Info.bMFDec_Enable = pstDIPRProperty_ex->stDIPR_MFDecInfo.bMFDec_Enable;
            stMFD_MVOP_DIP_Info.u8MFDec_Select = (MFD_DEVICE)pstDIPRProperty_ex->stDIPR_MFDecInfo.u8MFDec_Select;
            stMFD_MVOP_DIP_Info.bHMirror = pstDIPRProperty_ex->stDIPR_MFDecInfo.bHMirror;
            stMFD_MVOP_DIP_Info.bVMirror = pstDIPRProperty_ex->stDIPR_MFDecInfo.bVMirror;
            switch(pstDIPRProperty_ex->stDIPR_MFDecInfo.enMFDec_tile_mode)
            {
                case E_DIP_MFEDC_TILE_16_32:
                    stMFD_MVOP_DIP_Info.enMFDec_tile_mode = E_MFD_TILE_16_32;
                    break;
                case E_DIP_MFEDC_TILE_32_16:
                    stMFD_MVOP_DIP_Info.enMFDec_tile_mode = E_MFD_TILE_32_16;
                    break;
                case E_DIP_MFEDC_TILE_32_32:
                    stMFD_MVOP_DIP_Info.enMFDec_tile_mode = E_MFD_TILE_32_32;
                    break;
                default:
                    stMFD_MVOP_DIP_Info.enMFDec_tile_mode = E_MFD_TILE_NONE;
                    break;
            }
            stMFD_MVOP_DIP_Info.bUncompress_mode = pstDIPRProperty_ex->stDIPR_MFDecInfo.bUncompress_mode;
            stMFD_MVOP_DIP_Info.bBypass_codec_mode = pstDIPRProperty_ex->stDIPR_MFDecInfo.bBypass_codec_mode;
            stMFD_MVOP_DIP_Info.en_MFDecVP9_mode = (EN_DRV_MFD_VP9_MODE)pstDIPRProperty_ex->stDIPR_MFDecInfo.en_MFDecVP9_mode;
            stMFD_MVOP_DIP_Info.phyLuma_FB_Base = pstDIPRProperty_ex->stDIPR_MFDecInfo.phyLuma_FB_Base;
            stMFD_MVOP_DIP_Info.phyChroma_FB_Base = pstDIPRProperty_ex->stDIPR_MFDecInfo.phyChroma_FB_Base;
            stMFD_MVOP_DIP_Info.u16FB_Pitch = pstDIPRProperty_ex->stDIPR_MFDecInfo.u16FB_Pitch;
            stMFD_MVOP_DIP_Info.u16Crop_X = pstDIPRProperty_ex->stDIPR_MFDecInfo.u16StartX;
            stMFD_MVOP_DIP_Info.u16Crop_Y = pstDIPRProperty_ex->stDIPR_MFDecInfo.u16StartY;
            stMFD_MVOP_DIP_Info.u16Crop_H = pstDIPRProperty_ex->stDIPR_MFDecInfo.u16HSize;
            stMFD_MVOP_DIP_Info.u16Crop_V = pstDIPRProperty_ex->stDIPR_MFDecInfo.u16VSize;
            stMFD_MVOP_DIP_Info.u16FB_HSize = pstDIPRProperty_ex->stDIPR_MFDecInfo.u16HSize;
            stMFD_MVOP_DIP_Info.u16FB_VSize = pstDIPRProperty_ex->stDIPR_MFDecInfo.u16VSize;
            stMFD_MVOP_DIP_Info.phyBitlen_Base = pstDIPRProperty_ex->stDIPR_MFDecInfo.phyBitlen_Base;
            stMFD_MVOP_DIP_Info.u16Bitlen_Pitch = pstDIPRProperty_ex->stDIPR_MFDecInfo.u16Bitlen_Pitch;
            if(pstDIPRProperty_ex->stDIPRProperty.enDataFmt == DIP_DATA_FMT_YUV420_H265_10BITS)
            {
                enMFDBitDepth = E_MFD_10BIT;
                pstDIPRProperty_ex->stDIPRProperty.enDataFmt = DIP_DATA_FMT_YUV420_H265;
            }
            else
            {
                enMFDBitDepth = E_MFD_8BIT;
            }
            MDrv_MFD_SetReg(Mfdec_Res, MFD_DIP_SET_BIT_DEPTH, &enMFDBitDepth);
            MDrv_MFD_SetReg(Mfdec_Res, MFD_DIP_SET_MFDEC_INFO, &stMFD_MVOP_DIP_Info);
        }
        else
        {
            XC_ERR("[%s] no MFDEC resource\n",__FUNCTION__);
            return E_APIXC_RET_FAIL;
        }
    }
#endif

    pSetDIPRProperty_ex = (DIP_SET_DIPR_PROPERTY_EX*)utopia_malloc(sizeof(DIP_SET_DIPR_PROPERTY_EX));
    if (pSetDIPRProperty_ex == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }
    memcpy(&(pSetDIPRProperty_ex->stDIPRProperty_ex),pstDIPRProperty_ex, sizeof(ST_XC_DIPR_PROPERTY_EX));
    pSetDIPRProperty_ex->enDIPRFlag = E_DIP_SET_DIPR_EX;
    pSetDIPRProperty_ex->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_DIPR_PROPERTY,(void*)pSetDIPRProperty_ex) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_DIPR_PROPERTY_EX fail\n");
        utopia_free(pSetDIPRProperty_ex);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetDIPRProperty_ex);
#else
        MDrv_XC_DIP_SetDIPRProperty_EX(NULL,pstDIPRProperty_ex, eWindow);
#endif
        return E_APIXC_RET_OK;
#else
        return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

    }


//******************************************************************************
/// API for MApi_XC_DIP_Rotation
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_Rotation(MS_BOOL bRotation,EN_XC_DIP_ROTATION eRoDirection,SCALER_DIP_WIN eTmpWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
    DIP_SET_PROPERTY* pSetProperty = NULL;

#ifdef GOPD_DIP
    if(eTmpWindow == Gopd_dip){
        XC_ERR("eWindow[%d] doesnt support Rotate\n",eTmpWindow);
        return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
    }
#endif

    if(pInstantDip[eTmpWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetProperty = (DIP_SET_PROPERTY*)utopia_malloc(sizeof(DIP_SET_PROPERTY));
    if (pSetProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pSetProperty->Property.stRotation.bRotation = bRotation;
    pSetProperty->Property.stRotation.eRoDirection = eRoDirection;
    pSetProperty->eFlag = DIP_PRO_ROTATION;
	pSetProperty->eWindow = eTmpWindow;
    if(UtopiaIoctl(pInstantDip[eTmpWindow],MDrv_CMD_DIP_SET_PROPERTY,(void*)pSetProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_ROTATION fail\n");
		utopia_free(pSetProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetProperty);
#else
	MDrv_XC_DIP_SetRotation(NULL,bRotation,eRoDirection,eTmpWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}
//******************************************************************************
/// API for MApi_XC_DIP_SetPinpon
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_SetPinpon(MS_BOOL bPinpon,MS_PHY u32PinponAddr,SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
    DIP_SET_PROPERTY* pSetProperty = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        XC_ERR("eWindow[%d] doesnt support Pinpon\n",eWindow);
        return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetProperty = (DIP_SET_PROPERTY*)utopia_malloc(sizeof(DIP_SET_PROPERTY));
    if (pSetProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pSetProperty->Property.stPinpon.bPinpon = bPinpon;
    pSetProperty->Property.stPinpon.u32PinponAddr = u32PinponAddr;
    pSetProperty->eFlag = DIP_PRO_PINPON;
	pSetProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_PROPERTY,(void*)pSetProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_PINPON fail\n");
		utopia_free(pSetProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetProperty);
#else
		MDrv_XC_DIP_SetPinpon(NULL,bPinpon,u32PinponAddr,eWindow);
#endif
		return E_APIXC_RET_OK;
#else
		return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//******************************************************************************
/// API for MApi_XC_InterruptAttach
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
#ifdef GOPD_DIP
#ifdef MSOS_TYPE_LINUX
void dip_thread(void)
{
    while(1)
    {
        if( MApi_GOP_DWIN_GetIntrStatus() )
            (void)(pGopd_CB)(E_INT_IRQ_DIPW);
        MsOS_DelayTask(FRAME_DELAY);
        if(bDeAttached)
            break;
    }
}
#endif
#endif

E_APIXC_ReturnValue MApi_XC_DIP_InterruptAttach(InterruptCb pIntCb,SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
#ifdef MSOS_TYPE_LINUX
        pGopd_CB = pIntCb;
        if(bDeAttached == TRUE)
        {
            pthread_create(&id,NULL,(void *) dip_thread,NULL);
            bDeAttached = FALSE;
        }
        return E_APIXC_RET_OK;
#else
    	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif
    }
#endif

#if (!defined(DIP_UTOPIA2K) || defined(STELLAR))
	DIP_INTER_CB* pIntrCb = NULL;
    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_OK;
    }

    pIntrCb = (DIP_INTER_CB*)utopia_malloc(sizeof(DIP_INTER_CB));
    if (pIntrCb == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
#ifdef STELLAR
    pIntrCb->pIntCb = NULL;
#elif !defined(DIP_UTOPIA2K)
    pIntrCb->pIntCb = pIntCb;
#endif
    pIntrCb->eFlag = DIP_INTR_ATTACH;
	pIntrCb->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_INTERRUPT_CB,(void*)pIntrCb) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_INTERRUPT DIP_INTR_ATTACH fail\n");
		utopia_free(pIntrCb);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pIntrCb);
    #ifdef STELLAR
    _DIP_Callback_task(pIntCb,eWindow);
    #endif
#else
    ST_DIP_CAPS_IRQ* pstDIPCapsIrqSupport = NULL;
    MS_BOOL bIrqSupport = FALSE;
    pstDIPCapsIrqSupport = (ST_DIP_CAPS_IRQ*)utopia_malloc(sizeof(ST_DIP_CAPS_IRQ));
    if(pstDIPCapsIrqSupport != NULL)
    {
        pstDIPCapsIrqSupport->u32Version = ST_DIP_CAPS_IRQ_VERSION;
        pstDIPCapsIrqSupport->u32Length = sizeof(ST_DIP_CAPS_IRQ);
        MApi_XC_DIP_GetChipCaps(E_DIP_CAPS_DIP_IRQ_SUPPORT, (void*)pstDIPCapsIrqSupport, eWindow);
        bIrqSupport = pstDIPCapsIrqSupport->bIrqSupport;
    }
    else
    {
        XC_ERR("%s ST_DIP_CAPS_IRQ alloc fail\n",__FUNCTION__);
    }
    if(bIrqSupport == FALSE)
    {
        return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
    }

    if((eWindow == DWIN1_WINDOW) && (MApi_XC_DIP_CheckIntIndependent(eWindow) == TRUE))
    {
        MsOS_AttachInterrupt(E_INT_IRQ_DIP2,_MApi_XC_DIP2_CallBack);
        _gpdip2IntIndepCb = pIntCb;
        MsOS_EnableInterrupt(E_INT_IRQ_DIP2);
        _gbDIP2IndepIsr = TRUE;
    }
    else
    {
        if(_gbAPIAttached == FALSE)
        {
            MsOS_AttachInterrupt(E_INT_IRQ_DIPW,_MApi_XC_DIP_CallBack);
        }
        _gbAPIAttached = TRUE;

        if( (_gbAPIDipIsr == FALSE) && (_gbAPIDwin0Isr == FALSE) && (_gbAPIDwin1Isr == FALSE))
        {
            MsOS_EnableInterrupt(E_INT_IRQ_DIPW);
        }

        if( eWindow == DIP_WINDOW )
        {
            _gpAPIDipIntCb = pIntCb;
            _gbAPIDipIsr = TRUE;
        }
        else if( eWindow == DWIN0_WINDOW )
        {
            _gpAPIDwin0IntCb = pIntCb;
            _gbAPIDwin0Isr = TRUE;
        }
        else if( eWindow == DWIN1_WINDOW )
        {
            _gpAPIDwin1IntCb = pIntCb;
            _gbAPIDwin1Isr = TRUE;
        }
        else
        {
            return E_APIXC_RET_FAIL;
        }
    }
#endif
#else
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR)
	MDrv_XC_DIP_InterruptAttach(NULL,pIntCb,eWindow);
	_XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
#endif
	return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}
//******************************************************************************
/// API for MApi_XC_InterruptDetach
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_InterruptDetach(SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
#ifdef MSOS_TYPE_LINUX
        bDeAttached = TRUE;
        if( pthread_join(id, NULL) != 0 )
        {
            XC_DBUG("[DIP] InterruptDetach fail \n");
        }
        return E_APIXC_RET_OK;
#else
    	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif
    }
#endif

#if (!defined(DIP_UTOPIA2K) || defined(STELLAR))
    DIP_INTER_CB* pIntrCb = NULL;
    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_OK;
    }

    pIntrCb = (DIP_INTER_CB*)utopia_malloc(sizeof(DIP_INTER_CB));
    if (pIntrCb == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pIntrCb->eFlag = DIP_INTR_DETACH;
	pIntrCb->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_INTERRUPT_CB,(void*)pIntrCb) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_INTERRUPT DIP_INTR_DETACH fail\n");
		utopia_free(pIntrCb);
        return E_APIXC_RET_OK;
    }
    utopia_free(pIntrCb);
#else

    if((eWindow == DWIN1_WINDOW) && (MApi_XC_DIP_CheckIntIndependent(eWindow) == TRUE))
    {
        _gbDIP2IndepIsr = FALSE;
        MsOS_DisableInterrupt(E_INT_IRQ_DIP2);
    }
    else
    {
        if( eWindow == DIP_WINDOW )
        {
            _gbAPIDipIsr = FALSE;
        }
        else if( eWindow == DWIN0_WINDOW )
        {
            _gbAPIDwin0Isr = FALSE;
        }
        else if( eWindow == DWIN1_WINDOW )
        {
            _gbAPIDwin1Isr = FALSE;
        }
        else
        {
            return E_APIXC_RET_FAIL;
        }

        if( (_gbAPIDipIsr == FALSE) && (_gbAPIDwin0Isr == FALSE) && (_gbAPIDwin1Isr == FALSE))
        {
            MsOS_DisableInterrupt(E_INT_IRQ_DIPW);
            //MsOS_DetachInterrupt(E_INT_IRQ_DIPW); // do not deattach, because deattach will take 100 ms to wait pthread join
        }
    }
#endif
#else
    _XC_SEMAPHORE_ENTRY(pInstance,E_XC_ID_VAR)
	MDrv_XC_DIP_InterruptDetach(NULL,eWindow);
	_XC_SEMAPHORE_RETURN(pInstance,E_XC_ID_VAR);
#endif
	return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}

//******************************************************************************
/// API for MApi_XC_DIP_SetHVSP
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_SetHVSP(MS_BOOL bSelect,SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION
    DIP_SET_PROPERTY* pSetProperty = NULL;

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        XC_ERR("eWindow[%d] doesnt support HVSP swap\n",eWindow);
        return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetProperty = (DIP_SET_PROPERTY*)utopia_malloc(sizeof(DIP_SET_PROPERTY));
    if (pSetProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pSetProperty->Property.bHVSP = bSelect;
    pSetProperty->eFlag = DIP_PRO_HVSP;
	pSetProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_PROPERTY,(void*)pSetProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_PINPON fail\n");
		utopia_free(pSetProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetProperty);

    return E_APIXC_RET_OK;
#else
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif
}

//******************************************************************************
/// API for MApi_XC_DIP_Set420TileBlock
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_Set420TileBlock(EN_XC_DIP_TILE_BLOCK eTileBlock,SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION
    DIP_SET_PROPERTY* pSetProperty = NULL;
#if DIPR_WIDTH_PATCH
    if(eWindow >= MAX_DIP_WINDOW)
    {
        return E_APIXC_RET_FAIL;
    }
    if(_gbDIPRPatchFlowEna[eWindow] == FALSE)
    {
        _MApi_XC_DIP_SaveTileFmt(eTileBlock,eWindow);
    }
#endif

#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        XC_ERR("eWindow[%d] doesnt support 420 tile block\n",eWindow);
        return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetProperty = (DIP_SET_PROPERTY*)utopia_malloc(sizeof(DIP_SET_PROPERTY));
    if (pSetProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    pSetProperty->Property.eTileBlock = eTileBlock;
    pSetProperty->eFlag = DIP_PRO_TILEBLOCK;
	pSetProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_PROPERTY,(void*)pSetProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_PROPERTY DIP_PRO_PINPON fail\n");
		utopia_free(pSetProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetProperty);

    return E_APIXC_RET_OK;
#else
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif
}

//******************************************************************************
/// API for MApi_XC_DIP_SetPowerState
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
MS_U32 MApi_XC_DIP_SetPowerState(EN_POWER_MODE u16PowerState)
{
#ifndef DISABLE_DIP_FUNCTION
#ifdef UTOPIA_V2

#else

#endif
#endif
    return UTOPIA_STATUS_SUCCESS;
}

//******************************************************************************
/// API for MApi_XC_DIP_SetDbgLevel
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
MS_U32 MApi_XC_DIP_SetDbgLevel(EN_XC_DIP_DEBUG_LEVEL u8DbgLevel)
{
#ifndef DISABLE_DIP_FUNCTION
    if(pInstantDip[DIP_WINDOW] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }

    if(UtopiaIoctl(pInstantDip[DIP_WINDOW], MDrv_CMD_DIP_SET_DBG_LEVEL, (void*)(&u8DbgLevel)) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_DBG_LEVEL fail\n");
        return E_APIXC_RET_FAIL;
    }
    return E_APIXC_RET_OK;
#else
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif
}
//================================ Obsolete Function Start =======================================
//-------------------------------------------------------------------------------------------------
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  Alert !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  Alert !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/// !!! below function will be "REMOVED" or "REFACTORING" later, please do not use it !!!!!!!!!
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
/// Set DWIN base0 base 1 address
/// @param  u32FBAddress0,u32FBAddress1     \b IN: the framebuffer address
/// @param  eWindow         \b IN: the window
//-------------------------------------------------------------------------------------------------
E_APIXC_ReturnValue MApi_XC_DIP_SetWinProperty_Ex(MS_U8 u8BufCnt ,MS_U16 u16Width, MS_U16 u16Height, MS_PHY u32BufStart ,MS_PHY u32BufEnd, SCALER_DIP_SOURCE_TYPE eSource, SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

    ST_XC_DIP_WINPROPERTY stDIPWinProperty;
    stDIPWinProperty.u8BufCnt    = u8BufCnt;
    stDIPWinProperty.u16Width    = u16Width;
    stDIPWinProperty.u16Pitch    = 0x0;
    stDIPWinProperty.u16Height   = u16Height;
    stDIPWinProperty.u32BufStart = u32BufStart;
    stDIPWinProperty.u32BufEnd   = u32BufEnd;
    stDIPWinProperty.enSource     = eSource;
#ifdef UTOPIA_V2
	DIP_SET_WIN_PROPERTY* pSetWinProperty = NULL;
    ST_DIP_GET_XC_STATUS* pstGetXCStatus = NULL;
#ifdef GOPD_DIP
    if(eWindow == Gopd_dip){
        GopD_property.u16w = u16Width;
        GopD_property.u16h = u16Height;
        GopD_property.u16fbw = u16Width;
        GopD_property.u32fbaddr0 = u32BufStart;
        GopD_property.u32fbaddr1 = u32BufEnd;
        if(eSource == SCALER_DIP_SOURCE_TYPE_MAIN)
            MApi_GOP_DWIN_SetSourceSel(DWIN_SRC_IP);
        else if(eSource == SCALER_DIP_SOURCE_TYPE_OP_CAPTURE)
            MApi_GOP_DWIN_SetSourceSel(DWIN_SRC_OP);
        else{
            XC_ERR("eWindow[%d] doesnt support capture source[%d]\n",eWindow,eSource);
            return E_APIXC_RET_FAIL_ENUM_NOT_SUPPORT;
        }
        MApi_GOP_DWIN_SetWinProperty(&GopD_property);
        return E_APIXC_RET_OK;
    }
#endif

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }

    pstGetXCStatus = (ST_DIP_GET_XC_STATUS*)utopia_malloc(sizeof(ST_DIP_GET_XC_STATUS));
    if (pstGetXCStatus == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }
    _Mapi_XC_DIP_GetXCStatus(pstGetXCStatus);
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_FILL_XC_API_INFO,(void*)pstGetXCStatus) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_FILL_XC_API_INFO fail\n");
        utopia_free(pstGetXCStatus);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pstGetXCStatus);


    _genSource[eWindow] = eSource;


    pSetWinProperty = (DIP_SET_WIN_PROPERTY*)utopia_malloc(sizeof(DIP_SET_WIN_PROPERTY));
    if (pSetWinProperty == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
    	return E_APIXC_RET_FAIL;
    }
    memcpy(&(pSetWinProperty->WinProperty),&stDIPWinProperty, sizeof(ST_XC_DIP_WINPROPERTY));
	pSetWinProperty->eWindow = eWindow;
    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_WINPROPERTY,(void*)pSetWinProperty) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_WINPROPERTY fail\n");
		utopia_free(pSetWinProperty);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetWinProperty);
#else
    MDrv_XC_DIP_SetWinProperty(NULL,&stDIPWinProperty, eWindow);
#endif
    return E_APIXC_RET_OK;
#else
	return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif

}
MS_U32 MDrv_XC_DIP_GetResourceByPipeID(MS_U32 u32PipeID, MS_U32*  U32RetResNum, DIPRESOURCE* pResource)
{
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
}

MS_U32 MDrv_XC_DIP_ConfigPipe(MS_U32 u32PipeID, MS_U32 u32SecureDMA, MS_U32 u32OperationMode)
{
    SCALER_DIP_WIN eWindow=DIP_WINDOW;
    DIP_CONFIG_PIPE DIP_Config_Pipe;
    //MS_U32 i=0;

    CheckDIPInstanceOpen(eWindow,UTOPIA_STATUS_FAIL);

    DIP_Config_Pipe.u32PipeID = u32PipeID;
    DIP_Config_Pipe.u32SecureDMA = u32SecureDMA;
    DIP_Config_Pipe.u32OperationMode = u32OperationMode;
    DIP_Config_Pipe.u32ReturnValue = UTOPIA_STATUS_FAIL;

    /*for(i=0;i<MAX_DIP_WINDOW;i++)
    {
        if (pInstantDip[(SCALER_DIP_WIN)i] == NULL)
        {
            if( i == (MAX_DIP_WINDOW-1))
            {
                XC_ERR("DIP not Opened\n");
                return UTOPIA_STATUS_FAIL;
            }
            continue;
        }
        else
        {
            eWindow = (SCALER_DIP_WIN)i;
            break;
        }
    }*/

    if(UtopiaIoctl(pInstantDip[eWindow], MDrv_CMD_DIP_CONFIG_PIPE, (void*)&DIP_Config_Pipe) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Obtain XC engine fail\n");
        return UTOPIA_STATUS_FAIL;
    }
    else
    {
        return DIP_Config_Pipe.u32ReturnValue;
    }
}

MS_U32 MDrv_XC_DIP_CheckPipe(MS_U32 u32PipeID, MS_U32 u32SecureDMA, MS_U32 u32OperationMode)
{
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
}

//******************************************************************************
/// API for MApi_XC_DIP_SetConfig
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_SetConfig(EN_DIP_CONFIG_TYPE enType, void *plist,SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION
    DIP_SETCONFIG_PARAM* pSetConfig = NULL;

    if(pInstantDip[eWindow] == NULL)
    {
        XC_ERR("DIP not Opened\n");
        return E_APIXC_RET_FAIL;
    }
    pSetConfig = (DIP_SETCONFIG_PARAM*)utopia_malloc(sizeof(DIP_SETCONFIG_PARAM));
    if (pSetConfig == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }

    pSetConfig->cfg_type = enType;
    pSetConfig->pCfg = plist;
    pSetConfig->eWindow = eWindow;

    if(UtopiaIoctl(pInstantDip[eWindow],MDrv_CMD_DIP_SET_CONFIG,(void*)pSetConfig) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_SET_CONFIG fail\n");
		utopia_free(pSetConfig);
        return E_APIXC_RET_FAIL;
    }
    utopia_free(pSetConfig);

    return E_APIXC_RET_OK;
#else
    return E_APIXC_RET_FAIL_FUN_NOT_SUPPORT;
#endif
}

#ifdef STELLAR
//******************************************************************************
/// API for MApi_XC_DIP_GetLatestFrameIdx
/// @return E_APIXC_RET_OK - Success
//******************************************************************************
E_APIXC_ReturnValue MApi_XC_DIP_GetLatestFrameIdx(MS_U32 * pIdx,SCALER_DIP_WIN eWindow)
{
#ifndef DISABLE_DIP_FUNCTION

#ifdef UTOPIA_V2
    MS_U16 u16FrameIdx = 0;
    DIP_LATEST_FRAME_INFO * pFrameInfo = NULL;

    pFrameInfo = (DIP_LATEST_FRAME_INFO*)utopia_malloc(sizeof(DIP_LATEST_FRAME_INFO));
    if (pFrameInfo == NULL)
    {
        XC_ERR("%s alloc fail\n",__FUNCTION__);
        return E_APIXC_RET_FAIL;
    }
    pFrameInfo->eWindow = eWindow;
    pFrameInfo->u16Frame= 0x0;
    if(UtopiaIoctl(pInstantDip[eWindow], MDrv_CMD_DIP_GET_LATEST_FRAME, (void*)pFrameInfo) != UTOPIA_STATUS_SUCCESS)
    {
        XC_ERR("Ioctl MDrv_CMD_DIP_GET_LATEST_FRAME fail\n");
        utopia_free(pFrameInfo);
        return E_APIXC_RET_FAIL;
    }
    *pIdx = pFrameInfo->u16Frame;
    utopia_free(pFrameInfo);

    return E_APIXC_RET_OK;
#else
    return MDrv_XC_DIP_GetLatestFrame(NULL,eWindow);
#endif
#else
    *pIdx = 0x0;
    return E_APIXC_RET_FAIL;
#endif

}
#endif


#endif
//================================ Obsolete Function End =======================================

//=============== DIP =====================//
