Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_wb / dec / src / dec_acelp_4p_in_64.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_acelp_4p_in_64.cpp
35
36 ------------------------------------------------------------------------------
37  INPUT AND OUTPUT DEFINITIONS
38
39      int16 index[],    (i) : index (20): 5+5+5+5 = 20 bits.
40                        (i) : index (36): 9+9+9+9 = 36 bits.
41                        (i) : index (44): 13+9+13+9 = 44 bits.
42                        (i) : index (52): 13+13+13+13 = 52 bits.
43                        (i) : index (64): 2+2+2+2+14+14+14+14 = 64 bits.
44                        (i) : index (72): 10+2+10+2+10+14+10+14 = 72 bits.
45                        (i) : index (88): 11+11+11+11+11+11+11+11 = 88 bits.
46      int16 nbbits,     (i) : 20, 36, 44, 52, 64, 72 or 88 bits
47      int16 code[]      (o) Q9: algebraic (fixed) codebook excitation
48
49 ------------------------------------------------------------------------------
50  FUNCTION DESCRIPTION
51
52    20, 36, 44, 52, 64, 72, 88 bits algebraic codebook decoder.
53    4 tracks x 16 positions per track = 64 samples.
54
55    20 bits --> 4 pulses in a frame of 64 samples.
56    36 bits --> 8 pulses in a frame of 64 samples.
57    44 bits --> 10 pulses in a frame of 64 samples.
58    52 bits --> 12 pulses in a frame of 64 samples.
59    64 bits --> 16 pulses in a frame of 64 samples.
60    72 bits --> 18 pulses in a frame of 64 samples.
61    88 bits --> 24 pulses in a frame of 64 samples.
62
63    All pulses can have two (2) possible amplitudes: +1 or -1.
64    Each pulse can have sixteen (16) possible positions.
65
66 ------------------------------------------------------------------------------
67  REQUIREMENTS
68
69
70 ------------------------------------------------------------------------------
71  REFERENCES
72
73 ------------------------------------------------------------------------------
74  PSEUDO-CODE
75
76 ------------------------------------------------------------------------------
77 */
78
79
80 /*----------------------------------------------------------------------------
81 ; INCLUDES
82 ----------------------------------------------------------------------------*/
83
84 #include "pv_amr_wb_type_defs.h"
85 #include "pvamrwbdecoder_basic_op.h"
86 #include "pvamrwbdecoder_cnst.h"
87 #include "pvamrwbdecoder_acelp.h"
88
89 #include "q_pulse.h"
90
91 /*----------------------------------------------------------------------------
92 ; MACROS
93 ; Define module specific macros here
94 ----------------------------------------------------------------------------*/
95
96
97 /*----------------------------------------------------------------------------
98 ; DEFINES
99 ; Include all pre-processor statements here. Include conditional
100 ; compile variables also.
101 ----------------------------------------------------------------------------*/
102 #define L_CODE    64                       /* codevector length  */
103 #define NB_TRACK  4                        /* number of track    */
104 #define NB_POS    16                       /* number of position */
105
106 /*----------------------------------------------------------------------------
107 ; LOCAL FUNCTION DEFINITIONS
108 ; Function Prototype declaration
109 ----------------------------------------------------------------------------*/
110 #ifdef __cplusplus
111 extern "C"
112 {
113 #endif
114
115     void add_pulses(int16 pos[], int16 nb_pulse, int16 track, int16 code[]);
116
117 #ifdef __cplusplus
118 }
119 #endif
120
121 /*----------------------------------------------------------------------------
122 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
123 ; Variable declaration - defined here and used outside this module
124 ----------------------------------------------------------------------------*/
125
126 /*----------------------------------------------------------------------------
127 ; EXTERNAL FUNCTION REFERENCES
128 ; Declare functions defined elsewhere and referenced in this module
129 ----------------------------------------------------------------------------*/
130
131 /*----------------------------------------------------------------------------
132 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
133 ; Declare variables used in this module but defined elsewhere
134 ----------------------------------------------------------------------------*/
135
136 /*----------------------------------------------------------------------------
137 ; FUNCTION CODE
138 ----------------------------------------------------------------------------*/
139
140 void dec_acelp_4p_in_64(
141     int16 index[],  /* (i) : index (20): 5+5+5+5 = 20 bits.                 */
142     /* (i) : index (36): 9+9+9+9 = 36 bits.                 */
143     /* (i) : index (44): 13+9+13+9 = 44 bits.               */
144     /* (i) : index (52): 13+13+13+13 = 52 bits.             */
145     /* (i) : index (64): 2+2+2+2+14+14+14+14 = 64 bits.     */
146     /* (i) : index (72): 10+2+10+2+10+14+10+14 = 72 bits.   */
147     /* (i) : index (88): 11+11+11+11+11+11+11+11 = 88 bits. */
148     int16 nbbits,   /* (i) : 20, 36, 44, 52, 64, 72 or 88 bits              */
149     int16 code[]    /* (o) Q9: algebraic (fixed) codebook excitation        */
150 )
151 {
152     int16 k, pos[6];
153     int32 L_index;
154     pv_memset(code, 0, L_CODE*sizeof(*code));
155
156     /* decode the positions and signs of pulses and build the codeword */
157
158
159     switch (nbbits)
160     {
161         case 20:
162             for (k = 0; k < NB_TRACK; k++)
163             {
164                 L_index = index[k];
165                 dec_1p_N1(L_index, 4, 0, pos);
166                 add_pulses(pos, 1, k, code);
167             }
168             break;
169
170         case  36:
171             for (k = 0; k < NB_TRACK; k++)
172             {
173                 L_index = index[k];
174                 dec_2p_2N1(L_index, 4, 0, pos);
175                 add_pulses(pos, 2, k, code);
176             }
177             break;
178         case 44:
179             for (k = 0; k < NB_TRACK - 2; k++)
180             {
181                 L_index = index[k];
182                 dec_3p_3N1(L_index, 4, 0, pos);
183                 add_pulses(pos, 3, k, code);
184             }
185             for (k = 2; k < NB_TRACK; k++)
186             {
187                 L_index = index[k];
188                 dec_2p_2N1(L_index, 4, 0, pos);
189                 add_pulses(pos, 2, k, code);
190             }
191             break;
192         case 52:
193             for (k = 0; k < NB_TRACK; k++)
194             {
195                 L_index = index[k];
196                 dec_3p_3N1(L_index, 4, 0, pos);
197                 add_pulses(pos, 3, k, code);
198             }
199             break;
200         case 64:
201             for (k = 0; k < NB_TRACK; k++)
202             {
203                 L_index = ((int32)index[k] << 14) + index[k + NB_TRACK];
204                 dec_4p_4N(L_index, 4, 0, pos);
205                 add_pulses(pos, 4, k, code);
206             }
207             break;
208         case 72:
209             for (k = 0; k < NB_TRACK - 2; k++)
210             {
211                 L_index = ((int32)index[k] << 10) + index[k + NB_TRACK];
212                 dec_5p_5N(L_index, 4, 0, pos);
213                 add_pulses(pos, 5, k, code);
214             }
215             for (k = 2; k < NB_TRACK; k++)
216             {
217                 L_index = ((int32)index[k] << 14) + index[k + NB_TRACK];
218                 dec_4p_4N(L_index, 4, 0, pos);
219                 add_pulses(pos, 4, k, code);
220             }
221             break;
222         case 88:
223             for (k = 0; k < NB_TRACK; k++)
224             {
225                 L_index = ((int32)index[k] << 11) + index[k + NB_TRACK];
226                 dec_6p_6N_2(L_index, 4, 0, pos);
227                 add_pulses(pos, 6, k, code);
228             }
229         default:
230             break;
231     }
232
233
234 }
235
236
237
238 void add_pulses(int16 pos[], int16 nb_pulse, int16 track, int16 code[])
239 {
240     int16 i, k;
241
242     for (k = 0; k < nb_pulse; k++)
243     {
244         /* i = ((pos[k] & (NB_POS-1))*NB_TRACK) + track; */
245         i = ((pos[k] & (NB_POS - 1)) << 2) + track;
246
247         if ((pos[k] & NB_POS) == 0)
248         {
249             code[i] +=  512;
250         }
251         else
252         {
253             code[i] -=  512;
254         }
255     }
256
257 }