/* 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_if.h,v $
 * $Revision: #3 $
 *
 *---------------------------------------------------------------------------*/

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


#ifndef MUXER_IF_H
#define MUXER_IF_H

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

/*lint -save -e961 */
#include "drv_common.h"
#include "x_common.h"
#include "x_mm_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"


//-----------------------------------------------------------------------------
// Macro definitions
//-----------------------------------------------------------------------------


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

typedef enum
{
    MX_RET_OK                =  0,
    MX_RET_GNRL_ERR          = -1,
    MX_RET_NO_AVAIL_INST     = -2,
    MX_RET_INST_NOT_OCCUPIED = -3
} MX_RET;


typedef enum
{
    eMUXER_STATE_STOP,          // stop
    eMUXER_STATE_PAUSE,       // pause
    eMUXER_STATE_RUN,           // run
    eMUXER_STATE_FLUSH,
    eMUXER_STATE_MAX
} ENUM_MUXER_STATE_T;


typedef enum
{
    eMUXER_STRMTYPE_VIDEO,          // video
    eMUXER_STRMTYPE_AUDIO,          // audio
    eMUXER_STRMTYPE_VBI,            // teletext
    eMUXER_STRMTYPE_EIT, 
    eMUXER_STRMTYPE_MAX
} ENUM_MUXER_STRMTYPE_T;


/** ENUM_MUXER_PID_TYPE_T
 * pid type
 */
typedef enum
{
    eMUXER_PID_TYPE_VIDEO = 0,                      ///< pid type video
    eMUXER_PID_TYPE_AUDIO,                          ///< pid type audio
    eMUXER_PID_TYPE_PCR,                            ///< pid type pcr
    eMUXER_PID_TYPE_OTHER                           ///< pid type unknown
} ENUM_MUXER_PID_TYPE_T;


typedef struct
{
    UPTR   u4FifoStart;
    UPTR   u4FifoEnd;
    UPTR   u4StartAddr;
    UINT32 u4FrameSize;
    UINT32 u4VideoType;
    UINT32 u4Pts;
    VOID*  pvTag;
} VENC_PES_INFO_T;


typedef struct
{
    UPTR   u4FifoStart;
    UPTR   u4FifoEnd;
    UPTR   u4StartAddr;
    UINT32 u4Pts;
    UINT32 u4FrameSize;
} AENC_PES_INFO_T;

typedef struct
{
    UINT32 u4Handle;
    UINT16 u2Pid;
    INT32  i4Repetitiontime;
    INT32  i4Sectionid;    
    INT32  i4sectionlen;
    UINT8* pu1sectiondata;
} MUXER_ADD_SECTION_INFO_T;

typedef struct
{
    UPTR   u4FifoStart;
    UPTR   u4FifoEnd;
    UPTR   u4StartAddr;
    UINT32 u4Pts;
    UINT32 u4FrameSize;
} VBIENC_PES_INFO_T;


typedef struct
{
    UPTR u4FifoStart;
    UPTR u4FifoEnd;
    UPTR u4StartAddr;
    UINT32 u4FrameSize;
    UINT32 u4PesType;
} MUXER_PES_INFO_T;


/*
*   Container format.
*/
typedef enum
{
    MUXER_FMT_UNKNOWN,
    MUXER_FMT_AVI,
    MUXER_FMT_MPEG1_DAT,
    MUXER_FMT_MPEG2_PS,
    MUXER_FMT_MPEG2_TS,         // 188 ts
    MUXER_FMT_MPEG2_TS_192,     // 192 ts, time case
    MUXER_FMT_MP4,
    MUXER_FMT_CMPB,
    MUXER_FMT_RAW
} ENUM_MUXER_FMT_T;


/*
*   The video codec format.
*/
typedef enum
{
    ENUM_MUXER_VENC_UNKNOWN,
    ENUM_MUXER_VENC_MPEG1,
    ENUM_MUXER_VENC_MPEG2,
    ENUM_MUXER_VENC_H264,
    ENUM_MUXER_VENC_MAX
}ENUM_MUXER_VENC_T;


/*
*   The Audio codec format.
*/
typedef enum
{
    ENUM_MUXER_AENC_UNKNOWN = 0,        /* Unknown Audio Codec Type*/
    ENUM_MUXER_AENC_MPEG,               /* MPEG1 or MPEG2 audio, layer I or layer II*/
    ENUM_MUXER_AENC_MP3,                /* MPEG1 or MPEG2 audio, layer III*/
    ENUM_MUXER_AENC_AAC,                /* AAC, or HE-AAC*/
    ENUM_MUXER_AENC_DD,                 /* Dolby Digital, or Dolby Digital Plus*/
    ENUM_MUXER_AENC_TRUEHD,             /* Dolby TrueHD*/
    ENUM_MUXER_AENC_PCM,                /* PCM*/
    ENUM_MUXER_AENC_DTS,                /* DTS*/
    ENUM_MUXER_AENC_DTS_HD_HR,          /* DTS-HD High Resolution*/
    ENUM_MUXER_AENC_DTS_HD_MA,          /* DTS-HD Master Audio*/
    ENUM_MUXER_AENC_WMA,                /* WMA standard; not WMA Pro nor WMA Lossless*/
    ENUM_MUXER_AENC_COOK,               /* Cook*/
    ENUM_MUXER_AENC_VORBIS,             /* Vorbis*/
    ENUM_MUXER_AENC_FLAC,               /* Free Lossless Audio Codec*/
    ENUM_MUXER_AENC_MONKEY              /* Monkey's Audio*/
}ENUM_MUXER_AENC_T;


/*
*   Data Notification structure
*/
typedef struct _MX_CB_DATA_T
{
    UPTR   u4Addr;
    UINT32 u4Len;
    UINT32 u4Pts;
    UINT32 u4Flag;
} MX_CB_DATA_T;


/* notification conditions */
typedef enum _MX_NFY_COND_T
{
    MX_COND_ERROR    = -2, /* Error */
    MX_COND_OVERFLOW = -1, /* Buffer Overflow */
    MX_COND_DATA     =  0, /* Buffer Data Available */
} MX_NFY_COND_T;


/* Overflow reasons */
typedef enum _MX_OVERFLOW_REASON_T
{
    MX_OVERFLOW_REASON_RING_BUF_FULL    = 1,
    MX_OVERFLOW_REASON_METADATA_BUF_FULL = 2
} MX_OVERFLOW_REASON_T;


/* Error reasons */
typedef enum _MX_ERROR_REASON_T
{
    MX_ERROR_REASON_INTERNAL    = 1
} MX_ERROR_REASON_T;


/*
*   Notification function
*/
typedef VOID (*MX_FCT_NFY)(UCHAR ucMxId, MX_NFY_COND_T eType, VOID *pvData);


//-----------------------------------------------------------------------------
// 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);

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_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);

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_IF_H

