-
Notifications
You must be signed in to change notification settings - Fork 1
/
cod_amr.c
1502 lines (1197 loc) · 50.2 KB
/
cod_amr.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
/* ------------------------------------------------------------------
* Copyright (C) 1998-2009 PacketVideo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
* See the License for the specific language governing permissions
* and limitations under the License.
* -------------------------------------------------------------------
*/
/****************************************************************************************
Portions of this file are derived from the following 3GPP standard:
3GPP TS 26.073
ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
Available from http://www.3gpp.org
(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
Permission to distribute, modify and use this file under the standard license
terms listed above has been obtained from the copyright holder.
****************************************************************************************/
/*
------------------------------------------------------------------------------
Filename: cod_amr.cpp
Functions: cod_amr_init
cod_amr_reset
cod_amr_exit
cod_amr_first
cod_amr
------------------------------------------------------------------------------
MODULE DESCRIPTION
These functions comprise the main encoder routine operating on a frame basis.
------------------------------------------------------------------------------
*/
/*----------------------------------------------------------------------------
; INCLUDES
----------------------------------------------------------------------------*/
#include "cod_amr.h"
#include "typedef.h"
#include "cnst.h"
#include "qua_gain.h"
#include "lpc.h"
#include "lsp.h"
#include "pre_big.h"
#include "ol_ltp.h"
#include "p_ol_wgh.h"
#include "spreproc.h"
#include "cl_ltp.h"
#include "pred_lt.h"
#include "spstproc.h"
#include "cbsearch.h"
#include "gain_q.h"
#include "convolve.h"
#include "ton_stab.h"
#include "vad.h"
#include "dtx_enc.h"
#include "oscl_mem.h"
#ifdef VAD2
#include "vad2.h"
#endif
/*----------------------------------------------------------------------------
; MACROS
; Define module specific macros here
----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
; DEFINES
; Include all pre-processor statements here. Include conditional
; compile variables also.
----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
; LOCAL FUNCTION DEFINITIONS
; Function Prototype declaration
----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
; LOCAL VARIABLE DEFINITIONS
; Variable declaration - defined here and used outside this module
----------------------------------------------------------------------------*/
/* Spectral expansion factors */
static const Word16 gamma1[M] =
{
30802, 28954, 27217, 25584, 24049,
22606, 21250, 19975, 18777, 17650
};
/* gamma1 differs for the 12k2 coder */
static const Word16 gamma1_12k2[M] =
{
29491, 26542, 23888, 21499, 19349,
17414, 15672, 14105, 12694, 11425
};
static const Word16 gamma2[M] =
{
19661, 11797, 7078, 4247, 2548,
1529, 917, 550, 330, 198
};
/*
------------------------------------------------------------------------------
FUNCTION NAME: cod_amr_init
------------------------------------------------------------------------------
INPUT AND OUTPUT DEFINITIONS
Inputs:
state = pointer to a pointer to a structure of type cod_amrState
Outputs:
Structure pointed to by the pointer pointed to by state is
initialized to its reset value
state points to the allocated memory
Returns:
Returns 0 if memory was successfully initialized,
otherwise returns -1.
Global Variables Used:
None.
Local Variables Needed:
None.
------------------------------------------------------------------------------
FUNCTION DESCRIPTION
This function allocates memory and initializes state variables.
------------------------------------------------------------------------------
REQUIREMENTS
None.
------------------------------------------------------------------------------
REFERENCES
cod_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
------------------------------------------------------------------------------
PSEUDO-CODE
int cod_amr_init (cod_amrState **state, Flag dtx)
{
cod_amrState* s;
if (state == (cod_amrState **) NULL){
fprintf(stderr, "cod_amr_init: invalid parameter\n");
return -1;
}
*state = NULL;
// allocate memory
if ((s= (cod_amrState *) malloc(sizeof(cod_amrState))) == NULL){
fprintf(stderr, "cod_amr_init: can not malloc state structure\n");
return -1;
}
s->lpcSt = NULL;
s->lspSt = NULL;
s->clLtpSt = NULL;
s->gainQuantSt = NULL;
s->pitchOLWghtSt = NULL;
s->tonStabSt = NULL;
s->vadSt = NULL;
s->dtx_encSt = NULL;
s->dtx = dtx;
// Init sub states
if (cl_ltp_init(&s->clLtpSt) ||
lsp_init(&s->lspSt) ||
gainQuant_init(&s->gainQuantSt) ||
p_ol_wgh_init(&s->pitchOLWghtSt) ||
ton_stab_init(&s->tonStabSt) ||
#ifndef VAD2
vad1_init(&s->vadSt) ||
#else
vad2_init(&s->vadSt) ||
#endif
dtx_enc_init(&s->dtx_encSt) ||
lpc_init(&s->lpcSt)) {
cod_amr_exit(&s);
return -1;
}
cod_amr_reset(s);
*state = s;
return 0;
}
------------------------------------------------------------------------------
CAUTION [optional]
[State any special notes, constraints or cautions for users of this function]
------------------------------------------------------------------------------
*/
Word16 cod_amr_init(cod_amrState **state, Flag dtx)
{
cod_amrState* s;
if (state == (cod_amrState **) NULL)
{
/* fprint(stderr, "cod_amr_init: invalid parameter\n"); */
return(-1);
}
*state = NULL;
/* allocate memory */
if ((s = (cod_amrState *) oscl_malloc(sizeof(cod_amrState))) == NULL)
{
/* fprint(stderr, "cod_amr_init:
can not malloc state structure\n"); */
return(-1);
}
get_const_tbls(&s->common_amr_tbls);
s->lpcSt = NULL;
s->lspSt = NULL;
s->clLtpSt = NULL;
s->gainQuantSt = NULL;
s->pitchOLWghtSt = NULL;
s->tonStabSt = NULL;
s->vadSt = NULL;
s->dtx_encSt = NULL;
s->dtx = dtx;
/* Initialize overflow Flag */
s->overflow = 0;
/* Init sub states */
if (cl_ltp_init(&s->clLtpSt) ||
lsp_init(&s->lspSt) ||
gainQuant_init(&s->gainQuantSt) ||
p_ol_wgh_init(&s->pitchOLWghtSt) ||
ton_stab_init(&s->tonStabSt) ||
#ifndef VAD2
vad1_init(&s->vadSt) ||
#else
vad2_init(&s->vadSt) ||
#endif
dtx_enc_init(&s->dtx_encSt, s->common_amr_tbls.lsp_init_data_ptr) ||
lpc_init(&s->lpcSt))
{
cod_amr_exit(&s);
return(-1);
}
cod_amr_reset(s);
*state = s;
return(0);
}
/****************************************************************************/
/*
------------------------------------------------------------------------------
FUNCTION NAME: cod_amr_reset
------------------------------------------------------------------------------
INPUT AND OUTPUT DEFINITIONS
Inputs:
state = pointer to a structure of type cod_amrState
Outputs:
Structure pointed to by state is initialized to initial values.
Returns:
Returns 0 if memory was successfully initialized,
otherwise returns -1.
Global Variables Used:
None.
Local Variables Needed:
None.
------------------------------------------------------------------------------
FUNCTION DESCRIPTION
This function resets the state memory for cod_amr.
------------------------------------------------------------------------------
REQUIREMENTS
None.
------------------------------------------------------------------------------
REFERENCES
cod_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
------------------------------------------------------------------------------
PSEUDO-CODE
int cod_amr_reset (cod_amrState *st)
{
Word16 i;
if (st == (cod_amrState *) NULL){
fprintf(stderr, "cod_amr_reset: invalid parameter\n");
return -1;
}
*-----------------------------------------------------------------------*
* Initialize pointers to speech vector. *
*-----------------------------------------------------------------------*
st->new_speech = st->old_speech + L_TOTAL - L_FRAME; // New speech
st->speech = st->new_speech - L_NEXT; // Present frame
st->p_window = st->old_speech + L_TOTAL - L_WINDOW; // For LPC window
st->p_window_12k2 = st->p_window - L_NEXT; // EFR LPC window: no lookahead
// Initialize static pointers
st->wsp = st->old_wsp + PIT_MAX;
st->exc = st->old_exc + PIT_MAX + L_INTERPOL;
st->zero = st->ai_zero + MP1;
st->error = st->mem_err + M;
st->h1 = &st->hvec[L_SUBFR];
// Static vectors to zero
Set_zero(st->old_speech, L_TOTAL);
Set_zero(st->old_exc, PIT_MAX + L_INTERPOL);
Set_zero(st->old_wsp, PIT_MAX);
Set_zero(st->mem_syn, M);
Set_zero(st->mem_w, M);
Set_zero(st->mem_w0, M);
Set_zero(st->mem_err, M);
Set_zero(st->zero, L_SUBFR);
Set_zero(st->hvec, L_SUBFR); // set to zero "h1[-L_SUBFR..-1]"
// OL LTP states
for (i = 0; i < 5; i++)
{
st->old_lags[i] = 40;
}
// Reset lpc states
lpc_reset(st->lpcSt);
// Reset lsp states
lsp_reset(st->lspSt);
// Reset clLtp states
cl_ltp_reset(st->clLtpSt);
gainQuant_reset(st->gainQuantSt);
p_ol_wgh_reset(st->pitchOLWghtSt);
ton_stab_reset(st->tonStabSt);
#ifndef VAD2
vad1_reset(st->vadSt);
#else
vad2_reset(st->vadSt);
#endif
dtx_enc_reset(st->dtx_encSt);
st->sharp = SHARPMIN;
return 0;
}
------------------------------------------------------------------------------
CAUTION [optional]
[State any special notes, constraints or cautions for users of this function]
------------------------------------------------------------------------------
*/
Word16 cod_amr_reset(cod_amrState *st)
{
Word16 i;
if (st == (cod_amrState *) NULL)
{
/* fprint(stderr, "cod_amr_reset: invalid parameter\n"); */
return(-1);
}
/*-----------------------------------------------------------------------*
* Initialize pointers to speech vector. *
*-----------------------------------------------------------------------*/
st->new_speech = st->old_speech + L_TOTAL - L_FRAME; /* New speech */
st->speech = st->new_speech - L_NEXT; /* Present frame */
st->p_window = st->old_speech + L_TOTAL - L_WINDOW; /* For LPC window */
st->p_window_12k2 = st->p_window - L_NEXT; /* EFR LPC window: no lookahead */
/* Initialize static pointers */
st->wsp = st->old_wsp + PIT_MAX;
st->exc = st->old_exc + PIT_MAX + L_INTERPOL;
st->zero = st->ai_zero + MP1;
st->error = st->mem_err + M;
st->h1 = &st->hvec[L_SUBFR];
/* Initialize overflow Flag */
st->overflow = 0;
/* Static vectors to zero */
oscl_memset(st->old_speech, 0, sizeof(Word16)*L_TOTAL);
oscl_memset(st->old_exc, 0, sizeof(Word16)*(PIT_MAX + L_INTERPOL));
oscl_memset(st->old_wsp, 0, sizeof(Word16)*PIT_MAX);
oscl_memset(st->mem_syn, 0, sizeof(Word16)*M);
oscl_memset(st->mem_w, 0, sizeof(Word16)*M);
oscl_memset(st->mem_w0, 0, sizeof(Word16)*M);
oscl_memset(st->mem_err, 0, sizeof(Word16)*M);
oscl_memset(st->zero, 0, sizeof(Word16)*L_SUBFR);
oscl_memset(st->hvec, 0, sizeof(Word16)*L_SUBFR); /* set to zero "h1[-L_SUBFR..-1]" */
/* OL LTP states */
for (i = 0; i < 5; i++)
{
st->old_lags[i] = 40;
}
/* Reset lpc states */
lpc_reset(st->lpcSt);
/* Reset lsp states */
lsp_reset(st->lspSt);
/* Reset clLtp states */
cl_ltp_reset(st->clLtpSt);
gainQuant_reset(st->gainQuantSt);
p_ol_wgh_reset(st->pitchOLWghtSt);
ton_stab_reset(st->tonStabSt);
#ifndef VAD2
vad1_reset(st->vadSt);
#else
vad2_reset(st->vadSt);
#endif
dtx_enc_reset(st->dtx_encSt, st->common_amr_tbls.lsp_init_data_ptr);
st->sharp = SHARPMIN;
return(0);
}
/****************************************************************************/
/*
------------------------------------------------------------------------------
FUNCTION NAME: cod_amr_exit
------------------------------------------------------------------------------
INPUT AND OUTPUT DEFINITIONS
Inputs:
state = pointer to a pointer to a structure of type cod_amrState
Outputs:
state points to a NULL address
Returns:
None.
Global Variables Used:
None.
Local Variables Needed:
None.
------------------------------------------------------------------------------
FUNCTION DESCRIPTION
This function frees the memory used for state memory.
------------------------------------------------------------------------------
REQUIREMENTS
None.
------------------------------------------------------------------------------
REFERENCES
cod_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
------------------------------------------------------------------------------
PSEUDO-CODE
void cod_amr_exit (cod_amrState **state)
{
if (state == NULL || *state == NULL)
return;
// dealloc members
lpc_exit(&(*state)->lpcSt);
lsp_exit(&(*state)->lspSt);
gainQuant_exit(&(*state)->gainQuantSt);
cl_ltp_exit(&(*state)->clLtpSt);
p_ol_wgh_exit(&(*state)->pitchOLWghtSt);
ton_stab_exit(&(*state)->tonStabSt);
#ifndef VAD2
vad1_exit(&(*state)->vadSt);
#else
vad2_exit(&(*state)->vadSt);
#endif
dtx_enc_exit(&(*state)->dtx_encSt);
// deallocate memory
free(*state);
*state = NULL;
return;
}
------------------------------------------------------------------------------
CAUTION [optional]
[State any special notes, constraints or cautions for users of this function]
------------------------------------------------------------------------------
*/
void cod_amr_exit(cod_amrState **state)
{
if (state == NULL || *state == NULL)
{
return;
}
/* dealloc members */
lpc_exit(&(*state)->lpcSt);
lsp_exit(&(*state)->lspSt);
gainQuant_exit(&(*state)->gainQuantSt);
cl_ltp_exit(&(*state)->clLtpSt);
p_ol_wgh_exit(&(*state)->pitchOLWghtSt);
ton_stab_exit(&(*state)->tonStabSt);
#ifndef VAD2
vad1_exit(&(*state)->vadSt);
#else
vad2_exit(&(*state)->vadSt);
#endif
dtx_enc_exit(&(*state)->dtx_encSt);
/* deallocate memory */
oscl_free(*state); // BX
*state = NULL;
return;
}
/****************************************************************************/
/*
------------------------------------------------------------------------------
FUNCTION NAME: cod_amr_first
------------------------------------------------------------------------------
INPUT AND OUTPUT DEFINITIONS
Inputs:
st = pointer to a structure of type cod_amrState
new_speech = pointer to buffer of length L_FRAME that contains
the speech input (Word16)
Outputs:
The structure of type cod_amrState pointed to by st is updated.
Returns:
return_value = 0 (int)
Global Variables Used:
None.
Local Variables Needed:
None.
------------------------------------------------------------------------------
FUNCTION DESCRIPTION
This function copes with look-ahead and calls cod_amr.
No input argument are passed to this function. However, before
calling this function, 40 new speech data should be copied to the
vector new_speech[]. This is a global pointer which is declared in
this file (it points to the end of speech buffer minus 200).
------------------------------------------------------------------------------
REQUIREMENTS
None.
------------------------------------------------------------------------------
REFERENCES
cod_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
------------------------------------------------------------------------------
PSEUDO-CODE
int cod_amr_first(cod_amrState *st, // i/o : State struct
Word16 new_speech[]) // i : speech input (L_FRAME)
{
Copy(new_speech,&st->new_speech[-L_NEXT], L_NEXT);
// Copy(new_speech,st->new_speech,L_FRAME);
return 0;
}
------------------------------------------------------------------------------
CAUTION [optional]
[State any special notes, constraints or cautions for users of this function]
------------------------------------------------------------------------------
*/
Word16 cod_amr_first(cod_amrState *st, /* i/o : State struct */
Word16 new_speech[]) /* i : speech input (L_FRAME) */
{
oscl_memcpy(&st->new_speech[-L_NEXT], new_speech, L_NEXT*sizeof(Word16));
/* Copy(new_speech,st->new_speech,L_FRAME); */
return(0);
}
/****************************************************************************/
/*
------------------------------------------------------------------------------
FUNCTION NAME: cod_amr
------------------------------------------------------------------------------
INPUT AND OUTPUT DEFINITIONS
Inputs:
st = pointer to a structure of type cod_amrState
mode = AMR mode of type enum Mode
new_speech = pointer to buffer of length L_FRAME that contains
the speech input of type Word16
ana = pointer to the analysis parameters of type Word16
usedMode = pointer to the used mode of type enum Mode
synth = pointer to a buffer containing the local synthesis speech of
type Word16
Outputs:
The structure of type cod_amrState pointed to by st is updated.
The analysis parameter buffer pointed to by ana is updated.
The value pointed to by usedMode is updated.
The local synthesis speech buffer pointed to by synth is updated.
Returns:
return_value = 0 (int)
Global Variables Used:
None.
Local Variables Needed:
None.
------------------------------------------------------------------------------
FUNCTION DESCRIPTION
This function is the main encoder routine. It is called every 20 ms speech
frame, operating on the newly read 160 speech samples. It performs the
principle encoding functions to produce the set of encoded parameters
which include the LSP, adaptive codebook, and fixed codebook
quantization indices (addresses and gains).
Before calling this function, 160 new speech data should be copied to the
vector new_speech[]. This is a global pointer which is declared in
this file (it points to the end of speech buffer minus 160).
The outputs of the function are:
ana[]: vector of analysis parameters.
synth[]: Local synthesis speech (for debugging purposes)
------------------------------------------------------------------------------
REQUIREMENTS
None.
------------------------------------------------------------------------------
REFERENCES
cod_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
------------------------------------------------------------------------------
PSEUDO-CODE
int cod_amr(
cod_amrState *st, // i/o : State struct
enum Mode mode, // i : AMR mode
Word16 new_speech[], // i : speech input (L_FRAME)
Word16 ana[], // o : Analysis parameters
enum Mode *usedMode, // o : used mode
Word16 synth[] // o : Local synthesis
)
{
// LPC coefficients
Word16 A_t[(MP1) * 4]; // A(z) unquantized for the 4 subframes
Word16 Aq_t[(MP1) * 4]; // A(z) quantized for the 4 subframes
Word16 *A, *Aq; // Pointer on A_t and Aq_t
Word16 lsp_new[M];
// Other vectors
Word16 xn[L_SUBFR]; // Target vector for pitch search
Word16 xn2[L_SUBFR]; // Target vector for codebook search
Word16 code[L_SUBFR]; // Fixed codebook excitation
Word16 y1[L_SUBFR]; // Filtered adaptive excitation
Word16 y2[L_SUBFR]; // Filtered fixed codebook excitation
Word16 gCoeff[6]; // Correlations between xn, y1, & y2:
Word16 res[L_SUBFR]; // Short term (LPC) prediction residual
Word16 res2[L_SUBFR]; // Long term (LTP) prediction residual
// Vector and scalars needed for the MR475
Word16 xn_sf0[L_SUBFR]; // Target vector for pitch search
Word16 y2_sf0[L_SUBFR]; // Filtered codebook innovation
Word16 code_sf0[L_SUBFR]; // Fixed codebook excitation
Word16 h1_sf0[L_SUBFR]; // The impulse response of sf0
Word16 mem_syn_save[M]; // Filter memory
Word16 mem_w0_save[M]; // Filter memory
Word16 mem_err_save[M]; // Filter memory
Word16 sharp_save; // Sharpening
Word16 evenSubfr; // Even subframe indicator
Word16 T0_sf0 = 0; // Integer pitch lag of sf0
Word16 T0_frac_sf0 = 0; // Fractional pitch lag of sf0
Word16 i_subfr_sf0 = 0; // Position in exc[] for sf0
Word16 gain_pit_sf0; // Quantized pitch gain for sf0
Word16 gain_code_sf0; // Quantized codebook gain for sf0
// Scalars
Word16 i_subfr, subfrNr;
Word16 T_op[L_FRAME/L_FRAME_BY2];
Word16 T0, T0_frac;
Word16 gain_pit, gain_code;
// Flags
Word16 lsp_flag = 0; // indicates resonance in LPC filter
Word16 gp_limit; // pitch gain limit value
Word16 vad_flag; // VAD decision flag
Word16 compute_sid_flag; // SID analysis flag
Copy(new_speech, st->new_speech, L_FRAME);
*usedMode = mode;
// DTX processing
if (st->dtx)
{ // no test() call since this if is only in simulation env
// Find VAD decision
#ifdef VAD2
vad_flag = vad2 (st->new_speech, st->vadSt);
vad_flag = vad2 (st->new_speech+80, st->vadSt) || vad_flag;
#else
vad_flag = vad1(st->vadSt, st->new_speech);
#endif
// NB! usedMode may change here
compute_sid_flag = tx_dtx_handler(st->dtx_encSt,
vad_flag,
usedMode);
}
else
{
compute_sid_flag = 0;
}
*------------------------------------------------------------------------*
* - Perform LPC analysis: *
* * autocorrelation + lag windowing *
* * Levinson-durbin algorithm to find a[] *
* * convert a[] to lsp[] *
* * quantize and code the LSPs *
* * find the interpolated LSPs and convert to a[] for all *
* subframes (both quantized and unquantized) *
*------------------------------------------------------------------------*
// LP analysis
lpc(st->lpcSt, mode, st->p_window, st->p_window_12k2, A_t);
// From A(z) to lsp. LSP quantization and interpolation
lsp(st->lspSt, mode, *usedMode, A_t, Aq_t, lsp_new, &ana);
// Buffer lsp's and energy
dtx_buffer(st->dtx_encSt,
lsp_new,
st->new_speech);
// Check if in DTX mode
if (sub(*usedMode, MRDTX) == 0)
{
dtx_enc(st->dtx_encSt,
compute_sid_flag,
st->lspSt->qSt,
st->gainQuantSt->gc_predSt,
&ana);
Set_zero(st->old_exc, PIT_MAX + L_INTERPOL);
Set_zero(st->mem_w0, M);
Set_zero(st->mem_err, M);
Set_zero(st->zero, L_SUBFR);
Set_zero(st->hvec, L_SUBFR); // set to zero "h1[-L_SUBFR..-1]"
// Reset lsp states
lsp_reset(st->lspSt);
Copy(lsp_new, st->lspSt->lsp_old, M);
Copy(lsp_new, st->lspSt->lsp_old_q, M);
// Reset clLtp states
cl_ltp_reset(st->clLtpSt);
st->sharp = SHARPMIN;
}
else
{
// check resonance in the filter
lsp_flag = check_lsp(st->tonStabSt, st->lspSt->lsp_old);
}
*----------------------------------------------------------------------*
* - Find the weighted input speech w_sp[] for the whole speech frame *
* - Find the open-loop pitch delay for first 2 subframes *
* - Set the range for searching closed-loop pitch in 1st subframe *
* - Find the open-loop pitch delay for last 2 subframes *
*----------------------------------------------------------------------*
#ifdef VAD2
if (st->dtx)
{ // no test() call since this if is only in simulation env
st->vadSt->L_Rmax = 0;
st->vadSt->L_R0 = 0;
}
#endif
for(subfrNr = 0, i_subfr = 0;
subfrNr < L_FRAME/L_FRAME_BY2;
subfrNr++, i_subfr += L_FRAME_BY2)
{
// Pre-processing on 80 samples
pre_big(mode, gamma1, gamma1_12k2, gamma2, A_t, i_subfr, st->speech,
st->mem_w, st->wsp);
if ((sub(mode, MR475) != 0) && (sub(mode, MR515) != 0))
{
// Find open loop pitch lag for two subframes
ol_ltp(st->pitchOLWghtSt, st->vadSt, mode, &st->wsp[i_subfr],
&T_op[subfrNr], st->old_lags, st->ol_gain_flg, subfrNr,
st->dtx);
}
}
if ((sub(mode, MR475) == 0) || (sub(mode, MR515) == 0))
{
// Find open loop pitch lag for ONE FRAME ONLY
// search on 160 samples
ol_ltp(st->pitchOLWghtSt, st->vadSt, mode, &st->wsp[0], &T_op[0],
st->old_lags, st->ol_gain_flg, 1, st->dtx);
T_op[1] = T_op[0];
}
#ifdef VAD2
if (st->dtx)
{ // no test() call since this if is only in simulation env
LTP_flag_update(st->vadSt, mode);
}
#endif
#ifndef VAD2
// run VAD pitch detection
if (st->dtx)
{ // no test() call since this if is only in simulation env
vad_pitch_detection(st->vadSt, T_op);
}
#endif
if (sub(*usedMode, MRDTX) == 0)
{
goto the_end;
}
*------------------------------------------------------------------------*
* Loop for every subframe in the analysis frame *
*------------------------------------------------------------------------*
* To find the pitch and innovation parameters. The subframe size is *
* L_SUBFR and the loop is repeated L_FRAME/L_SUBFR times. *
* - find the weighted LPC coefficients *
* - find the LPC residual signal res[] *
* - compute the target signal for pitch search *
* - compute impulse response of weighted synthesis filter (h1[]) *
* - find the closed-loop pitch parameters *
* - encode the pitch dealy *
* - update the impulse response h1[] by including fixed-gain pitch *
* - find target vector for codebook search *
* - codebook search *
* - encode codebook address *
* - VQ of pitch and codebook gains *
* - find synthesis speech *
* - update states of weighting filter *
*------------------------------------------------------------------------*
A = A_t; // pointer to interpolated LPC parameters
Aq = Aq_t; // pointer to interpolated quantized LPC parameters
evenSubfr = 0;
subfrNr = -1;
for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
{
subfrNr = add(subfrNr, 1);
evenSubfr = sub(1, evenSubfr);
// Save states for the MR475 mode
if ((evenSubfr != 0) && (sub(*usedMode, MR475) == 0))
{
Copy(st->mem_syn, mem_syn_save, M);
Copy(st->mem_w0, mem_w0_save, M);
Copy(st->mem_err, mem_err_save, M);
sharp_save = st->sharp;
}
*-----------------------------------------------------------------*
* - Preprocessing of subframe *
*-----------------------------------------------------------------*
if (sub(*usedMode, MR475) != 0)
{
subframePreProc(*usedMode, gamma1, gamma1_12k2,
gamma2, A, Aq, &st->speech[i_subfr],
st->mem_err, st->mem_w0, st->zero,
st->ai_zero, &st->exc[i_subfr],
st->h1, xn, res, st->error);
}
else
{ // MR475
subframePreProc(*usedMode, gamma1, gamma1_12k2,
gamma2, A, Aq, &st->speech[i_subfr],
st->mem_err, mem_w0_save, st->zero,
st->ai_zero, &st->exc[i_subfr],
st->h1, xn, res, st->error);
// save impulse response (modified in cbsearch)
if (evenSubfr != 0)
{
Copy (st->h1, h1_sf0, L_SUBFR);
}
}
// copy the LP residual (res2 is modified in the CL LTP search)
Copy (res, res2, L_SUBFR);
*-----------------------------------------------------------------*
* - Closed-loop LTP search *
*-----------------------------------------------------------------*
cl_ltp(st->clLtpSt, st->tonStabSt, *usedMode, i_subfr, T_op, st->h1,
&st->exc[i_subfr], res2, xn, lsp_flag, xn2, y1,
&T0, &T0_frac, &gain_pit, gCoeff, &ana,
&gp_limit);
// update LTP lag history
if ((subfrNr == 0) && (st->ol_gain_flg[0] > 0))
{
st->old_lags[1] = T0;
}
if ((sub(subfrNr, 3) == 0) && (st->ol_gain_flg[1] > 0))
{
st->old_lags[0] = T0;
}