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-2012 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: Miscellaneous.
26 //
27 */
28 
29 #include <ipps.h>
30 #include "owng729.h"
31 #include "vadg729.h"
32 #include <ippdefs.h>
33 
34 #if defined(__ICL ) && defined(_IPP_A6)
35     #include <xmmintrin.h>
36 #endif
37 
38 #define     ZEROcrossBegin  120
39 #define     VADinitFrame    32
40 #define     ZEROcrossEnd    200
41 static __ALIGN32 CONST Ipp16s t[GAIN_NUM+1] = {820,26<<1,26};
42 static __ALIGN32 CONST Ipp16s t1[GAIN_NUM+1] = {0,0,1};
43 static __ALIGN32 CONST Ipp16s energyLogTable[17] = { 2000+466, 2000+520, 2000+571, 2000+619, 2000+665, 2000+708, 2000+749,
44     2789, 2000+827, 2000+863, 2000+898, 2000+931, 2000+964, 2000+995, 3000+25, 3000+54, 3000+83};
45 static __ALIGN32 CONST Ipp32s wlag_bwd[BWLPCF_DIM]={
46     2100000000+47252352,   2100000000+47202688,   2100000000+47120000,   2100000000+47004032,   2100000000+46855040,
47     2100000000+46673024,   2100000000+46457728,   2100000000+46209536,   2100000000+45928192,   2100000000+45613952,
48     2100000000+45266432,   2100000000+44886016,   2100000000+44472704,   2100000000+44026240,   2100000000+43546880,
49     2100000000+43034624,   2100000000+42489344,   2100000000+41911296,   2100000000+41300224,   2100000000+40656512,
50     2100000000+39979776,   2100000000+39270400,   2100000000+38528128,   2100000000+37753344,   2100000000+36945920,
51     2100000000+36105600,   2100000000+35232896,   2100000000+34327552,   2100000000+33389696,   2100000000+32419328
52 };
53 __ALIGN32 CONST Ipp16s presetOldA[LPF_DIM+1]={ (1<<12), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
54 
55 __ALIGN32 CONST Ipp16s presetLSP[LPF_DIM]={ 3*10000, 26*1000,21*1000, 15*1000, 8*1000, 0*1000,
56     -8*1000,-15*1000,-21*1000,-26*1000};
57 
58 __ALIGN32 CONST Ipp16s resetPrevLSP[LPF_DIM] = { 2000+339, 4000+679,7000+18, 9000+358, 11000+698,
59     14000+37, 16000+377,18000+717, 21000+56, 23000+396};
60 __ALIGN32 CONST Ipp16s SIDgain[32] = {
61     (1<<1), (1<<2)+1,  (1<<3), (1<<3)+5,
62     (1<<4)+(1<<2), (1<<5), 5*10, (1<<6),
63     8*10,  100+1,  100+27,  100+60,
64     200+01,  200+53,  300+18,  400+1,
65     500+5,  600+35,  800, 1000+7,
66     1000+268, 1000+596, 2000+10, 2000+530,
67     3000+185, 4000+9, 5000+48, 6000+355,
68     8*1000,10000+71,12000+679,15000+962
69 };
70 __ALIGN32 CONST Ipp16s areas[L_prevExcitat-1+3] = {
71     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
72     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
73     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
74     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
75     2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
76     2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
77     3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
78     3,3,3,3,3,3,3,3,3,3,3,3,3,0,0,0
79 };
80 __ALIGN32 CONST Ipp16s LUT1[CDBK1_DIM]={
81         5,
82         1,
83         4,
84         7,
85         3,
86         0,
87         6,
88         2
89 };
90 __ALIGN32 CONST Ipp16s LUT2[CDBK2_DIM]={
91         4,
92         6,
93         0,
94         2,
95         12,
96         14,
97         8,
98         10,
99         15,
100         11,
101         9,
102         13,
103         7,
104         3,
105         1,
106         5
107 };
108 
109 static __ALIGN32 CONST Ipp16s logTable[33]={
110     0,1455,2866,4236,5568,6863,
111 8124,9352,10549,11716,12855,13967,
112 15054,16117,17156,18172,19167,20142,
113 21097,22033,22951,23852,24735,25603,
114 26455,27291,28113,28922,29716,30497,
115 31266,32023, IPP_MAX_16S};
116 
117 __ALIGN32 CONST Ipp16s cngCache[CNG_STACK_SIZE][LP_SUBFRAME_DIM]={
118     {/* 0 */
119 86,-609,391,173,-80,-718,-47,559,235,676,
120 -4,-110,5,-521,-506,-300,256,-727,-527,-9,
121 -38,60,-69,-263,661,-722,-154,1500,303,-511,
122 -272,-866,938,179,91,833,4,-698,933,-573*1
123     },
124     {/* 1 */
125 225,547,-292,-470,302,264,728,-578,-294,332,
126 1589,-845,487,241,241,775,83,-523,-242,188,
127 -992,602,650,-48,332,542,-153,581,-39,323,
128 931,25,-59,-569,-192,-178,-238,-596,60,482*1
129 },
130 {/*2*/
131 -57,-821,254,-1,365,-278,21,-371,-12,-536,
132 11,1020,-166,455,-798,74,-95,133,152,-647,
133 199,-476,-209,-633,-306,-350,-859,118,436,-511,
134 -260,68,379,63,50,755,33,-674,585,-383*1
135 },
136 {/*3*/
137 1140,589,364,-527,-516,-83,-220,1154,-27,878,
138 -707,-1166,557,-626,437,-317,-810,15,654,115,
139 -34,-273,-570,-380,-185,-464,863,-268,271,464,
140 -169,-571,312,-47,-592,-779,-64,49,616,646*1
141 },
142 {/*4*/
143 259,-1109,-623,852,406,757,-493,911,1032,-480,
144 630,937,-421,-214,184,-92,-883,-491,-293,382,
145 -864,735,221,-1223,-878,394,192,723,101,-1004,
146 130,-434,535,127,549,-88,-86,201,935,739*1
147 },
148 {/*5*/
149 -937,-304,-546,-350,572,460,626,-690,-128,-471,
150 103,347,549,-539,443,199,41,1282,-400,404,
151 -114,-642,644,-576,1106,-677,505,333,119,151,
152 -307,-455,-517,-204,-254,-378,-800,-208,150,-462*1
153 },
154 {/*6*/
155 -497,770,-829,-271,-722,281,1105,-392,-212,15,
156 192,-290,523,486,-1010,37,-55,-356,1085,75,
157 -408,51,331,338,486,169,1338,312,-443,512,
158 -1016,-1,387,-970,-73,-91,-94,-691,-443,44*1
159 },
160 {/*7*/
161 993,-320,-251,208,577,376,150,-71,-254,657,
162 -890,-255,-478,9,215,-342,-93,482,391,-334,
163 387,-486,152,-229,452,179,8,-29,100,-1109,
164 -39,780,-155,-252,9,-364,709,188,-358,-387*1
165 },
166 {/*8*/
167 -49,-395,248,506,25,-9,-461,-148,68,-166,
168 -179,-BW_EXP_FACT ,-434,156,72,1009,-455,-65,-1246,772,
169 -509,-319,477,-1034,942,420,-393,201,-200,102,
170 242,-133,-1885,777,-692,-498,-531,-117,-121,128*1
171 },
172 {/*9*/
173 -828,402,-490,-144,1216,806,-62,-588,-483,539,
174 -305,345,217,111,315,-419,-266,-476,-760,194,
175 -910,-200,52,646,-177,429,705,-597,-629,-127,
176 685,47,-216,182,-518,20,-476,-182,-858,344*1
177 },
178 {/*10*/
179 -250,903,-395,-654,-483,-491,-260,-400,-493,-636,
180 -414,79,-278,-46,1193,781,160,-255,-561,172,
181 311,-753,470,-214,173,1022,-324,-375,772,-52,
182 131,711,1082,635,297,486,81,521,688,484*1
183 },
184 {/*11*/
185 -492,537,420,213,-1076,170,-115,-362,485,-102,
186 469,-330,605,235,126,-201,-202,667,-122,-1001,
187 -914,-340,-273,-168,-505,284,-329,-264,-1,1005,
188 -288,761,-424,-227,360,-679,272,-338,-431,26*1
189 },
190 {/*12*/
191 345,156,94,317,-549,-809,208,-1432,574,-783,
192 803,-654,640,748,341,1117,-349,-314,360,807,
193 165,-383,-1190,-558,648,-485,-211,604,-950,-106,
194 223,-313,493,-272,-910,273,-145,43,-28,316*1
195 },
196 {/*13*/
197 838,-630,164,-588,-38,566,-24,-495,-49,67,
198 653,-563,291,431,145,234,984,636,-10,358,
199 492,-856,-326,835,-159,53,-289,-897,-459,-223,
200 -925,-228,-406,-146,-696,-232,-2,-230,396,117*1
201 },
202 {/*14*/
203 75,505,-74,-735,473,-128,-588,23,584,486,
204 -365,494,406,-211,81,-348,-571,339,240,571,
205 723,602,-402,-847,-830,-448,205,1032,-112,263,
206 -1013,62,-193,685,554,-683,973,-1228,808,843*1
207 },
208 {/*15*/
209 301,-392,-151,1056,702,-671,-470,-199,174,-343,
210 -694,-847,-769,82,-821,138,-593,602,683,-318,
211 192,195,748,-677,48,395,-116,-429,-511,-332,
212 -371,-84,-463,-452,493,356,-830,1061,-594,381*1
213 },
214 {/*16*/
215 64,-830,609,447,379,-460,1164,-222,149,-633,
216 -362,100,-624,-327,129,391,106,-52,75,-375,
217 -219,190,1014,-148,447,-623,-128,1070,570,67,
218 747,-815,-364,-298,-459,-1197,207,328,352,-74*1
219 },
220 {/*17*/
221 -258,-552,-344,654,-342,540,517,-125,439,-63,
222 -319,-41,-477,-315,222,-628,-528,810,-422,-352,
223 -228,-274,311,277,425,-493,-141,745,-107,151,
224 -240,30,226,636,524,690,-114,-64,105,167*1
225 },
226 {/*18*/
227 381,506,-475,-1123,4,-264,539,780,362,215,
228 242,346,433,-619,-344,137,215,-204,-705,-361,
229 -290,-1102,-331,-634,964,-239,268,-172,-629,335,
230 -446,515,-463,622,89,-111,-584,620,335,0*1
231 },
232 {/*19*/
233 -692,-1027,628,210,309,446,-371,450,-643,478,
234 261,-237,550,607,-1058,707,302,319,-234,-300,
235 -361,128,-337,-188,-417,32,679,-491,136,545,
236 256,837,272,129,-72,726,-519,-190,-303,-314*1
237 },
238 {/*20*/
239 -1242,-123,-605,31,408,-336,-1018,57,491,442,
240 -440,-460,29,165,107,14,48,-145,621,-55,
241 34,26,80,-157,-11,-348,-494,222,-89,-241,
242 -75,-456,-198,-167,311,-141,-340,-124,154,141*1
243 },
244 {/*21*/
245 780,-100,322,286,592,505,-201,-213,-779,437,
246 139,-873,-263,-302,-192,356,94,846,-172,-112,
247 -222,811,201,285,327,-409,-99,9,203,-1276,
248 -46,86,944,768,870,2,-12,605,-421,271*1
249 },
250 {/*22*/
251 -962,-727,353,-376,-452,-993,-48,238,-229,502,
252 286,53,217,684,-179,-421,1400,67,-421,-158,
253 -266,697,74,840,341,528,-232,15,-365,-442,
254 -313,-587,690,-163,341,60,-80,-173,196,-94*1
255 },
256 {/*23*/
257 145,200,230,265,338,-543,726,81,1138,-167,
258 295,-518,-13,-206,-553,2,-557,-1174,-792,-405,
259 -492,515,88,-1229,-332,763,-983,92,949,-428,
260 89,484,-235,12,234,461,214,549,-38,533*1
261 },
262 {/*24*/
263 -343,-121,-317,252,724,234,-35,591,734,556,
264 -296,385,-311,335,434,148,-365,81,-403,390,
265 -450,308,-1273,89,-568,-10,898,267,307,-359,
266 -36,-98,637,-229,1047,-496,424,-130,561,-413*1
267 },
268 {/*25*/
269 144,-10,402,-892,-531,235,-353,-471,169,-705,
270 267,-211,-314,244,216,-110,66,8,1028,-170,
271 -225,125,145,-389,-677,-431,-899,255,760,-122,
272 -54,740,-12,1050,630,40,-943,-497,131,-307*1
273 },
274 {/*26*/
275 557,805,-1268,-640,-477,660,-910,351,762,738,
276 -839,29,-336,17,-33,-68,839,-993,998,-966,
277 189,-242,-308,409,-236,220,147,471,74,-117,
278 -199,246,97,-231,-832,756,343,-1144,294,467*1
279 },
280 {/*27*/
281 284,762,-803,-282,309,-22,293,262,429,827,
282 462,-121,-890,-1301,214,103,-1090,252,-961,424,
283 -681,-148,7,328,335,-451,-464,-183,-600,365,
284 184,-87,-416,254,417,106,-135,-786,233,-118*1
285 },
286 {/*28*/
287 -149,359,96,-264,974,-639,179,2,16,380,
288 229,237,563,342,-776,-73,53,-239,-278,98,
289 24,171,701,-276,-38,-474,-401,343,-131,-128,
290 -1022,-95,255,-837,-140,-52,610,-555,199,-42*1
291 },
292 {/*29*/
293 682,6,184,-31,671,19,-675,413,-525,-129,
294 353,-326,-856,-436,198,822,188,-392,394,787,
295 49,517,-593,591,-251,-783,307,236,826,-194,
296 536,-280,206,746,-932,69,451,-521,1,257*1
297 },
298 {/*30*/
299 233,912,-316,-473,856,-10,419,510,-346,-707,
300 -159,-1356,-299,355,0,75,-541,106,-130,193,
301 -46,-432,-547,24,160,-233,283,590,-433,-835,
302 -1222,869,733,836,568,347,-436,-342,535,-462*1
303 },
304 {/*31*/
305 -754,1201,-390,137,254,505,-102,-511,-689,418,
306 -229,474,-1,938,251,-494,-752,19,-196,171,
307 -384,-805,-36,419,81,-508,221,252,-383,-116,
308 61,-332,274,373,510,-819,-510,-67,542,325*1
309 },
310 };
311 __ALIGN32 CONST Ipp16s cngSeedOut[CNG_STACK_SIZE]={
312     -26766,    5273,  -20980,   12987,   20566,    2637,  -17200,  -25649,
313     24058,  -31807,   27220,    6819,  -31650,  -27403,   23192,    9015,
314     18306,    4585,   -3684,   -5749,   -5274,  -29027,   21344,   24991,
315     29706,   24337,   25572,   16243,  -23186,  -26811,  -14552,   -2297
316 };
317 __ALIGN32 CONST Ipp32s cngInvSqrt[CNG_STACK_SIZE]={
318     320863, 323781, 388729, 303149, 269026, 324406, 304813, 391279,
319     300993, 335038, 312683, 356481, 286752, 349341, 292467, 315662,
320     323859, 418904, 340163, 350720, 452932, 342818, 349410, 315175,
321     357434, 352913, 286583, 332201, 413044, 346627, 309243, 358885
322 };
323 __ALIGN32 CONST Ipp16s gammaFac1[2*(LPF_DIM+1)]={
324 IPP_MAX_16S,24000+576,18000+432,13000+824,10000+368,7000+776,5000+832,4000+374,3000+281,2000+461,1000+846,
325 IPP_MAX_16S,24000+576,18000+432,13000+824,10000+368,7000+776,5000+832,4000+374,3000+281,2000+461,1000+846
326 };
327 __ALIGN32 CONST Ipp16s g729gammaFac2_pst[LPF_DIM+1] = {
328     IPP_MAX_16S,  BWF2_PST,  9000+912,  5000+451,  2000+998, 1000+649,  900+7,  400+99,  200+74,  100+51,   83
329 };
330 __ALIGN32 CONST Ipp16s g729gammaFac1_pst[LPF_DIM+1] = {
331     IPP_MAX_16S,  BWF2, 16000+57, 11200+40,  7800+68,  5500+8, 3800+56, 2600+99, 1800+89, 1300+22,  900+25
332 };
333 
334 static __ALIGN32 CONST Ipp16s gStats[6] ={400<<4,400<<3,400*6,400<<2,400<<1,400};
335 static __ALIGN32 CONST Ipp16s gThrs[6]  ={955,819,614,410,205,0};
336 static __ALIGN32 CONST Ipp16s gStats1[6]={400<<4,400<<3,400<<2,400<<1,400};
337 
338 /* ///////////////////////////////////////////////////////////////////////////////////////
339 //  Name:        updateExcErr_G729
340 //  Purpose:     update excitation error
341 //  Parameters:
342 //  pSrcDst - coder error
343 */
updateExcErr_G729(Ipp16s val,Ipp32s indx,Ipp32s * pSrcDst)344 void updateExcErr_G729( Ipp16s val, Ipp32s indx, Ipp32s *pSrcDst) {
345     Ipp32s   i, area1, area2, L_tmp, L_tmp1=0, tmp = 0;
346     Ipp16s high, low;
347     L_tmp = -1;
348     if(indx < LP_SUBFRAME_DIM) {
349         high   = (Ipp16s)(pSrcDst[0] >> 16);
350         low    = (Ipp16s)((pSrcDst[0]>>1) & IPP_MAX_16S);
351         tmp    = high*val + ((low*val)>>15);
352         tmp    = (tmp << 2) + (Ipp32s)BWF_HARMONIC;
353         L_tmp  = IPP_MAX(L_tmp,tmp);
354         high   = (Ipp16s)(tmp >> 16);
355         low    = (Ipp16s)((tmp>>1) & IPP_MAX_16S);
356         L_tmp1 = high*val + ((low*val)>>15);
357         L_tmp1 = ShiftL_32s(L_tmp1, 2);
358         L_tmp1 = Add_32s(L_tmp1, (Ipp32s)BWF_HARMONIC);
359         L_tmp  = IPP_MAX(L_tmp,L_tmp1);
360     } else {
361         area1 = areas[indx-LP_SUBFRAME_DIM];
362         area2 = areas[indx-1];
363         for(i = area1; i <= area2; i++) {
364             high   = (Ipp16s)(pSrcDst[i] >> 16);
365             low    = (Ipp16s)((pSrcDst[i]>>1) & IPP_MAX_16S);
366             L_tmp1 = high*val + ((low*val)>>15);
367             L_tmp1 = (L_tmp1 << 2) + (Ipp32s)BWF_HARMONIC;
368             L_tmp  = IPP_MAX(L_tmp,L_tmp1);
369         }
370     }
371     for(i=3; i>=1; i--) {
372         pSrcDst[i] = pSrcDst[i-1];
373     }
374     pSrcDst[0] = L_tmp;
375     return;
376 }
calcErr_G729(Ipp32s val,Ipp32s * pSrc)377 Ipp16s calcErr_G729(Ipp32s val, Ipp32s *pSrc) {
378     Ipp16s i, area1;
379     area1 = areas[IPP_MAX(0, val - LP_SUBFRAME_DIM - 10)];
380     for(i=areas[val + 10 - 2]; i>=area1; i--) {
381         if(pSrc[i] > 983040000)
382             return 1; /* Error threshold improving BWF_HARMONIC. * 60000.   */
383     }
384     return 0;
385 }
386 /* ///////////////////////////////////////////////////////////////////////////////////////
387 //  Name:    Log2_G729
388 //  Purpose: update excitation error
389 //  Parameters:
390 //   pDst1   - log2 integer part.    0<=val<=30
391 //   pDst2   - log2 fractional part. 0<=val<1
392 //   pSrcDst - coder error
393 */
Log2_G729(Ipp32s val,Ipp16s * pDst1,Ipp16s * pDst2)394 void Log2_G729(Ipp32s val, Ipp16s *pDst1, Ipp16s *pDst2) {
395     Ipp16s order, i, j;
396     Ipp32s   L_tmp;
397     if( val <= 0 ) {
398         pDst1[0] = pDst2[0] = 0;
399         return;
400     }
401     order    = Exp_32s(val);
402     val    <<= order;
403     pDst1[0] = (Ipp16s)(30 - order);
404     i        = (Ipp16s)(val>>25);
405     j        = (Ipp16s)((val >> 10) & IPP_MAX_16S);
406     i       -= 32;
407     L_tmp    = logTable[i]<<15;
408     L_tmp   -= (logTable[i] - logTable[i+1]) * j;
409     pDst2[0] = (Ipp16s)(L_tmp >> 15);
410     return;
411 }
NoiseExcitationFactorization_G729B_16s(const Ipp16s * pSrc,Ipp32s val1,Ipp16s val2,Ipp16s * pDst,Ipp32s len)412 void NoiseExcitationFactorization_G729B_16s(const Ipp16s *pSrc,
413                                             Ipp32s val1, Ipp16s val2, Ipp16s *pDst, Ipp32s len) {
414     Ipp32s   order, L_tmp;
415     Ipp16s high, low, tmp1;
416 
417     high = (Ipp16s)(val1>>16);
418     low = (Ipp16s)((val1>>1)&IPP_MAX_16S);
419     tmp1 = (Ipp16s)(((val2 * 19043)+BWF_HARMONIC)>>15);
420     tmp1 = (Ipp16s)(tmp1 + val2);
421     L_tmp = high*tmp1 + ((low*tmp1)>>15);
422     order = Norm_32s_I(&L_tmp);
423     tmp1 = (Ipp16s)(L_tmp>>16);
424     order -= 15;
425     ippsMulC_NR_16s_Sfs(pSrc,tmp1,pDst,len,15);
426     if(order<0) {
427         ippsLShiftC_16s(pDst,-order,pDst,len);
428     } else if(order>0) {
429         Ipp16s const1=(Ipp16s)(1<<(order-1));
430         ippsAddC_16s_Sfs(pDst,const1,pDst,len,0);
431         /* ippsRShiftC_NR_16s_I() */
432         ippsRShiftC_16s(pDst,order,pDst,len);
433     }
434 }
435 
ComfortNoiseExcitation_G729B_16s_I(const Ipp16s * pSrc,const Ipp16s * pPos,const Ipp16s * pSign,Ipp16s val,Ipp16s val1,Ipp16s * pSrcDst,Ipp16s * pSrcDst1,Ipp16s * shiftDst)436 Ipp32s ComfortNoiseExcitation_G729B_16s_I(const Ipp16s *pSrc, const Ipp16s *pPos,
437                                        const Ipp16s *pSign, Ipp16s val, Ipp16s val1,
438                                        Ipp16s *pSrcDst, Ipp16s *pSrcDst1, Ipp16s *shiftDst){
439     Ipp16s i, j, tmp1, tmp2, max, high, low, tmp3;
440     Ipp16s x1, x2, x3 = (Ipp16s)(val1<<1);
441     Ipp16s sVal0, sVal1, sVal2, sVal3;
442     Ipp32s   L_tmp, L_tmp1, L_tmp2, order, deltaSign = 1;
443 
444     ippsMulC_NR_16s_ISfs(x3,pSrcDst,LP_SUBFRAME_DIM,15);
445     ippsAdd_16s_Sfs(pSrc,pSrcDst,pSrcDst,LP_SUBFRAME_DIM,0);
446     ippsMax_16s(pSrcDst,LP_SUBFRAME_DIM,&tmp1);
447     ippsMin_16s(pSrcDst,LP_SUBFRAME_DIM,&tmp2);
448     if(tmp2==IPP_MIN_16S)
449         tmp2++;
450     if(tmp2<0)
451         tmp2 = (Ipp16s)(-tmp2);
452     max = (Ipp16s)(IPP_MAX(tmp1,tmp2));
453     if(max == 0)
454         order = 0;
455     else {
456         order = 3 - Exp_16s(max);
457         if(order < 0)
458             order = 0;
459     }
460     ippsRShiftC_16s(pSrcDst,order,shiftDst, LP_SUBFRAME_DIM);
461     ippsDotProd_16s32s_Sfs(shiftDst,shiftDst, LP_SUBFRAME_DIM, &L_tmp1, 0);
462     L_tmp1 <<= 1;
463 
464     tmp3 = 0;
465     for(i=0; i<4; i++) {
466         j = pPos[i];
467         if(pSign[i] == 0) {
468             tmp3 = (Ipp16s)(tmp3 - (pSrcDst[j] >> order));
469         } else {
470             tmp3 = (Ipp16s)(tmp3 + (pSrcDst[j] >> order));
471         }
472     }
473 
474     L_tmp   = val * LP_SUBFRAME_DIM;
475     L_tmp >>= 5;
476     L_tmp2  = val * L_tmp;
477     tmp1    = (Ipp16s)(order<<1);
478     L_tmp   = L_tmp2 >> tmp1;
479     L_tmp  -= L_tmp1;
480     tmp3  >>= 1;
481     L_tmp  += (tmp3 * tmp3)<<1;
482     order++;
483     if(L_tmp < 0) {
484         sVal0 = Abs_16s(pSrc[pPos[0]]); sVal1 = Abs_16s(pSrc[pPos[1]]);
485         sVal2 = Abs_16s(pSrc[pPos[2]]); sVal3 = Abs_16s(pSrc[pPos[3]]);
486         ippsCopy_16s(pSrc, pSrcDst, LP_SUBFRAME_DIM);
487         tmp1 = (Ipp16s)(sVal0 | sVal1);
488         tmp2 = (Ipp16s)(sVal2 | sVal3);
489         tmp1 = (Ipp16s)(tmp1 | tmp2);
490         order = ((tmp1 & BWF_HARMONIC) == 0) ? 1 : 2;
491 
492         for(tmp3=i=0; i<4; i++) {
493             tmp1 = (Ipp16s)(pSrc[pPos[i]] >> order);
494             if(pSign[i] == 0) {
495                 tmp3 = (Ipp16s)(tmp3 - tmp1);
496             } else {
497                 tmp3 = (Ipp16s)(tmp3 + tmp1);
498             }
499         }
500         high   = (Ipp16s)(L_tmp2>>15);
501         low    = (Ipp16s)(L_tmp2 & IPP_MAX_16S);
502         L_tmp  = high * 24576 + ((low*24576)>>15);
503         tmp1   = (Ipp16s)(2 * order - 2);
504         L_tmp  = L_tmp >> tmp1;
505         L_tmp += 2 * tmp3 * tmp3;
506         deltaSign = -1;
507     }
508 
509     tmp2 = (Ipp16s)ownSqrt_32s(L_tmp>>1);
510     x1   = (Ipp16s)(tmp2 - tmp3);
511     x2   = (Ipp16s)(-(tmp3 + tmp2));
512     sVal1 = Abs_16s(x1);
513     sVal2 = Abs_16s(x2);
514     if(sVal2 < sVal1)
515         x1 = x2;
516     tmp1 = (Ipp16s)(2 - order);
517     if(tmp1>=0)
518         pSrcDst1[0] = ShiftR_NR_16s(x1, tmp1);
519     else
520         pSrcDst1[0] = ShiftL_16s((Ipp16s)x1, (Ipp16u)(-tmp1));
521     if(pSrcDst1[0] > 5000)
522         pSrcDst1[0] = 5000;
523     else
524         if(pSrcDst1[0] < -5000)
525             pSrcDst1[0] = -5000;
526 
527     for(i=0; i<4; i++) {
528         j = pPos[i];
529         if(pSign[i] != 0) {
530             pSrcDst[j] = (Ipp16s)(pSrcDst[j] + pSrcDst1[0]);
531         } else {
532             pSrcDst[j] = (Ipp16s)(pSrcDst[j] - pSrcDst1[0]);
533         }
534     }
535     return deltaSign;
536 }
RandomCodebookParm_G729B_16s(Ipp16s * pSeed,Ipp16s * pPos,Ipp16s * pSign,Ipp16s * pGain,Ipp16s * delay)537 void RandomCodebookParm_G729B_16s(Ipp16s *pSeed, Ipp16s *pPos, Ipp16s *pSign,
538                                   Ipp16s *pGain, Ipp16s* delay){
539     Ipp16s tmp1, tmp2;
540     Ipp16s fractal;
541 
542     /* generate random adaptive codebook & fixed codebook parameters */
543     tmp1 = Rand_16s(pSeed);
544     fractal = (Ipp16s)((tmp1 & 3) - 1);
545     if(fractal == 2)
546         fractal = 0;
547 
548     tmp1   >>= 2;
549     delay[0] = (Ipp16s)((tmp1 & 0x003f) + 40);
550 
551     tmp1  >>= 6;
552     tmp2    = (Ipp16s)(tmp1 & 7);
553     pPos[0] = (Ipp16s)(5 * tmp2);
554 
555     tmp1   >>= 3;
556     pSign[0] = (Ipp16s)(tmp1 & 1);
557 
558     tmp1  >>= 1;
559     tmp2    = (Ipp16s)(tmp1 & 7);
560     pPos[1] = (Ipp16s)(5 * tmp2 + 1);
561 
562     tmp1   >>= 3;
563     pSign[1] = (Ipp16s)(tmp1 & 1);
564     tmp1     = Rand_16s(pSeed);
565     tmp2     = (Ipp16s)(tmp1 & 7);
566     pPos[2]  = (Ipp16s)(5 * tmp2 + 2);
567 
568     tmp1   >>= 3;
569     pSign[2] = (Ipp16s)(tmp1 & 1);
570 
571     tmp1  >>= 1;
572     tmp2    = (Ipp16s)(tmp1 & 0x000f);
573     pPos[3] = (Ipp16s)((tmp2 & 1) + 3);
574     tmp2    = (Ipp16s)((tmp2>>1) & 7);
575     pPos[3] = (Ipp16s)(pPos[3] + 5 * tmp2);
576 
577     tmp1   >>= 4;
578     pSign[3] = (Ipp16s)(tmp1 & 1);
579     delay[1] = fractal;
580     *pGain   = (Ipp16s)(Rand_16s(pSeed) & 0x1fff);
581 }
582 
ownQuantizeEnergy(Ipp32s val,Ipp16s order,Ipp16s * pDst)583 static Ipp16s ownQuantizeEnergy(Ipp32s val, Ipp16s order, Ipp16s *pDst) {
584     Ipp16s exp, fractal, tmp, tmp1, index;
585     Log2_G729(val, &exp, &fractal);
586     tmp1 = (Ipp16s)(exp - order);
587     tmp = (Ipp16s)(tmp1 << 10);
588     tmp = (Ipp16s)(tmp + ((fractal + 0x10)>>5));
589     if(tmp <= -2721) {/* 8 dB */
590         pDst[0] = -12;
591         return 0;
592     }
593     if(tmp > 22111) { /* 65 dB */
594         pDst[0] = 66;
595         return 31;
596     }
597     if(tmp <= 4762) { /* 14 dB */
598         tmp += 3401;
599         index = (Ipp16s)((tmp * 24)>>15);
600         if(index < 1)
601             index = 1;
602         pDst[0] = (Ipp16s)((index<<2) - 8);
603         return index;
604     }
605     tmp -= 340;
606     index = (Ipp16s)(((tmp*193)>>17) - 1);
607     if(index < 6)
608         index = 6;
609     pDst[0] = (Ipp16s)((index<<1) + 4);
610     return(index);
611 }
612 /* ///////////////////////////////////////////////////////////////////////////////////////
613 //  Name:    QuantSIDGain_G729B_16s
614 //  Purpose:
615 //  Parameters:
616 //   pSrc      array of energies
617 //   pSrcSfs   corresponding scaling factors
618 //   len       number of energies
619 //   pDst      decoded energies in dB
620 //   pIndx     SID gain quantization index
621 */
QuantSIDGain_G729B_16s(const Ipp16s * pSrc,const Ipp16s * pSrcSfs,Ipp32s len,Ipp16s * pDst,Ipp32s * pDstIndx)622 void QuantSIDGain_G729B_16s (const Ipp16s *pSrc, const Ipp16s *pSrcSfs,
623                              Ipp32s len, Ipp16s *pDst, Ipp32s *pDstIndx){
624     Ipp16s i,order,temp,high,low;
625     Ipp32s   val;
626     if(len == 0) {
627         val = *pSrc;
628 
629         if(pSrcSfs[0]<0)
630             val >>= - pSrcSfs[0];
631         else
632             val <<=  pSrcSfs[0];
633         order  = 0;
634         high = (Ipp16s)(val >> 16);
635         low  = (Ipp16s)((val>>1)&IPP_MAX_16S);
636         val  = high *  t[0] + ((low * t[0])>>15);
637     } else {
638         ippsMin_16s(pSrcSfs,len,&order);
639         order = (Ipp16s)(order + 16 - t1[len]);
640         val = 0;
641         for(i=0; i<len; i++) {
642             temp = (Ipp16s)(order - pSrcSfs[i]);
643             val += (pSrc[i]<<temp);
644         }
645         high = (Ipp16s)(val >> 16);
646         low  = (Ipp16s)((val>>1)&IPP_MAX_16S);
647         val  = high *  t[i] + (((low * t[i] >> 1) & 0xffff8000)>>14);
648     }
649 
650     *pDstIndx = ownQuantizeEnergy(val, order, pDst);
651 
652 }
Sum_G729_16s_Sfs(const Ipp16s * pSrc,const Ipp16s * pSrcSfs,Ipp16s * pDst,Ipp16s * pDSfs,Ipp32s len,Ipp32s * pSum)653 void Sum_G729_16s_Sfs(const Ipp16s *pSrc, const Ipp16s *pSrcSfs,
654                       Ipp16s *pDst, Ipp16s *pDSfs, Ipp32s len, Ipp32s* pSum) {
655 
656     Ipp16s sfs, order;
657     Ipp16s i, j;
658     sfs = pSrcSfs[0];
659     for(i=1; i<len; i++) {
660         if(pSrcSfs[i] < sfs)
661             sfs = pSrcSfs[i];
662     }
663     for(j=0; j<LPF_DIM+1; j++) {
664         pSum[j] = 0;
665     }
666     sfs += 14;
667     for(i=0; i<len; i++) {
668         order = (Ipp16s)(sfs - pSrcSfs[i]);
669         if(order<0) {
670             for(j=0; j<LPF_DIM+1; j++) {
671                 pSum[j] += pSrc[(LPF_DIM+1)*i+j] >> -order;
672             }
673         } else {
674             for(j=0; j<LPF_DIM+1; j++) {
675                 pSum[j] += pSrc[(LPF_DIM+1)*i+j] << order;
676             }
677         }
678     }
679     order = Norm_32s_I(&pSum[0]);
680     pDst[0] = (Ipp16s)(pSum[0]>>16);
681     for(i=1; i<=LPF_DIM; i++) {
682         pDst[i] = (Ipp16s)((pSum[i]<<order)>>16);
683     }
684     order -= 16;
685     *pDSfs = (Ipp16s)(sfs + order);
686 
687 }
688 
VADMusicDetection(G729Codec_Type codecType,Ipp32s autoCorr,Ipp16s expAutoCorr,Ipp16s * pRc,Ipp16s * lags,Ipp16s * pgains,Ipp16s stat_flg,Ipp16s * Vad,Ipp8s * pVADmem)689 void VADMusicDetection(G729Codec_Type codecType,Ipp32s autoCorr,Ipp16s expAutoCorr,Ipp16s *pRc,
690                        Ipp16s *lags, Ipp16s *pgains,Ipp16s stat_flg,Ipp16s *Vad,Ipp8s* pVADmem) {
691     Ipp16s i, j, exp, fractal, tmp, tmp1, tmp2, tmp3, flag1, flag2, flag,
692             pderr=IPP_MAX_16S, logE , spectrDist, threshold;
693     Ipp32s   L_tmp;
694     VADmemory_Obj *vadMem = (VADmemory_Obj*)pVADmem;
695     for(i=0; i<4; i++) {
696         j = (Ipp16s)((pRc[i] * pRc[i])>>15);
697         j = (Ipp16s)(IPP_MAX_16S - j);
698         pderr = (Ipp16s)((pderr * j)>>15);
699     }
700     L_tmp = 2*(autoCorr>>16)*pderr + 2*((((autoCorr>>1)&0x7fff)*pderr)>>15);
701     Log2_G729(L_tmp, &exp, &fractal);
702     L_tmp = 9864*exp + ((fractal*9864)>>15);
703     i = (Ipp16s)(expAutoCorr - 2);
704     L_tmp += 9864 * i;
705     L_tmp = ShiftL_32s(L_tmp,12);
706     logE = (Ipp16s)((L_tmp>>16) - 4875);
707     L_tmp = 0;
708     for(i=0; i<10; i++) {
709         j = (Ipp16s)(vadMem->musicRC[i] - pRc[i]);
710         L_tmp += j * j;
711     }
712     if(L_tmp > IPP_MAX_32S/2)
713         spectrDist = IPP_MAX_32S >> 16;
714     else
715         spectrDist = (Ipp16s)(L_tmp>>15);
716     if( *Vad == 0 ) {
717         for(i=0; i<10; i++) {
718             L_tmp = COEFF1 * vadMem->musicRC[i];
719             L_tmp += PITCH_SHARP_MIN * pRc[i];
720             vadMem->musicRC[i] = (Ipp16s)(L_tmp>>15);
721         }
722         L_tmp = COEFF1 * vadMem->musicSEMean;
723         L_tmp += PITCH_SHARP_MIN * logE;
724         vadMem->musicSEMean = (Ipp16s)(L_tmp>>15);
725     }
726     /* flag determination */
727     tmp3=0; L_tmp=0;
728     for(i=0; i<5; i++) {
729         L_tmp += pgains[i] * 6554;
730         tmp3 = (Ipp16s)(tmp3 + lags[i]);
731     }
732     L_tmp = (L_tmp>>15) * 6554;
733     L_tmp += vadMem->MeanPgain * 26214;
734     vadMem->MeanPgain = (Ipp16s)(L_tmp>>15);
735     L_tmp = 0;
736     for(i=0; i<5; i++) {
737         j = (Ipp16s)(lags[i] << 2);
738         j = (Ipp16s)(j + lags[i]);
739         j = (Ipp16s)(j - tmp3);
740         L_tmp += j * j;
741     }
742     if(L_tmp > 256)
743         tmp2 = IPP_MAX_32S>>16;
744     else
745         tmp2 = (Ipp16s)(L_tmp<<7);
746     if( codecType == G729D_CODEC)
747         threshold = 11960;
748     else
749         threshold = 10322;
750     if(vadMem->MeanPgain > threshold)
751         flag2 =1;
752     else
753         flag2 =0;
754     if( (tmp2 < 21632)  && (vadMem->MeanPgain > 7373))
755         flag1 =1;
756     else
757         flag1 =0;
758     flag= (Ipp16s)(((vadMem->VADPrev & (flag1 | flag2))| (flag2)));
759     if( (pRc[1] <= 14746) && (pRc[1] >= 0) && (vadMem->MeanPgain < BWF_HARMONIC_E))
760         vadMem->conscCounterFlagR++;
761     else
762         vadMem->conscCounterFlagR =0;
763     if( (stat_flg == 1) && (*Vad == 1))
764         vadMem->musicCounter += 256;
765     if( (vadMem->frameCounter & 0x003f) == 0) {
766         if( vadMem->frameCounter == 64)
767             vadMem->musicMCounter = vadMem->musicCounter;
768         else {
769             L_tmp = COEFF1 * vadMem->musicMCounter;
770             L_tmp += PITCH_SHARP_MIN * vadMem->musicCounter;
771             vadMem->musicMCounter = (Ipp16s)(L_tmp>>15);
772         }
773     }
774     if( vadMem->musicCounter == 0)
775         vadMem->conscCounter++;
776     else
777         vadMem->conscCounter = 0;
778     if(  ((vadMem->conscCounter > 500) || (vadMem->conscCounterFlagR > 150))) vadMem->musicMCounter = 0;
779 
780     if( (vadMem->frameCounter & 0x003f) == 0) {
781         vadMem->musicCounter = 0;
782     }
783 
784     if( flag== 1 )
785         vadMem->count_pflag += 256;
786 
787     if( (vadMem->frameCounter & 0x003f) == 0) {
788         if( vadMem->frameCounter == 64)
789             vadMem->Mcount_pflag = vadMem->count_pflag;
790         else {
791             if(vadMem->count_pflag > 6400) {
792                 tmp1 = N0_98;
793                 tmp = 655;
794             } else
795                 if(vadMem->count_pflag > 5120) {
796                 tmp1 = 31130;
797                 tmp = 1638;
798             } else {
799                 tmp1 = COEFF1;
800                 tmp = PITCH_SHARP_MIN;
801             }
802             L_tmp = tmp1 * vadMem->Mcount_pflag;
803             L_tmp += tmp * vadMem->count_pflag;
804             vadMem->Mcount_pflag = (Ipp16s)(L_tmp>>15);
805         }
806     }
807     if( vadMem->count_pflag == 0)
808         vadMem->conscCounterFlagP++;
809     else
810         vadMem->conscCounterFlagP = 0;
811 
812     if(((vadMem->conscCounterFlagP > 100) || (vadMem->conscCounterFlagR > 150))) vadMem->Mcount_pflag = 0;
813 
814 
815     if( (vadMem->frameCounter & 0x003f) == 0)
816         vadMem->count_pflag = 0;
817 
818     if(codecType == G729E_CODEC) {
819         if( (spectrDist > 4915) && (logE > (vadMem->musicSEMean + 819)) && (vadMem->VADPrevEnergy > 10240) )
820             *Vad =1;
821         else if( ((spectrDist > 12452) || (logE > (vadMem->musicSEMean + 819))) &&
822                  (vadMem->VADPrevEnergy > 10240) )
823             *Vad =1;
824         else if( ( (vadMem->Mcount_pflag > 2560) || (vadMem->musicMCounter > 280) || (vadMem->frameCounter < 64))
825                  && (vadMem->VADPrevEnergy > 1433))
826             *Vad =1;
827     }
828     return;
829 }
SynthesisFilterOvf_G729_16s_I(const Ipp16s * pLPC,Ipp16s * pSrcDst,Ipp32s len,Ipp8s * pMemUpdated,Ipp32s histLen)830 void SynthesisFilterOvf_G729_16s_I(const Ipp16s * pLPC, Ipp16s * pSrcDst,
831                                    Ipp32s len, Ipp8s * pMemUpdated,Ipp32s histLen) {
832     Ipp32s nTaps;
833     SynthesisFilterState *SynFltSt;
834 
835     SynFltSt=(SynthesisFilterState*)pMemUpdated;
836     nTaps = SynFltSt->nTaps;
837     if(!histLen)
838         ippsSynthesisFilter_G729E_16s_I(pLPC,30, pSrcDst, len, SynFltSt->buffer);
839     else
840         ippsSynthesisFilterLow_NR_16s_ISfs(pLPC,pSrcDst, len, 12, SynFltSt->buffer+histLen);
841     ippsCopy_16s((pSrcDst+len-nTaps), SynFltSt->buffer, nTaps);
842 }
843 
SynthesisFilter_G729_16s(const Ipp16s * pLPC,const Ipp16s * pSrc,Ipp16s * pDst,Ipp32s len,Ipp8s * pMemUpdated,Ipp32s histLen)844 IppStatus SynthesisFilter_G729_16s (const Ipp16s * pLPC, const Ipp16s * pSrc,
845                                     Ipp16s * pDst, Ipp32s len, Ipp8s * pMemUpdated,Ipp32s histLen) {
846     Ipp32s nTaps;
847     SynthesisFilterState *SynFltSt;
848     IppStatus sts=ippStsNoErr;
849     SynFltSt=(SynthesisFilterState*)pMemUpdated;
850     nTaps = SynFltSt->nTaps;
851     if(histLen==0)
852         ippsSynthesisFilter_G729E_16s(pLPC, 30,pSrc, pDst, len, SynFltSt->buffer);
853     else
854         sts = ippsSynthesisFilter_NR_16s_Sfs(pLPC,pSrc,pDst, len, 12, SynFltSt->buffer+histLen);
855 
856     if(sts != ippStsOverflow)
857         ippsCopy_16s((pDst+len-nTaps), SynFltSt->buffer, nTaps);
858     return sts;
859 }
860 
SynthesisFilter_G729_16s_update(const Ipp16s * pLPC,const Ipp16s * pSrc,Ipp16s * pDst,Ipp32s len,Ipp8s * pMemUpdated,Ipp32s histLen,Ipp32s update)861 IppStatus SynthesisFilter_G729_16s_update (const Ipp16s * pLPC, const Ipp16s * pSrc,
862                                            Ipp16s * pDst, Ipp32s len, Ipp8s * pMemUpdated,Ipp32s histLen, Ipp32s update) {
863     Ipp32s nTaps;
864     SynthesisFilterState *SynFltSt;
865     IppStatus sts=ippStsNoErr;
866 
867     SynFltSt=(SynthesisFilterState*)pMemUpdated;
868     nTaps = SynFltSt->nTaps;
869     if(histLen==0) ippsSynthesisFilter_G729E_16s(pLPC, 30,pSrc, pDst, len, SynFltSt->buffer);
870     else
871         sts = ippsSynthesisFilter_NR_16s_Sfs(pLPC,pSrc,pDst, len, 12, SynFltSt->buffer+histLen);
872     if((sts != ippStsOverflow)&&(update==1))
873         ippsCopy_16s((pDst+len-nTaps), SynFltSt->buffer, nTaps);
874     return sts;
875 }
876 
SynthesisFilterOvf_G729_16s(const Ipp16s * pLPC,const Ipp16s * pSrc,Ipp16s * pDst,Ipp32s len,Ipp8s * pMemUpdated,Ipp32s histLen)877 void SynthesisFilterOvf_G729_16s (const Ipp16s * pLPC, const Ipp16s * pSrc,
878                                   Ipp16s * pDst, Ipp32s len, Ipp8s * pMemUpdated,Ipp32s histLen) {
879     Ipp32s nTaps;
880     SynthesisFilterState *SynFltSt;
881     SynFltSt=(SynthesisFilterState*)pMemUpdated;
882     nTaps = SynFltSt->nTaps;
883     if(histLen==0) ippsSynthesisFilter_G729E_16s(pLPC, 30, pSrc, pDst, len, SynFltSt->buffer);
884     else
885         ippsSynthesisFilter_NR_16s_Sfs(pLPC,pSrc,pDst, len, 12, SynFltSt->buffer+histLen);
886     ippsCopy_16s((pDst+len-nTaps), SynFltSt->buffer, nTaps);
887 }
888 
SynthesisFilterSize_G729(Ipp32s * pSize)889 void SynthesisFilterSize_G729 (Ipp32s *pSize) {
890     *pSize = sizeof(SynthesisFilterState)+BWLPCF_DIM*sizeof(Ipp16s);
891     *pSize += 32;
892 
893 }
894 
SynthesisFilterInit_G729(Ipp8s * pMemUpdated)895 void SynthesisFilterInit_G729 (Ipp8s * pMemUpdated) {
896     SynthesisFilterState *SynFltSt;
897     Ipp32s size;
898     SynFltSt=(SynthesisFilterState*)pMemUpdated;
899     size = sizeof(SynthesisFilterState);
900     SynFltSt->buffer = (Ipp16s*)((Ipp8s*)SynFltSt + size);
901     //SynFltSt->buffer = (Ipp16s*)(((((Ipp8s*)SynFltSt->buffer + 7) - (Ipp8s*)NULL)&~7));
902     SynFltSt->buffer = IPP_ALIGNED_PTR(SynFltSt->buffer, 32);
903     SynFltSt->nTaps = BWLPCF_DIM;
904     ippsZero_16s(SynFltSt->buffer,BWLPCF_DIM);
905 }
CodewordImpConv_G729(Ipp32s index,const Ipp16s * pSrc1,const Ipp16s * pSrc2,Ipp16s * pDst)906 void CodewordImpConv_G729(Ipp32s index, const Ipp16s *pSrc1,const Ipp16s *pSrc2,Ipp16s *pDst) {
907     Ipp32s i;
908     Ipp32s sign0, sign1, sign2, sign3;     /* 1, -1 */
909     Ipp32s idx0, idx1, idx2, idx3;         /* position*/
910 
911     idx0 = index & 0x7;
912     idx1 = (index>>3) & 0x7;
913     idx2 = (index>>6) & 0x7;
914     idx3 = index>>9;
915 
916     idx0 = (idx0<<2)+idx0;
917     idx1 = (idx1<<2)+idx1+1;
918     idx2 = (idx2<<2)+idx2+2;
919     idx3 = ((idx3>>1)<<2)+(idx3>>1)+(idx3&1)+3;
920 
921     if(idx0>idx1) {
922         i=idx0; idx0=idx1; idx1=i;
923     }
924     if(idx2>idx3) {
925         i=idx2; idx2=idx3; idx3=i;
926     }
927     if(idx0>idx2) {
928         i=idx0; idx0=idx2; idx2=i;
929     }
930     if(idx1>idx3) {
931         i=idx1; idx1=idx3; idx3=i;
932     }
933     if(idx1>idx2) {
934         i=idx1; idx1=idx2; idx2=i;
935     }
936 
937     sign0 = ((pSrc1[idx0] >> 15)<<1)+1;
938     sign1 = ((pSrc1[idx1] >> 15)<<1)+1;
939     sign2 = ((pSrc1[idx2] >> 15)<<1)+1;
940     sign3 = ((pSrc1[idx3] >> 15)<<1)+1;
941     for(i=0; i<idx0; i++)
942         pDst[i]=0;
943     for(; i<idx1; i++)
944         pDst[i]=(Ipp16s)(sign0*pSrc2[i-idx0]);
945     for(; i<idx2; i++)
946         pDst[i]=Cnvrt_32s16s(sign0*pSrc2[i-idx0]+sign1*pSrc2[i-idx1]);
947     for(; i<idx3; i++)
948         pDst[i]=Cnvrt_32s16s(sign0*pSrc2[i-idx0]+sign1*pSrc2[i-idx1]+sign2*pSrc2[i-idx2]);
949     for(; i<LP_SUBFRAME_DIM; i++)
950         pDst[i]=Cnvrt_32s16s(sign0*pSrc2[i-idx0]+sign1*pSrc2[i-idx1]+sign2*pSrc2[i-idx2]+sign3*pSrc2[i-idx3]);
951 
952 }
953 /*=============================================================
954 // pSrc - reflection coefficients
955 // pDst - log area ratio coefficients
956 */
_ippsRCToLAR_G729_16s(const Ipp16s * pSrc,Ipp16s * pDst,Ipp32s len)957 void _ippsRCToLAR_G729_16s (const Ipp16s* pSrc, Ipp16s* pDst, Ipp32s len) {
958     Ipp16s refC, Ax;
959     Ipp32s   i, L_tmp, L_Bx;
960     for(i=0; i<len; i++) {
961         refC = Abs_16s(pSrc[i]);
962         refC >>= 4;
963         if(refC <= 1299  /* 0.6341 */) {
964             pDst[i] = refC;
965         } else {
966             if(refC <= 1815 /* 0.8864 */) {
967                 Ax = 4567;
968                 L_Bx = 3271557L/* 0.78 */;
969             } else if(refC <= 1944 /* 0.9490 */) {
970                 Ax = 11776               /* 5.75 */;
971                 L_Bx = 16357786L           /* 3.90 */;
972             } else {
973                 Ax = 27443               /* 13.40 */;
974                 L_Bx = 46808433L           /* 11.16 */;
975             }
976             refC >>= 1;
977             L_tmp = (refC * Ax)<<1;
978             L_tmp=Sub_32s(L_tmp,L_Bx);
979             pDst[i] = (Ipp16s)(L_tmp >> 11);
980         }
981         if(pSrc[i] < 0) {
982             pDst[i]=Cnvrt_32s16s(-pDst[i]);
983         }
984     }
985 }
986 /* ///////////////////////////////////////////////////////////////////////////////////////
987 //  Name:    ownMakeDecision
988 //  Purpose:
989 //  Parameters:
990 //  bwf1    - banwidth expansion parameter
991 //  bwf2    - bandwidth expansion parameter
992 //  intLSF  - interpolated LSF
993 //  newLSF  - new LSF vector for 2nd subframe
994 //  pRc     - reflection coefficients
995 */
_ippsPWGammaFactor_G729_16s(const Ipp16s * pLAR,const Ipp16s * pLSF,Ipp16s * flat,Ipp16s * pGamma1,Ipp16s * pGamma2,Ipp16s * pMem)996 void _ippsPWGammaFactor_G729_16s ( const Ipp16s *pLAR, const Ipp16s *pLSF,
997                                    Ipp16s *flat, Ipp16s *pGamma1, Ipp16s *pGamma2, Ipp16s *pMem ) {
998 
999     Ipp16s  lar0, lar1, bwf1,bwf2;
1000     Ipp32s  i, L_tmp, L_min;
1001 
1002     lar0 = pLAR[0];
1003     lar1 = pLAR[1];
1004 
1005     if(*flat != 0) {
1006         if((lar0 < -3562 /* -1.74 */)&&(lar1 > 1336 /* 0.65 */)) {
1007             *flat = 0;
1008         }
1009     } else {
1010         if((lar0 > -3116 /* -1.52 */) || (lar1 < 890 /* 0.43 */) ) {
1011             *flat = 1;
1012         }
1013     }
1014 
1015     if(*flat == 0) {
1016         ippsLShiftC_16s(pLSF,1,pMem,LPF_DIM);
1017         bwf1 = N0_98 /* 0.98 */;
1018         L_min = pMem[1] - pMem[0];
1019         for(i=1; i<LPF_DIM-1; i++) {
1020             L_tmp = pMem[i+1] - pMem[i];
1021             if(L_tmp < L_min) {
1022                 L_min = L_tmp;
1023             }
1024         }
1025 
1026         L_tmp = SIX_PI * L_min;
1027         L_tmp >>= 15;
1028         L_tmp = 1024 /* 1 */ - L_tmp;
1029         L_tmp = L_tmp << 5;
1030         if(L_tmp > BWF2 /* 0.70 */) {
1031             bwf2 = BWF2;
1032         } else if(L_tmp < 13107 /* 0.40 */) {
1033             bwf2 = 13107;
1034         } else {
1035             bwf2 = (Ipp16s)L_tmp;
1036         }
1037     } else {
1038         bwf1 = 30802 /* 0.94 */;
1039         bwf2 = 19661 /* 0.60 */;
1040     }
1041     *pGamma1 = bwf1;
1042     *pGamma2 = bwf2;
1043 
1044 }
1045 
PitchTracking_G729E(Ipp16s * delay,Ipp16s * delay_frac,Ipp16s * prevDelay,Ipp16s * stat_N,Ipp16s * pitchStatDelayt,Ipp16s * pitchStatFractiont)1046 void PitchTracking_G729E( Ipp16s *delay,Ipp16s *delay_frac, Ipp16s *prevDelay, Ipp16s *stat_N,
1047                           Ipp16s *pitchStatDelayt, Ipp16s *pitchStatFractiont){
1048     Ipp16s dist, dist_min, pitch_mult;
1049     Ipp16s i, is_mult;
1050 
1051     dist = (Ipp16s)(delay[0] - prevDelay[0]);
1052     if(dist < 0) {
1053         dist = (Ipp16s)(-dist);
1054         is_mult = 0;
1055     } else {
1056         is_mult = 1;
1057     }
1058     if(dist < 5) {
1059         *stat_N += 1;
1060         if(*stat_N > 7) *stat_N = 7 ;
1061         *pitchStatDelayt = delay[0];
1062         *pitchStatFractiont = delay_frac[0];
1063     } else {
1064 
1065         dist_min =  dist;
1066         if( is_mult == 0) {
1067             pitch_mult = (Ipp16s)(delay[0] + delay[0]);
1068             for(i=2; i<5; i++) {
1069                 dist = Abs_16s((Ipp16s)(pitch_mult - prevDelay[0]));
1070                 if(dist <= dist_min) dist_min = dist;
1071                 pitch_mult = (Ipp16s)(pitch_mult + delay[0]);
1072             }
1073         } else {
1074             pitch_mult = (Ipp16s)(prevDelay[0] + prevDelay[0]);
1075             for(i=2; i<5; i++) {
1076                 dist = Abs_16s((Ipp16s)(pitch_mult - delay[0]));
1077                 if(dist <= dist_min) dist_min = dist;
1078                 pitch_mult = (Ipp16s)(pitch_mult + prevDelay[0]);
1079             }
1080         }
1081         if(dist_min < 5) {
1082             if(*stat_N > 0) {
1083                 delay[0] = *pitchStatDelayt;
1084                 delay_frac[0] = *pitchStatFractiont;
1085             }
1086             *stat_N -= 1;
1087             if(*stat_N < 0) *stat_N = 0 ;
1088         } else {
1089             *stat_N = 0;
1090             *pitchStatDelayt = delay[0];
1091             *pitchStatFractiont = delay_frac[0];
1092         }
1093     }
1094     prevDelay[0] = delay[0];
1095     return;
1096 }
1097 
stat(Ipp16s prevB,Ipp16s prevF,Ipp16s mode,Ipp16s prevMode,Ipp16s * statGlobal,Ipp16s * BWDFrameCounter,Ipp16s * val_BWDFrameCounter)1098 static void stat( Ipp16s prevB, Ipp16s prevF, Ipp16s mode, Ipp16s prevMode,
1099                        Ipp16s *statGlobal, Ipp16s *BWDFrameCounter, Ipp16s *val_BWDFrameCounter  ) {
1100     Ipp32s  i;
1101     if(mode == 1) {
1102         *BWDFrameCounter += 1;
1103         *val_BWDFrameCounter += 250;
1104         if(*BWDFrameCounter == 20)
1105             statGlobal[0] = Add_16s(statGlobal[0], 2500);
1106         else
1107             if(*BWDFrameCounter > 20)
1108             statGlobal[0] += 500;
1109     }else
1110         if((mode == 0)&&(prevMode == 1)) {
1111             if(*BWDFrameCounter < 20) {
1112                 statGlobal[0] = (Ipp16s)(statGlobal[0] - 5000 + *val_BWDFrameCounter);
1113             }
1114         *BWDFrameCounter = *val_BWDFrameCounter = 0;
1115         }
1116     if(statGlobal[0] < 13000) {
1117         for(i=1; i<6;i++)
1118             if(prevB > prevF + gThrs[i]) {
1119                 statGlobal[0] = (Ipp16s)(statGlobal[0] + gStats[i]);
1120                 break;
1121             }
1122     }
1123     for(i=0; i<5;i++)
1124         if(prevB < prevF - gThrs[i]) {
1125             statGlobal[0] = (Ipp16s)(statGlobal[0] - gStats1[i]);
1126             break;
1127         }
1128     if( statGlobal[0] > 32000)
1129         statGlobal[0] = 32000;
1130     else
1131         if(statGlobal[0] < 0)
1132             statGlobal[0] = 0;
1133     return;
1134 }
1135 
SetLPCMode_G729E(Ipp16s * pSignal,Ipp16s * fwdA,Ipp16s * pBwdLPC,Ipp16s * mode,Ipp16s * newLSP,Ipp16s * oldLSP,G729Encoder_Obj * encoderObj)1136 void SetLPCMode_G729E ( Ipp16s *pSignal,  Ipp16s *fwdA, Ipp16s *pBwdLPC, Ipp16s *mode,
1137                         Ipp16s *newLSP, Ipp16s *oldLSP, G729Encoder_Obj *encoderObj){
1138     Ipp16s i,gap,prevF,prevB,gpredB,temp;
1139     Ipp32s   L_threshLPC,LSPdist,sigEnergy;
1140     LOCAL_ALIGN_ARRAY(32, Ipp16s, res, LP_FRAME_DIM, encoderObj);
1141     sigEnergy = enerDB(pSignal, LP_FRAME_DIM);
1142     ippsResidualFilter_G729E_16s(&pBwdLPC[BWLPCF1_DIM], BWLPCF_DIM,pSignal, res, LP_FRAME_DIM);
1143     prevB = (Ipp16s)(sigEnergy - enerDB(res, LP_FRAME_DIM));
1144     encoderObj->interpCoeff2_2 -= 410;
1145     if( encoderObj->interpCoeff2_2 < 0) encoderObj->interpCoeff2_2 = 0;
1146     temp = (Ipp16s)((1<<12) - encoderObj->interpCoeff2_2);
1147     ippsInterpolateC_G729_16s_Sfs(pBwdLPC + BWLPCF1_DIM, temp,
1148                                   encoderObj->pPrevFilt, encoderObj->interpCoeff2_2, pBwdLPC + BWLPCF1_DIM, BWLPCF1_DIM, 12);
1149     ippsInterpolate_G729_16s
1150     (pBwdLPC + BWLPCF1_DIM, encoderObj->pPrevFilt, pBwdLPC, BWLPCF1_DIM);
1151     ippsResidualFilter_G729E_16s(pBwdLPC, BWLPCF_DIM,pSignal, res, LP_SUBFRAME_DIM);
1152     ippsResidualFilter_G729E_16s(&pBwdLPC[BWLPCF1_DIM], BWLPCF_DIM,&pSignal[LP_SUBFRAME_DIM], &res[LP_SUBFRAME_DIM], LP_SUBFRAME_DIM);
1153     gpredB = (Ipp16s)(sigEnergy - enerDB(res, LP_FRAME_DIM));
1154     ippsResidualFilter_G729E_16s(fwdA,LPF_DIM, pSignal, res, LP_SUBFRAME_DIM);
1155     ippsResidualFilter_G729E_16s(&fwdA[LPF_DIM+1], LPF_DIM,&pSignal[LP_SUBFRAME_DIM], &res[LP_SUBFRAME_DIM], LP_SUBFRAME_DIM);
1156     prevF = (Ipp16s)(sigEnergy - enerDB(res, LP_FRAME_DIM));
1157     temp = (Ipp16s)(encoderObj->statGlobal>>7);
1158     temp = (Ipp16s)(temp * 3);
1159     gap = (Ipp16s)(temp + 205);
1160     if( (gpredB > prevF - gap) &&
1161         (prevB > prevF - gap) && (prevB > 0) && (gpredB > 0) )
1162         mode[0] = 1;
1163 
1164     else
1165         mode[0] = 0;
1166     for(LSPdist=i=0; i<LPF_DIM; i++) {
1167         temp = (Ipp16s)(oldLSP[i] - newLSP[i]);
1168         LSPdist += temp * temp;
1169     }
1170     if(encoderObj->statGlobal < 13000)
1171         mode[0] = 0;
1172     if(encoderObj->statGlobal < 32000) {
1173         L_threshLPC = 0;
1174     } else {
1175         L_threshLPC = 32212255;
1176     }
1177     if((LSPdist < L_threshLPC)&&(mode[0] == 0)&&(encoderObj->prevLPmode == 1)&&(prevB > 0)&&(gpredB > 0)) {
1178         mode[0] = 1;
1179     }
1180     if(sigEnergy < BWF_HARMONIC_E) {
1181         mode[0] = 0;
1182         if(encoderObj->statGlobal > 13000)
1183             encoderObj->statGlobal = 13000;
1184     } else {
1185         tstDominantBWDmode(&encoderObj->BWDcounter2,&encoderObj->FWDcounter2,&encoderObj->dominantBWDmode, mode[0]);
1186 
1187         stat(prevB, prevF, mode[0], encoderObj->prevLPmode,
1188                   &encoderObj->statGlobal, &encoderObj->BWDFrameCounter, &encoderObj->val_BWDFrameCounter);
1189     }
1190     if(mode[0] == 0)
1191         encoderObj->interpCoeff2_2 = 4506;
1192     LOCAL_ALIGN_ARRAY_FREE(32, Ipp16s, res, LP_FRAME_DIM, encoderObj);
1193     return;
1194 }
1195 
tstDominantBWDmode(Ipp16s * BWDcounter2,Ipp16s * FWDcounter2,Ipp16s * dominantBWDmode,Ipp16s mode)1196 void tstDominantBWDmode(   Ipp16s *BWDcounter2,   Ipp16s *FWDcounter2,   Ipp16s *dominantBWDmode,  Ipp16s mode) {
1197     Ipp16s tmp, count_all;
1198     if(mode == 0)
1199         *FWDcounter2+=1;
1200     else *BWDcounter2+=1;
1201     count_all = (Ipp16s)(*BWDcounter2 + *FWDcounter2);
1202     if(count_all == 100) {
1203         count_all >>= 1;
1204         *BWDcounter2 >>= 1;
1205         *FWDcounter2 >>= 1;
1206     }
1207     *dominantBWDmode = 0; if(count_all >= 10) {
1208         tmp = (Ipp16s)(*FWDcounter2<<2);
1209         if(*BWDcounter2 > tmp) *dominantBWDmode = 1;
1210     }
1211     return;
1212 }
1213 
enerDB(Ipp16s * synth,Ipp16s L)1214 Ipp16s enerDB(Ipp16s *synth, Ipp16s L) {
1215 
1216     Ipp16s i,energyLog,index;
1217     Ipp32s L_energy;
1218     ippsDotProd_16s32s_Sfs(synth,synth,L,&L_energy,0);
1219     if(L_energy > (IPP_MAX_32S>>1))
1220         L_energy = (IPP_MAX_32S>>1);
1221     for(i = 0; L_energy > 32; i++)
1222         L_energy >>= 1;
1223     index = (Ipp16s)(L_energy - (1<<4));
1224     energyLog = 617;
1225     if(index < 0)
1226         energyLog = 1;
1227     else {
1228         if(i > 1)
1229             energyLog = (Ipp16s)(i*617);
1230         energyLog = (Ipp16s)(energyLog + energyLogTable[index]);
1231     }
1232     return(energyLog);
1233 }
1234 
NormBy0_32s(Ipp32s * src,Ipp32s * dst,Ipp32s len)1235 static void NormBy0_32s(Ipp32s *src, Ipp32s *dst,Ipp32s len) {
1236     Ipp32s i, sum, norm;
1237     sum = src[0] + 1;
1238     norm = Norm_32s_I(&sum);
1239     dst[0] = sum;
1240 
1241     for(i = 1; i <len; i++) {
1242         sum = src[i] + 1;
1243         dst[i] = sum << norm;
1244     }
1245     return;
1246 }
1247 
BWDLagWindow(Ipp32s * pSrc,Ipp32s * pDst)1248 void BWDLagWindow(Ipp32s *pSrc, Ipp32s *pDst) {
1249     Ipp16s i;
1250     for(i=0; i<BWLPCF_DIM; i++) {
1251         pSrc[i+1] = Mul_32s(pSrc[i+1]>>1,wlag_bwd[i]>>1);
1252     }
1253     NormBy0_32s(pSrc,pDst,BWLPCF_DIM+1);
1254 }
1255 
ExtractBitsG729(const Ipp8u ** pBits,Ipp32s * nBit,Ipp32s Count)1256 Ipp16s ExtractBitsG729( const Ipp8u **pBits, Ipp32s *nBit, Ipp32s Count) {
1257     Ipp32s i;
1258     Ipp16s temp, bits = 0;
1259     for( i = 0 ; i < Count ; i ++ ) {
1260         temp   = (Ipp16s)(((pBits[0])[(i + nBit[0])>>3] >> (7 - ((i + nBit[0]) & 7)) ) & 1);
1261         bits <<= 1 ;
1262         bits  = (Ipp16s)(bits + temp);
1263     }
1264     pBits[0] += (Count + nBit[0])>>3;
1265     nBit[0]   = (Count + nBit[0]) & 7;
1266     return bits;
1267 }
1268 
1269