ld8a.h
上传用户:zhouyunkk
上传日期:2013-01-10
资源大小:59k
文件大小:18k
源码类别:

语音压缩

开发平台:

C/C++

  1. /*
  2.    ITU-T G.729 Annex C - Reference C code for floating point
  3.                          implementation of G.729 Annex A
  4.                          Version 1.01 of 15.September.98
  5. */
  6. /*
  7. ----------------------------------------------------------------------
  8.                     COPYRIGHT NOTICE
  9. ----------------------------------------------------------------------
  10.    ITU-T G.729 Annex C ANSI C source code
  11.    Copyright (C) 1998, AT&T, France Telecom, NTT, University of
  12.    Sherbrooke.  All rights reserved.
  13. ----------------------------------------------------------------------
  14. */
  15. /*-----------------------------------------------------------*
  16.  * ld8a.h - include file for G.729a 8.0 kb/s codec           *
  17.  *-----------------------------------------------------------*/
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #ifdef PI
  21. #undef PI
  22. #endif
  23. #ifdef PI2
  24. #undef PI2
  25. #endif
  26. #define PI              (F)3.141592654
  27. #define PI2             (F)6.283185307
  28. #define FLT_MAX_G729    (F)1.0e38    /* largest floating point number      */
  29. #define FLT_MIN_G729    (F)-1.0e38   /* largest floating point number      */
  30. /*--------------------------------------------------------------------------*
  31.  *       Codec constant parameters (coder, decoder, and postfilter)         *
  32.  *--------------------------------------------------------------------------*/
  33. #define L_TOTAL         240     /* Total size of speech buffer              */
  34. #define L_WINDOW        240     /* LPC analysis window size                 */
  35. #define L_NEXT          40      /* Samples of next frame needed for LPC ana.*/
  36. #define L_FRAME         80      /* LPC update frame size                    */
  37. #define L_SUBFR         40      /* Sub-frame size                           */
  38. #define PIT_MIN         20      /* Minimum pitch lag in samples             */
  39. #define PIT_MAX         143     /* Maximum pitch lag in samples             */
  40. #define L_INTERPOL      (10+1)  /* Length of filter for interpolation.      */
  41. #define GAMMA1       (F)0.75    /* Bandwitdh expansion for W(z)             */
  42. /*--------------------------------------------------------------------------*
  43.  * constants for lpc analysis and lsp quantizer                             *
  44.  *--------------------------------------------------------------------------*/
  45. #define M               10      /* LPC order                                */
  46. #define MP1             (M+1)   /* LPC order+1                              */
  47. #define NC              (M/2)   /* LPC order / 2                            */
  48. #define WNC          (F)1.0001  /* white noise correction factor            */
  49. #define GRID_POINTS      50     /* resolution of lsp search                 */
  50. #define MA_NP           4       /* MA prediction order for LSP              */
  51. #define MODE            2       /* number of modes for MA prediction        */
  52. #define NC0_B           7       /* number of bits in first stage            */
  53. #define NC0          (1<<NC0_B) /* number of entries in first stage         */
  54. #define NC1_B           5       /* number of bits in second stage           */
  55. #define NC1          (1<<NC1_B) /* number of entries in second stage        */
  56. #define L_LIMIT         (F)0.005   /*  */
  57. #define M_LIMIT         (F)3.135   /*  */
  58. #define GAP1            (F)0.0012  /*  */
  59. #define GAP2            (F)0.0006  /*  */
  60. #define GAP3            (F)0.0392  /*  */
  61. #define PI04            PI*(F)0.04   /* pi*0.04 */
  62. #define PI92            PI*(F)0.92   /* pi*0.92 */
  63. #define CONST12         (F)1.2
  64. /*------------------------------------------------------------------*
  65.  *  Constants for long-term predictor                               *
  66.  *------------------------------------------------------------------*/
  67. #define  SHARPMAX       (F)0.7945  /* Maximum value of pitch sharpening */
  68. #define  SHARPMIN       (F)0.2     /* Minimum value of pitch sharpening */
  69. #define  GAIN_PIT_MAX   (F)1.2     /* Maximum adaptive codebook gain    */
  70. #define  UP_SAMP         3         /* Resolution of fractional delays   */
  71. #define  L_INTER10       10        /* Length for pitch interpolation    */
  72. #define  FIR_SIZE_SYN    (UP_SAMP*L_INTER10+1)
  73. /*-----------------------*
  74.  * Innovative codebook.  *
  75.  *-----------------------*/
  76. #define DIM_RR  616 /* size of correlation matrix                            */
  77. #define NB_POS  8   /* Number of positions for each pulse                    */
  78. #define STEP    5   /* Step betweem position of the same pulse.              */
  79. #define MSIZE   64  /* Size of vectors for cross-correlation between 2 pulses*/
  80. /*------------------------------------------------------------------*
  81.  *  gain quantizer                                                  *
  82.  *------------------------------------------------------------------*/
  83. #define MEAN_ENER     (F)36.0   /* Average innovation energy */
  84. #define NCODE1           8      /* Codebook 1 size */
  85. #define NCODE2           16     /* Codebook 2 size */
  86. #define NCAN1            4      /* Pre-selecting order for #1 */
  87. #define NCAN2            8      /* Pre-selecting order for #2 */
  88. #define INV_COEF      (F)-0.032623
  89. /*------------------------------------------------------------------*
  90.  *  Constant for postfilter                                         *
  91.  *------------------------------------------------------------------*/
  92. #define  GAMMA2_PST  (F)0.55       /* Formant postfilt factor (numerator)   */
  93. #define  GAMMA1_PST  (F)0.70       /* Formant postfilt factor (denominator) */
  94. #define  GAMMAP      (F)0.50       /* Harmonic postfilt factor              */
  95. #define  INV_GAMMAP  ((F)1.0/((F)1.0+GAMMAP))
  96. #define  GAMMAP_2    (GAMMAP/((F)1.0+GAMMAP))
  97. #define  MU          (F)0.8        /* Factor for tilt compensation filter   */
  98. #define  AGC_FAC     (F)0.9        /* Factor for automatic gain control     */
  99. #define  AGC_FAC1     ((F)1.-AGC_FAC)
  100. #define  L_H 22   /* size of truncated impulse response of A(z/g1)/A(z/g2) */
  101. /*--------------------------------------------------------------------------*
  102.  * Constants for taming procedure.                           *
  103.  *--------------------------------------------------------------------------*/
  104. #define GPCLIP      (F)0.95     /* Maximum pitch gain if taming is needed */
  105. #define GPCLIP2     (F)0.94     /* Maximum pitch gain if taming is needed */
  106. #define GP0999      (F)0.9999   /* Maximum pitch gain if taming is needed    */
  107. #define THRESH_ERR  (F)60000.   /* Error threshold taming    */
  108. #define INV_L_SUBFR (FLOAT) ((F)1./(FLOAT)L_SUBFR) /* =0.025 */
  109. /*-----------------------*
  110.  * Bitstream constants   *
  111.  *-----------------------*/
  112. #define BIT_0     (INT16)0x007f /* definition of zero-bit in bit-stream     */
  113. #define BIT_1     (INT16)0x0081 /* definition of one-bit in bit-stream      */
  114. #define SYNC_WORD (INT16)0x6b21 /* definition of frame erasure flag         */
  115. #define PRM_SIZE        11      /* number of parameters per 10 ms frame     */
  116. #define SERIAL_SIZE     82      /* bits per frame                           */
  117. #define SIZE_WORD (INT16)80     /* number of speech bits                     */
  118. /*-------------------------------*
  119.  * Pre and post-process functions*
  120.  *-------------------------------*/
  121. void init_post_process( void
  122. );
  123. void post_process(
  124.    FLOAT signal[],      /* (i/o)  : signal           */
  125.    int lg               /* (i)    : lenght of signal */
  126. );
  127. void init_pre_process( void
  128. );
  129. void pre_process(
  130.    FLOAT signal[],      /* (i/o)  : signal           */
  131.    int lg               /* (i)    : lenght of signal */
  132. );
  133. /*----------------------------------*
  134.  * Main coder and decoder functions *
  135.  *----------------------------------*/
  136. void  init_coder_ld8a(void);
  137. void  coder_ld8a(
  138.  int ana[]              /* output: analysis parameters */
  139. );
  140. void  init_decod_ld8a(void);
  141. void  decod_ld8a(
  142.   int parm[],          /* (i)   : vector of synthesis parameters
  143.                                   parm[0] = bad frame indicator (bfi)  */
  144.   FLOAT   synth[],     /* (o)   : synthesis speech                     */
  145.   FLOAT   A_t[],       /* (o)   : decoded LP filter in 2 subframes     */
  146.   int *T2,              /* (o)   : decoded pitch lag in 2 subframes     */
  147.   int     bfi           /* (i)   :bad frame indicator (bfi)      */
  148. );
  149. /*-------------------------------*
  150.  * LPC analysis and filtering.   *
  151.  *-------------------------------*/
  152. void  autocorr(FLOAT *x, int m, FLOAT *r);
  153. void  lag_window(int m, FLOAT r[]);
  154. FLOAT levinson(FLOAT *a, FLOAT *r, FLOAT *r_c);
  155. void  az_lsp(FLOAT *a, FLOAT *lsp, FLOAT *old_lsp);
  156. void  int_qlpc(FLOAT lsp_new[], FLOAT lsp_old[], FLOAT a[]);
  157. void  weight_az(FLOAT *a,  FLOAT gamma, int m,  FLOAT *ap);
  158. void residu(    /* filter A(z)                                       */
  159.  FLOAT *a,      /* input : prediction coefficients a[0:m+1], a[0]=1. */
  160.  FLOAT *x,      /* input : input signal x[0:l-1], x[-1:m] are needed */
  161.  FLOAT *y,      /* output: output signal y[0:l-1] NOTE: x[] and y[]
  162.                             cannot point to same array               */
  163.  int l          /* input : dimension of x and y                      */
  164. );
  165. void syn_filt(
  166.  FLOAT a[],     /* input : predictor coefficients a[0:m]    */
  167.  FLOAT x[],     /* input : excitation signal                */
  168.  FLOAT y[],     /* output: filtered output signal           */
  169.  int l,         /* input : vector dimension                 */
  170.  FLOAT mem[],   /* in/out: filter memory                    */
  171.  int update_m   /* input : 0 = no memory update, 1 = update */
  172. );
  173. void convolve(
  174.  FLOAT x[],             /* input : input vector x[0:l]                     */
  175.  FLOAT h[],             /* input : impulse response or second input h[0:l] */
  176.  FLOAT y[],             /* output: x convolved with h , y[0:l]             */
  177.  int l                  /* input : dimension of all vectors                */
  178. );
  179. /*-------------------------------------------------------------*
  180.  * Prototypes of pitch functions                               *
  181.  *-------------------------------------------------------------*/
  182. int pitch_ol_fast(  /* output: open loop pitch lag                        */
  183.    FLOAT signal[],  /* input : signal used to compute the open loop pitch */
  184.                     /*     signal[-pit_max] to signal[-1] should be known */
  185.    int L_frame      /* input : length of frame to compute pitch           */
  186. );
  187. int pitch_fr3_fast(     /* output: integer part of pitch period */
  188.  FLOAT exc[],           /* input : excitation buffer            */
  189.  FLOAT xn[],            /* input : target vector                */
  190.  FLOAT h[],             /* input : impulse response.            */
  191.  int L_subfr,           /* input : Length of subframe           */
  192.  int t0_min,            /* input : minimum value in the searched range */
  193.  int t0_max,            /* input : maximum value in the searched range */
  194.  int i_subfr,           /* input : indicator for first subframe        */
  195.  int *pit_frac          /* output: chosen fraction                     */
  196. );
  197. FLOAT g_pitch(FLOAT xn[], FLOAT y1[], FLOAT g_coeff[], int l);
  198. int enc_lag3(     /* output: Return index of encoding */
  199.   int T0,         /* input : Pitch delay              */
  200.   int T0_frac,    /* input : Fractional pitch delay   */
  201.   int *T0_min,    /* in/out: Minimum search delay     */
  202.   int *T0_max,    /* in/out: Maximum search delay     */
  203.   int pit_min,    /* input : Minimum pitch delay      */
  204.   int pit_max,    /* input : Maximum pitch delay      */
  205.   int pit_flag    /* input : Flag for 1st subframe    */
  206. );
  207. void dec_lag3(     /* Decode the pitch lag                   */
  208.   int index,       /* input : received pitch index           */
  209.   int pit_min,     /* input : minimum pitch lag              */
  210.   int pit_max,     /* input : maximum pitch lag              */
  211.   int i_subfr,     /* input : subframe flag                  */
  212.   int *T0,         /* output: integer part of pitch lag      */
  213.   int *T0_frac     /* output: fractional part of pitch lag   */
  214. );
  215. void pred_lt_3(       /* Compute adaptive codebook                       */
  216.  FLOAT exc[],         /* in/out: excitation vector, exc[0:l_sub-1] = out */
  217.  int t0,              /* input : pitch lag                               */
  218.  int frac,            /* input : Fraction of pitch lag (-1, 0, 1)  / 3   */
  219.  int l_sub            /* input : length of subframe.                     */
  220. );
  221. int parity_pitch(int pitch_i);
  222. int check_parity_pitch(int pitch_i, int parity);
  223. void cor_h_x(
  224.      FLOAT h[],         /* (i) :Impulse response of filters      */
  225.      FLOAT X[],         /* (i) :Target vector                    */
  226.      FLOAT D[]          /* (o) :Correlations between h[] and D[] */
  227. );
  228. /*-----------------------*
  229.  * Innovative codebook.  *
  230.  *-----------------------*/
  231. int ACELP_code_A(       /* (o) :index of pulses positions    */
  232.   FLOAT x[],            /* (i) :Target vector                */
  233.   FLOAT h[],            /* (i) :Inpulse response of filters  */
  234.   int T0,               /* (i) :Pitch lag                    */
  235.   FLOAT pitch_sharp,    /* (i) :Last quantized pitch gain    */
  236.   FLOAT code[],         /* (o) :Innovative codebook          */
  237.   FLOAT y[],            /* (o) :Filtered innovative codebook */
  238.   int *sign             /* (o) :Signs of 4 pulses            */
  239. );
  240. void  decod_ACELP(int signs, int positions, FLOAT cod[]);
  241. /*-----------------------------------------------------------*
  242.  * Prototypes of LSP VQ functions                            *
  243.  *-----------------------------------------------------------*/
  244. void qua_lsp(
  245.   FLOAT lsp[],       /* (i) : Unquantized LSP            */
  246.   FLOAT lsp_q[],     /* (o) : Quantized LSP              */
  247.   int ana[]          /* (o) : indexes                    */
  248. );
  249. void lsp_encw_reset(void);
  250. void lsp_expand_1( FLOAT buf[], FLOAT c);
  251. void lsp_expand_2( FLOAT buf[], FLOAT c);
  252. void lsp_expand_1_2( FLOAT buf[], FLOAT c);
  253. void lsp_get_quant(
  254.   FLOAT lspcb1[][M],
  255.   FLOAT lspcb2[][M],
  256.   int code0,
  257.   int code1,
  258.   int code2,
  259.   FLOAT fg[][M],
  260.   FLOAT freq_prev[][M],
  261.   FLOAT lspq[],
  262.   FLOAT fg_sum[]
  263. );
  264. void d_lsp(
  265. int index[],           /* input : indexes                 */
  266. FLOAT lsp_new[],       /* output: decoded lsp             */
  267. int bfi                /* input : frame erase information */
  268. );
  269. void lsp_decw_reset(void);
  270. void lsp_prev_extract(
  271.   FLOAT lsp[M],
  272.   FLOAT lsp_ele[M],
  273.   FLOAT fg[MA_NP][M],
  274.   FLOAT freq_prev[MA_NP][M],
  275.   FLOAT fg_sum_inv[M]
  276. );
  277. void lsp_prev_update(
  278.   FLOAT lsp_ele[M],
  279.   FLOAT freq_prev[MA_NP][M]
  280. );
  281. /*--------------------------------------------------------------------------*
  282.  * gain VQ functions.                                                       *
  283.  *--------------------------------------------------------------------------*/
  284. int qua_gain(FLOAT code[], FLOAT *coeff, int lcode, FLOAT *gain_pit,
  285.         FLOAT *gain_code, int tameflag   );
  286. void  dec_gain(int indice, FLOAT code[], int lcode, int bfi, FLOAT *gain_pit,
  287.                FLOAT *gain_code);
  288. void gain_predict(
  289.   FLOAT past_qua_en[],  /* input :Past quantized energies       */
  290.   FLOAT code[],         /* input: Innovative vector.            */
  291.   int l_subfr,          /* input : Subframe length.             */
  292.   FLOAT *gcode0         /* output : Predicted codebook gain     */
  293. );
  294. void gain_update(
  295.   FLOAT past_qua_en[], /* input/output :Past quantized energies  */
  296.   FLOAT g_code         /* input        : quantized gain          */
  297. );
  298. void gain_update_erasure(FLOAT *past_qua_en);
  299. void  corr_xy2(FLOAT xn[], FLOAT y1[], FLOAT y2[], FLOAT g_coeff[]);
  300. /*-----------------------*
  301.  * Bitstream function    *
  302.  *-----------------------*/
  303. void  prm2bits_ld8k(int prm[], unsigned char * bits);
  304. void  bits2prm_ld8k(unsigned char * bits, int prm[]);
  305. /*-----------------------------------------------------------*
  306.  * Prototypes for the post filtering                         *
  307.  *-----------------------------------------------------------*/
  308. void init_post_filter(void);
  309. void post_filter(
  310.   FLOAT *syn,     /* in/out: synthesis speech (postfiltered is output)    */
  311.   FLOAT *a_t,     /* input : interpolated LPC parameters in all subframes */
  312.   int *T          /* input : decoded pitch lags in all subframes          */
  313. );
  314. /*------------------------------------------------------------*
  315.  * prototypes for taming procedure.                           *
  316.  *------------------------------------------------------------*/
  317. void   init_exc_err(void);
  318. void   update_exc_err(FLOAT gain_pit, int t0);
  319. int test_err(int t0, int t0_frac);
  320. /*-----------------------------------------------------------*
  321.  * Prototypes for auxiliary functions                        *
  322.  *-----------------------------------------------------------*/
  323. void set_zero(
  324.   FLOAT  x[],           /* (o)  : vector to clear  */
  325.   int L                 /* (i)  : length of vector */
  326. );
  327. void copy(
  328.   FLOAT  x[],           /* (i)  : input vector   */
  329.   FLOAT  y[],           /* (o)  : output vector  */
  330.   int L                 /* (i)  : vector length  */
  331. );
  332. INT16 random_g729(void);
  333. void fwrite16(
  334.  FLOAT *data,           /* input: inputdata            */
  335.  int length,            /* input: length of data array */
  336.  FILE *fp               /* input: file pointer         */
  337. );