Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_wb / dec / src / dec_alg_codebook.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.173
22     ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
23     Available from http://www.3gpp.org
24
25 (C) 2007, 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_alg_codebook.cpp
35
36 ------------------------------------------------------------------------------
37  INPUT AND OUTPUT DEFINITIONS
38
39
40 ------------------------------------------------------------------------------
41  FUNCTION DESCRIPTION
42
43         decoding of algebraic codebook
44
45 ------------------------------------------------------------------------------
46  REQUIREMENTS
47
48
49 ------------------------------------------------------------------------------
50  REFERENCES
51
52 ------------------------------------------------------------------------------
53  PSEUDO-CODE
54
55 ------------------------------------------------------------------------------
56 */
57
58
59 /*----------------------------------------------------------------------------
60 ; INCLUDES
61 ----------------------------------------------------------------------------*/
62
63 #include "pv_amr_wb_type_defs.h"
64 #include "pvamrwbdecoder_basic_op.h"
65 #include "q_pulse.h"
66
67 /*----------------------------------------------------------------------------
68 ; MACROS
69 ; Define module specific macros here
70 ----------------------------------------------------------------------------*/
71
72
73 /*----------------------------------------------------------------------------
74 ; DEFINES
75 ; Include all pre-processor statements here. Include conditional
76 ; compile variables also.
77 ----------------------------------------------------------------------------*/
78
79 #define NB_POS 16                          /* pos in track, mask for sign bit */
80
81 /*----------------------------------------------------------------------------
82 ; DEFINES
83 ; Include all pre-processor statements here. Include conditional
84 ; compile variables also.
85 ----------------------------------------------------------------------------*/
86
87 /*----------------------------------------------------------------------------
88 ; LOCAL FUNCTION DEFINITIONS
89 ; Function Prototype declaration
90 ----------------------------------------------------------------------------*/
91
92 /*----------------------------------------------------------------------------
93 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
94 ; Variable declaration - defined here and used outside this module
95 ----------------------------------------------------------------------------*/
96
97 /*----------------------------------------------------------------------------
98 ; EXTERNAL FUNCTION REFERENCES
99 ; Declare functions defined elsewhere and referenced in this module
100 ----------------------------------------------------------------------------*/
101
102 /*----------------------------------------------------------------------------
103 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
104 ; Declare variables used in this module but defined elsewhere
105 ----------------------------------------------------------------------------*/
106
107 /*----------------------------------------------------------------------------
108 ; FUNCTION CODE
109 ----------------------------------------------------------------------------*/
110 void dec_1p_N1(int32 index, int16 N, int16 offset, int16 pos[])
111 {
112     int16 pos1;
113     int32 mask, i;
114
115     mask = ((1 << N) - 1);
116     /*-------------------------------------------------------*
117      * Decode 1 pulse with N+1 bits:                         *
118      *-------------------------------------------------------*/
119     pos1 = ((index & mask) + offset);
120
121     i = ((index >> N) & 1L);            /* i = ((index >> N) & 1); */
122
123     if (i == 1)
124     {
125         pos1 += NB_POS;
126     }
127     pos[0] = pos1;
128
129 }
130
131
132
133 /*----------------------------------------------------------------------------
134 ; FUNCTION CODE
135 ----------------------------------------------------------------------------*/
136
137 void dec_2p_2N1(int32 index, int16 N, int16 offset, int16 pos[])
138 {
139     int16 pos1, pos2, tmp;
140     int32 mask, i;
141
142     mask = (int32)(sub_int16(shl_int16(1, N), 1)); /* mask = ((1<<N)-1); */
143     /*-------------------------------------------------------*
144      * Decode 2 pulses with 2*N+1 bits:                      *
145      *-------------------------------------------------------*/
146     /* pos1 = (((index >> N) & mask) + offset); */
147     pos1 = (int16)(add_int32((shr_int32(index, N) & mask), (int32)(offset)));
148     tmp = shl_int16(N, 1);
149     i = (index >> tmp) & 1L;         /* i = (index >> (2*N)) & 1; */
150     pos2 = add_int16((int16)(index & mask), offset); /* pos2 = ((index & mask) + offset); */
151
152     if (pos2 < pos1)              /* ((pos2 - pos1) < 0) */
153     {
154         if (i == 1)
155         {                                  /* (i == 1) */
156             pos1 += NB_POS;      /* pos1 += NB_POS; */
157         }
158         else
159         {
160             pos2 += NB_POS;      /* pos2 += NB_POS;  */
161         }
162     }
163     else
164     {
165         if (i == 1)
166         {                                  /* (i == 1) */
167             pos1 += NB_POS;      /* pos1 += NB_POS; */
168             pos2 += NB_POS;      /* pos2 += NB_POS; */
169         }
170     }
171
172     pos[0] = pos1;
173     pos[1] = pos2;
174
175     return;
176 }
177
178
179
180 /*----------------------------------------------------------------------------
181 ; FUNCTION CODE
182 ----------------------------------------------------------------------------*/
183
184 void dec_3p_3N1(int32 index, int16 N, int16 offset, int16 pos[])
185 {
186     int16 j, tmp;
187     int32 mask, idx;
188
189     /*-------------------------------------------------------*
190      * Decode 3 pulses with 3*N+1 bits:                      *
191      *-------------------------------------------------------*/
192     tmp = sub_int16(shl_int16(N, 1), 1);               /* mask = ((1<<((2*N)-1))-1); */
193
194     mask = ((1 << ((2 * N) - 1)) - 1);
195
196     idx = index & mask;
197     j = offset;
198     tmp = (N << 1) - 1;
199
200
201     if (((index >> tmp) & 1L) != 0L)
202     {                                      /* if (((index >> ((2*N)-1)) & 1) == 1){ */
203         j += (1 << (N - 1)); /* j += (1<<(N-1)); */
204     }
205     dec_2p_2N1(idx, (int16)(N - 1), j, pos);
206
207     mask = ((1 << (N + 1)) - 1);
208     tmp = N << 1;                     /* idx = (index >> (2*N)) & mask; */
209     idx = (index >> tmp) & mask;
210
211     dec_1p_N1(idx, N, offset, pos + 2);
212
213     return;
214 }
215
216
217 /*----------------------------------------------------------------------------
218 ; FUNCTION CODE
219 ----------------------------------------------------------------------------*/
220
221 void dec_4p_4N1(int32 index, int16 N, int16 offset, int16 pos[])
222 {
223     int16 j, tmp;
224     int32 mask, idx;
225
226     /*-------------------------------------------------------*
227      * Decode 4 pulses with 4*N+1 bits:                      *
228      *-------------------------------------------------------*/
229     tmp = (N << 1) - 1;
230     mask = (1L << tmp) - 1L;
231     idx = index & mask;
232     j = offset;
233     tmp = (N << 1) - 1;
234
235
236     if (((index >> tmp) & 1L) != 0L)
237     {                                      /* (((index >> ((2*N)-1)) & 1) == 1) */
238         j += (1 << (N - 1)); /* j += (1<<(N-1)); */
239     }
240     dec_2p_2N1(idx, (int16)(N - 1), j, pos);
241
242
243     tmp = (N << 1) + 1;             /* mask = ((1<<((2*N)+1))-1); */
244     mask = (1L << tmp) - 1L;
245     idx = (index >> (N << 1)) & mask;   /* idx = (index >> (2*N)) & mask; */
246     dec_2p_2N1(idx, N, offset, pos + 2);      /* dec_2p_2N1(idx, N, offset, pos+2); */
247
248     return;
249 }
250
251
252
253 /*----------------------------------------------------------------------------
254 ; FUNCTION CODE
255 ----------------------------------------------------------------------------*/
256
257 void dec_4p_4N(int32 index, int16 N, int16 offset, int16 pos[])
258 {
259     int16 j, n_1, tmp;
260
261     /*-------------------------------------------------------*
262      * Decode 4 pulses with 4*N bits:                        *
263      *-------------------------------------------------------*/
264
265     n_1 = N - 1;
266     j = offset + (1 << n_1);          /* j = offset + (1 << n_1); */
267
268     tmp = (N << 2) - 2;
269
270     switch ((index >> tmp) & 3)
271     {                                      /* ((index >> ((4*N)-2)) & 3) */
272         case 0:
273             tmp = (n_1 << 2) + 1;
274
275             if ((index >> tmp) & 1)
276             {                                  /* (((index >> ((4*n_1)+1)) & 1) == 0) */
277                 dec_4p_4N1(index, n_1, j, pos);
278             }
279             else
280             {
281                 dec_4p_4N1(index, n_1, offset, pos);
282             }
283             break;
284         case 1:
285             tmp = (3 * n_1) + 1; /* dec_1p_N1((index>>((3*n_1)+1)), n_1, offset, pos) */
286             dec_1p_N1(index >> tmp, n_1, offset, pos);
287             dec_3p_3N1(index, n_1, j, pos + 1);
288             break;
289         case 2:
290             tmp = (n_1 << 1) + 1;       /* dec_2p_2N1((index>>((2*n_1)+1)), n_1, offset, pos); */
291             dec_2p_2N1(index >> tmp, n_1, offset, pos);
292             dec_2p_2N1(index, n_1, j, pos + 2);
293             break;
294         case 3:
295             tmp = n_1 + 1;                 /* dec_3p_3N1((index>>(n_1+1)), n_1, offset, pos); */
296             dec_3p_3N1(index >> tmp, n_1, offset, pos);
297             dec_1p_N1(index, n_1, j, pos + 3);
298             break;
299     }
300     return;
301 }
302
303
304 /*----------------------------------------------------------------------------
305 ; FUNCTION CODE
306 ----------------------------------------------------------------------------*/
307
308 void dec_5p_5N(int32 index, int16 N, int16 offset, int16 pos[])
309 {
310     int16 j, n_1, tmp;
311     int32 idx;
312
313     /*-------------------------------------------------------*
314      * Decode 5 pulses with 5*N bits:                        *
315      *-------------------------------------------------------*/
316
317     n_1 = (int16)(N - 1);
318     j = add_int16(offset, shl_int16(1, n_1));          /* j = offset + (1 << n_1); */
319     tmp = (N << 1) + 1;             /* idx = (index >> ((2*N)+1)); */
320     idx = index >> tmp;
321     tmp = (5 * N) - 1;    /* ((5*N)-1)) */
322
323
324     if ((index >> tmp) & 1)    /* ((index >> ((5*N)-1)) & 1)  */
325     {
326         dec_3p_3N1(idx, n_1, j, pos);
327         dec_2p_2N1(index, N, offset, pos + 3);
328     }
329     else
330     {
331         dec_3p_3N1(idx, n_1, offset, pos);
332         dec_2p_2N1(index, N, offset, pos + 3);
333     }
334     return;
335 }
336
337
338 /*----------------------------------------------------------------------------
339 ; FUNCTION CODE
340 ----------------------------------------------------------------------------*/
341
342 void dec_6p_6N_2(int32 index, int16 N, int16 offset, int16 pos[])
343 {
344     int16 j, n_1, offsetA, offsetB;
345
346     n_1 = N - 1;
347     j = offset + (1 << n_1);       /* j = offset + (1 << n_1); */
348
349
350     /* !!  N and n_1 are constants -> it doesn't need to be operated by Basic Operators */
351
352     offsetA = offsetB = j;
353
354     if (((index >> (6*N - 5)) & 1L) == 0)
355     {                                      /* if (((index >> ((6*N)-5)) & 1) == 0) */
356         offsetA = offset;
357     }
358     else
359     {
360         offsetB = offset;
361     }
362
363
364     switch ((index >> (6*N - 4)) & 3)
365     {                                      /* (index >> ((6*N)-4)) & 3 */
366         case 0:
367             dec_5p_5N(index >> N, n_1, offsetA, pos);  /* dec_5p_5N(index>>N, n_1, offsetA, pos); */
368             dec_1p_N1(index, n_1, offsetA, pos + 5);
369             break;
370         case 1:
371             dec_5p_5N(index >> N, n_1, offsetA, pos);  /* dec_5p_5N(index>>N, n_1, offsetA, pos); */
372             dec_1p_N1(index, n_1, offsetB, pos + 5);
373             break;
374         case 2:
375             dec_4p_4N(index >> (2*n_1 + 1), n_1, offsetA, pos); /* dec_4p_4N(index>>((2*n_1)+1 ), n_1, offsetA, pos); */
376             dec_2p_2N1(index, n_1, offsetB, pos + 4);
377             break;
378         case 3:
379             dec_3p_3N1(index >> (3*n_1 + 1), n_1, offset, pos); /* dec_3p_3N1(index>>((3*n_1)+ 1), n_1, offset, pos); */
380             dec_3p_3N1(index, n_1, j, pos + 3);
381             break;
382     }
383     return;
384 }