1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
30 ------------------------------------------------------------------------------
34 Filename: dec_alg_codebook.cpp
36 ------------------------------------------------------------------------------
37 INPUT AND OUTPUT DEFINITIONS
40 ------------------------------------------------------------------------------
43 decoding of algebraic codebook
45 ------------------------------------------------------------------------------
49 ------------------------------------------------------------------------------
52 ------------------------------------------------------------------------------
55 ------------------------------------------------------------------------------
59 /*----------------------------------------------------------------------------
61 ----------------------------------------------------------------------------*/
63 #include "pv_amr_wb_type_defs.h"
64 #include "pvamrwbdecoder_basic_op.h"
67 /*----------------------------------------------------------------------------
69 ; Define module specific macros here
70 ----------------------------------------------------------------------------*/
73 /*----------------------------------------------------------------------------
75 ; Include all pre-processor statements here. Include conditional
76 ; compile variables also.
77 ----------------------------------------------------------------------------*/
79 #define NB_POS 16 /* pos in track, mask for sign bit */
81 /*----------------------------------------------------------------------------
83 ; Include all pre-processor statements here. Include conditional
84 ; compile variables also.
85 ----------------------------------------------------------------------------*/
87 /*----------------------------------------------------------------------------
88 ; LOCAL FUNCTION DEFINITIONS
89 ; Function Prototype declaration
90 ----------------------------------------------------------------------------*/
92 /*----------------------------------------------------------------------------
93 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
94 ; Variable declaration - defined here and used outside this module
95 ----------------------------------------------------------------------------*/
97 /*----------------------------------------------------------------------------
98 ; EXTERNAL FUNCTION REFERENCES
99 ; Declare functions defined elsewhere and referenced in this module
100 ----------------------------------------------------------------------------*/
102 /*----------------------------------------------------------------------------
103 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
104 ; Declare variables used in this module but defined elsewhere
105 ----------------------------------------------------------------------------*/
107 /*----------------------------------------------------------------------------
109 ----------------------------------------------------------------------------*/
110 void dec_1p_N1(int32 index, int16 N, int16 offset, int16 pos[])
115 mask = ((1 << N) - 1);
116 /*-------------------------------------------------------*
117 * Decode 1 pulse with N+1 bits: *
118 *-------------------------------------------------------*/
119 pos1 = ((index & mask) + offset);
121 i = ((index >> N) & 1L); /* i = ((index >> N) & 1); */
133 /*----------------------------------------------------------------------------
135 ----------------------------------------------------------------------------*/
137 void dec_2p_2N1(int32 index, int16 N, int16 offset, int16 pos[])
139 int16 pos1, pos2, tmp;
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); */
152 if (pos2 < pos1) /* ((pos2 - pos1) < 0) */
156 pos1 += NB_POS; /* pos1 += NB_POS; */
160 pos2 += NB_POS; /* pos2 += NB_POS; */
167 pos1 += NB_POS; /* pos1 += NB_POS; */
168 pos2 += NB_POS; /* pos2 += NB_POS; */
180 /*----------------------------------------------------------------------------
182 ----------------------------------------------------------------------------*/
184 void dec_3p_3N1(int32 index, int16 N, int16 offset, int16 pos[])
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); */
194 mask = ((1 << ((2 * N) - 1)) - 1);
201 if (((index >> tmp) & 1L) != 0L)
202 { /* if (((index >> ((2*N)-1)) & 1) == 1){ */
203 j += (1 << (N - 1)); /* j += (1<<(N-1)); */
205 dec_2p_2N1(idx, (int16)(N - 1), j, pos);
207 mask = ((1 << (N + 1)) - 1);
208 tmp = N << 1; /* idx = (index >> (2*N)) & mask; */
209 idx = (index >> tmp) & mask;
211 dec_1p_N1(idx, N, offset, pos + 2);
217 /*----------------------------------------------------------------------------
219 ----------------------------------------------------------------------------*/
221 void dec_4p_4N1(int32 index, int16 N, int16 offset, int16 pos[])
226 /*-------------------------------------------------------*
227 * Decode 4 pulses with 4*N+1 bits: *
228 *-------------------------------------------------------*/
230 mask = (1L << tmp) - 1L;
236 if (((index >> tmp) & 1L) != 0L)
237 { /* (((index >> ((2*N)-1)) & 1) == 1) */
238 j += (1 << (N - 1)); /* j += (1<<(N-1)); */
240 dec_2p_2N1(idx, (int16)(N - 1), j, pos);
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); */
253 /*----------------------------------------------------------------------------
255 ----------------------------------------------------------------------------*/
257 void dec_4p_4N(int32 index, int16 N, int16 offset, int16 pos[])
261 /*-------------------------------------------------------*
262 * Decode 4 pulses with 4*N bits: *
263 *-------------------------------------------------------*/
266 j = offset + (1 << n_1); /* j = offset + (1 << n_1); */
270 switch ((index >> tmp) & 3)
271 { /* ((index >> ((4*N)-2)) & 3) */
273 tmp = (n_1 << 2) + 1;
275 if ((index >> tmp) & 1)
276 { /* (((index >> ((4*n_1)+1)) & 1) == 0) */
277 dec_4p_4N1(index, n_1, j, pos);
281 dec_4p_4N1(index, n_1, offset, pos);
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);
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);
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);
304 /*----------------------------------------------------------------------------
306 ----------------------------------------------------------------------------*/
308 void dec_5p_5N(int32 index, int16 N, int16 offset, int16 pos[])
313 /*-------------------------------------------------------*
314 * Decode 5 pulses with 5*N bits: *
315 *-------------------------------------------------------*/
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)); */
321 tmp = (5 * N) - 1; /* ((5*N)-1)) */
324 if ((index >> tmp) & 1) /* ((index >> ((5*N)-1)) & 1) */
326 dec_3p_3N1(idx, n_1, j, pos);
327 dec_2p_2N1(index, N, offset, pos + 3);
331 dec_3p_3N1(idx, n_1, offset, pos);
332 dec_2p_2N1(index, N, offset, pos + 3);
338 /*----------------------------------------------------------------------------
340 ----------------------------------------------------------------------------*/
342 void dec_6p_6N_2(int32 index, int16 N, int16 offset, int16 pos[])
344 int16 j, n_1, offsetA, offsetB;
347 j = offset + (1 << n_1); /* j = offset + (1 << n_1); */
350 /* !! N and n_1 are constants -> it doesn't need to be operated by Basic Operators */
352 offsetA = offsetB = j;
354 if (((index >> (6*N - 5)) & 1L) == 0)
355 { /* if (((index >> ((6*N)-5)) & 1) == 0) */
364 switch ((index >> (6*N - 4)) & 3)
365 { /* (index >> ((6*N)-4)) & 3 */
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);
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);
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);
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);