summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/amrnb/enc/src/pitch_ol.cpp
blob: d3a2ec0d6e10ba42513e61948e40401bb399a806 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
/* ------------------------------------------------------------------
 * 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.
****************************************************************************************/
/*
------------------------------------------------------------------------------



 Pathname: ./audio/gsm-amr/c/src/pitch_ol.c
 Funtions: Pitch_ol
           Lag_max

------------------------------------------------------------------------------
 MODULE DESCRIPTION

 The modules in this file compute the open loop pitch lag.
------------------------------------------------------------------------------
*/


/*----------------------------------------------------------------------------
; INCLUDES
----------------------------------------------------------------------------*/
#include <string.h>

#include "pitch_ol.h"
#include "typedef.h"
#include "basicop_malloc.h"
#include "cnst.h"
#include "inv_sqrt.h"
#include "vad.h"
#include "calc_cor.h"
#include "hp_max.h"
#include "basic_op.h"

/*----------------------------------------------------------------------------
; MACROS
; Define module specific macros here
----------------------------------------------------------------------------*/


/*----------------------------------------------------------------------------
; DEFINES
; Include all pre-processor statements here. Include conditional
; compile variables also.
----------------------------------------------------------------------------*/
#define THRESHOLD 27853

/*----------------------------------------------------------------------------
; LOCAL FUNCTION DEFINITIONS
; Function Prototype declaration
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; LOCAL VARIABLE DEFINITIONS
; Variable declaration - defined here and used outside this module
----------------------------------------------------------------------------*/


/*
------------------------------------------------------------------------------
 FUNCTION NAME: Lag_max
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS (If VAD2 is defined)

 Inputs
    corr = pointer to buffer of correlation values (Word32)
    scal_sig = pointer to buffer of scaled signal values (Word16)
    scal_fac = scaled signal factor (Word16)
    scal_flag = EFR compatible scaling flag (Word16)
    L_frame = length of frame to compute pitch (Word16)
    lag_max = maximum lag (Word16)
    lag_min = minimum lag (Word16)
    cor_max = pointer to the normalized correlation of selected lag (Word16)
    rmax = pointer to max(<s[i]*s[j]>), (Word32)
    r0 = pointer to the residual energy (Word32)
    dtx  = dtx flag; equal to 1, if dtx is enabled, 0, otherwise (Flag)

 Outputs:
    cor_max contains the newly calculated normalized correlation of the
      selected lag
    rmax contains the newly calculated max(<s[i]*s[j]>)
    r0 contains the newly calculated residual energy

 Returns:
    p_max = lag of the max correlation found (Word16)

 Global Variables Used:
    None.

 Local Variables Needed:
    None.

------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS (If VAD2 is not defined)

 Inputs
    vadSt = pointer to a vadState structure
    corr = pointer to buffer of correlation values (Word32)
    scal_sig = pointer to buffer of scaled signal values (Word16)
    scal_fac = scaled signal factor (Word16)
    scal_flag = EFR compatible scaling flag (Word16)
    L_frame = length of frame to compute pitch (Word16)
    lag_max = maximum lag (Word16)
    lag_min = minimum lag (Word16)
    cor_max = pointer to the normalized correlation of selected lag (Word16)
    dtx  = dtx flag; equal to 1, if dtx is enabled, 0, otherwise (Flag)
    pOverflow = pointer to overflow indicator (Flag)

 Outputs:
    cor_max contains the newly calculated normalized correlation of the
      selected lag
    vadSt contains the updated VAD state parameters
    pOverflow -> 1 if the math operations called by this routine saturate

 Returns:
    p_max = lag of the max correlation found (Word16)

 Global Variables Used:
    None.

 Local Variables Needed:
    None.

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

 Find the lag that has maximum correlation of scal_sig in a given delay range.
 The correlation is given by:

         cor[t] = <scal_sig[n],scal_sig[n-t]>,  t=lag_min,...,lag_max

 The function returns the maximum correlation after normalization and the
 corresponding lag.

------------------------------------------------------------------------------
 REQUIREMENTS

 None.

------------------------------------------------------------------------------
 REFERENCES

 pitch_ol.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

------------------------------------------------------------------------------
 PSEUDO-CODE

#ifdef VAD2
static Word16 Lag_max ( // o   : lag found
    Word32 corr[],      // i   : correlation vector.
    Word16 scal_sig[],  // i   : scaled signal.
    Word16 scal_fac,    // i   : scaled signal factor.
    Word16 scal_flag,   // i   : if 1 use EFR compatible scaling
    Word16 L_frame,     // i   : length of frame to compute pitch
    Word16 lag_max,     // i   : maximum lag
    Word16 lag_min,     // i   : minimum lag
    Word16 *cor_max,    // o   : normalized correlation of selected lag
    Word32 *rmax,       // o   : max(<s[i]*s[j]>)
    Word32 *r0,         // o   : residual energy
    Flag dtx            // i   : dtx flag; use dtx=1, do not use dtx=0
    )
#else
static Word16 Lag_max ( // o   : lag found
    vadState *vadSt,    // i/o : VAD state struct
    Word32 corr[],      // i   : correlation vector.
    Word16 scal_sig[],  // i   : scaled signal.
    Word16 scal_fac,    // i   : scaled signal factor.
    Word16 scal_flag,   // i   : if 1 use EFR compatible scaling
    Word16 L_frame,     // i   : length of frame to compute pitch
    Word16 lag_max,     // i   : maximum lag
    Word16 lag_min,     // i   : minimum lag
    Word16 *cor_max,    // o   : normalized correlation of selected lag
    Flag dtx            // i   : dtx flag; use dtx=1, do not use dtx=0
    )
#endif
{
    Word16 i, j;
    Word16 *p;
    Word32 max, t0;
    Word16 max_h, max_l, ener_h, ener_l;
    Word16 p_max = 0; // initialization only needed to keep gcc silent

    max = MIN_32;
    p_max = lag_max;

    for (i = lag_max, j = (PIT_MAX-lag_max-1); i >= lag_min; i--, j--)
    {
       if (L_sub (corr[-i], max) >= 0)
       {
          max = corr[-i];
          p_max = i;
       }
    }

    // compute energy

    t0 = 0;
    p = &scal_sig[-p_max];
    for (i = 0; i < L_frame; i++, p++)
    {
        t0 = L_mac (t0, *p, *p);
    }
    // 1/sqrt(energy)

    if (dtx)
    {  // no test() call since this if is only in simulation env
#ifdef VAD2
       *rmax = max;
       *r0 = t0;
#else
       // check tone
       vad_tone_detection (vadSt, max, t0);
#endif
    }

    t0 = Inv_sqrt (t0);

    if (scal_flag)
    {
       t0 = L_shl (t0, 1);
    }

    // max = max/sqrt(energy)

    L_Extract (max, &max_h, &max_l);
    L_Extract (t0, &ener_h, &ener_l);

    t0 = Mpy_32 (max_h, max_l, ener_h, ener_l);

    if (scal_flag)
    {
      t0 = L_shr (t0, scal_fac);
      *cor_max = extract_h (L_shl (t0, 15)); // divide by 2
    }
    else
    {
      *cor_max = extract_l(t0);
    }

    return (p_max);
}

------------------------------------------------------------------------------
 RESOURCES USED [optional]

 When the code is written for a specific target processor the
 the resources used should be documented below.

 HEAP MEMORY USED: x bytes

 STACK MEMORY USED: x bytes

 CLOCK CYCLES: (cycle count equation for this function) + (variable
                used to represent cycle count for each subroutine
                called)
     where: (cycle count variable) = cycle count for [subroutine
                                     name]

------------------------------------------------------------------------------
 CAUTION [optional]
 [State any special notes, constraints or cautions for users of this function]

------------------------------------------------------------------------------
*/

#ifdef VAD2
static Word16 Lag_max(  /* o   : lag found                               */
    Word32 corr[],      /* i   : correlation vector.                     */
    Word16 scal_sig[],  /* i   : scaled signal.                          */
    Word16 scal_fac,    /* i   : scaled signal factor.                   */
    Word16 scal_flag,   /* i   : if 1 use EFR compatible scaling         */
    Word16 L_frame,     /* i   : length of frame to compute pitch        */
    Word16 lag_max,     /* i   : maximum lag                             */
    Word16 lag_min,     /* i   : minimum lag                             */
    Word16 *cor_max,    /* o   : normalized correlation of selected lag  */
    Word32 *rmax,       /* o   : max(<s[i]*s[j]>)                        */
    Word32 *r0,         /* o   : residual energy                         */
    Flag dtx,           /* i   : dtx flag; use dtx=1, do not use dtx=0   */
    Flag *pOverflow     /* i/o : overflow Flag                           */
)
#else
static Word16 Lag_max(  /* o   : lag found                               */
    vadState *vadSt,    /* i/o : VAD state struct                        */
    Word32 corr[],      /* i   : correlation vector.                     */
    Word16 scal_sig[],  /* i   : scaled signal.                          */
    Word16 scal_fac,    /* i   : scaled signal factor.                   */
    Word16 scal_flag,   /* i   : if 1 use EFR compatible scaling         */
    Word16 L_frame,     /* i   : length of frame to compute pitch        */
    Word16 lag_max,     /* i   : maximum lag                             */
    Word16 lag_min,     /* i   : minimum lag                             */
    Word16 *cor_max,    /* o   : normalized correlation of selected lag  */
    Flag dtx,           /* i   : dtx flag; use dtx=1, do not use dtx=0   */
    Flag *pOverflow     /* i/o : overflow Flag                           */
)
#endif
{
    register Word16 i;
    Word16 *p;
    Word32 max;
    Word32 t0;
    Word16 max_h;
    Word16 max_l;
    Word16 ener_h;
    Word16 ener_l;
    Word16 p_max = 0; /* initialization only needed to keep gcc silent */
    Word32 L_temp;
    Word32 L_temp_2;
    Word32 L_temp_3;
    Word32  *p_corr = &corr[-lag_max];

    max = MIN_32;
    p_max = lag_max;

    for (i = lag_max; i >= lag_min; i--)
    {
        /* The negative array index is equivalent to a negative */
        /* address offset, i.e., corr[-i] == *(corr - i)        */
        if (*(p_corr++) >= max)
        {
            p_corr--;
            max = *(p_corr++);
            p_max = i;
        }
    }

    /* compute energy */

    t0 = 0;

    /* The negative array index is equivalent to a negative          */
    /* address offset, i.e., scal_sig[-p_max] == *(scal_sig - p_max) */
    p = &scal_sig[-p_max];
    for (i = (L_frame >> 2); i != 0; i--)
    {
        t0 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p), t0);
        p++;
        t0 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p), t0);
        p++;
        t0 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p), t0);
        p++;
        t0 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p), t0);
        p++;
    }

    t0 <<= 1;
    /* 1/sqrt(energy) */

    if (dtx)
    {  /* no test() call since this if is only in simulation env */
        /* check tone */
#ifdef VAD2
        *rmax = max;
        *r0 = t0;
#else
        /* check tone */
        vad_tone_detection(vadSt, max, t0, pOverflow);
#endif
    }

    t0 = Inv_sqrt(t0, pOverflow);

    if (scal_flag)
    {
        if (t0 > (Word32) 0x3fffffffL)
        {
            t0 = MAX_32;
        }
        else
        {
            t0 = t0 << 1;
        }
    }

    /* max = max/sqrt(energy)  */
    /* The following code is an inlined version of */
    /* L_Extract (max, &max_h, &max_l), i.e.       */
    /*                                             */
    /* *max_h = extract_h (max);                   */
    max_h = (Word16)(max >> 16);

    /* L_temp_2 = L_shr(max,1), which is used in      */
    /* the calculation of *max_l (see next operation) */
    L_temp_2 = max >> 1;

    /* *max_l = extract_l (L_msu (L_shr (max, 1), *max_h, 16384)); */
    L_temp_3 = (Word32)(max_h << 15);

    L_temp = L_temp_2 - L_temp_3;

    max_l = (Word16)L_temp;

    /* The following code is an inlined version of */
    /* L_Extract (t0, &ener_h, &ener_l), i.e.      */
    /*                                             */
    /* *ener_h = extract_h (t0);                   */
    ener_h = (Word16)(t0 >> 16);

    /* L_temp_2 = L_shr(t0,1), which is used in        */
    /* the calculation of *ener_l (see next operation) */

    L_temp_2 = t0 >> 1;

    L_temp_3 = (Word32)(ener_h << 15);

    L_temp = L_temp_2 - L_temp_3;

    ener_l = (Word16)L_temp;

    t0 = Mpy_32(max_h, max_l, ener_h, ener_l, pOverflow);

    if (scal_flag)
    {
        t0 = L_shr(t0, scal_fac, pOverflow);

        if (t0 > (Word32) 0X0000FFFFL)
        {
            *cor_max = MAX_16;
        }
        else if (t0 < (Word32) 0xFFFF0000L)
        {
            *cor_max = MIN_16;
        }
        else
        {
            *cor_max = (Word16)(t0 >> 1);
        }
    }
    else
    {
        *cor_max = (Word16)t0;
    }

    return (p_max);
}

/*----------------------------------------------------------------------------
; End Function: Lag_max
----------------------------------------------------------------------------*/


/*
------------------------------------------------------------------------------
 FUNCTION NAME: Lag_max_wrapper
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs
    corr = pointer to buffer of correlation values (Word32)
    scal_sig = pointer to buffer of scaled signal values (Word16)
    scal_fac = scaled signal factor (Word16)
    scal_flag = EFR compatible scaling flag (Word16)
    L_frame = length of frame to compute pitch (Word16)
    lag_max = maximum lag (Word16)
    lag_min = minimum lag (Word16)
    cor_max = pointer to the normalized correlation of selected lag (Word16)
    rmax = pointer to max(<s[i]*s[j]>), (Word32)
    r0 = pointer to the residual energy (Word32)
    dtx  = dtx flag; equal to 1, if dtx is enabled, 0, otherwise (Flag)
    pOverflow = pointer to overflow indicator (Flag)

 Outputs:
    cor_max contains the newly calculated normalized correlation of the
      selected lag
    rmax contains the newly calculated max(<s[i]*s[j]>)
    r0 contains the newly calculated residual energy
    pOverflow -> 1 if the math operations called by this routine saturate

 Returns:
    p_max = lag of the max correlation found (Word16)

 Global Variables Used:
    None.

 Local Variables Needed:
    None.

------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS (If VAD2 is not defined)

 Inputs
    vadSt = pointer to a vadState structure
    corr = pointer to buffer of correlation values (Word32)
    scal_sig = pointer to buffer of scaled signal values (Word16)
    scal_fac = scaled signal factor (Word16)
    scal_flag = EFR compatible scaling flag (Word16)
    L_frame = length of frame to compute pitch (Word16)
    lag_max = maximum lag (Word16)
    lag_min = minimum lag (Word16)
    cor_max = pointer to the normalized correlation of selected lag (Word16)
    dtx  = dtx flag; equal to 1, if dtx is enabled, 0, otherwise (Flag)
    pOverflow = pointer to overflow indicator (Flag)

 Outputs:
    cor_max contains the newly calculated normalized correlation of the
      selected lag
    vadSt contains the updated VAD state parameters
    pOverflow -> 1 if the math operations called by this routine saturate

 Returns:
    p_max = lag of the max correlation found (Word16)

 Global Variables Used:
    None.

 Local Variables Needed:
    None.

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

 This function provides external access to the local function Lag_max.

------------------------------------------------------------------------------
 REQUIREMENTS

 None

------------------------------------------------------------------------------
 REFERENCES

 pitch_ol.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

------------------------------------------------------------------------------
 PSEUDO-CODE

#ifdef VAD2
 CALL Lag_max(corr = corr
          scal_sig = scal_sig
          scal_fac = scal_fac
          scal_flag = scal_flag
          L_frame = L_frame
          lag_max = lag_max
          lag_min = lag_min
          cor_max = cor_max
          rmax = rmax
          r0 = r0
          dtx = dtx
          pOverflow = pOverflow)
   MODIFYING(nothing)
   RETURNING(temp)

#else
 CALL Lag_max(vadSt = vadSt
          corr = corr
          scal_sig = scal_sig
          scal_fac = scal_fac
          scal_flag = scal_flag
          L_frame = L_frame
          lag_max = lag_max
          lag_min = lag_min
          cor_max = cor_max
          dtx = dtx
          pOverflow = pOverflow)
   MODIFYING(nothing)
   RETURNING(temp)

#endif

------------------------------------------------------------------------------
 RESOURCES USED [optional]

 When the code is written for a specific target processor the
 the resources used should be documented below.

 HEAP MEMORY USED: x bytes

 STACK MEMORY USED: x bytes

 CLOCK CYCLES: (cycle count equation for this function) + (variable
                used to represent cycle count for each subroutine
                called)
     where: (cycle count variable) = cycle count for [subroutine
                                     name]

------------------------------------------------------------------------------
 CAUTION [optional]
 [State any special notes, constraints or cautions for users of this function]

------------------------------------------------------------------------------
*/

#ifdef VAD2
Word16 Lag_max_wrapper(  /* o   : lag found                          */
    Word32 corr[],      /* i   : correlation vector.                     */
    Word16 scal_sig[],  /* i   : scaled signal.                          */
    Word16 scal_fac,    /* i   : scaled signal factor.                   */
    Word16 scal_flag,   /* i   : if 1 use EFR compatible scaling         */
    Word16 L_frame,     /* i   : length of frame to compute pitch        */
    Word16 lag_max,     /* i   : maximum lag                             */
    Word16 lag_min,     /* i   : minimum lag                             */
    Word16 *cor_max,    /* o   : normalized correlation of selected lag  */
    Word32 *rmax,       /* o   : max(<s[i]*s[j]>)                        */
    Word32 *r0,         /* o   : residual energy                         */
    Flag dtx,           /* i   : dtx flag; use dtx=1, do not use dtx=0   */
    Flag *pOverflow     /* i/o : overflow Flag                           */
)
{
    Word16 temp;

    temp = Lag_max(corr, scal_sig, scal_fac, scal_flag, L_frame, lag_max,
                   lag_min, cor_max, rmax, r0, dtx, pOverflow);

    return(temp);
}

#else
Word16 Lag_max_wrapper(  /* o   : lag found                          */
    vadState *vadSt,    /* i/o : VAD state struct                        */
    Word32 corr[],      /* i   : correlation vector.                     */
    Word16 scal_sig[],  /* i   : scaled signal.                          */
    Word16 scal_fac,    /* i   : scaled signal factor.                   */
    Word16 scal_flag,   /* i   : if 1 use EFR compatible scaling         */
    Word16 L_frame,     /* i   : length of frame to compute pitch        */
    Word16 lag_max,     /* i   : maximum lag                             */
    Word16 lag_min,     /* i   : minimum lag                             */
    Word16 *cor_max,    /* o   : normalized correlation of selected lag  */
    Flag dtx,           /* i   : dtx flag; use dtx=1, do not use dtx=0   */
    Flag *pOverflow     /* i/o : overflow Flag                           */
)
{
    Word16 temp;

    temp = Lag_max(vadSt, corr, scal_sig, scal_fac, scal_flag, L_frame,
                   lag_max, lag_min, cor_max, dtx, pOverflow);

    return(temp);
}

#endif

/*----------------------------------------------------------------------------
; End Function: Lag_max_wrapper
----------------------------------------------------------------------------*/

/*
------------------------------------------------------------------------------
 FUNCTION NAME: Pitch_ol
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    vadSt = pointer to a vadState structure
    mode =  data of type enum Mode specifies the mode.
    signal = pointer to buffer of signal used to compute the open loop
         pitch
    where signal[-pit_max] to signal[-1] should be known
    pit_min = 16 bit value specifies the minimum pitch lag
    pit_max = 16 bit value specifies the maximum pitch lag
    L_frame = 16 bit value specifies the length of frame to compute pitch
    idx = 16 bit value specifies the frame index
    dtx = Data of type 'Flag' used for dtx. Use dtx=1, do not use dtx=0
    pOverflow = pointer to overflow indicator (Flag)

 Outputs
    vadSt = The vadSt state structure may be modified.
    pOverflow -> 1 if the math operations called by this routine saturate

 Returns:
    p_max1 = 16 bit value representing the open loop pitch lag.

 Global Variables Used:
    None.

 Local Variables Needed:
    None.

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

 This function computes the open loop pitch lag based on the perceptually
 weighted speech signal. This is done in the following steps:
       - find three maxima of the correlation <sw[n],sw[n-T]>,
         dividing the search range into three parts:
              pit_min ... 2*pit_min-1
            2*pit_min ... 4*pit_min-1
            4*pit_min ...   pit_max
       - divide each maximum by <sw[n-t], sw[n-t]> where t is the delay at
         that maximum correlation.
       - select the delay of maximum normalized correlation (among the
         three candidates) while favoring the lower delay ranges.


------------------------------------------------------------------------------
 REQUIREMENTS

 None.

------------------------------------------------------------------------------
 REFERENCES

 pitch_ol.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

------------------------------------------------------------------------------
 PSEUDO-CODE

Word16 Pitch_ol (      // o   : open loop pitch lag
    vadState *vadSt,   // i/o : VAD state struct
    enum Mode mode,    // i   : coder mode
    Word16 signal[],   // i   : signal used to compute the open loop pitch
                       //    signal[-pit_max] to signal[-1] should be known
    Word16 pit_min,    // i   : minimum pitch lag
    Word16 pit_max,    // i   : maximum pitch lag
    Word16 L_frame,    // i   : length of frame to compute pitch
    Word16 idx,        // i   : frame index
    Flag dtx           // i   : dtx flag; use dtx=1, do not use dtx=0
    )
{
    Word16 i, j;
    Word16 max1, max2, max3;
    Word16 p_max1, p_max2, p_max3;
    Word16 scal_flag = 0;
    Word32 t0;
#ifdef VAD2
    Word32  r01, r02, r03;
    Word32  rmax1, rmax2, rmax3;
#else
    Word16 corr_hp_max;
#endif
    Word32 corr[PIT_MAX+1], *corr_ptr;

    // Scaled signal

    Word16 scaled_signal[L_FRAME + PIT_MAX];
    Word16 *scal_sig, scal_fac;

#ifndef VAD2
    if (dtx)
    {  // no test() call since this if is only in simulation env
       // update tone detection
       if ((sub(mode, MR475) == 0) || (sub(mode, MR515) == 0))
       {
          vad_tone_detection_update (vadSt, 1);
       }
       else
       {
          vad_tone_detection_update (vadSt, 0);
       }
    }
#endif

    scal_sig = &scaled_signal[pit_max];

    t0 = 0L;
    for (i = -pit_max; i < L_frame; i++)
    {
        t0 = L_mac (t0, signal[i], signal[i]);
    }

     *--------------------------------------------------------*
     * Scaling of input signal.                               *
     *                                                        *
     *   if Overflow        -> scal_sig[i] = signal[i]>>3     *
     *   else if t0 < 1^20  -> scal_sig[i] = signal[i]<<3     *
     *   else               -> scal_sig[i] = signal[i]        *
     *--------------------------------------------------------*

     *--------------------------------------------------------*
     *  Verification for risk of overflow.                    *
     *--------------------------------------------------------*

    if (L_sub (t0, MAX_32) == 0L)               // Test for overflow
    {
        for (i = -pit_max; i < L_frame; i++)
        {
            scal_sig[i] = shr (signal[i], 3);
        }
        scal_fac = 3;
    }
    else if (L_sub (t0, (Word32) 1048576L) < (Word32) 0)
        // if (t0 < 2^20)
    {
        for (i = -pit_max; i < L_frame; i++)
        {
            scal_sig[i] = shl (signal[i], 3);
        }
        scal_fac = -3;
    }
    else
    {
        for (i = -pit_max; i < L_frame; i++)
        {
            scal_sig[i] = signal[i];
        }
        scal_fac = 0;
    }

    // calculate all coreelations of scal_sig, from pit_min to pit_max
    corr_ptr = &corr[pit_max];
    comp_corr (scal_sig, L_frame, pit_max, pit_min, corr_ptr);

     *--------------------------------------------------------------------*
     *  The pitch lag search is divided in three sections.                *
     *  Each section cannot have a pitch multiple.                        *
     *  We find a maximum for each section.                               *
     *  We compare the maximum of each section by favoring small lags.    *
     *                                                                    *
     *  First section:  lag delay = pit_max     downto 4*pit_min          *
     *  Second section: lag delay = 4*pit_min-1 downto 2*pit_min          *
     *  Third section:  lag delay = 2*pit_min-1 downto pit_min            *
     *--------------------------------------------------------------------*

    // mode dependent scaling in Lag_max
    if (sub(mode, MR122) == 0)
    {
       scal_flag = 1;
    }
    else
    {
       scal_flag = 0;
    }

#ifdef VAD2
    j = shl (pit_min, 2);
    p_max1 = Lag_max (corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
                      pit_max, j, &max1, &rmax1, &r01, dtx);

    i = sub (j, 1);
    j = shl (pit_min, 1);
    p_max2 = Lag_max (corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
                      i, j, &max2, &rmax2, &r02, dtx);

    i = sub (j, 1);
    p_max3 = Lag_max (corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
                      i, pit_min, &max3, &rmax3, &r03, dtx);
#else
    j = shl (pit_min, 2);
    p_max1 = Lag_max (vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
                      pit_max, j, &max1, dtx);

    i = sub (j, 1);
    j = shl (pit_min, 1);
    p_max2 = Lag_max (vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
                      i, j, &max2, dtx);

    i = sub (j, 1);
    p_max3 = Lag_max (vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
                      i, pit_min, &max3, dtx);

    if (dtx)
    {  // no test() call since this if is only in simulation env
       if (sub(idx, 1) == 0)
       {
          // calculate max high-passed filtered correlation of all lags
          hp_max (corr_ptr, scal_sig, L_frame, pit_max, pit_min, &corr_hp_max);

          // update complex background detector
          vad_complex_detection_update(vadSt, corr_hp_max);
       }
    }
#endif

     *--------------------------------------------------------------------*
     * Compare the 3 sections maximum, and favor small lag.               *
     *--------------------------------------------------------------------*

    if (sub (mult (max1, THRESHOLD), max2) < 0)
    {
        max1 = max2;
        p_max1 = p_max2;
#ifdef VAD2
        if (dtx)
        {
            rmax1 = rmax2;
            r01 = r02;
#endif
    }
    if (sub (mult (max1, THRESHOLD), max3) < 0)
    {
        p_max1 = p_max3;
#ifdef VAD2
        if (dtx)
        {
            rmax1 = rmax3;
            r01 = r03;
        }
#endif
    }

#ifdef VAD2
    if (dtx)
    {
        vadSt->L_Rmax = L_add(vadSt->L_Rmax, rmax1);   // Save max correlation
        vadSt->L_R0 =   L_add(vadSt->L_R0, r01);        // Save max energy
    }
#endif

    return (p_max1);
}

------------------------------------------------------------------------------
 RESOURCES USED [optional]

 When the code is written for a specific target processor the
 the resources used should be documented below.

 HEAP MEMORY USED: x bytes

 STACK MEMORY USED: x bytes

 CLOCK CYCLES: (cycle count equation for this function) + (variable
                used to represent cycle count for each subroutine
                called)
     where: (cycle count variable) = cycle count for [subroutine
                                     name]

------------------------------------------------------------------------------
 CAUTION [optional]
 [State any special notes, constraints or cautions for users of this function]

------------------------------------------------------------------------------
*/

Word16 Pitch_ol(       /* o   : open loop pitch lag                         */
    vadState *vadSt,   /* i/o : VAD state struct                            */
    enum Mode mode,    /* i   : coder mode                                  */
    Word16 signal[],   /* i   : signal used to compute the open loop pitch  */
    /*    signal[-pit_max] to signal[-1] should be known */
    Word16 pit_min,    /* i   : minimum pitch lag                           */
    Word16 pit_max,    /* i   : maximum pitch lag                           */
    Word16 L_frame,    /* i   : length of frame to compute pitch            */
    Word16 idx,        /* i   : frame index                                 */
    Flag dtx,          /* i   : dtx flag; use dtx=1, do not use dtx=0       */
    Flag *pOverflow    /* i/o : overflow Flag                               */
)
{
    Word16 i;
    Word16 j;
    Word16 max1;
    Word16 max2;
    Word16 max3;
    Word16 p_max1;
    Word16 p_max2;
    Word16 p_max3;
    Word16 scal_flag = 0;
    Word32 t0;

#ifdef VAD2
    Word32 r01;
    Word32 r02;
    Word32 r03;
    Word32 rmax1;
    Word32 rmax2;
    Word32 rmax3;
#else
    Word16 corr_hp_max;
#endif
    Word32 corr[PIT_MAX+1];
    Word32 *corr_ptr;

    /* Scaled signal */

    Word16 scaled_signal[L_FRAME + PIT_MAX];
    Word16 *scal_sig;
    Word16 *p_signal;
    Word16 scal_fac;
    Word32 L_temp;

#ifndef VAD2
    if (dtx)
    {   /* no test() call since this if is only in simulation env */
        /* update tone detection */
        if ((mode == MR475) || (mode == MR515))
        {
            vad_tone_detection_update(vadSt, 1, pOverflow);
        }
        else
        {
            vad_tone_detection_update(vadSt, 0, pOverflow);
        }
    }
#endif


    t0 = 0L;
    p_signal = &signal[-pit_max];

    for (i = -pit_max; i < L_frame; i++)
    {
        t0 += (((Word32) * (p_signal)) * *(p_signal)) << 1;
        p_signal++;
        if (t0 < 0)
        {
            t0 = MAX_32;
            break;
        }

    }

    /*--------------------------------------------------------*
     * Scaling of input signal.                               *
     *                                                        *
     *   if Overflow        -> scal_sig[i] = signal[i]>>3     *
     *   else if t0 < 1^20  -> scal_sig[i] = signal[i]<<3     *
     *   else               -> scal_sig[i] = signal[i]        *
     *--------------------------------------------------------*/

    /*--------------------------------------------------------*
     *  Verification for risk of overflow.                    *
     *--------------------------------------------------------*/

    scal_sig = &scaled_signal[0];
    p_signal = &signal[-pit_max];

    if (t0 == MAX_32)     /* Test for overflow */
    {

        for (i = (pit_max + L_frame) >> 1; i != 0; i--)
        {
            *(scal_sig++) = (Word16)(((Word32) * (p_signal++) >> 3));
            *(scal_sig++) = (Word16)(((Word32) * (p_signal++) >> 3));
        }

        if ((pit_max + L_frame) & 1)
        {
            *(scal_sig) = (Word16)(((Word32) * (p_signal) >> 3));
        }

        scal_fac = 3;
    }
    else if (t0 < (Word32)1048576L)
        /* if (t0 < 2^20) */
    {
        for (i = (pit_max + L_frame) >> 1; i != 0; i--)
        {
            *(scal_sig++) = (Word16)(((Word32) * (p_signal++) << 3));
            *(scal_sig++) = (Word16)(((Word32) * (p_signal++) << 3));
        }

        if ((pit_max + L_frame) & 1)
        {
            *(scal_sig) = (Word16)(((Word32) * (p_signal) << 3));
        }
        scal_fac = -3;
    }
    else
    {

        memcpy(scal_sig, p_signal, (L_frame + pit_max)*sizeof(*signal));
        scal_fac = 0;
    }

    /* calculate all coreelations of scal_sig, from pit_min to pit_max */
    corr_ptr = &corr[pit_max];

    scal_sig = &scaled_signal[pit_max];

    comp_corr(scal_sig, L_frame, pit_max, pit_min, corr_ptr);

    /*--------------------------------------------------------------------*
     *  The pitch lag search is divided in three sections.                *
     *  Each section cannot have a pitch multiple.                        *
     *  We find a maximum for each section.                               *
     *  We compare the maximum of each section by favoring small lags.    *
     *                                                                    *
     *  First section:  lag delay = pit_max     downto 4*pit_min          *
     *  Second section: lag delay = 4*pit_min-1 downto 2*pit_min          *
     *  Third section:  lag delay = 2*pit_min-1 downto pit_min            *
     *--------------------------------------------------------------------*/

    /* mode dependent scaling in Lag_max */

    if (mode == MR122)
    {
        scal_flag = 1;
    }
    else
    {
        scal_flag = 0;
    }

#ifdef VAD2
    L_temp = ((Word32)pit_min) << 2;
    if (L_temp != (Word32)((Word16) L_temp))
    {
        *pOverflow = 1;
        j = (pit_min > 0) ? MAX_16 : MIN_16;
    }
    else
    {
        j = (Word16)L_temp;
    }

    p_max1 = Lag_max(corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
                     pit_max, j, &max1, &rmax1, &r01, dtx, pOverflow);

    i = j - 1;

    j = pit_min << 1;

    p_max2 = Lag_max(corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
                     i, j, &max2, &rmax2, &r02, dtx, pOverflow);

    i = j - 1;

    p_max3 = Lag_max(corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
                     i, pit_min, &max3, &rmax3, &r03, dtx, pOverflow);

#else
    L_temp = ((Word32)pit_min) << 2;
    if (L_temp != (Word32)((Word16) L_temp))
    {
        *pOverflow = 1;
        j = (pit_min > 0) ? MAX_16 : MIN_16;
    }
    else
    {
        j = (Word16)L_temp;
    }

    p_max1 = Lag_max(vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
                     pit_max, j, &max1, dtx, pOverflow);

    i = j - 1;


    j = pit_min << 1;


    p_max2 = Lag_max(vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
                     i, j, &max2, dtx, pOverflow);

    i = j - 1;
    p_max3 = Lag_max(vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
                     i, pit_min, &max3, dtx, pOverflow);

    if (dtx)
    {  /* no test() call since this if is only in simulation env */

        if (idx == 1)
        {
            /* calculate max high-passed filtered correlation of all lags */
            hp_max(corr_ptr, scal_sig, L_frame, pit_max, pit_min, &corr_hp_max,
                   pOverflow);

            /* update complex background detector */
            vad_complex_detection_update(vadSt, corr_hp_max);
        }
    }
#endif

    /*--------------------------------------------------------------------*
     * Compare the 3 sections maximum, and favor small lag.               *
     *--------------------------------------------------------------------*/

    i =  mult(max1, THRESHOLD, pOverflow);

    if (i < max2)
    {
        max1 = max2;
        p_max1 = p_max2;

#ifdef VAD2
        if (dtx)
        {
            rmax1 = rmax2;
            r01 = r02;
        }
#endif
    }

    i =  mult(max1, THRESHOLD, pOverflow);

    if (i < max3)
    {
        p_max1 = p_max3;

#ifdef VAD2
        if (dtx)
        {
            rmax1 = rmax3;
            r01 = r03;
        }
#endif
    }

#ifdef VAD2
    if (dtx)
    {
        /* Save max correlation */
        vadSt->L_Rmax = L_add(vadSt->L_Rmax, rmax1, pOverflow);
        /* Save max energy */
        vadSt->L_R0 =   L_add(vadSt->L_R0, r01, pOverflow);
    }
#endif

    return (p_max1);
}