/*****************************************************************************
 * Copyright (c) 2018, Broadcom Inc.                                         *
 *                                                                           *
 * All Rights Reserved.                                                      *
 *                                                                           *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Inc.;             *
 * the contents of this file may not be disclosed to third parties, copied   *
 * or duplicated in any form, in whole or in part, without the prior         *
 * written permission of Broadcom Inc.                                       *
 *****************************************************************************/
/**
 * @file vend_sp.h
 * @brief Header file for overlay
 * @Note
 */
#ifndef __VEND_SP_H__
#define __VEND_SP_H__

#ifndef ENABLE_SDK
#include "dsp_sdk_types.h"
#endif

//#ifdef ENABLE_INEAR_ALGO
#include "SamsungSolomonVoiceW.h"
//#endif


/**
 * Definitions.
 */
#if 0   // samsung modification
/* Memory sizes. Should be tuned according to really need */
#define DYVE_SCRATCH_MEM_SIZE (10*1024)  /* DYVE: 10KB */
#define DYVE_SCRATCH_MEM_SIZE_INT32 ((DYVE_SCRATCH_MEM_SIZE+3)>>2)

#define SS_SPEECH_ENC_SCRATCH_MEM_SIZE (10*1024) /* SS ENC: 10KB */
#define SS_SPEECH_ENC_SCRATCH_MEM_SIZE_INT32 ((SS_SPEECH_ENC_SCRATCH_MEM_SIZE+3)>>2)

#define SS_SPEECH_DEC_SCRATCH_MEM_SIZE (10*1024) /* SS DEC: 10KB */
#define SS_SPEECH_DEC_SCRATCH_MEM_SIZE_INT32 ((SS_SPEECH_DEC_SCRATCH_MEM_SIZE+3)>>2)

#define INEAR_SCRATCH_MEM_SIZE (30*1024) /* Inear: 30KB */
#define INEAR_SCRATCH_MEM_SIZE_INT32 ((INEAR_SCRATCH_MEM_SIZE+3)>>2)
#else
/* Memory sizes. Should be tuned according to really need */
#define DYVE_SCRATCH_MEM_SIZE (1*1024)  /* DYVE: 1KB */
#define DYVE_SCRATCH_MEM_SIZE_INT32 ((DYVE_SCRATCH_MEM_SIZE+3)>>2)

#define SS_SPEECH_ENC_SCRATCH_MEM_SIZE (5*1024) /* SS ENC: 5KB */
#define SS_SPEECH_ENC_SCRATCH_MEM_SIZE_INT32 ((SS_SPEECH_ENC_SCRATCH_MEM_SIZE+3)>>2)

#define SS_SPEECH_DEC_SCRATCH_MEM_SIZE (5*1024) /* SS DEC: 5KB */
#define SS_SPEECH_DEC_SCRATCH_MEM_SIZE_INT32 ((SS_SPEECH_DEC_SCRATCH_MEM_SIZE+3)>>2)

#define INEAR_SCRATCH_MEM_SIZE (/*97*//*145*/1*1024) /* Inear: 35KB */
#define INEAR_SCRATCH_MEM_SIZE_INT32 ((INEAR_SCRATCH_MEM_SIZE+3)>>2)
#endif


/* Common definitions */
#define WBS_FRAME 120
#define WBS_FRAME_BYTE (WBS_FRAME*2)

/* SS Speech definitions */
#define SS_SPEECH_FS 16000
#define SS_SPEECH_MAX_PACKET 30
#define SS_SPEECH_FRAME_SIZE 120
#define SS_SPEECH_DATA_LEN 28


/* DyVE definitions */
#define DYVE_DIAMONDVOICE_FRAME_NB 160
#define DYVE_DIAMONDVOICE_FRAME_WB 320

#define DYVE_LONG_BUF_LENGTH 480
#define DYVE_LONG_BUF_LENGTH_BYTE (DYVE_LONG_BUF_LENGTH*2)

/* Inear definitions */
#define INEAR_FRAME_SIZE_NB 160
#define INEAR_FRAME_SIZE_WB 320
#define INEAR_LONG_BUF_LENGTH 480
#define INEAR_LONG_BUF_LENGTH_BYTE (INEAR_LONG_BUF_LENGTH*2)

/* 7.5ms short buffer size definitions */
#define SHORT_BUF_LENGTH_SAMPLE 120
#define SHORT_BUF_LENGTH_BYTE   (SHORT_BUF_LENGTH_SAMPLE*2)



/**
 * Extern Variables
 */
extern UINT8 g_inear_in_main_long_buf[INEAR_LONG_BUF_LENGTH_BYTE] __attribute__((aligned(4)));       /* input 20ms buffer (allocate 30ms buffering)     */
extern UINT8 g_inear_in_sub_long_buf[INEAR_LONG_BUF_LENGTH_BYTE] __attribute__((aligned(4)));        /* input 20ms buffer (allocate 30ms buffering)     */
extern UINT8 g_inear_in_third_long_buf[INEAR_LONG_BUF_LENGTH_BYTE] __attribute__((aligned(4)));      /* input 20ms buffer (allocate 30ms buffering)      */
extern UINT8 g_inear_in_long_buf_rx[INEAR_LONG_BUF_LENGTH_BYTE] __attribute__((aligned(4)));         /* rxinput buffer                                  */
extern UINT8 g_inear_out_long_buf[INEAR_LONG_BUF_LENGTH_BYTE] __attribute__((aligned(4)));           /* output 20ms buffer (allocate 30ms buffering)    */
#ifdef ENABLE_DYVE_ALGO_AND_BUFF
extern UINT8 g_dyve_in_long_buf[DYVE_LONG_BUF_LENGTH_BYTE] __attribute__((aligned(4)));              /* input 20ms buffer (allocate 30ms buffering)     */
extern UINT8 g_dyve_in_long_buf_tx[DYVE_LONG_BUF_LENGTH_BYTE] __attribute__((aligned(4)));           /* input 20ms buffer (allocate 30ms buffering)     */
extern UINT8 g_dyve_out_long_buf[DYVE_LONG_BUF_LENGTH_BYTE] __attribute__((aligned(4)));             /* output 20ms buffer (allocate 30ms buffering)    */
#else
extern UINT8 g_inear_in_long_buf_rx_backup_two_frame[INEAR_LONG_BUF_LENGTH] __attribute__((aligned(4)));    /* backup 15ms buffer for g_inear_in_long_buf_rx before encoder start */
#endif

/**
 *  DyVE external parameters
 */
typedef struct
{
    INT32  sampling_rate;               /* sampling rate                                   */   /* Updated by wrapper        */
    INT16  dec_frame_size;              /* number of samples per decoder frmae for DyVE IO */   /* Updated by wrapper        */
    INT16  frame_size;                  /* number of PCM samples                           */   /* Updated by wrapper        */

    void  *dyve_vars;                   /* DyVE engine handler                             */   /* Updated by entry function */
    void  *dyve_param;                  /* DyVE tuning parameters                          */   /* Updated by entry function */
    INT16 *in_long_buf;
    INT16 *in_long_buf_tx;
    INT16 *out_long_buf;

    INT32  short_input_frame_cnt;       /* input 7.5ms frame counter                       */   /* Updated by entry function */
    INT32  long_input_frame_cnt;        /* input 20ms frame counter                        */   /* Updated by entry function */
    INT32  short_output_frame_cnt;      /* output 7.5ms frame counter                      */   /* Updated by entry function */
    INT32  long_output_frame_cnt;       /* output 20ms frame counter                       */   /* Updated by entry function */
    INT32  dec_cnt;                     /* sppech decoder function entering times          */
    INT16  dyve_start_threshold;        /* DyVE starts by the threshold                    */
}DYVE_EXT_ST_t;



/**
 *  Inear external parameters
 */
typedef struct
{
    INT32  sampling_rate;               /* sampling rate                                    */   /* Updated by wrapper        */
    INT16  enc_frame_size;              /* number of samples per encoder frmae for inear IO */   /* Updated by wrapper        */
    INT16  frame_size;                  /* number of PCM samples                            */   /* Updated by wrapper        */
#if 0
    void  *inear_engine;                /* Inear engine handler                             */   /* Updated by entry function */
    void  *inear_param;                 /* Inear tuning parameters                          */   /* Updated by entry function */
#else
    SamsungSolomonVoiceParam           *DiamondVoice_DV_TX_Config;           /* Inear tuning parameters                          */   /* Updated by entry function */
    SamsungSolomonVoiceParam           *DiamondVoice_DV_TX_Config_Outer;     /* Inear tuning parameters                          */   /* Updated by entry function */
    SamsungSolomonVoiceEngine_Handle_t *SamsungDiamondVoiceEngine_vars_Tx;   /* Inear engine handler                             */   /* Updated by entry function */
#endif

    INT16 *in_main_long_buf;            /* input 20ms buffer (allocate 30ms buffering)      */   /* Updated by entry function */
    INT16 *in_sub_long_buf;             /* input 20ms buffer (allocate 30ms buffering)      */   /* Updated by entry function */
    INT16 *in_third_long_buf;           /* input 20ms buffer (allocate 30ms buffering)      */   /* Updated by entry function */
    INT16 *in_long_buf_rx;              /* rxinput buffer                                   */   /* Updated by entry function */
    INT16 *out_long_buf;                /* output 20ms buffer (allocate 30ms buffering)     */   /* Updated by entry function */
    INT32  mem_offset;                  /* runtime memory size                              */   /* Updated by entry function */
    INT32  short_in_main_frame_cnt;     /* input 7.5ms frame counter                        */   /* Updated by entry function */
    INT32  long_in_main_frame_cnt;      /* input 20ms frame counter                         */   /* Updated by entry function */
    INT32  short_output_frame_cnt;      /* output 7.5ms frame counter                       */   /* Updated by entry function */
    INT32  long_output_frame_cnt;       /* output 20ms frame counter                        */   /* Updated by entry function */
    INT32  inear_in_farend_offset_latch;/* keep the previous inear in farend offset value   */
    INT16  inear_start_threshold;       /* Inear starts by the threshold                    */
    INT32  enc_cnt;                     /* speech encoder function entering times           */
    INT16  enc_start;                   /* start of the 1st good packet for encoder         */
    UINT32 main_in_cnt;
    UINT32 sub_in_cnt;
    UINT32 third_in_cnt;
    UINT32 rx_in_cnt;
    UINT32 out_cnt;
    UINT32 inear_fail_cnt;
    UINT32 dump_inear_in_cnt;
    UINT32 dump_inear_out_cnt;
}INEAR_EXT_ST_t;




/**
 *  SS speech decoder external parameters
 */
typedef struct
{
    //INT32  sampling_rate;    /* sampling rate                   */   /* Updated by wrapper        */
    INT16  plc_frame;        /* 1: lost frame, 0: good frame    */   /* Updated by wrapper        */

    void  *dec_inst;         /* decoder instance                */   /* Updated by entry function */
    UINT8 *in_buf;           /* input buffer                    */   /* Updated by entry function */
    UINT8 *out_buf;          /* output buffer                   */   /* Updated by entry function */
    INT32  mem_offset;        /* runtime memory size             */   /* Updated by entry function */
    INT32  frame_cnt;         /* frame counter                   */   /* Updated by entry function */
    INT32  dram_dec_size;     /* Size of bytes of dram_dec       */   /* Updated by entry function */
    UINT32 dec_cnt;           /* decoder counter number after good */
}SS_SPEECH_DEC_EXT_ST_t;


/**
 *  SS speech encoder external parameters
 */
typedef struct
{
    void  *enc_inst;        /* encoder instance             */   /* Updated by entry function */
    UINT8 *in_buf;          /* input buffer                 */   /* Updated by entry function */
    UINT8 *out_buf;         /* output buffer                */   /* Updated by entry function */
    INT32  frame_size;      /* number of PCM samples        */   /* Updated by entry function */
    INT32  mem_offset;      /* runtime memory size          */   /* Updated by entry function */
    INT32  frame_cnt;       /* frame counter                */   /* Updated by entry function */
    INT32  dram_enc_size;   /* Size of bytes of dram_enc    */   /* Updated by entry function */

}SS_SPEECH_ENC_EXT_ST_t;


#endif //__VEND_SP_H__
