Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / calc_cor.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: calc_cor.cpp
35
36 ------------------------------------------------------------------------------
37 */
38
39 /*----------------------------------------------------------------------------
40 ; INCLUDES
41 ----------------------------------------------------------------------------*/
42 #include "calc_cor.h"
43 #include "basic_op.h"
44 /*----------------------------------------------------------------------------
45 ; MACROS
46 ; Define module specific macros here
47 ----------------------------------------------------------------------------*/
48
49
50 /*----------------------------------------------------------------------------
51 ; DEFINES
52 ; Include all pre-processor statements here. Include conditional
53 ; compile variables also.
54 ----------------------------------------------------------------------------*/
55
56 /*----------------------------------------------------------------------------
57 ; LOCAL FUNCTION DEFINITIONS
58 ; Function Prototype declaration
59 ----------------------------------------------------------------------------*/
60
61 /*----------------------------------------------------------------------------
62 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
63 ; Variable declaration - defined here and used outside this module
64 ----------------------------------------------------------------------------*/
65
66
67 /*
68 ------------------------------------------------------------------------------
69  FUNCTION NAME: comp_corr
70 ------------------------------------------------------------------------------
71  INPUT AND OUTPUT DEFINITIONS
72
73  Inputs:
74     scal_sig = array of input samples. (Word16)
75     L_frame = length of frame used to compute pitch(Word16)
76     lag_max = maximum lag (Word16)
77     lag_min = minimum lag (Word16)
78     corr = pointer to array of correlations corresponding to the selected
79         lags. (Word32)
80
81  Outputs:
82     corr = pointer to array of correlations corresponding to the selected
83         lags. (Word32)
84
85  Returns:
86     none
87
88  Global Variables Used:
89     none
90
91  Local Variables Needed:
92     none
93
94 ------------------------------------------------------------------------------
95  FUNCTION DESCRIPTION
96
97  This function calculates all correlations of scal_sig[] in a given delay
98  range.
99
100  The correlation is given by
101
102          cor[t] = <scal_sig[n],scal_sig[n-t]>,  t=lag_min,...,lag_max
103
104  The function outputs all of the correlations
105
106 ------------------------------------------------------------------------------
107  REQUIREMENTS
108
109  none
110
111 ------------------------------------------------------------------------------
112  REFERENCES
113
114  [1] calc_cor.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
115
116 ------------------------------------------------------------------------------
117  PSEUDO-CODE
118
119 void comp_corr (
120     Word16 scal_sig[],  // i   : scaled signal.
121     Word16 L_frame,     // i   : length of frame to compute pitch
122     Word16 lag_max,     // i   : maximum lag
123     Word16 lag_min,     // i   : minimum lag
124     Word32 corr[])      // o   : correlation of selected lag
125 {
126     Word16 i, j;
127     Word16 *p, *p1;
128     Word32 t0;
129
130     for (i = lag_max; i >= lag_min; i--)
131     {
132        p = scal_sig;
133        p1 = &scal_sig[-i];
134        t0 = 0;
135
136        for (j = 0; j < L_frame; j++, p++, p1++)
137        {
138           t0 = L_mac (t0, *p, *p1);
139        }
140        corr[-i] = t0;
141     }
142
143     return;
144 }
145
146 ------------------------------------------------------------------------------
147  CAUTION [optional]
148  [State any special notes, constraints or cautions for users of this function]
149
150 ------------------------------------------------------------------------------
151 */
152
153 void comp_corr(
154     Word16 scal_sig[],  /* i   : scaled signal.                     */
155     Word16 L_frame,     /* i   : length of frame to compute pitch   */
156     Word16 lag_max,     /* i   : maximum lag                        */
157     Word16 lag_min,     /* i   : minimum lag                        */
158     Word32 corr[])      /* o   : correlation of selected lag        */
159 {
160
161
162
163
164     /*---------------------------------------------------
165     ; lag_max and lag_min are typically negative numbers
166     -----------------------------------------------------*/
167
168
169     /* PIT_MIN_MR122 18        Minimum pitch lag (MR122 mode)           */
170     /* PIT_MIN       20        Minimum pitch lag (all other modes)      */
171     /* PIT_MAX       143       Maximum pitch lag                        */
172
173
174     Word16 i;
175     Word16 j;
176     Word16 *p;
177     Word16 *p1;
178     Word16 *p2;
179     Word16 *p_scal_sig;
180     Word32 t1;
181     Word32 t2;
182     Word32 t3;
183     Word32 t4;
184
185     corr = corr - lag_max ;
186     p_scal_sig = &scal_sig[-lag_max];
187
188     for (i = ((lag_max - lag_min) >> 2) + 1; i > 0; i--)
189     {
190         t1 = 0;
191         t2 = 0;
192         t3 = 0;
193         t4 = 0;
194         p  = &scal_sig[0];
195         p1 = p_scal_sig++;
196         p_scal_sig++;
197         p2 = p_scal_sig++;
198         p_scal_sig++;
199         for (j = (L_frame >> 1); j != 0; j--)
200         {
201             t1 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p1++), t1);
202             t2 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p1), t2);
203             t3 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p2++), t3);
204             t4 = amrnb_fxp_mac_16_by_16bb((Word32) * (p++), (Word32) * (p2), t4);
205
206             t1 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p1++), t1);
207             t2 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p1), t2);
208             t3 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p2++), t3);
209             t4 = amrnb_fxp_mac_16_by_16bb((Word32) * (p++), (Word32) * (p2), t4);
210         }
211
212         *(corr++) = t1 << 1;
213         *(corr++) = t2 << 1;
214         *(corr++) = t3 << 1;
215         *(corr++) = t4 << 1;
216
217     }
218
219     return;
220 }