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  Pathname: ./audio/gsm-amr/c/src/d4_17pf.c
35  Functions: decode_4i40_17bits
36 
37      Date: 01/28/2002
38 
39 ------------------------------------------------------------------------------
40  REVISION HISTORY
41 
42  Description: Modified to place file in the correct template format. Eliminated
43  use of special functions to perform simple mathematical operations.
44 
45  Description: An incorrect comment in the original source lead me to implement
46  the calculation of pos[2] incorrectly.  The correct formula is pos2 =i*5+2,
47  not pos2 = i*5 + 1.
48 
49  Description:  Replaced "int" and/or "char" with OSCL defined types.
50 
51  Description: Added #ifdef __cplusplus around extern'ed table.
52 
53  Description:
54 
55 ------------------------------------------------------------------------------
56  MODULE DESCRIPTION
57 
58 
59  FUNCTION:  decode_4i40_17bits (decod_ACELP())
60 
61  PURPOSE:   Algebraic codebook decoder. For details about the encoding see
62             c4_17pf.c
63 */
64 
65 /*----------------------------------------------------------------------------
66 ; INCLUDES
67 ----------------------------------------------------------------------------*/
68 #include "typedef.h"
69 #include "basic_op.h"
70 #include "cnst.h"
71 #include "d4_17pf.h"
72 
73 /*--------------------------------------------------------------------------*/
74 #ifdef __cplusplus
75 extern "C"
76 {
77 #endif
78 
79     /*----------------------------------------------------------------------------
80     ; MACROS
81     ; Define module specific macros here
82     ----------------------------------------------------------------------------*/
83 
84     /*----------------------------------------------------------------------------
85     ; DEFINES
86     ; Include all pre-processor statements here. Include conditional
87     ; compile variables also.
88     ----------------------------------------------------------------------------*/
89 #define NB_PULSE 4           /* number of pulses  */
90 
91 
92     /*----------------------------------------------------------------------------
93     ; LOCAL FUNCTION DEFINITIONS
94     ; Function Prototype declaration
95     ----------------------------------------------------------------------------*/
96 
97     /*----------------------------------------------------------------------------
98     ; LOCAL VARIABLE DEFINITIONS
99     ; Variable declaration - defined here and used outside this module
100     ----------------------------------------------------------------------------*/
101 
102     /*----------------------------------------------------------------------------
103     ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
104     ; Declare variables used in this module but defined elsewhere
105     ----------------------------------------------------------------------------*/
106     extern const Word16 dgray[];
107 
108     /*--------------------------------------------------------------------------*/
109 #ifdef __cplusplus
110 }
111 #endif
112 
113 /*
114 ------------------------------------------------------------------------------
115  FUNCTION NAME: decode_4i40_17bits
116 ------------------------------------------------------------------------------
117  INPUT AND OUTPUT DEFINITIONS
118 
119  Inputs:
120     sign  -- Word16 -- signs of 3 pulses.
121     index -- Word16 -- Positions of the 3 pulses.
122 
123  Outputs:
124     cod[] -- array of type Word16 -- algebraic (fixed) codebook excitation
125 
126  Returns:
127     None
128 
129  Global Variables Used:
130     None
131 
132  Local Variables Needed:
133     None
134 
135 ------------------------------------------------------------------------------
136  FUNCTION DESCRIPTION
137 
138 
139 ------------------------------------------------------------------------------
140  REQUIREMENTS
141 
142  None
143 
144 ------------------------------------------------------------------------------
145  REFERENCES
146 
147  d4_17pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
148 
149 ------------------------------------------------------------------------------
150  PSEUDO-CODE
151 
152 
153 ------------------------------------------------------------------------------
154  RESOURCES USED [optional]
155 
156  When the code is written for a specific target processor the
157  the resources used should be documented below.
158 
159  HEAP MEMORY USED: x bytes
160 
161  STACK MEMORY USED: x bytes
162 
163  CLOCK CYCLES: (cycle count equation for this function) + (variable
164                 used to represent cycle count for each subroutine
165                 called)
166      where: (cycle count variable) = cycle count for [subroutine
167                                      name]
168 
169 ------------------------------------------------------------------------------
170  CAUTION [optional]
171  [State any special notes, constraints or cautions for users of this function]
172 
173 ------------------------------------------------------------------------------
174 */
175 
decode_4i40_17bits(Word16 sign,Word16 index,Word16 cod[])176 void decode_4i40_17bits(
177     Word16 sign,   /* i : signs of 4 pulses.                       */
178     Word16 index,  /* i : Positions of the 4 pulses.               */
179     Word16 cod[]   /* o : algebraic (fixed) codebook excitation    */
180 )
181 {
182     Word16 i;
183     Word16 j;
184 
185     Word16 pos[NB_PULSE];
186 
187     /* Index is a 13-bit value.  3 bits each correspond to the
188      * positions 0-2, with 4 bits allocated for position 3.
189      *
190      *
191      * [][][][] [][][] [][][] [][][]
192      *    |       |      |     |
193      *    |       |      |     |
194      *   pos3    pos2   pos1  pos0
195      */
196 
197     /* Decode the positions */
198 
199     i = index & 0x7;
200 
201     i = dgray[i];
202 
203     pos[0] = i * 5; /* pos0 =i*5 */
204 
205 
206     index >>= 3;
207 
208     i = index & 0x7;
209 
210     i = dgray[i];
211 
212     pos[1] = i * 5 + 1;  /* pos1 =i*5+1 */
213 
214 
215 
216     index >>= 3;
217 
218     i = index & 0x7;
219 
220     i = dgray[i];
221 
222     pos[2] = i * 5 + 2; /* pos2 =i*5+2 */
223 
224 
225 
226 
227 
228     index >>= 3;
229 
230     j = index & 0x1;
231 
232     index >>= 1;
233 
234     i = index & 0x7;
235 
236     i = dgray[i];
237 
238     pos[3] = i * 5 + 3 + j; /* pos3 =i*5+3+j */
239 
240 
241     /* decode the signs  and build the codeword */
242 
243     for (i = 0; i < L_SUBFR; i++)
244     {
245         cod[i] = 0;
246     }
247 
248     for (j = 0; j < NB_PULSE; j++)
249     {
250         i = sign & 0x1;
251 
252         /* This line is equivalent to...
253          *
254          *
255          *  if (i == 1)
256          *  {
257          *      cod[pos[j]] = 8191;
258          *  }
259          *  if (i == 0)
260          *  {
261          *      cod[pos[j]] = -8192;
262          *  }
263          */
264 
265         cod[pos[j]] = i * 16383 - 8192;
266 
267         sign >>= 1;
268     }
269 
270     return;
271 }
272