/*
***********************************************************************
*
* Copyright (C) 1999 Ericsson Radio Systems AB
* 
* The Association of Radio Industries and Businesses (ARIB) has the
* licence to reproduce, distribute and publish the Bit Exact C Code
* programs in this CD-ROM together with the other parts of the RCR
* STD-27 (Personal Digital Cellular Telecommunication System)
* specification.
* 
* Ericsson has declared Option 1 (royalty-free licence) to ARIB for
* its Essential Industrial Property Rights related to the ACELP
* specification and the Bit Exact C Code programs. The royalty-free
* licence is limited to the development and manufacturing of
* products complying with the RCR STD-27H specification (or
* subsequent versions) for sale and use in Japan. Copying of the
* program(s) for any other purpose requires the prior written
* permission from Ericsson.
* 
* The user of the Bit Exact C Code hereby acknowledges the
* following:
* 
* 1.  Ericsson does not provide the user with any warranties,
*     whether express or implied, including but not limited to
*     warranties of merchantability and fitness for a particular
*     purpose relating to the Bit Exact C Code.
* 
* 2.  Ericsson shall have no responsibility whatsoever to indemnify
*     the user against actions or claims for infringement of any
*     intellectual property rights, e.g. patents, copyrights,
*     registered designs or any other rights, by reason of the
*     user's use of the Bit Exact C Code.
* 
***********************************************************************
*/

void Init_Coder_6k7(void);

void Coder_6k7(
    Word16 vox_mode,/* (i)  : vox mode flag       */ 
    Word16 vox_ctrl,/* (i)  : vox control flag    */ 
    Word16 vad_ctrl,/* (i)  : VAD control flag    */ 
    Word16 ana[],   /* (o)  : Analysis parameters */
    Word16 synth[]  /* (o)  : Local synthesis     */
);

void Init_Decoder_6k7(void);

void Decoder_6k7(
    Word16 parm[],  /* (i) : vector of synthesis parameters      */
    Word16 bfi,     /* (i) : bfi flag                            */
    Word16 synth[], /* (o) : synthesis speech                    */
    Word16 A_t[]    /* (o) : decoded LP filter in 4 subframes    */
);

void Bits2prm_6k7(
    Word16 bits[],  /* (i) : serial bits (134 + bfi)                */
    Word16 prm[]    /* (o) : analysis parameters  (19+1 parameters) */
);

void Prm2bits_6k7(
    Word16 prm[],  /* (i) : analysis parameters (19+2 parameters)    */
    Word16 bits[]  /* (o) : serial bits (1*bfi + 2*frame type + 134) */
);

void Init_Post_Filter(void);

void Post_Filter(
    Word16 * syn,  /* (i/o): synthesis speech (postfiltered is output)    */
    Word16 * Az_4  /* (i)  : interpolated LPC parameters in all subframes */
);

Word16 code_3i40_14bits(
    Word16 x[],   /* (i)  : target vector                                 */
    Word16 h[],   /* (i)  : impulse response of weighted synthesis filter */
                  /*        h[-L_subfr..-1] must be set to zero.          */
    Word16 T0,    /* (i)  : Pitch lag                                     */
    Word16 pitch_sharp, /* (i)  : Last quantized pitch gain               */
    Word16 code[],      /* (o)  : Innovative codebook                     */
    Word16 y[],   /* (o)  : filtered fixed codebook excitation            */
    Word16 * sign /* (o)  : Signs of 4 pulses                             */
);
void Decod_ACELP(
    Word16 sign,   /* (i)  : signs of 3 pulses.                           */
    Word16 index,  /* (i)  : Positions of the 3 pulses.                   */
    Word16 cod[]   /* (o)  : algebraic (fixed) codebook excitation        */
);

void Int_lpc(
    Word16 lsp_old[], /* (i) : LSP vector at the 4th subframe of past frame    */
    Word16 lsp_new[], /* (i) : LSP vector at the 4th subframe of present frame */
    Word16 Az[]       /* (o) : interpolated LP parameters in all subframes     */
);
void Int_lpc2(
    Word16 lsp_old[],   /* (i) : LSP vector at the 4th subframe of past frame    */
    Word16 lsp_new[],   /* (i) : LSP vector at the 4th subframe of present frame */
    Word16 Az[] /* (o) : interpolated LP parameters in subframes 1.2.3   */
);

void Pred_lt_3(
    Word16 exc[],  /* (i/o): excitation buffer */
    Word16 T0,     /* (i)  : integer pitch lag */
    Word16 frac,   /* (i)  : fraction of lag   */
    Word16 L_subfr /* (i)  : subframe size     */
);

Word16 Pitch_ol(     /* (o) : open loop pitch lag                            */
    Word16 signal[], /* (i) : signal used to compute the open loop pitch     */
                     /*       signal[-pit_max] to signal[-1] should be known */
    Word16 pit_min,  /* (i) : minimum pitch lag                              */
    Word16 pit_max,  /* (i) : maximum pitch lag                              */
    Word16 L_frame   /* (i) : length of frame to compute pitch               */
);
Word16 Pitch_fr3(     /* (o) : pitch period.                            */
    Word16 exc[],     /* (i) : excitation buffer                        */
    Word16 xn[],      /* (i) : target vector                            */
    Word16 h[],       /* (i) : impulse response of filters.             */
    Word16 L_subfr,   /* (i) : Length of subframe                       */
    Word16 t0_min,    /* (i) : minimum value in the searched range.     */
    Word16 t0_max,    /* (i) : maximum value in the searched range.     */
    Word16 i_subfr,   /* (i) : indicator for first subframe.            */
    Word16 * pit_frac /* (o) : chosen fraction.                         */
);
Word16 G_pitch(       /* (o) : Gain of pitch lag saturated to 1.2       */
    Word16 xn[],      /* (i) : Pitch target.                            */
    Word16 y1[],      /* (i) : Filtered adaptive codebook.              */
    Word16 g_coeff[], /* (i) : Correlations need for gain quantization. */
    Word16 L_subfr    /* (i) : Length of subframe.                      */
);
Word16 Enc_lag3(      /* (o)  : Return index of encoding */
    Word16 T0,        /* (i)  : Pitch delay              */
    Word16 T0_frac,   /* (i)  : Fractional pitch delay   */
    Word16 * T0_min,  /* (i/o): Minimum search delay     */
    Word16 * T0_max,  /* (i/o): Maximum search delay     */
    Word16 pit_min,   /* (i)  : Minimum pitch delay      */
    Word16 pit_max,   /* (i)  : Maximum pitch delay      */
    Word16 pit_flag   /* (i)  : Flag for 1st subframe    */
);

Word16 Qua_gain(        /*  Return index of quantization.     */
    Word16 xn[],        /* (i) : Target vector.               */
    Word16 y1[],        /* (i) : Adaptive codebook.           */
    Word16 Y2[],        /* (i) : Filtered innovative vector.  */
    Word16 code[],      /* (i) : Innovative vector.           */
    Word16 g_coeff[],   /* (i) : Correlations <xn y1> <y1 y1> */
                        /*       Compute in G_pitch().        */
    Word16 L_subfr,     /* (i) : Subframe lenght.             */
    Word16 * gain_pit,  /* (o) : Pitch gain.                  */
    Word16 * gain_cod,  /* (o) : Code gain.                   */
    Word16 GpC_flag     /* (i) : Gp Clipping flag             */
);

void Q_plsf_3(
    Word16 * lsp1,      /* (i) : 1st LSP vector                     */
    Word16 vox,         /* (i)  : vox mode                           */
    Word16 * lsp1_q,    /* (o)  : quantized 1st LSP vector           */
    Word16 * indice,    /* (o)  : quantization indices of 3 matrices */
    Word16 * pred_init_i/* (i)  : init index for MA predictor in DTX mode */
);

void D_plsf_3(
    Word16 * past_lsf_q,/* i/o: Past quantized LSF vector                  */
    Word16 * indice,    /* (i): quantization indices of 3 submatrices      */
    Word16 * lsp1_q,    /* (o): quantized 1st LSP vector                   */
    Word16 blsfi        /* (i): bad lsf indicator (set to 1 if a bad frame */
);

void Dec_lag3(
    Word16 index,    /* (i) : received pitch index           */
    Word16 pit_min,  /* (i) : minimum pitch lag              */
    Word16 pit_max,  /* (i) : maximum pitch lag              */
    Word16 i_subfr,  /* (i) : subframe flag                  */
    Word16 * T0,     /* (o) : integer part of pitch lag      */
    Word16 * T0_frac /* (o) : fractional part of pitch lag   */
);

void Dec_gain(
    Word16 index,        /* (i) : index of quantization. */
    Word16 code[],       /* (i) : Innovative vector.     */
    Word16 L_subfr,      /* (i) : Subframe lenght.       */
    Word16 *past_qua_en, /* i/o : MA predictor state     */
    Word16 * gain_pit,   /* (o) : Pitch gain.            */
    Word16 * gain_cod    /* (o) : Code gain.             */
);

void proc_head(char *mes);

void ec_gain_code (
    Word16 *gbuf,
    Word16 *past_gain_code,
    Word16 *past_qua_en,    /* i/o : MA predictor state          */
    Word16 state,           /* i   : state of the state machine  */
    Word16 *gain_code       /* o   : decoded innovation gain     */
);

void ec_gain_code_update (
    Word16 *gbuf,
    Word16 *past_gain_code,
    Word16 *prev_gc,
    Word16 bfi,               /* i   : flag: frame is bad               */
    Word16 prev_bf,           /* i   : flag: previous frame was bad     */
    Word16 *gain_code         /* i/o : decoded innovation gain          */
);

void ec_gain_pitch (
    Word16 *pbuf,
    Word16 *past_gain_pit,
    Word16 state,           /* i   : state of the state machine        */
    Word16 *gain_pitch      /* o   : pitch gain (Q14)                  */
);

void ec_gain_pitch_update (
    Word16 *pbuf,
    Word16 *past_gain_pit,
    Word16 *prev_gp,
    Word16 bfi,             /* i   : flag: frame is bad                */
    Word16 prev_bf,         /* i   : flag: previous frame was bad      */
    Word16 *gain_pitch      /* i/o : pitch gain                        */
);

Word16 vox_tx_handler (
    Word16 *vox_state,      /* i/o: input VOX state     */ 
    Word16 vad,             /* i  : Voice Activity Flag */ 
    Word16 *vox_frame_type  /* o  : Frame type          */
);

void reorderEFRtoFRchannelFormat(
    Word16 serial[]  /* (i/o) : serial bits (134 (+ bfi))     */
);

void reorderFRtoEFRchannelFormat(
    Word16 serial[]  /* (i/o) : serial bits (134 (+ bfi))     */
);

Word16 Cb_gain_average (    
   Word16 gain_code,         /* i   : CB gain                          Q1  */ 
   Word16 lsp[],             /* i   : The LSP for the current frame    Q15 */ 
   Word16 lspAver[]          /* i   : The average of LSP for 8 frames  Q15 */     
);

void Int_lsf(
    Word16 lsf_old[], /* i : LSF vector at the 4th SF of past frame          */
    Word16 lsf_new[], /* i : LSF vector at the 4th SF of present frame       */
    Word16 i_subfr,   /* i : Pointer to current sf (equal to 0,40,80 or 120) */
    Word16 lsf_out[]  /* o : interpolated LSF parameters for current sf      */
);

void ph_disp (
      Word16 x[],          /* i/o Q0  : in:  LTP excitation signal       */
                           /*           out: total excitation signal     */
      Word16 cbGain,       /* i   Q1  : Codebook gain                    */
      Word16 ltpGain,      /* i   Q14 : LTP gain                         */
      Word16 inno[],       /* i/o Q13 : Innovation vector                */
      Word16 pitch_fac,    /* i   Q14 : pitch factor used to scale the
                                        LTP excitation                   */
      Word16 tmp_shift     /* i   Q0  : shift factor applied to sum of   
                                        scaled LTP ex & innov. before
                                        rounding                         */
);

void lsp_avg (
    Word16 *lsp_meanSave,     /* i/o : Mean LSP vector  Q15 */
    Word16 *lsp               /* i   : New LSP vector   Q15 */
);

Word16 gmed5 (        /* out      : index of the median value (0...4) */
    Word16 ind[]      /* in       : Past gain values                  */
);

void reset_gainq_mem(Word16 x);  /* Set all four old MA residuals to x */

void reset_cod_states(void);

Word16 Check_LSP(
    Word16 *lsp       /* (i) : Unquantized LSP's  */
);

Word16 Check_Gp_Clipping(
    Word16 *g_pitch   /* (i) : Pitch gain         */
);

void Update_Gp_Clipping(Word16 g_pitch);

