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 - Wideband (AMR-WB) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
30 ------------------------------------------------------------------------------
34 Filename: isf_extrapolation.cpp
36 ------------------------------------------------------------------------------
37 INPUT AND OUTPUT DEFINITIONS
39 int16 HfIsf[] (i/o) isf vector
41 ------------------------------------------------------------------------------
44 Conversion of 16th-order 12.8kHz ISF vector
45 into 20th-order 16kHz ISF vector
47 ------------------------------------------------------------------------------
51 ------------------------------------------------------------------------------
54 ------------------------------------------------------------------------------
57 ------------------------------------------------------------------------------
61 /*----------------------------------------------------------------------------
63 ----------------------------------------------------------------------------*/
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"
71 /*----------------------------------------------------------------------------
73 ; Define module specific macros here
74 ----------------------------------------------------------------------------*/
77 /*----------------------------------------------------------------------------
79 ; Include all pre-processor statements here. Include conditional
80 ; compile variables also.
81 ----------------------------------------------------------------------------*/
82 #define INV_LENGTH 2731 /* 1/12 */
84 /*----------------------------------------------------------------------------
85 ; LOCAL FUNCTION DEFINITIONS
86 ; Function Prototype declaration
87 ----------------------------------------------------------------------------*/
89 /*----------------------------------------------------------------------------
90 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
91 ; Variable declaration - defined here and used outside this module
92 ----------------------------------------------------------------------------*/
94 /*----------------------------------------------------------------------------
95 ; EXTERNAL FUNCTION REFERENCES
96 ; Declare functions defined elsewhere and referenced in this module
97 ----------------------------------------------------------------------------*/
99 /*----------------------------------------------------------------------------
100 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
101 ; Declare variables used in this module but defined elsewhere
102 ----------------------------------------------------------------------------*/
104 /*----------------------------------------------------------------------------
106 ----------------------------------------------------------------------------*/
109 void isf_extrapolation(int16 HfIsf[])
111 int16 IsfDiff[M - 2];
114 int16 coeff, mean, tmp, tmp2, tmp3;
115 int16 exp, exp2, hi, lo;
118 HfIsf[M16k - 1] = HfIsf[M - 1];
120 /* Difference vector */
121 for (i = 1; i < (M - 1); i++)
123 IsfDiff[i - 1] = sub_int16(HfIsf[i], HfIsf[i - 1]);
127 /* Mean of difference vector */
128 for (i = 3; i < (M - 1); i++)
130 L_tmp = mac_16by16_to_int32(L_tmp, IsfDiff[i - 1], INV_LENGTH);
133 mean = amr_wb_round(L_tmp);
138 for (i = 0; i < (M - 2); i++)
140 if (IsfDiff[i] > tmp)
146 for (i = 0; i < (M - 2); i++)
148 IsfDiff[i] = shl_int16(IsfDiff[i], exp);
150 mean = shl_int16(mean, exp);
151 for (i = 7; i < (M - 2); i++)
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);
161 for (i = 7; i < (M - 2); i++)
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);
171 for (i = 7; i < (M - 2); i++)
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);
181 if (IsfCorr[0] > IsfCorr[1])
191 if (IsfCorr[2] > IsfCorr[MaxCorr])
196 MaxCorr++; /* Maximum correlation of difference vector */
198 for (i = M - 1; i < (M16k - 1); i++)
200 tmp = sub_int16(HfIsf[i - 1 - MaxCorr], HfIsf[i - 2 - MaxCorr]);
201 HfIsf[i] = add_int16(HfIsf[i - 1], tmp);
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);
212 { /* Maximum value of ISF should be at most 7600 Hz */
215 tmp = sub_int16(tmp, HfIsf[M - 2]);
216 tmp2 = sub_int16(HfIsf[M16k - 2], HfIsf[M - 2]);
223 coeff = div_16by16(tmp, tmp2); /* Coefficient for stretching the ISF vector */
226 for (i = M - 1; i < (M16k - 1); i++)
228 tmp = mult_int16(sub_int16(HfIsf[i], HfIsf[i - 1]), coeff);
229 IsfDiff[i - (M - 1)] = shl_int16(tmp, exp);
232 for (i = M; i < (M16k - 1); i++)
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;
240 if (IsfDiff[i - (M - 1)] > IsfDiff[i - M])
242 IsfDiff[i - M] = 1280 - IsfDiff[i - (M - 1)];
246 IsfDiff[i - (M - 1)] = 1280 - IsfDiff[i - M];
251 for (i = M - 1; i < (M16k - 1); i++)
253 HfIsf[i] = add_int16(HfIsf[i - 1], IsfDiff[i - (M - 1)]);
256 for (i = 0; i < (M16k - 1); i++)
258 HfIsf[i] = mult_int16(HfIsf[i], 26214); /* Scale the ISF vector correctly for 16000 kHz */
261 Isf_isp(HfIsf, HfIsf, M16k);