Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_wb / dec / src / isf_extrapolation.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.173
22     ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
23     Available from http://www.3gpp.org
24
25 (C) 2007, 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: isf_extrapolation.cpp
35
36 ------------------------------------------------------------------------------
37  INPUT AND OUTPUT DEFINITIONS
38
39     int16 HfIsf[]    (i/o)  isf vector
40
41 ------------------------------------------------------------------------------
42  FUNCTION DESCRIPTION
43
44     Conversion of 16th-order 12.8kHz ISF vector
45     into 20th-order 16kHz ISF vector
46
47 ------------------------------------------------------------------------------
48  REQUIREMENTS
49
50
51 ------------------------------------------------------------------------------
52  REFERENCES
53
54 ------------------------------------------------------------------------------
55  PSEUDO-CODE
56
57 ------------------------------------------------------------------------------
58 */
59
60
61 /*----------------------------------------------------------------------------
62 ; INCLUDES
63 ----------------------------------------------------------------------------*/
64
65 #include "pv_amr_wb_type_defs.h"
66 #include "pvamrwbdecoder_basic_op.h"
67 #include "pvamrwbdecoder_cnst.h"
68 #include "pvamrwbdecoder_acelp.h"
69 #include "pvamrwb_math_op.h"
70
71 /*----------------------------------------------------------------------------
72 ; MACROS
73 ; Define module specific macros here
74 ----------------------------------------------------------------------------*/
75
76
77 /*----------------------------------------------------------------------------
78 ; DEFINES
79 ; Include all pre-processor statements here. Include conditional
80 ; compile variables also.
81 ----------------------------------------------------------------------------*/
82 #define INV_LENGTH 2731                    /* 1/12 */
83
84 /*----------------------------------------------------------------------------
85 ; LOCAL FUNCTION DEFINITIONS
86 ; Function Prototype declaration
87 ----------------------------------------------------------------------------*/
88
89 /*----------------------------------------------------------------------------
90 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
91 ; Variable declaration - defined here and used outside this module
92 ----------------------------------------------------------------------------*/
93
94 /*----------------------------------------------------------------------------
95 ; EXTERNAL FUNCTION REFERENCES
96 ; Declare functions defined elsewhere and referenced in this module
97 ----------------------------------------------------------------------------*/
98
99 /*----------------------------------------------------------------------------
100 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
101 ; Declare variables used in this module but defined elsewhere
102 ----------------------------------------------------------------------------*/
103
104 /*----------------------------------------------------------------------------
105 ; FUNCTION CODE
106 ----------------------------------------------------------------------------*/
107
108
109 void isf_extrapolation(int16 HfIsf[])
110 {
111     int16 IsfDiff[M - 2];
112     int32 IsfCorr[3];
113     int32 L_tmp;
114     int16 coeff, mean, tmp, tmp2, tmp3;
115     int16 exp, exp2, hi, lo;
116     int16 i, MaxCorr;
117
118     HfIsf[M16k - 1] = HfIsf[M - 1];
119
120     /* Difference vector */
121     for (i = 1; i < (M - 1); i++)
122     {
123         IsfDiff[i - 1] = sub_int16(HfIsf[i], HfIsf[i - 1]);
124     }
125     L_tmp = 0;
126
127     /* Mean of difference vector */
128     for (i = 3; i < (M - 1); i++)
129     {
130         L_tmp = mac_16by16_to_int32(L_tmp, IsfDiff[i - 1], INV_LENGTH);
131
132     }
133     mean = amr_wb_round(L_tmp);
134
135     IsfCorr[0] = 0;
136
137     tmp = 0;
138     for (i = 0; i < (M - 2); i++)
139     {
140         if (IsfDiff[i] > tmp)
141         {
142             tmp = IsfDiff[i];
143         }
144     }
145     exp = norm_s(tmp);
146     for (i = 0; i < (M - 2); i++)
147     {
148         IsfDiff[i] = shl_int16(IsfDiff[i], exp);
149     }
150     mean = shl_int16(mean, exp);
151     for (i = 7; i < (M - 2); i++)
152     {
153         tmp2 = sub_int16(IsfDiff[i], mean);
154         tmp3 = sub_int16(IsfDiff[i - 2], mean);
155         L_tmp = mul_16by16_to_int32(tmp2, tmp3);
156         int32_to_dpf(L_tmp, &hi, &lo);
157         L_tmp = mpy_dpf_32(hi, lo, hi, lo);
158         IsfCorr[0] = add_int32(IsfCorr[0], L_tmp);
159     }
160     IsfCorr[1] = 0;
161     for (i = 7; i < (M - 2); i++)
162     {
163         tmp2 = sub_int16(IsfDiff[i], mean);
164         tmp3 = sub_int16(IsfDiff[i - 3], mean);
165         L_tmp = mul_16by16_to_int32(tmp2, tmp3);
166         int32_to_dpf(L_tmp, &hi, &lo);
167         L_tmp = mpy_dpf_32(hi, lo, hi, lo);
168         IsfCorr[1] = add_int32(IsfCorr[1], L_tmp);
169     }
170     IsfCorr[2] = 0;
171     for (i = 7; i < (M - 2); i++)
172     {
173         tmp2 = sub_int16(IsfDiff[i], mean);
174         tmp3 = sub_int16(IsfDiff[i - 4], mean);
175         L_tmp = mul_16by16_to_int32(tmp2, tmp3);
176         int32_to_dpf(L_tmp, &hi, &lo);
177         L_tmp = mpy_dpf_32(hi, lo, hi, lo);
178         IsfCorr[2] = add_int32(IsfCorr[2], L_tmp);
179     }
180
181     if (IsfCorr[0] > IsfCorr[1])
182     {
183         MaxCorr = 0;
184     }
185     else
186     {
187         MaxCorr = 1;
188     }
189
190
191     if (IsfCorr[2] > IsfCorr[MaxCorr])
192     {
193         MaxCorr = 2;
194     }
195
196     MaxCorr++;             /* Maximum correlation of difference vector */
197
198     for (i = M - 1; i < (M16k - 1); i++)
199     {
200         tmp = sub_int16(HfIsf[i - 1 - MaxCorr], HfIsf[i - 2 - MaxCorr]);
201         HfIsf[i] = add_int16(HfIsf[i - 1], tmp);
202     }
203
204     /* tmp=7965+(HfIsf[2]-HfIsf[3]-HfIsf[4])/6; */
205     tmp = add_int16(HfIsf[4], HfIsf[3]);
206     tmp = sub_int16(HfIsf[2], tmp);
207     tmp = mult_int16(tmp, 5461);
208     tmp += 20390;
209
210
211     if (tmp > 19456)
212     {                                      /* Maximum value of ISF should be at most 7600 Hz */
213         tmp = 19456;
214     }
215     tmp = sub_int16(tmp, HfIsf[M - 2]);
216     tmp2 = sub_int16(HfIsf[M16k - 2], HfIsf[M - 2]);
217
218     exp2 = norm_s(tmp2);
219     exp = norm_s(tmp);
220     exp--;
221     tmp <<= exp;
222     tmp2 <<= exp2;
223     coeff = div_16by16(tmp, tmp2);              /* Coefficient for stretching the ISF vector */
224     exp = exp2 - exp;
225
226     for (i = M - 1; i < (M16k - 1); i++)
227     {
228         tmp = mult_int16(sub_int16(HfIsf[i], HfIsf[i - 1]), coeff);
229         IsfDiff[i - (M - 1)] = shl_int16(tmp, exp);
230     }
231
232     for (i = M; i < (M16k - 1); i++)
233     {
234         /* The difference between ISF(n) and ISF(n-2) should be at least 500 Hz */
235         tmp = IsfDiff[i - (M - 1)] + IsfDiff[i - M] - 1280;
236
237         if (tmp < 0)
238         {
239
240             if (IsfDiff[i - (M - 1)] > IsfDiff[i - M])
241             {
242                 IsfDiff[i - M] = 1280 - IsfDiff[i - (M - 1)];
243             }
244             else
245             {
246                 IsfDiff[i - (M - 1)] = 1280 - IsfDiff[i - M];
247             }
248         }
249     }
250
251     for (i = M - 1; i < (M16k - 1); i++)
252     {
253         HfIsf[i] = add_int16(HfIsf[i - 1], IsfDiff[i - (M - 1)]);
254     }
255
256     for (i = 0; i < (M16k - 1); i++)
257     {
258         HfIsf[i] = mult_int16(HfIsf[i], 26214);  /* Scale the ISF vector correctly for 16000 kHz */
259     }
260
261     Isf_isp(HfIsf, HfIsf, M16k);
262
263     return;
264 }
265
266