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