/* 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: xiaojun.zhu $
 * $Date: 2015/08/08 $
 * $RCSfile: muxer_drvif.h,v $
 * $Revision: #3 $
 *
 *---------------------------------------------------------------------------*/

/** @file muxer_drvif.h
 *  Public interface of software program stream demux driver
 */


#ifndef MUXER_DRVIF_H
#define MUXER_DRVIF_H

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

/*lint -save -e961 */
#include "x_common.h"
#include "x_typedef.h"
#include "x_timer.h"
#include "x_assert.h"
#include "x_os.h"
#include "x_bim.h"
#include "x_clksrc.h"
#include "x_hal_5381.h"
#include "x_hal_926.h"
#include "x_util.h"
#include "muxer_if.h"
#include "gcpu_if.h" // for scramble

//-----------------------------------------------------------------------------
// Macro definitions
//-----------------------------------------------------------------------------
#define MUXER_THREAD_PRIORITY  90  // ToDo: move it to drv_common.h and assign a fine value

#define MUXER_MAX_INST_NS   2

#define MUXER_MAX_STRM_NS   3

#define MUXER_MAX_SECTION_NS 9

#define MUXER_SECTION_BUF_SIZE   (4 * 1024)

#define MUXER_CMD_Q_SIZE 10
#define MUXER_VID_PES_Q_SIZE 1024
#define MUXER_AUD_PES_Q_SIZE 1024
#define MUXER_VBI_PES_Q_SIZE 1024
#define MUXER_DATA_Q_SIZE    1024

#define MUXER_VID_HDR_BUF_SIZE 128

#define MUXER_FPS_ACCURACY 1000

#define MUXER_INVALID_UINT32  0xFFFFFFFF
#define MUXER_INVALID_STRM_ID  0xFFFFFFFF

#define MUXER_TMP_BUF_SZ    (512 * 1024)
#define MUXER_SI_BUF_SIZE   (5 * 1024)

#define MUXER_VERIFY_ID_RET(id, ret)                                        \
do {                                                                        \
    if (MUXER_MAX_INST_NS <= (id))                                          \
    {                                                                       \
        LOG(3, "%s(%d): wrong id(%d)!!!\n", __FUNCTION__, __LINE__, (id));  \
        return ret;                                                         \
    }                                                                       \
} while (0)

#define MUXER_VERIFY_ID_SET(id, def)                                        \
do {                                                                        \
    if (MUXER_MAX_INST_NS <= (id))                                          \
    {                                                                       \
        LOG(3, "%s(%d): wrong id(%d)!!!\n", __FUNCTION__, __LINE__, (id));  \
        ASSERT(MUXER_MAX_INST_NS > (id));                                   \
        (id) = (def);                                                       \
    }                                                                       \
} while (0)

#define _MxInitSyncCmdFlag(obj)    x_sema_lock(obj->hMxSyncSema, X_SEMA_OPTION_NOWAIT)
#define _MxWaitSyncCmdDone(obj)    VERIFY(x_sema_lock(obj->hMxSyncSema, X_SEMA_OPTION_WAIT) == OSR_OK)
#define _MxNotifySyncCmdDone(obj)  VERIFY(x_sema_unlock(obj->hMxSyncSema) == OSR_OK)


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

typedef struct
{
    UINT32 u4Pcr;
    UINT32 u4Timestamp; // 192 timestamp
    UINT32 u4CurVidPts;
    UINT32 u4CurAudPts;
    UINT32 u4CurVbiPts;
    UINT32 u4AudResidueLen;
    UINT32 u4AudResiduePts;
    UCHAR *pucAudResidueBuf;

    UINT8 u1NetContinuity;
    UINT8 u1PatContinuity;
    UINT8 u1PmtContinuity;
    UINT8 u1VidContinuity;
    UINT8 u1AudContinuity;
	UINT8 u1SiContinuity;
    UINT8 u1TtxContinuity;
    UINT8 u1SectContinuity[MUXER_MAX_SECTION_NS];

#ifdef MUXER_VID_HDR_BUF_SIZE
    UCHAR aucVidHdrBuf[MUXER_VID_HDR_BUF_SIZE];
    UPTR u4VidHdrBufWp;
    UINT32 u4VidHdrBufSz;
#endif
} MUXER_MPG_INFO_T;

typedef struct
{
    UINT32 u4CurVidPts;
    UINT32 u4CurAudPts;
} MUXER_CMPB_INFO_T;

typedef struct
{
    UINT32 u4CurVidPts;
    UINT32 u4CurAudPts;
} MUXER_RAW_INFO_T;

typedef enum
{
    eMUXER_HDR_SUCCEED,      // header muxing succeed
    eMUXER_HDR_FAIL,         // header muxing fail
    eMUXER_HDR_NO_DATA,      // no data
    eMUXER_HDR_UNKNOWN_TYPE, // unknown muxing type
} MUXER_HDR_STATE_T;

typedef struct
{
    BOOL fgEnabled;
    UINT32 u4SecTionPts;
    MUXER_ADD_SECTION_INFO_T rAddSection;
} MUXER_SECTION_T;

typedef struct
{
    ENUM_MUXER_STRMTYPE_T eStrmType;
    BOOL fgEnabled;
    UINT32 u4CurPts;
} MUXER_STRM_T;

typedef BOOL (*PFN_START)(UCHAR ucMxId);
typedef BOOL (*PFN_STOP)(UCHAR ucMxId);
typedef MUXER_HDR_STATE_T (*PFN_MULTIPLEX_HDR)(UCHAR ucMxId);

typedef struct
{
    UCHAR   ucMxId;
    BOOL    fgMuxerInited;
    BOOL    fgOccupied;
    BOOL    fgEnVideo;
    BOOL    fgEnAudio;
    BOOL    fgEnTeletext;
    BOOL    fgScramble;
	BOOL    fgEnSI;
    BOOL    fgGetSI;
    BOOL    fgKeyframe;
    BOOL    fgFrame;
    BOOL    fgTrustZone;
    BOOL    fgFirstSI;

    HANDLE_T hMuxerMainThread;
    HANDLE_T hMuxerCmdQ;
    HANDLE_T hMuxerVidDataQ;
    HANDLE_T hMuxerAudDataQ;
    HANDLE_T hMuxerVBIDataQ;
    HANDLE_T hMuxerNfyTimer;
    HANDLE_T hMxSyncSema;
    HANDLE_T hMxWaitBufTimer;
    HANDLE_T hMxWaitBufSema;

    UCHAR *pucMxBuf;
    UCHAR *pucTmpFrmBuf;
    MX_FCT_NFY pfNotify;
    PFN_START pfnStart;
    PFN_STOP  pfnStop;
    PFN_MULTIPLEX_HDR pfnMuxHdr;

    UINT32 u4ValidStrmNs;
    UINT32 u4VidCnt;
    UINT32 u4AudCnt;
    UINT32 u4VBICnt;
    UINT32 u4RecTotalTime;
    UINT32 u4MxBufSz;
    UPTR u4MxBufWpNfy; // Notify until whole pes done
    UPTR u4MxBufWp;
    UPTR u4MxBufRp;
    UINT32 u4NfyPeriod;
    UPTR u4LastNfyBufWp;
    UINT32 u4BitPos;
    UINT32 u4MxBufFrmCnt;
    UINT16 u2VideoPid;
    UINT16 u2AudioPid;  
    UINT16 u2PcrPid;

    ENUM_MUXER_STRMTYPE_T eCurrentWaiting;
    ENUM_MUXER_STATE_T eMuxerPrevousStatus;
    ENUM_MUXER_STATE_T eMuxerStatus;
    ENUM_MUXER_FMT_T eCurFMT;
    ENUM_MUXER_VENC_T eVidFmt;
    ENUM_MUXER_AENC_T eAudFmt;

    VENC_PES_INFO_T rVEncPesInfo;
    AENC_PES_INFO_T rAEncPesInfo;
    VBIENC_PES_INFO_T rVBIEncPesInfo;
    MUXER_STRM_T arStrm[MUXER_MAX_STRM_NS];
    MUXER_SECTION_T arSection[MUXER_MAX_SECTION_NS];
    TSDESC_PARAM_T rTsDescParam; // for scramble

    union
    {
        MUXER_MPG_INFO_T  rMuxerMpgInfo;
        MUXER_CMPB_INFO_T rMuxerCmpbInfo;
        MUXER_RAW_INFO_T  rMuxerRawInfo;
    } rFmtInfo;
	UINT32 u4SiLen;
    UCHAR *puKnlSiAddr;
    UINT32 u4KnlSISize;
} MUXER_INFO_T;


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

//-----------------------------------------------------------------------------
// Prototype  of public functions
//-----------------------------------------------------------------------------
VOID _MUXER_Init(VOID);

MX_RET _MUXER_GetInst(UCHAR *pucMxId);

MX_RET _MUXER_ReleaseInst(UCHAR ucMxId);

MUXER_INFO_T *_MUXER_QueryInst(UCHAR ucMxId);

BOOL _MUXER_Start(UCHAR ucMxId);

BOOL _MUXER_Pause(UCHAR ucMxId);

BOOL _MUXER_Stop(UCHAR ucMxId);

BOOL _MUXER_Flush(UCHAR ucMxId);

BOOL _MUXER_SendVideoPes(UCHAR ucMxId, VENC_PES_INFO_T *prVEncPes);

BOOL _MUXER_SendAudioPes(UCHAR ucMxId, AENC_PES_INFO_T *prAEncPes);

BOOL _MUXER_SendVBIPes(UCHAR ucMxId, VBIENC_PES_INFO_T *prVBIEncPes);

BOOL _MUXER_ReceiveVideoPes(UCHAR ucMxId);

BOOL _MUXER_ReceiveAudioPes(UCHAR ucMxId);

BOOL _MUXER_ReceiveVBIPes(UCHAR ucMxId);

BOOL _MUXER_QueryVideoPesCnt(UCHAR ucMxId, UINT16 *pu2QueueSize);

BOOL _MUXER_QueryAudioPesCnt(UCHAR ucMxId, UINT16 *pu2QueueSize);

BOOL _MUXER_QueryVBIPesCnt(UCHAR ucMxId, UINT16 *pu2QueueSize);

VOID _MUXER_FlushData(UCHAR ucMxId);

BOOL _MUXER_SetMuxFmt(UCHAR ucMxId, ENUM_MUXER_FMT_T eMuxFmt);

BOOL _MUXER_SetVencFmt(UCHAR ucMxId, ENUM_MUXER_VENC_T eVidFmt);

BOOL _MUXER_SetAencFmt(UCHAR ucMxId, ENUM_MUXER_AENC_T eAudFmt);

BOOL _MUXER_SetSI(UCHAR ucMxId, UCHAR * pBuf, UINT32 u4Len);

BOOL _MUXER_SetBuffer(UCHAR ucMxId, UPTR u4BufSa, UINT32 u4BufSz, MX_FCT_NFY pfNotify, UINT32 u4NfyPeriod);

BOOL _MUXER_UpdateRp(UCHAR ucMxId, UPTR u4Rp);

BOOL _MUXER_AddStrm(UCHAR ucMxId, ENUM_MUXER_STRMTYPE_T eStrm, UINT32 *pu4StrmNo);

BOOL _MUXER_DelStrm(UCHAR ucMxId, UINT32 u4StrmNo);

BOOL _MUXER_SetScramble(UCHAR ucMxId, BOOL fgScramble);

BOOL _MUXER_SetScrambleKey(UCHAR ucMxId, MM_PVR_CRYPT_INFO_T *ptPvrCrypt);

BOOL _MUXER_Query(UCHAR ucMxId);

VOID vMxPutBits(MUXER_INFO_T *prMuxInfo, UINT32 u4Pattern, UINT32 u4BitLen);

VOID vMxCopyBytes(MUXER_INFO_T *prMuxInfo, UPTR u4StartAddr, UINT32 u4FrameSize);

BOOL fgMxBufferAvail(MUXER_INFO_T *prMuxInfo, UINT32 u4Len);

BOOL MxWaitBufferAvail(MUXER_INFO_T *prMuxInfo, UINT32 u4Len);

VOID vMxNotifyDataReady(MUXER_INFO_T *prMuxInfo, UINT32 u4Pts);

VOID _vMxUpdatePts(MUXER_INFO_T *prMuxInfo, UINT32 u4StrmNo, UINT32 u4Pts);

VOID _MxStrmSelect(MUXER_INFO_T *prMuxInfo, ENUM_MUXER_STRMTYPE_T *peStrm, UINT32 *pu4StrmNo);

VOID _MxVideoTypeAnalyze(MUXER_INFO_T *prMuxInfo, VENC_PES_INFO_T *prVidPesInfo);

BOOL _MUXER_ConfigPid(UCHAR ucMxId, ENUM_MUXER_PID_TYPE_T ePidType, UINT16 u2Pid);

BOOL _MUXER_AddSection(UCHAR ucMxId, MUXER_ADD_SECTION_INFO_T *ptAddSection);

BOOL _MUXER_DelSection(UCHAR ucMxId, INT32 i4SectionId);

BOOL _MUXER_UseTrustZone(UCHAR ucMxId, BOOL fgTrustZone);

#endif  // MUXER_DRVIF_H

