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.723.1 speech codec: main own header file.
26 //
27 */
28 
29 #ifndef __OWNG723_H__
30 #define __OWNG723_H__
31 
32 #if defined( _WIN32_WCE)
33 #pragma warning( disable : 4505 )
34 #endif
35 
36 #include "g723ipp.h"
37 #include "g723api.h"
38 #include "scratchmem.h"
39 
40 /* G.723.1 coder global constants */
41 #define  G723_FRM_LEN                  240
42 #define  G723_SBFR_LEN            (G723_FRM_LEN/4)
43 #define  G723_HALFFRM_LEN         (G723_FRM_LEN/2)
44 #define  G723_LPC_ORDER                 10                  /*order of LPC filters */
45 #define  G723_LPC_ORDERP1               11                  /*order of LPC filters plus 1*/
46 #define  SmoothCoeff                (Ipp32s) 0xffffc000     /* -0.25 */
47 #define  G723_MIN_PITCH                 18
48 #define  AdCdbkSizeHighRate             85
49 #define  G723_MAX_PITCH              ((G723_MIN_PITCH)+127)
50 #define  GRIDSIZE                        2
51 #define  AdCdbkSizeLowRate             170
52 #define  N_PULSES                        6
53 #define  G723_TOEPLIZ_MATRIX_SIZE      416
54 #define  N_GAINS                        24
55 #define  N_AUTOCORRS_BLOCKS              3  /* N frames for AutoCorrs averaging     */
56 #define  N_GAIN_AVER_FRMS                3  /* N frames for gain averaging    */
57 #define  NUM_PULSE_IN_BLOCK             11 /* N pulses per block             */
58 #define  INV_NUM_PULSE_IN_BLOCK       2979 /* 1/NUM_PULSE_IN_BLOCK                  */
59 #define  MAX_GAIN                       50 /* Maximum gain in CNG excitation    */
60 #define  AUOTOCORRS_BUFF_SIZE        ((N_AUTOCORRS_BLOCKS+1)*(G723_LPC_ORDER+1)) /* size of AutoCorrs array    */
61 #define  G723_MAX_GAIN               10000  /* Maximum gain for fixed CNG excitation   */
62 
63 typedef enum {
64    G723_Rate63 = 0,
65    G723_Rate53
66 }G723_Rate;
67 
68 typedef enum {
69    G723_UntransmittedFrm = 0,
70    G723_ActiveFrm,
71    G723_SIDFrm
72 }G723_FrameType;
73 
74 typedef  struct   {
75    Ipp16s    isBadFrame;
76    G723_FrameType     FrameType;
77    G723_Rate currRate;
78    Ipp32s    lLSPIdx ;
79    Ipp16s    PitchLag[2] ;
80    Ipp16s    AdCdbkLag[4];
81    Ipp16s    AdCdbkGain[4];
82    Ipp16s    sAmpIndex[4];
83    Ipp16s    sGrid[4];
84    Ipp16s    sTrainDirac[4];
85    Ipp16s    sAmplitude[4];
86    Ipp32s    sPosition[4];
87 } ParamStream_G723;
88 
89 typedef  struct   {
90    Ipp16s   sDelay;
91    Ipp16s   sGain;
92    Ipp16s   sScalingGain;
93 } GainInfo_G723;
94 
95 void DecoderCNG_G723(G723Decoder_Obj* decoderObj, ParamStream_G723 *CurrentParams, Ipp16s *pExcitation, Ipp16s *pDstLPC);
96 
97 void  FixedCodebookSearch_G723_16s(G723Encoder_Obj *encoderObj, ParamStream_G723 *Params, Ipp16s *pSrcDst, Ipp16s *ImpResp, Ipp16s Sfc);
98 void  InterpolationIndex_G723_16s( Ipp16s *pDecodedExc, Ipp16s sPitchLag, Ipp16s *pGain, Ipp16s *pGainSFS, Ipp16s *pDstIdx);
99 void ErrorUpdate_G723(Ipp32s *pError, Ipp16s openLoopLag, Ipp16s AdCbbkLag, Ipp16s AdCbbkGain, G723_Rate currRate);
100 void  PostFilter(G723Decoder_Obj* decoderObj, Ipp16s *pSrcDstSignal, Ipp16s *pSrcLPC );
101 void  LSPInterpolation(const Ipp16s *pSrcLSP, const Ipp16s *pSrcPrevLSP, Ipp16s *pDstLPC);
102 void    SetParam2Bitstream(G723Encoder_Obj* encoderObj, ParamStream_G723 *Params, Ipp8s *pDstBitStream);
103 void GetParamFromBitstream( const Ipp8s *pSrcBitStream, ParamStream_G723 *Params);
104 void   UpdateSineDetector(Ipp16s *SineWaveDetector, Ipp16s *isNotSineWave);
105 extern Ipp16s   LSFCTbl[G723_LPC_ORDER] ;
106 extern Ipp16s   PerceptualFltCoeffTbl[2*G723_LPC_ORDER] ;
107 extern Ipp16s   GainDBLvls[N_GAINS] ;
108 extern IppSpchBitRate SA_Rate[2];
109 
110 void EncoderCNG_G723(G723Encoder_Obj* encoderObj, ParamStream_G723 *Params, Ipp16s *pExcitation, Ipp16s *pDstLPC);
111 void UpdateAutoCorrs_G723(G723Encoder_Obj* encoderObj, const Ipp16s *pSrcAutoCorrs, const Ipp16s *pSrcAutoCorrsSFS);
112 void DecodeSIDGain_G723_16s (Ipp32s pIndx, Ipp16s *pGain);
113 
114 #define ComfortNoiseExcitation_G723_16s_Buff_Size (2*NUM_PULSE_IN_BLOCK+2*NUM_PULSE_IN_BLOCK+G723_SBFR_LEN/GRIDSIZE+4+2*G723_SBFR_LEN)*sizeof(Ipp16s)
115 
116 void ComfortNoiseExcitation_G723_16s (Ipp16s gain, Ipp16s *pPrevExc, Ipp16s *pExc,
117                                       Ipp16s *pSeed, Ipp16s *pOlp, Ipp16s *pLags,
118                                       Ipp16s *pGains, G723_Rate currRate, Ipp8s *buff, Ipp16s *CasheCounter);
119 void FixedCodebookGain_G723_16s(const Ipp16s *pSrc1, const Ipp16s *pSrc2,
120                                 Ipp16s *pGainCoeff, Ipp32s *pGainIdx, Ipp16s *y);
121 void ExcitationResidual_G723_16s(const Ipp16s *pSrc1, const Ipp16s *pSrc2,  Ipp16s *pSrcDst,G723Encoder_Obj *encoderObj);
122 
123 void FixedCodebookVector_G723_16s( Ipp32s lDecPos, Ipp32s lAmplitude, Ipp32s mamp, Ipp32s lGrid,
124        Ipp32s lGain, Ipp32s lNSbfr, G723_Rate currRate, Ipp16s *pDst, Ipp32s *pLag, Ipp16s *pGain );
125 
126 void QuantSIDGain_G723_16s(const Ipp16s *pSrc, const Ipp16s *pSrcSfs, Ipp32s len, Ipp32s *pIndx);
127 
128 void ResidualInterpolation_G723_16s_I
129         (Ipp16s *pSrcDst, Ipp16s *pDst,  Ipp32s lag, Ipp16s gain, Ipp16s *pSeed);
130 
131 struct _G723Encoder_Obj{
132    G723_Obj_t          objPrm;  /* must be on top     */
133 
134    Ipp16s              ZeroSignal[G723_SBFR_LEN];
135    Ipp16s              UnitImpulseSignal[G723_SBFR_LEN];
136    Ipp16s              PrevWeightedSignal[G723_MAX_PITCH+3];            /* All pitch operation buffers */
137    Ipp16s              FltSignal[G723_MAX_PITCH+G723_SBFR_LEN+3];     /* shift 3 - aligning */
138    Ipp16s              PrevExcitation[G723_MAX_PITCH+3];
139    Ipp16s              SignalDelayLine[G723_HALFFRM_LEN];/* Required memory for the delay */
140    Ipp16s              WeightedFltMem[2*G723_LPC_ORDER];/* Used delay lines */
141    Ipp16s              RingWeightedFltMem[2*G723_LPC_ORDER];/* formant perceptual weighting filter delay line */
142    Ipp16s              RingSynthFltMem[G723_LPC_ORDER]; /* synthesis filter delay line */
143    Ipp16s              PrevOpenLoopLags[2];
144    Ipp16s              PrevLSF[G723_LPC_ORDER];/* LSF previous vector */
145    Ipp16s              AverEnerCounter;
146    Ipp16s              PrevQGain;
147    Ipp16s              SIDLSP[G723_LPC_ORDER];
148    Ipp16s              CNGSeed;
149    Ipp16s              CurrGain;
150    Ipp16s              LPCSID[G723_LPC_ORDER];
151    Ipp16s              ReflectionCoeffSFS;
152    Ipp16s              sSearchTime;                 /* for ippsFixedCodebookSearch_G723_16s function*/
153    Ipp16s              prevSidLpc[G723_LPC_ORDERP1];
154    Ipp16s              SineWaveDetector;/* Sine wave detector */
155    Ipp16s              sSidGain;
156    Ipp16s              ReflectionCoeff[G723_LPC_ORDER+1];
157    G723_FrameType      PastFrameType;
158    Ipp16s              AutoCorrs[AUOTOCORRS_BUFF_SIZE];
159    Ipp16s              AutoCorrsSFS[N_AUTOCORRS_BLOCKS+1];
160    Ipp16s              GainAverEnergies[N_GAIN_AVER_FRMS];
161    Ipp32s              ExcitationError[5];/* Taming procedure errors */
162    Ipp32s              HPFltMem[2];/* High pass variables */
163    Ipp8s               *vadMem;               /* VAD memory */
164    Ipp32s              AdaptEnableFlag;
165    Ipp16s              CasheCounter;
166    ScratchMem_Obj      Mem;
167 };
168 
169 struct _G723Decoder_Obj{
170    G723_Obj_t          objPrm;  /* must be on top     */
171 
172    Ipp16s              PrevExcitation[G723_MAX_PITCH+3]; /* 3 - shift for aligning */
173    Ipp16s              PostFilterMem[2*G723_LPC_ORDER]; /* Fir+Iir delays */
174    Ipp16s              PrevLSF[G723_LPC_ORDER];/* LSF previous vector */
175    Ipp16s              ErasedFramesCounter;
176    Ipp16s              InterpolatedGain;
177    Ipp16s              SyntFltIIRMem[G723_LPC_ORDER];/* Used delay lines */
178    Ipp16s              InterpolationIdx;
179    Ipp16s              ResIntSeed;
180    Ipp16s              SIDLSP[G723_LPC_ORDER];
181    Ipp16s              ReflectionCoeff;
182    Ipp16s              PstFltGain;
183    Ipp16s              CurrGain;
184    G723_FrameType      PastFrameType;
185    Ipp16s              sSidGain;
186    Ipp16s              CNGSeed;
187    Ipp16s              CasheCounter;
188    ScratchMem_Obj      Mem;
189 };
190 
191 #define   G723_CODECFUN(type,name,arg)                extern type name arg
192 
193 #include "aux_fnxs.h"
194 
195 #endif /* __OWNG723_H__ */
196