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

/*-----------------------------------------------------------------------------
 *
 * Description:
 *
 *      The inter-module database
 *
 *---------------------------------------------------------------------------*/

#ifndef DRV_DBASE_H
#define DRV_DBASE_H


#include "x_lint.h"

LINT_EXT_HEADER_BEGIN
#include "x_typedef.h"
#include "x_os.h"
#include "x_timer.h"

#include "vdp_drvif.h"
#include "stc_drvif.h"
#include "mpv_drvif.h"
#include "aud_if.h"
LINT_EXT_HEADER_END


//=====================================================================
// Constant definitions

#define DRV_DBASE_CURRENT_VERSION       1

//=====================================================================
// Type definitions

//---------------------------------------------------------
// Denifitons of module parameters

typedef struct
{
	UINT32	u4DispOk;
	UINT32	u4DispFail;
	UINT32	u4DispSkip;
	UINT32	u4DispRepeat;		// Repeat due to AV Sync
	UINT32	u4DispDrop;			// Drop due to AV Sync
	UINT32	u4SyncMode;			// Mode of AV Sync

    HAL_TIME_T 		rStartTime;
} PARAM_VDP_FB_COUNTER_T;

typedef struct
{
	PARAM_VDP_FB_COUNTER_T	arFbCounter[VDP_NS][VDP_MAX_INPORT_NS];
	UINT32	au4UnderFlow[VDP_NS];
	UINT32	au4Tearing[VDP_NS];
	UINT32	au4StartLineCnt[VDP_NS];
	UINT32	au4StopLineCnt[VDP_NS];
} PARAM_VDP_T;

typedef struct
{
	UINT32 u4PsrDropNs;
	UINT32 u4RetrieveNs;
	UINT32 u4EsmSkipNs;
	UINT32 u4VpsrDropNs;
	UINT32 u4DecOkNs;
	UINT32 u4TimeOutNs;
	UINT32 u4ErrDropNs;
	UINT32 u4EsmQCnt;
	HAL_TIME_T rDecFirstTime;
	UINT32 u4NoIAfterSeqH;
	UINT32 u4HaveI;
	UINT16 u2HSize;
	UINT16 u2VSize;
	UCHAR ucPicStruct;
	UCHAR ucFrameRate;
	HAL_TIME_T rIDeltaTime;

} PARAM_MPV_T;

typedef struct
{
    /* common code field*/
    UINT32 dwInputUnderRun; /* error count for input underrun*/
    UINT32 dwInputUnderRunDec2; /* error count for input underrun of decoder 2*/
    UINT32 dwInputUnderRunMixSound; /* error count for input underrun of mixsound*/
    UINT32 dwOutputUnderRun; /* error count for input output underrun*/
    UINT32 dwOutputUnderRunDec2; /* error count for input output underrun of decoder2*/
    UINT32 dwOutputUnderRunMixSound; /* error count for input output underrun of mixsound*/
    UINT32 dwReserved0[9]; /* reserved field */
    /* first decoder's field*/
    UINT32 dwFrameRepeatCount; /* repeat count for frames*/
    UINT32 dwFrameDropCount; /* drop count for frames*/
    UINT32 dwTotalErrorCount; /* total error count*/
    UINT32 dwTotalFrameCount; /* total frame count*/
    UINT32 dwReserved1[3]; /* reserved field*/
    UINT32 dwDetailLogs[16] ; /* detail log for decoder1*/
    /* second decoder's field*/
    UINT32 dwFrameRepeatCountDec2; /* repeat count for frames*/
    UINT32 dwFrameDropCountDec2; /* drop count for frames*/
    UINT32 dwTotalErrorCountDec2; /* total error count*/
    UINT32 dwTotalFrameCountDec2; /* total frame count*/
    UINT32 dwReserved2[3]; /* reserved field*/
    UINT32 dwDetailLogsDec2[16] ; /* detail log for decoder2*/
} PARAM_AUD_T;

typedef struct
{
	UINT32 au4PcrRxNs[STC_SRC_NS];
	UINT32 u4VCxO;
} PARAM_PCR_T;

typedef struct
{
    UINT32 u4MinBw;
    UINT32 u4MaxBw;
    UINT32 u4AvgBw;
    UINT32 u4Count;
    UINT32 u4AvgBwFraction;
} PARAM_DRAM_T;

typedef struct
{
    UINT32 u4AudioPts;
    UINT32 u4AudioPtsStcDelta;
    UINT32 u4VideoPts;
    UINT32 u4VideoPtsStcDelta;

    INT32 i4AudioPtsStcDelta;
    INT32 i4VideoPtsStcDelta;
    BOOL fgAudioSync;
    BOOL fgVideoSync;
} PARAM_PCR_PTS_INFO;

//---------------------------------------------------------
// The database definition

typedef struct
{
    UINT32                  u4Version;      // Version number
    PARAM_MPV_T             rMpv[MPV_MAX_ES];           // MPEG decoder
    PARAM_VDP_T             rVdp;           // Video Plane
    PARAM_AUD_T             rAud;           // Audio
    PARAM_PCR_T				rPcr;			// PCR

    PARAM_DRAM_T            rDram;

#ifdef CC_GET_PCR_PTS_INFO
    PARAM_PCR_PTS_INFO      rPts;
#endif

    // Other modules go here
    // ...

} DRV_DBASE_T;



//=====================================================================
// Interface

extern BOOL DBS_Init(void);

extern DRV_DBASE_T* DBS_Lock(CRIT_STATE_T* prCritState);

extern BOOL DBS_Unlock(const DRV_DBASE_T* prDbase, CRIT_STATE_T rCritState);

#endif  // DRV_DBASE_H
