common_fix.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* -----------------------------------------------------------------------------------------------------------
  2. Software License for The Fraunhofer FDK AAC Codec Library for Android
  3. © Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
  4. All rights reserved.
  5. 1. INTRODUCTION
  6. The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
  7. the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
  8. This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
  9. AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
  10. audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
  11. independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
  12. of the MPEG specifications.
  13. Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
  14. may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
  15. individually for the purpose of encoding or decoding bit streams in products that are compliant with
  16. the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
  17. these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
  18. software may already be covered under those patent licenses when it is used for those licensed purposes only.
  19. Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
  20. are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
  21. applications information and documentation.
  22. 2. COPYRIGHT LICENSE
  23. Redistribution and use in source and binary forms, with or without modification, are permitted without
  24. payment of copyright license fees provided that you satisfy the following conditions:
  25. You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
  26. your modifications thereto in source code form.
  27. You must retain the complete text of this software license in the documentation and/or other materials
  28. provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
  29. You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
  30. modifications thereto to recipients of copies in binary form.
  31. The name of Fraunhofer may not be used to endorse or promote products derived from this library without
  32. prior written permission.
  33. You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
  34. software or your modifications thereto.
  35. Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
  36. and the date of any change. For modified versions of the FDK AAC Codec, the term
  37. "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
  38. "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
  39. 3. NO PATENT LICENSE
  40. NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
  41. ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
  42. respect to this software.
  43. You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
  44. by appropriate patent licenses.
  45. 4. DISCLAIMER
  46. This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
  47. "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
  48. of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  49. CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
  50. including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
  51. or business interruption, however caused and on any theory of liability, whether in contract, strict
  52. liability, or tort (including negligence), arising in any way out of the use of this software, even if
  53. advised of the possibility of such damage.
  54. 5. CONTACT INFORMATION
  55. Fraunhofer Institute for Integrated Circuits IIS
  56. Attention: Audio and Multimedia Departments - FDK AAC LL
  57. Am Wolfsmantel 33
  58. 91058 Erlangen, Germany
  59. www.iis.fraunhofer.de/amm
  60. [email protected]
  61. ----------------------------------------------------------------------------------------------------------- */
  62. /*************************** Fraunhofer IIS FDK Tools **********************
  63. Author(s): M. Lohwasser, M. Gayer
  64. Description: Flexible fixpoint library configuration
  65. ******************************************************************************/
  66. #ifndef _COMMON_FIX_H
  67. #define _COMMON_FIX_H
  68. #include "FDK_archdef.h"
  69. #include "machine_type.h"
  70. /* ***** Start of former fix.h ****** */
  71. /* Configure fractional or integer arithmetic */
  72. #define FIX_FRACT 0 /* Define this to "1" to use fractional arithmetic simulation in class fract instead of integer arithmetic */
  73. /* 1 for debug with extra runtime overflow checking. */
  74. /* Define bit sizes of integer fixpoint fractional data types */
  75. #define FRACT_BITS 16 /* single precision */
  76. #define DFRACT_BITS 32 /* double precision */
  77. #define ACCU_BITS 40 /* double precision plus overflow */
  78. /* Fixpoint equivalent type fot PCM audio time domain data. */
  79. #if defined(SAMPLE_BITS)
  80. #if (SAMPLE_BITS == DFRACT_BITS)
  81. #define FIXP_PCM FIXP_DBL
  82. #define FX_PCM2FX_DBL(x) ((FIXP_DBL)(x))
  83. #define FX_DBL2FX_PCM(x) ((INT_PCM)(x))
  84. #elif (SAMPLE_BITS == FRACT_BITS)
  85. #define FIXP_PCM FIXP_SGL
  86. #define FX_PCM2FX_DBL(x) FX_SGL2FX_DBL((FIXP_SGL)(x))
  87. #define FX_DBL2FX_PCM(x) FX_DBL2FX_SGL(x)
  88. #else
  89. #error SAMPLE_BITS different from FRACT_BITS or DFRACT_BITS not implemented!
  90. #endif
  91. #endif
  92. /* ****** End of former fix.h ****** */
  93. #define SGL_MASK ((1UL<<FRACT_BITS)-1) /* 16bit: (2^16)-1 = 0xFFFF */
  94. #define MAX_SHIFT_SGL (FRACT_BITS-1) /* maximum possible shift for FIXP_SGL values */
  95. #define MAX_SHIFT_DBL (DFRACT_BITS-1) /* maximum possible shift for FIXP_DBL values */
  96. /* Scale factor from/to float/fixpoint values. DO NOT USE THESE VALUES AS SATURATION LIMITS !! */
  97. #define FRACT_FIX_SCALE ((INT64(1)<<(FRACT_BITS-1)))
  98. #define DFRACT_FIX_SCALE ((INT64(1)<<(DFRACT_BITS-1)))
  99. /* Max and Min values for saturation purposes. DO NOT USE THESE VALUES AS SCALE VALUES !! */
  100. #define MAXVAL_SGL ((signed)0x00007FFF) /* this has to be synchronized to FRACT_BITS */
  101. #define MINVAL_SGL ((signed)0xFFFF8000) /* this has to be synchronized to FRACT_BITS */
  102. #define MAXVAL_DBL ((signed)0x7FFFFFFF) /* this has to be synchronized to DFRACT_BITS */
  103. #define MINVAL_DBL ((signed)0x80000000) /* this has to be synchronized to DFRACT_BITS */
  104. #define FX_DBL2FXCONST_SGL(val) ( ( ((((val) >> (DFRACT_BITS-FRACT_BITS-1)) + 1) > (((LONG)1<<FRACT_BITS)-1)) && ((LONG)(val) > 0) ) ? \
  105. (FIXP_SGL)(SHORT)(((LONG)1<<(FRACT_BITS-1))-1):(FIXP_SGL)(SHORT)((((val) >> (DFRACT_BITS-FRACT_BITS-1)) + 1) >> 1) )
  106. #define shouldBeUnion union /* unions are possible */
  107. typedef SHORT FIXP_SGL;
  108. typedef LONG FIXP_DBL;
  109. /* macros for compile-time conversion of constant float values to fixedpoint */
  110. #define FL2FXCONST_SPC FL2FXCONST_DBL
  111. #define MINVAL_DBL_CONST MINVAL_DBL
  112. #define MINVAL_SGL_CONST MINVAL_SGL
  113. #define FL2FXCONST_SGL(val) \
  114. (FIXP_SGL)( ( (val) >= 0) ? \
  115. ((( (double)(val) * (FRACT_FIX_SCALE) + 0.5 ) >= (double)(MAXVAL_SGL) ) ? (SHORT)(MAXVAL_SGL) : (SHORT)( (double)(val) * (double)(FRACT_FIX_SCALE) + 0.5)) : \
  116. ((( (double)(val) * (FRACT_FIX_SCALE) - 0.5) <= (double)(MINVAL_SGL_CONST) ) ? (SHORT)(MINVAL_SGL_CONST) : (SHORT)( (double)(val) * (double)(FRACT_FIX_SCALE) - 0.5)) )
  117. #define FL2FXCONST_DBL(val) \
  118. (FIXP_DBL)( ( (val) >= 0) ? \
  119. ((( (double)(val) * (DFRACT_FIX_SCALE) + 0.5 ) >= (double)(MAXVAL_DBL) ) ? (LONG)(MAXVAL_DBL) : (LONG)( (double)(val) * (double)(DFRACT_FIX_SCALE) + 0.5)) : \
  120. ((( (double)(val) * (DFRACT_FIX_SCALE) - 0.5) <= (double)(MINVAL_DBL_CONST) ) ? (LONG)(MINVAL_DBL_CONST) : (LONG)( (double)(val) * (double)(DFRACT_FIX_SCALE) - 0.5)) )
  121. /* macros for runtime conversion of float values to integer fixedpoint. NO OVERFLOW CHECK!!! */
  122. #define FL2FX_SPC FL2FX_DBL
  123. #define FL2FX_SGL(val) ( (val)>0.0f ? (SHORT)( (val)*(float)(FRACT_FIX_SCALE)+0.5f ) : (SHORT)( (val)*(float)(FRACT_FIX_SCALE)-0.5f ) )
  124. #define FL2FX_DBL(val) ( (val)>0.0f ? (LONG)( (val)*(float)(DFRACT_FIX_SCALE)+0.5f ) : (LONG)( (val)*(float)(DFRACT_FIX_SCALE)-0.5f ) )
  125. /* macros for runtime conversion of fixedpoint values to other fixedpoint. NO ROUNDING!!! */
  126. #define FX_ACC2FX_SGL(val) ((FIXP_SGL)((val)>>(ACCU_BITS-FRACT_BITS)))
  127. #define FX_ACC2FX_DBL(val) ((FIXP_DBL)((val)>>(ACCU_BITS-DFRACT_BITS)))
  128. #define FX_SGL2FX_ACC(val) ((FIXP_ACC)((LONG)(val)<<(ACCU_BITS-FRACT_BITS)))
  129. #define FX_SGL2FX_DBL(val) ((FIXP_DBL)((LONG)(val)<<(DFRACT_BITS-FRACT_BITS)))
  130. #define FX_DBL2FX_SGL(val) ((FIXP_SGL)((val)>>(DFRACT_BITS-FRACT_BITS)))
  131. /* ############################################################# */
  132. /* macros for runtime conversion of integer fixedpoint values to float. */
  133. /* This is just for temporary use and should not be required in a final version! */
  134. /* #define FX_DBL2FL(val) ((float)(pow(2.,-31.)*(float)val)) */ /* version #1 */
  135. #define FX_DBL2FL(val) ((float)((double)(val)/(double)DFRACT_FIX_SCALE)) /* version #2 - identical to class dfract cast from dfract to float */
  136. /* ############################################################# */
  137. #include "fixmul.h"
  138. FDK_INLINE LONG fMult(SHORT a, SHORT b) { return fixmul_SS(a, b); }
  139. FDK_INLINE LONG fMult(SHORT a, LONG b) { return fixmul_SD(a, b); }
  140. FDK_INLINE LONG fMult(LONG a, SHORT b) { return fixmul_DS(a, b); }
  141. FDK_INLINE LONG fMult(LONG a, LONG b) { return fixmul_DD(a, b); }
  142. FDK_INLINE LONG fPow2(LONG a) { return fixpow2_D(a); }
  143. FDK_INLINE LONG fPow2(SHORT a) { return fixpow2_S(a); }
  144. FDK_INLINE INT fMultI(LONG a, SHORT b) { return ( (INT)(((1<<(FRACT_BITS-2)) +
  145. fixmuldiv2_DD(a,((INT)b<<FRACT_BITS)))>>(FRACT_BITS-1)) ); }
  146. FDK_INLINE INT fMultIfloor(LONG a, INT b) { return ( (INT)((1 +
  147. fixmuldiv2_DD(a,(b<<FRACT_BITS))) >> (FRACT_BITS-1)) ); }
  148. FDK_INLINE INT fMultIceil(LONG a, INT b) { return ( (INT)(((INT)0x7fff +
  149. fixmuldiv2_DD(a,(b<<FRACT_BITS))) >> (FRACT_BITS-1)) ); }
  150. FDK_INLINE LONG fMultDiv2(SHORT a, SHORT b) { return fixmuldiv2_SS(a, b); }
  151. FDK_INLINE LONG fMultDiv2(SHORT a, LONG b) { return fixmuldiv2_SD(a, b); }
  152. FDK_INLINE LONG fMultDiv2(LONG a, SHORT b) { return fixmuldiv2_DS(a, b); }
  153. FDK_INLINE LONG fMultDiv2(LONG a, LONG b) { return fixmuldiv2_DD(a, b); }
  154. FDK_INLINE LONG fPow2Div2(LONG a) { return fixpow2div2_D(a); }
  155. FDK_INLINE LONG fPow2Div2(SHORT a) { return fixpow2div2_S(a); }
  156. FDK_INLINE LONG fMultDiv2BitExact(LONG a, LONG b) { return fixmuldiv2BitExact_DD(a, b); }
  157. FDK_INLINE LONG fMultDiv2BitExact(SHORT a, LONG b) { return fixmuldiv2BitExact_SD(a, b); }
  158. FDK_INLINE LONG fMultDiv2BitExact(LONG a, SHORT b) { return fixmuldiv2BitExact_DS(a, b); }
  159. FDK_INLINE LONG fMultBitExact(LONG a, LONG b) { return fixmulBitExact_DD(a, b); }
  160. FDK_INLINE LONG fMultBitExact(SHORT a, LONG b) { return fixmulBitExact_SD(a, b); }
  161. FDK_INLINE LONG fMultBitExact(LONG a, SHORT b) { return fixmulBitExact_DS(a, b); }
  162. /* ******************************************************************************** */
  163. #include "abs.h"
  164. FDK_INLINE FIXP_DBL fAbs(FIXP_DBL x)
  165. { return fixabs_D(x); }
  166. FDK_INLINE FIXP_SGL fAbs(FIXP_SGL x)
  167. { return fixabs_S(x); }
  168. /* workaround for TI C6x compiler but not for TI ARM9E compiler */
  169. #if (!defined(__TI_COMPILER_VERSION__) || defined(__TI_TMS470_V5__)) && !defined(__LP64__)
  170. FDK_INLINE INT fAbs(INT x)
  171. { return fixabs_I(x); }
  172. #endif
  173. /* ******************************************************************************** */
  174. #include "clz.h"
  175. FDK_INLINE INT fNormz(FIXP_DBL x)
  176. { return fixnormz_D(x); }
  177. FDK_INLINE INT fNormz(FIXP_SGL x)
  178. { return fixnormz_S(x); }
  179. FDK_INLINE INT fNorm(FIXP_DBL x)
  180. { return fixnorm_D(x); }
  181. FDK_INLINE INT fNorm(FIXP_SGL x)
  182. { return fixnorm_S(x); }
  183. /* ******************************************************************************** */
  184. /* ******************************************************************************** */
  185. /* ******************************************************************************** */
  186. #include "clz.h"
  187. #define fixp_abs(x) fAbs(x)
  188. #define fixMin(a,b) fMin(a,b)
  189. #define fixMax(a,b) fMax(a,b)
  190. #define CntLeadingZeros(x) fixnormz_D(x)
  191. #define CountLeadingBits(x) fixnorm_D(x)
  192. #include "fixmadd.h"
  193. /* y = (x+0.5*a*b) */
  194. FDK_INLINE FIXP_DBL fMultAddDiv2(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
  195. { return fixmadddiv2_DD(x, a, b); }
  196. FDK_INLINE FIXP_DBL fMultAddDiv2(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
  197. { return fixmadddiv2_SD(x, a, b); }
  198. FDK_INLINE FIXP_DBL fMultAddDiv2(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
  199. { return fixmadddiv2_DS(x, a, b); }
  200. FDK_INLINE FIXP_DBL fMultAddDiv2(FIXP_DBL x, FIXP_SGL a, FIXP_SGL b)
  201. { return fixmadddiv2_SS(x, a, b); }
  202. FDK_INLINE FIXP_DBL fPow2AddDiv2(FIXP_DBL x, FIXP_DBL a)
  203. { return fixpadddiv2_D(x, a); }
  204. FDK_INLINE FIXP_DBL fPow2AddDiv2(FIXP_DBL x, FIXP_SGL a)
  205. { return fixpadddiv2_S(x, a); }
  206. /* y = 2*(x+0.5*a*b) = (2x+a*b) */
  207. FDK_INLINE FIXP_DBL fMultAdd(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
  208. { return fixmadd_DD(x, a, b); }
  209. inline FIXP_DBL fMultAdd(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
  210. { return fixmadd_SD(x, a, b); }
  211. inline FIXP_DBL fMultAdd(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
  212. { return fixmadd_DS(x, a, b); }
  213. inline FIXP_DBL fMultAdd(FIXP_DBL x, FIXP_SGL a, FIXP_SGL b)
  214. { return fixmadd_SS(x, a, b); }
  215. inline FIXP_DBL fPow2Add(FIXP_DBL x, FIXP_DBL a)
  216. { return fixpadd_D(x, a); }
  217. inline FIXP_DBL fPow2Add(FIXP_DBL x, FIXP_SGL a)
  218. { return fixpadd_S(x, a); }
  219. /* y = (x-0.5*a*b) */
  220. inline FIXP_DBL fMultSubDiv2(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
  221. { return fixmsubdiv2_DD(x, a, b); }
  222. inline FIXP_DBL fMultSubDiv2(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
  223. { return fixmsubdiv2_SD(x, a, b); }
  224. inline FIXP_DBL fMultSubDiv2(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
  225. { return fixmsubdiv2_DS(x, a, b); }
  226. inline FIXP_DBL fMultSubDiv2(FIXP_DBL x, FIXP_SGL a, FIXP_SGL b)
  227. { return fixmsubdiv2_SS(x, a, b); }
  228. /* y = 2*(x-0.5*a*b) = (2*x-a*b) */
  229. FDK_INLINE FIXP_DBL fMultSub(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
  230. { return fixmsub_DD(x, a, b); }
  231. inline FIXP_DBL fMultSub(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
  232. { return fixmsub_SD(x, a, b); }
  233. inline FIXP_DBL fMultSub(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
  234. { return fixmsub_DS(x, a, b); }
  235. inline FIXP_DBL fMultSub(FIXP_DBL x, FIXP_SGL a, FIXP_SGL b)
  236. { return fixmsub_SS(x, a, b); }
  237. FDK_INLINE FIXP_DBL fMultAddDiv2BitExact(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
  238. { return fixmadddiv2BitExact_DD(x, a, b); }
  239. FDK_INLINE FIXP_DBL fMultAddDiv2BitExact(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
  240. { return fixmadddiv2BitExact_SD(x, a, b); }
  241. FDK_INLINE FIXP_DBL fMultAddDiv2BitExact(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
  242. { return fixmadddiv2BitExact_DS(x, a, b); }
  243. FDK_INLINE FIXP_DBL fMultSubDiv2BitExact(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
  244. { return fixmsubdiv2BitExact_DD(x, a, b); }
  245. FDK_INLINE FIXP_DBL fMultSubDiv2BitExact(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
  246. { return fixmsubdiv2BitExact_SD(x, a, b); }
  247. FDK_INLINE FIXP_DBL fMultSubDiv2BitExact(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
  248. { return fixmsubdiv2BitExact_DS(x, a, b); }
  249. #include "fixminmax.h"
  250. FDK_INLINE FIXP_DBL fMin(FIXP_DBL a, FIXP_DBL b)
  251. { return fixmin_D(a,b); }
  252. FDK_INLINE FIXP_DBL fMax(FIXP_DBL a, FIXP_DBL b)
  253. { return fixmax_D(a,b); }
  254. FDK_INLINE FIXP_SGL fMin(FIXP_SGL a, FIXP_SGL b)
  255. { return fixmin_S(a,b); }
  256. FDK_INLINE FIXP_SGL fMax(FIXP_SGL a, FIXP_SGL b)
  257. { return fixmax_S(a,b); }
  258. /* workaround for TI C6x compiler but not for TI ARM9E */
  259. #if ((!defined(__TI_COMPILER_VERSION__) || defined(__TI_TMS470_V5__)) && !defined(__LP64__)) || (FIX_FRACT == 1)
  260. FDK_INLINE INT fMax(INT a, INT b)
  261. { return fixmax_I(a,b); }
  262. FDK_INLINE INT fMin(INT a, INT b)
  263. { return fixmin_I(a,b); }
  264. #endif
  265. inline UINT fMax(UINT a, UINT b)
  266. { return fixmax_UI(a,b); }
  267. inline UINT fMin(UINT a, UINT b)
  268. { return fixmin_UI(a,b); }
  269. /* Complex data types */
  270. typedef shouldBeUnion {
  271. /* vector representation for arithmetic */
  272. struct {
  273. FIXP_SGL re;
  274. FIXP_SGL im;
  275. } v;
  276. /* word representation for memory move */
  277. LONG w;
  278. } FIXP_SPK;
  279. typedef shouldBeUnion {
  280. /* vector representation for arithmetic */
  281. struct {
  282. FIXP_DBL re;
  283. FIXP_DBL im;
  284. } v;
  285. /* word representation for memory move */
  286. INT64 w;
  287. } FIXP_DPK;
  288. #include "fixmul.h"
  289. #include "fixmadd.h"
  290. #include "cplx_mul.h"
  291. #include "scale.h"
  292. #include "fixpoint_math.h"
  293. #endif