Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_wb / dec / src / oversamp_12k8_to_16k.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: oversamp_12k8_to_16k.cpp
35
36 ------------------------------------------------------------------------------
37  INPUT AND OUTPUT DEFINITIONS
38
39      int16 signal[],             input signal / output is divided by 16
40      int16 lg,                   lenght of signal
41      int16 mem[]                 in/out: memory (size=30)
42      int16 x[]                   scratch mem ( size= 60)
43
44 ------------------------------------------------------------------------------
45  FUNCTION DESCRIPTION
46
47  Oversamp_16k : oversampling from 12.8kHz to 16kHz.
48
49
50 ------------------------------------------------------------------------------
51  REQUIREMENTS
52
53
54 ------------------------------------------------------------------------------
55  REFERENCES
56
57 ------------------------------------------------------------------------------
58  PSEUDO-CODE
59
60 ------------------------------------------------------------------------------
61 */
62
63
64 /*----------------------------------------------------------------------------
65 ; INCLUDES
66 ----------------------------------------------------------------------------*/
67
68 #include "pv_amr_wb_type_defs.h"
69 #include "pvamrwbdecoder_basic_op.h"
70 #include "pvamrwbdecoder_acelp.h"
71 #include "pvamrwbdecoder_cnst.h"
72
73 /*----------------------------------------------------------------------------
74 ; MACROS
75 ; Define module specific macros here
76 ----------------------------------------------------------------------------*/
77
78
79 /*----------------------------------------------------------------------------
80 ; DEFINES
81 ; Include all pre-processor statements here. Include conditional
82 ; compile variables also.
83 ----------------------------------------------------------------------------*/
84
85 #define FAC4   4
86 #define FAC5   5
87 #define INV_FAC5   6554                    /* 1/5 in Q15 */
88 #define DOWN_FAC  26215                    /* 4/5 in Q15 */
89 #define UP_FAC    20480                    /* 5/4 in Q14 */
90 #define NB_COEF_DOWN  15
91 #define NB_COEF_UP    12
92 #define N_LOOP_COEF_UP    4
93
94 /*----------------------------------------------------------------------------
95 ; LOCAL FUNCTION DEFINITIONS
96 ; Function Prototype declaration
97 ----------------------------------------------------------------------------*/
98
99 #ifdef __cplusplus
100 extern "C"
101 {
102 #endif
103
104
105     /* Local functions */
106
107     void AmrWbUp_samp(
108         int16 * sig_d,                       /* input:  signal to oversampling  */
109         int16 * sig_u,                       /* output: oversampled signal      */
110         int16 L_frame                        /* input:  length of output        */
111     );
112
113
114     int16 AmrWbInterpol(                      /* return result of interpolation */
115         int16 * x,                           /* input vector                   */
116         const int16 * fir,                   /* filter coefficient             */
117         int16 nb_coef                        /* number of coefficients         */
118     );
119
120
121 #ifdef __cplusplus
122 }
123 #endif
124
125 /*----------------------------------------------------------------------------
126 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
127 ; Variable declaration - defined here and used outside this module
128 ----------------------------------------------------------------------------*/
129
130
131 /* 1/5 resolution interpolation filter  (in Q14)  */
132 /* -1.5dB @ 6kHz,    -6dB @ 6.4kHz, -10dB @ 6.6kHz,
133     -20dB @ 6.9kHz, -25dB @ 7kHz,   -55dB @ 8kHz  */
134
135
136 const int16 fir_up[4][24] =
137 {
138
139     {
140         -1,        12,       -33,       68,       -119,       191,
141         -291,       430,      -634,       963,     -1616,      3792,
142         15317,     -2496,      1288,      -809,       542,      -369,
143         247,      -160,        96,       -52,        23,        -6,
144     },
145     {
146         -4,        24,       -62,       124,      -213,       338,
147         -510,       752,     -1111,      1708,     -2974,      8219,
148         12368,     -3432,      1881,     -1204,       812,      -552,
149         368,      -235,       139,       -73,        30,        -7,
150     },
151     {
152         -7,        30,       -73,       139,      -235,       368,
153         -552,       812,     -1204,      1881,     -3432,     12368,
154         8219,     -2974,      1708,     -1111,       752,      -510,
155         338,      -213,       124,       -62,        24,        -4,
156     },
157     {
158         -6,        23,       -52,        96,      -160,       247,
159         -369,       542,      -809,      1288,     -2496,     15317,
160         3792,     -1616,       963,      -634,       430,      -291,
161         191,      -119,        68,       -33,        12,        -1,
162     }
163 };
164
165 /*----------------------------------------------------------------------------
166 ; EXTERNAL FUNCTION REFERENCES
167 ; Declare functions defined elsewhere and referenced in this module
168 ----------------------------------------------------------------------------*/
169
170 /*----------------------------------------------------------------------------
171 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
172 ; Declare variables used in this module but defined elsewhere
173 ----------------------------------------------------------------------------*/
174
175 /*----------------------------------------------------------------------------
176 ; FUNCTION CODE
177 ----------------------------------------------------------------------------*/
178
179
180 /* output: memory (2*NB_COEF_UP) set to zeros  */
181 void oversamp_12k8_to_16k_init(int16 mem[])
182 {
183     pv_memset((void *)mem, 0, (2*NB_COEF_UP)*sizeof(*mem));
184
185 }
186
187 /*----------------------------------------------------------------------------
188 ; FUNCTION CODE
189 ----------------------------------------------------------------------------*/
190
191 void oversamp_12k8_to_16k(
192     int16 sig12k8[],                     /* input:  signal to oversampling  */
193     int16 lg,                            /* input:  length of input         */
194     int16 sig16k[],                      /* output: oversampled signal      */
195     int16 mem[],                         /* in/out: memory (2*NB_COEF_UP)   */
196     int16 signal[]
197 )
198 {
199     int16 lg_up;
200
201     pv_memcpy((void *)signal,
202               (void *)mem,
203               (2*NB_COEF_UP)*sizeof(*mem));
204
205     pv_memcpy((void *)(signal + (2*NB_COEF_UP)),
206               (void *)sig12k8,
207               lg*sizeof(*sig12k8));
208
209     lg_up = lg + (lg >> 2); /* 5/4 of lg */
210
211     AmrWbUp_samp(signal + NB_COEF_UP, sig16k, lg_up);
212
213     pv_memcpy((void *)mem,
214               (void *)(signal + lg),
215               (2*NB_COEF_UP)*sizeof(*signal));
216
217     return;
218 }
219
220
221
222 /*----------------------------------------------------------------------------
223 ; FUNCTION CODE
224 ----------------------------------------------------------------------------*/
225
226
227 void AmrWbUp_samp(
228     int16 * sig_d,                       /* input:  signal to oversampling  */
229     int16 * sig_u,                       /* output: oversampled signal      */
230     int16 L_frame                        /* input:  length of output        */
231 )
232 {
233
234     int32 i;
235     int16 frac;
236     int16 * pt_sig_u = sig_u;
237
238     frac = 1;
239     for (int16 j = 0; j < L_frame; j++)
240     {
241         i = ((int32)j * INV_FAC5) >> 13;       /* integer part = pos * 1/5 */
242
243         frac--;
244         if (frac)
245         {
246             *(pt_sig_u++) = AmrWbInterpol(&sig_d[i],
247                                           fir_up[(FAC5-1) - frac],
248                                           N_LOOP_COEF_UP);
249         }
250         else
251         {
252             *(pt_sig_u++) = sig_d[i+12 - NB_COEF_UP ];
253             frac = FAC5;
254         }
255     }
256
257 }
258
259 /*----------------------------------------------------------------------------
260 ; FUNCTION CODE
261 ----------------------------------------------------------------------------*/
262
263
264 /* Fractional interpolation of signal at position (frac/resol) */
265
266
267 int16 AmrWbInterpol(                      /* return result of interpolation */
268     int16 * x,                           /* input vector                   */
269     const int16 *fir,                    /* filter coefficient             */
270     int16 nb_coef                        /* number of coefficients         */
271 )
272 {
273     int32 L_sum;
274     const int16 *pt_fir = fir;
275
276     int16 tmp1, tmp2, tmp3, tmp4;
277     int16 *pt_x = x - nb_coef - (nb_coef << 1) + 1;
278
279
280     tmp1 = *(pt_x++);
281     tmp2 = *(pt_x++);
282     tmp3 = *(pt_x++);
283     tmp4 = *(pt_x++);
284     L_sum = fxp_mac_16by16(tmp1, *(pt_fir++), 0x00002000L);
285     L_sum = fxp_mac_16by16(tmp2, *(pt_fir++), L_sum);
286     L_sum = fxp_mac_16by16(tmp3, *(pt_fir++), L_sum);
287     L_sum = fxp_mac_16by16(tmp4, *(pt_fir++), L_sum);
288     tmp1 = *(pt_x++);
289     tmp2 = *(pt_x++);
290     tmp3 = *(pt_x++);
291     tmp4 = *(pt_x++);
292     L_sum = fxp_mac_16by16(tmp1, *(pt_fir++), L_sum);
293     L_sum = fxp_mac_16by16(tmp2, *(pt_fir++), L_sum);
294     L_sum = fxp_mac_16by16(tmp3, *(pt_fir++), L_sum);
295     L_sum = fxp_mac_16by16(tmp4, *(pt_fir++), L_sum);
296     tmp1 = *(pt_x++);
297     tmp2 = *(pt_x++);
298     tmp3 = *(pt_x++);
299     tmp4 = *(pt_x++);
300     L_sum = fxp_mac_16by16(tmp1, *(pt_fir++), L_sum);
301     L_sum = fxp_mac_16by16(tmp2, *(pt_fir++), L_sum);
302     L_sum = fxp_mac_16by16(tmp3, *(pt_fir++), L_sum);
303     L_sum = fxp_mac_16by16(tmp4, *(pt_fir++), L_sum);
304     tmp1 = *(pt_x++);
305     tmp2 = *(pt_x++);
306     tmp3 = *(pt_x++);
307     tmp4 = *(pt_x++);
308     L_sum = fxp_mac_16by16(tmp1, *(pt_fir++), L_sum);
309     L_sum = fxp_mac_16by16(tmp2, *(pt_fir++), L_sum);
310     L_sum = fxp_mac_16by16(tmp3, *(pt_fir++), L_sum);
311     L_sum = fxp_mac_16by16(tmp4, *(pt_fir++), L_sum);
312     tmp1 = *(pt_x++);
313     tmp2 = *(pt_x++);
314     tmp3 = *(pt_x++);
315     tmp4 = *(pt_x++);
316     L_sum = fxp_mac_16by16(tmp1, *(pt_fir++), L_sum);
317     L_sum = fxp_mac_16by16(tmp2, *(pt_fir++), L_sum);
318     L_sum = fxp_mac_16by16(tmp3, *(pt_fir++), L_sum);
319     L_sum = fxp_mac_16by16(tmp4, *(pt_fir++), L_sum);
320     tmp1 = *(pt_x++);
321     tmp2 = *(pt_x++);
322     tmp3 = *(pt_x++);
323     tmp4 = *(pt_x++);
324     L_sum = fxp_mac_16by16(tmp1, *(pt_fir++), L_sum);
325     L_sum = fxp_mac_16by16(tmp2, *(pt_fir++), L_sum);
326     L_sum = fxp_mac_16by16(tmp3, *(pt_fir++), L_sum);
327     L_sum = fxp_mac_16by16(tmp4, *(pt_fir++), L_sum);
328
329
330     L_sum = shl_int32(L_sum, 2);               /* saturation can occur here */
331
332     return ((int16)(L_sum >> 16));
333 }
334