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) 2005-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/A/B/D/E speech codec: main own header file.
26 //
27 */
28
29 #ifndef __OWNG729_H__
30 #define __OWNG729_H__
31
32 #if defined( _WIN32_WCE)
33 #pragma warning( disable : 4505 )
34 #endif
35
36 #ifndef G72X_IPP_LEGACY
37 #include <ipps.h>
38 #include <ippsc.h>
39 #else
40 #include <ipps90legacy.h>
41 #include <ippsc90legacy.h>
42 #endif
43 #include "g729api.h"
44 #include "scratchmem.h"
45
46 #define ENC_KEY 0xecd729
47 #define DEC_KEY 0xdec729
48
49 #define G729_ENCODER_SCRATCH_MEMORY_SIZE (10240+40)
50 #define SEC_STAGE_BITS 5
51 #define FIR_STAGE_BITS 7
52 #define PARM_DIM 11
53 #define FIR_STAGE (1<<FIR_STAGE_BITS)
54 #define SPEECH_BUF_DIM 240
55 #define SEC_STAGE (1<<SEC_STAGE_BITS)
56 #define LP_LOOK_AHEAD 40
57 #define LP_WINDOW_DIM 240
58 #define LP_SUBFRAME_DIM 40
59 #define LP_FRAME_DIM 80
60 #define LPC_WINDOW (speechHistory + SPEECH_BUF_DIM - LP_WINDOW_DIM)
61 #define PRESENT_SPEECH (speechHistory + SPEECH_BUF_DIM - LP_FRAME_DIM - LP_LOOK_AHEAD)
62 #define MIN_PITCH_LAG 20
63 #define LPF_DIM 10 /*LP filter order */
64 #define INTERPOLATION_FILTER_DIM (10+1)
65 #define MAX_PITCH_LAG 143
66 #define L_prevExcitat (MAX_PITCH_LAG+INTERPOLATION_FILTER_DIM)
67 #define PITCH_SHARP_MIN 3277 /*pitch sharpening min = 0.2 */
68 #define BWF2 22938 /*bandwitdh = 0.7 */
69 #define PITCH_SHARP_MAX 13017 /*pitch sharpening max = 0.8 */
70 #define COEFF1 29491
71 #define CDBK1_BIT_NUM 3
72 #define CDBK2_BIT_NUM 4
73 #define CDBK1_DIM (1<<CDBK1_BIT_NUM)
74 #define LSF_MIN 40 /*lsf min = 0.005 */
75 #define CDBK2_DIM (1<<CDBK2_BIT_NUM)
76 #define LSP_MA_ORDER 4 /*MA moving average */
77 #define BW_EXP_FACT 321 /*bandwidth expansion factor */
78 #define LSF_MAX 25681 /*lsf max = 3.135 */
79 #define BWF2_PST 18022 /*weighting factor */
80 #define PITCH_GAIN_MAX 15564 /*for improving */
81 #define IMP_RESP_LEN 20
82 #define BWF1_PST BWF2 /*weighting factor */
83 #define CORR_DIM 616
84 #define RES_FIL_DIM (MAX_PITCH_LAG + 1 + 8)
85 #define SID_FRAME_MIN 3
86 #define SEED_INIT 11111
87 #define ACF_NOW 2
88 #define ACF_TOTAL 3
89 #define GAIN0 28672
90 #define GAIN1 (-IPP_MIN_16S-GAIN0)
91 #define GAIN_NUM 2
92 #define TOTAL_ACF_DIM (ACF_TOTAL *(LPF_DIM+1))
93 #define VAD_LPC_DIM 12
94 #define ACF_DIM (ACF_NOW *(LPF_DIM+1))
95 #define SIX_PI 19302
96
97 #define BWLPCF_DIM 30 /*backward LP filter order*/
98 #define SYNTH_BWD_DIM BWLPCF_DIM + 35
99 #define BWLPCF1_DIM (BWLPCF_DIM+1)
100 #define TBWD_DIM LP_FRAME_DIM + SYNTH_BWD_DIM
101 #define N0_98 32113
102 #define BWF2_PST_E 21300 /*Numerator wgt factor */
103 #define BWF1_PST_E BWF2 /*Denominator wgt factor 0.70*/
104 #define IMP_RESP_LEN_E 32 /*Imp.resp. len */
105 #define BWF_HARMONIC_E (1<<13)
106 #define BWF_HARMONIC (1<<14)
107
108 #define CDBK1_BIT_NUM_6K 3
109 #define CDBK2_BIT_NUM_6K 3
110 #define CDBK1_DIM_6K (1<<CDBK1_BIT_NUM_6K)
111 #define CDBK2_DIM_6K (1<<CDBK2_BIT_NUM_6K)
112 #define CNG_STACK_SIZE (1<<5)
113 #define G729_CODECFUN(type,name,arg) extern type name arg
114
115 typedef struct _SynthesisFilterState {
116 Ipp32s nTaps;
117 Ipp16s *buffer;
118 }SynthesisFilterState;
119
120 typedef struct _G729Coder_Obj {
121 Ipp32s objSize;
122 Ipp32s key;
123 Ipp32u mode;
124 G729Codec_Type codecType;
125 }G729Coder_Obj;
126
127 struct _G729Decoder_Obj {
128 G729Coder_Obj objPrm;
129 ScratchMem_Obj Mem;
130 Ipp16s resFilBuf1[RES_FIL_DIM+LP_SUBFRAME_DIM];
131 Ipp16s prevLSPfreq[LSP_MA_ORDER][LPF_DIM];
132 Ipp16s LTPostFilt[TBWD_DIM];
133 Ipp16s prevExcitat[LP_FRAME_DIM+L_prevExcitat];
134 Ipp16s prevSubfrLSP[LPF_DIM];
135 Ipp16s prevSubfrLSPquant[LPF_DIM];
136 Ipp16s zeroPostFiltVec1[LPF_DIM+BWLPCF_DIM+2];
137 Ipp16s decPrm[20]; /*analysis data pointer */
138 Ipp32s coderErr[4]; /*memory for improving */
139 Ipp16s pBwdLPC[BWLPCF1_DIM];
140 Ipp16s pBwdLPC2[BWLPCF1_DIM];
141 Ipp16s pPrevBwdLPC[BWLPCF1_DIM];
142 Ipp32s hwState[BWLPCF1_DIM];
143 Ipp16s pPrevFilt[BWLPCF1_DIM]; /* selected previously filter */
144 G729Codec_Type codecType;
145 Ipp16s preemphFilt;
146 Ipp16s seed;
147 Ipp16s voiceFlag;
148 Ipp16s gainExact; /*gain's precision */
149 Ipp16s betaPreFilter; /*quant adaptive codebook gain from the previous subframe */
150 Ipp16s gainNow;
151 Ipp16s sidGain;
152 Ipp16s seedSavage;
153 Ipp16s CNGvar;
154 Ipp16s SIDflag0;
155 Ipp16s lspSID[LPF_DIM];
156 Ipp16s SIDflag1;
157 Ipp16s CNGidx; /*CNG cache parameters */
158 Ipp8s *postProc; /*High pass post processing filter memory */
159 Ipp8s *synFltw; /*Synthesis filter memory */
160 Ipp8s *synFltw0; /*Synthesis filter memory */
161 Ipp32s pstFltMode; /*post filter: 1- on, 0- off */
162 Ipp16s gains[2]; /*pitch + vcodebook gains */
163 Ipp16s prevFrameDelay;
164 Ipp16s prevMA; /*previous MA prediction coef.*/
165 Ipp16s prevFrameQuantEn[4];
166 Ipp16s prevVoiceFlag;
167 Ipp16s prevBFI;
168 Ipp16s prevLPmode;
169 Ipp16s interpCoeff2;
170 Ipp16s interpCoeff2_2;
171 Ipp16s valGainAttenuation;
172 Ipp16s BFIcount;
173 IppsPhaseDispersion_State_G729D *PhDispMem;
174 Ipp16s pPrevBwdRC[2];
175 Ipp16s BWDFrameCounter;
176 Ipp16s stat_pitch; /*pitch stationarity */
177 Ipp16s prevFrameDelay2; /*previous frame delay */
178 Ipp16s pitchStatIntDelay;
179 Ipp16s pitchStatFracDelay;
180 Ipp16s prevPitch;
181 Ipp16s gammaPost1;
182 Ipp16s gammaPost2;
183 Ipp16s gammaHarm;
184 Ipp16s BWDcounter2;
185 Ipp16s FWDcounter2;
186 };
187 struct _G729Encoder_Obj {
188 G729Coder_Obj objPrm;
189 ScratchMem_Obj Mem;
190 Ipp16s encSyn[LP_FRAME_DIM];/*encodersynthesisbuffer*/
191 Ipp16s speechHistory[SPEECH_BUF_DIM];
192 Ipp16s prevLSPfreq[LSP_MA_ORDER][LPF_DIM];
193 Ipp16s energySfs[GAIN_NUM];/*energyscalefactors*/
194 Ipp16s pACF[ACF_DIM];
195 Ipp16s ACFsum[TOTAL_ACF_DIM];
196 Ipp16s BWDsynth[TBWD_DIM];
197 Ipp16s energy[GAIN_NUM];
198 Ipp16s prevFrameQuantEn[4];/*quantizedenergyforpreviousframes*/
199 Ipp16s resFilMem0[BWLPCF_DIM];
200 Ipp16s resFilMem[BWLPCF_DIM+LP_SUBFRAME_DIM];
201 Ipp16s quantLspSID[LPF_DIM];
202 Ipp16s prevSubfrLSP[LPF_DIM];
203 Ipp16s prevSubfrLSPquant[LPF_DIM];
204 Ipp32s hwState[BWLPCF1_DIM];
205 Ipp16s ACFsfs[ACF_NOW];
206 Ipp16s ACFsumSfs[ACF_TOTAL];
207 Ipp16s pPrevFilt[BWLPCF1_DIM];
208 Ipp16s pPrevBwdLPC[BWLPCF1_DIM];
209 Ipp16s pBwdLPC2[BWLPCF1_DIM];
210 Ipp16s betaPreFilter;/*quantadaptivecodebookgainfromtheprevioussubframe*/
211 Ipp16s prevWgtSpeech[LP_FRAME_DIM+MAX_PITCH_LAG+1];
212 Ipp16s prevExcitat[LP_FRAME_DIM+L_prevExcitat+2];
213 Ipp16s encPrm[19];
214 Ipp16s zeroPostFiltVec1[LP_SUBFRAME_DIM+BWLPCF_DIM+1];/*zeroextendedimpulseresponse*/
215 Ipp16s prevCoeff[LPF_DIM+1];
216 Ipp16s prevSubfrLPC[LPF_DIM+1];
217 Ipp16s reflC[LPF_DIM+1];
218 G729Codec_Type codecType;
219 Ipp16s ACnorm;
220 Ipp16s ACFcounter;
221 Ipp16s gainNow;
222 Ipp16s energyDim;/*energiesnumber*/
223 Ipp16s SIDframeCounter;
224 Ipp16s prevSubfrSmooth;/*perceptualweighting*/
225 Ipp16s sidGain;
226 Ipp16s speechDiff;
227 Ipp16s prevLPmode;
228 Ipp16s *pSynth;
229 Ipp16s pPrevBwdRC[2];
230 Ipp16s prevLAR[2];/*previoussubframelogarearatio*/
231 Ipp16s prevDTXEnergy;
232 Ipp16s seed;
233 Ipp16s CNGidx;/*CNGcacheparameters*/
234 Ipp8s *preProc;/*highpasspreprocessingfiltermemory*/
235 Ipp8s *synFltw;/*synthesisfilter1memory*/
236 Ipp8s *synFltw0;/*synthesisfilter2memory*/
237 Ipp8s *vadMem;/*VADmemory*/
238 Ipp32s mode;/*mode's*/
239 Ipp16s extraTime;/*fixedcodebooksearchextratime*/
240 Ipp16s prevRC[2];
241 Ipp32s coderErr[4];
242 Ipp16s dominantBWDmode;
243 Ipp16s interpCoeff2_2;
244 Ipp16s statGlobal;/*globalstationaritymesure*/
245 Ipp16s pLag[5];
246 Ipp16s pGain[5];
247 Ipp16s BWDFrameCounter;/*consecutivebackwardframesNbre*/
248 Ipp16s val_BWDFrameCounter;/*BWDFrameCounterassociated*/
249 Ipp16s BWDcounter2;
250 Ipp16s FWDcounter2;
251 };
252
253 Ipp16s ExtractBitsG729(const Ipp8u **pSrc, Ipp32s *len, Ipp32s Count);
254 void NoiseExcitationFactorization_G729B_16s(const Ipp16s *pSrc,Ipp32s val1,
255 Ipp16s val2, Ipp16s *pDst, Ipp32s len);
256 Ipp32s ComfortNoiseExcitation_G729B_16s_I(const Ipp16s *pSrc, const Ipp16s *pPos,
257 const Ipp16s *pSign, Ipp16s val, Ipp16s t,
258 Ipp16s *pSrcDst, Ipp16s *t2, Ipp16s *Sfs);
259 void RandomCodebookParm_G729B_16s(Ipp16s *pSrc1, Ipp16s *pSrc2, Ipp16s *pSrc3,
260 Ipp16s *pSrc4, Ipp16s *n);
261 void QuantSIDGain_G729B_16s(const Ipp16s *pSrc, const Ipp16s *pSrcSfs,
262 Ipp32s len, Ipp16s *p, Ipp32s *pIdx);
263 void Sum_G729_16s_Sfs(const Ipp16s *pSrc, const Ipp16s *pSrcSfs,
264 Ipp16s *pDst, Ipp16s *pDstSfs, Ipp32s len, Ipp32s*pSumMem);
265 void VADMusicDetection( G729Codec_Type codecType, Ipp32s Val, Ipp16s expVal, Ipp16s *rc,
266 Ipp16s *lags, Ipp16s *pgains, Ipp16s stat_flg,
267 Ipp16s *Vad, Ipp8s*pVADmem);
268 IppStatus SynthesisFilter_G729_16s (const Ipp16s *pLPC, const Ipp16s *pSrc,
269 Ipp16s *pDst, Ipp32s len, Ipp8s *pMemUpdated, Ipp32s HistLen);
270 IppStatus SynthesisFilter_G729_16s_update (const Ipp16s *pLPC, const Ipp16s *pSrc,
271 Ipp16s *pDst, Ipp32s len, Ipp8s *pMemUpdated, Ipp32s hLen,
272 Ipp32s update);
273 void SynthesisFilterOvf_G729_16s_I(const Ipp16s *pLPC, Ipp16s *pSrcDst,
274 Ipp32s len, Ipp8s *pMemUpdated, Ipp32s HistLen);
275 void SynthesisFilterOvf_G729_16s (const Ipp16s *pLPC, const Ipp16s *pSrc,
276 Ipp16s *pDst, Ipp32s len, Ipp8s *pMemUpdated, Ipp32s HistLen);
277 void SynthesisFilterInit_G729 (Ipp8s *pMemUpdated);
278 void SynthesisFilterSize_G729 (Ipp32s *pSize);
279 void CodewordImpConv_G729(Ipp32s index, const Ipp16s *pSrc1,const Ipp16s *pSrc2,Ipp16s *pDst);
280 void _ippsRCToLAR_G729_16s (const Ipp16s*pSrc, Ipp16s*pDst, Ipp32s len);
281 void _ippsPWGammaFactor_G729_16s (const Ipp16s*pLAR, const Ipp16s*pLSF,
282 Ipp16s *flat, Ipp16s*pGamma1, Ipp16s*pGamma2, Ipp16s *pMem );
283 void CNG_encoder(Ipp16s *exc, Ipp16s *prevSubfrLSPquant, Ipp16s *Aq, Ipp16s *ana, G729Encoder_Obj *encoderObj);
284 void CNG_Update(Ipp16s *pSrc, Ipp16s val, Ipp16s vad, G729Encoder_Obj *encoderObj);
285 void Post_G729(Ipp16s idx, Ipp16s id, const Ipp16s *LPC, Ipp16s *pDst, Ipp16s *voiceFlag, G729Decoder_Obj *decoderObj);
286 void Post_G729AB(Ipp16s idx, Ipp16s id, const Ipp16s *LPC, Ipp16s *pDst, Ipp16s vad, G729Decoder_Obj *decoderObj);
287 void Post_G729I(Ipp16s idx, Ipp16s id, const Ipp16s *LPC, Ipp16s *pDst, Ipp16s *val, Ipp16s val1, Ipp16s val2,
288 Ipp16s fType, G729Decoder_Obj *decoderObj);
289 void Post_G729Base(Ipp16s idx, Ipp16s id, const Ipp16s *LPC, Ipp16s *pDst, Ipp16s *voiceFlag, Ipp16s ftyp, G729Decoder_Obj *decoderObj);
290 void updateExcErr_G729(Ipp16s x, Ipp32s y, Ipp32s *err);
291 Ipp16s calcErr_G729(Ipp32s val, Ipp32s *pSrc);
292 void BWDLagWindow(Ipp32s *pSrc, Ipp32s *pDst);
293 void SetLPCMode_G729E(Ipp16s *signal_ptr, Ipp16s *aFwd, Ipp16s *pBwdLPC,
294 Ipp16s *lpMode, Ipp16s *lspnew, Ipp16s *lspold,
295 G729Encoder_Obj *encoderObj);
296 void PitchTracking_G729E(Ipp16s *val1, Ipp16s *val2, Ipp16s *prevPitch,
297 Ipp16s *pitchStat, Ipp16s *pitchStatIntDelay, Ipp16s *pitchStatFracDelay);
298 Ipp16s enerDB(Ipp16s *synth, Ipp16s L);
299 void tstDominantBWDmode(Ipp16s *BWDcounter2,Ipp16s *FWDcounter2,Ipp16s *highStat, Ipp16s mode);
300 void Init_CNG_encoder(G729Encoder_Obj *encoderObj);
301 void Log2_G729(Ipp32s val, Ipp16s *pDst1, Ipp16s *pDst2);
302
303 extern CONST Ipp16s gammaFac1[2*(LPF_DIM+1)];
304 extern CONST Ipp16s g729gammaFac1_pst[LPF_DIM+1];
305 extern CONST Ipp16s g729gammaFac2_pst[LPF_DIM+1];
306 extern CONST Ipp16s cngSeedOut[CNG_STACK_SIZE];
307 extern CONST Ipp16s cngCache[CNG_STACK_SIZE][LP_SUBFRAME_DIM];
308 extern CONST Ipp16s LUT1[CDBK1_DIM];
309 extern CONST Ipp16s presetLSP[LPF_DIM];
310 extern CONST Ipp32s cngInvSqrt[CNG_STACK_SIZE];
311 extern CONST Ipp16s resetPrevLSP[LPF_DIM];
312 extern CONST Ipp16s LUT2[CDBK2_DIM];
313 extern CONST Ipp16s presetOldA[LPF_DIM+1];
314 extern CONST Ipp16s areas[L_prevExcitat-1+3];
315 extern CONST Ipp16s SIDgain[32];
316
317 #include "aux_fnxs.h"
318
equality(Ipp32s val)319 __INLINE Ipp32s equality( Ipp32s val) {
320 Ipp32s temp, i, bit;
321 Ipp32s sum;
322 sum = 1;
323 temp = val >> 1;
324 for(i = 0; i <6; i++) {
325 temp >>= 1;
326 bit = temp & 1;
327 sum += bit;
328 }
329 sum &= 1;
330 return sum;
331 }
332
333 static __ALIGN32 CONST Ipp16s table0[8]={
334 1*1, 2*1, 0*1,
335 1*1, 2*1, 0*1,
336 1*1, 2*1};
DecodeAdaptCodebookDelays(Ipp16s * prevFrameDelay,Ipp16s * prevFrameDelay2,Ipp16s * delay,Ipp32s id,Ipp32s bad_pitch,Ipp32s pitchIndx,G729Codec_Type type)337 __INLINE void DecodeAdaptCodebookDelays(Ipp16s *prevFrameDelay, Ipp16s *prevFrameDelay2,Ipp16s *delay,
338 Ipp32s id, Ipp32s bad_pitch,Ipp32s pitchIndx,G729Codec_Type type) {
339 Ipp16s minPitchSearchDelay, maxPitchSearchDelay;
340
341 if(bad_pitch == 0) {
342 if(id == 0) { /*if 1-st subframe */
343 if(pitchIndx < 197) {
344 delay[0] = (Ipp16s)((pitchIndx+2)/3 + 19);
345 delay[1] = (Ipp16s)(pitchIndx - delay[0] * 3 + 58);
346 } else {
347 delay[0] = (Ipp16s)(pitchIndx - 112);
348 delay[1] = 0;
349 }
350
351 } else {
352 /*find minPitchSearchDelay and maxPitchSearchDelay for 2-nd subframe */
353 minPitchSearchDelay = (Ipp16s)(delay[0] - 5);
354 if(minPitchSearchDelay < MIN_PITCH_LAG)
355 minPitchSearchDelay = MIN_PITCH_LAG;
356
357 maxPitchSearchDelay = (Ipp16s)(minPitchSearchDelay + 9);
358 if(maxPitchSearchDelay > MAX_PITCH_LAG) {
359 minPitchSearchDelay = MAX_PITCH_LAG - 9;
360 }
361 if(type == G729D_CODEC /* i.e. 6.4 kbps */) {
362 pitchIndx = pitchIndx & 15;
363 if(pitchIndx <= 3) {
364 delay[0] = (Ipp16s)(minPitchSearchDelay + pitchIndx);
365 delay[1] = 0;
366 } else if(pitchIndx < 12) {
367 delay[1] = table0[pitchIndx - 4];
368 delay[0] = (Ipp16s)((pitchIndx - delay[1])/3 + 2 + minPitchSearchDelay);
369
370 if(delay[1] == 2) {
371 delay[1] = -1;
372 delay[0]++;
373 }
374 } else {
375 delay[0] = (Ipp16s)(minPitchSearchDelay + pitchIndx - 6);
376 delay[1] = 0;
377 }
378 } else {
379 delay[0] = (Ipp16s)(minPitchSearchDelay + (pitchIndx + 2)/3 - 1);
380 delay[1] = (Ipp16s)(pitchIndx - 2 - 3 *((pitchIndx + 2)/3 - 1));
381 }
382 }
383 *prevFrameDelay = delay[0];
384 *prevFrameDelay2 = delay[1];
385 } else { /* non-equal or bad frame*/
386 delay[0] = *prevFrameDelay;
387 if(type == G729E_CODEC) {
388 delay[1] = *prevFrameDelay2;
389 } else {
390 delay[1] = 0;
391 *prevFrameDelay += 1;
392 if(*prevFrameDelay > MAX_PITCH_LAG) {
393 *prevFrameDelay = MAX_PITCH_LAG;
394 }
395 }
396 }
397 }
398
399 #endif /* __OWNG729_H__ */
400