1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
30 ------------------------------------------------------------------------------
34 Filename: pitch_ol.cpp
38 ------------------------------------------------------------------------------
41 The modules in this file compute the open loop pitch lag.
42 ------------------------------------------------------------------------------
46 /*----------------------------------------------------------------------------
48 ----------------------------------------------------------------------------*/
51 #include "basicop_malloc.h"
60 /*----------------------------------------------------------------------------
62 ; Define module specific macros here
63 ----------------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------------
68 ; Include all pre-processor statements here. Include conditional
69 ; compile variables also.
70 ----------------------------------------------------------------------------*/
71 #define THRESHOLD 27853
73 /*----------------------------------------------------------------------------
74 ; LOCAL FUNCTION DEFINITIONS
75 ; Function Prototype declaration
76 ----------------------------------------------------------------------------*/
78 /*----------------------------------------------------------------------------
79 ; LOCAL VARIABLE DEFINITIONS
80 ; Variable declaration - defined here and used outside this module
81 ----------------------------------------------------------------------------*/
85 ------------------------------------------------------------------------------
86 FUNCTION NAME: Lag_max
87 ------------------------------------------------------------------------------
88 INPUT AND OUTPUT DEFINITIONS (If VAD2 is defined)
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)
104 cor_max contains the newly calculated normalized correlation of the
106 rmax contains the newly calculated max(<s[i]*s[j]>)
107 r0 contains the newly calculated residual energy
110 p_max = lag of the max correlation found (Word16)
112 Global Variables Used:
115 Local Variables Needed:
118 ------------------------------------------------------------------------------
119 INPUT AND OUTPUT DEFINITIONS (If VAD2 is not defined)
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)
135 cor_max contains the newly calculated normalized correlation of the
137 vadSt contains the updated VAD state parameters
138 pOverflow -> 1 if the math operations called by this routine saturate
141 p_max = lag of the max correlation found (Word16)
143 Global Variables Used:
146 Local Variables Needed:
149 ------------------------------------------------------------------------------
152 Find the lag that has maximum correlation of scal_sig in a given delay range.
153 The correlation is given by:
155 cor[t] = <scal_sig[n],scal_sig[n-t]>, t=lag_min,...,lag_max
157 The function returns the maximum correlation after normalization and the
160 ------------------------------------------------------------------------------
165 ------------------------------------------------------------------------------
168 pitch_ol.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
170 ------------------------------------------------------------------------------
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
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
205 Word16 max_h, max_l, ener_h, ener_l;
206 Word16 p_max = 0; // initialization only needed to keep gcc silent
211 for (i = lag_max, j = (PIT_MAX-lag_max-1); i >= lag_min; i--, j--)
213 if (L_sub (corr[-i], max) >= 0)
223 p = &scal_sig[-p_max];
224 for (i = 0; i < L_frame; i++, p++)
226 t0 = L_mac (t0, *p, *p);
231 { // no test() call since this if is only in simulation env
237 vad_tone_detection (vadSt, max, t0);
248 // max = max/sqrt(energy)
250 L_Extract (max, &max_h, &max_l);
251 L_Extract (t0, &ener_h, &ener_l);
253 t0 = Mpy_32 (max_h, max_l, ener_h, ener_l);
257 t0 = L_shr (t0, scal_fac);
258 *cor_max = extract_h (L_shl (t0, 15)); // divide by 2
262 *cor_max = extract_l(t0);
268 ------------------------------------------------------------------------------
270 [State any special notes, constraints or cautions for users of this function]
272 ------------------------------------------------------------------------------
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 */
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 */
314 Word16 p_max = 0; /* initialization only needed to keep gcc silent */
318 Word32 *p_corr = &corr[-lag_max];
323 for (i = lag_max; i >= lag_min; i--)
325 /* The negative array index is equivalent to a negative */
326 /* address offset, i.e., corr[-i] == *(corr - i) */
327 if (*(p_corr++) >= max)
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--)
344 t0 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p), t0);
346 t0 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p), t0);
348 t0 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p), t0);
350 t0 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p), t0);
358 { /* no test() call since this if is only in simulation env */
365 vad_tone_detection(vadSt, max, t0, pOverflow);
369 t0 = Inv_sqrt(t0, pOverflow);
373 if (t0 > (Word32) 0x3fffffffL)
383 /* max = max/sqrt(energy) */
384 /* The following code is an inlined version of */
385 /* L_Extract (max, &max_h, &max_l), i.e. */
387 /* *max_h = extract_h (max); */
388 max_h = (Word16)(max >> 16);
390 /* L_temp_2 = L_shr(max,1), which is used in */
391 /* the calculation of *max_l (see next operation) */
394 /* *max_l = extract_l (L_msu (L_shr (max, 1), *max_h, 16384)); */
395 L_temp_3 = (Word32)(max_h << 15);
397 L_temp = L_temp_2 - L_temp_3;
399 max_l = (Word16)L_temp;
401 /* The following code is an inlined version of */
402 /* L_Extract (t0, &ener_h, &ener_l), i.e. */
404 /* *ener_h = extract_h (t0); */
405 ener_h = (Word16)(t0 >> 16);
407 /* L_temp_2 = L_shr(t0,1), which is used in */
408 /* the calculation of *ener_l (see next operation) */
412 L_temp_3 = (Word32)(ener_h << 15);
414 L_temp = L_temp_2 - L_temp_3;
416 ener_l = (Word16)L_temp;
418 t0 = Mpy_32(max_h, max_l, ener_h, ener_l, pOverflow);
422 t0 = L_shr(t0, scal_fac, pOverflow);
424 if (t0 > (Word32) 0X0000FFFFL)
428 else if (t0 < (Word32) 0xFFFF0000L)
434 *cor_max = (Word16)(t0 >> 1);
439 *cor_max = (Word16)t0;
445 /*----------------------------------------------------------------------------
446 ; End Function: Lag_max
447 ----------------------------------------------------------------------------*/
451 ------------------------------------------------------------------------------
452 FUNCTION NAME: Lag_max_wrapper
453 ------------------------------------------------------------------------------
454 INPUT AND OUTPUT DEFINITIONS
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)
471 cor_max contains the newly calculated normalized correlation of the
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
478 p_max = lag of the max correlation found (Word16)
480 Global Variables Used:
483 Local Variables Needed:
486 ------------------------------------------------------------------------------
487 INPUT AND OUTPUT DEFINITIONS (If VAD2 is not defined)
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)
503 cor_max contains the newly calculated normalized correlation of the
505 vadSt contains the updated VAD state parameters
506 pOverflow -> 1 if the math operations called by this routine saturate
509 p_max = lag of the max correlation found (Word16)
511 Global Variables Used:
514 Local Variables Needed:
517 ------------------------------------------------------------------------------
520 This function provides external access to the local function Lag_max.
522 ------------------------------------------------------------------------------
527 ------------------------------------------------------------------------------
530 pitch_ol.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
532 ------------------------------------------------------------------------------
536 CALL Lag_max(corr = corr
539 scal_flag = scal_flag
547 pOverflow = pOverflow)
552 CALL Lag_max(vadSt = vadSt
556 scal_flag = scal_flag
562 pOverflow = pOverflow)
568 ------------------------------------------------------------------------------
570 [State any special notes, constraints or cautions for users of this function]
572 ------------------------------------------------------------------------------
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 */
593 temp = Lag_max(corr, scal_sig, scal_fac, scal_flag, L_frame, lag_max,
594 lag_min, cor_max, rmax, r0, dtx, pOverflow);
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 */
616 temp = Lag_max(vadSt, corr, scal_sig, scal_fac, scal_flag, L_frame,
617 lag_max, lag_min, cor_max, dtx, pOverflow);
624 /*----------------------------------------------------------------------------
625 ; End Function: Lag_max_wrapper
626 ----------------------------------------------------------------------------*/
629 ------------------------------------------------------------------------------
630 FUNCTION NAME: Pitch_ol
631 ------------------------------------------------------------------------------
632 INPUT AND OUTPUT DEFINITIONS
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
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)
648 vadSt = The vadSt state structure may be modified.
649 pOverflow -> 1 if the math operations called by this routine saturate
652 p_max1 = 16 bit value representing the open loop pitch lag.
654 Global Variables Used:
657 Local Variables Needed:
660 ------------------------------------------------------------------------------
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.
676 ------------------------------------------------------------------------------
681 ------------------------------------------------------------------------------
684 pitch_ol.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
686 ------------------------------------------------------------------------------
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
702 Word16 max1, max2, max3;
703 Word16 p_max1, p_max2, p_max3;
704 Word16 scal_flag = 0;
707 Word32 r01, r02, r03;
708 Word32 rmax1, rmax2, rmax3;
712 Word32 corr[PIT_MAX+1], *corr_ptr;
716 Word16 scaled_signal[L_FRAME + PIT_MAX];
717 Word16 *scal_sig, scal_fac;
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))
725 vad_tone_detection_update (vadSt, 1);
729 vad_tone_detection_update (vadSt, 0);
734 scal_sig = &scaled_signal[pit_max];
737 for (i = -pit_max; i < L_frame; i++)
739 t0 = L_mac (t0, signal[i], signal[i]);
742 *--------------------------------------------------------*
743 * Scaling of input signal. *
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 *--------------------------------------------------------*
750 *--------------------------------------------------------*
751 * Verification for risk of overflow. *
752 *--------------------------------------------------------*
754 if (L_sub (t0, MAX_32) == 0L) // Test for overflow
756 for (i = -pit_max; i < L_frame; i++)
758 scal_sig[i] = shr (signal[i], 3);
762 else if (L_sub (t0, (Word32) 1048576L) < (Word32) 0)
765 for (i = -pit_max; i < L_frame; i++)
767 scal_sig[i] = shl (signal[i], 3);
773 for (i = -pit_max; i < L_frame; i++)
775 scal_sig[i] = signal[i];
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);
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. *
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 *--------------------------------------------------------------------*
795 // mode dependent scaling in Lag_max
796 if (sub(mode, MR122) == 0)
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);
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);
816 p_max3 = Lag_max (corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
817 i, pit_min, &max3, &rmax3, &r03, dtx);
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);
824 j = shl (pit_min, 1);
825 p_max2 = Lag_max (vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
829 p_max3 = Lag_max (vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
830 i, pit_min, &max3, dtx);
833 { // no test() call since this if is only in simulation env
834 if (sub(idx, 1) == 0)
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);
839 // update complex background detector
840 vad_complex_detection_update(vadSt, corr_hp_max);
845 *--------------------------------------------------------------------*
846 * Compare the 3 sections maximum, and favor small lag. *
847 *--------------------------------------------------------------------*
849 if (sub (mult (max1, THRESHOLD), max2) < 0)
860 if (sub (mult (max1, THRESHOLD), max3) < 0)
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
883 ------------------------------------------------------------------------------
885 [State any special notes, constraints or cautions for users of this function]
887 ------------------------------------------------------------------------------
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 */
911 Word16 scal_flag = 0;
924 Word32 corr[PIT_MAX+1];
929 Word16 scaled_signal[L_FRAME + PIT_MAX];
937 { /* no test() call since this if is only in simulation env */
938 /* update tone detection */
939 if ((mode == MR475) || (mode == MR515))
941 vad_tone_detection_update(vadSt, 1, pOverflow);
945 vad_tone_detection_update(vadSt, 0, pOverflow);
952 p_signal = &signal[-pit_max];
954 for (i = -pit_max; i < L_frame; i++)
956 t0 += (((Word32) * (p_signal)) * *(p_signal)) << 1;
966 /*--------------------------------------------------------*
967 * Scaling of input signal. *
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 *--------------------------------------------------------*/
974 /*--------------------------------------------------------*
975 * Verification for risk of overflow. *
976 *--------------------------------------------------------*/
978 scal_sig = &scaled_signal[0];
979 p_signal = &signal[-pit_max];
981 if (t0 == MAX_32) /* Test for overflow */
984 for (i = (pit_max + L_frame) >> 1; i != 0; i--)
986 *(scal_sig++) = (Word16)(((Word32) * (p_signal++) >> 3));
987 *(scal_sig++) = (Word16)(((Word32) * (p_signal++) >> 3));
990 if ((pit_max + L_frame) & 1)
992 *(scal_sig) = (Word16)(((Word32) * (p_signal) >> 3));
997 else if (t0 < (Word32)1048576L)
1000 for (i = (pit_max + L_frame) >> 1; i != 0; i--)
1002 *(scal_sig++) = (Word16)(((Word32) * (p_signal++) << 3));
1003 *(scal_sig++) = (Word16)(((Word32) * (p_signal++) << 3));
1006 if ((pit_max + L_frame) & 1)
1008 *(scal_sig) = (Word16)(((Word32) * (p_signal) << 3));
1015 oscl_memcpy(scal_sig, p_signal, (L_frame + pit_max)*sizeof(*signal));
1019 /* calculate all coreelations of scal_sig, from pit_min to pit_max */
1020 corr_ptr = &corr[pit_max];
1022 scal_sig = &scaled_signal[pit_max];
1024 comp_corr(scal_sig, L_frame, pit_max, pit_min, corr_ptr);
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. *
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 *--------------------------------------------------------------------*/
1037 /* mode dependent scaling in Lag_max */
1049 L_temp = ((Word32)pit_min) << 2;
1050 if (L_temp != (Word32)((Word16) L_temp))
1053 j = (pit_min > 0) ? MAX_16 : MIN_16;
1060 p_max1 = Lag_max(corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
1061 pit_max, j, &max1, &rmax1, &r01, dtx, pOverflow);
1067 p_max2 = Lag_max(corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
1068 i, j, &max2, &rmax2, &r02, dtx, pOverflow);
1072 p_max3 = Lag_max(corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
1073 i, pit_min, &max3, &rmax3, &r03, dtx, pOverflow);
1076 L_temp = ((Word32)pit_min) << 2;
1077 if (L_temp != (Word32)((Word16) L_temp))
1080 j = (pit_min > 0) ? MAX_16 : MIN_16;
1087 p_max1 = Lag_max(vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
1088 pit_max, j, &max1, dtx, pOverflow);
1096 p_max2 = Lag_max(vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
1097 i, j, &max2, dtx, pOverflow);
1100 p_max3 = Lag_max(vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
1101 i, pit_min, &max3, dtx, pOverflow);
1104 { /* no test() call since this if is only in simulation env */
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,
1112 /* update complex background detector */
1113 vad_complex_detection_update(vadSt, corr_hp_max);
1118 /*--------------------------------------------------------------------*
1119 * Compare the 3 sections maximum, and favor small lag. *
1120 *--------------------------------------------------------------------*/
1122 i = (Word16)(((Word32)max1 * THRESHOLD) >> 15);
1138 i = (Word16)(((Word32)max1 * THRESHOLD) >> 15);
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);