Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / dec / src / d4_17pf.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: d4_17pf.cpp
35  Functions: decode_4i40_17bits
36
37 ------------------------------------------------------------------------------
38  MODULE DESCRIPTION
39
40
41  FUNCTION:  decode_4i40_17bits (decod_ACELP())
42
43  PURPOSE:   Algebraic codebook decoder. For details about the encoding see
44             c4_17pf.c
45 */
46
47 /*----------------------------------------------------------------------------
48 ; INCLUDES
49 ----------------------------------------------------------------------------*/
50 #include "typedef.h"
51 #include "basic_op.h"
52 #include "cnst.h"
53 #include "d4_17pf.h"
54
55 /*--------------------------------------------------------------------------*/
56 #ifdef __cplusplus
57 extern "C"
58 {
59 #endif
60
61     /*----------------------------------------------------------------------------
62     ; MACROS
63     ; Define module specific macros here
64     ----------------------------------------------------------------------------*/
65
66     /*----------------------------------------------------------------------------
67     ; DEFINES
68     ; Include all pre-processor statements here. Include conditional
69     ; compile variables also.
70     ----------------------------------------------------------------------------*/
71 #define NB_PULSE 4           /* number of pulses  */
72
73
74     /*----------------------------------------------------------------------------
75     ; LOCAL FUNCTION DEFINITIONS
76     ; Function Prototype declaration
77     ----------------------------------------------------------------------------*/
78
79     /*----------------------------------------------------------------------------
80     ; LOCAL VARIABLE DEFINITIONS
81     ; Variable declaration - defined here and used outside this module
82     ----------------------------------------------------------------------------*/
83
84     /*----------------------------------------------------------------------------
85     ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
86     ; Declare variables used in this module but defined elsewhere
87     ----------------------------------------------------------------------------*/
88
89     /*--------------------------------------------------------------------------*/
90 #ifdef __cplusplus
91 }
92 #endif
93
94 /*
95 ------------------------------------------------------------------------------
96  FUNCTION NAME: decode_4i40_17bits
97 ------------------------------------------------------------------------------
98  INPUT AND OUTPUT DEFINITIONS
99
100  Inputs:
101     sign  -- Word16 -- signs of 3 pulses.
102     index -- Word16 -- Positions of the 3 pulses.
103
104  Outputs:
105     cod[] -- array of type Word16 -- algebraic (fixed) codebook excitation
106
107  Returns:
108     None
109
110  Global Variables Used:
111     None
112
113  Local Variables Needed:
114     None
115
116 ------------------------------------------------------------------------------
117  FUNCTION DESCRIPTION
118
119
120 ------------------------------------------------------------------------------
121  REQUIREMENTS
122
123  None
124
125 ------------------------------------------------------------------------------
126  REFERENCES
127
128  d4_17pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
129
130 ------------------------------------------------------------------------------
131  PSEUDO-CODE
132
133
134 ------------------------------------------------------------------------------
135  CAUTION [optional]
136  [State any special notes, constraints or cautions for users of this function]
137
138 ------------------------------------------------------------------------------
139 */
140
141 void decode_4i40_17bits(
142     Word16 sign,   /* i : signs of 4 pulses.                       */
143     Word16 index,  /* i : Positions of the 4 pulses.               */
144     const Word16* dgray_ptr, /* i : Pointer to read-only table         */
145     Word16 cod[]   /* o : algebraic (fixed) codebook excitation    */
146 )
147 {
148     Word16 i;
149     Word16 j;
150
151     Word16 pos[NB_PULSE];
152
153     /* Index is a 13-bit value.  3 bits each correspond to the
154      * positions 0-2, with 4 bits allocated for position 3.
155      *
156      *
157      * [][][][] [][][] [][][] [][][]
158      *    |       |      |     |
159      *    |       |      |     |
160      *   pos3    pos2   pos1  pos0
161      */
162
163     /* Decode the positions */
164
165     i = index & 0x7;
166
167     i = dgray_ptr[i];
168
169     pos[0] = i * 5; /* pos0 =i*5 */
170
171
172     index >>= 3;
173
174     i = index & 0x7;
175
176     i = dgray_ptr[i];
177
178     pos[1] = i * 5 + 1;  /* pos1 =i*5+1 */
179
180
181
182     index >>= 3;
183
184     i = index & 0x7;
185
186     i = dgray_ptr[i];
187
188     pos[2] = i * 5 + 2; /* pos2 =i*5+2 */
189
190
191
192
193
194     index >>= 3;
195
196     j = index & 0x1;
197
198     index >>= 1;
199
200     i = index & 0x7;
201
202     i = dgray_ptr[i];
203
204     pos[3] = i * 5 + 3 + j; /* pos3 =i*5+3+j */
205
206
207     /* decode the signs  and build the codeword */
208
209     for (i = 0; i < L_SUBFR; i++)
210     {
211         cod[i] = 0;
212     }
213
214     for (j = 0; j < NB_PULSE; j++)
215     {
216         i = sign & 0x1;
217
218         /* This line is equivalent to...
219          *
220          *
221          *  if (i == 1)
222          *  {
223          *      cod[pos[j]] = 8191;
224          *  }
225          *  if (i == 0)
226          *  {
227          *      cod[pos[j]] = -8192;
228          *  }
229          */
230
231         cod[pos[j]] = i * 16383 - 8192;
232
233         sign >>= 1;
234     }
235
236     return;
237 }