Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / dec / src / dec_lag6.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_lag6.cpp
35  Functions: Dec_lag6
36
37  ------------------------------------------------------------------------------
38  INPUT AND OUTPUT DEFINITIONS
39
40  Inputs:
41     index   -- Word16 -- received pitch index
42     pit_min  -- Word16 -- minimum pitch lag
43     pit_max  -- Word16 -- maximum pitch lag
44     i_subfr -- Word16 -- subframe flag
45     T0 -- Pointer to type Word16 -- integer part of pitch lag
46
47  Outputs:
48
49     T0 -- Pointer to type Word16 -- integer part of pitch lag
50     T0_frac -- Pointer to type Word16 -- fractional part of pitch lag
51     pOverflow -- Pointer to type Flag -- Flag set when overflow occurs
52
53  Returns:
54     None.
55
56  Global Variables Used:
57     None
58
59  Local Variables Needed:
60     None
61
62 ------------------------------------------------------------------------------
63  FUNCTION DESCRIPTION
64
65  PURPOSE:  Decoding of fractional pitch lag with 1/6 resolution.
66            Extract the integer and fraction parts of the pitch lag from
67            the received adaptive codebook index.
68
69   See "Enc_lag6.c" for more details about the encoding procedure.
70
71   The fractional lag in 1st and 3rd subframes is encoded with 9 bits
72   while that in 2nd and 4th subframes is relatively encoded with 6 bits.
73   Note that in relative encoding only 61 values are used. If the
74   decoder receives 61, 62, or 63 as the relative pitch index, it means
75   that a transmission error occurred. In this case, the pitch lag from
76   previous subframe (actually from previous frame) is used.
77
78 ------------------------------------------------------------------------------
79  REQUIREMENTS
80
81
82
83 ------------------------------------------------------------------------------
84  REFERENCES
85
86  dec_lag6.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
87
88 ------------------------------------------------------------------------------
89  PSEUDO-CODE
90
91
92
93 ------------------------------------------------------------------------------
94 */
95
96
97 /*----------------------------------------------------------------------------
98 ; INCLUDES
99 ----------------------------------------------------------------------------*/
100 #include "dec_lag6.h"
101 #include "typedef.h"
102 #include "basic_op.h"
103
104 /*----------------------------------------------------------------------------
105 ; MACROS
106 ; Define module specific macros here
107 ----------------------------------------------------------------------------*/
108
109
110 /*----------------------------------------------------------------------------
111 ; DEFINES
112 ; Include all pre-processor statements here. Include conditional
113 ; compile variables also.
114 ----------------------------------------------------------------------------*/
115
116 /*----------------------------------------------------------------------------
117 ; LOCAL FUNCTION DEFINITIONS
118 ; Function Prototype declaration
119 ----------------------------------------------------------------------------*/
120
121
122 /*----------------------------------------------------------------------------
123 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
124 ; Variable declaration - defined here and used outside this module
125 ----------------------------------------------------------------------------*/
126
127 /*----------------------------------------------------------------------------
128 ; EXTERNAL FUNCTION REFERENCES
129 ; Declare functions defined elsewhere and referenced in this module
130 ----------------------------------------------------------------------------*/
131
132 /*----------------------------------------------------------------------------
133 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
134 ; Declare variables used in this module but defined elsewhere
135 ----------------------------------------------------------------------------*/
136
137 /*----------------------------------------------------------------------------
138 ; FUNCTION CODE
139 ----------------------------------------------------------------------------*/
140
141 void Dec_lag6(
142     Word16 index,      /* input : received pitch index           */
143     Word16 pit_min,    /* input : minimum pitch lag              */
144     Word16 pit_max,    /* input : maximum pitch lag              */
145     Word16 i_subfr,    /* input : subframe flag                  */
146     Word16 *T0,        /* in/out: integer part of pitch lag      */
147     Word16 *T0_frac,   /* output: fractional part of pitch lag   */
148     Flag   *pOverflow  /* o : Flag set when overflow occurs      */
149 )
150 {
151     Word16 i;
152     Word16 T0_min;
153     Word16 T0_max;
154     Word16 k;
155
156     if (i_subfr == 0)          /* if 1st or 3rd subframe */
157     {
158         if (index < 463)
159         {
160             /* T0 = (index+5)/6 + 17 */
161             i = index + 5;
162             i = ((Word32) i * 5462) >> 15;
163
164
165             i += 17;
166
167             *T0 = i;
168
169             /* i = 3* (*T0) */
170
171             i <<= 1;
172             i += *T0;
173
174             /* *T0_frac = index - T0*6 + 105 */
175
176             i <<= 1;
177
178             i = index - i;
179
180             *T0_frac = i + 105;
181         }
182         else
183         {
184             *T0 = index - 368;
185
186             *T0_frac = 0;
187         }
188     }
189     else       /* second or fourth subframe */
190     {
191         /* find T0_min and T0_max for 2nd (or 4th) subframe */
192
193         T0_min = *T0 - 5;
194
195         if (T0_min < pit_min)
196         {
197             T0_min = pit_min;
198         }
199
200         T0_max = T0_min + 9;
201
202         if (T0_max > pit_max)
203         {
204             T0_max = pit_max;
205
206             T0_min = T0_max - 9;
207         }
208
209         /* i = (index+5)/6 - 1 */
210         i = index + 5;
211
212         i = ((Word32) i * 5462) >> 15;
213
214
215         i -= 1;
216
217         *T0 = i + T0_min;
218
219         /* i = 3* (*T0) */
220
221         i = i + (i << 1);
222
223         i <<= 1;
224
225         k = index - 3;
226
227         *T0_frac = k - i;
228     }
229 }