Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / dec / src / dec_lag3.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: dec_lag3.cpp
35  Functions: Dec_lag3
36
37  ------------------------------------------------------------------------------
38  INPUT AND OUTPUT DEFINITIONS
39
40  Inputs:
41     index   -- Word16 -- received pitch index
42     t0_min  -- Word16 -- minimum of search range
43     t0_max  -- Word16 -- maximum of search range
44     i_subfr -- Word16 -- subframe flag
45     T0_prev -- Word16 -- integer pitch delay of last subframe
46                          used in 2nd and 4th subframes
47     flag4   -- Word16 -- flag for encoding with 4 bits
48
49  Outputs:
50
51     T0 -- Pointer to type Word16 -- integer part of pitch lag
52     T0_frac -- Pointer to type Word16 -- fractional part of pitch lag
53     pOverflow -- Pointer to type Flag -- Flag set when overflow occurs
54
55  Returns:
56     None.
57
58  Global Variables Used:
59     None
60
61  Local Variables Needed:
62     None
63
64
65               )
66 ------------------------------------------------------------------------------
67  FUNCTION DESCRIPTION
68
69  PURPOSE:  Decoding of fractional pitch lag with 1/3 resolution.
70            Extract the integer and fraction parts of the pitch lag from
71            the received adaptive codebook index.
72
73   See "Enc_lag3.c" for more details about the encoding procedure.
74
75   The fractional lag in 1st and 3rd subframes is encoded with 8 bits
76   while that in 2nd and 4th subframes is relatively encoded with 4, 5
77   and 6 bits depending on the mode.
78
79 ------------------------------------------------------------------------------
80  REQUIREMENTS
81
82
83
84 ------------------------------------------------------------------------------
85  REFERENCES
86
87  dec_lag3.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
88
89 ------------------------------------------------------------------------------
90  PSEUDO-CODE
91
92
93
94 ------------------------------------------------------------------------------
95 */
96
97
98 /*----------------------------------------------------------------------------
99 ; INCLUDES
100 ----------------------------------------------------------------------------*/
101 #include "dec_lag3.h"
102 #include "typedef.h"
103 #include "basic_op.h"
104
105 /*----------------------------------------------------------------------------
106 ; MACROS
107 ; Define module specific macros here
108 ----------------------------------------------------------------------------*/
109
110
111 /*----------------------------------------------------------------------------
112 ; DEFINES
113 ; Include all pre-processor statements here. Include conditional
114 ; compile variables also.
115 ----------------------------------------------------------------------------*/
116
117 /*----------------------------------------------------------------------------
118 ; LOCAL FUNCTION DEFINITIONS
119 ; Function Prototype declaration
120 ----------------------------------------------------------------------------*/
121
122
123 /*----------------------------------------------------------------------------
124 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
125 ; Variable declaration - defined here and used outside this module
126 ----------------------------------------------------------------------------*/
127
128 /*----------------------------------------------------------------------------
129 ; EXTERNAL FUNCTION REFERENCES
130 ; Declare functions defined elsewhere and referenced in this module
131 ----------------------------------------------------------------------------*/
132
133 /*----------------------------------------------------------------------------
134 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
135 ; Declare variables used in this module but defined elsewhere
136 ----------------------------------------------------------------------------*/
137
138 /*----------------------------------------------------------------------------
139 ; FUNCTION CODE
140 ----------------------------------------------------------------------------*/
141
142 void Dec_lag3(Word16 index,     /* i : received pitch index                 */
143               Word16 t0_min,    /* i : minimum of search range              */
144               Word16 t0_max,    /* i : maximum of search range              */
145               Word16 i_subfr,   /* i : subframe flag                        */
146               Word16 T0_prev,   /* i : integer pitch delay of last subframe
147                                        used in 2nd and 4th subframes        */
148               Word16 * T0,      /* o : integer part of pitch lag            */
149               Word16 * T0_frac, /* o : fractional part of pitch lag         */
150               Word16 flag4,     /* i : flag for encoding with 4 bits        */
151               Flag  *pOverflow  /* o : Flag set when overflow occurs        */
152              )
153 {
154     Word16 i;
155     Word16 tmp_lag;
156
157     if (i_subfr == 0)    /* if 1st or 3rd subframe */
158     {
159
160         if (index < 197)
161         {
162
163             tmp_lag = index + 2;
164
165             tmp_lag =
166                 mult(
167                     tmp_lag,
168                     10923,
169                     pOverflow);
170
171             i = tmp_lag + 19;
172
173             *T0 = i;
174
175             /* i = 3 * (*T0) */
176
177             i <<=  1;
178             i += *T0;
179
180             tmp_lag = index - i;
181
182             *T0_frac = tmp_lag + 58;
183         }
184         else
185         {
186             *T0 = index - 112;
187
188             *T0_frac = 0;
189         }
190
191     }
192     else
193     {  /* 2nd or 4th subframe */
194
195         if (flag4 == 0)
196         {
197
198             /* 'normal' decoding: either with 5 or 6 bit resolution */
199
200             i = index + 2;
201
202             i = ((Word32) i * 10923) >> 15;
203
204
205             i -= 1;
206
207             *T0 = i + t0_min;
208
209             /* i = 3* (*T0) */
210             i = i + (i << 1);
211
212             tmp_lag = index - 2;
213
214             *T0_frac = tmp_lag - i;
215         }
216         else
217         {
218
219             /* decoding with 4 bit resolution */
220
221             tmp_lag = T0_prev;
222
223             i =
224                 sub(
225                     tmp_lag,
226                     t0_min,
227                     pOverflow);
228
229             if (i > 5)
230             {
231                 tmp_lag = t0_min + 5;
232             }
233
234             i = t0_max - tmp_lag;
235
236             if (i > 4)
237             {
238                 tmp_lag = t0_max - 4;
239             }
240
241             if (index < 4)
242             {
243                 i = tmp_lag - 5;
244
245                 *T0 = i + index;
246
247                 *T0_frac = 0;
248             }
249             else
250             {
251                 /* 4 >= index < 12 */
252                 if (index < 12)
253                 {
254                     i = index - 5;
255                     i = ((Word32) i * 10923) >> 15;
256
257
258                     i--;
259
260                     *T0 =  i + tmp_lag;
261
262                     i = i + (i << 1);
263
264                     tmp_lag = index - 9;
265
266                     *T0_frac = tmp_lag - i;
267                 }
268                 else
269                 {
270                     i = index - 12;
271
272                     i = i + tmp_lag;
273
274                     *T0 = i + 1;
275
276                     *T0_frac = 0;
277                 }
278             }
279
280         } /* end if (decoding with 4 bit resolution) */
281     }
282
283     return;
284 }