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_fr.cpp
36 ------------------------------------------------------------------------------
40 Purpose : Find the pitch period with 1/3 or 1/6 subsample
41 : resolution (closed loop).
43 ------------------------------------------------------------------------------
46 /*----------------------------------------------------------------------------
48 ----------------------------------------------------------------------------*/
62 /*----------------------------------------------------------------------------
64 ; Define module specific macros here
65 ----------------------------------------------------------------------------*/
67 /*----------------------------------------------------------------------------
69 ; Include all pre-processor statements here. Include conditional
70 ; compile variables also.
71 ----------------------------------------------------------------------------*/
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 ----------------------------------------------------------------------------*/
84 * mode dependent parameters used in Pitch_fr()
85 * Note: order of MRxx in 'enum Mode' is important!
89 Word16 max_frac_lag; /* lag up to which fractional lags are used */
90 Word16 flag3; /* enable 1/3 instead of 1/6 fract. resolution */
91 Word16 first_frac; /* first fractional to check */
92 Word16 last_frac; /* last fractional to check */
93 Word16 delta_int_low; /* integer lag below TO to start search from */
94 Word16 delta_int_range; /* integer range around T0 */
95 Word16 delta_frc_low; /* fractional below T0 */
96 Word16 delta_frc_range; /* fractional range around T0 */
97 Word16 pit_min; /* minimum pitch */
98 } mode_dep_parm[N_MODES] =
100 /* MR475 */ { 84, 1, -2, 2, 5, 10, 5, 9, PIT_MIN },
101 /* MR515 */ { 84, 1, -2, 2, 5, 10, 5, 9, PIT_MIN },
102 /* MR59 */ { 84, 1, -2, 2, 3, 6, 5, 9, PIT_MIN },
103 /* MR67 */ { 84, 1, -2, 2, 3, 6, 5, 9, PIT_MIN },
104 /* MR74 */ { 84, 1, -2, 2, 3, 6, 5, 9, PIT_MIN },
105 /* MR795 */ { 84, 1, -2, 2, 3, 6, 10, 19, PIT_MIN },
106 /* MR102 */ { 84, 1, -2, 2, 3, 6, 5, 9, PIT_MIN },
107 /* MR122 */ { 94, 0, -3, 3, 3, 6, 5, 9, PIT_MIN_MR122 }
111 ------------------------------------------------------------------------------
112 FUNCTION NAME: Norm_Corr
113 ------------------------------------------------------------------------------
114 INPUT AND OUTPUT DEFINITIONS
117 exc[] = pointer to buffer of type Word16
118 xn[] = pointer to buffer of type Word16
119 h[] = pointer to buffer of type Word16
120 L_subfr = length of sub frame (Word16)
121 t_min = the minimum table value of type Word16
122 t_max = the maximum table value of type Word16
123 corr_norm[] = pointer to buffer of type Word16
126 pOverflow = 1 if the math functions called result in overflow else zero.
131 Global Variables Used:
134 Local Variables Needed:
137 ------------------------------------------------------------------------------
140 FUNCTION: Norm_Corr()
142 PURPOSE: Find the normalized correlation between the target vector
143 and the filtered past excitation.
146 The normalized correlation is given by the correlation between the
147 target and filtered past excitation divided by the square root of
148 the energy of filtered excitation.
149 corr[k] = <x[], y_k[]>/sqrt(y_k[],y_k[])
150 where x[] is the target vector and y_k[] is the filtered past
151 excitation at delay k.
154 ------------------------------------------------------------------------------
159 ------------------------------------------------------------------------------
162 pitch_fr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
164 ------------------------------------------------------------------------------
167 static void Norm_Corr (Word16 exc[], Word16 xn[], Word16 h[], Word16 L_subfr,
168 Word16 t_min, Word16 t_max, Word16 corr_norm[])
171 Word16 corr_h, corr_l, norm_h, norm_l;
174 // Usally dynamic allocation of (L_subfr)
175 Word16 excf[L_SUBFR];
176 Word16 scaling, h_fac, *s_excf, scaled_excf[L_SUBFR];
180 // compute the filtered excitation for the first delay t_min
182 Convolve (&exc[k], h, excf, L_subfr);
184 // scale "excf[]" to avoid overflow
186 for (j = 0; j < L_subfr; j++) {
187 scaled_excf[j] = shr (excf[j], 2);
190 // Compute 1/sqrt(energy of excf[])
193 for (j = 0; j < L_subfr; j++) {
194 s = L_mac (s, excf[j], excf[j]);
196 if (L_sub (s, 67108864L) <= 0) { // if (s <= 2^26)
202 // "excf[]" is divided by 2
203 s_excf = scaled_excf;
208 // loop for every possible period
210 for (i = t_min; i <= t_max; i++) {
211 // Compute 1/sqrt(energy of excf[])
214 for (j = 0; j < L_subfr; j++) {
215 s = L_mac (s, s_excf[j], s_excf[j]);
219 L_Extract (s, &norm_h, &norm_l);
221 // Compute correlation between xn[] and excf[]
224 for (j = 0; j < L_subfr; j++) {
225 s = L_mac (s, xn[j], s_excf[j]);
227 L_Extract (s, &corr_h, &corr_l);
229 // Normalize correlation = correlation * (1/sqrt(energy))
231 s = Mpy_32 (corr_h, corr_l, norm_h, norm_l);
233 corr_norm[i] = extract_h (L_shl (s, 16));
235 // modify the filtered excitation excf[] for the next iteration
237 if (sub (i, t_max) != 0) {
239 for (j = L_subfr - 1; j > 0; j--) {
240 s = L_mult (exc[k], h[j]);
241 s = L_shl (s, h_fac);
242 s_excf[j] = add (extract_h (s), s_excf[j - 1]);
244 s_excf[0] = shr (exc[k], scaling);
250 ------------------------------------------------------------------------------
252 [State any special notes, constraints or cautions for users of this function]
254 ------------------------------------------------------------------------------
257 static void Norm_Corr(Word16 exc[],
275 Word16 excf[L_SUBFR];
279 Word16 scaled_excf[L_SUBFR];
288 /* compute the filtered excitation for the first delay t_min */
290 Convolve(&exc[k], h, excf, L_subfr);
292 /* scale "excf[]" to avoid overflow */
294 p_s_excf = scaled_excf;
297 for (j = (L_subfr >> 1); j != 0; j--)
300 *(p_s_excf++) = temp >> 2;
301 s += (Word32) temp * temp;
303 *(p_s_excf++) = temp >> 2;
304 s += (Word32) temp * temp;
308 if (s <= (67108864L >> 1))
316 /* "excf[]" is divided by 2 */
317 s_excf = scaled_excf;
322 /* loop for every possible period */
324 for (i = t_min; i <= t_max; i++)
326 /* Compute 1/sqrt(energy of excf[]) */
335 s += (Word32) * (p_x++) * *(p_s_excf);
336 s2 += ((Word32)(*(p_s_excf)) * (*(p_s_excf)));
338 s += (Word32) * (p_x++) * *(p_s_excf);
339 s2 += ((Word32)(*(p_s_excf)) * (*(p_s_excf)));
344 s2 = Inv_sqrt(s2, pOverflow);
345 norm_h = (Word16)(s2 >> 16);
346 norm_l = (Word16)((s2 >> 1) - (norm_h << 15));
347 corr_h = (Word16)(s >> 15);
348 corr_l = (Word16)((s) - (corr_h << 15));
350 /* Normalize correlation = correlation * (1/sqrt(energy)) */
352 s = Mpy_32(corr_h, corr_l, norm_h, norm_l, pOverflow);
354 corr_norm[i] = (Word16) s ;
356 /* modify the filtered excitation excf[] for the next iteration */
361 p_s_excf = &s_excf[L_subfr - 1];
362 p_h = &h[L_subfr - 1];
364 p_excf = &s_excf[L_subfr - 2];
365 for (j = (L_subfr - 1) >> 1; j != 0; j--)
367 s = ((Word32) temp * *(p_h--)) >> h_fac;
368 *(p_s_excf--) = (Word16) s + *(p_excf--);
369 s = ((Word32) temp * *(p_h--)) >> h_fac;
370 *(p_s_excf--) = (Word16) s + *(p_excf--);
373 s = ((Word32) temp * *(p_h)) >> h_fac;
374 *(p_s_excf--) = (Word16) s + *(p_excf);
376 *(p_s_excf) = temp >> scaling;
383 /****************************************************************************/
387 ------------------------------------------------------------------------------
388 FUNCTION NAME: searchFrac
389 ------------------------------------------------------------------------------
390 INPUT AND OUTPUT DEFINITIONS
393 lag = pointer to integer pitch of type Word16
394 frac = pointer to starting point of search fractional pitch of type Word16
395 last_frac = endpoint of search of type Word16
396 corr[] = pointer to normalized correlation of type Word16
397 flag3 = subsample resolution (3: =1 / 6: =0) of type Word16
405 Global Variables Used:
408 Local Variables Needed:
411 ------------------------------------------------------------------------------
414 FUNCTION: searchFrac()
416 PURPOSE: Find fractional pitch
419 The function interpolates the normalized correlation at the
420 fractional positions around lag T0. The position at which the
421 interpolation function reaches its maximum is the fractional pitch.
422 Starting point of the search is frac, end point is last_frac.
423 frac is overwritten with the fractional pitch.
425 ------------------------------------------------------------------------------
430 ------------------------------------------------------------------------------
433 pitch_fr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
435 ------------------------------------------------------------------------------
438 static void searchFrac (
439 Word16 *lag, // i/o : integer pitch
440 Word16 *frac, // i/o : start point of search -
442 Word16 last_frac, // i : endpoint of search
443 Word16 corr[], // i : normalized correlation
444 Word16 flag3 // i : subsample resolution
452 // Test the fractions around T0 and choose the one which maximizes
453 // the interpolated normalized correlation.
455 max = Interpol_3or6 (&corr[*lag], *frac, flag3); // function result
457 for (i = add (*frac, 1); i <= last_frac; i++) {
458 corr_int = Interpol_3or6 (&corr[*lag], i, flag3);
459 if (sub (corr_int, max) > 0) {
466 // Limit the fraction value in the interval [-2,-1,0,1,2,3]
468 if (sub (*frac, -3) == 0) {
470 *lag = sub (*lag, 1);
474 // limit the fraction value between -1 and 1
476 if (sub (*frac, -2) == 0) {
478 *lag = sub (*lag, 1);
480 if (sub (*frac, 2) == 0) {
482 *lag = add (*lag, 1);
487 ------------------------------------------------------------------------------
489 [State any special notes, constraints or cautions for users of this function]
491 ------------------------------------------------------------------------------
494 static void searchFrac(
495 Word16 *lag, /* i/o : integer pitch */
496 Word16 *frac, /* i/o : start point of search -
498 Word16 last_frac, /* i : endpoint of search */
499 Word16 corr[], /* i : normalized correlation */
500 Word16 flag3, /* i : subsample resolution
509 /* Test the fractions around T0 and choose the one which maximizes */
510 /* the interpolated normalized correlation. */
512 max = Interpol_3or6(&corr[*lag], *frac, flag3, pOverflow);
513 /* function result */
515 for (i = *frac + 1; i <= last_frac; i++)
517 corr_int = Interpol_3or6(&corr[*lag], i, flag3, pOverflow);
527 /* Limit the fraction value in the interval [-2,-1,0,1,2,3] */
537 /* limit the fraction value between -1 and 1 */
552 /****************************************************************************/
556 ------------------------------------------------------------------------------
557 FUNCTION NAME: getRange
558 ------------------------------------------------------------------------------
559 INPUT AND OUTPUT DEFINITIONS
562 T0 = integer pitch of type Word16
563 delta_low = search start offset of type Word16
564 delta_range = search range of type Word16
565 pitmin = minimum pitch of type Word16
566 pitmax = maximum pitch of type Word16
567 t0_min = search range minimum of type Word16
568 t0_max = search range maximum of type Word16
571 pOverflow = 1 if the math functions called result in overflow else zero.
576 Global Variables Used:
579 Local Variables Needed:
582 ------------------------------------------------------------------------------
587 PURPOSE: Sets range around open-loop pitch or integer pitch of last subframe
590 Takes integer pitch T0 and calculates a range around it with
591 t0_min = T0-delta_low and t0_max = (T0-delta_low) + delta_range
592 t0_min and t0_max are bounded by pitmin and pitmax
593 ------------------------------------------------------------------------------
598 ------------------------------------------------------------------------------
601 pitch_fr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
603 ------------------------------------------------------------------------------
606 static void getRange (
607 Word16 T0, // i : integer pitch
608 Word16 delta_low, // i : search start offset
609 Word16 delta_range, // i : search range
610 Word16 pitmin, // i : minimum pitch
611 Word16 pitmax, // i : maximum pitch
612 Word16 *t0_min, // o : search range minimum
613 Word16 *t0_max) // o : search range maximum
615 *t0_min = sub(T0, delta_low);
616 if (sub(*t0_min, pitmin) < 0) {
619 *t0_max = add(*t0_min, delta_range);
620 if (sub(*t0_max, pitmax) > 0) {
622 *t0_min = sub(*t0_max, delta_range);
626 ------------------------------------------------------------------------------
628 [State any special notes, constraints or cautions for users of this function]
630 ------------------------------------------------------------------------------
632 static void getRange(
633 Word16 T0, /* i : integer pitch */
634 Word16 delta_low, /* i : search start offset */
635 Word16 delta_range, /* i : search range */
636 Word16 pitmin, /* i : minimum pitch */
637 Word16 pitmax, /* i : maximum pitch */
638 Word16 *t0_min, /* o : search range minimum */
639 Word16 *t0_max, /* o : search range maximum */
644 OSCL_UNUSED_ARG(pOverflow);
647 temp = T0 - delta_low;
658 *t0_min = pitmax - delta_range;
665 /****************************************************************************/
669 ------------------------------------------------------------------------------
670 FUNCTION NAME: Pitch_fr_init
671 ------------------------------------------------------------------------------
672 INPUT AND OUTPUT DEFINITIONS
675 state = pointer to a pointer of structure type Pitch_fr_State.
681 Returns a zero if successful and -1 if not successful.
683 Global Variables Used:
686 Local Variables Needed:
689 ------------------------------------------------------------------------------
692 Function: Pitch_fr_init
693 Purpose: Allocates state memory and initializes state memory
695 ------------------------------------------------------------------------------
700 ------------------------------------------------------------------------------
703 pitch_fr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
705 ------------------------------------------------------------------------------
708 int Pitch_fr_init (Pitch_frState **state)
712 if (state == (Pitch_frState **) NULL){
713 // fprintf(stderr, "Pitch_fr_init: invalid parameter\n");
719 if ((s= (Pitch_frState *) malloc(sizeof(Pitch_frState))) == NULL){
720 // fprintf(stderr, "Pitch_fr_init: can not malloc state structure\n");
730 ------------------------------------------------------------------------------
732 [State any special notes, constraints or cautions for users of this function]
734 ------------------------------------------------------------------------------
736 Word16 Pitch_fr_init(Pitch_frState **state)
740 if (state == (Pitch_frState **) NULL)
742 /* fprintf(stderr, "Pitch_fr_init: invalid parameter\n"); */
747 /* allocate memory */
748 if ((s = (Pitch_frState *) oscl_malloc(sizeof(Pitch_frState))) == NULL)
750 /* fprintf(stderr, "Pitch_fr_init: can not malloc state structure\n"); */
761 /****************************************************************************/
765 ------------------------------------------------------------------------------
766 FUNCTION NAME: Pitch_fr_reset
767 ------------------------------------------------------------------------------
768 INPUT AND OUTPUT DEFINITIONS
771 state = pointer to a pointer of structure type Pitch_fr_State.
777 Returns a zero if successful and -1 if not successful.
779 Global Variables Used:
782 Local Variables Needed:
785 ------------------------------------------------------------------------------
788 Function: Pitch_fr_reset
789 Purpose: Initializes state memory to zero
791 ------------------------------------------------------------------------------
796 ------------------------------------------------------------------------------
799 pitch_fr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
801 ------------------------------------------------------------------------------
804 int Pitch_fr_reset (Pitch_frState *state)
807 if (state == (Pitch_frState *) NULL){
808 // fprintf(stderr, "Pitch_fr_reset: invalid parameter\n");
812 state->T0_prev_subframe = 0;
817 ------------------------------------------------------------------------------
819 [State any special notes, constraints or cautions for users of this function]
821 ------------------------------------------------------------------------------
823 Word16 Pitch_fr_reset(Pitch_frState *state)
826 if (state == (Pitch_frState *) NULL)
828 /* fprintf(stderr, "Pitch_fr_reset: invalid parameter\n"); */
832 state->T0_prev_subframe = 0;
838 /****************************************************************************/
842 ------------------------------------------------------------------------------
843 FUNCTION NAME: Pitch_fr_exit
844 ------------------------------------------------------------------------------
845 INPUT AND OUTPUT DEFINITIONS
848 state = pointer to a pointer of structure type Pitch_fr_State.
856 Global Variables Used:
859 Local Variables Needed:
862 ------------------------------------------------------------------------------
865 Function: Pitch_fr_exit
866 Purpose: The memory for state is freed.
868 ------------------------------------------------------------------------------
873 ------------------------------------------------------------------------------
876 pitch_fr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
878 ------------------------------------------------------------------------------
881 void Pitch_fr_exit (Pitch_frState **state)
883 if (state == NULL || *state == NULL)
893 ------------------------------------------------------------------------------
895 [State any special notes, constraints or cautions for users of this function]
897 ------------------------------------------------------------------------------
899 void Pitch_fr_exit(Pitch_frState **state)
901 if (state == NULL || *state == NULL)
904 /* deallocate memory */
911 /****************************************************************************/
915 ------------------------------------------------------------------------------
916 FUNCTION NAME: Pitch_fr
917 ------------------------------------------------------------------------------
918 INPUT AND OUTPUT DEFINITIONS
921 st = pointer to stat structure of type Pitch_frState
922 mode = codec mode of type enum Mode
923 T_op[] = pointer to open loop pitch lags of type Word16
924 exc[] = pointer to excitation buffer of type Word16
925 xn[] = pointer to target vector of type Word16
926 h[] = pointer to impulse response of synthesis and weighting filters
928 L_subfr = length of subframe of type Word16
929 i_subfr = subframe offset of type Word16
932 pit_frac = pointer to pitch period (fractional) of type Word16
933 resu3 = pointer to subsample resolution of type Word16
934 ana_index = pointer to index of encoding of type Word16
939 Global Variables Used:
942 Local Variables Needed:
945 ------------------------------------------------------------------------------
950 PURPOSE: Find the pitch period with 1/3 or 1/6 subsample resolution
954 - find the normalized correlation between the target and filtered
955 past excitation in the search range.
956 - select the delay with maximum normalized correlation.
957 - interpolate the normalized correlation at fractions -3/6 to 3/6
958 with step 1/6 around the chosen delay.
959 - The fraction which gives the maximum interpolated value is chosen.
961 ------------------------------------------------------------------------------
966 ------------------------------------------------------------------------------
969 pitch_fr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
971 ------------------------------------------------------------------------------
974 Word16 Pitch_fr ( // o : pitch period (integer)
975 Pitch_frState *st, // i/o : State struct
976 enum Mode mode, // i : codec mode
977 Word16 T_op[], // i : open loop pitch lags
978 Word16 exc[], // i : excitation buffer Q0
979 Word16 xn[], // i : target vector Q0
980 Word16 h[], // i : impulse response of synthesis and
981 weighting filters Q12
982 Word16 L_subfr, // i : Length of subframe
983 Word16 i_subfr, // i : subframe offset
984 Word16 *pit_frac, // o : pitch period (fractional)
985 Word16 *resu3, // o : subsample resolution 1/3 (=1) or 1/6 (=0)
986 Word16 *ana_index // o : index of encoding
991 Word16 t0_min, t0_max;
992 Word16 max, lag, frac;
995 Word16 corr_v[40]; // Total length = t0_max-t0_min+1+2*L_INTER_SRCH
1000 Word16 delta_int_low, delta_int_range;
1001 Word16 delta_frc_low, delta_frc_range;
1003 Word16 frame_offset;
1004 Word16 delta_search;
1006 //-----------------------------------------------------------------------
1007 // set mode specific variables
1008 //----------------------------------------------------------------------
1010 max_frac_lag = mode_dep_parm[mode].max_frac_lag;
1011 flag3 = mode_dep_parm[mode].flag3;
1012 frac = mode_dep_parm[mode].first_frac;
1013 last_frac = mode_dep_parm[mode].last_frac;
1014 delta_int_low = mode_dep_parm[mode].delta_int_low;
1015 delta_int_range = mode_dep_parm[mode].delta_int_range;
1017 delta_frc_low = mode_dep_parm[mode].delta_frc_low;
1018 delta_frc_range = mode_dep_parm[mode].delta_frc_range;
1019 pit_min = mode_dep_parm[mode].pit_min;
1021 //-----------------------------------------------------------------------
1022 // decide upon full or differential search
1023 //-----------------------------------------------------------------------
1027 if ((i_subfr == 0) || (sub(i_subfr,L_FRAME_BY2) == 0)) {
1031 if (((sub((Word16)mode, (Word16)MR475) != 0) && (sub((Word16)mode,
1032 (Word16)MR515) != 0)) ||
1033 (sub(i_subfr,L_FRAME_BY2) != 0)) {
1035 // set t0_min, t0_max for full search
1036 // this is *not* done for mode MR475, MR515 in subframe 3
1038 delta_search = 0; // no differential search
1040 // calculate index into T_op which contains the open-loop
1041 // pitch estimations for the 2 big subframes
1047 // get T_op from the corresponding half frame and
1048 // set t0_min, t0_max
1050 getRange (T_op[frame_offset], delta_int_low, delta_int_range,
1051 pit_min, PIT_MAX, &t0_min, &t0_max);
1055 // mode MR475, MR515 and 3. Subframe: delta search as well
1056 getRange (st->T0_prev_subframe, delta_frc_low, delta_frc_range,
1057 pit_min, PIT_MAX, &t0_min, &t0_max);
1062 // for Subframe 2 and 4
1063 // get range around T0 of previous subframe for delta search
1065 getRange (st->T0_prev_subframe, delta_frc_low, delta_frc_range,
1066 pit_min, PIT_MAX, &t0_min, &t0_max);
1069 //-----------------------------------------------------------------------
1070 Find interval to compute normalized correlation
1071 -----------------------------------------------------------------------
1073 t_min = sub (t0_min, L_INTER_SRCH);
1074 t_max = add (t0_max, L_INTER_SRCH);
1076 corr = &corr_v[-t_min];
1078 //-----------------------------------------------------------------------
1079 Compute normalized correlation between target and filtered excitation
1080 -----------------------------------------------------------------------
1082 Norm_Corr (exc, xn, h, L_subfr, t_min, t_max, corr);
1084 //-----------------------------------------------------------------------
1086 -----------------------------------------------------------------------
1091 for (i = t0_min + 1; i <= t0_max; i++) {
1092 if (sub (corr[i], max) >= 0) {
1098 //-----------------------------------------------------------------------
1099 Find fractional pitch
1100 -----------------------------------------------------------------------
1101 if ((delta_search == 0) && (sub (lag, max_frac_lag) > 0)) {
1103 // full search and integer pitch greater than max_frac_lag
1104 // fractional search is not needed, set fractional to zero
1110 // if differential search AND mode MR475 OR MR515 OR MR59 OR MR67
1111 // then search fractional with 4 bits resolution
1113 if ((delta_search != 0) &&
1114 ((sub ((Word16)mode, (Word16)MR475) == 0) ||
1115 (sub ((Word16)mode, (Word16)MR515) == 0) ||
1116 (sub ((Word16)mode, (Word16)MR59) == 0) ||
1117 (sub ((Word16)mode, (Word16)MR67) == 0))) {
1119 // modify frac or last_frac according to position of last
1120 // integer pitch: either search around integer pitch,
1121 // or only on left or right side
1123 tmp_lag = st->T0_prev_subframe;
1124 if ( sub( sub(tmp_lag, t0_min), 5) > 0)
1125 tmp_lag = add (t0_min, 5);
1126 if ( sub( sub(t0_max, tmp_lag), 4) > 0)
1127 tmp_lag = sub (t0_max, 4);
1129 if ((sub (lag, tmp_lag) == 0) ||
1130 (sub (lag, sub(tmp_lag, 1)) == 0)) {
1132 // normal search in fractions around T0
1134 searchFrac (&lag, &frac, last_frac, corr, flag3);
1137 else if (sub (lag, sub (tmp_lag, 2)) == 0) {
1138 // limit search around T0 to the right side
1140 searchFrac (&lag, &frac, last_frac, corr, flag3);
1142 else if (sub (lag, add(tmp_lag, 1)) == 0) {
1143 // limit search around T0 to the left side
1145 searchFrac (&lag, &frac, last_frac, corr, flag3);
1148 // no fractional search
1153 // test the fractions around T0
1154 searchFrac (&lag, &frac, last_frac, corr, flag3);
1157 //-----------------------------------------------------------------------
1159 //-----------------------------------------------------------------------
1162 // flag4 indicates encoding with 4 bit resolution;
1163 // this is needed for mode MR475, MR515 and MR59
1166 if ( (sub ((Word16)mode, (Word16)MR475) == 0) ||
1167 (sub ((Word16)mode, (Word16)MR515) == 0) ||
1168 (sub ((Word16)mode, (Word16)MR59) == 0) ||
1169 (sub ((Word16)mode, (Word16)MR67) == 0) ) {
1173 // encode with 1/3 subsample resolution
1175 *ana_index = Enc_lag3(lag, frac, st->T0_prev_subframe,
1176 t0_min, t0_max, delta_search, flag4);
1182 // encode with 1/6 subsample resolution
1184 *ana_index = Enc_lag6(lag, frac, t0_min, delta_search);
1188 //-----------------------------------------------------------------------
1189 // update state variables
1190 //-----------------------------------------------------------------------
1192 st->T0_prev_subframe = lag;
1194 //-----------------------------------------------------------------------
1195 // update output variables
1196 //-----------------------------------------------------------------------
1206 ------------------------------------------------------------------------------
1208 [State any special notes, constraints or cautions for users of this function]
1210 ------------------------------------------------------------------------------
1212 Word16 Pitch_fr( /* o : pitch period (integer) */
1213 Pitch_frState *st, /* i/o : State struct */
1214 enum Mode mode, /* i : codec mode */
1215 Word16 T_op[], /* i : open loop pitch lags */
1216 Word16 exc[], /* i : excitation buffer Q0 */
1217 Word16 xn[], /* i : target vector Q0 */
1218 Word16 h[], /* i : impulse response of synthesis and
1219 weighting filters Q12 */
1220 Word16 L_subfr, /* i : Length of subframe */
1221 Word16 i_subfr, /* i : subframe offset */
1222 Word16 *pit_frac, /* o : pitch period (fractional) */
1223 Word16 *resu3, /* o : subsample resolution 1/3 (=1) or 1/6 (=0) */
1224 Word16 *ana_index, /* o : index of encoding */
1238 Word16 corr_v[40]; /* Total length = t0_max-t0_min+1+2*L_INTER_SRCH */
1240 Word16 max_frac_lag;
1244 Word16 delta_int_low;
1245 Word16 delta_int_range;
1246 Word16 delta_frc_low;
1247 Word16 delta_frc_range;
1249 Word16 frame_offset;
1250 Word16 delta_search;
1252 /*-----------------------------------------------------------------------*
1253 * set mode specific variables *
1254 *-----------------------------------------------------------------------*/
1256 max_frac_lag = mode_dep_parm[mode].max_frac_lag;
1257 flag3 = mode_dep_parm[mode].flag3;
1258 frac = mode_dep_parm[mode].first_frac;
1259 last_frac = mode_dep_parm[mode].last_frac;
1260 delta_int_low = mode_dep_parm[mode].delta_int_low;
1261 delta_int_range = mode_dep_parm[mode].delta_int_range;
1263 delta_frc_low = mode_dep_parm[mode].delta_frc_low;
1264 delta_frc_range = mode_dep_parm[mode].delta_frc_range;
1265 pit_min = mode_dep_parm[mode].pit_min;
1267 /*-----------------------------------------------------------------------*
1268 * decide upon full or differential search *
1269 *-----------------------------------------------------------------------*/
1273 if ((i_subfr == 0) || (i_subfr == L_FRAME_BY2))
1276 /* Subframe 1 and 3 */
1278 if (((mode != MR475) && (mode != MR515)) || (i_subfr != L_FRAME_BY2))
1281 /* set t0_min, t0_max for full search */
1282 /* this is *not* done for mode MR475, MR515 in subframe 3 */
1284 delta_search = 0; /* no differential search */
1286 /* calculate index into T_op which contains the open-loop */
1287 /* pitch estimations for the 2 big subframes */
1293 /* get T_op from the corresponding half frame and */
1294 /* set t0_min, t0_max */
1296 getRange(T_op[frame_offset], delta_int_low, delta_int_range,
1297 pit_min, PIT_MAX, &t0_min, &t0_max, pOverflow);
1302 /* mode MR475, MR515 and 3. Subframe: delta search as well */
1303 getRange(st->T0_prev_subframe, delta_frc_low, delta_frc_range,
1304 pit_min, PIT_MAX, &t0_min, &t0_max, pOverflow);
1310 /* for Subframe 2 and 4 */
1311 /* get range around T0 of previous subframe for delta search */
1313 getRange(st->T0_prev_subframe, delta_frc_low, delta_frc_range,
1314 pit_min, PIT_MAX, &t0_min, &t0_max, pOverflow);
1317 /*-----------------------------------------------------------------------*
1318 * Find interval to compute normalized correlation *
1319 *-----------------------------------------------------------------------*/
1321 t_min = t0_min - L_INTER_SRCH;
1322 t_max = t0_max + L_INTER_SRCH;
1324 corr = &corr_v[-t_min];
1326 /*-----------------------------------------------------------------------*
1327 * Compute normalized correlation between target and filtered excitation *
1328 *-----------------------------------------------------------------------*/
1330 Norm_Corr(exc, xn, h, L_subfr, t_min, t_max, corr, pOverflow);
1332 /*-----------------------------------------------------------------------*
1333 * Find integer pitch *
1334 *-----------------------------------------------------------------------*/
1339 for (i = t0_min + 1; i <= t0_max; i++)
1348 /*-----------------------------------------------------------------------*
1349 * Find fractional pitch *
1350 *-----------------------------------------------------------------------*/
1351 if ((delta_search == 0) && (lag > max_frac_lag))
1354 /* full search and integer pitch greater than max_frac_lag */
1355 /* fractional search is not needed, set fractional to zero */
1362 /* if differential search AND mode MR475 OR MR515 OR MR59 OR MR67 */
1363 /* then search fractional with 4 bits resolution */
1365 if ((delta_search != 0) &&
1366 ((mode == MR475) || (mode == MR515) ||
1367 (mode == MR59) || (mode == MR67)))
1370 /* modify frac or last_frac according to position of last */
1371 /* integer pitch: either search around integer pitch, */
1372 /* or only on left or right side */
1374 tmp_lag = st->T0_prev_subframe;
1375 if ((tmp_lag - t0_min) > 5)
1377 tmp_lag = t0_min + 5;
1379 if ((t0_max - tmp_lag) > 4)
1381 tmp_lag = t0_max - 4;
1384 if ((lag == tmp_lag) || (lag == (tmp_lag - 1)))
1387 /* normal search in fractions around T0 */
1389 searchFrac(&lag, &frac, last_frac, corr, flag3, pOverflow);
1392 else if (lag == (tmp_lag - 2))
1394 /* limit search around T0 to the right side */
1396 searchFrac(&lag, &frac, last_frac, corr, flag3, pOverflow);
1398 else if (lag == (tmp_lag + 1))
1400 /* limit search around T0 to the left side */
1402 searchFrac(&lag, &frac, last_frac, corr, flag3, pOverflow);
1406 /* no fractional search */
1411 /* test the fractions around T0 */
1412 searchFrac(&lag, &frac, last_frac, corr, flag3, pOverflow);
1415 /*-----------------------------------------------------------------------*
1417 *-----------------------------------------------------------------------*/
1421 /* flag4 indicates encoding with 4 bit resolution; */
1422 /* this is needed for mode MR475, MR515 and MR59 */
1425 if ((mode == MR475) || (mode == MR515) ||
1426 (mode == MR59) || (mode == MR67))
1431 /* encode with 1/3 subsample resolution */
1433 *ana_index = Enc_lag3(lag, frac, st->T0_prev_subframe,
1434 t0_min, t0_max, delta_search, flag4, pOverflow);
1435 /* function result */
1440 /* encode with 1/6 subsample resolution */
1442 *ana_index = Enc_lag6(lag, frac, t0_min, delta_search, pOverflow);
1443 /* function result */
1446 /*-----------------------------------------------------------------------*
1447 * update state variables *
1448 *-----------------------------------------------------------------------*/
1450 st->T0_prev_subframe = lag;
1452 /*-----------------------------------------------------------------------*
1453 * update output variables *
1454 *-----------------------------------------------------------------------*/