Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_wb / dec / src / homing_amr_wb_dec.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: homing_amr_wb_dec.cpp
35
36 ------------------------------------------------------------------------------
37
38
39
40 INPUT AND OUTPUT DEFINITIONS
41
42 Input
43     int16 input_frame[],            16-bit input frame
44     int16 mode                      16-bit mode
45     int16 nparms                    16-bit number of parameters
46 Returns
47     Int16 i             number of leading zeros on x
48
49
50 ------------------------------------------------------------------------------
51  FUNCTION DESCRIPTION
52
53     Performs the homing routines
54
55     int16 dhf_test(int16 input_frame[], int16 mode, int16 nparms)
56     int16 decoder_homing_frame_test(int16 input_frame[], int16 mode)
57     int16 decoder_homing_frame_test_first(int16 input_frame[], int16 mode)
58
59 ------------------------------------------------------------------------------
60  REQUIREMENTS
61
62
63 ------------------------------------------------------------------------------
64  REFERENCES
65
66 ------------------------------------------------------------------------------
67  PSEUDO-CODE
68
69 ------------------------------------------------------------------------------
70 */
71
72
73 /*----------------------------------------------------------------------------
74 ; INCLUDES
75 ----------------------------------------------------------------------------*/
76
77 #include "pv_amr_wb_type_defs.h"
78 #include "pvamrwbdecoder_cnst.h"
79 #include "pvamrwbdecoder.h"
80 #include "pvamrwbdecoder_basic_op.h"
81 #include "get_amr_wb_bits.h"
82 #include "pvamrwbdecoder_api.h"
83 #include "pvamrwbdecoder.h"
84
85 /*----------------------------------------------------------------------------
86 ; MACROS
87 ; Define module specific macros here
88 ----------------------------------------------------------------------------*/
89
90
91 /*----------------------------------------------------------------------------
92 ; DEFINES
93 ; Include all pre-processor statements here. Include conditional
94 ; compile variables also.
95 ----------------------------------------------------------------------------*/
96 #define DHF_PARMS_MAX 32 /* homing frame pattern             */
97 #define NUM_OF_SPMODES 9
98
99 #define PRML 15
100 #define PRMN_7k NBBITS_7k/PRML + 1
101 #define PRMN_9k NBBITS_9k/PRML + 1
102 #define PRMN_12k NBBITS_12k/PRML + 1
103 #define PRMN_14k NBBITS_14k/PRML + 1
104 #define PRMN_16k NBBITS_16k/PRML + 1
105 #define PRMN_18k NBBITS_18k/PRML + 1
106 #define PRMN_20k NBBITS_20k/PRML + 1
107 #define PRMN_23k NBBITS_23k/PRML + 1
108 #define PRMN_24k NBBITS_24k/PRML + 1
109
110 /*----------------------------------------------------------------------------
111 ; LOCAL FUNCTION DEFINITIONS
112 ; Function Prototype declaration
113 ----------------------------------------------------------------------------*/
114 #ifdef __cplusplus
115 extern "C"
116 {
117 #endif
118
119     int16 dhf_test(int16 input_frame[], int32 mode, int16 nparms);
120
121 #ifdef __cplusplus
122 }
123 #endif
124
125 /*----------------------------------------------------------------------------
126 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
127 ; Variable declaration - defined here and used outside this module
128 ----------------------------------------------------------------------------*/
129 static const int16 prmnofsf[NUM_OF_SPMODES] =
130 {
131     63,  81, 100,
132     108, 116, 128,
133     136, 152, 156
134 };
135
136
137 const int16 dfh_M7k[PRMN_7k] =
138 {
139     3168, 29954, 29213, 16121,
140     64, 13440, 30624, 16430,
141     19008
142 };
143
144 const int16 dfh_M9k[PRMN_9k] =
145 {
146     3168, 31665,  9943, 9123,
147     15599,  4358, 20248, 2048,
148     17040, 27787, 16816, 13888
149 };
150
151 const int16 dfh_M12k[PRMN_12k] =
152 {
153     3168, 31665,  9943,  9128,
154     3647,  8129, 30930, 27926,
155     18880, 12319,   496,  1042,
156     4061, 20446, 25629, 28069,
157     13948
158 };
159
160 const int16 dfh_M14k[PRMN_14k] =
161 {
162     3168, 31665,  9943,  9131,
163     24815,   655, 26616, 26764,
164     7238, 19136,  6144,    88,
165     4158, 25733, 30567, 30494,
166     221, 20321, 17823
167 };
168
169 const int16 dfh_M16k[PRMN_16k] =
170 {
171     3168, 31665,  9943,  9131,
172     24815,   700,  3824,  7271,
173     26400,  9528,  6594, 26112,
174     108,  2068, 12867, 16317,
175     23035, 24632,  7528,  1752,
176     6759, 24576
177 };
178
179 const int16 dfh_M18k[PRMN_18k] =
180 {
181     3168, 31665,  9943,  9135,
182     14787, 14423, 30477, 24927,
183     25345, 30154,   916,  5728,
184     18978,  2048,   528, 16449,
185     2436,  3581, 23527, 29479,
186     8237, 16810, 27091, 19052,
187     0
188 };
189
190 const int16 dfh_M20k[PRMN_20k] =
191 {
192     3168, 31665,  9943,  9129,
193     8637, 31807, 24646,   736,
194     28643,  2977,  2566, 25564,
195     12930, 13960,  2048,   834,
196     3270,  4100, 26920, 16237,
197     31227, 17667, 15059, 20589,
198     30249, 29123, 0
199 };
200
201 const int16 dfh_M23k[PRMN_23k] =
202 {
203     3168, 31665,  9943,  9132,
204     16748,  3202, 28179, 16317,
205     30590, 15857, 19960,  8818,
206     21711, 21538,  4260, 16690,
207     20224,  3666,  4194,  9497,
208     16320, 15388,  5755, 31551,
209     14080,  3574, 15932,    50,
210     23392, 26053, 31216
211 };
212
213 const int16 dfh_M24k[PRMN_24k] =
214 {
215     3168, 31665,  9943,  9134,
216     24776,  5857, 18475, 28535,
217     29662, 14321, 16725,  4396,
218     29353, 10003, 17068, 20504,
219     720,     0,  8465, 12581,
220     28863, 24774,  9709, 26043,
221     7941, 27649, 13965, 15236,
222     18026, 22047, 16681,  3968
223 };
224
225
226 /*----------------------------------------------------------------------------
227 ; EXTERNAL FUNCTION REFERENCES
228 ; Declare functions defined elsewhere and referenced in this module
229 ----------------------------------------------------------------------------*/
230
231 /*----------------------------------------------------------------------------
232 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
233 ; Declare variables used in this module but defined elsewhere
234 ----------------------------------------------------------------------------*/
235
236 /*----------------------------------------------------------------------------
237 ; FUNCTION CODE
238 ----------------------------------------------------------------------------*/
239
240 int16 dhf_test(int16 input_frame[], int32 mode, int16 nparms)
241 {
242     int16 i, j, tmp, shift;
243     int16 param[DHF_PARMS_MAX];
244     int16 *prms;
245
246     /* overall table with the parameters of the
247     decoder homing frames for all modes */
248
249     const int16 *dhf[] =
250     {
251         dfh_M7k,
252         dfh_M9k,
253         dfh_M12k,
254         dfh_M14k,
255         dfh_M16k,
256         dfh_M18k,
257         dfh_M20k,
258         dfh_M23k,
259         dfh_M24k,
260         dfh_M24k
261     };
262
263     prms = input_frame;
264     j = 0;
265     i = 0;
266
267     if (mode != MRDTX)
268     {
269         if (mode != MODE_24k)
270         {
271             /* convert the received serial bits */
272             tmp = nparms - 15;
273             while (tmp > j)
274             {
275                 param[i] = Serial_parm(15, &prms);
276                 j += 15;
277                 i++;
278             }
279             tmp = nparms - j;
280             param[i] = Serial_parm(tmp, &prms);
281             shift = 15 - tmp;
282             param[i] = shl_int16(param[i], shift);
283         }
284         else
285         {
286             /*If mode is 23.85Kbit/s, remove high band energy bits */
287             for (i = 0; i < 10; i++)
288             {
289                 param[i] = Serial_parm(15, &prms);
290             }
291             param[10] = Serial_parm(15, &prms) & 0x61FF;
292
293             for (i = 11; i < 17; i++)
294             {
295                 param[i] = Serial_parm(15, &prms);
296             }
297             param[17] = Serial_parm(15, &prms) & 0xE0FF;
298
299             for (i = 18; i < 24; i++)
300             {
301                 param[i] = Serial_parm(15, &prms);
302             }
303             param[24] = Serial_parm(15, &prms) & 0x7F0F;
304
305             for (i = 25; i < 31; i++)
306             {
307                 param[i] = Serial_parm(15, &prms);
308             }
309
310             tmp = Serial_parm(8, &prms);
311             param[31] = shl_int16(tmp, 7);
312             shift = 0;
313         }
314
315         /* check if the parameters matches the parameters of the corresponding decoder homing frame */
316         tmp = i;
317         j = 0;
318         for (i = 0; i < tmp; i++)
319         {
320             j = (param[i] ^ dhf[mode][i]);
321             if (j)
322             {
323                 break;
324             }
325         }
326         tmp = 0x7fff;
327         tmp >>= shift;
328         tmp = shl_int16(tmp, shift);
329         tmp = (dhf[mode][i] & tmp);
330         tmp = (param[i] ^ tmp);
331         j = (int16)(j | tmp);
332
333     }
334     else
335     {
336         j = 1;
337     }
338
339     return (!j);
340 }
341
342 /*----------------------------------------------------------------------------
343 ; FUNCTION CODE
344 ----------------------------------------------------------------------------*/
345
346
347 int16 pvDecoder_AmrWb_homing_frame_test(int16 input_frame[], int16 mode)
348 {
349     /* perform test for COMPLETE parameter frame */
350     return dhf_test(input_frame, mode, AMR_WB_COMPRESSED[mode]);
351 }
352
353 /*----------------------------------------------------------------------------
354 ; FUNCTION CODE
355 ----------------------------------------------------------------------------*/
356
357
358 int16 pvDecoder_AmrWb_homing_frame_test_first(int16 input_frame[], int16 mode)
359 {
360     /* perform test for FIRST SUBFRAME of parameter frame ONLY */
361     return dhf_test(input_frame, mode, prmnofsf[mode]);
362 }