#ifndef __ADAPTOR_COM_INTF_H__
#define __ADAPTOR_COM_INTF_H__

#define _SIGNED_HALF(val)    ((val)>=0? 0.5:-0.5)

#define _MININTVAL(wl)    ((long long)~0<<((wl)-1))    //min value of the integer with word length wl
#define _MAXINTVAL(wl)    (~_MININTVAL(wl))    //max value of the integer with word length wl

#define _CLIP(val, wl)    ((val)> _MAXINTVAL(wl)? _MAXINTVAL(wl):((val)<_MININTVAL(wl)? _MININTVAL(wl):val))

#define _FLOAT_VAL_OF_FXP_REP(val, wl, iwl)    ((val)*((long long)1<<((wl)-(iwl))))

#define FILTER_FXP16(val, iwl)    ((short)_CLIP(_FLOAT_VAL_OF_FXP_REP(val, 16, iwl) + _SIGNED_HALF(val), 16))    //convert to 16-bit fxp with SRS_RND_INF mode
#define FILTER_FXP32(val, iwl)    ((int)_CLIP(_FLOAT_VAL_OF_FXP_REP(val, 32, iwl) + _SIGNED_HALF(val), 32))    //convert to 32-bit fxp with SRS_RND_INF mode
#define FILTER_FXP24(val, iwl)    ((int)_CLIP(_FLOAT_VAL_OF_FXP_REP(val, 24, iwl) + _SIGNED_HALF(val), 24))    //convert to 24-bit fxp with SRS_RND_INF mode

typedef enum __IIRFILTER_TYPE_E
{
    IIRFilterType_Unknown = 0,
    IIRFilterType_Butterworth2OrderSampleRate2x,
    IIRFilterType_Butterworth2OrderSampleRate4x,
    IIRFilterType_Butterworth2OrderSampleRate8x,
}IIRFILTER_TYPE_E;

typedef struct __IIR_FILTER_COFF_T
{
    IIRFILTER_TYPE_E eFilterType;    // The filter type
    unsigned int u4CoffCnt;    // Numerator Coff count
    unsigned int u4FixedPointIwl;    // Coff fixed point interger bit length
    const int *pi4NumeratorCoff;    // The pointer of Numerator Coff
    const int *pi4DenominatorCoff;    // The pointer of Denominator Coff
}IIR_FILTER_COFF_T;

typedef struct __IIR_FILTER_MAIN_T
{
    IIR_FILTER_COFF_T tFilterCoff;
    int *pi4HistoryOutput;    // The pointer of history output(Yn)
    int *pi4HistoryInput;    // The pointer of history input(Xn)
}IIR_FILTER_MAIN_T;

typedef struct __AUD_DOWNSAMPLE_OBJ_T
{
    IIR_FILTER_MAIN_T tIIRFilterObj;
    int ai4FilterHOutput[3];    // The pointer of history output(Yn)
    int ai4FilterHInput[3];    // The pointer of history input(Xn)
    unsigned int u4RatioNum;
    unsigned int u4InputCnt;
}AUD_DOWNSAMPLE_OBJ_T;

typedef struct __AUD_UPSAMPLE_OBJ_T
{
    IIR_FILTER_MAIN_T tIIRFilterObj;
    int ai4FilterHOutput[3];    // The pointer of history output(Yn)
    int ai4FilterHInput[3];    // The pointer of history input(Xn)
    int ai4UpsampleWeight[8];
    unsigned int u4RatioNum;
    int i4HistoryInput;
}AUD_UPSAMPLE_OBJ_T;

typedef struct __AUD_DROPSAMPLE_OBJ_T
{
    IIR_FILTER_MAIN_T tIIRFilterObj;
    int ai4FilterHOutput[3];    // The pointer of history output(Yn)
    int ai4FilterHInput[3];    // The pointer of history input(Xn)
    unsigned int u4RatioNum;
    unsigned int u4InputCnt;
    unsigned int u4IsBypassFilter;
}AUD_DROPSAMPLE_OBJ_T;

int i4IIR_Filter_Proc(IIR_FILTER_MAIN_T *ptIIRFilterMain, int i4Input, int *pi4Output);
int i4AudUpsampleLinearInterpolationInit(AUD_UPSAMPLE_OBJ_T *pAudUpsampleObj, unsigned int u4RatioNum);
int i4AudUpsampleLinearInterpolationProc(AUD_UPSAMPLE_OBJ_T *pAudUpsampleObj, int i4Input, int *pai4Output);
int i4AudDownsampleInit(AUD_DOWNSAMPLE_OBJ_T *pAudDownsampleObj, unsigned int u4RatioNum);
int i4AudDownsampleJudgeOutput(AUD_DOWNSAMPLE_OBJ_T *pAudDownsampleObj, int i4Input, int *i4Output);
int i4AudDropsampleInit(AUD_DROPSAMPLE_OBJ_T *pAudDropsampleObj, unsigned int u4RatioNum, unsigned int u4IsBypassFilter);
int i4AudDropsampleJudgeOutput(AUD_DROPSAMPLE_OBJ_T *pAudDropsampleObj, int i4Input, int *i4Output);

#endif