Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / dec / src / a_refl.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: a_refl.cpp
35  Functions: a_refl
36
37 ------------------------------------------------------------------------------
38 */
39
40
41 /*----------------------------------------------------------------------------
42 ; INCLUDES
43 ----------------------------------------------------------------------------*/
44 #include "a_refl.h"
45 #include "typedef.h"
46 #include "cnst.h"
47 #include "basic_op.h"
48
49 /*----------------------------------------------------------------------------
50 ; MACROS [optional]
51 ; [Define module specific macros here]
52 ----------------------------------------------------------------------------*/
53
54 /*----------------------------------------------------------------------------
55 ; DEFINES [optional]
56 ; [Include all pre-processor statements here. Include conditional
57 ; compile variables also.]
58 ----------------------------------------------------------------------------*/
59
60 /*----------------------------------------------------------------------------
61 ; LOCAL FUNCTION DEFINITIONS
62 ; [List function prototypes here]
63 ----------------------------------------------------------------------------*/
64
65 /*----------------------------------------------------------------------------
66 ; LOCAL VARIABLE DEFINITIONS
67 ; [Variable declaration - defined here and used outside this module]
68 ----------------------------------------------------------------------------*/
69
70 /*
71 ------------------------------------------------------------------------------
72  FUNCTION NAME: AMREncode
73 ------------------------------------------------------------------------------
74  INPUT AND OUTPUT DEFINITIONS
75
76  Inputs:
77     a[] = pointer to directform coefficients of type Word16
78     refl[] = pointer to reflection coefficients of type Word16
79
80  Outputs:
81     pOverflow = 1 if overflow exists in the math operations else zero.
82
83  Returns:
84     None
85
86  Global Variables Used:
87     None
88
89  Local Variables Needed:
90     None
91
92 ------------------------------------------------------------------------------
93  FUNCTION DESCRIPTION
94
95      File             : a_refl.c
96      Purpose          : Convert from direct form coefficients to
97                         reflection coefficients
98
99 ------------------------------------------------------------------------------
100  REQUIREMENTS
101
102  None
103
104 ------------------------------------------------------------------------------
105  REFERENCES
106
107  [1] a_refl.c , 3GPP TS 26.101 version 4.1.0 Release 4, June 2001
108
109 ------------------------------------------------------------------------------
110  PSEUDO-CODE
111
112
113 void A_Refl(
114    Word16 a[],        // i   : Directform coefficients
115    Word16 refl[]      // o   : Reflection coefficients
116 )
117 {
118    // local variables
119    Word16 i,j;
120    Word16 aState[M];
121    Word16 bState[M];
122    Word16 normShift;
123    Word16 normProd;
124    Word32 L_acc;
125    Word16 scale;
126    Word32 L_temp;
127    Word16 temp;
128    Word16 mult;
129
130    // initialize states
131    for (i = 0; i < M; i++)
132    {
133       aState[i] = a[i];
134    }
135
136    // backward Levinson recursion
137    for (i = M-1; i >= 0; i--)
138    {
139       if (sub(abs_s(aState[i]), 4096) >= 0)
140       {
141          goto ExitRefl;
142       }
143
144       refl[i] = shl(aState[i], 3);
145
146       L_temp = L_mult(refl[i], refl[i]);
147       L_acc = L_sub(MAX_32, L_temp);
148
149       normShift = norm_l(L_acc);
150       scale = sub(15, normShift);
151
152       L_acc = L_shl(L_acc, normShift);
153       normProd = pv_round(L_acc);
154
155       mult = div_s(16384, normProd);
156
157       for (j = 0; j < i; j++)
158       {
159          L_acc = L_deposit_h(aState[j]);
160          L_acc = L_msu(L_acc, refl[i], aState[i-j-1]);
161
162          temp = pv_round(L_acc);
163          L_temp = L_mult(mult, temp);
164          L_temp = L_shr_r(L_temp, scale);
165
166          if (L_sub(L_abs(L_temp), 32767) > 0)
167          {
168             goto ExitRefl;
169          }
170
171          bState[j] = extract_l(L_temp);
172       }
173
174       for (j = 0; j < i; j++)
175       {
176          aState[j] = bState[j];
177       }
178    }
179    return;
180
181 ExitRefl:
182    for (i = 0; i < M; i++)
183    {
184       refl[i] = 0;
185    }
186 }
187
188 ------------------------------------------------------------------------------
189  CAUTION [optional]
190  [State any special notes, constraints or cautions for users of this function]
191
192 ------------------------------------------------------------------------------
193 */
194
195 void A_Refl(
196     Word16 a[],        /* i   : Directform coefficients */
197     Word16 refl[],     /* o   : Reflection coefficients */
198     Flag   *pOverflow
199 )
200 {
201     /* local variables */
202     Word16 i;
203     Word16 j;
204     Word16 aState[M];
205     Word16 bState[M];
206     Word16 normShift;
207     Word16 normProd;
208     Word32 L_acc;
209     Word16 scale;
210     Word32 L_temp;
211     Word16 temp;
212     Word16 mult;
213
214     /* initialize states */
215     for (i = 0; i < M; i++)
216     {
217         aState[i] = a[i];
218     }
219
220     /* backward Levinson recursion */
221     for (i = M - 1; i >= 0; i--)
222     {
223         if (abs_s(aState[i]) >= 4096)
224         {
225             for (i = 0; i < M; i++)
226             {
227                 refl[i] = 0;
228             }
229             break;
230         }
231
232         refl[i] = shl(aState[i], 3, pOverflow);
233
234         L_temp = L_mult(refl[i], refl[i], pOverflow);
235         L_acc = L_sub(MAX_32, L_temp, pOverflow);
236
237         normShift = norm_l(L_acc);
238         scale = 15 - normShift;
239
240         L_acc = L_shl(L_acc, normShift, pOverflow);
241         normProd = pv_round(L_acc, pOverflow);
242
243         mult = div_s(16384, normProd);
244
245         for (j = 0; j < i; j++)
246         {
247             L_acc = ((Word32)aState[j] << 16);
248             L_acc = L_msu(L_acc, refl[i], aState[i-j-1], pOverflow);
249
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);
253
254
255             Word32 L_tmp_abs = L_temp - (L_temp < 0);
256             L_tmp_abs = L_tmp_abs ^(L_tmp_abs >> 31);
257
258             if (L_tmp_abs > 32767)
259             {
260                 for (i = 0; i < M; i++)
261                 {
262                     refl[i] = 0;
263                 }
264                 break;
265             }
266
267             bState[j] = (Word16)(L_temp);
268         }
269
270         for (j = 0; j < i; j++)
271         {
272             aState[j] = bState[j];
273         }
274     }
275     return;
276 }
277
278
279
280
281
282
283