/* SPDX-License-Identifier: LGPL-2.1-only OR BSD-3-Clause */
/******************************************************************************
 *
 * This file is provided under a dual license.  When you use or
 * distribute this software, you may choose to be licensed under
 * version 2.1 of the GNU Lesser General Public License
 * ("LGPLv2.1 License") or BSD License.
 *
 * LGPLv2.1 License
 *
 * Copyright(C) 2019 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html for more
 * details.
 *
 * BSD LICENSE
 *
 * Copyright(C) 2019 MediaTek Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/

/*-----------------------------------------------------------------------------
 *
 * $Author: dtvbm11 $
 * $Date  $
 * $RCSfile: video_def.h,v $
 * $Revision: #1 $
 *
 *---------------------------------------------------------------------------*/

#ifndef _VIDEO_DEF_H_
#define _VIDEO_DEF_H_

#include "typedef.h"
#include "feature.h"

#include "x_lint.h"
LINT_EXT_HEADER_BEGIN
#include "x_tv_dec.h"

#ifndef CC_DRIVER_PROGRAM
#include "d_drv_cust.h"
#endif
    LINT_EXT_HEADER_END
// *********************************************************************
// Flags to notify event
// *********************************************************************

 // Main Flags
#define MAIN_FLG_MODE_CHG (0x1L)
#define MAIN_FLG_MODE_DET_DONE (0x1L<<1)
#define MAIN_FLG_MODE_FIXCOLOR (0x1L<<2)
// Pip Flags
#define PIP_FLG_MODE_CHG (0x1L<<8)
#define PIP_FLG_MODE_DET_DONE (0x1L<<9)
#define PIP_FLG_MODE_FIXCOLOR (0x1L<<10)
//Scalar Flags
#define SCALER_MAIN_MODE_CHG  ((0x1L)<<14)
#define SCALER_PIP_MODE_CHG  ((0x1L)<<15)
#define MAIN_DISP_PRM_CHG ((0x1L)<<16)
#define PIP_DISP_PRM_CHG ((0x1L)<<17)
#define SCALE_PRM_CHG ((0x1L)<<18)
#define MAIN_3D_RES_CHG ((0x1L)<<19)
#define PIP_3D_RES_CHG ((0x1L)<<20)
#define MAIN_MUTECOLOR_CHG ((0x1L)<<21)
#define PIP_MUTECOLOR_CHG ((0x1L)<<22)
#define SCPOS_MODECHG ((0x1L)<<23)
#define MAIN_HV_CHG ((0x1L)<<24)
#define PIP_HV_CHG ((0x1L)<<25)
// DVI Event Flag
#define DVI_OUTPUTV (0x1L<<26)
#define DVI_DEBUG_MODE (0x1L<<27)

// Comb Event Flag
#define SUPPORT_COMB_FINETUNE 0

extern BOOL fgIsEventFlgSet(UINT32 u4arg);
extern VOID vSetEventFlg(UINT32 u4arg) reentrant;
extern VOID vClrEventFlg(UINT32 u4arg) reentrant;

extern BOOL fgIsMainFlgSet(UINT32 u4arg);
extern VOID vSetMainFlg(UINT32 u4arg);
extern VOID vClrMainFlg(UINT32 u4arg);

extern BOOL fgIsPipFlgSet(UINT32 u4arg);
extern VOID vSetPipFlg(UINT32 u4arg);
extern VOID vClrPipFlg(UINT32 u4arg);

extern BOOL fgIsScposFlgSet(UINT32 u4arg);
extern VOID vSetScposFlg(UINT32 u4arg);
extern VOID vClrScposFlg(UINT32 u4arg);

extern BOOL fgIsDviFlgSet(UINT32 u4arg);
extern VOID vSetDviFlg(UINT32 u4arg);
extern VOID vClrDviFlg(UINT32 u4arg);

#ifndef __MODEL_slt__
#define MAIN_FREEZE_DELAY	3
#define PIP_FREEZE_DELAY	3
#else
#define MAIN_FREEZE_DELAY	0
#define PIP_FREEZE_DELAY	0
#endif

#define SCALE_VTOTAL_DO_NOT_CARE 0xffff 
#define _MJC_VMAXMIN_DONOTCARE  0xFFFF



// *********************************************************************
// Extern structure declaration
// *********************************************************************

typedef struct RVDecStatus
{
    UINT8 bIsMain;
    UINT8 bIsPip;
} RVDecStatus;

  // *********************************************************************
  // Channel filter: general function and structure defined for Main/Pip
  // *********************************************************************
typedef struct RVChannel
{
    //UINT8 bStatus; should use a status to represent 8 UINT8 status information
    UINT8 bIsChannelOn;
    UINT8 bIsGoingToFreeze;
    UINT8 bIsFreeze;
    UINT8 bIs444;
    UINT8 bIsRGB;
    UINT8 bDecType;

    UINT16 u2_e_type;
    UINT16 u2_e_ui2_id;
    UINT32 u4_Cap;

#ifndef CC_DRIVER_PROGRAM
    DRV_COMP_ID_T t_comp_id;
    TV_DEC_CTRL_T t_Tvd_Ctrl;

    DRV_ID_T t_mon_id;          // Comp ID for current monitored video source
#endif
    UINT8 u1NeedNotify;
} RVChannel;

  // *********************************************************************
  // Display Related register value
  // *********************************************************************
  // Those register value that should be set is recorded in this struct, to avoid
  // Too Many calculation in VSync
typedef struct RScalerInfo
{
	UINT32 wMJCInputWidth;
	UINT32 wMJCInputHeight;
	
	UINT32 wMJCOutputXOffset;
	UINT32 wMJCOutputYOffset;
	UINT32 wMJCOutputWidth;
	UINT32 wMJCOutputHeight;
	UINT32 wMJCOutputVTotal;

} RScalerInfo;

typedef struct RDispPrm
{
    // Picture related
    UINT16 wHPosOfst;           // H Porch
    UINT16 wVPosOfst;           // V Porch
    UINT16 wOverscanRight;      // 3d overscan right side
    UINT16 wOverscanBottom;     // 3d overscan bottom side
 
    UINT32 u4USclH;             // H Up Scaling Factor
    UINT32 u4USclV;             // V Up Scaling Factor
    UINT32 u4DSclH;             // H Down scaling Factor
    UINT32 u4DSclV;             // V Down scaling Factor
 } RDispPrm;

  // *********************************************************************
  // Display Related Information
  // *********************************************************************
/** Brief of VDP_REGION_T
 *  Video plane region (source/output).
 */
typedef struct
{
    UINT32 u4X;
    UINT32 u4Y;
    UINT32 u4Width;
    UINT32 u4Height;
} VDP_DISP_REGION_T;

/** Brief of VDP_OVERSCAN_REGION_T
 *  Video plane overscan region.
 */
typedef struct
{
    UINT32 u4Top;
    UINT32 u4Bottom;
    UINT32 u4Left;
    UINT32 u4Right;
} VDP_PIC_OVERSCAN_REGION_T;

typedef struct RDispInfo
{
    UINT16 wXOffset;
    UINT16 wYOffset;
    UINT16 wWidth;
    UINT16 wHeight;    
    
    VDP_DISP_REGION_T rOutRegion;
    
    UINT32 u4MuteColor;    // Mute color
} RDispInfo;

typedef struct RPicInfo
{
    UINT8 u1Interlace;                      //is interlace source
    UINT16 wHTotal;                         //(in pixel) Source H Total
    UINT16 wVTotal;                         //(in pixel) Source V Total
    UINT16 wSrcW;                           //(in pixel) Source original width
    UINT16 wSrcH;                           //(in pixel) Source original height
    
    VDP_DISP_REGION_T rSrcRegion;           //(0~10000)Source region
    VDP_PIC_OVERSCAN_REGION_T rOverScan;    //(0~10000)Overscan region
    
    UINT16 wXOffset;                        //(in pixel) X start point after doing overscan & source region
    UINT16 wYOffset;                        //(in pixel) Y start point after doing overscan & source region 
    UINT16 wTargetW;                        //(in pixel) Width after doing overscan & source region 
    UINT16 wTargetH;                        //(in pixel) Height after doing overscan & source region
    
    UINT16 wHPorch;                         // decoder porch, usually for workaround
    UINT16 wVPorch;                         // decoder porch, usually for workaround
    UINT16 wUIHPorch;                       // 1000 bias. less than 1000 means minus value.
    UINT16 wUIVPorch;                       // 1000 bias. less than 1000 means minus value.    
} RPicInfo;



typedef struct MonOutPicInfo
{
    UINT8 u1MonOutInSel;                    //monitor mux select
    UINT8 u1MonOutInSel_2;                  //monitor mux select in if u1MonOutInSel switch to Panel_out 
    UINT8 u1Interlace;                      //is interlace source
    UINT16 wHTotal;                         //(in pixel) Source H Total
    UINT16 wVTotal;                         //(in pixel) Source V Total
    UINT16 wSrcW;                           //(in pixel) Source original width
    UINT16 wSrcH;                           //(in pixel) Source original height
	UINT16 u2InFramerate;
	UINT16 u2OutFramerate;
    
    VDP_DISP_REGION_T  rSrcRegion;          //(0~10000)Source region
    VDP_DISP_REGION_T  rRegenScan;          //monitor out regen
//    VDP_PIC_OVERSCAN_REGION_T rOverScan;    //(0~10000)Overscan region
    
    UINT16 wXOffset;                        //(in pixel) X start point after doing overscan & source region
    UINT16 wYOffset;                        //(in pixel) Y start point after doing overscan & source region 
    UINT16 wTargetW;                        //(in pixel) Width after doing overscan & source region 
    UINT16 wTargetH;                        //(in pixel) Height after doing overscan & source region
    
	UINT8  wInput3DFmt;
    UINT16 w3Dto2DInWidth;
    UINT16 w3Dto2DInHeight;
    UINT16 w3Dto2DOutWidth;
    UINT16 w3Dto2DOutHeight;


    UINT16 wPdsInWidth;
    UINT16 wPdsInHeight;
    UINT16 wPdsOutWidth;                    //monitor support down scaling only
    UINT16 wPdsOutHeight;

    UINT8  u1TimeGenSel;                   //NTSC or PAL select    
	UINT8  u1Output;						// 0:tve  1:venc 
    UINT16 wOutputWidth;
    UINT16 wOutputHeight;
	
    UINT32  u4MonOutModeChg;
} MonOutPicInfo;





// Peggy 20080128 for [DTV00022615]
typedef struct Disp_Tune
{
    UINT32 u4PanelW;
    UINT32 u4PanelH;
    UINT32 u4SrcW;
    UINT32 u4SrcH;
    UINT32 u4FrameRate;
    UINT32 u4Interlace;
    UINT32 u4USclVMin;
    UINT32 u4USclVMax;
    UINT32 u4DSclVMin;
    UINT32 u4DSclVMax;
    UINT32 VSP;
    UINT32 TUNE;
} Disp_Tune;

typedef struct
{
    UINT16 u4SrcWidth;
    UINT16 u4SrcHeight;    
    UINT32 u4SrcAddr;    
    UINT32 u4Pitch;
    UINT32 u4YFrmNum;   
    UINT16 u4DesWidth;
    UINT16 u4DesHeight;    
    UINT32 u4DesAddrL;    
    UINT32 u4DesAddrR;	
    UINT32 u4SrcStartX;
    UINT32 u4SrcStartY;
    UINT32 u4Subsample;
    UINT32 u4DumpSize;
    UINT32 u4Index;
} DUMP_3D_INFO_T;


// *********************************************************************
// Video state machine define
// *********************************************************************
  /* Define for States of Video State Machine */
enum
{
    VDO_STATE_IDLE,
    VDO_STATE_WAIT_MODE_DET
};

  // define the auto state machine
enum
{
    VDO_AUTO_NOT_BEGIN,
    VDO_AUTO_POSITION_START,
    VDO_AUTO_POSITION_1_START,
    VDO_AUTO_POSITION_SET,
    VDO_AUTO_PHASE_START,
    VDO_AUTO_PHASE_1_START,
    VDO_AUTO_CLOCK_START,
    VDO_AUTO_CLOCK_1_START,
    VDO_AUTO_CLOCK_2_START
};

enum
{
    VDO_AUTO_COLOR_NOT_BEGIN,
    VDO_AUTO_COLOR_START,
    VDO_AUTO_COLOR_1_START,
    VDO_AUTO_COLOR_2_START,
    VDO_AUTO_COLOR_3_START,
    VDO_AUTO_COLOR_3_START_Delay1,
    VDO_AUTO_COLOR_4_START,
    VDO_AUTO_COLOR_5_START,
    VDO_AUTO_COLOR_CLAMP_DELAY,
    VDO_AUTO_COLOR_END,
    VDO_AUTO_COLOR_BLANK1,
    VDO_AUTO_COLOR_2_START_Interation1,
    VDO_AUTO_COLOR_2_START_Interation_End,
    VDO_AUTO_COLOR_1P0_START,
};

enum
{
    ON_CHIP_AUTO_COLOR_DO_NOTHING,
    ON_CHIP_AUTO_COLOR_INITIAL_STATE,
    ON_CHIP_AUTO_COLOR_SEARCH_TARGET,
    ON_CHIP_AUTO_COLOR_WAIT_VALUE,
    ON_CHIP_AUTO_COLOR_CHECK_VALUE,
    ON_CHIP_AUTO_COLOR_FINISH,
    ON_CHIP_AUTO_COLOR_END
};

enum
{
   BLK_NOTHING,
   BLK_START,
   BLK_LEVEL_STABLE,
   BLK_OFFSET_ADJ,
   BLK_OFFSET_DELAY,  // [SA7_Michael] 080828 for adjusting blanking level stable
   BLK_OFFSET_DELAY1,
   BLK_OFFSET_DELAY2,
   BLK_OFFSET_CHECK,
   BLK_GAIN_ADJ,
   BLK_DELAY,
   BLK_MEASURE,
   BLK_CLAMP_DELAY,
   BLK_APICMD,
   BLK_END  
};

enum
{
    ON_CHIP_GAIN_MODE,
    ON_CHIP_OFFSET_MODE,
    OFFSET_MODE,
    GAIN_MODE,
    DIGITAL_OFFSET_MODE
};

enum
{
    EXT_AUTO_COLOR_NOT_BEGIN,
    EXT_AUTO_COLOR_START,
    EXT_AUTO_COLOR_WAIT,
    EXT_AUTO_COLOR_OFFSETGAIN,
    // EXT_AUTO_COLOR_GAIN,
    EXT_AUTO_COLOR_END
};

enum
{
    INT_HDTV,
    INT_VGA,
    INT_SCART,
    INT_VGA_COMPOENT,
    EXT_HDTV,
    EXT_VGA
};

// *********************************************************************
// Global Variable Declaration
// *********************************************************************

// Used for Video State Machine for Main Loop
extern UINT8 _bMainState;
extern UINT8 _bPipState;

// Decoder Status
extern RVDecStatus _rTvd3dStat;
extern RVDecStatus _rVGAStat;
extern RVDecStatus _rYPBPRStat;
extern RVDecStatus _rDVIStat;

// add for 5371
extern RVDecStatus _rMpegSDStat;
extern RVDecStatus _rMpegHDStat;

// Main Channel
extern RVChannel _rMChannel;

// Pip Channel
extern RVChannel _rPChannel;


//mt5399 monitor out porting
extern  MonOutPicInfo   _rMonitorOutInfo;


#define fgIsMainDec() (_rMChannel.bDecType)
#define fgIsPipDec() (_rPChannel.bDecType)
#define fgIsSrcDec(bPath) ((bPath == SV_VP_MAIN) ? fgIsMainDec() : fgIsPipDec())

#define fgIsMainTvd3d() (_rTvd3dStat.bIsMain)
#define fgIsPipTvd3d() (_rTvd3dStat.bIsPip)
#define fgIsMainVga() (_rVGAStat.bIsMain)
#define fgIsPipVga() (_rVGAStat.bIsPip)
#define fgIsMainYPbPr() (_rYPBPRStat.bIsMain)
#define fgIsPipYPbPr() (_rYPBPRStat.bIsPip)
#define fgIsMainDVI() (_rDVIStat.bIsMain)
#define fgIsPipDVI() (_rDVIStat.bIsPip)

#define fgIsMainDTV() ((_rMpegHDStat.bIsMain) || (_rMpegSDStat.bIsMain))
#define fgIsPipDTV() ((_rMpegHDStat.bIsPip) || (_rMpegSDStat.bIsPip))
#define fgIsSrcDTV(bPath) ((bPath == SV_VP_MAIN) ? fgIsMainDTV() : fgIsPipDTV())

#define fgIsMainScart() (VSS_MAJOR(_bSrcMainNew) == VSS_SCART)
#define fgIsPipScart() (VSS_MAJOR(_bSrcSubNew) == VSS_SCART)


#if SUPPORT_POP
#define bGetVideoDecType(bPath) (((bPath)==SV_VP_MAIN) ? _rMChannel.bDecType : _rPChannel.bDecType)
#else
#define bGetVideoDecType(bPath) (((bPath)<=SV_VP_PIP) ? _rMChannel.bDecType : SV_VD_MAX)
#endif

// Display related information
extern RDispInfo _rMDispInfo;
extern RDispInfo _rPDispInfo;

// Display related register parameter
extern RDispPrm _rMDispPrm;
extern RDispPrm _rPDispPrm;

// Pic related info
extern RPicInfo _rMPicInfo;
extern RPicInfo _rPPicInfo;


// for mute
extern BOOL _fgMainBlueMute;
extern BOOL _fgSubBlueMute;
extern BOOL _fgMainBlackMute;
extern BOOL _fgSubBlackMute;

extern BOOL _fgVideoReady;

#endif
