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 ------------------------------------------------------------------------------
36 ------------------------------------------------------------------------------
39 /*----------------------------------------------------------------------------
41 ----------------------------------------------------------------------------*/
44 #include "basicop_malloc.h"
48 /*----------------------------------------------------------------------------
50 ; Define module specific macros here
51 ----------------------------------------------------------------------------*/
53 /*----------------------------------------------------------------------------
55 ; Include all pre-processor statements here. Include conditional
56 ; compile variables also.
57 ----------------------------------------------------------------------------*/
59 /*----------------------------------------------------------------------------
60 ; LOCAL FUNCTION DEFINITIONS
61 ; Function Prototype declaration
62 ----------------------------------------------------------------------------*/
64 /*----------------------------------------------------------------------------
65 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
66 ; Variable declaration - defined here and used outside this module
67 ----------------------------------------------------------------------------*/
70 ------------------------------------------------------------------------------
72 ------------------------------------------------------------------------------
73 INPUT AND OUTPUT DEFINITIONS
76 h = vector containing the impulse response of the weighted synthesis
77 filter; vector contents are of type Word16; vector length is
79 sign = vector containing the sign information for the correlation
80 values; vector contents are of type Word16; vector length is
82 rr = autocorrelation matrix; matrix contents are of type Word16;
83 matrix dimension is L_CODE by L_CODE
86 rr contents are the newly calculated autocorrelation values
91 Global Variables Used:
94 Local Variables Needed:
97 ------------------------------------------------------------------------------
100 This function computes correlations of the impulse response (h) needed for
101 the codebook search, and includes the sign information into the correlations.
103 The correlations are given by:
104 rr[i][j] = sum_{n=i}^{L-1} h[n-i] h[n-j]; i>=j; i,j=0,...,L-1
106 The sign information is included by:
107 rr[i][j] = rr[i][j]*sign[i]*sign[j]
109 ------------------------------------------------------------------------------
114 ------------------------------------------------------------------------------
117 cor_h.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
119 ------------------------------------------------------------------------------
123 Word16 h[], // (i) : impulse response of weighted synthesis
125 Word16 sign[], // (i) : sign of d[n]
126 Word16 rr[][L_CODE] // (o) : matrix of autocorrelation
129 Word16 i, j, k, dec, h2[L_CODE];
132 // Scaling for maximum precision
135 for (i = 0; i < L_CODE; i++)
136 s = L_mac (s, h[i], h[i]);
138 j = sub (extract_h (s), 32767);
141 for (i = 0; i < L_CODE; i++)
143 h2[i] = shr (h[i], 1);
149 k = extract_h (L_shl (Inv_sqrt (s), 7));
150 k = mult (k, 32440); // k = 0.99*k
152 for (i = 0; i < L_CODE; i++)
154 h2[i] = pv_round (L_shl (L_mult (h[i], k), 9));
161 for (k = 0; k < L_CODE; k++, i--)
163 s = L_mac (s, h2[k], h2[k]);
164 rr[i][i] = pv_round (s);
167 for (dec = 1; dec < L_CODE; dec++)
172 for (k = 0; k < (L_CODE - dec); k++, i--, j--)
174 s = L_mac (s, h2[k], h2[k + dec]);
175 rr[j][i] = mult (pv_round (s), mult (sign[i], sign[j]));
181 ---------------------------------------------------------------------------
183 [State any special notes, constraints or cautions for users of this function]
185 ------------------------------------------------------------------------------
189 Word16 h[], /* (i) : impulse response of weighted synthesis
191 Word16 sign[], /* (i) : sign of d[n] */
192 Word16 rr[][L_CODE], /* (o) : matrix of autocorrelation */
216 /* Scaling for maximum precision */
218 /* Initialize accumulator to 1 since left shift happens */
219 /* after the accumulation of the sum of squares (original */
220 /* code initialized s to 2) */
224 for (i = (L_CODE >> 1); i != 0 ; i--)
227 s = amrnb_fxp_mac_16_by_16bb((Word32) tmp1, (Word32) tmp1, s);
229 s = amrnb_fxp_mac_16_by_16bb((Word32) tmp1, (Word32) tmp1, s);
240 for (i = (L_CODE >> 1); i != 0; i--)
242 *(p_h2++) = *(p_h++) >> 1;
243 *(p_h2++) = *(p_h++) >> 1;
251 s = Inv_sqrt(s, pOverflow);
253 if (s < (Word32) 0x00ffffffL)
256 dec = (Word16)(((s >> 9) * 32440) >> 15);
260 dec = 32440; /* 0.99 */
266 for (i = (L_CODE >> 1); i != 0; i--)
268 *(p_h2++) = (Word16)((amrnb_fxp_mac_16_by_16bb((Word32) * (p_h++), (Word32) dec, 0x020L)) >> 6);
269 *(p_h2++) = (Word16)((amrnb_fxp_mac_16_by_16bb((Word32) * (p_h++), (Word32) dec, 0x020L)) >> 6);
272 /* build matrix rr[] */
278 rr1 = &rr[L_CODE-1][L_CODE-1];
280 for (i = L_CODE >> 1; i != 0 ; i--)
283 s = amrnb_fxp_mac_16_by_16bb((Word32) tmp1, (Word32) tmp1, s);
284 *rr1 = (Word16)((s + 0x00004000L) >> 15);
287 s = amrnb_fxp_mac_16_by_16bb((Word32) tmp1, (Word32) tmp1, s);
288 *rr1 = (Word16)((s + 0x00004000L) >> 15);
293 p_rr_ref1 = rr[L_CODE-1];
295 for (dec = 1; dec < L_CODE; dec += 2)
297 rr1 = &p_rr_ref1[L_CODE-1-dec];
299 rr2 = &rr[L_CODE-1-dec][L_CODE-1];
300 rr3 = &rr[L_CODE-1-(dec+1)][L_CODE-1];
305 p_sign1 = &sign[L_CODE - 1];
306 p_sign2 = &sign[L_CODE - 1 - dec];
311 for (i = (L_CODE - dec - 1); i != 0 ; i--)
313 s = amrnb_fxp_mac_16_by_16bb((Word32) * (p_h2), (Word32) * (p_h++), s);
314 s2 = amrnb_fxp_mac_16_by_16bb((Word32) * (p_h2++), (Word32) * (p_h), s2);
316 tmp1 = (Word16)((s + 0x00004000L) >> 15);
317 tmp11 = (Word16)((s2 + 0x00004000L) >> 15);
319 tmp2 = ((Word32) * (p_sign1) * *(p_sign2--)) >> 15;
320 tmp22 = ((Word32) * (p_sign1--) * *(p_sign2)) >> 15;
322 *rr2 = ((Word32) tmp1 * tmp2) >> 15;
324 *rr1 = ((Word32) tmp11 * tmp22) >> 15;
333 s = amrnb_fxp_mac_16_by_16bb((Word32) * (p_h2), (Word32) * (p_h), s);
335 tmp1 = (Word16)((s + 0x00004000L) >> 15);
337 tmp2 = ((Word32) * (p_sign1) * *(p_sign2)) >> 15;
338 *rr1 = ((Word32) tmp1 * tmp2) >> 15;