Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / pitch_ol.cpp
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
20
21     3GPP TS 26.073
22     ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23     Available from http://www.3gpp.org
24
25 (C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26 Permission to distribute, modify and use this file under the standard license
27 terms listed above has been obtained from the copyright holder.
28 ****************************************************************************************/
29 /*
30 ------------------------------------------------------------------------------
31
32
33
34  Filename: pitch_ol.cpp
35  Funtions: Pitch_ol
36            Lag_max
37
38 ------------------------------------------------------------------------------
39  MODULE DESCRIPTION
40
41  The modules in this file compute the open loop pitch lag.
42 ------------------------------------------------------------------------------
43 */
44
45
46 /*----------------------------------------------------------------------------
47 ; INCLUDES
48 ----------------------------------------------------------------------------*/
49 #include "pitch_ol.h"
50 #include "typedef.h"
51 #include "basicop_malloc.h"
52 #include "cnst.h"
53 #include "inv_sqrt.h"
54 #include "vad.h"
55 #include "calc_cor.h"
56 #include "hp_max.h"
57 #include "oscl_mem.h"
58 #include "basic_op.h"
59
60 /*----------------------------------------------------------------------------
61 ; MACROS
62 ; Define module specific macros here
63 ----------------------------------------------------------------------------*/
64
65
66 /*----------------------------------------------------------------------------
67 ; DEFINES
68 ; Include all pre-processor statements here. Include conditional
69 ; compile variables also.
70 ----------------------------------------------------------------------------*/
71 #define THRESHOLD 27853
72
73 /*----------------------------------------------------------------------------
74 ; LOCAL FUNCTION DEFINITIONS
75 ; Function Prototype declaration
76 ----------------------------------------------------------------------------*/
77
78 /*----------------------------------------------------------------------------
79 ; LOCAL VARIABLE DEFINITIONS
80 ; Variable declaration - defined here and used outside this module
81 ----------------------------------------------------------------------------*/
82
83
84 /*
85 ------------------------------------------------------------------------------
86  FUNCTION NAME: Lag_max
87 ------------------------------------------------------------------------------
88  INPUT AND OUTPUT DEFINITIONS (If VAD2 is defined)
89
90  Inputs
91     corr = pointer to buffer of correlation values (Word32)
92     scal_sig = pointer to buffer of scaled signal values (Word16)
93     scal_fac = scaled signal factor (Word16)
94     scal_flag = EFR compatible scaling flag (Word16)
95     L_frame = length of frame to compute pitch (Word16)
96     lag_max = maximum lag (Word16)
97     lag_min = minimum lag (Word16)
98     cor_max = pointer to the normalized correlation of selected lag (Word16)
99     rmax = pointer to max(<s[i]*s[j]>), (Word32)
100     r0 = pointer to the residual energy (Word32)
101     dtx  = dtx flag; equal to 1, if dtx is enabled, 0, otherwise (Flag)
102
103  Outputs:
104     cor_max contains the newly calculated normalized correlation of the
105       selected lag
106     rmax contains the newly calculated max(<s[i]*s[j]>)
107     r0 contains the newly calculated residual energy
108
109  Returns:
110     p_max = lag of the max correlation found (Word16)
111
112  Global Variables Used:
113     None.
114
115  Local Variables Needed:
116     None.
117
118 ------------------------------------------------------------------------------
119  INPUT AND OUTPUT DEFINITIONS (If VAD2 is not defined)
120
121  Inputs
122     vadSt = pointer to a vadState structure
123     corr = pointer to buffer of correlation values (Word32)
124     scal_sig = pointer to buffer of scaled signal values (Word16)
125     scal_fac = scaled signal factor (Word16)
126     scal_flag = EFR compatible scaling flag (Word16)
127     L_frame = length of frame to compute pitch (Word16)
128     lag_max = maximum lag (Word16)
129     lag_min = minimum lag (Word16)
130     cor_max = pointer to the normalized correlation of selected lag (Word16)
131     dtx  = dtx flag; equal to 1, if dtx is enabled, 0, otherwise (Flag)
132     pOverflow = pointer to overflow indicator (Flag)
133
134  Outputs:
135     cor_max contains the newly calculated normalized correlation of the
136       selected lag
137     vadSt contains the updated VAD state parameters
138     pOverflow -> 1 if the math operations called by this routine saturate
139
140  Returns:
141     p_max = lag of the max correlation found (Word16)
142
143  Global Variables Used:
144     None.
145
146  Local Variables Needed:
147     None.
148
149 ------------------------------------------------------------------------------
150  FUNCTION DESCRIPTION
151
152  Find the lag that has maximum correlation of scal_sig in a given delay range.
153  The correlation is given by:
154
155          cor[t] = <scal_sig[n],scal_sig[n-t]>,  t=lag_min,...,lag_max
156
157  The function returns the maximum correlation after normalization and the
158  corresponding lag.
159
160 ------------------------------------------------------------------------------
161  REQUIREMENTS
162
163  None.
164
165 ------------------------------------------------------------------------------
166  REFERENCES
167
168  pitch_ol.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
169
170 ------------------------------------------------------------------------------
171  PSEUDO-CODE
172
173 #ifdef VAD2
174 static Word16 Lag_max ( // o   : lag found
175     Word32 corr[],      // i   : correlation vector.
176     Word16 scal_sig[],  // i   : scaled signal.
177     Word16 scal_fac,    // i   : scaled signal factor.
178     Word16 scal_flag,   // i   : if 1 use EFR compatible scaling
179     Word16 L_frame,     // i   : length of frame to compute pitch
180     Word16 lag_max,     // i   : maximum lag
181     Word16 lag_min,     // i   : minimum lag
182     Word16 *cor_max,    // o   : normalized correlation of selected lag
183     Word32 *rmax,       // o   : max(<s[i]*s[j]>)
184     Word32 *r0,         // o   : residual energy
185     Flag dtx            // i   : dtx flag; use dtx=1, do not use dtx=0
186     )
187 #else
188 static Word16 Lag_max ( // o   : lag found
189     vadState *vadSt,    // i/o : VAD state struct
190     Word32 corr[],      // i   : correlation vector.
191     Word16 scal_sig[],  // i   : scaled signal.
192     Word16 scal_fac,    // i   : scaled signal factor.
193     Word16 scal_flag,   // i   : if 1 use EFR compatible scaling
194     Word16 L_frame,     // i   : length of frame to compute pitch
195     Word16 lag_max,     // i   : maximum lag
196     Word16 lag_min,     // i   : minimum lag
197     Word16 *cor_max,    // o   : normalized correlation of selected lag
198     Flag dtx            // i   : dtx flag; use dtx=1, do not use dtx=0
199     )
200 #endif
201 {
202     Word16 i, j;
203     Word16 *p;
204     Word32 max, t0;
205     Word16 max_h, max_l, ener_h, ener_l;
206     Word16 p_max = 0; // initialization only needed to keep gcc silent
207
208     max = MIN_32;
209     p_max = lag_max;
210
211     for (i = lag_max, j = (PIT_MAX-lag_max-1); i >= lag_min; i--, j--)
212     {
213        if (L_sub (corr[-i], max) >= 0)
214        {
215           max = corr[-i];
216           p_max = i;
217        }
218     }
219
220     // compute energy
221
222     t0 = 0;
223     p = &scal_sig[-p_max];
224     for (i = 0; i < L_frame; i++, p++)
225     {
226         t0 = L_mac (t0, *p, *p);
227     }
228     // 1/sqrt(energy)
229
230     if (dtx)
231     {  // no test() call since this if is only in simulation env
232 #ifdef VAD2
233        *rmax = max;
234        *r0 = t0;
235 #else
236        // check tone
237        vad_tone_detection (vadSt, max, t0);
238 #endif
239     }
240
241     t0 = Inv_sqrt (t0);
242
243     if (scal_flag)
244     {
245        t0 = L_shl (t0, 1);
246     }
247
248     // max = max/sqrt(energy)
249
250     L_Extract (max, &max_h, &max_l);
251     L_Extract (t0, &ener_h, &ener_l);
252
253     t0 = Mpy_32 (max_h, max_l, ener_h, ener_l);
254
255     if (scal_flag)
256     {
257       t0 = L_shr (t0, scal_fac);
258       *cor_max = extract_h (L_shl (t0, 15)); // divide by 2
259     }
260     else
261     {
262       *cor_max = extract_l(t0);
263     }
264
265     return (p_max);
266 }
267
268 ------------------------------------------------------------------------------
269  CAUTION [optional]
270  [State any special notes, constraints or cautions for users of this function]
271
272 ------------------------------------------------------------------------------
273 */
274
275 #ifdef VAD2
276 static Word16 Lag_max(  /* o   : lag found                               */
277     Word32 corr[],      /* i   : correlation vector.                     */
278     Word16 scal_sig[],  /* i   : scaled signal.                          */
279     Word16 scal_fac,    /* i   : scaled signal factor.                   */
280     Word16 scal_flag,   /* i   : if 1 use EFR compatible scaling         */
281     Word16 L_frame,     /* i   : length of frame to compute pitch        */
282     Word16 lag_max,     /* i   : maximum lag                             */
283     Word16 lag_min,     /* i   : minimum lag                             */
284     Word16 *cor_max,    /* o   : normalized correlation of selected lag  */
285     Word32 *rmax,       /* o   : max(<s[i]*s[j]>)                        */
286     Word32 *r0,         /* o   : residual energy                         */
287     Flag dtx,           /* i   : dtx flag; use dtx=1, do not use dtx=0   */
288     Flag *pOverflow     /* i/o : overflow Flag                           */
289 )
290 #else
291 static Word16 Lag_max(  /* o   : lag found                               */
292     vadState *vadSt,    /* i/o : VAD state struct                        */
293     Word32 corr[],      /* i   : correlation vector.                     */
294     Word16 scal_sig[],  /* i   : scaled signal.                          */
295     Word16 scal_fac,    /* i   : scaled signal factor.                   */
296     Word16 scal_flag,   /* i   : if 1 use EFR compatible scaling         */
297     Word16 L_frame,     /* i   : length of frame to compute pitch        */
298     Word16 lag_max,     /* i   : maximum lag                             */
299     Word16 lag_min,     /* i   : minimum lag                             */
300     Word16 *cor_max,    /* o   : normalized correlation of selected lag  */
301     Flag dtx,           /* i   : dtx flag; use dtx=1, do not use dtx=0   */
302     Flag *pOverflow     /* i/o : overflow Flag                           */
303 )
304 #endif
305 {
306     register Word16 i;
307     Word16 *p;
308     Word32 max;
309     Word32 t0;
310     Word16 max_h;
311     Word16 max_l;
312     Word16 ener_h;
313     Word16 ener_l;
314     Word16 p_max = 0; /* initialization only needed to keep gcc silent */
315     Word32 L_temp;
316     Word32 L_temp_2;
317     Word32 L_temp_3;
318     Word32  *p_corr = &corr[-lag_max];
319
320     max = MIN_32;
321     p_max = lag_max;
322
323     for (i = lag_max; i >= lag_min; i--)
324     {
325         /* The negative array index is equivalent to a negative */
326         /* address offset, i.e., corr[-i] == *(corr - i)        */
327         if (*(p_corr++) >= max)
328         {
329             p_corr--;
330             max = *(p_corr++);
331             p_max = i;
332         }
333     }
334
335     /* compute energy */
336
337     t0 = 0;
338
339     /* The negative array index is equivalent to a negative          */
340     /* address offset, i.e., scal_sig[-p_max] == *(scal_sig - p_max) */
341     p = &scal_sig[-p_max];
342     for (i = (L_frame >> 2); i != 0; i--)
343     {
344         t0 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p), t0);
345         p++;
346         t0 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p), t0);
347         p++;
348         t0 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p), t0);
349         p++;
350         t0 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p), t0);
351         p++;
352     }
353
354     t0 <<= 1;
355     /* 1/sqrt(energy) */
356
357     if (dtx)
358     {  /* no test() call since this if is only in simulation env */
359         /* check tone */
360 #ifdef VAD2
361         *rmax = max;
362         *r0 = t0;
363 #else
364         /* check tone */
365         vad_tone_detection(vadSt, max, t0, pOverflow);
366 #endif
367     }
368
369     t0 = Inv_sqrt(t0, pOverflow);
370
371     if (scal_flag)
372     {
373         if (t0 > (Word32) 0x3fffffffL)
374         {
375             t0 = MAX_32;
376         }
377         else
378         {
379             t0 = t0 << 1;
380         }
381     }
382
383     /* max = max/sqrt(energy)  */
384     /* The following code is an inlined version of */
385     /* L_Extract (max, &max_h, &max_l), i.e.       */
386     /*                                             */
387     /* *max_h = extract_h (max);                   */
388     max_h = (Word16)(max >> 16);
389
390     /* L_temp_2 = L_shr(max,1), which is used in      */
391     /* the calculation of *max_l (see next operation) */
392     L_temp_2 = max >> 1;
393
394     /* *max_l = extract_l (L_msu (L_shr (max, 1), *max_h, 16384)); */
395     L_temp_3 = (Word32)(max_h << 15);
396
397     L_temp = L_temp_2 - L_temp_3;
398
399     max_l = (Word16)L_temp;
400
401     /* The following code is an inlined version of */
402     /* L_Extract (t0, &ener_h, &ener_l), i.e.      */
403     /*                                             */
404     /* *ener_h = extract_h (t0);                   */
405     ener_h = (Word16)(t0 >> 16);
406
407     /* L_temp_2 = L_shr(t0,1), which is used in        */
408     /* the calculation of *ener_l (see next operation) */
409
410     L_temp_2 = t0 >> 1;
411
412     L_temp_3 = (Word32)(ener_h << 15);
413
414     L_temp = L_temp_2 - L_temp_3;
415
416     ener_l = (Word16)L_temp;
417
418     t0 = Mpy_32(max_h, max_l, ener_h, ener_l, pOverflow);
419
420     if (scal_flag)
421     {
422         t0 = L_shr(t0, scal_fac, pOverflow);
423
424         if (t0 > (Word32) 0X0000FFFFL)
425         {
426             *cor_max = MAX_16;
427         }
428         else if (t0 < (Word32) 0xFFFF0000L)
429         {
430             *cor_max = MIN_16;
431         }
432         else
433         {
434             *cor_max = (Word16)(t0 >> 1);
435         }
436     }
437     else
438     {
439         *cor_max = (Word16)t0;
440     }
441
442     return (p_max);
443 }
444
445 /*----------------------------------------------------------------------------
446 ; End Function: Lag_max
447 ----------------------------------------------------------------------------*/
448
449
450 /*
451 ------------------------------------------------------------------------------
452  FUNCTION NAME: Lag_max_wrapper
453 ------------------------------------------------------------------------------
454  INPUT AND OUTPUT DEFINITIONS
455
456  Inputs
457     corr = pointer to buffer of correlation values (Word32)
458     scal_sig = pointer to buffer of scaled signal values (Word16)
459     scal_fac = scaled signal factor (Word16)
460     scal_flag = EFR compatible scaling flag (Word16)
461     L_frame = length of frame to compute pitch (Word16)
462     lag_max = maximum lag (Word16)
463     lag_min = minimum lag (Word16)
464     cor_max = pointer to the normalized correlation of selected lag (Word16)
465     rmax = pointer to max(<s[i]*s[j]>), (Word32)
466     r0 = pointer to the residual energy (Word32)
467     dtx  = dtx flag; equal to 1, if dtx is enabled, 0, otherwise (Flag)
468     pOverflow = pointer to overflow indicator (Flag)
469
470  Outputs:
471     cor_max contains the newly calculated normalized correlation of the
472       selected lag
473     rmax contains the newly calculated max(<s[i]*s[j]>)
474     r0 contains the newly calculated residual energy
475     pOverflow -> 1 if the math operations called by this routine saturate
476
477  Returns:
478     p_max = lag of the max correlation found (Word16)
479
480  Global Variables Used:
481     None.
482
483  Local Variables Needed:
484     None.
485
486 ------------------------------------------------------------------------------
487  INPUT AND OUTPUT DEFINITIONS (If VAD2 is not defined)
488
489  Inputs
490     vadSt = pointer to a vadState structure
491     corr = pointer to buffer of correlation values (Word32)
492     scal_sig = pointer to buffer of scaled signal values (Word16)
493     scal_fac = scaled signal factor (Word16)
494     scal_flag = EFR compatible scaling flag (Word16)
495     L_frame = length of frame to compute pitch (Word16)
496     lag_max = maximum lag (Word16)
497     lag_min = minimum lag (Word16)
498     cor_max = pointer to the normalized correlation of selected lag (Word16)
499     dtx  = dtx flag; equal to 1, if dtx is enabled, 0, otherwise (Flag)
500     pOverflow = pointer to overflow indicator (Flag)
501
502  Outputs:
503     cor_max contains the newly calculated normalized correlation of the
504       selected lag
505     vadSt contains the updated VAD state parameters
506     pOverflow -> 1 if the math operations called by this routine saturate
507
508  Returns:
509     p_max = lag of the max correlation found (Word16)
510
511  Global Variables Used:
512     None.
513
514  Local Variables Needed:
515     None.
516
517 ------------------------------------------------------------------------------
518  FUNCTION DESCRIPTION
519
520  This function provides external access to the local function Lag_max.
521
522 ------------------------------------------------------------------------------
523  REQUIREMENTS
524
525  None
526
527 ------------------------------------------------------------------------------
528  REFERENCES
529
530  pitch_ol.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
531
532 ------------------------------------------------------------------------------
533  PSEUDO-CODE
534
535 #ifdef VAD2
536  CALL Lag_max(corr = corr
537           scal_sig = scal_sig
538           scal_fac = scal_fac
539           scal_flag = scal_flag
540           L_frame = L_frame
541           lag_max = lag_max
542           lag_min = lag_min
543           cor_max = cor_max
544           rmax = rmax
545           r0 = r0
546           dtx = dtx
547           pOverflow = pOverflow)
548    MODIFYING(nothing)
549    RETURNING(temp)
550
551 #else
552  CALL Lag_max(vadSt = vadSt
553           corr = corr
554           scal_sig = scal_sig
555           scal_fac = scal_fac
556           scal_flag = scal_flag
557           L_frame = L_frame
558           lag_max = lag_max
559           lag_min = lag_min
560           cor_max = cor_max
561           dtx = dtx
562           pOverflow = pOverflow)
563    MODIFYING(nothing)
564    RETURNING(temp)
565
566 #endif
567
568 ------------------------------------------------------------------------------
569  CAUTION [optional]
570  [State any special notes, constraints or cautions for users of this function]
571
572 ------------------------------------------------------------------------------
573 */
574
575 #ifdef VAD2
576 Word16 Lag_max_wrapper(  /* o   : lag found                          */
577     Word32 corr[],      /* i   : correlation vector.                     */
578     Word16 scal_sig[],  /* i   : scaled signal.                          */
579     Word16 scal_fac,    /* i   : scaled signal factor.                   */
580     Word16 scal_flag,   /* i   : if 1 use EFR compatible scaling         */
581     Word16 L_frame,     /* i   : length of frame to compute pitch        */
582     Word16 lag_max,     /* i   : maximum lag                             */
583     Word16 lag_min,     /* i   : minimum lag                             */
584     Word16 *cor_max,    /* o   : normalized correlation of selected lag  */
585     Word32 *rmax,       /* o   : max(<s[i]*s[j]>)                        */
586     Word32 *r0,         /* o   : residual energy                         */
587     Flag dtx,           /* i   : dtx flag; use dtx=1, do not use dtx=0   */
588     Flag *pOverflow     /* i/o : overflow Flag                           */
589 )
590 {
591     Word16 temp;
592
593     temp = Lag_max(corr, scal_sig, scal_fac, scal_flag, L_frame, lag_max,
594                    lag_min, cor_max, rmax, r0, dtx, pOverflow);
595
596     return(temp);
597 }
598
599 #else
600 Word16 Lag_max_wrapper(  /* o   : lag found                          */
601     vadState *vadSt,    /* i/o : VAD state struct                        */
602     Word32 corr[],      /* i   : correlation vector.                     */
603     Word16 scal_sig[],  /* i   : scaled signal.                          */
604     Word16 scal_fac,    /* i   : scaled signal factor.                   */
605     Word16 scal_flag,   /* i   : if 1 use EFR compatible scaling         */
606     Word16 L_frame,     /* i   : length of frame to compute pitch        */
607     Word16 lag_max,     /* i   : maximum lag                             */
608     Word16 lag_min,     /* i   : minimum lag                             */
609     Word16 *cor_max,    /* o   : normalized correlation of selected lag  */
610     Flag dtx,           /* i   : dtx flag; use dtx=1, do not use dtx=0   */
611     Flag *pOverflow     /* i/o : overflow Flag                           */
612 )
613 {
614     Word16 temp;
615
616     temp = Lag_max(vadSt, corr, scal_sig, scal_fac, scal_flag, L_frame,
617                    lag_max, lag_min, cor_max, dtx, pOverflow);
618
619     return(temp);
620 }
621
622 #endif
623
624 /*----------------------------------------------------------------------------
625 ; End Function: Lag_max_wrapper
626 ----------------------------------------------------------------------------*/
627
628 /*
629 ------------------------------------------------------------------------------
630  FUNCTION NAME: Pitch_ol
631 ------------------------------------------------------------------------------
632  INPUT AND OUTPUT DEFINITIONS
633
634  Inputs:
635     vadSt = pointer to a vadState structure
636     mode =  data of type enum Mode specifies the mode.
637     signal = pointer to buffer of signal used to compute the open loop
638          pitch
639     where signal[-pit_max] to signal[-1] should be known
640     pit_min = 16 bit value specifies the minimum pitch lag
641     pit_max = 16 bit value specifies the maximum pitch lag
642     L_frame = 16 bit value specifies the length of frame to compute pitch
643     idx = 16 bit value specifies the frame index
644     dtx = Data of type 'Flag' used for dtx. Use dtx=1, do not use dtx=0
645     pOverflow = pointer to overflow indicator (Flag)
646
647  Outputs
648     vadSt = The vadSt state structure may be modified.
649     pOverflow -> 1 if the math operations called by this routine saturate
650
651  Returns:
652     p_max1 = 16 bit value representing the open loop pitch lag.
653
654  Global Variables Used:
655     None.
656
657  Local Variables Needed:
658     None.
659
660 ------------------------------------------------------------------------------
661  FUNCTION DESCRIPTION
662
663  This function computes the open loop pitch lag based on the perceptually
664  weighted speech signal. This is done in the following steps:
665        - find three maxima of the correlation <sw[n],sw[n-T]>,
666          dividing the search range into three parts:
667               pit_min ... 2*pit_min-1
668             2*pit_min ... 4*pit_min-1
669             4*pit_min ...   pit_max
670        - divide each maximum by <sw[n-t], sw[n-t]> where t is the delay at
671          that maximum correlation.
672        - select the delay of maximum normalized correlation (among the
673          three candidates) while favoring the lower delay ranges.
674
675
676 ------------------------------------------------------------------------------
677  REQUIREMENTS
678
679  None.
680
681 ------------------------------------------------------------------------------
682  REFERENCES
683
684  pitch_ol.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
685
686 ------------------------------------------------------------------------------
687  PSEUDO-CODE
688
689 Word16 Pitch_ol (      // o   : open loop pitch lag
690     vadState *vadSt,   // i/o : VAD state struct
691     enum Mode mode,    // i   : coder mode
692     Word16 signal[],   // i   : signal used to compute the open loop pitch
693                        //    signal[-pit_max] to signal[-1] should be known
694     Word16 pit_min,    // i   : minimum pitch lag
695     Word16 pit_max,    // i   : maximum pitch lag
696     Word16 L_frame,    // i   : length of frame to compute pitch
697     Word16 idx,        // i   : frame index
698     Flag dtx           // i   : dtx flag; use dtx=1, do not use dtx=0
699     )
700 {
701     Word16 i, j;
702     Word16 max1, max2, max3;
703     Word16 p_max1, p_max2, p_max3;
704     Word16 scal_flag = 0;
705     Word32 t0;
706 #ifdef VAD2
707     Word32  r01, r02, r03;
708     Word32  rmax1, rmax2, rmax3;
709 #else
710     Word16 corr_hp_max;
711 #endif
712     Word32 corr[PIT_MAX+1], *corr_ptr;
713
714     // Scaled signal
715
716     Word16 scaled_signal[L_FRAME + PIT_MAX];
717     Word16 *scal_sig, scal_fac;
718
719 #ifndef VAD2
720     if (dtx)
721     {  // no test() call since this if is only in simulation env
722        // update tone detection
723        if ((sub(mode, MR475) == 0) || (sub(mode, MR515) == 0))
724        {
725           vad_tone_detection_update (vadSt, 1);
726        }
727        else
728        {
729           vad_tone_detection_update (vadSt, 0);
730        }
731     }
732 #endif
733
734     scal_sig = &scaled_signal[pit_max];
735
736     t0 = 0L;
737     for (i = -pit_max; i < L_frame; i++)
738     {
739         t0 = L_mac (t0, signal[i], signal[i]);
740     }
741
742      *--------------------------------------------------------*
743      * Scaling of input signal.                               *
744      *                                                        *
745      *   if Overflow        -> scal_sig[i] = signal[i]>>3     *
746      *   else if t0 < 1^20  -> scal_sig[i] = signal[i]<<3     *
747      *   else               -> scal_sig[i] = signal[i]        *
748      *--------------------------------------------------------*
749
750      *--------------------------------------------------------*
751      *  Verification for risk of overflow.                    *
752      *--------------------------------------------------------*
753
754     if (L_sub (t0, MAX_32) == 0L)               // Test for overflow
755     {
756         for (i = -pit_max; i < L_frame; i++)
757         {
758             scal_sig[i] = shr (signal[i], 3);
759         }
760         scal_fac = 3;
761     }
762     else if (L_sub (t0, (Word32) 1048576L) < (Word32) 0)
763         // if (t0 < 2^20)
764     {
765         for (i = -pit_max; i < L_frame; i++)
766         {
767             scal_sig[i] = shl (signal[i], 3);
768         }
769         scal_fac = -3;
770     }
771     else
772     {
773         for (i = -pit_max; i < L_frame; i++)
774         {
775             scal_sig[i] = signal[i];
776         }
777         scal_fac = 0;
778     }
779
780     // calculate all coreelations of scal_sig, from pit_min to pit_max
781     corr_ptr = &corr[pit_max];
782     comp_corr (scal_sig, L_frame, pit_max, pit_min, corr_ptr);
783
784      *--------------------------------------------------------------------*
785      *  The pitch lag search is divided in three sections.                *
786      *  Each section cannot have a pitch multiple.                        *
787      *  We find a maximum for each section.                               *
788      *  We compare the maximum of each section by favoring small lags.    *
789      *                                                                    *
790      *  First section:  lag delay = pit_max     downto 4*pit_min          *
791      *  Second section: lag delay = 4*pit_min-1 downto 2*pit_min          *
792      *  Third section:  lag delay = 2*pit_min-1 downto pit_min            *
793      *--------------------------------------------------------------------*
794
795     // mode dependent scaling in Lag_max
796     if (sub(mode, MR122) == 0)
797     {
798        scal_flag = 1;
799     }
800     else
801     {
802        scal_flag = 0;
803     }
804
805 #ifdef VAD2
806     j = shl (pit_min, 2);
807     p_max1 = Lag_max (corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
808                       pit_max, j, &max1, &rmax1, &r01, dtx);
809
810     i = sub (j, 1);
811     j = shl (pit_min, 1);
812     p_max2 = Lag_max (corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
813                       i, j, &max2, &rmax2, &r02, dtx);
814
815     i = sub (j, 1);
816     p_max3 = Lag_max (corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
817                       i, pit_min, &max3, &rmax3, &r03, dtx);
818 #else
819     j = shl (pit_min, 2);
820     p_max1 = Lag_max (vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
821                       pit_max, j, &max1, dtx);
822
823     i = sub (j, 1);
824     j = shl (pit_min, 1);
825     p_max2 = Lag_max (vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
826                       i, j, &max2, dtx);
827
828     i = sub (j, 1);
829     p_max3 = Lag_max (vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
830                       i, pit_min, &max3, dtx);
831
832     if (dtx)
833     {  // no test() call since this if is only in simulation env
834        if (sub(idx, 1) == 0)
835        {
836           // calculate max high-passed filtered correlation of all lags
837           hp_max (corr_ptr, scal_sig, L_frame, pit_max, pit_min, &corr_hp_max);
838
839           // update complex background detector
840           vad_complex_detection_update(vadSt, corr_hp_max);
841        }
842     }
843 #endif
844
845      *--------------------------------------------------------------------*
846      * Compare the 3 sections maximum, and favor small lag.               *
847      *--------------------------------------------------------------------*
848
849     if (sub (mult (max1, THRESHOLD), max2) < 0)
850     {
851         max1 = max2;
852         p_max1 = p_max2;
853 #ifdef VAD2
854         if (dtx)
855         {
856             rmax1 = rmax2;
857             r01 = r02;
858 #endif
859     }
860     if (sub (mult (max1, THRESHOLD), max3) < 0)
861     {
862         p_max1 = p_max3;
863 #ifdef VAD2
864         if (dtx)
865         {
866             rmax1 = rmax3;
867             r01 = r03;
868         }
869 #endif
870     }
871
872 #ifdef VAD2
873     if (dtx)
874     {
875         vadSt->L_Rmax = L_add(vadSt->L_Rmax, rmax1);   // Save max correlation
876         vadSt->L_R0 =   L_add(vadSt->L_R0, r01);        // Save max energy
877     }
878 #endif
879
880     return (p_max1);
881 }
882
883 ------------------------------------------------------------------------------
884  CAUTION [optional]
885  [State any special notes, constraints or cautions for users of this function]
886
887 ------------------------------------------------------------------------------
888 */
889
890 Word16 Pitch_ol(       /* o   : open loop pitch lag                         */
891     vadState *vadSt,   /* i/o : VAD state struct                            */
892     enum Mode mode,    /* i   : coder mode                                  */
893     Word16 signal[],   /* i   : signal used to compute the open loop pitch  */
894     /*    signal[-pit_max] to signal[-1] should be known */
895     Word16 pit_min,    /* i   : minimum pitch lag                           */
896     Word16 pit_max,    /* i   : maximum pitch lag                           */
897     Word16 L_frame,    /* i   : length of frame to compute pitch            */
898     Word16 idx,        /* i   : frame index                                 */
899     Flag dtx,          /* i   : dtx flag; use dtx=1, do not use dtx=0       */
900     Flag *pOverflow    /* i/o : overflow Flag                               */
901 )
902 {
903     Word16 i;
904     Word16 j;
905     Word16 max1;
906     Word16 max2;
907     Word16 max3;
908     Word16 p_max1;
909     Word16 p_max2;
910     Word16 p_max3;
911     Word16 scal_flag = 0;
912     Word32 t0;
913
914 #ifdef VAD2
915     Word32 r01;
916     Word32 r02;
917     Word32 r03;
918     Word32 rmax1;
919     Word32 rmax2;
920     Word32 rmax3;
921 #else
922     Word16 corr_hp_max;
923 #endif
924     Word32 corr[PIT_MAX+1];
925     Word32 *corr_ptr;
926
927     /* Scaled signal */
928
929     Word16 scaled_signal[L_FRAME + PIT_MAX];
930     Word16 *scal_sig;
931     Word16 *p_signal;
932     Word16 scal_fac;
933     Word32 L_temp;
934
935 #ifndef VAD2
936     if (dtx)
937     {   /* no test() call since this if is only in simulation env */
938         /* update tone detection */
939         if ((mode == MR475) || (mode == MR515))
940         {
941             vad_tone_detection_update(vadSt, 1, pOverflow);
942         }
943         else
944         {
945             vad_tone_detection_update(vadSt, 0, pOverflow);
946         }
947     }
948 #endif
949
950
951     t0 = 0L;
952     p_signal = &signal[-pit_max];
953
954     for (i = -pit_max; i < L_frame; i++)
955     {
956         t0 += (((Word32) * (p_signal)) * *(p_signal)) << 1;
957         p_signal++;
958         if (t0 < 0)
959         {
960             t0 = MAX_32;
961             break;
962         }
963
964     }
965
966     /*--------------------------------------------------------*
967      * Scaling of input signal.                               *
968      *                                                        *
969      *   if Overflow        -> scal_sig[i] = signal[i]>>3     *
970      *   else if t0 < 1^20  -> scal_sig[i] = signal[i]<<3     *
971      *   else               -> scal_sig[i] = signal[i]        *
972      *--------------------------------------------------------*/
973
974     /*--------------------------------------------------------*
975      *  Verification for risk of overflow.                    *
976      *--------------------------------------------------------*/
977
978     scal_sig = &scaled_signal[0];
979     p_signal = &signal[-pit_max];
980
981     if (t0 == MAX_32)     /* Test for overflow */
982     {
983
984         for (i = (pit_max + L_frame) >> 1; i != 0; i--)
985         {
986             *(scal_sig++) = (Word16)(((Word32) * (p_signal++) >> 3));
987             *(scal_sig++) = (Word16)(((Word32) * (p_signal++) >> 3));
988         }
989
990         if ((pit_max + L_frame) & 1)
991         {
992             *(scal_sig) = (Word16)(((Word32) * (p_signal) >> 3));
993         }
994
995         scal_fac = 3;
996     }
997     else if (t0 < (Word32)1048576L)
998         /* if (t0 < 2^20) */
999     {
1000         for (i = (pit_max + L_frame) >> 1; i != 0; i--)
1001         {
1002             *(scal_sig++) = (Word16)(((Word32) * (p_signal++) << 3));
1003             *(scal_sig++) = (Word16)(((Word32) * (p_signal++) << 3));
1004         }
1005
1006         if ((pit_max + L_frame) & 1)
1007         {
1008             *(scal_sig) = (Word16)(((Word32) * (p_signal) << 3));
1009         }
1010         scal_fac = -3;
1011     }
1012     else
1013     {
1014
1015         oscl_memcpy(scal_sig, p_signal, (L_frame + pit_max)*sizeof(*signal));
1016         scal_fac = 0;
1017     }
1018
1019     /* calculate all coreelations of scal_sig, from pit_min to pit_max */
1020     corr_ptr = &corr[pit_max];
1021
1022     scal_sig = &scaled_signal[pit_max];
1023
1024     comp_corr(scal_sig, L_frame, pit_max, pit_min, corr_ptr);
1025
1026     /*--------------------------------------------------------------------*
1027      *  The pitch lag search is divided in three sections.                *
1028      *  Each section cannot have a pitch multiple.                        *
1029      *  We find a maximum for each section.                               *
1030      *  We compare the maximum of each section by favoring small lags.    *
1031      *                                                                    *
1032      *  First section:  lag delay = pit_max     downto 4*pit_min          *
1033      *  Second section: lag delay = 4*pit_min-1 downto 2*pit_min          *
1034      *  Third section:  lag delay = 2*pit_min-1 downto pit_min            *
1035      *--------------------------------------------------------------------*/
1036
1037     /* mode dependent scaling in Lag_max */
1038
1039     if (mode == MR122)
1040     {
1041         scal_flag = 1;
1042     }
1043     else
1044     {
1045         scal_flag = 0;
1046     }
1047
1048 #ifdef VAD2
1049     L_temp = ((Word32)pit_min) << 2;
1050     if (L_temp != (Word32)((Word16) L_temp))
1051     {
1052         *pOverflow = 1;
1053         j = (pit_min > 0) ? MAX_16 : MIN_16;
1054     }
1055     else
1056     {
1057         j = (Word16)L_temp;
1058     }
1059
1060     p_max1 = Lag_max(corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
1061                      pit_max, j, &max1, &rmax1, &r01, dtx, pOverflow);
1062
1063     i = j - 1;
1064
1065     j = pit_min << 1;
1066
1067     p_max2 = Lag_max(corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
1068                      i, j, &max2, &rmax2, &r02, dtx, pOverflow);
1069
1070     i = j - 1;
1071
1072     p_max3 = Lag_max(corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
1073                      i, pit_min, &max3, &rmax3, &r03, dtx, pOverflow);
1074
1075 #else
1076     L_temp = ((Word32)pit_min) << 2;
1077     if (L_temp != (Word32)((Word16) L_temp))
1078     {
1079         *pOverflow = 1;
1080         j = (pit_min > 0) ? MAX_16 : MIN_16;
1081     }
1082     else
1083     {
1084         j = (Word16)L_temp;
1085     }
1086
1087     p_max1 = Lag_max(vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
1088                      pit_max, j, &max1, dtx, pOverflow);
1089
1090     i = j - 1;
1091
1092
1093     j = pit_min << 1;
1094
1095
1096     p_max2 = Lag_max(vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
1097                      i, j, &max2, dtx, pOverflow);
1098
1099     i = j - 1;
1100     p_max3 = Lag_max(vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
1101                      i, pit_min, &max3, dtx, pOverflow);
1102
1103     if (dtx)
1104     {  /* no test() call since this if is only in simulation env */
1105
1106         if (idx == 1)
1107         {
1108             /* calculate max high-passed filtered correlation of all lags */
1109             hp_max(corr_ptr, scal_sig, L_frame, pit_max, pit_min, &corr_hp_max,
1110                    pOverflow);
1111
1112             /* update complex background detector */
1113             vad_complex_detection_update(vadSt, corr_hp_max);
1114         }
1115     }
1116 #endif
1117
1118     /*--------------------------------------------------------------------*
1119      * Compare the 3 sections maximum, and favor small lag.               *
1120      *--------------------------------------------------------------------*/
1121
1122     i = (Word16)(((Word32)max1 * THRESHOLD) >> 15);
1123
1124     if (i < max2)
1125     {
1126         max1 = max2;
1127         p_max1 = p_max2;
1128
1129 #ifdef VAD2
1130         if (dtx)
1131         {
1132             rmax1 = rmax2;
1133             r01 = r02;
1134         }
1135 #endif
1136     }
1137
1138     i = (Word16)(((Word32)max1 * THRESHOLD) >> 15);
1139
1140
1141     if (i < max3)
1142     {
1143         p_max1 = p_max3;
1144
1145 #ifdef VAD2
1146         if (dtx)
1147         {
1148             rmax1 = rmax3;
1149             r01 = r03;
1150         }
1151 #endif
1152     }
1153
1154 #ifdef VAD2
1155     if (dtx)
1156     {
1157         /* Save max correlation */
1158         vadSt->L_Rmax = L_add(vadSt->L_Rmax, rmax1, pOverflow);
1159         /* Save max energy */
1160         vadSt->L_R0 =   L_add(vadSt->L_R0, r01, pOverflow);
1161     }
1162 #endif
1163
1164     return (p_max1);
1165 }