/* 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: danning.wang $
 * $Date: 2016/02/19 $
 * $RCSfile: dmx_mm.h,v $
 * $Revision: #4 $
 *
 *---------------------------------------------------------------------------*/

/** @file dmx_mm.h
 *  Demux multi-media driver - common definitions
 */

#ifndef DMX_MM_H
#define DMX_MM_H

//-----------------------------------------------------------------------------
// Include files
//-----------------------------------------------------------------------------

#include "dmx_mm_if.h"
#include "dmx.h"
#include "x_hal_5381.h"


//-----------------------------------------------------------------------------
// Configurations
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Constant definitions
//-----------------------------------------------------------------------------
#define DDI_BUF_ALIGNMENT                       16
#define DDI_POINTER_ALIGNMENT                   4

#define MM_WAITDMX_POLLING_TIMES                50

#ifdef CC_TRUSTZONE_SUPPORT
#define DMX_TS130_PID_BUFSIZE       (128 * 1024)
#define DMX_TS134_PID_BUFSIZE       (512 * 1024)
#else
#define DMX_TS130_PID_BUFSIZE       (512 * 1024)
#define DMX_TS134_PID_BUFSIZE       (512 * 1024)
#endif

#define DMX_RPU_HEADER_BUFFSIZE		(12 * 1024)
//
// DDI registers
//

#define DDI_BASE                                (IO_VIRT + 0x1e000) // 0x2001e000

#define DDI_REG_GLOBAL_CTRL                     0xc00

#define DDI_REG_PERIOD_M                        0x410
#define DDI_REG_PERIOD_N                        0x414
#define DDI_REG_PERIOD_K                        0x418
#define DDI_REG_RATE_CMD                        0x420
#define DDI_REG_LATCHED_PERIOD_M                0x440
#define DDI_REG_LATCHED_PERIOD_N                0x444
#define DDI_REG_PKT_QUADBYTE_LIMIT              0x450
#define DDI_REG_DMX_RX_CTRL                     0x460
#define DDI_REG_DMA_REAL_RP                     0x4D0

#define DDI_REG_DCR_INT_SET                     0x800
#define DDI_REG_DCR_INT_CLR                     0x804
#define DDI_REG_DCR_INT_MASK                    0x808
#define DDI_REG_DMA_BUF_START                   0x850
#define DDI_REG_DMA_BUF_END                     0x854
#define DDI_REG_DMA_RP                          0x858
#define DDI_REG_DMA_RP_INIT                     0x85c
#define DDI_REG_DMA_WP                          0x864
#define DDI_REG_DMA_AP                          0x868
#define DDI_REG_DMA_CTRL                        0x86c


#if defined(CC_MT5890) || defined(CC_MT5891)
#define DDI_DEFAULT_PORT    DMX_DDI_PORT_FRAMER3
#else //MT5882 and MT5883
#define DDI_DEFAULT_PORT    DMX_DDI_PORT_FRAMER1
#endif


//-----------------------------------------------------------------------------
// Type definitions
//-----------------------------------------------------------------------------

#if 0
//// Program Stream Struct
typedef struct
{
    BOOL fgEnable;              // Valid
    BOOL fgAllocateBuffer;      // Allocate buffer or not

    // Buffer info
    UINT32 u4BufStart;          // Buffer start
    UINT32 u4BufEnd;            // Buffer end
    UINT32 u4BufLen;            // Buffer length
    UINT32 u4Rp;                // Read pointer
    UINT32 u4Wp;                // Write pointer
    UINT32 u4SectionRp;         // Section read pointer
    UINT32 u4PesRp;             // PES read pointer

    UINT32 u4HeaderBufAddr;     // Header buffer start
    UINT32 u4NextPic;           // Next picture number in current PES

    UINT8 u1StreamID;
    DMX_PS_STREAM_TYPE_T eStreamType;  ///< Stream type
} PS_STRUCT_T;
#endif

/// DDI
typedef struct
{
    DMX_DDI_MODE_T eMode;
    DMX_DDI_STATE_T eState;
    UINT32 u4RateN;         // rate = (N / M) Mbits/sec
    UINT32 u4RateM;         // rate = (N / M) Mbits/sec
    BOOL fgAllocBuf;        // Indicate if DDI allocates buffer for users.
    UINT32 u4BufAddr;       // the starting address of buffer
    UINT32 u4BufSize;       // buffer size
    UINT32 u4Threshold;     // copy data if the threshold of free space is met
    UINT32 u4Rp;            // read pointer to buffer
    UINT32 u4Wp;            // write pointer to buffer
    PFN_DDI_NOTIFY pfnDDINotify;
    UINT32 u4TransferredSize;   // transferred data size
} DMX_DDI_STRUCT_T;

typedef enum
{
    DDI_DATA_TYPE_PROLOG,   // Prolog: for Framer's Internal Sync Lock
    DDI_DATA_TYPE_EPILOG,   // Epilog: for flushing Demux's internal buffer
    DDI_DATA_TYPE_USER      // User data
} DDI_DATA_TYPE_T;

typedef struct
{
    UINT32 u4SN;            // serial number
    DDI_DATA_TYPE_T eDataType;
    DMX_MM_DATA_T rMmData;
} DDI_MSG_T;

//// Multi-media


//-----------------------------------------------------------------------------
// Macro definitions
//-----------------------------------------------------------------------------
///
/// DDI regsiter access commands
///
#define DDI_READ32(off)                 IO_READ32(DDI_BASE, (off))
#define DDI_WRITE32(off, val)           IO_WRITE32(DDI_BASE, (off), (val))
#define DDI_REG32(off)                  IO_READ32(DDI_BASE, (off))

#ifdef CC_DMX_EMULATION
#define IS_KERN_LOGICAL_ADDRESS(addr)   TRUE
#else
#define IS_KERN_LOGICAL_ADDRESS(addr) \
    (((UINT32)(addr) >= 0xBF000000) ? TRUE : FALSE)
#endif

//-----------------------------------------------------------------------------
// Prototype  of inter-file functions
//-----------------------------------------------------------------------------

//
// DDI Emu
//

EXTERN BOOL _DMX_DDI_EMU_Init(UINT8 u1TsIndex);
EXTERN BOOL _DMX_DDI_EMU_Transfer(UINT8 u1Type, UINT32 u4Addr, UINT32 u4Size);
EXTERN BOOL _DMX_DDI_EMU_SetBuffer(UINT32 u4Addr, UINT32 u4Size);

//
// DDI
//
EXTERN BOOL _DMX_DDI_Init(void);
EXTERN BOOL _DMX_DDI_Set(UINT32 u4Flags, const DMX_DDI_T *prDDI);
EXTERN BOOL _DMX_DDI_Get(UINT8 u1TsIdx, UINT32 u4Flags, DMX_DDI_T *prDDI);
EXTERN BOOL _DMX_DDI_Free(void);

EXTERN void _DMX_DDI_SetPort(DMX_DDI_PORT_T ePort, BOOL fgForce);
EXTERN void _DMX_DDI_SetPortEx(DMX_DDI_PORT_T ePort, UINT8 u1Pidx, BOOL fgForce,  BOOL fgTs);
EXTERN DMX_DDI_PORT_T _DMX_DDI_GetPort(void);
EXTERN UINT8 _DMX_DDI_GetTunerIndex(void);

EXTERN BOOL _DMX_DDI_Notify(void);

EXTERN BOOL _DMX_DDI_FlushBuf(void);
EXTERN UINT32 _DMX_DDI_GetFreeBufSize(UINT8 u1Inst);

EXTERN BOOL _DMX_DDI_SetPacketSize(UINT8 u1TsIdx, UINT8 u1PacketSize);
EXTERN UINT8 _DMX_DDI_GetPacketSize(void);
EXTERN BOOL _DMX_DDI_SetSyncOffset(UINT8 u1Inst, UINT8 u1Offset);
EXTERN BOOL _DMX_DDI_SingleMove(UINT8 u1TsIdx, UINT32 u4BufStart, UINT32 u4BufEnd,
                                UINT32 u4Addr, UINT32 u4Size, UINT32 u4SkipLen);

EXTERN BOOL _DMX_DDI_TSMoveData(DMX_MM_DATA_T *prData);

EXTERN BOOL _DMX_DDI_PowerDown(VOID);
EXTERN BOOL _DMX_DDI_TSRequestReset(UINT8 u1Inst);

EXTERN BOOL _DMX_DDI_Unlock(void);  // for auto-reset

EXTERN void _DMX_DDI_HWReset(void);

EXTERN BOOL _DMX_DDI_WaitTransfer(UINT8 u1TsIdx, UINT32 u4BufStart, UINT32 u4BufEnd,
                              UINT32 u4SendAddr, UINT32 u4SendSize, UINT32 u4SkipLen);

EXTERN BOOL _DMX_DDI_InitISR(void);
EXTERN void _DMX_DDI_SetDMAInt(BOOL fgEmpty, BOOL fgAlert);
EXTERN void _DMX_DDI_SetDMAAlert_Pointer(UINT32 u4AlertPoint);

EXTERN BOOL _DDI_TsResetDmxBuffers(UINT8 u1Inst);

EXTERN BOOL _DMX_SetTSFilePlayPath(UINT8 u1TsIndex, DMX_TSFMT_T eFmt);
EXTERN void _DMX_DDI_SetDelayByte(UINT8 u1DelayByte);
EXTERN BOOL _DMX_DDI_SendEos(UINT8 u1TsIdx);

EXTERN BOOL _DDI_Sync_VideoWp(UINT8 u1TsIdx);
EXTERN BOOL _DDI_SendTSEOS(UINT8 u1TsIdx);
EXTERN BOOL _DDI_SendAllDummyPackets(UINT8 u1TsIdx);
EXTERN BOOL _DMX_DDI_FillBuf(UINT32 u4DataAddr, UINT32 u4DataSize, UINT32 *pu4FilledSize);
EXTERN BOOL _DMX_DDI_StreamStart(VOID);
EXTERN BOOL _DMX_DDI_StreamStop(VOID);

//
// PVR
//
EXTERN BOOL _DMX_PVRPlay_Init(void);
EXTERN BOOL _DMX_PVRPlay_130byteEnable(UINT8 u1TsoutIdx, BOOL fgEnable, UINT8 u1PktSize);
EXTERN VOID _DMX_PVRPlay_Reset(UINT8 u1TsOutIdx);
EXTERN BOOL _DMX_PVRPlay_SendNullPackets(UINT8 u1TsOutIdx);
EXTERN BOOL _DMX_PVRPlay_Free(UINT8 u1TsOutIdx);
EXTERN BOOL _DMX_PVRPlay_Start(UINT8 u1TsOutIdx);
EXTERN BOOL _DMX_PVRPlay_Stop(UINT8 u1TsOutIdx, BOOL fgForce);
EXTERN BOOL _DMX_PVRPlay_Pause(UINT8 u1TsOutIdx, DMX_PVRPLAY_PTR_T *prPtr);
EXTERN BOOL _DMX_PVRPlay_Resume(UINT8 u1TsOutIdx, DMX_PVRPLAY_PTR_T *prPtr);
EXTERN BOOL _DMX_PVRPlay_GetTimeStamp(UINT8 u1TsOutIdx, UINT32 *pu4RetLocalStamp);
EXTERN DMX_PVR_PLAYSTATE_T _DMX_PVRPlay_GetState(UINT8 u1TsOutIdx);
EXTERN BOOL _DMX_PVRPlay_Set(UINT8 u1TsoutIdx, UINT32 u4Flags, const DMX_PVR_PLAY_T* prPVRPlay);
EXTERN BOOL _DMX_PVRPlay_Get(UINT8 u1TsOutIdx, UINT32 u4Flags, DMX_PVR_PLAY_T* prPVRPlay);
EXTERN VOID _DMX_PVRPlay_Notify(UINT8 u1TsOutIdx);
EXTERN BOOL _DMX_PVRPlay_UpdateWp(UINT8 u1TsOutIdx, UINT32 u4Addr, UINT32 u4Size);
EXTERN BOOL _DMX_PVRPlay_FreeBuf(UINT8 u1TsOutIdx);
EXTERN BOOL _DMX_PVRPlay_FlushBuf(UINT8 u1TsOutIdx);
EXTERN BOOL _DMX_PVRPlay_WaitTransfer(UINT8 u1TsOutIdx, UINT32 u4BufStart, UINT32 u4BufEnd,
                              UINT32 u4SendAddr, UINT32 u4SendSize);
EXTERN BOOL _DMX_PVRPlay_SingleMove(UINT8 u1TsOutIdx, const DMX_MM_DATA_T* prData);
EXTERN BOOL _DMX_PVRPlay_TSMoveData(UINT8 u1TsOutIdx, const DMX_MM_DATA_T *prData);
EXTERN BOOL _DMX_PVRPlay_GetBufPointer(UINT8 u1TsOutIdx, DMX_PVRPLAY_BUFPTR_T *prPtr);
EXTERN BOOL _DMX_PVRPlay_SetBuffer(UINT8 u1TsOutIdx, UINT32 u4BufStart, UINT32 u4BufEnd, UINT32 u4Wp, UINT32 u4Rp);
EXTERN BOOL _DMX_PVRPlay_RequestReset(UINT8 u1TsOutIdx, UINT8 u1TsIdx);
EXTERN void _DMX_PVRPlay_GetCounter(UINT8 u1TsOutIdx, DMX_PVRPLAY_COUNTER_T *prCounter);
EXTERN void _DMX_PVRPlay_ResetCounter(UINT8 u1TsOutIdx);
EXTERN void _DMX_Set_PB_STEER_BIT_9(UINT8 u1TsOutIdx,BOOL bit_9_enable);

EXTERN void _DMX_PVRPlay_SetPort(UINT8 u1TsOutIdx, DMX_PVRPLAY_PORT_T ePort);
EXTERN BOOL _DMX_PVRPlay_SetPortEx(UINT8 u1PvrIdx, UINT8 u1TsIdx, UINT8 u1Pidx,
                                    DMX_PVRPLAY_PORT_T ePort, BOOL fgIsTs, BOOL fgIsOutput);
EXTERN DMX_PVRPLAY_PORT_T _DMX_PVRPlay_GetPort(UINT8 u1TsOutIdx);
EXTERN BOOL _DMX_PVRPlay_SetMoveMode(UINT8 u1TsOutIdx, DMX_TSFMT_T eTSFmt, BOOL fgForce);
EXTERN BOOL _DMX_OpenTimeshift(UINT8 u1TsOutIdx, BOOL fgOpen);
EXTERN BOOL _DMX_IsTimeshiftOpen(UINT8 u1TsOutIdx);
EXTERN BOOL _DmxPVRPlay_SetFramerPktSize(UINT8 u1TsOutIdx, UINT16 u2PktSize);

//
// Program stream
//
EXTERN BOOL _DMX_PS_SetStream(UINT8 u1Pidx, UINT32 u4Flags, const DMX_PS_T *prPs);
EXTERN BOOL _DMX_PS_GetStream(UINT8 u1Pidx, UINT32 u4Flags, DMX_PS_T *prPs);
EXTERN BOOL _DMX_PS_FreeStream(UINT8 u1Pidx);
EXTERN BOOL _DMX_PS_DetectStreamID(BOOL fgDetect, UINT16 u2Pairs, UINT32 u4Len);
EXTERN BOOL _DMX_PS_SetAudioType(UINT8 u1Pidx, DMX_AUDIO_TYPE_T eAudioType);
EXTERN DMX_AUDIO_TYPE_T _DMX_PS_GetAudioType(void);
EXTERN BOOL _DMX_PS_SetDataFormat(DMX_DATA_FORMAT_T eDataFormat);
EXTERN DMX_DATA_FORMAT_T _DMX_PS_GetDataFormat(void);
EXTERN BOOL _DMX_PS_CPU_SetHDCPKeyIv(UINT8 u1Inst, UINT8 *pu1Key, UINT8 *pu1Iv);

#ifdef CC_DMX_PS_CPU
EXTERN BOOL _DMX_PS_CPU_Init(void);
EXTERN BOOL _DMX_PS_CPU_SetStream(UINT8 u1Idx, UINT32 u4Flags, const DMX_PS_T *prPs);
EXTERN BOOL _DMX_PS_CPU_GetStream(UINT8 u1Idx, UINT32 u4Flags, DMX_PS_T *prPs);
EXTERN BOOL _DMX_PS_CPU_FreeStream(UINT8 u1Idx);
EXTERN BOOL _DMX_PS_CPU_FlushBuffer(UINT8 u1Idx);
EXTERN BOOL _DMX_PS_CPU_Reset(UINT8 u1Inst);
EXTERN BOOL _DMX_PS_CPU_SetAudioType(UINT8 u1Idx, DMX_AUDIO_TYPE_T eAudioType);
EXTERN DMX_AUDIO_TYPE_T _DMX_PS_CPU_GetAudioType(UINT8 u1Pidx);
EXTERN BOOL _DMX_PS_CPU_SetDataFormat(UINT8 u1Inst, DMX_DATA_FORMAT_T eDataFormat);
EXTERN DMX_DATA_FORMAT_T _DMX_PS_CPU_GetDataFormat(UINT8 u1Inst);
EXTERN BOOL _DMX_PS_CPU_MoveData(DMX_MM_DATA_T *prData);
EXTERN void _DMX_PS_CPU_SetVideoHeader(UINT8 u1Idx, DMX_PES_MSG_T *prPes);
EXTERN void _DMX_PS_CPU_SetAudioHeader(UINT8 u1Idx, DMX_AUDIO_PES_T *prPes);
EXTERN void _DMX_PS_CPU_GetExtraAudioInfo(UINT8 u1Idx, DMX_AUDIO_PES_T *prPes);
EXTERN BOOL _DMX_PS_MoveData(UINT8 u1Idx, const DMX_PS_MOVEDATA_CMD_T *prData, UINT32 u4TimeOut);
EXTERN UINT8 _DMX_PS_GetAvailable_PSID(UINT8 u1Inst);
EXTERN BOOL _DMX_PS_Free_PSID(UINT8 u1Inst);
#endif  // CC_DMX_PS_CPU


//
// Elementary stream
//
EXTERN BOOL _DMX_ES_SetPid(UINT8 u1Pidx, UINT32 u4Flags, const DMX_ES_T *prPs);
EXTERN BOOL _DMX_ES_GetPid(UINT8 u1Pidx, UINT32 u4Flags, DMX_ES_T *prPs);
EXTERN BOOL _DMX_ES_FreePid(UINT8 u1Pidx);
EXTERN BOOL _DMX_ES_SetFileLength(UINT8 u1Pidx, UINT32 u4Len);
EXTERN void _DMX_ES_SetMoveIndex(UINT8 u1Idx);
EXTERN UINT8 _DMX_ES_GetMoveIndex(void);
EXTERN BOOL _DMX_ES_FlushBuffer(UINT8 u1Idx);
EXTERN BOOL _DMX_ES_Reset(UINT8 u1Idx);
EXTERN BOOL _DMX_ES_MoveData(UINT8 u1Idx, DMX_MM_DATA_T *prData);

//
// Multi-media playback
//
EXTERN BOOL _DMX_MM_Init(void);
EXTERN BOOL _DMX_MM_Set(UINT8 u1Idx, UINT32 u4Flags, const DMX_MM_T *prMM);
EXTERN BOOL _DMX_MM_Get(UINT8 u1Idx, UINT32 u4Flags, DMX_MM_T *prMM);
EXTERN BOOL _DMX_MM_Free(UINT8 u1Idx);

EXTERN BOOL _DMX_MM_GetSearchStartCode(UINT8 u1Idx);

EXTERN UINT32 _DMX_MM_GetBufEmptySize(UINT8 u1Idx);

EXTERN BOOL _DMX_MM_MoveData(UINT8 u1Pidx, const DMX_MM_DATA_T *prData, UINT32 u4TimeOut);
EXTERN void _DMX_MM_SetHeader(UINT8 u1Idx, DMX_PES_MSG_T *prPes);
EXTERN void _DMX_MM_SetAudioHeader(UINT8 u1Idx, DMX_AUDIO_PES_T *prPes);
EXTERN void _DMX_MM_SetTimer(UINT8 u1Idx, DMX_PES_MSG_T *prPes);


EXTERN void _DMX_MM_ISRHandler(UINT8 u1Idx);
EXTERN BOOL _DMX_MM_IsFrameHead(void);

EXTERN BOOL _DMX_MM_SetDRMMode(UINT8 u1Idx, DMX_DRM_MODE_T eMode);
EXTERN DMX_DRM_MODE_T _DMX_MM_GetDRMMode(UINT8 u1Idx);
EXTERN BOOL _DMX_MM_SetAES(UINT8 u1Idx, UINT32 u4Flags, const DMX_MM_DRM_AES_T *prDRM);
EXTERN BOOL _DMX_MM_SetSecureAES(UINT8 u1Idx, UINT32 u4Flags, const DMX_MM_DRM_AES_T *prDRM, BOOL fgReload);
EXTERN BOOL _DMX_MM_GetAES(UINT8 u1Idx, UINT32 u4Flags, DMX_MM_DRM_AES_T *prDRM);
EXTERN BOOL _DMX_MM_UnlockBuffer(UINT8 u1Idx, UINT8 u1SerialNumber, UINT32 u4Size,
                                 UINT32 u4FrameAddr);
EXTERN BOOL _DMX_MM_GetBuffer(UINT8 u1Idx, UINT8 u1SerialNumber, UINT32 u4FrameAddr,
                    UINT32 u4SkipSize, UINT32 u4CopySize, UCHAR* pu1Dest);
EXTERN BOOL _DMX_MM_FlushBuffer(UINT8 u1Idx);
EXTERN BOOL _DMX_MM_Reset(UINT8 u1Inst);
EXTERN BOOL _DMX_MM_AllocateBuffer(UINT8 u1Idx, UINT32 u4Size);
EXTERN BOOL _DMX_MM_FreeBuffer(UINT8 u1Idx);

EXTERN UINT32 _DMXMMGetData(void);

EXTERN BOOL _DMX_MM_SendAudioChunk(UINT8 u1Pidx, UINT32 u4Wp, UINT32 u4Pts, BOOL fgEOS);
EXTERN UINT8 _DMX_MM_GetEnabledPidIndex(DMX_INPUT_TYPE_T eInputType, DMX_PID_TYPE_T ePidType);

EXTERN BOOL _DMX_MM_TSFlushBuffer(UINT8 u1Idx);
EXTERN BOOL _DMX_MM_ESFlushBuffer(UINT8 u1Idx);

EXTERN BOOL _DMX_FlushRPUBuffer(UINT8 u1Pidx);
//
// DTCP
//
EXTERN BOOL _DMX_DTCP_SetAES(UINT32 u4Flags, const DMX_MM_DRM_AES_T *prDRM);
EXTERN BOOL _DMX_DTCP_MoveData(const DMX_DTCP_DATA_T *prData);
EXTERN BOOL _DMX_DTCP_MoveBlockData(const DMX_DTCP_BLKDATA_T *prBlkData);

EXTERN BOOL _DMX_AES_SetDecryptInfo(UINT32 u4Flags, const DMX_MM_DRM_AES_T *prDRM);
EXTERN BOOL _DMX_AES_MoveData(const DMX_AES_DATA_T *prData);
EXTERN BOOL _DMX_AES_MoveBlockData(const DMX_AES_BLKDATA_T *prBlkData);

EXTERN UINT8 _DMX_AES_AllocEngine(void);
EXTERN BOOL _DMX_AES_FreeEngine(UINT8 u1Idx);
EXTERN BOOL _DMX_AES_SetDecryptInfoEx(UINT8 u1Idx, UINT32 u4Flags, const DMX_MM_DRM_AES_T *prDRM);
EXTERN BOOL _DMX_AES_MoveDataEx(UINT8 u1Idx, const DMX_AES_DATA_T *prData);
EXTERN BOOL _DMX_AES_MoveBlockDataEx(UINT8 u1Idx, const DMX_AES_BLKDATA_T *prBlkData);
EXTERN BOOL _DMX_SW_AES_MoveDataEx(UINT8 u1Idx, const DMX_AES_DATA_T *prData);
EXTERN BOOL _DMX_Marlin_MoveData(UINT8 u1Idx, const DMX_DTCP_DATA_T *prData);
EXTERN BOOL _DMX_Marlin_SetDecryptInfo(UINT8 u1Idx, UINT32 u4Flags, const DMX_MM_DRM_AES_T *prDRM);
///
/// ND-DRM
///
EXTERN BOOL _DMX_MM_SetNDDRM(UINT8 u1Idx, UINT32 u4Flags, const DMX_MM_DRM_AES_T *prDRM);
EXTERN BOOL _DMX_NDDRM_MoveData(UINT8 u1Idx, const DMX_MM_DATA_T *prData, UINT32 u4TimeOut);

#ifdef DMX_SUPPORT_PDDRM
EXTERN BOOL _DMX_MM_InitCipherCtx(void);
EXTERN BOOL _DMX_MM_SetPDDRM(UINT8 u1Idx, UINT32 u4Flags, const DMX_MM_DRM_PD_T *prDRM);
EXTERN BOOL _DMX_PDDRM_MoveData(UINT8 u1Idx, const DMX_MM_DATA_T *prData, UINT32 u4TimeOut);
EXTERN BOOL _DMX_PDDRM_Decrypt(UINT8 u1Idx, const DMX_MM_DATA_T *prData);
#endif

///
/// dmx_audio_handler
///
#ifdef CC_DMX_AUDIO_PREBUF
EXTERN BOOL _DMX_InitAudHandler(void);
EXTERN BOOL _DMX_AudHandler_AddPes(DMX_AUDIO_PES_T* prPes);
EXTERN BOOL _DMX_AudHandler_SetActivePidx(UINT8 u1Idx);
EXTERN BOOL _DMX_AudHandler_SetActivePidxEx(UINT8 u1Idx, UINT8* pu1Buf, UINT32 u4Size);
EXTERN BOOL _DMX_AudHandler_Set(UINT8 u1Idx, BOOL fgToDec);
EXTERN BOOL _DMX_AudHandler_Free(UINT8 u1Idx);
EXTERN BOOL _DMX_AudHandler_SeekAndUpdate(UINT8 u1Idx, UINT32 u4Pts, UINT32 *pu4Pts);
EXTERN void _DMX_AudHandler_SetEnable(BOOL fgEnable);
EXTERN BOOL _DMX_AudHandler_SetQSize(UINT8 u1Idx, UINT16 u2Size);
EXTERN BOOL _DMX_AudHandler_UpdateRp(UINT8 u1Idx, UINT32 u4Size);
EXTERN BOOL _DMX_AudHandler_FlushQ(UINT8 u1Idx);
EXTERN UINT8 _DMX_AudHandler_GetHardAudioBufferIdx(VOID);
EXTERN BOOL _DMX_AudHandler_GetEnable(VOID);

#endif  // CC_DMX_AUDIO_PREBUF

// dmx_multimm_api.c
EXTERN void _DMX_MUL_LockPicSem(UINT8 u1Inst, DMX_MM_DATA_T *prData);
EXTERN void _DMX_MUL_UnlockPicSem(UINT8 u1Inst, DMX_MM_DATA_T *prData);
EXTERN void _DMX_MUL_UnlockPicSemWhenFinishedCallback(void);
EXTERN void _DMX_MUL_ModifyPicCount(UINT8 u1Pidx, BOOL fbIncrement);
EXTERN UINT32 _DMX_MUL_GetPicCount(UINT8 u1Pidx);
EXTERN BOOL _DMX_MUL_Init(void);
EXTERN void _DMX_MUL_EnableMultipleInst(BOOL fgEnable);
EXTERN UINT8 _DMX_MUL_GetAvailableInst(DMX_CONN_TYPE_T eConnType);
EXTERN BOOL _DMX_MUL_FreeInst(UINT8 u1Inst);
EXTERN BOOL _DMX_MUL_SetInstType(UINT8 u1Inst, DMX_INPUT_TYPE_T eInputType);
EXTERN DMX_INPUT_TYPE_T _DMX_MUL_GetInstInputType(UINT8 u1Inst);
EXTERN UINT8 _DMX_MUL_GetAvailablePidx(UINT8 u1Inst);
EXTERN UINT8 _DMX_MUL_GetAvailableFilter(UINT8 u1Inst);
EXTERN BOOL _DMX_MUL_SetSubtitlePidx(UINT8 u1Inst, UINT8 u1Pidx);
EXTERN UINT8 _DMX_MUL_GetSubtitlePidx(UINT8 u1Inst);
EXTERN BOOL _DMX_MUL_SetTSIdx(UINT8 u1Inst);
EXTERN UINT8 _DMX_MUL_GetTsIdx(UINT8 u1Inst);
EXTERN BOOL _DMX_MUL_Enable_TSPath(UINT8 u1TsIdx, BOOL fgEnable);
EXTERN BOOL _DMX_MUL_ForceToFreeVdec0(BOOL fgEnable);
EXTERN BOOL _DMX_MUL_ForceToFreePCR(BOOL fgEnable);
EXTERN UINT8  _DMX_MUL_GetInputTypeTsIdx(DMX_INPUT_TYPE_T eInputType);

// Temporary, for old mw_if
EXTERN BOOL _DMX_MUL_SetPidxInstance(UINT8 u1Inst, UINT8 u1Pidx);
EXTERN BOOL _DMX_MUL_FreePidx(UINT8 u1Pidx);
EXTERN BOOL _DMX_MUL_FreeFilter(UINT8 u1FilterIndex);
EXTERN BOOL _DMX_MUL_MoveData(UINT8 u1Inst, DMX_MM_DATA_T *prData);
EXTERN BOOL _DMX_MUL_RequestReset(UINT8 u1Inst);
EXTERN UINT32 _DMX_MUL_GetEmptySize(UINT8 u1Inst, DMX_PID_TYPE_T eType, UINT8 u1Pidx);
EXTERN BOOL _DMX_MUL_SetDecoderCallbacks(UINT8 u1Inst, const DMX_DECODER_CALLBACKS_T* prCallbacks);
EXTERN UINT32 _DMX_MUL_GetMinFreeDRAMBufferSize(UINT8 u1Inst, DMX_PID_TYPE_T eType, UINT8 u1Pidx);
EXTERN DMX_DECODER_CALLBACKS_T* _DMX_MUL_GetInstDecoderCallback(UINT8 u1Inst);
EXTERN DMX_DECODER_CALLBACKS_T* _DMX_MUL_GetPidxDecoderCallback(UINT8 u1Pidx);
EXTERN UINT8 _DMX_MUL_GetPidxInstId(UINT8 u1Pidx);
EXTERN DMX_INPUT_TYPE_T _DMX_MUL_GetInstInputType(UINT8 u1Inst);
EXTERN BOOL _DMX_MUL_SetVideoType(UINT8 u1Inst, DMX_VIDEO_TYPE_T eVideoType);
EXTERN DMX_VIDEO_TYPE_T _DMX_MUL_GetVideoType(UINT8 u1Inst);
EXTERN BOOL _DMX_MUL_SetTSMode(UINT8 u1Inst, DMX_TSFMT_T eFmt);
EXTERN DMX_TSFMT_T _DMX_MUL_GetTSMode(UINT8 u1Inst);
EXTERN BOOL _DMX_MUL_SetFifoOutput(UINT8 u1Inst, DMX_PID_TYPE_T ePidType, UINT8 u1ChannelId, BOOL fgEnable);
EXTERN BOOL _DMX_DDI_130byteEnable(BOOL fgEnable, UINT8 u1PktSize);
EXTERN BOOL _DMX_MUL_GetMulInstInfo(UINT8 u1InstId, DMX_MUL_INSTINFO_T* prMulInst);
EXTERN BOOL _DMX_MUL_GetMulPidInfo(UINT8 u1Pidx, DMX_MUL_PIDINFO_T* prMulPid);
EXTERN BOOL _DMX_MUL_GetMulFilterInfo(UINT8 u1FilterIdx, DMX_MUL_FILTER_INFO_T* prMulFilter);
EXTERN UINT8 _DMX_MUL_GetDtvInstFromTsIdx(UINT8 u1TsIdx);

EXTERN BOOL _DMX_DDI_FramerSwitch(UINT8 u1Idx, DMX_FRONTEND_T eFrontEnd);
EXTERN BOOL _DDI_UsePVR_Update(UINT8 u1TsIdx, BOOL fgUsePvr);
EXTERN BOOL _DDI_UsePVR(UINT8 u1TsIdx);
EXTERN BOOL _DDI_ClearAlignInfo(UINT8 u1TsIdx);
EXTERN BOOL _DDI_SendAlignPackets(UINT8 u1Inst);
EXTERN BOOL _DMX_TS_SetHeader(DMX_PES_MSG_T *prPes);
// Emulation
#ifdef CC_DMX_EMULATION
EXTERN void _DDI_EMU_Notify(void);
EXTERN void _PVR_EMU_Notify(void);
EXTERN void _PVR_EMU_NotifyEx(UINT8 u1TsOutIdx);
EXTERN void _Framer_Unlock_Notify(void);

#endif // CC_DMX_EMULATION

// TS 130
EXTERN BOOL _DMX_TS130_Init(void);
EXTERN BOOL _DMX_TS130_SetPidArray(void);
EXTERN BOOL _DMX_TS130_MoveData(DMX_MM_DATA_T *prData, UINT32 u4PacketSize);
EXTERN BOOL _DMX_TS130_RequestReset(UINT8 u1TsIdx);
EXTERN void _DMX_TS130_ISR(void);
EXTERN BOOL _DMX_TS130_SendDummyPacket(UINT8 u1TsIdx, UINT8 u1Pidx);
#ifndef CC_TRUSTZONE_SUPPORT
EXTERN BOOL _DMX_TS134_MoveData(DMX_MM_DATA_T *prData);
#endif

#endif  // DMX_MM_H

