2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
5 © Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and the date of any change. For modified versions of the FDK AAC Codec, the term
50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
73 5. CONTACT INFORMATION
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
78 91058 Erlangen, Germany
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
84 /*************************** Fraunhofer IIS FDK Tools **********************
86 Author(s): Haricharan Lakshman, Manuel Jander
87 Description: Trigonometric functions fixed point fractional implementation.
89 ******************************************************************************/
91 #include "FDK_trigFcts.h"
93 #include "fixpoint_math.h"
98 #define IMPROVE_ATAN2_ACCURACY 1 // 0 --> 59 dB SNR 1 --> 65 dB SNR
102 #if IMPROVE_ATAN2_ACCURACY
103 static const FIXP_DBL f_atan_expand_range
[MAXSFTAB
-(MINSFTAB
-1)] =
105 /*****************************************************************************
107 * Table holds fixp_atan() output values which are outside of input range
108 * of fixp_atan() to improve SNR of fixp_atan2().
110 * This Table might also be used in fixp_atan() [todo] so there a wider input
111 * range can be covered, too.
113 * Matlab (generate table):
114 * for scl = 7:25 % MINSFTAB .. MAXSFTAB
115 * at=atan(0.5 *(2^scl)); % 0.5 because get in 'middle' area of current scale level 'scl'
116 * at/2 % div at by ATO_SCALE
119 * Table divided by 2=ATO_SCALE <-- SF=ATO_SF
120 *****************************************************************************/
121 FL2FXCONST_DBL(7.775862990872099e-001), FL2FXCONST_DBL(7.814919928673978e-001), FL2FXCONST_DBL(7.834450483314648e-001),
122 FL2FXCONST_DBL(7.844216021392089e-001), FL2FXCONST_DBL(7.849098823026687e-001), FL2FXCONST_DBL(7.851540227918509e-001),
123 FL2FXCONST_DBL(7.852760930873737e-001), FL2FXCONST_DBL(7.853371282415015e-001), FL2FXCONST_DBL(7.853676458193612e-001),
124 FL2FXCONST_DBL(7.853829046083906e-001), FL2FXCONST_DBL(7.853905340029177e-001), FL2FXCONST_DBL(7.853943487001828e-001),
125 FL2FXCONST_DBL(7.853962560488155e-001), FL2FXCONST_DBL(7.853972097231319e-001), FL2FXCONST_DBL(7.853976865602901e-001),
126 FL2FXCONST_DBL(7.853979249788692e-001), FL2FXCONST_DBL(7.853980441881587e-001), FL2FXCONST_DBL(7.853981037928035e-001),
127 FL2FXCONST_DBL(7.853981335951259e-001)
128 // pi/4 = 0.785398163397448 = pi/2/ATO_SCALE
132 FIXP_DBL
fixp_atan2(FIXP_DBL y
, FIXP_DBL x
)
135 FIXP_DBL at
; // atan out
136 FIXP_DBL at2
; // atan2 out
137 FIXP_DBL ret
= FL2FXCONST_DBL(-1.0f
);
142 if (y
> FL2FXCONST_DBL(0.0f
))
144 if (x
> FL2FXCONST_DBL(0.0f
)) {
145 q
= fDivNormHighPrec( y
, x
, &sf
); // both pos.
147 else if (x
< FL2FXCONST_DBL(0.0f
)) {
148 q
= -fDivNormHighPrec( y
,-x
, &sf
); // x neg.
150 else {//(x ==FL2FXCONST_DBL(0.0f))
151 q
= FL2FXCONST_DBL(+1.0f
); // y/x = pos/zero = +Inf
155 else if (y
< FL2FXCONST_DBL(0.0f
))
157 if (x
> FL2FXCONST_DBL(0.0f
)) {
158 q
= -fDivNormHighPrec(-y
, x
, &sf
); // y neg.
160 else if (x
< FL2FXCONST_DBL(0.0f
)) {
161 q
= fDivNormHighPrec(-y
,-x
, &sf
); // both neg.
163 else {//(x ==FL2FXCONST_DBL(0.0f))
164 q
= FL2FXCONST_DBL(-1.0f
); // y/x = neg/zero = -Inf
168 else { // (y ==FL2FXCONST_DBL(0.0f))
169 q
= FL2FXCONST_DBL(0.0f
);
176 if ( sfo
> ATI_SF
) {
177 // --- could not calc fixp_atan() here bec of input data out of range
178 // ==> therefore give back boundary values
180 #if IMPROVE_ATAN2_ACCURACY
181 if (sfo
> MAXSFTAB
) sfo
= MAXSFTAB
;
184 if ( q
> FL2FXCONST_DBL(0.0f
) ) {
185 #if IMPROVE_ATAN2_ACCURACY
186 at
= +f_atan_expand_range
[sfo
-ATI_SF
-1];
188 at
= FL2FXCONST_DBL( +M_PI
/2 / ATO_SCALE
);
191 else if ( q
< FL2FXCONST_DBL(0.0f
) ) {
192 #if IMPROVE_ATAN2_ACCURACY
193 at
= -f_atan_expand_range
[sfo
-ATI_SF
-1];
195 at
= FL2FXCONST_DBL( -M_PI
/2 / ATO_SCALE
);
198 else { // q== FL2FXCONST_DBL(0.0f)
199 at
= FL2FXCONST_DBL( 0.0f
);
202 // --- calc of fixp_atan() is possible; input data within range
203 // ==> set q on fixed scale level as desired from fixp_atan()
205 if (stf
> 0) q
= q
<< (INT
)fMin( stf
,DFRACT_BITS
-1);
206 else q
= q
>> (INT
)fMin(-stf
,DFRACT_BITS
-1);
207 at
= fixp_atan(q
); // ATO_SF
212 at2
= at
>> (AT2O_SF
- ATO_SF
); // now AT2O_SF for atan2
213 if ( x
> FL2FXCONST_DBL(0.0f
) ) {
216 else if ( x
< FL2FXCONST_DBL(0.0f
) ) {
217 if ( y
>= FL2FXCONST_DBL(0.0f
) ) {
218 ret
= at2
+ FL2FXCONST_DBL( M_PI
/ AT2O_SCALE
);
220 ret
= at2
- FL2FXCONST_DBL( M_PI
/ AT2O_SCALE
);
225 if ( y
> FL2FXCONST_DBL(0.0f
) ) {
226 ret
= FL2FXCONST_DBL( +M_PI
/2 / AT2O_SCALE
);
228 else if ( y
< FL2FXCONST_DBL(0.0f
) ) {
229 ret
= FL2FXCONST_DBL( -M_PI
/2 / AT2O_SCALE
);
231 else if ( y
== FL2FXCONST_DBL(0.0f
) ) {
232 ret
= FL2FXCONST_DBL(0.0f
);
239 FIXP_DBL
fixp_atan(FIXP_DBL x
)
242 FIXP_DBL result
, temp
;
244 // SNR of fixp_atan() = 56 dB
245 FIXP_DBL ONEBY3P56
= (FIXP_DBL
)0x26800000; // 1.0/3.56 in q31
246 FIXP_DBL P281
= (FIXP_DBL
)0x00013000; // 0.281 in q18
247 FIXP_DBL ONEP571
= (FIXP_DBL
)0x6487ef00; // 1.571 in q30
249 if (x
< FIXP_DBL(0)) {
257 if(x
< ( Q(Q_ATANINP
)-FL2FXCONST_DBL(0.00395)) )
261 temp
= fPow2(x
); // q25 * q25 - (DFRACT_BITS-1) = q19
262 temp
= fMult(temp
, ONEBY3P56
); // q19 * q31 - (DFRACT_BITS-1) = q19
263 temp
= temp
+ Q(19); // q19 + q19 = q19
264 result
= fDivNorm(x
, temp
, &res_e
);
265 result
= scaleValue(result
, (Q_ATANOUT
-Q_ATANINP
+19-DFRACT_BITS
+1) + res_e
);
267 else if( x
< FL2FXCONST_DBL(1.28/64.0) )
270 FIXP_DBL PI_BY_4
= FL2FXCONST_DBL(3.1415926/4.0) >> 1; /* pi/4 in q30 */
272 delta_fix
= (x
- FL2FXCONST_DBL(1.0/64.0)) << 5; /* q30 */
273 result
= PI_BY_4
+ (delta_fix
>> 1) - (fPow2Div2(delta_fix
));
279 temp
= fPow2Div2(x
); // q25 * q25 - (DFRACT_BITS-1) - 1 = q18
280 temp
= temp
+ P281
; // q18 + q18 = q18
281 result
= fDivNorm(x
, temp
, &res_e
);
282 result
= scaleValue(result
, (Q_ATANOUT
-Q_ATANINP
+18-DFRACT_BITS
+1) + res_e
);
283 result
= ONEP571
- result
; // q30 + q30 = q30
294 #include "FDK_tools_rom.h"
296 FIXP_DBL
fixp_cos(FIXP_DBL x
, int scale
)
298 FIXP_DBL residual
, error
, sine
, cosine
;
300 residual
= fixp_sin_cos_residual_inline(x
, scale
, &sine
, &cosine
);
301 error
= fMult(sine
, residual
);
303 return cosine
- error
;
306 FIXP_DBL
fixp_sin(FIXP_DBL x
, int scale
)
308 FIXP_DBL residual
, error
, sine
, cosine
;
310 residual
= fixp_sin_cos_residual_inline(x
, scale
, &sine
, &cosine
);
311 error
= fMult(cosine
, residual
);
316 void fixp_cos_sin (FIXP_DBL x
, int scale
, FIXP_DBL
*cos
, FIXP_DBL
*sin
)
318 FIXP_DBL residual
, error0
, error1
, sine
, cosine
;
320 residual
= fixp_sin_cos_residual_inline(x
, scale
, &sine
, &cosine
);
321 error0
= fMult(sine
, residual
);
322 error1
= fMult(cosine
, residual
);
323 *cos
= cosine
- error0
;
324 *sin
= sine
+ error1
;