Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / src / pred_lt.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  Filename: pred_lt.cpp
31
32 ------------------------------------------------------------------------------
33 */
34
35 /*----------------------------------------------------------------------------
36 ; INCLUDES
37 ----------------------------------------------------------------------------*/
38 #include "pred_lt.h"
39 #include "cnst.h"
40
41 /*----------------------------------------------------------------------------
42 ; MACROS
43 ; Define module specific macros here
44 ----------------------------------------------------------------------------*/
45
46 /*----------------------------------------------------------------------------
47 ; DEFINES
48 ; Include all pre-processor statements here. Include conditional
49 ; compile variables also.
50 ----------------------------------------------------------------------------*/
51 #define UP_SAMP_MAX  6
52 #define L_INTER10    (L_INTERPOL-1)
53 #define FIR_SIZE     (UP_SAMP_MAX*L_INTER10+1)
54
55 /*----------------------------------------------------------------------------
56 ; LOCAL FUNCTION DEFINITIONS
57 ; Function Prototype declaration
58 ----------------------------------------------------------------------------*/
59
60 /*----------------------------------------------------------------------------
61 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
62 ; Variable declaration - defined here and used outside this module
63 ----------------------------------------------------------------------------*/
64
65 /* 1/6 resolution interpolation filter  (-3 dB at 3600 Hz) */
66 /* Note: the 1/3 resolution filter is simply a subsampled
67  *       version of the 1/6 resolution filter, i.e. it uses
68  *       every second coefficient:
69  *
70  *          inter_3l[k] = inter_6[2*k], 0 <= k <= 3*L_INTER10
71  */
72
73 const Word16 inter_6_pred_lt[FIR_SIZE] =
74 {
75     29443,
76     28346, 25207, 20449, 14701,  8693,  3143,
77     -1352, -4402, -5865, -5850, -4673, -2783,
78     -672,  1211,  2536,  3130,  2991,  2259,
79     1170,     0, -1001, -1652, -1868, -1666,
80     -1147,  -464,   218,   756,  1060,  1099,
81     904,   550,   135,  -245,  -514,  -634,
82     -602,  -451,  -231,     0,   191,   308,
83     340,   296,   198,    78,   -36,  -120,
84     -163,  -165,  -132,   -79,   -19,    34,
85     73,    91,    89,    70,    38,     0
86 };
87
88
89 /*
90 ------------------------------------------------------------------------------
91  FUNCTION NAME: Pred_lt_3or6
92 ------------------------------------------------------------------------------
93  INPUT AND OUTPUT DEFINITIONS
94
95  Inputs:
96     exc = buffer containing the excitation (Word16)
97     T0 = integer pitch lag (Word16)
98     frac = fraction of lag (Word16)
99     L_subfr = number of samples per subframe (Word16)
100     flag3 = flag to indicate the upsampling rate; if set, upsampling
101             rate is 3, otherwise, upsampling rate is 6 (Word16)
102
103     pOverflow = pointer to overflow (Flag)
104
105  Returns:
106     None
107
108  Outputs:
109     exc buffer contains the newly formed adaptive codebook excitation
110     pOverflow -> 1 if the add operation resulted in overflow
111
112  Global Variables Used:
113     inter_6_pred_lt = (1/6) resolution interpolation filter table (Word16)
114
115  Local Variables Needed:
116     None
117
118 ------------------------------------------------------------------------------
119  FUNCTION DESCRIPTION
120
121  This function computes the result of long term prediction with fractional
122  interpolation of resolution 1/3 or 1/6. (Interpolated past excitation).
123
124  The past excitation signal at the given delay is interpolated at
125  the given fraction to build the adaptive codebook excitation.
126  On return exc[0..L_subfr-1] contains the interpolated signal
127  (adaptive codebook excitation).
128
129 ------------------------------------------------------------------------------
130  REQUIREMENTS
131
132  None
133
134 ------------------------------------------------------------------------------
135  REFERENCES
136
137  pred_lt.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
138
139 ------------------------------------------------------------------------------
140  PSEUDO-CODE
141
142 void Pred_lt_3or6 (
143     Word16 exc[],     // in/out: excitation buffer
144     Word16 T0,        // input : integer pitch lag
145     Word16 frac,      // input : fraction of lag
146     Word16 L_subfr,   // input : subframe size
147     Word16 flag3      // input : if set, upsampling rate = 3 (6 otherwise)
148 )
149 {
150     Word16 i, j, k;
151     Word16 *pX0, *pX1, *pX2;
152     const Word16 *pC1, *pC2;
153     Word32 s;
154
155     pX0 = &exc[-T0];
156
157     frac = negate (frac);
158     if (flag3 != 0)
159     {
160       frac = shl (frac, 1);   // inter_3l[k] = inter_6[2*k] -> k' = 2*k
161     }
162
163     if (frac < 0)
164     {
165         frac = add (frac, UP_SAMP_MAX);
166         pX0--;
167     }
168
169     for (j = 0; j < L_subfr; j++)
170     {
171         pX1 = pX0++;
172         pX2 = pX0;
173         pC1 = &inter_6[frac];
174         pC2 = &inter_6[sub (UP_SAMP_MAX, frac)];
175
176         s = 0;
177         for (i = 0, k = 0; i < L_INTER10; i++, k += UP_SAMP_MAX)
178         {
179             s = L_mac (s, pX1[-i], pC1[k]);
180             s = L_mac (s, pX2[i], pC2[k]);
181         }
182
183         exc[j] = pv_round (s);
184     }
185
186     return;
187 }
188
189 ------------------------------------------------------------------------------
190  CAUTION [optional]
191  [State any special notes, constraints or cautions for users of this function]
192
193 ------------------------------------------------------------------------------
194 */
195
196 OSCL_EXPORT_REF void Pred_lt_3or6(
197     Word16 exc[],     /* in/out: excitation buffer                          */
198     Word16 T0,        /* input : integer pitch lag                          */
199     Word16 frac,      /* input : fraction of lag                            */
200     Word16 L_subfr,   /* input : subframe size                              */
201     Word16 flag3,     /* input : if set, upsampling rate = 3 (6 otherwise)  */
202     Flag  *pOverflow  /* output: if set, overflow occurred in this function */
203 )
204 {
205     register Word16 i;
206     register Word16 j;
207     register Word16 k;
208
209     Word16 *pX0;
210     Word16 *pX2;
211     Word16 *pX3;
212     Word16 *p_exc;
213     Word16 *pC1;
214     const Word16 *pC1_ref;
215     const Word16 *pC2_ref;
216
217     Word16 Coeff_1[(L_INTER10<<1)];
218
219     Word32 s1;
220     Word32 s2;
221     OSCL_UNUSED_ARG(pOverflow);
222
223     pX0 = &(exc[-T0]);
224
225     /* frac goes between -3 and 3 */
226
227     frac = -frac;
228
229     if (flag3 != 0)
230     {
231         frac <<= 1;   /* inter_3l[k] = inter_6[2*k] -> k' = 2*k */
232     }
233
234     if (frac < 0)
235     {
236         frac += UP_SAMP_MAX;
237         pX0--;
238     }
239
240     pC1_ref = &inter_6_pred_lt[frac];
241     pC2_ref = &inter_6_pred_lt[UP_SAMP_MAX-frac];
242
243
244     pC1 = Coeff_1;
245
246     k = 0;
247
248     for (i = L_INTER10 >> 1; i > 0; i--)
249     {
250         *(pC1++) = pC1_ref[k];
251         *(pC1++) = pC2_ref[k];
252         k += UP_SAMP_MAX;
253         *(pC1++) = pC1_ref[k];
254         *(pC1++) = pC2_ref[k];
255         k += UP_SAMP_MAX;
256
257     }
258
259     p_exc = exc;
260
261     for (j = (L_subfr >> 1); j != 0 ; j--)
262     {
263         pX0++;
264         pX2 = pX0;
265         pX3 = pX0++;
266
267         pC1 = Coeff_1;
268
269         s1  = 0x00004000L;
270         s2  = 0x00004000L;
271
272         for (i = L_INTER10 >> 1; i > 0; i--)
273         {
274             s2 += ((Word32) * (pX3--)) * *(pC1);
275             s1 += ((Word32) * (pX3)) * *(pC1++);
276             s1 += ((Word32) * (pX2++)) * *(pC1);
277             s2 += ((Word32) * (pX2)) * *(pC1++);
278             s2 += ((Word32) * (pX3--)) * *(pC1);
279             s1 += ((Word32) * (pX3)) * *(pC1++);
280             s1 += ((Word32) * (pX2++)) * *(pC1);
281             s2 += ((Word32) * (pX2)) * *(pC1++);
282
283         } /* for (i = L_INTER10>>1; i > 0; i--) */
284
285         *(p_exc++) = (Word16)(s1 >> 15);
286         *(p_exc++) = (Word16)(s2 >> 15);
287
288     } /* for (j = (L_subfr>>1); j != 0 ; j--) */
289
290     return;
291 }