1 /*/////////////////////////////////////////////////////////////////////////////
2 //
3 //                  INTEL CORPORATION PROPRIETARY INFORMATION
4 //     This software is supplied under the terms of a license agreement or
5 //     nondisclosure agreement with Intel Corporation and may not be copied
6 //     or disclosed except in accordance with the terms of that agreement.
7 //          Copyright(c) 2004-2011 Intel Corporation. All Rights Reserved.
8 //
9 //     Intel(R) Integrated Performance Primitives
10 //     USC - Unified Speech Codec interface library
11 //
12 // By downloading and installing USC codec, you hereby agree that the
13 // accompanying Materials are being provided to you under the terms and
14 // conditions of the End User License Agreement for the Intel(R) Integrated
15 // Performance Primitives product previously accepted by you. Please refer
16 // to the file ippEULA.rtf or ippEULA.txt located in the root directory of your Intel(R) IPP
17 // product installation for more information.
18 //
19 // A speech coding standards promoted by ITU, ETSI, 3GPP and other
20 // organizations. Implementations of these standards, or the standard enabled
21 // platforms may require licenses from various entities, including
22 // Intel Corporation.
23 //
24 //
25 // Purpose: G.729 floating-point speech codec: main own header file.
26 //
27 */
28 
29 #ifndef __OWNG729FP_H__
30 #define __OWNG729FP_H__
31 
32 #ifndef G72X_IPP_LEGACY
33     #include <ipps.h>
34     #include <ippsc.h>
35 #else
36     #include <ipps90legacy.h>
37     #include <ippsc90legacy.h>
38 #endif
39 #include "g729fpapi.h"
40 #include "scratchmem.h"
41 
42 #define ENC_KEY 0xecd729
43 #define DEC_KEY 0xdec729
44 
45 #define G729FP_ENCODER_SCRATCH_MEMORY_SIZE   (2*7168+40)
46 
47 #define SPEECH_BUFF_LEN 240     /* Total size of speech buffer               */
48 #define FRM_LEN         80      /* LPC update frame size                     */
49 #define SUBFR_LEN       40      /* Sub-frame size                            */
50 #define NUN_SUBFRAMES   FRM_LEN/SUBFR_LEN
51 
52 #define WINDOW_LEN        240     /* LPC analysis window size                  */
53 #define LOOK_AHEAD_LEN          40      /* Samples of next frame needed for LPC ana. */
54 
55 #define LPC_ORDER       10      /* LPC order                                 */
56 #define LPC_ORDERP1     (LPC_ORDER+1)    /* LPC order+1                               */
57 
58 #define MOVING_AVER_ORDER          4       /* MA prediction order for LSP                     */
59 #define N_BITS_1ST_STAGE           7       /* number of bits in first stage for LSP index     */
60 #define N_ELEM_1ST_STAGE          (1<<N_BITS_1ST_STAGE) /* number of entries in first stage for LSP index  */
61 #define N_BITS_2ND_STAGE           5       /* number of bits in second stage for LSP index    */
62 #define N_ELEM_2ND_STAGE          (1<<N_BITS_2ND_STAGE) /* number of entries in second stage for LSP index */
63 
64 #define LSF_LOW_LIMIT         (Ipp32f)0.005
65 #define LSF_HI_LIMIT          (Ipp32f)3.135
66 #define LSF_DIST              (Ipp32f)0.0392
67 
68 #ifdef CLIPPING_DENORMAL_MODE
69    #define DENORMAL_THRESHOLD 1.0E-7
70 
71    #define CLIP_DENORMAL(src,dst) if(fabs(src) < DENORMAL_THRESHOLD) dst = 0;\
72                                  else dst = src
73    #define CLIP_DENORMAL_I(srcdst) if(fabs(srcdst) < DENORMAL_THRESHOLD) srcdst = 0;
74 #else
75 
76    #define CLIP_DENORMAL(src,dst) dst = src
77    #define CLIP_DENORMAL_I(srcdst)
78 #endif
79 
80 #define LAR_THRESH1   (Ipp32f)-1.74
81 #define LAR_THRESH2   (Ipp32f)-1.52
82 #define LAR_THRESH3   (Ipp32f)0.65
83 #define LAR_THRESH4   (Ipp32f)0.43
84 #define GAMMA1_TILTED         (Ipp32f)0.98
85 #define GAMMA2_TILTED_MAX     (Ipp32f)0.7
86 #define GAMMA2_TILTED_MIN     (Ipp32f)0.4
87 #define GAMMA1_FLAT           (Ipp32f)0.94
88 #define GAMMA2_FLAT           (Ipp32f)0.6
89 #define GAMMA2_TILTED_SCALE   (Ipp32f)-6.0
90 #define GAMMA2_TILTED_SHIFT   (Ipp32f)1.0
91 
92 #define PITCH_LAG_MIN   20      /* Minimum pitch lag in samples              */
93 #define PITCH_LAG_MAX   143     /* Maximum pitch lag in samples              */
94 #define INTERPOL_LEN    (10+1)  /* Length of filter for interpolation.       */
95 #define INTER_PITCH_LEN 10      /* Length for pitch interpolation            */
96 #define INTERPOL4_LEN   4       /* Upsampling ration for pitch search        */
97 #define UP_SAMPLING     3       /* Resolution of fractional delays           */
98 #define PITCH_THRESH    0.85f   /* Threshold to favor smaller pitch lags     */
99 #define GAIN_PIT_MAX    1.2f    /* Maximum adaptive codebook gain            */
100 
101 /* Constants for fixed codebook search. */
102 #define TOEPLIZ_MATRIX_SIZE  616 /* size of correlation matrix                            */
103 
104 #define PITCH_SHARPMAX        (Ipp32f)0.7945  /* Maximum value of pitch sharpening */
105 #define PITCH_SHARPMIN        (Ipp32f)0.2     /* minimum value of pitch sharpening */
106 
107 /* Constants for taming procedure.*/
108 #define MAX_GAIN_TIMING      (Ipp32f)0.95     /* Maximum pitch gain if taming is needed */
109 #define MAX_GAIN_TIMING2     (Ipp32f)0.94     /* Maximum pitch gain if taming is needed */
110 #define THRESH_ERR           (Ipp32f)60000.   /* Error threshold taming    */
111 #define INV_SUBFR_LEN (Ipp32f) ((Ipp32f)1./(Ipp32f)SUBFR_LEN) /* =0.025 */
112 
113 /* Constants for postfilter */
114 /* Ipp16s term pst parameters :  */
115 #define GAMMA1_POSTFLT      (Ipp32f)0.7     /* denominator weighting factor           */
116 #define GAMMA2_POSTFLT      (Ipp32f)0.55    /* numerator  weighting factor            */
117 #define SHORTTERM_POSTFLT_LEN       20      /* impulse response length                   */
118 #define GAMMA3_POSTFLT_P    (Ipp32f)0.2     /* tilt weighting factor when k1>0        */
119 #define GAMMA3_POSTFLT_M    (Ipp32f)0.9     /* tilt weighting factor when k1<0        */
120 
121 /* long term pst parameters :   */
122 #define SUBFR_LENP1 (SUBFR_LEN + 1) /* Sub-frame size + 1                        */
123 #define FRAC_DELAY_RES        8       /* resolution for fractionnal delay          */
124 
125 #define SHORT_INT_FLT_LEN     4       /* length of Ipp16s interp. subfilters        */
126 #define LONG_INT_FLT_LEN      16      /* length of long interp. subfilters         */
127 #define SHORT_INT_FLT_LEN_BY2    (SHORT_INT_FLT_LEN/2)
128 #define LONG_INT_FLT_LEN_BY2     (LONG_INT_FLT_LEN/2)
129 
130 #define LTPTHRESHOLD    (Ipp32f)0.5f    /* threshold LT to switch off postfilter */
131 #define AGC_FACTOR      (Ipp32f)0.9875  /* gain adjustment factor                 */
132 
133 #define AGC_FACTORM1    ((Ipp32f)1. - AGC_FACTOR)    /* gain adjustment factor                 */
134 
135 /* Array sizes */
136 #define RESISDUAL_MEMORY (PITCH_LAG_MAX + 1 + LONG_INT_FLT_LEN_BY2)
137 #define SIZE_RESISDUAL_MEMORY (RESISDUAL_MEMORY + SUBFR_LEN)
138 #define SIZE_SEARCH_DEL_MEMORY  ((FRAC_DELAY_RES-1) * SUBFR_LENP1)
139 #define SIZE_LONG_INT_FLT_MEMORY ((FRAC_DELAY_RES-1) * LONG_INT_FLT_LEN)
140 #define SIZE_SHORT_INT_FLT_MEMORY ((FRAC_DELAY_RES-1) * SHORT_INT_FLT_LEN)
141 
142 #define G729D_MODE      0      /* Low  rate  (6400 bit/s)       */
143 #define G729_BASE       1      /* Full rate  (8000 bit/s)       */
144 #define G729E_MODE      2      /* High rate (11800 bit/s)       */
145 
146 /* backward LPC analysis parameters */
147 #define BWD_LPC_ORDER         30         /* Order of Backward LP filter.              */
148 #define BWD_LPC_ORDERP1               (BWD_LPC_ORDER+1)  /* Order of Backward LP filter + 1           */
149 #define NON_RECURSIVE_PART    35
150 #define BWD_SYNTH_MEM           (BWD_LPC_ORDER + NON_RECURSIVE_PART)
151 #define BWD_ANALISIS_WND_LEN             (FRM_LEN + BWD_SYNTH_MEM)
152 #define BWD_GAMMA 0.98f
153 
154 /* Annex E adaptive Ipp16s term postfilter parameters:*/
155 #define GAMMA1_POSTFLT_E  0.7f      /* denominator weighting factor */
156 #define GAMMA2_POSTFLT_E  0.65f     /* numerator  weighting factor */
157 #define SHORTTERM_POSTFLT_LEN_E   32        /* Lenght of the impulse response*/
158 #define GAMMA_HARM_POSTFLT_E 0.25f
159 #define GAMMA_HARM_POSTFLT   0.5f
160 
161 /* Constants for backward/forward decision*/
162 #define THRES_ENERGY 40.f /*Low energy frame threshold*/
163 /* Gains levels */
164 #define TH1 1.f
165 #define TH2 2.f
166 #define TH3 3.f
167 #define TH4 4.f
168 #define TH5 4.7f
169 #define GAP_FACT (Ipp32f)0.000114375f
170 #define INVERSE_LOG2 (Ipp32f) (1./log10(2.))
171 
172 /*Constants for gain quantization.*/
173 #define MEAN_ENER        (Ipp32f)36.0              /* average innovation energy */
174 #define NCODE1_BITS  3                             /* number of Codebook-bit */
175 #define NCODE2_BITS  4                             /* number of Codebook-bit */
176 #define SIZECODEBOOK1    (1<<NCODE1_BITS)          /* Codebook 1 size */
177 #define SIZECODEBOOK2    (1<<NCODE2_BITS)          /* Codebook 2 size */
178 #define NUM_CAND1            4                     /* Pre-selecting order for #1 */
179 #define NUM_CAND2            8                     /* Pre-selecting order for #2 */
180 #define INV_COEF_BASE   (Ipp32f)-0.032623
181 
182 /*Constants for gain quantization in Annex D mode*/
183 #define NCODE1_B_ANNEXD  3                         /* number of Codebook-bit */
184 #define NCODE2_B_ANNEXD  3                         /* number of Codebook-bit */
185 #define SIZECODEBOOK1_ANNEXD (1<<NCODE1_B_ANNEXD)  /* Codebook 1 size */
186 #define SIZECODEBOOK2_ANNEXD (1<<NCODE2_B_ANNEXD)  /* Codebook 2 size */
187 #define NUM_CAND1_ANNEXD  6                        /* Pre-selecting order for #1 */
188 #define NUM_CAND2_ANNEXD  6                        /* Pre-selecting order for #2 */
189 #define INV_COEF_ANNEXD  ((Ipp32f)-0.027599)
190 #define NUM_TRACK_ACELP          4
191 
192 /* VAD */
193 #define     LPC_ORDERP2      12                 /* LPC order plus 2*/
194 #define     VAD_NOISE         0                 /* Non-active frame*/
195 #define     VAD_VOICE         1                 /* Active frame*/
196 #define     END_OF_INIT   32
197 #define     ZC_START_INDEX      120
198 #define     ZC_END_INDEX        200
199 
200 /* DTX constants */
201 #define ENCODER         1
202 #define DECODER         0
203 #define INIT_SEED_VAL       11111
204 #define N_MIN_SIM_RRAMES      3
205 #define ITAKURATHRESH1         (Ipp32f)1.1481628/2.
206 #define ITAKURATHRESH2         (Ipp32f)1.0966466/2.
207 
208 #define GAIN_INT_FACTOR        (Ipp32f)0.875
209 #define INV_GAIN_INT_FACTOR    ((Ipp32f)1. - GAIN_INT_FACTOR)
210 
211 #define MIN_ENER        (Ipp32f)0.1588489319   /*- 8 dB threshold*/
212 
213 /* CNG constants */
214 #define NORM_GAUSS      (Ipp32f)3.16227766  /* sqrt(40)xalpha, alpha=0.5 */
215 #define K_MUL_COEFF              (Ipp32f)3.          /* 4 x (1 - alpha ** 2), alpha=0.5*/
216 #define CNG_MAX_GAIN           (Ipp32f)5000.
217 
218 #define GAMMA1_G729A       (Ipp32f)0.75    /* Bandwitdh expansion for W(z)             */
219 #define  GAMMA_POSTFLT_G729A      (Ipp32f)0.50       /* Harmonic postfilt factor              */
220 #define  INV_GAMMA_POSTFLT_G729A  ((Ipp32f)1.0/((Ipp32f)1.0+GAMMA_POSTFLT_G729A))
221 #define  GAMMA2_POSTFLT_G729A    (GAMMA_POSTFLT_G729A/((Ipp32f)1.0+GAMMA_POSTFLT_G729A))
222 #define  TILT_FLT_FACTOR          (Ipp32f)0.8        /* Factor for tilt compensation filter   */
223 #define  AGC_FACTOR_G729A     (Ipp32f)0.9        /* Factor for automatic gain control     */
224 #define  AGC_FACTOR_1M_G729A     ((Ipp32f)1.-AGC_FACTOR_G729A)
225 #define  PST_IMPRESP_LEN 22   /* size of truncated impulse response of A(z/g1)/A(z/g2) */
226 
227 #define CLIP_TO_UPLEVEL(value,maxValue)\
228    if(value>maxValue) value=maxValue
229 
230 #define CLIP_TO_LOWLEVEL(value,minValue)\
231    if(value<minValue) value=minValue
232 
233 void CNGGetSize(Ipp32s *pDstSize);
234 void CNGInit(Ipp8s *cngMem);
235 void PSTGetSize(Ipp32s *pDstSize);
236 void PSTInit(Ipp8s *cngMem);
237 void MSDGetSize(Ipp32s *pDstSize);
238 void MSDInit(Ipp8s *msdMem);
239 void PHDGetSize(Ipp32s *pDstSize);
240 void PHDInit(Ipp8s *phdMem);
241 
242 Ipp32s  ExtractBitsG729FP( const Ipp8u **pBits, Ipp32s *nBit, Ipp32s Count );
243 
244 void ownAutoCorr_G729_32f(Ipp32f *pSrc, Ipp32s len, Ipp32f *pDst, Ipp32f *pExtBuff);
245 void ownACOS_G729_32f(Ipp32f *pSrc, Ipp32f *pDst, Ipp32s len);
246 void ownCOS_G729_32f(Ipp32f *pSrc, Ipp32f *pDst, Ipp32s len);
247 Ipp32f ownAdaptiveCodebookGainCoeff_G729_32f(Ipp32f *pSrcTargetVector, Ipp32f *pSrcFltAdaptivCdbkVec,
248                Ipp32f *pDstCorrCoeff, Ipp32s len);
249 
250 void AdaptiveCodebookGainCoeff_G729_32f( Ipp32f *pSrcTargetVector, Ipp32f *pSrcFltAdaptiveCodebookVector,
251                                          Ipp32f *pSrcFltInnovation, Ipp32f *pDstCoeff);
252 
253 Ipp32s ownAdaptiveCodebookSearch_G729A_32f(Ipp32f *pSrcExc, Ipp32f *pSrcTargetVector, Ipp32f *pSrcImpulseResponse,
254   Ipp32s minPitchDelay, Ipp32s maxPitchDelay, Ipp32s nSbfr, Ipp32s *fracPartPitchDelay, Ipp32f *pExtBuff);
255 
256 void PWGammaFactor_G729(Ipp32f *pGamma1, Ipp32f *pGamma2, Ipp32f *pIntLSF, Ipp32f *CurrLSF,
257                    Ipp32f *ReflectCoeff, Ipp32s   *isFlat, Ipp32f *PrevLogAreaRatioCoeff);
258 
259 void MusicDetection_G729E_32f( G729FPEncoder_Obj *encoderObj, G729Codec_Type codecType, Ipp32f Energy, Ipp32f *ReflectCoeff, Ipp32s *Vad,
260                                  Ipp32f EnergydB,Ipp8s *msdMem,Ipp32f *pExtBuff);
261 
262 void PitchTracking_G729FPE(Ipp32s *T0, Ipp32s *T0_frac, Ipp32s *lPrevPitchPT, Ipp32s *lStatPitchPT, Ipp32s *lStatPitch2PT,  Ipp32s *lStatFracPT);
263 
264 void OpenLoopPitchSearch_G729_32f(const Ipp32f *pSrc, Ipp32s* bestLag);
265 
266 void WeightLPCCoeff_G729(Ipp32f *pSrcLPC, Ipp32f valWeightingFactor, Ipp32s len, Ipp32f *pDstWeightedLPC);
267 
268 Ipp32s TestErrorContribution_G729(Ipp32s valPitchDelay, Ipp32s valFracPitchDelay, Ipp32f *ExcErr);
269 
270 void UpdateExcErr_G729(Ipp32f valPitchGain, Ipp32s valPitchDelay, Ipp32f *pExcErr);
271 
272 void isBackwardModeDominant_G729(Ipp32s *isBackwardModeDominant, Ipp32s LPCMode, Ipp32s *pCounterBackward, Ipp32s *pCounterForward);
273 
274 Ipp32f CalcEnergy_dB_G729(Ipp32f *pSrc, Ipp32s len);
275 
276 void InterpolatedBackwardFilter_G729(Ipp32f *pSrcDstLPCBackwardFlt, Ipp32f *pSrcPrevFilter, Ipp32f *pSrcDstIntCoeff);
277 
278 void PhaseDispersionUpdate_G729D(Ipp32f valPitchGain, Ipp32f valCodebookGain, Ipp8s *phdMem);
279 void PhaseDispersion_G729D(Ipp32f *pSrcExcSignal, Ipp32f *pDstFltExcSignal, Ipp32f valCodebookGain,
280                            Ipp32f valPitchGain, Ipp32f *pSrcDstInnovation, Ipp8s *phdMem,Ipp8s *pExtBuff);
281 
282 void SetLPCMode_G729FPE(G729FPEncoder_Obj* encoderObj, Ipp32f *pSrcSignal, Ipp32f *pSrcForwardLPCFilter,
283                         Ipp32f *pSrcBackwardLPCFilter, Ipp32s *pDstLPCMode, Ipp32f *pSrcLSP,Ipp32f *pExtBuff);
284 
285 Ipp32s AdaptiveCodebookSearch_G729_32f(Ipp32f *pSrcExc, Ipp32f *pSrcTargetVector, Ipp32f *pSrcImpulseResponse, Ipp32s len,
286                                        Ipp32s minLag, Ipp32s maxLag, Ipp32s valSubframeNum, Ipp32s *pDstFracPitch, G729Codec_Type codecType,Ipp32f *pExtBuff);
287 
288 void ComfortNoiseExcitation_G729(Ipp32f fCurrGain, Ipp32f *exc, Ipp16s *sCNGSeed, Ipp32s flag_cod, Ipp32f *ExcitationError, Ipp8s *phdMem, Ipp8s *pExtBuff);
289 
290 void QuantSIDGain_G729B(Ipp32f *ener, Ipp32s lNumSavedEnergies, Ipp32f *enerq, Ipp32s *idx);
291 
292 Ipp32s GainQuant_G729(Ipp32f *FixedCodebookExc, Ipp32f *pGainCoeff, Ipp32s lSbfrLen, Ipp32f *gain_pit, Ipp32f *fCodeGain,
293                       Ipp32s tamingflag, Ipp32f *PastQuantEnergy, G729Codec_Type codecType,Ipp8s *pExtBuff);
294 void DecodeGain_G729(Ipp32s index, Ipp32f *code, Ipp32s l_subfr, Ipp32f *pitchGain, Ipp32f *codeGain, Ipp32s rate, Ipp32f *PastQuantEnergy);
295 
296 void Post_G729E(G729FPDecoder_Obj *decoderObj, Ipp32s pitchDelay, Ipp32f *pSignal, Ipp32f *pLPC, Ipp32f *pDstFltSignal, Ipp32s *pVoicing,
297                 Ipp32s len, Ipp32s lenLPC, Ipp32s Vad);
298 
299 typedef struct _G729CCoder_Obj {
300    Ipp32s         objSize;
301    Ipp32s         key;
302    Ipp32u         mode;          /* coder mode's */
303    G729Codec_Type codecType;
304 }G729CCoder_Obj;
305 
306 #define SUMAUTOCORRS_NUM       3
307 #define SUMAUTOCORRS_SIZE      (SUMAUTOCORRS_NUM * LPC_ORDERP1)
308 #define CURRAUTOCORRS_NUM      2
309 #define AUTOCORRS_SIZE         (CURRAUTOCORRS_NUM * LPC_ORDERP1)
310 #define GAINS_NUM              2
311 
312 extern CONST Ipp32f InitLSP[LPC_ORDER];
313 extern CONST Ipp32f InitFrequences[LPC_ORDER];
314 extern CONST Ipp32f lagBwd[BWD_LPC_ORDER];
315 extern CONST Ipp32f SIDGainTbl[32];
316 
317 typedef struct _CNGmemory{
318    Ipp32f AutoCorrs[AUTOCORRS_SIZE];
319    Ipp32f SumAutoCorrs[SUMAUTOCORRS_SIZE];
320    Ipp32f Energies[GAINS_NUM];
321    Ipp32s lAutoCorrsCounter;
322    Ipp32f fCurrGain;
323    Ipp32s lFltChangeFlag;
324    Ipp32f SIDQuantLSP[LPC_ORDER];
325    Ipp32f ReflectCoeffs[LPC_ORDERP1];
326    Ipp32s lNumSavedEnergies;
327    Ipp32f fSIDGain;
328    Ipp32f fPrevEnergy;
329    Ipp32s lFrameCounter0;
330 }CNGmemory;
331 
332 typedef struct _PSTmemory{
333    Ipp32f STPNumCoeff[SHORTTERM_POSTFLT_LEN_E];  /* s.t. numerator coeff.        */
334    Ipp32f STPMemory[BWD_LPC_ORDER];              /* s.t. postfilter memory       */
335    Ipp32f ZeroMemory[BWD_LPC_ORDER];             /* null memory to compute h_st  */
336    Ipp32f ResidualMemory[SIZE_RESISDUAL_MEMORY]; /* A(gamma2) residual           */
337    Ipp32f gainPrec;
338 }PSTmemory;
339 
340 typedef struct _MusDetectMemory{
341    Ipp32s lMusicCounter;
342    Ipp32f fMusicCounter;
343    Ipp32s lZeroMusicCounter;
344    Ipp32f fMeanPitchGain;
345    Ipp32s lPFlagCounter;
346    Ipp32f fMeanPFlagCounter;
347    Ipp32s lConscPFlagCounter;
348    Ipp32s lRCCounter;
349    Ipp32f MeanRC[10];
350    Ipp32f fMeanFullBandEnergy;
351 }MusDetectMemory;
352 
353 typedef struct _PHDmemory{
354    Ipp32s prevDispState;
355    Ipp32f gainMem[6];
356    Ipp32f prevCbGain;
357    Ipp32s onset;
358 }PHDmemory;
359 
360 struct _G729FPEncoder_Obj{
361    G729CCoder_Obj      objPrm;
362    ScratchMem_Obj      Mem;
363    Ipp32f OldSpeechBuffer[SPEECH_BUFF_LEN];
364    Ipp32f fBetaPreFilter;
365    Ipp32f OldWeightedSpeechBuffer[FRM_LEN+PITCH_LAG_MAX];
366    Ipp32f OldExcitationBuffer[FRM_LEN+PITCH_LAG_MAX+INTERPOL_LEN];
367    Ipp32f WeightedFilterMemory[BWD_LPC_ORDER];
368    Ipp32f FltMem[BWD_LPC_ORDER];
369    Ipp32f OldLSP[LPC_ORDER];
370    Ipp32f OldQuantLSP[LPC_ORDER];
371    Ipp32f ExcitationError[4];
372    IppsIIRState_32f *iirstate;
373    Ipp32f PastQuantEnergy[4];
374    Ipp32f PrevFreq[MOVING_AVER_ORDER][LPC_ORDER];    /* previous LSP vector       */
375    /* Last forkward A(z) for case of unstable filter */
376    Ipp32f OldForwardLPC[LPC_ORDERP1];
377    Ipp32f OldForwardRC[2];
378    Ipp16s sFrameCounter; /* frame counter for VAD*/
379    /* DTX variables */
380    Ipp32s prevVADDec;
381    Ipp32s prevPrevVADDec;
382    Ipp16s sCNGSeed;
383    Ipp8s *vadMem;
384    Ipp8s *cngMem;
385    Ipp8s *msdMem;
386    /* G729CA_CODEC*/
387    Ipp32f ZeroMemory[LPC_ORDER];
388    /* Not G.729A */
389    Ipp32f SynFltMemory[BWD_LPC_ORDER];
390    Ipp32f ErrFltMemory[BWD_LPC_ORDER+SUBFR_LEN];
391    Ipp32f UnitImpulse[SUBFR_LEN+BWD_LPC_ORDERP1];
392    /* for G.729E */
393    /* for the backward analysis */
394    Ipp32f PrevFlt[BWD_LPC_ORDERP1]; /* Previous selected filter */
395    Ipp32f SynthBuffer[BWD_ANALISIS_WND_LEN];
396    Ipp32s prevLPCMode;
397    Ipp32f BackwardLPCMemory[BWD_LPC_ORDERP1];
398    Ipp32s isBWDDominant;
399    Ipp32f fInterpolationCoeff;
400    Ipp16s sGlobalStatInd;  /* Mesure of global stationnarity */
401    Ipp16s sBWDStatInd;       /* Num of consecutive backward frames */
402    Ipp16s sValBWDStatInd;   /* Value associated with stat_bwd */
403    /* Last backward A(z) for case of unstable filter */
404    Ipp32f OldBackwardLPC[BWD_LPC_ORDERP1];
405    Ipp32f OldBackwardRC[2];
406    Ipp32s LagBuffer[5];
407    Ipp32f PitchGainBuffer[5];
408    Ipp32s  sBWDFrmCounter;
409    Ipp32s sFWDFrmCounter;
410    Ipp32s isSmooth;
411    Ipp32f LogAreaRatioCoeff[2];
412    Ipp32s sSearchTimes;
413    IppsWinHybridState_G729E_32f *pHWState;
414 };
415 
416 struct _G729FPDecoder_Obj{
417    G729CCoder_Obj       objPrm;
418    ScratchMem_Obj      Mem;
419    Ipp32f OldExcitationBuffer[FRM_LEN+PITCH_LAG_MAX+INTERPOL_LEN];
420    Ipp32f fBetaPreFilter;   /* pitch sharpening of previous frame */
421    Ipp32s   prevPitchDelay; /* integer delay of previous frame    */
422    Ipp32f fCodeGain;        /* Code gain                          */
423    Ipp32f fPitchGain;       /* Pitch gain                         */
424    Ipp32f OldLSP[LPC_ORDER];
425    IppsIIRState_32f *iirstate;
426    Ipp32f PastQuantEnergy[4];
427    Ipp32f PrevFreq[MOVING_AVER_ORDER][LPC_ORDER];    /* previous LSP vector       */
428    Ipp32s prevMA;                  /* previous MA prediction coef.*/
429    Ipp32f prevLSF[LPC_ORDER];            /* previous LSF vector         */
430    /* for G.729B */
431    Ipp16s sFESeed;
432    /* CNG variables */
433    Ipp32s  prevFrameType;
434    Ipp32s  prevSID[4];/*prev SID need for erased only*/
435    Ipp16s sCNGSeed;
436    Ipp32f SID;
437    Ipp32f fCurrGain;
438    Ipp32f SIDLSP[LPC_ORDER];
439    Ipp32f fSIDGain;
440    Ipp32f SynFltMemory[BWD_LPC_ORDER];        /* Synthesis filter's memory          */
441    Ipp8s *phdMem;
442    /* for G.729A */
443    Ipp32f PstFltMemoryA[LPC_ORDER];
444    Ipp32f fPastGain;
445    Ipp32f ResidualBufferA[PITCH_LAG_MAX+SUBFR_LEN]; /* inverse filtered synthesis (with A(z/GAMMA2_POSTFLT))   */
446    Ipp32f *ResidualMemory;
447    Ipp32f PstSynMemoryA[LPC_ORDER];   /* memory of filter 1/A(z/GAMMA1_POSTFLT) */
448    Ipp32f fPreemphMemoryA;
449    /* Not G.729A */
450    Ipp32f SynthBuffer[BWD_ANALISIS_WND_LEN];  /* Synthesis                   */
451    Ipp32s   prevFracPitchDelay;    /* integer delay of previous frame    */
452    /* for the backward analysis */
453    Ipp32f BackwardUnqLPC[BWD_LPC_ORDERP1];
454    Ipp32f BackwardLPCMemory[BWD_LPC_ORDERP1];
455    Ipp32s   lPrevVoicing;
456    Ipp32s   lPrevBFI;
457    Ipp32s   prevLPCMode;
458    Ipp32f fFEInterpolationCoeff;
459    Ipp32f fInterpolationCoeff;
460    Ipp32f PrevFlt[BWD_LPC_ORDERP1]; /* Previous selected filter */
461    Ipp32s   lPrevPitchPT;
462    Ipp32s   lStatPitchPT;
463    Ipp32s   lStatPitch2PT;
464    Ipp32s   lStatFracPT;
465    /* Last backward A(z) for case of unstable filter */
466    Ipp32f OldBackwardLPC[BWD_LPC_ORDERP1];
467    Ipp32f OldBackwardRC[2];
468    Ipp32f  fPitchGainMemory;
469    Ipp32f  fCodeGainMemory;
470    Ipp32f  fGainMuting;
471    Ipp32s  lBFICounter;
472    Ipp32s  sBWDStatInd;
473    Ipp32s  lVoicing; /* voicing from previous frame */
474    Ipp32f g1PST;
475    Ipp32f g2PST;
476    Ipp32f gHarmPST;
477    Ipp32s  sBWDFrmCounter;
478    Ipp32s sFWDFrmCounter;
479    Ipp8s *pstMem;
480    IppsWinHybridState_G729E_32f *pHWState;
481 };
482 
483 #define   G729_CODECFUN(type,name,arg)                extern type name arg
484 
Parity(Ipp32s val)485 __INLINE Ipp32s Parity( Ipp32s val){
486   Ipp32s temp, nBits, bit;
487   Ipp32s sum;
488 
489   temp = val >> 1;
490   sum = 1;
491   for (nBits = 0; nBits <= 5; nBits++) {
492     temp = temp >> 1;
493     bit = temp & 0x00000001;
494     sum += bit;
495   }
496   sum = sum & 0x00000001;
497 
498   return sum;
499 }
500 
501 static __ALIGN32 CONST Ipp16s modtab[]={1, 2, 0, 1, 2, 0, 1, 2};
502 
DecodeAdaptCodebookDelays(Ipp32s * prevPitchDelay,Ipp32s * prevFracPitchDelay,Ipp32s * delayLine,Ipp32s NSbfr,Ipp32s badPitch,Ipp32s pitchIndx,G729Codec_Type type)503 __INLINE void DecodeAdaptCodebookDelays(Ipp32s *prevPitchDelay, Ipp32s *prevFracPitchDelay,Ipp32s *delayLine,
504                                          Ipp32s NSbfr, Ipp32s badPitch,Ipp32s pitchIndx,G729Codec_Type type){
505    Ipp16s minPitchDelay, maxPitchDelay;
506 
507    if(badPitch == 0){
508 
509       if (NSbfr == 0)                  /* if 1st subframe */
510       {
511          if (pitchIndx < 197)
512          {
513             delayLine[0] = (pitchIndx+2)/3 + 19;
514             delayLine[1] = pitchIndx - delayLine[0] * 3 + 58;
515          }
516          else
517          {
518             delayLine[0] = pitchIndx - 112;
519             delayLine[1] = 0;
520          }
521 
522       } else  {/* second subframe */
523          /* find minPitchDelay and maxPitchDelay for 2nd subframe */
524          minPitchDelay = (Ipp16s)(delayLine[0] - 5);
525          CLIP_TO_LOWLEVEL(minPitchDelay,PITCH_LAG_MIN);
526 
527          maxPitchDelay = (Ipp16s)(minPitchDelay + 9);
528          if (maxPitchDelay > PITCH_LAG_MAX)
529          {
530             maxPitchDelay = PITCH_LAG_MAX;
531             minPitchDelay = (Ipp16s)(maxPitchDelay - 9);
532          }
533          if (type == G729D_MODE) {
534             pitchIndx = pitchIndx & 15;
535             if (pitchIndx <= 3) {
536                delayLine[0] = minPitchDelay + pitchIndx;
537                delayLine[1] = 0;
538             }
539             else if (pitchIndx < 12) {
540                /* *T0_frac = index % 3; */
541                delayLine[1] = modtab[pitchIndx - 4];
542                delayLine[0] = (pitchIndx - delayLine[1])/3 + minPitchDelay + 2;
543 
544                if (delayLine[1] == 2) {
545                   delayLine[1] = -1;
546                   delayLine[0] += 1;
547                }
548             }
549             else {
550                 delayLine[0] = minPitchDelay + pitchIndx - 6;
551                 delayLine[1] = 0;
552             }
553 
554          }
555          else {
556             delayLine[0] = minPitchDelay + (pitchIndx + 2)/3 - 1;
557             delayLine[1] = pitchIndx - 2 - 3 * ((pitchIndx + 2)/3 - 1);
558          }
559       }
560       *prevPitchDelay = delayLine[0];
561       *prevFracPitchDelay = delayLine[1];
562    }else {                     /* Bad frame, or parity error */
563       delayLine[0]  =  *prevPitchDelay;
564       if (type == G729E_MODE) {
565          delayLine[1] = *prevFracPitchDelay;
566       }
567       else {
568          delayLine[1] = 0;
569          *prevPitchDelay += 1;
570          CLIP_TO_UPLEVEL(*prevPitchDelay,PITCH_LAG_MAX);
571       }
572    }
573 }
574 
575 void CodewordImpConv_G729_32f(Ipp32s index, const Ipp32f *pSrc1,const Ipp32f *pSrc2,Ipp32f *pDst);
576 
577 #define isVarZero(var) (fabs(var) < IPP_MINABS_32F)
578 
579 #endif /* __OWNG729FP_H__ */
580