-
Notifications
You must be signed in to change notification settings - Fork 7
/
encg729fp.c
1475 lines (1259 loc) · 67.1 KB
/
encg729fp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*/////////////////////////////////////////////////////////////////////////////
//
// INTEL CORPORATION PROPRIETARY INFORMATION
// This software is supplied under the terms of a license agreement or
// nondisclosure agreement with Intel Corporation and may not be copied
// or disclosed except in accordance with the terms of that agreement.
// Copyright(c) 2004-2007 Intel Corporation. All Rights Reserved.
//
// Intel(R) Integrated Performance Primitives
// USC - Unified Speech Codec interface library
//
// By downloading and installing USC codec, you hereby agree that the
// accompanying Materials are being provided to you under the terms and
// conditions of the End User License Agreement for the Intel(R) Integrated
// Performance Primitives product previously accepted by you. Please refer
// to the file ippEULA.rtf or ippEULA.txt located in the root directory of your Intel(R) IPP
// product installation for more information.
//
// A speech coding standards promoted by ITU, ETSI, 3GPP and other
// organizations. Implementations of these standards, or the standard enabled
// platforms may require licenses from various entities, including
// Intel Corporation.
//
//
// Purpose: G.729 floating-point speech codec: encode API functions.
//
*/
#include <math.h>
#include "vadg729fp.h"
#include "owng729fp.h"
__ALIGN32 CONST Ipp32f InitLSP[LPC_ORDER] =
{ 0.9595f, 0.8413f, 0.6549f, 0.4154f, 0.1423f,
-0.1423f, -0.4154f, -0.6549f, -0.8413f, -0.9595f
};
__ALIGN32 CONST Ipp32f InitFrequences[LPC_ORDER] = { /* previous LSP vector(init) */
0.285599f, 0.571199f, 0.856798f, 1.142397f, 1.427997f,
1.713596f, 1.999195f, 2.284795f, 2.570394f, 2.855993f
}; /* IPP_PI*(j+1)/(LPC_ORDER+1) */
static __ALIGN32 CONST Ipp32f b140[3] = {0.92727435E+00f, -0.18544941E+01f, 0.92727435E+00f};
static __ALIGN32 CONST Ipp32f a140[3] = {1.00000000E+00f, 0.19059465E+01f, -0.91140240E+00f};
static __ALIGN32 CONST Ipp32f lwindow[LPC_ORDER+2] = {
0.99879038f, 0.99546894f, 0.98995779f,
0.98229335f, 0.97252620f, 0.96072035f,
0.94695264f, 0.93131180f, 0.91389754f,
0.89481964f, 0.87419660f, 0.85215437f
};
__ALIGN32 CONST Ipp32f lagBwd[BWD_LPC_ORDER] = {
0.999892f, 0.999869f, 0.999831f, 0.999777f, 0.999707f,
0.999622f, 0.999522f, 0.999407f, 0.999276f, 0.999129f,
0.998968f, 0.998790f, 0.998598f, 0.998390f, 0.998167f,
0.997928f, 0.997674f, 0.997405f, 0.997121f, 0.996821f,
0.996506f, 0.996175f, 0.995830f, 0.995469f, 0.995093f,
0.994702f, 0.994295f, 0.993874f, 0.993437f, 0.992985f,
};
/* Quantization of SID gain */
__ALIGN32 CONST Ipp32f SIDGainTbl[32] = {
0.502f, 1.262f, 2.000f, 3.170f,
5.024f, 7.962f, 12.619f, 15.887f,
20.000f, 25.179f, 31.698f, 39.905f,
50.238f, 63.246f, 79.621f, 100.237f,
126.191f, 158.866f, 200.000f, 251.785f,
316.979f, 399.052f, 502.377f, 632.456f,
796.214f, 1002.374f, 1261.915f, 1588.656f,
2000.000f, 2517.851f, 3169.786f, 3990.525f
};
#define AVG(a,b,c,d) (Ipp32s)( ((a)+(b)+(c)+(d))/4.0f + 0.5f)
static void UpdateVad_I(G729FPEncoder_Obj* encoderObj, Ipp32f *Excitation, Ipp32f *forwardLPC, Ipp32f *WeightedSpeech,
Ipp32f *gamma1, Ipp32f *gamma2, Ipp32f *pSynth,
Ipp32f *pError, Ipp32f *SpeechWnd, Ipp32s* dst,G729Codec_Type codecType);
static void UpdateVad_A(G729FPEncoder_Obj* encoderObj, Ipp32f *Excitation,
Ipp32f *WeightedSpeech, Ipp32f *SpeechWnd, Ipp32s* dst);
static void UpdateCNG(Ipp32f *pSrcAutoCorr, Ipp32s Vad, Ipp8s *cngMem);
static Ipp32s CodecType2Num(G729Codec_Type codecType)
{
switch(codecType) {
case G729D_CODEC:
return 0;
case G729_CODEC:
return 1;
case G729A_CODEC:
return 1;
case G729E_CODEC:
return 2;
}
return -1;
}
static Ipp32s ownEncoderObjSize()
{
Ipp32s codecSize, fltsize;
codecSize = sizeof(G729FPEncoder_Obj);
ippsIIRGetStateSize_32f(2,&fltsize);
codecSize += fltsize;
VADGetSize(&fltsize);
codecSize += fltsize;
CNGGetSize(&fltsize);
codecSize += fltsize;
MSDGetSize(&fltsize);
codecSize += fltsize;
ippsWinHybridGetStateSize_G729E_32f(&fltsize);
codecSize += fltsize;
return codecSize;
}
G729_CODECFUN( APIG729_Status, apiG729FPEncoder_Alloc,
(G729Codec_Type codecType, Ipp32s *pCodecSize))
{
if ((codecType != G729_CODEC)&&(codecType != G729A_CODEC)
&&(codecType != G729D_CODEC)&&(codecType != G729E_CODEC)&&(codecType != G729I_CODEC)){
return APIG729_StsBadCodecType;
}
*pCodecSize = ownEncoderObjSize();
return APIG729_StsNoErr;
}
G729_CODECFUN( APIG729_Status, apiG729FPCodec_ScratchMemoryAlloc,(Ipp32s *pCodecSize))
{
if(NULL==pCodecSize)
return APIG729_StsBadArgErr;
*pCodecSize = G729FP_ENCODER_SCRATCH_MEMORY_SIZE;
return APIG729_StsNoErr;
}
G729_CODECFUN( APIG729_Status, apiG729FPEncoder_Mode,
(G729FPEncoder_Obj* encoderObj, G729Encode_Mode mode))
{
if(G729Encode_VAD_Enabled != mode && G729Encode_VAD_Disabled != mode){
return APIG729_StsBadArgErr;
}
encoderObj->objPrm.mode = mode;
return APIG729_StsNoErr;
}
G729_CODECFUN( APIG729_Status, apiG729FPEncoder_InitBuff,
(G729FPEncoder_Obj* encoderObj, Ipp8s *buff))
{
if(buff==NULL) return APIG729_StsBadArgErr;
if(encoderObj==NULL) return APIG729_StsBadArgErr;
if(NULL==encoderObj || NULL==buff)
return APIG729_StsBadArgErr;
encoderObj->Mem.base = buff;
encoderObj->Mem.CurPtr = encoderObj->Mem.base;
encoderObj->Mem.VecPtr = (Ipp32s *)(encoderObj->Mem.base+G729FP_ENCODER_SCRATCH_MEMORY_SIZE);
return APIG729_StsNoErr;
}
G729_CODECFUN( APIG729_Status, apiG729FPEncoder_Init,
(G729FPEncoder_Obj* encoderObj, G729Codec_Type codecType, G729Encode_Mode mode))
{
Ipp32s i;
Ipp32s fltsize;
void* pBuf;
Ipp32f coeff[6];
Ipp8s* oldMemBuff;
if(NULL==encoderObj)
return APIG729_StsBadArgErr;
if ((codecType != G729_CODEC)&&(codecType != G729A_CODEC)
&&(codecType != G729D_CODEC)&&(codecType != G729E_CODEC)&&(codecType != G729I_CODEC)){
return APIG729_StsBadCodecType;
}
oldMemBuff = encoderObj->Mem.base; /* if Reinit */
ippsZero_16s((Ipp16s*)encoderObj,sizeof(G729FPEncoder_Obj)>>1) ;
encoderObj->objPrm.objSize = ownEncoderObjSize();
encoderObj->objPrm.mode = mode;
encoderObj->objPrm.key = ENC_KEY;
encoderObj->objPrm.codecType=codecType;
coeff[0] = b140[0];
coeff[1] = b140[1];
coeff[2] = b140[2];
coeff[3] = a140[0];
coeff[4] = -a140[1];
coeff[5] = -a140[2];
pBuf = (Ipp8s*)encoderObj + sizeof(G729FPEncoder_Obj);
ippsIIRInit_32f(&encoderObj->iirstate,coeff,2,NULL,pBuf);
ippsIIRGetStateSize_32f(2,&fltsize);
encoderObj->vadMem = (Ipp8s *)((Ipp8s*)pBuf + fltsize);
VADGetSize(&fltsize);
encoderObj->cngMem = (Ipp8s *)((Ipp8s*)encoderObj->vadMem + fltsize);
CNGGetSize(&fltsize);
encoderObj->msdMem = (Ipp8s *)((Ipp8s*)encoderObj->cngMem + fltsize);
MSDGetSize(&fltsize);
encoderObj->pHWState = (IppsWinHybridState_G729E_32f *)((Ipp8s*)encoderObj->msdMem + fltsize);
/* Static vectors to zero */
ippsZero_32f(encoderObj->OldSpeechBuffer, SPEECH_BUFF_LEN);
ippsZero_32f(encoderObj->OldExcitationBuffer, PITCH_LAG_MAX+INTERPOL_LEN);
ippsZero_32f(encoderObj->OldWeightedSpeechBuffer, PITCH_LAG_MAX);
ippsZero_32f(encoderObj->WeightedFilterMemory, BWD_LPC_ORDER);
ippsZero_32f(encoderObj->FltMem, BWD_LPC_ORDER);
encoderObj->fBetaPreFilter = PITCH_SHARPMIN;
ippsCopy_32f(InitLSP, encoderObj->OldLSP, LPC_ORDER);
ippsCopy_32f(InitLSP, encoderObj->OldQuantLSP, LPC_ORDER);
for(i=0; i<4; i++) encoderObj->ExcitationError[i] = 1.f;
encoderObj->PastQuantEnergy[0]=encoderObj->PastQuantEnergy[1]=encoderObj->PastQuantEnergy[2]=encoderObj->PastQuantEnergy[3]=-14.0;
for(i=0; i<MOVING_AVER_ORDER; i++)
ippsCopy_32f (&InitFrequences[0], &encoderObj->PrevFreq[i][0], LPC_ORDER );
ippsZero_32f(encoderObj->OldForwardLPC, LPC_ORDERP1);
encoderObj->OldForwardLPC[0]= 1.f;
ippsZero_32f(encoderObj->OldForwardRC, 2);
encoderObj->sFrameCounter = 0;
/* For G.729B */
/* Initialize VAD/DTX parameters */
//if(mode == G729Encode_VAD_Enabled) {
encoderObj->prevVADDec = 1;
encoderObj->prevPrevVADDec = 1;
encoderObj->sCNGSeed = INIT_SEED_VAL;
VADInit(encoderObj->vadMem);
CNGInit(encoderObj->cngMem);
MSDInit(encoderObj->msdMem);
//}
encoderObj->prevLPCMode = 0;
if(codecType==G729A_CODEC) {
ippsZero_32f(encoderObj->ZeroMemory, LPC_ORDER);
} else {
ippsZero_32f(encoderObj->SynFltMemory, BWD_LPC_ORDER);
ippsZero_32f(encoderObj->ErrFltMemory, BWD_LPC_ORDER);
ippsZero_32f(&encoderObj->UnitImpulse[BWD_LPC_ORDERP1], SUBFR_LEN);
ippsZero_32f(encoderObj->PrevFlt, BWD_LPC_ORDERP1);
encoderObj->PrevFlt[0] = 1.f;
ippsWinHybridInit_G729E_32f(encoderObj->pHWState);
ippsZero_32f(encoderObj->SynthBuffer, BWD_ANALISIS_WND_LEN);
ippsZero_32f(encoderObj->BackwardLPCMemory, BWD_LPC_ORDERP1);
encoderObj->BackwardLPCMemory[0] = 1.f;
encoderObj->isBWDDominant = 0;
encoderObj->fInterpolationCoeff = 1.1f; /* Filter interpolation parameter */
encoderObj->sGlobalStatInd = 10000; /* Mesure of global stationnarity */
encoderObj->sBWDStatInd = 0; /* Nbre of consecutive backward frames */
encoderObj->sValBWDStatInd = 0; /* Value associated with stat_bwd */
ippsZero_32f(encoderObj->OldBackwardLPC, BWD_LPC_ORDERP1);
encoderObj->OldBackwardLPC[0]= 1.f;
ippsZero_32f(encoderObj->OldBackwardRC, 2);
ippsSet_32s(20,encoderObj->LagBuffer,5);
ippsSet_32f(0.7f,encoderObj->PitchGainBuffer,5);
encoderObj->sBWDFrmCounter = 0;
encoderObj->sFWDFrmCounter = 0;
encoderObj->isSmooth = 1;
encoderObj->LogAreaRatioCoeff[0] = encoderObj->LogAreaRatioCoeff[1] = 0.f;
encoderObj->sSearchTimes = 30;
}
apiG729FPEncoder_InitBuff(encoderObj,oldMemBuff);
return APIG729_StsNoErr;
}
G729_CODECFUN( APIG729_Status, apiG729FPEncode,
(G729FPEncoder_Obj* encoderObj,const Ipp16s* src, Ipp8u *dst,G729Codec_Type codecType, Ipp32s *frametype))
{
/* LPC analysis */
LOCAL_ALIGN_ARRAY(32, Ipp32f, forwardAutoCorr, (LPC_ORDERP2+1),encoderObj); /* Autocorrelations (forward) */
LOCAL_ALIGN_ARRAY(32, Ipp32f, backwardAutoCorr, BWD_LPC_ORDERP1,encoderObj); /* Autocorrelations (backward) */
LOCAL_ALIGN_ARRAY(32, Ipp32f, backwardReflectCoeff, BWD_LPC_ORDER,encoderObj); /* Reflection coefficients : backward analysis */
LOCAL_ALIGN_ARRAY(32, Ipp32f, forwardLPC, LPC_ORDERP1*2,encoderObj); /* A(z) forward unquantized for the 2 subframes */
LOCAL_ALIGN_ARRAY(32, Ipp32f, forwardQntLPC, LPC_ORDERP1*2,encoderObj); /* A(z) forward quantized for the 2 subframes */
LOCAL_ALIGN_ARRAY(32, Ipp32f, backwardLPC, 2*BWD_LPC_ORDERP1,encoderObj); /* A(z) backward for the 2 subframes */
LOCAL_ALIGN_ARRAY(32, Ipp32f, WeightedLPC1, BWD_LPC_ORDERP1,encoderObj); /* A(z) with spectral expansion */
LOCAL_ALIGN_ARRAY(32, Ipp32f, WeightedLPC2, BWD_LPC_ORDERP1,encoderObj); /* A(z) with spectral expansion */
LOCAL_ALIGN_ARRAY(32, Ipp32f, CurrLSP, LPC_ORDER,encoderObj);
LOCAL_ALIGN_ARRAY(32, Ipp32f, tmpLSP, LPC_ORDER,encoderObj);
LOCAL_ALIGN_ARRAY(32, Ipp32f, CurrQntLSP, LPC_ORDER,encoderObj); /* LSPs at 2th subframe */
LOCAL_ALIGN_ARRAY(32, Ipp32f, TmpAlignVec, WINDOW_LEN,encoderObj);
/* Other vectors */
LOCAL_ALIGN_ARRAY(32, Ipp32f, ImpulseResponse, SUBFR_LEN,encoderObj); /* Impulse response*/
LOCAL_ALIGN_ARRAY(32, Ipp32f, TargetVector, SUBFR_LEN,encoderObj); /* Target vector for pitch search */
LOCAL_ALIGN_ARRAY(32, Ipp32f, FltTargetVector, SUBFR_LEN,encoderObj); /* Target vector for codebook search */
LOCAL_ALIGN_ARRAY(32, Ipp32f, FixedCodebookExc, SUBFR_LEN,encoderObj); /* Fixed codebook excitation */
LOCAL_ALIGN_ARRAY(32, Ipp32f, FltAdaptExc, SUBFR_LEN,encoderObj); /* Filtered adaptive excitation */
LOCAL_ALIGN_ARRAY(32, Ipp32f, FltFixedCodebookExc, SUBFR_LEN,encoderObj); /* Filtered fixed codebook excitation */
LOCAL_ALIGN_ARRAY(32, Ipp32f, PitchPredResidual, SUBFR_LEN,encoderObj); /* Pitch prediction residual */
LOCAL_ARRAY(Ipp32f, forwardReflectCoeff, LPC_ORDER,encoderObj); /* Reflection coefficients : forward analysis */
LOCAL_ARRAY(Ipp32f, InterpolatedLSF, LPC_ORDER,encoderObj); /* Interpolated LSF 1st subframe. */
LOCAL_ARRAY(Ipp32f, CurrLSF, LPC_ORDER,encoderObj);
LOCAL_ARRAY(Ipp32f, CurrFreq, LPC_ORDER,encoderObj);
LOCAL_ARRAY(Ipp32f, TmpAutoCorr, LPC_ORDERP1,encoderObj);
LOCAL_ARRAY(Ipp32s, EncodedParams, 19,encoderObj);
LOCAL_ARRAY(Ipp32f, g_coeff, 5,encoderObj); /* Correlations between TargetVector, FltAdaptExc, & FltFixedCodebookExc:
<FltAdaptExc,FltAdaptExc>, <TargetVector,FltAdaptExc>, <FltFixedCodebookExc,FltFixedCodebookExc>, <TargetVector,FltFixedCodebookExc>,<FltAdaptExc,FltFixedCodebookExc>*/
LOCAL_ARRAY(Ipp32f, gamma1, 2,encoderObj); /* Weighting factor for the 2 subframes */
LOCAL_ARRAY(Ipp32f, gamma2, 2,encoderObj);
LOCAL_ARRAY(Ipp32s, code_lsp, 2,encoderObj);
LOCAL_ARRAY(Ipp32s, delayLine, 2,encoderObj);
Ipp32f *SpeechWnd, *pWindow;
Ipp32f *newSpeech; /* Global variable */
/* Weighted speech vector */
Ipp32f *WeightedSpeech;
/* Excitation vector */
Ipp32f *Excitation;
/* Zero vector */
Ipp32f *pZero=NULL;
Ipp32f *pError=NULL;
Ipp32f *pSynth=NULL;
Ipp32f *pQLPC;
Ipp32f *pUnQLPC;
Ipp32f *pLPC, *pQntLPC;
/* Scalars */
Ipp32s LPCMode; /* LP Backward (1) / Forward (0) Indication mode */
Ipp32s apLen, aqLen;
Ipp32s i, j, NGamma, NSbfr;
Ipp32s openLoopPitch, pitchDelay, minPitchDelay, maxPitchDelay, fracPartPitchDelay;
Ipp32s index, taming;
Ipp32f pitchGain, codeGain;
Ipp32s VADDecision;
/* G.729 ANNEXE variables*/
Ipp32s isSaturateAZ;
Ipp32f EnergydB;
Ipp32s avg_lag;
Ipp32f tmp_lev;
IppStatus sts;
Ipp32s *anau;
if(NULL==src || NULL ==dst)
return APIG729_StsBadArgErr;
if ((codecType != G729_CODEC)&&(codecType != G729A_CODEC)&&(codecType != G729D_CODEC)&&(codecType != G729E_CODEC))
return APIG729_StsBadCodecType;
if(encoderObj->objPrm.objSize <= 0)
return APIG729_StsNotInitialized;
if(ENC_KEY != encoderObj->objPrm.key)
return APIG729_StsBadCodecType;
ippsZero_32f(WeightedLPC1,BWD_LPC_ORDERP1);
ippsZero_32f(WeightedLPC2,BWD_LPC_ORDERP1);
anau = &EncodedParams[0];
newSpeech = encoderObj->OldSpeechBuffer + SPEECH_BUFF_LEN - FRM_LEN;
SpeechWnd = newSpeech - LOOK_AHEAD_LEN; /* Present frame */
pWindow = encoderObj->OldSpeechBuffer + SPEECH_BUFF_LEN - WINDOW_LEN; /* For LPC window */
WeightedSpeech = encoderObj->OldWeightedSpeechBuffer + PITCH_LAG_MAX;
Excitation = encoderObj->OldExcitationBuffer + PITCH_LAG_MAX + INTERPOL_LEN;
if(codecType!=G729A_CODEC){
pZero = encoderObj->UnitImpulse + BWD_LPC_ORDERP1;
pError = encoderObj->ErrFltMemory + BWD_LPC_ORDER;
pSynth = encoderObj->SynthBuffer + BWD_SYNTH_MEM;
}
if (encoderObj->sFrameCounter == 32767) encoderObj->sFrameCounter = 256;
else encoderObj->sFrameCounter++;
ippsConvert_16s32f(src,newSpeech,FRM_LEN);
ippsIIR_32f_I(newSpeech,FRM_LEN,encoderObj->iirstate);
#ifdef CLIPPING_DENORMAL_MODE
ippsIIRGetDlyLine_32f(encoderObj->iirstate, TmpAlignVec);
CLIP_DENORMAL_I(TmpAlignVec[0]);
CLIP_DENORMAL_I(TmpAlignVec[1]);
ippsIIRSetDlyLine_32f(encoderObj->iirstate, TmpAlignVec);
#endif
ownAutoCorr_G729_32f(pWindow, LPC_ORDERP2, forwardAutoCorr,TmpAlignVec); /* Autocorrelations */
ippsCopy_32f(forwardAutoCorr, TmpAutoCorr, LPC_ORDERP1);
/* Lag windowing */
ippsMul_32f_I(lwindow, &forwardAutoCorr[1], LPC_ORDERP2);
/* Levinson Durbin */
tmp_lev = 0;
sts = ippsLevinsonDurbin_G729_32f(forwardAutoCorr, LPC_ORDER, &forwardLPC[LPC_ORDERP1], forwardReflectCoeff, &tmp_lev);
if(sts == ippStsOverflow) {
ippsCopy_32f(encoderObj->OldForwardLPC,&forwardLPC[LPC_ORDERP1],LPC_ORDER+1);
forwardReflectCoeff[0] = encoderObj->OldForwardRC[0];
forwardReflectCoeff[1] = encoderObj->OldForwardRC[1];
} else {
ippsCopy_32f(&forwardLPC[LPC_ORDERP1],encoderObj->OldForwardLPC,LPC_ORDER+1);
encoderObj->OldForwardRC[0] = forwardReflectCoeff[0];
encoderObj->OldForwardRC[1] = forwardReflectCoeff[1];
}
/* Convert A(z) to lsp */
if(codecType==G729A_CODEC){
ippsLPCToLSP_G729A_32f(&forwardLPC[LPC_ORDERP1], encoderObj->OldLSP, CurrLSP);
} else {
ippsLPCToLSP_G729_32f(&forwardLPC[LPC_ORDERP1], encoderObj->OldLSP, CurrLSP);
}
if (encoderObj->objPrm.mode == G729Encode_VAD_Enabled) {
ownACOS_G729_32f(CurrLSP, CurrLSF, LPC_ORDER);
VoiceActivityDetect_G729_32f(forwardReflectCoeff[1], CurrLSF, forwardAutoCorr, pWindow, encoderObj->sFrameCounter,
encoderObj->prevVADDec, encoderObj->prevPrevVADDec, &VADDecision, &EnergydB,encoderObj->vadMem,TmpAlignVec);
if(codecType!=G729A_CODEC){
MusicDetection_G729E_32f( encoderObj, codecType, forwardAutoCorr[0],forwardReflectCoeff, &VADDecision, EnergydB,encoderObj->msdMem,TmpAlignVec);
}
UpdateCNG(TmpAutoCorr, VADDecision,encoderObj->cngMem);
} else VADDecision = 1;
if(VADDecision == 0) {
if(codecType==G729A_CODEC){
UpdateVad_A(encoderObj, Excitation, WeightedSpeech, SpeechWnd, anau);
} else {
/* Inactive frame */
ippsCopy_32f(&encoderObj->SynthBuffer[FRM_LEN], &encoderObj->SynthBuffer[0], BWD_SYNTH_MEM);
/* Find interpolated LPC parameters in all subframes unquantized. *
* The interpolated parameters are in array forwardLPC of size (LPC_ORDER+1)*4 */
if( encoderObj->prevLPCMode == 0) {
ippsInterpolateC_G729_32f(encoderObj->OldLSP, 0.5f, CurrLSP, 0.5f, tmpLSP, LPC_ORDER);
ippsLSPToLPC_G729_32f(tmpLSP, forwardLPC);
ownACOS_G729_32f(tmpLSP, InterpolatedLSF, LPC_ORDER);
ownACOS_G729_32f(CurrLSP, CurrLSF, LPC_ORDER);
} else {
/* no interpolation */
/* unquantized */
ippsLSPToLPC_G729_32f(CurrLSP, forwardLPC); /* Subframe 1 */
ownACOS_G729_32f(CurrLSP, CurrLSF, LPC_ORDER); /* transformation from LSP to LSF (freq.domain) */
ippsCopy_32f(CurrLSF, InterpolatedLSF, LPC_ORDER); /* Subframe 1 */
}
if (encoderObj->sGlobalStatInd > 10000) {
encoderObj->sGlobalStatInd -= 2621;
if(encoderObj->sGlobalStatInd < 10000) encoderObj->sGlobalStatInd = 10000 ;
}
encoderObj->isBWDDominant = 0;
encoderObj->fInterpolationCoeff = 1.1f;
ippsCopy_32f(CurrLSP, encoderObj->OldLSP, LPC_ORDER);
PWGammaFactor_G729(gamma1, gamma2, InterpolatedLSF, CurrLSF, forwardReflectCoeff,&encoderObj->isSmooth, encoderObj->LogAreaRatioCoeff);
UpdateVad_I(encoderObj, Excitation, forwardLPC, WeightedSpeech, gamma1, gamma2, pSynth,
pError, SpeechWnd, anau, codecType);
/* update previous filter for next frame */
ippsCopy_32f(&forwardQntLPC[LPC_ORDERP1], encoderObj->PrevFlt, LPC_ORDERP1);
for(i=LPC_ORDERP1; i <BWD_LPC_ORDERP1; i++) encoderObj->PrevFlt[i] = 0.f;
}
encoderObj->prevLPCMode = 0;
encoderObj->fBetaPreFilter = PITCH_SHARPMIN;
/* Update memories for next frames */
ippsMove_32f(&encoderObj->OldSpeechBuffer[FRM_LEN], &encoderObj->OldSpeechBuffer[0], SPEECH_BUFF_LEN-FRM_LEN);
ippsMove_32f(&encoderObj->OldWeightedSpeechBuffer[FRM_LEN], &encoderObj->OldWeightedSpeechBuffer[0], PITCH_LAG_MAX);
ippsMove_32f(&encoderObj->OldExcitationBuffer[FRM_LEN], &encoderObj->OldExcitationBuffer[0], PITCH_LAG_MAX+INTERPOL_LEN);
anau = EncodedParams+1;
if(EncodedParams[0] == 0){ /* untransmitted*/
*frametype=0;
}else{
*frametype=1; /* SID*/
dst[0] = (Ipp8u)(((anau[0] & 0x1) << 7) | ((anau[1] & 0x1f) << 2) | ((anau[2] & 0xf)>>2)); /* 1+5+2 */
dst[1] = (Ipp8u)(((anau[2] & 0x3) << 6) | ((anau[3] & 0x1f) << 1)); /* 2+5+LSB */
}
CLEAR_SCRATCH_MEMORY(encoderObj);
return APIG729_StsNoErr;
}
/* Active frame */
*anau++ = CodecType2Num(codecType)+2; /* bit rate mode */
if(encoderObj->objPrm.mode == G729Encode_VAD_Enabled) {
encoderObj->sCNGSeed = INIT_SEED_VAL;
encoderObj->prevPrevVADDec = encoderObj->prevVADDec;
encoderObj->prevVADDec = VADDecision;
}
/* LSP quantization */
ippsLSPQuant_G729E_32f( CurrLSP, (Ipp32f*)encoderObj->PrevFreq, CurrFreq, CurrQntLSP, code_lsp);
if( encoderObj->prevLPCMode == 0) {
if(codecType!=G729A_CODEC){
ippsInterpolateC_G729_32f(encoderObj->OldLSP, 0.5f, CurrLSP, 0.5f, tmpLSP, LPC_ORDER);
ippsLSPToLPC_G729_32f(tmpLSP, forwardLPC);
ownACOS_G729_32f(tmpLSP, InterpolatedLSF, LPC_ORDER);
ownACOS_G729_32f(CurrLSP, CurrLSF, LPC_ORDER);
}
ippsInterpolateC_G729_32f(encoderObj->OldQuantLSP, 0.5f, CurrQntLSP, 0.5f, tmpLSP, LPC_ORDER);
ippsLSPToLPC_G729_32f(tmpLSP, forwardQntLPC);
ippsLSPToLPC_G729_32f(CurrQntLSP, &forwardQntLPC[LPC_ORDER+1]);
} else {
/* no interpolation */
/* unquantized */
ippsLSPToLPC_G729_32f(CurrLSP, forwardLPC); /* Subframe 1 */
ownACOS_G729_32f(CurrLSP, CurrLSF, LPC_ORDER); /* transformation from LSP to LSF (freq.domain) */
ippsCopy_32f(CurrLSF, InterpolatedLSF, LPC_ORDER); /* Subframe 1 */
ippsLSPToLPC_G729_32f(CurrQntLSP, &forwardQntLPC[LPC_ORDERP1]); /* Subframe 2 */
ippsCopy_32f(&forwardQntLPC[LPC_ORDERP1], forwardQntLPC, LPC_ORDERP1); /* Subframe 1 */
}
/* Decision for the switch Forward / Backward mode */
if(codecType == G729E_CODEC) {
/* LPC recursive Window as in G728 */
ippsWinHybrid_G729E_32f(encoderObj->SynthBuffer, backwardAutoCorr, encoderObj->pHWState);
/* Lag windowing */
ippsMul_32f_I(lagBwd, &backwardAutoCorr[1], BWD_LPC_ORDER);
if (backwardAutoCorr[0] < 1.0f) backwardAutoCorr[0] = 1.0f;
sts = ippsLevinsonDurbin_G729_32f(backwardAutoCorr, BWD_LPC_ORDER, &backwardLPC[BWD_LPC_ORDERP1], backwardReflectCoeff, &tmp_lev);
if(sts == ippStsOverflow) {
ippsCopy_32f(encoderObj->OldBackwardLPC,&backwardLPC[BWD_LPC_ORDERP1],BWD_LPC_ORDER+1);
backwardReflectCoeff[0] = encoderObj->OldBackwardRC[0];
backwardReflectCoeff[1] = encoderObj->OldBackwardRC[1];
} else {
ippsCopy_32f(&backwardLPC[BWD_LPC_ORDERP1],encoderObj->OldBackwardLPC,BWD_LPC_ORDER+1);
encoderObj->OldBackwardRC[0] = backwardReflectCoeff[0];
encoderObj->OldBackwardRC[1] = backwardReflectCoeff[1];
}
/* Tests saturation of backwardLPC */
isSaturateAZ = 0;
for (i=BWD_LPC_ORDERP1; i<2*BWD_LPC_ORDERP1; i++) if (backwardLPC[i] >= 8.f) {isSaturateAZ = 1;break;}
if (isSaturateAZ == 1) ippsCopy_32f(encoderObj->BackwardLPCMemory, &backwardLPC[BWD_LPC_ORDERP1], BWD_LPC_ORDERP1);
else ippsCopy_32f(&backwardLPC[BWD_LPC_ORDERP1], encoderObj->BackwardLPCMemory, BWD_LPC_ORDERP1);
/* Additional bandwidth expansion on backward filter */
WeightLPCCoeff_G729(&backwardLPC[BWD_LPC_ORDERP1], BWD_GAMMA, BWD_LPC_ORDER, &backwardLPC[BWD_LPC_ORDERP1]);
SetLPCMode_G729FPE(encoderObj,SpeechWnd, forwardQntLPC, backwardLPC, &LPCMode, CurrLSP,TmpAlignVec);
*anau++ = LPCMode;
} else {
if (encoderObj->sGlobalStatInd > 10000) {
encoderObj->sGlobalStatInd -= 2621;
if( encoderObj->sGlobalStatInd < 10000) encoderObj->sGlobalStatInd = 10000 ;
}
LPCMode = 0;
encoderObj->isBWDDominant = 0;
encoderObj->fInterpolationCoeff = 1.1f;
}
/* Update synthesis signal for next frame. */
ippsCopy_32f(&encoderObj->SynthBuffer[FRM_LEN], &encoderObj->SynthBuffer[0], BWD_SYNTH_MEM);
/* Update the LSPs for the next frame */
ippsCopy_32f(CurrLSP, encoderObj->OldLSP, LPC_ORDER);
if( LPCMode == 0) {
ippsCopy_32f(CurrQntLSP, encoderObj->OldQuantLSP, LPC_ORDER);
ippsMove_32f(encoderObj->PrevFreq[0], encoderObj->PrevFreq[1], 3*LPC_ORDER);
ippsCopy_32f(CurrFreq, encoderObj->PrevFreq[0], LPC_ORDER);
*anau++ = code_lsp[0];
*anau++ = code_lsp[1];
}
/* Find the weighted input speech w_sp[] for the whole speech frame */
if(LPCMode == 0) {
apLen = LPC_ORDER;
aqLen = LPC_ORDER;
if (encoderObj->isBWDDominant == 0) pUnQLPC = forwardLPC;
else pUnQLPC = forwardQntLPC;
pQLPC = forwardQntLPC;
if(codecType==G729A_CODEC) {
/* Compute A(z/gamma) */
WeightLPCCoeff_G729(&forwardQntLPC[0], GAMMA1_G729A, LPC_ORDER, &forwardLPC[0]);
WeightLPCCoeff_G729(&forwardQntLPC[LPC_ORDERP1], GAMMA1_G729A, LPC_ORDER, &forwardLPC[LPC_ORDERP1]);
} else {
PWGammaFactor_G729(gamma1, gamma2, InterpolatedLSF, CurrLSF, forwardReflectCoeff,&encoderObj->isSmooth, encoderObj->LogAreaRatioCoeff);
/* update previous filter for next frame */
ippsCopy_32f(&pQLPC[LPC_ORDERP1], encoderObj->PrevFlt, LPC_ORDERP1);
for(i=LPC_ORDERP1; i <BWD_LPC_ORDERP1; i++) encoderObj->PrevFlt[i] = 0.f;
for(j=LPC_ORDERP1; j<BWD_LPC_ORDERP1; j++) encoderObj->UnitImpulse[j] = 0.f;
}
} else {
if (encoderObj->isBWDDominant == 0) {
apLen = LPC_ORDER;
pUnQLPC = forwardLPC;
} else {
apLen = BWD_LPC_ORDER;
pUnQLPC = backwardLPC;
}
aqLen = BWD_LPC_ORDER;
pQLPC = backwardLPC;
/* ADAPTIVE BANDWIDTH EXPANSION FOR THE PERCEPTUAL WEIGHTING FILTER */
if (encoderObj->isBWDDominant == 0) {
gamma1[0] = 0.9f;
gamma1[1] = 0.9f;
gamma2[0] = 0.4f;
gamma2[1] = 0.4f;
} else {
gamma1[0] = 0.98f;
gamma1[1] = 0.98f;
gamma2[0] = 0.4f;
gamma2[1] = 0.4f;
}
if (encoderObj->isBWDDominant == 0) {
for(j=LPC_ORDERP1; j<BWD_LPC_ORDERP1; j++) encoderObj->UnitImpulse[j] = 0.f;
}
/* update previous filter for next frame */
ippsCopy_32f(&pQLPC[BWD_LPC_ORDERP1], encoderObj->PrevFlt, BWD_LPC_ORDERP1);
}
/* 3.3 Compute weighted input speech for the whole speech frame. */
if(codecType!=G729A_CODEC){
pLPC = pUnQLPC;
for (i=0; i<2; i++) {
WeightLPCCoeff_G729(pLPC, gamma1[i], apLen, WeightedLPC1);
WeightLPCCoeff_G729(pLPC, gamma2[i], apLen, WeightedLPC2);
#ifdef CLIPPING_DENORMAL_MODE
for(j=0; j<BWD_LPC_ORDERP1; j++) CLIP_DENORMAL_I(WeightedLPC1[j]);
for(j=0; j<BWD_LPC_ORDERP1; j++) CLIP_DENORMAL_I(WeightedLPC2[j]);
#endif
ippsConvBiased_32f(WeightedLPC1,apLen+1,&SpeechWnd[i*SUBFR_LEN],SUBFR_LEN+apLen,&WeightedSpeech[i*SUBFR_LEN],SUBFR_LEN,apLen);
ippsSynthesisFilter_G729_32f(WeightedLPC2, apLen, &WeightedSpeech[i*SUBFR_LEN], &WeightedSpeech[i*SUBFR_LEN], SUBFR_LEN, &encoderObj->FltMem[BWD_LPC_ORDER-apLen]);
for(j=0; j<BWD_LPC_ORDER; j++) encoderObj->FltMem[j] = WeightedSpeech[i*SUBFR_LEN+SUBFR_LEN-BWD_LPC_ORDER+j];
pLPC += apLen+1;
}
/* 3.4 Open-loop analysis. */
OpenLoopPitchSearch_G729_32f(WeightedSpeech, &openLoopPitch);
for (i= 0; i< 4; i++)
encoderObj->LagBuffer[i] = encoderObj->LagBuffer[i+1];
avg_lag = AVG(encoderObj->LagBuffer[0],encoderObj->LagBuffer[1],encoderObj->LagBuffer[2],encoderObj->LagBuffer[3]);
if( abs( (Ipp32s) (openLoopPitch/2.0) - avg_lag)<=2)
encoderObj->LagBuffer[4] = (Ipp32s) (openLoopPitch/2.0);
else if( abs((Ipp32s) (openLoopPitch/3.0) - avg_lag)<=2)
encoderObj->LagBuffer[4] = (Ipp32s) (openLoopPitch/3.0);
else
encoderObj->LagBuffer[4] = openLoopPitch;
} else {
ippsConvBiased_32f(&forwardQntLPC[0],LPC_ORDER+1,SpeechWnd,SUBFR_LEN+LPC_ORDER,Excitation,SUBFR_LEN,LPC_ORDER);
ippsConvBiased_32f(&forwardQntLPC[LPC_ORDERP1],LPC_ORDER+1,&SpeechWnd[SUBFR_LEN],SUBFR_LEN+LPC_ORDER,&Excitation[SUBFR_LEN],SUBFR_LEN,LPC_ORDER);
{
WeightedLPC1[0] = 1.0f;
for(i=1; i<=LPC_ORDER; i++)
WeightedLPC1[i] = forwardLPC[i] - 0.7f * forwardLPC[i-1];
ippsSynthesisFilter_G729_32f(WeightedLPC1, LPC_ORDER, &Excitation[0], &WeightedSpeech[0], SUBFR_LEN, encoderObj->FltMem);
for (i = 0; i < LPC_ORDER; i++) CLIP_DENORMAL(WeightedSpeech[(SUBFR_LEN-LPC_ORDER)+i],encoderObj->FltMem[i]);
for(i=1; i<=LPC_ORDER; i++)
WeightedLPC1[i] = forwardLPC[i+LPC_ORDERP1] - 0.7f * forwardLPC[i-1+LPC_ORDERP1];
ippsSynthesisFilter_G729_32f(WeightedLPC1, LPC_ORDER, &Excitation[SUBFR_LEN], &WeightedSpeech[SUBFR_LEN], SUBFR_LEN, encoderObj->FltMem);
for (i = 0; i < LPC_ORDER; i++) CLIP_DENORMAL(WeightedSpeech[SUBFR_LEN+(SUBFR_LEN-LPC_ORDER)+i],encoderObj->FltMem[i]);
}
/* Annex A.3.4 Open-loop analysis */
ippsOpenLoopPitchSearch_G729A_32f(WeightedSpeech, &openLoopPitch);
}
/* Range for closed loop pitch search in 1st subframe */
minPitchDelay = openLoopPitch - 3;
if (minPitchDelay < PITCH_LAG_MIN) minPitchDelay = PITCH_LAG_MIN;
maxPitchDelay = minPitchDelay + 6;
if (maxPitchDelay > PITCH_LAG_MAX) {
maxPitchDelay = PITCH_LAG_MAX;
minPitchDelay = maxPitchDelay - 6;
}
pLPC = pUnQLPC; /* pointer to interpolated "unquantized"LPC parameters */
pQntLPC = pQLPC; /* pointer to interpolated "quantized" LPC parameters */
for (NSbfr = 0, NGamma = 0; NSbfr < FRM_LEN; NSbfr += SUBFR_LEN, NGamma++) {
if(codecType!=G729A_CODEC){
/* LPC computing: weights of filter */
WeightLPCCoeff_G729(pLPC, gamma1[NGamma], apLen, WeightedLPC1);
WeightLPCCoeff_G729(pLPC, gamma2[NGamma], apLen, WeightedLPC2);
/* Clause 3.5 Computation of impulse response */
for (i = 0; i <=apLen; i++) encoderObj->UnitImpulse[i] = WeightedLPC1[i];
ippsSynthesisFilter_G729_32f(pQntLPC, aqLen, encoderObj->UnitImpulse, ImpulseResponse, SUBFR_LEN, pZero);
ippsSynthesisFilter_G729_32f(WeightedLPC2, apLen, ImpulseResponse, ImpulseResponse, SUBFR_LEN, pZero);
/* pass the resudual r(n) through 1/A(z) */
ippsConvBiased_32f(pQntLPC,aqLen+1,&SpeechWnd[NSbfr],SUBFR_LEN+aqLen,&Excitation[NSbfr],SUBFR_LEN,aqLen);
for (i=0; i<SUBFR_LEN; i++) PitchPredResidual[i] = Excitation[NSbfr+i];
ippsSynthesisFilter_G729_32f(pQntLPC, aqLen, &Excitation[NSbfr], pError, SUBFR_LEN, &encoderObj->ErrFltMemory[BWD_LPC_ORDER-aqLen]);
/* then through the weighting filter W(z) where TargetVector is a target signal*/
ippsConvBiased_32f(WeightedLPC1,aqLen+1,pError,SUBFR_LEN+aqLen,TargetVector,SUBFR_LEN,aqLen);
ippsSynthesisFilter_G729_32f(WeightedLPC2, apLen, TargetVector, TargetVector, SUBFR_LEN, &encoderObj->WeightedFilterMemory[BWD_LPC_ORDER-apLen]);
pitchDelay = AdaptiveCodebookSearch_G729_32f(&Excitation[NSbfr], TargetVector, ImpulseResponse, SUBFR_LEN, minPitchDelay, maxPitchDelay,
NSbfr, &fracPartPitchDelay, codecType,TmpAlignVec);
} else {
/* Computation of impulse response */
ImpulseResponse[0] = 1.0f;
ippsZero_32f(&ImpulseResponse[1], SUBFR_LEN-1);
ippsSynthesisFilter_G729_32f(pLPC, LPC_ORDER, ImpulseResponse, ImpulseResponse, SUBFR_LEN, &ImpulseResponse[1]);
/* Annex A.3.6 Computation of target signal */
ippsSynthesisFilter_G729_32f(pLPC, LPC_ORDER, &Excitation[NSbfr], TargetVector, SUBFR_LEN, encoderObj->WeightedFilterMemory);
/* Annex A.3.7 Adaptive-codebook search */
pitchDelay = ownAdaptiveCodebookSearch_G729A_32f(&Excitation[NSbfr], TargetVector, ImpulseResponse, minPitchDelay, maxPitchDelay,
NSbfr, &fracPartPitchDelay,TmpAlignVec);
}
if (NSbfr == 0) { /* if 1st subframe */
/* encode pitch delay (with fraction) */
if (pitchDelay <= 85)
index = pitchDelay*3 - 58 + fracPartPitchDelay;
else
index = pitchDelay + 112;
/* find T0_min and T0_max for second subframe */
minPitchDelay = pitchDelay - 5;
if (minPitchDelay < PITCH_LAG_MIN) minPitchDelay = PITCH_LAG_MIN;
maxPitchDelay = minPitchDelay + 9;
if (maxPitchDelay > PITCH_LAG_MAX) {
maxPitchDelay = PITCH_LAG_MAX;
minPitchDelay = maxPitchDelay - 9;
}
} else { /* second subframe */
if (codecType == G729D_CODEC) { /* 4 bits in 2nd subframe (6.4 kbps) */
if (pitchDelay < minPitchDelay + 3)
index = pitchDelay - minPitchDelay;
else if (pitchDelay < minPitchDelay + 7)
index = (pitchDelay - (minPitchDelay + 3)) * 3 + fracPartPitchDelay + 3;
else
index = (pitchDelay - (minPitchDelay + 7)) + 13;
} else {
index = pitchDelay - minPitchDelay;
index = index*3 + 2 + fracPartPitchDelay;
}
}
*anau++ = index;
if ( (NSbfr == 0) && (codecType != G729D_CODEC) ) {
*anau = Parity(index);
if( codecType == G729E_CODEC) {
*anau ^= ((index >> 1) & 0x0001);
}
anau++;
}
if(codecType!=G729A_CODEC){
delayLine[0] = pitchDelay;
delayLine[1] = fracPartPitchDelay;
ippsDecodeAdaptiveVector_G729_32f_I(delayLine, &Excitation[NSbfr]);
ippsConvBiased_32f( &Excitation[NSbfr], SUBFR_LEN , ImpulseResponse, SUBFR_LEN , FltAdaptExc, SUBFR_LEN ,0);
} else {
ippsSynthesisFilter_G729_32f(pLPC, LPC_ORDER, &Excitation[NSbfr], FltAdaptExc, SUBFR_LEN, encoderObj->ZeroMemory);
}
/* clause 3.7.3 Computation of the adaptive-codebook gain */
pitchGain = ownAdaptiveCodebookGainCoeff_G729_32f(TargetVector, FltAdaptExc, g_coeff, SUBFR_LEN);
/* clip pitch gain if taming is necessary */
taming = TestErrorContribution_G729(pitchDelay, fracPartPitchDelay, encoderObj->ExcitationError);
if( taming == 1){
CLIP_TO_UPLEVEL(pitchGain,MAX_GAIN_TIMING);
}
/* Annex A3.8.1 */
/* clause 3.8.1 Equ 50 */
ippsAdaptiveCodebookContribution_G729_32f(pitchGain, FltAdaptExc, TargetVector, FltTargetVector);
/* Fixed codebook search. */
{
LOCAL_ALIGN_ARRAY(32, Ipp32f, dn, SUBFR_LEN,encoderObj);
LOCAL_ALIGN_ARRAY(32, Ipp32f, rr, TOEPLIZ_MATRIX_SIZE,encoderObj);
/* pitch contribution to ImpulseResponse */
if(pitchDelay < SUBFR_LEN) {
ippsHarmonicFilter_32f_I(encoderObj->fBetaPreFilter,pitchDelay,&ImpulseResponse[pitchDelay],SUBFR_LEN-pitchDelay);
}
ippsCrossCorr_32f(ImpulseResponse, SUBFR_LEN, FltTargetVector, SUBFR_LEN, dn, SUBFR_LEN, 0);
switch (codecType) {
case G729_CODEC: /* 8 kbit/s */
{
ippsToeplizMatrix_G729_32f(ImpulseResponse, rr);
ippsFixedCodebookSearch_G729_32f(dn, rr, FixedCodebookExc, anau, &encoderObj->sSearchTimes, NSbfr);
CodewordImpConv_G729_32f(anau[0],FixedCodebookExc,ImpulseResponse,FltFixedCodebookExc );
anau += 2;
break;
}
case G729A_CODEC: /* 8 kbit/s */
{
ippsToeplizMatrix_G729_32f(ImpulseResponse, rr);
ippsFixedCodebookSearch_G729A_32f(dn, rr, FixedCodebookExc, anau);
CodewordImpConv_G729_32f(anau[0],FixedCodebookExc,ImpulseResponse,FltFixedCodebookExc );
anau += 2;
break;
}
case G729D_CODEC: /* 6.4 kbit/s */
{
ippsToeplizMatrix_G729D_32f(ImpulseResponse, rr);
for(j=0; j<SUBFR_LEN; j++) CLIP_DENORMAL_I(dn[j]);
ippsFixedCodebookSearch_G729D_32f(dn, rr, ImpulseResponse, FixedCodebookExc, FltFixedCodebookExc, anau);
anau += 2;
break;
}
case G729E_CODEC: /* 11.8 kbit/s */
{
/* calculate residual after long term prediction */
ippsAdaptiveCodebookContribution_G729_32f(pitchGain, &Excitation[NSbfr], PitchPredResidual, PitchPredResidual);
ippsFixedCodebookSearch_G729E_32f(LPCMode, dn, PitchPredResidual, ImpulseResponse, FixedCodebookExc, FltFixedCodebookExc, anau);
anau += 5;
break;
}
} /* end of switch */
/* Include fixed-gain pitch contribution into FixedCodebookExc. */
if(pitchDelay < SUBFR_LEN) {
ippsHarmonicFilter_32f_I(encoderObj->fBetaPreFilter,pitchDelay,&FixedCodebookExc[pitchDelay],SUBFR_LEN-pitchDelay);
}
LOCAL_ALIGN_ARRAY_FREE(32, Ipp32f, rr, TOEPLIZ_MATRIX_SIZE,encoderObj);
LOCAL_ALIGN_ARRAY_FREE(32, Ipp32f, dn, SUBFR_LEN,encoderObj);
}
/* Annex A3.9 Quantization of gains */
/* + Clause 3.9*/
AdaptiveCodebookGainCoeff_G729_32f( TargetVector, FltAdaptExc, FltFixedCodebookExc, g_coeff);
index = GainQuant_G729(FixedCodebookExc, g_coeff, SUBFR_LEN, &pitchGain, &codeGain, taming,encoderObj->PastQuantEnergy,codecType,(Ipp8s *)TmpAlignVec);
*anau++ = index;
/* Update and bound pre filter factor with quantized adaptive codebook gain */
for (i= 0; i< 4; i++)
encoderObj->PitchGainBuffer[i] = encoderObj->PitchGainBuffer[i+1];
encoderObj->PitchGainBuffer[4] = pitchGain;
encoderObj->fBetaPreFilter = pitchGain;
CLIP_TO_UPLEVEL(encoderObj->fBetaPreFilter,PITCH_SHARPMAX);
CLIP_TO_LOWLEVEL(encoderObj->fBetaPreFilter,PITCH_SHARPMIN);
/* Find the total excitation. */
ippsInterpolateC_G729_32f(&Excitation[NSbfr], pitchGain, FixedCodebookExc, codeGain, &Excitation[NSbfr], SUBFR_LEN);
/* Update error function for taming process. */
UpdateExcErr_G729(pitchGain, pitchDelay, encoderObj->ExcitationError);
if(codecType!=G729A_CODEC){
/* Find synthesis speech corresponding to Excitation. */
ippsSynthesisFilter_G729_32f(pQntLPC, aqLen, &Excitation[NSbfr], &pSynth[NSbfr], SUBFR_LEN, &encoderObj->SynFltMemory[BWD_LPC_ORDER-aqLen]);
for(j=0; j<BWD_LPC_ORDER; j++) encoderObj->SynFltMemory[j] = pSynth[NSbfr+SUBFR_LEN-BWD_LPC_ORDER+j];
/* Update filters memories. */
for (i = SUBFR_LEN-BWD_LPC_ORDER, j = 0; i < SUBFR_LEN; i++, j++) {
encoderObj->ErrFltMemory[j] = SpeechWnd[NSbfr+i] - pSynth[NSbfr+i];
encoderObj->WeightedFilterMemory[j] = TargetVector[i] - pitchGain*FltAdaptExc[i] - codeGain*FltFixedCodebookExc[i];
}
} else {
/* Update filters memories. */
for (i = SUBFR_LEN-LPC_ORDER, j = 0; i < SUBFR_LEN; i++, j++)
encoderObj->WeightedFilterMemory[j] = TargetVector[i] - pitchGain*FltAdaptExc[i] - codeGain*FltFixedCodebookExc[i];
}
pLPC += apLen+1;
pQntLPC += aqLen+1;
}
encoderObj->prevLPCMode = LPCMode;
/* Update signal for next frame. */
ippsMove_32f(&encoderObj->OldSpeechBuffer[FRM_LEN], &encoderObj->OldSpeechBuffer[0], SPEECH_BUFF_LEN-FRM_LEN);
ippsMove_32f(&encoderObj->OldWeightedSpeechBuffer[FRM_LEN], &encoderObj->OldWeightedSpeechBuffer[0], PITCH_LAG_MAX);
ippsMove_32f(&encoderObj->OldExcitationBuffer[FRM_LEN], &encoderObj->OldExcitationBuffer[0], PITCH_LAG_MAX+INTERPOL_LEN);
if((codecType == G729_CODEC)&&(VADDecision != 1)) {
anau = EncodedParams;
} else if(codecType == G729E_CODEC) {
anau = EncodedParams+2;
} else {
anau = EncodedParams+1;
}
if((codecType == G729_CODEC)||(codecType == G729A_CODEC)) {
*frametype=3;
dst[0] = (Ipp8u)(anau[0] & 0xff);
dst[1] = (Ipp8u)((anau[1] & 0x3ff) >> 2);
dst[2] = (Ipp8u)(((anau[1] & 0x3) << 6) | ((anau[2]>>2)&0x3f) ); /* 2 + 6 */
/* 2 + 1 + 5*/
dst[3] = (Ipp8u)(((anau[2] & 0x3) << 6) | ((anau[3] & 0x1) << 5) | ((anau[4] & 0x1fff) >> 8) );
dst[4] = (Ipp8u)(anau[4] & 0xff); /* 8*/
dst[5] = (Ipp8u)(((anau[5] & 0xf)<<4) | ((anau[6] & 0x7f) >> 3)); /* 4 + 4*/
dst[6] = (Ipp8u)(((anau[6] & 0x7)<< 5) | (anau[7] & 0x1f)); /* 3 + 5*/
dst[7] = (Ipp8u)((anau[8] & 0x1fff) >> 5); /* 8*/
dst[8] = (Ipp8u)(((anau[8] & 0x1f) << 3) | ((anau[9] & 0xf) >> 1)); /* 5 + 3*/
dst[9] = (Ipp8u)(((anau[9] & 0x1) << 7) | (anau[10] & 0x7f)); /* 1 + 7*/
} else if(codecType == G729D_CODEC) {/* D */
*frametype=2;
dst[0] = (Ipp8u)( anau[0] & 0xff); /*8*/
dst[1] = (Ipp8u)( (anau[1] & 0x3ff) >> 2); /*8*/
dst[2] = (Ipp8u)( ((anau[1] & 0x3) << 6) | ((anau[2]>>2)&0x3f)); /* 2 + 6 */
dst[3] = (Ipp8u)( ((anau[2] & 0x3) << 6) | ((anau[3]>>3)&0x3f)); /* 2 + 6 */
dst[4] = (Ipp8u)( ((anau[3] & 0x7) << 5) | ((anau[4] & 0x3) << 3) | ((anau[5] >> 3)& 0x7)); /* 3 + 2 + 3 */
dst[5] = (Ipp8u)( ((anau[5] & 0x7) << 5) | ((anau[6] & 0xf) << 1)| ((anau[7] >> 8)& 0x1)); /* 3 + 4 + 1*/
dst[6] = (Ipp8u)( anau[7] & 0xff); /* 8*/
dst[7] = (Ipp8u)( (anau[8] & 0x3) << 6 | (anau[9] & 0x3f)); /* 2 + 6*/
} else if(codecType == G729E_CODEC) {/* E*/
*frametype=4;
if(LPCMode == 0) { /* Forward*/
dst[0] = (Ipp8u)( (anau[0] >> 2) & 0x3f); /* 2 + 6 */
dst[1] = (Ipp8u)( ((anau[0] & 0x3) << 6) | ((anau[1]>>4)&0x3f)); /* 2 + 6 */
dst[2] = (Ipp8u)( ((anau[1] & 0xf) << 4) | ((anau[2]>>4)&0xf)); /* 4 + 4 */
dst[3] = (Ipp8u)( ((anau[2] & 0xf) << 4) | ((anau[3]&0x1)<<3) | ((anau[4]>>4)&0x7)); /* 4 + 1 + 3 */
dst[4] = (Ipp8u)( ((anau[4] & 0xf) << 4) | ((anau[5]>>3)&0xf)); /* 4 + 4 */
dst[5] = (Ipp8u)( ((anau[5] & 0x7) << 5) | ((anau[6]>>2)&0x1f)); /* 3 + 5 */
dst[6] = (Ipp8u)( ((anau[6] & 0x3) << 6) | ((anau[7]>>1)&0x3f)); /* 2 + 6 */
dst[7] = (Ipp8u)( ((anau[7]& 0x1) << 7) | (anau[8]&0x7f)); /* 1 + 7 */
dst[8] = (Ipp8u)( ((anau[9]& 0x7f) << 1) | ((anau[10]>>4)&0x1)); /* 7 + 1 */
dst[9] = (Ipp8u)( ((anau[10] & 0xf) << 4) | ((anau[11]>>3)&0xf)); /* 4 + 4 */
dst[10] = (Ipp8u)( ((anau[11] & 0x7) << 5) | ((anau[12]>>2)&0x1f)); /* 3 + 5 */
dst[11] = (Ipp8u)( ((anau[12] & 0x3) << 6) | ((anau[13]>>1)&0x3f)); /* 2 + 6 */
dst[12] = (Ipp8u)( ((anau[13]& 0x1) << 7) | (anau[14]&0x7f)); /* 1 + 7 */
dst[13] = (Ipp8u)( ((anau[15]& 0x7f) << 1) | ((anau[16]>>6)&0x1)); /* 7 + 1 */
dst[14] = (Ipp8u)( ((anau[16] & 0x3f) << 2));
} else { /* Backward*/
dst[0] = (Ipp8u)((3<<6) | ((anau[0] >> 2) & 0x3f));
dst[1] = (Ipp8u)(((anau[0] & 0x3) << 6) | ((anau[1]&0x1)<<5) | ((anau[2]>>8)&0x1f)); /* 2 + 1 + 5 */
dst[2] = (Ipp8u)(anau[2] & 0xff);
dst[3] = (Ipp8u)((anau[3] >> 2) & 0xff); /* 2 + 6 */
dst[4] = (Ipp8u)(((anau[3] & 0x3) << 6) | ((anau[4]>>1)&0x3f)); /* 2 + 6 */
dst[5] = (Ipp8u)(((anau[4]& 0x1) << 7) | (anau[5]&0x7f)); /* 1 + 7 */
dst[6] = (Ipp8u)(((anau[6]& 0x7f) << 1) | ((anau[7]>>6)&0x1)); /* 7 + 1 */
dst[7] = (Ipp8u)(((anau[7]&0x3f) << 2) | ((anau[8] >>3)&0x3));
dst[8] = (Ipp8u)(((anau[8] & 0x7) << 5) | ((anau[9]>>8)&0x1f)); /* 3 + 5 */
dst[9] = (Ipp8u)(anau[9] & 0xff);
dst[10] = (Ipp8u)((anau[10] >> 2) & 0xff); /* 2 + 6 */
dst[11] = (Ipp8u)(((anau[10] & 0x3) << 6) | ((anau[11]>>1)&0x3f)); /* 2 + 6 */
dst[12] = (Ipp8u)(((anau[11]& 0x1) << 7) | (anau[12]&0x7f)); /* 1 + 7 */
dst[13] = (Ipp8u)(((anau[13]& 0x7f) << 1) | ((anau[14]>>6)&0x1)); /* 7 + 1 */
dst[14] = (Ipp8u)(((anau[14] & 0x3f) << 2));
}
}
CLEAR_SCRATCH_MEMORY(encoderObj);
return APIG729_StsNoErr;
}
G729_CODECFUN( APIG729_Status, apiG729FPEncodeVAD,
(G729FPEncoder_Obj* encoderObj,const Ipp16s* src, Ipp16s* dst, G729Codec_Type codecType, Ipp32s *frametype ))
{
LOCAL_ALIGN_ARRAY(32, Ipp32f, forwardAutoCorr, (LPC_ORDERP2+1),encoderObj); /* Autocorrelations (forward) */
LOCAL_ALIGN_ARRAY(32, Ipp32f, forwardLPC, LPC_ORDERP1*2,encoderObj); /* A(z) forward unquantized for the 2 subframes */
LOCAL_ALIGN_ARRAY(32, Ipp32f, forwardQntLPC, LPC_ORDERP1*2,encoderObj); /* A(z) forward quantized for the 2 subframes */
LOCAL_ALIGN_ARRAY(32, Ipp32f, WeightedLPC1, BWD_LPC_ORDERP1,encoderObj); /* A(z) with spectral expansion */
LOCAL_ALIGN_ARRAY(32, Ipp32f, WeightedLPC2, BWD_LPC_ORDERP1,encoderObj); /* A(z) with spectral expansion */
LOCAL_ALIGN_ARRAY(32, Ipp32f, TmpAlignVec, WINDOW_LEN,encoderObj);
LOCAL_ARRAY(Ipp32f, TmpAutoCorr, LPC_ORDERP1,encoderObj);
LOCAL_ARRAY(Ipp32f, forwardReflectCoeff, LPC_ORDER,encoderObj);
LOCAL_ARRAY(Ipp32f, CurrLSP, LPC_ORDER,encoderObj);
LOCAL_ARRAY(Ipp32f, CurrLSF, LPC_ORDER,encoderObj);
LOCAL_ARRAY(Ipp32f, tmpLSP, LPC_ORDER,encoderObj);
LOCAL_ARRAY(Ipp32f, InterpolatedLSF, LPC_ORDER,encoderObj);
LOCAL_ARRAY(Ipp32s, EncodedParams, 5,encoderObj);
LOCAL_ARRAY(Ipp32f, gamma1, 2,encoderObj);
LOCAL_ARRAY(Ipp32f, gamma2, 2,encoderObj);
Ipp32f *SpeechWnd, *pWindow;
Ipp32f *newSpeech;
/* Weighted speech vector */
Ipp32f *WeightedSpeech;
/* Excitation vector */
Ipp32f *Excitation;
Ipp32f *pError=NULL;
Ipp32f *pSynth=NULL;
Ipp32s *anau;
Ipp16s isVAD;
Ipp32f tmp_lev;
Ipp32s VADDecision, i;
Ipp32f EnergydB;
IppStatus sts;
if(NULL==encoderObj || NULL==src || NULL ==dst)
return APIG729_StsBadArgErr;
if ((codecType != G729_CODEC)&&(codecType != G729A_CODEC)
&&(codecType != G729D_CODEC)&&(codecType != G729E_CODEC))
return APIG729_StsBadCodecType;
if(encoderObj->objPrm.objSize <= 0)
return APIG729_StsNotInitialized;
if(ENC_KEY != encoderObj->objPrm.key)
return APIG729_StsBadCodecType;
isVAD = (Ipp16s)(encoderObj->objPrm.mode == G729Encode_VAD_Enabled);
if(!isVAD) return APIG729_StsNoErr;
ippsZero_32f(WeightedLPC1,BWD_LPC_ORDERP1);
ippsZero_32f(WeightedLPC2,BWD_LPC_ORDERP1);