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/****************************************************************************************
19Portions 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)
26Permission to distribute, modify and use this file under the standard license
27terms listed above has been obtained from the copyright holder.
28****************************************************************************************/
29/*
30------------------------------------------------------------------------------
31
32
33
34 Pathname: ./audio/gsm-amr/c/src/calc_cor.c
35
36     Date: 06/12/2000
37
38------------------------------------------------------------------------------
39 REVISION HISTORY
40
41 Description: Initial Optimization
42
43 Description: Optimize code by calculating two correlation per iteration
44              of the outer loop.
45
46 Description: Delete psedocode
47
48 Description: Synchronized file with UMTS version 3.2.0. Updated coding
49              template. Removed unnecessary include files.
50
51 Description: Made the following changes per comments from Phase 2/3 review:
52              1. Defined one local variable per line.
53
54 Description:
55              1. Eliminated unused include file typedef.h.
56              2. Replaced array addressing by pointers
57              3. Unrolled loops to save extra accesses to memory
58
59 Description:  Replaced "int" and/or "char" with OSCL defined types.
60
61 Description: Using inline functions from fxp_arithmetic.h for mac operations.
62
63 Description: Replacing fxp_arithmetic.h with basic_op.h.
64
65 Description:
66
67------------------------------------------------------------------------------
68*/
69
70/*----------------------------------------------------------------------------
71; INCLUDES
72----------------------------------------------------------------------------*/
73#include "calc_cor.h"
74#include "basic_op.h"
75/*----------------------------------------------------------------------------
76; MACROS
77; Define module specific macros here
78----------------------------------------------------------------------------*/
79
80
81/*----------------------------------------------------------------------------
82; DEFINES
83; Include all pre-processor statements here. Include conditional
84; compile variables also.
85----------------------------------------------------------------------------*/
86
87/*----------------------------------------------------------------------------
88; LOCAL FUNCTION DEFINITIONS
89; Function Prototype declaration
90----------------------------------------------------------------------------*/
91
92/*----------------------------------------------------------------------------
93; LOCAL STORE/BUFFER/POINTER DEFINITIONS
94; Variable declaration - defined here and used outside this module
95----------------------------------------------------------------------------*/
96
97
98/*
99------------------------------------------------------------------------------
100 FUNCTION NAME: comp_corr
101------------------------------------------------------------------------------
102 INPUT AND OUTPUT DEFINITIONS
103
104 Inputs:
105    scal_sig = array of input samples. (Word16)
106    L_frame = length of frame used to compute pitch(Word16)
107    lag_max = maximum lag (Word16)
108    lag_min = minimum lag (Word16)
109    corr = pointer to array of correlations corresponding to the selected
110        lags. (Word32)
111
112 Outputs:
113    corr = pointer to array of correlations corresponding to the selected
114        lags. (Word32)
115
116 Returns:
117    none
118
119 Global Variables Used:
120    none
121
122 Local Variables Needed:
123    none
124
125------------------------------------------------------------------------------
126 FUNCTION DESCRIPTION
127
128 This function calculates all correlations of scal_sig[] in a given delay
129 range.
130
131 The correlation is given by
132
133         cor[t] = <scal_sig[n],scal_sig[n-t]>,  t=lag_min,...,lag_max
134
135 The function outputs all of the correlations
136
137------------------------------------------------------------------------------
138 REQUIREMENTS
139
140 none
141
142------------------------------------------------------------------------------
143 REFERENCES
144
145 [1] calc_cor.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
146
147------------------------------------------------------------------------------
148 PSEUDO-CODE
149
150void comp_corr (
151    Word16 scal_sig[],  // i   : scaled signal.
152    Word16 L_frame,     // i   : length of frame to compute pitch
153    Word16 lag_max,     // i   : maximum lag
154    Word16 lag_min,     // i   : minimum lag
155    Word32 corr[])      // o   : correlation of selected lag
156{
157    Word16 i, j;
158    Word16 *p, *p1;
159    Word32 t0;
160
161    for (i = lag_max; i >= lag_min; i--)
162    {
163       p = scal_sig;
164       p1 = &scal_sig[-i];
165       t0 = 0;
166
167       for (j = 0; j < L_frame; j++, p++, p1++)
168       {
169          t0 = L_mac (t0, *p, *p1);
170       }
171       corr[-i] = t0;
172    }
173
174    return;
175}
176
177------------------------------------------------------------------------------
178 RESOURCES USED [optional]
179
180 When the code is written for a specific target processor the
181 the resources used should be documented below.
182
183 HEAP MEMORY USED: x bytes
184
185 STACK MEMORY USED: x bytes
186
187 CLOCK CYCLES: (cycle count equation for this function) + (variable
188                used to represent cycle count for each subroutine
189                called)
190     where: (cycle count variable) = cycle count for [subroutine
191                                     name]
192
193------------------------------------------------------------------------------
194 CAUTION [optional]
195 [State any special notes, constraints or cautions for users of this function]
196
197------------------------------------------------------------------------------
198*/
199
200void comp_corr(
201    Word16 scal_sig[],  /* i   : scaled signal.                     */
202    Word16 L_frame,     /* i   : length of frame to compute pitch   */
203    Word16 lag_max,     /* i   : maximum lag                        */
204    Word16 lag_min,     /* i   : minimum lag                        */
205    Word32 corr[])      /* o   : correlation of selected lag        */
206{
207
208
209
210
211    /*---------------------------------------------------
212    ; lag_max and lag_min are typically negative numbers
213    -----------------------------------------------------*/
214
215
216    /* PIT_MIN_MR122 18        Minimum pitch lag (MR122 mode)           */
217    /* PIT_MIN       20        Minimum pitch lag (all other modes)      */
218    /* PIT_MAX       143       Maximum pitch lag                        */
219
220
221    Word16 i;
222    Word16 j;
223    Word16 *p;
224    Word16 *p1;
225    Word16 *p2;
226    Word16 *p_scal_sig;
227    Word32 t1;
228    Word32 t2;
229    Word32 t3;
230    Word32 t4;
231
232    corr = corr - lag_max ;
233    p_scal_sig = &scal_sig[-lag_max];
234
235    for (i = ((lag_max - lag_min) >> 2) + 1; i > 0; i--)
236    {
237        t1 = 0;
238        t2 = 0;
239        t3 = 0;
240        t4 = 0;
241        p  = &scal_sig[0];
242        p1 = p_scal_sig++;
243        p_scal_sig++;
244        p2 = p_scal_sig++;
245        p_scal_sig++;
246        for (j = (L_frame >> 1); j != 0; j--)
247        {
248            t1 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p1++), t1);
249            t2 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p1), t2);
250            t3 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p2++), t3);
251            t4 = amrnb_fxp_mac_16_by_16bb((Word32) * (p++), (Word32) * (p2), t4);
252
253            t1 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p1++), t1);
254            t2 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p1), t2);
255            t3 = amrnb_fxp_mac_16_by_16bb((Word32) * (p), (Word32) * (p2++), t3);
256            t4 = amrnb_fxp_mac_16_by_16bb((Word32) * (p++), (Word32) * (p2), t4);
257        }
258
259        *(corr++) = t1 << 1;
260        *(corr++) = t2 << 1;
261        *(corr++) = t3 << 1;
262        *(corr++) = t4 << 1;
263
264    }
265
266    return;
267}
268