Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / src / log2_norm.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.073
22     ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23     Available from http://www.3gpp.org
24
25 (C) 2004, 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  Filename: log2_norm.cpp
32
33 ------------------------------------------------------------------------------
34 */
35
36 /*----------------------------------------------------------------------------
37 ; INCLUDES
38 ----------------------------------------------------------------------------*/
39 #include    "log2_norm.h"
40
41 /*----------------------------------------------------------------------------
42 ; MACROS
43 ; Define module specific macros here
44 ----------------------------------------------------------------------------*/
45
46
47 /*----------------------------------------------------------------------------
48 ; DEFINES
49 ; Include all pre-processor statements here. Include conditional
50 ; compile variables also.
51 ----------------------------------------------------------------------------*/
52
53 /*----------------------------------------------------------------------------
54 ; LOCAL FUNCTION DEFINITIONS
55 ; Function Prototype declaration
56 ----------------------------------------------------------------------------*/
57
58 /*----------------------------------------------------------------------------
59 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
60 ; Variable declaration - defined here and used outside this module
61 ----------------------------------------------------------------------------*/
62
63
64 /*
65 ------------------------------------------------------------------------------
66  FUNCTION NAME: Log2_norm
67 ------------------------------------------------------------------------------
68  INPUT AND OUTPUT DEFINITIONS
69
70  Inputs:
71     L_x = normalized input value of type Word32
72     exp = number of shifts required to normalize L_x; it is of type Word16
73     exponent = pointer to the integer part of Log2 (of type Word16)
74            whose valid range is: 0 <= value <= 30
75     fraction = pointer to the fractional part of Log2 (of type Word16)
76            whose valid range is: 0 <= value < 1
77
78  Outputs:
79     exponent points to the newly calculated integer part of Log2
80     fraction points to the newly calculated fractional part of Log2
81
82  Returns:
83     None
84
85  Global Variables Used:
86     None
87
88  Local Variables Needed:
89     table = Log2 table of constants of type Word16
90
91 ------------------------------------------------------------------------------
92  FUNCTION DESCRIPTION
93
94  The function Log2(L_x) calculates the logarithm of the normalized input
95  buffer L_x. The logarithm is approximated by a table and linear
96  interpolation. The following steps are used to compute Log2(L_x):
97
98  1. exponent = 30 - norm_exponent
99  2. i = bit25-b31 of L_x;  32<=i<=63  (because of normalization).
100  3. a = bit10-b24
101  4. i = i - 32
102  5. fraction = table[i]<<16 - (table[i] - table[i+1]) * a * 2
103
104 ------------------------------------------------------------------------------
105  REQUIREMENTS
106
107  None
108
109 ------------------------------------------------------------------------------
110  REFERENCES
111
112  log2.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
113
114 ------------------------------------------------------------------------------
115  PSEUDO-CODE
116
117 void Log2_norm (
118     Word32 L_x,         // (i) : input value (normalized)
119     Word16 exp,         // (i) : norm_l (L_x)
120     Word16 *exponent,   // (o) : Integer part of Log2.   (range: 0<=val<=30)
121     Word16 *fraction    // (o) : Fractional part of Log2. (range: 0<=val<1)
122 )
123 {
124     Word16 i, a, tmp;
125     Word32 L_y;
126
127     if (L_x <= (Word32) 0)
128     {
129         *exponent = 0;
130         *fraction = 0;
131         return;
132     }
133
134     *exponent = sub (30, exp);
135
136     L_x = L_shr (L_x, 9);
137     i = extract_h (L_x);                // Extract b25-b31
138     L_x = L_shr (L_x, 1);
139     a = extract_l (L_x);                // Extract b10-b24 of fraction
140     a = a & (Word16) 0x7fff;
141
142     i = sub (i, 32);
143
144     L_y = L_deposit_h (table[i]);       // table[i] << 16
145     tmp = sub (table[i], table[i + 1]); // table[i] - table[i+1]
146     L_y = L_msu (L_y, tmp, a);          // L_y -= tmp*a*2
147
148     *fraction = extract_h (L_y);
149
150     return;
151 }
152
153 ------------------------------------------------------------------------------
154  CAUTION [optional]
155  [State any special notes, constraints or cautions for users of this function]
156
157 ------------------------------------------------------------------------------
158 */
159
160 void Log2_norm(
161     Word32 L_x,         /* (i) : input value (normalized)                   */
162     Word16 exp,         /* (i) : norm_l (L_x)                               */
163     Word16 *exponent,   /* (o) : Integer part of Log2.   (range: 0<=val<=30)*/
164     Word16 *fraction    /* (o) : Fractional part of Log2. (range: 0<=val<1) */
165 )
166 {
167     Word16 i, a, tmp;
168     Word32 L_y;
169
170     if (L_x <= (Word32) 0)
171     {
172         *exponent = 0;
173         *fraction = 0;
174     }
175     else
176     {
177         /* Calculate exponent portion of Log2 */
178         *exponent = 30 - exp;
179
180         /* At this point, L_x > 0       */
181         /* Shift L_x to the right by 10 to extract bits 10-31,  */
182         /* which is needed to calculate fractional part of Log2 */
183         L_x >>= 10;
184         i = (Word16)(L_x >> 15);    /* Extract b25-b31 */
185         a = L_x & 0x7fff;           /* Extract b10-b24 of fraction */
186
187         /* Calculate table index -> subtract by 32 is done for           */
188         /* proper table indexing, since 32<=i<=63 (due to normalization) */
189         i -= 32;
190
191         /* Fraction part of Log2 is approximated by using table[]    */
192         /* and linear interpolation, i.e.,                           */
193         /* fraction = table[i]<<16 - (table[i] - table[i+1]) * a * 2 */
194         L_y = (Word32) log2_tbl[i] << 16;  /* table[i] << 16        */
195         tmp = log2_tbl[i] - log2_tbl[i + 1];  /* table[i] - table[i+1] */
196         L_y -= (((Word32) tmp) * a) << 1; /* L_y -= tmp*a*2        */
197
198         *fraction = (Word16)(L_y >> 16);
199     }
200
201     return;
202 }