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 ------------------------------------------------------------------------------
37 ------------------------------------------------------------------------------
41 /*----------------------------------------------------------------------------
43 ----------------------------------------------------------------------------*/
49 /*----------------------------------------------------------------------------
51 ; [Define module specific macros here]
52 ----------------------------------------------------------------------------*/
54 /*----------------------------------------------------------------------------
56 ; [Include all pre-processor statements here. Include conditional
57 ; compile variables also.]
58 ----------------------------------------------------------------------------*/
60 /*----------------------------------------------------------------------------
61 ; LOCAL FUNCTION DEFINITIONS
62 ; [List function prototypes here]
63 ----------------------------------------------------------------------------*/
65 /*----------------------------------------------------------------------------
66 ; LOCAL VARIABLE DEFINITIONS
67 ; [Variable declaration - defined here and used outside this module]
68 ----------------------------------------------------------------------------*/
71 ------------------------------------------------------------------------------
72 FUNCTION NAME: AMREncode
73 ------------------------------------------------------------------------------
74 INPUT AND OUTPUT DEFINITIONS
77 a[] = pointer to directform coefficients of type Word16
78 refl[] = pointer to reflection coefficients of type Word16
81 pOverflow = 1 if overflow exists in the math operations else zero.
86 Global Variables Used:
89 Local Variables Needed:
92 ------------------------------------------------------------------------------
96 Purpose : Convert from direct form coefficients to
97 reflection coefficients
99 ------------------------------------------------------------------------------
104 ------------------------------------------------------------------------------
107 [1] a_refl.c , 3GPP TS 26.101 version 4.1.0 Release 4, June 2001
109 ------------------------------------------------------------------------------
114 Word16 a[], // i : Directform coefficients
115 Word16 refl[] // o : Reflection coefficients
131 for (i = 0; i < M; i++)
136 // backward Levinson recursion
137 for (i = M-1; i >= 0; i--)
139 if (sub(abs_s(aState[i]), 4096) >= 0)
144 refl[i] = shl(aState[i], 3);
146 L_temp = L_mult(refl[i], refl[i]);
147 L_acc = L_sub(MAX_32, L_temp);
149 normShift = norm_l(L_acc);
150 scale = sub(15, normShift);
152 L_acc = L_shl(L_acc, normShift);
153 normProd = pv_round(L_acc);
155 mult = div_s(16384, normProd);
157 for (j = 0; j < i; j++)
159 L_acc = L_deposit_h(aState[j]);
160 L_acc = L_msu(L_acc, refl[i], aState[i-j-1]);
162 temp = pv_round(L_acc);
163 L_temp = L_mult(mult, temp);
164 L_temp = L_shr_r(L_temp, scale);
166 if (L_sub(L_abs(L_temp), 32767) > 0)
171 bState[j] = extract_l(L_temp);
174 for (j = 0; j < i; j++)
176 aState[j] = bState[j];
182 for (i = 0; i < M; i++)
188 ------------------------------------------------------------------------------
190 [State any special notes, constraints or cautions for users of this function]
192 ------------------------------------------------------------------------------
196 Word16 a[], /* i : Directform coefficients */
197 Word16 refl[], /* o : Reflection coefficients */
201 /* local variables */
214 /* initialize states */
215 for (i = 0; i < M; i++)
220 /* backward Levinson recursion */
221 for (i = M - 1; i >= 0; i--)
223 if (abs_s(aState[i]) >= 4096)
225 for (i = 0; i < M; i++)
232 refl[i] = shl(aState[i], 3, pOverflow);
234 L_temp = L_mult(refl[i], refl[i], pOverflow);
235 L_acc = L_sub(MAX_32, L_temp, pOverflow);
237 normShift = norm_l(L_acc);
238 scale = 15 - normShift;
240 L_acc = L_shl(L_acc, normShift, pOverflow);
241 normProd = pv_round(L_acc, pOverflow);
243 mult = div_s(16384, normProd);
245 for (j = 0; j < i; j++)
247 L_acc = ((Word32)aState[j] << 16);
248 L_acc = L_msu(L_acc, refl[i], aState[i-j-1], pOverflow);
250 temp = pv_round(L_acc, pOverflow);
251 L_temp = L_mult(mult, temp, pOverflow);
252 L_temp = L_shr_r(L_temp, scale, pOverflow);
255 Word32 L_tmp_abs = L_temp - (L_temp < 0);
256 L_tmp_abs = L_tmp_abs ^(L_tmp_abs >> 31);
258 if (L_tmp_abs > 32767)
260 for (i = 0; i < M; i++)
267 bState[j] = (Word16)(L_temp);
270 for (j = 0; j < i; j++)
272 aState[j] = bState[j];