1/* -----------------------------------------------------------------------------
2Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4© Copyright  1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten
5Forschung e.V. All rights reserved.
6
7 1.    INTRODUCTION
8The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10scheme for digital audio. This FDK AAC Codec software is intended to be used on
11a wide variety of Android devices.
12
13AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14general perceptual audio codecs. AAC-ELD is considered the best-performing
15full-bandwidth communications codec by independent studies and is widely
16deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17specifications.
18
19Patent licenses for necessary patent claims for the FDK AAC Codec (including
20those of Fraunhofer) may be obtained through Via Licensing
21(www.vialicensing.com) or through the respective patent owners individually for
22the purpose of encoding or decoding bit streams in products that are compliant
23with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24Android devices already license these patent claims through Via Licensing or
25directly from the patent owners, and therefore FDK AAC Codec software may
26already be covered under those patent licenses when it is used for those
27licensed purposes only.
28
29Commercially-licensed AAC software libraries, including floating-point versions
30with enhanced sound quality, are also available from Fraunhofer. Users are
31encouraged to check the Fraunhofer website for additional applications
32information and documentation.
33
342.    COPYRIGHT LICENSE
35
36Redistribution and use in source and binary forms, with or without modification,
37are permitted without payment of copyright license fees provided that you
38satisfy the following conditions:
39
40You must retain the complete text of this software license in redistributions of
41the FDK AAC Codec or your modifications thereto in source code form.
42
43You must retain the complete text of this software license in the documentation
44and/or other materials provided with redistributions of the FDK AAC Codec or
45your modifications thereto in binary form. You must make available free of
46charge copies of the complete source code of the FDK AAC Codec and your
47modifications thereto to recipients of copies in binary form.
48
49The name of Fraunhofer may not be used to endorse or promote products derived
50from this library without prior written permission.
51
52You may not charge copyright license fees for anyone to use, copy or distribute
53the FDK AAC Codec software or your modifications thereto.
54
55Your modified versions of the FDK AAC Codec must carry prominent notices stating
56that you changed the software and the date of any change. For modified versions
57of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59AAC Codec Library for Android."
60
613.    NO PATENT LICENSE
62
63NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65Fraunhofer provides no warranty of patent non-infringement with respect to this
66software.
67
68You may use this FDK AAC Codec software or modifications thereto only for
69purposes that are authorized by appropriate patent licenses.
70
714.    DISCLAIMER
72
73This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75including but not limited to the implied warranties of merchantability and
76fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78or consequential damages, including but not limited to procurement of substitute
79goods or services; loss of use, data, or profits, or business interruption,
80however caused and on any theory of liability, whether in contract, strict
81liability, or tort (including negligence), arising in any way out of the use of
82this software, even if advised of the possibility of such damage.
83
845.    CONTACT INFORMATION
85
86Fraunhofer Institute for Integrated Circuits IIS
87Attention: Audio and Multimedia Departments - FDK AAC LL
88Am Wolfsmantel 33
8991058 Erlangen, Germany
90
91www.iis.fraunhofer.de/amm
92amm-info@iis.fraunhofer.de
93----------------------------------------------------------------------------- */
94
95/******************* Library for basic calculation routines ********************
96
97   Author(s):   M. Lohwasser, M. Gayer
98
99   Description: Flexible fixpoint library configuration
100
101*******************************************************************************/
102
103#ifndef COMMON_FIX_H
104#define COMMON_FIX_H
105
106#include "FDK_archdef.h"
107#include "machine_type.h"
108
109/* ***** Start of former fix.h ****** */
110
111/* Define bit sizes of integer fixpoint fractional data types */
112#define FRACT_BITS 16  /* single precision */
113#define DFRACT_BITS 32 /* double precision */
114#define ACCU_BITS 40   /* double precision plus overflow */
115
116/* Fixpoint equivalent type fot PCM audio time domain data. */
117#if defined(SAMPLE_BITS)
118#if (SAMPLE_BITS == DFRACT_BITS)
119#define FIXP_PCM FIXP_DBL
120#define MAXVAL_FIXP_PCM MAXVAL_DBL
121#define MINVAL_FIXP_PCM MINVAL_DBL
122#define FX_PCM2FX_DBL(x) ((FIXP_DBL)(x))
123#define FX_DBL2FX_PCM(x) ((INT_PCM)(x))
124#elif (SAMPLE_BITS == FRACT_BITS)
125#define FIXP_PCM FIXP_SGL
126#define MAXVAL_FIXP_PCM MAXVAL_SGL
127#define MINVAL_FIXP_PCM MINVAL_SGL
128#define FX_PCM2FX_DBL(x) FX_SGL2FX_DBL((FIXP_SGL)(x))
129#define FX_DBL2FX_PCM(x) FX_DBL2FX_SGL(x)
130#else
131#error SAMPLE_BITS different from FRACT_BITS or DFRACT_BITS not implemented!
132#endif
133#endif
134
135/* ****** End of former fix.h ****** */
136
137#define SGL_MASK ((1UL << FRACT_BITS) - 1) /* 16bit: (2^16)-1 = 0xFFFF */
138
139#define MAX_SHIFT_SGL \
140  (FRACT_BITS - 1) /* maximum possible shift for FIXP_SGL values */
141#define MAX_SHIFT_DBL \
142  (DFRACT_BITS - 1) /* maximum possible shift for FIXP_DBL values */
143
144/* Scale factor from/to float/fixpoint values. DO NOT USE THESE VALUES AS
145 * SATURATION LIMITS !! */
146#define FRACT_FIX_SCALE ((INT64(1) << (FRACT_BITS - 1)))
147#define DFRACT_FIX_SCALE ((INT64(1) << (DFRACT_BITS - 1)))
148
149/* Max and Min values for saturation purposes. DO NOT USE THESE VALUES AS SCALE
150 * VALUES !! */
151#define MAXVAL_SGL \
152  ((signed)0x00007FFF) /* this has to be synchronized to FRACT_BITS */
153#define MINVAL_SGL \
154  ((signed)0xFFFF8000) /* this has to be synchronized to FRACT_BITS */
155#define MAXVAL_DBL \
156  ((signed)0x7FFFFFFF) /* this has to be synchronized to DFRACT_BITS */
157#define MINVAL_DBL \
158  ((signed)0x80000000) /* this has to be synchronized to DFRACT_BITS */
159
160#define FX_DBL2FXCONST_SGL(val)                                               \
161  ((((((val) >> (DFRACT_BITS - FRACT_BITS - 1)) + 1) >                        \
162     (((LONG)1 << FRACT_BITS) - 1)) &&                                        \
163    ((LONG)(val) > 0))                                                        \
164       ? (FIXP_SGL)(SHORT)(((LONG)1 << (FRACT_BITS - 1)) - 1)                 \
165       : (FIXP_SGL)(SHORT)((((val) >> (DFRACT_BITS - FRACT_BITS - 1)) + 1) >> \
166                           1))
167
168#define shouldBeUnion union /* unions are possible */
169
170typedef SHORT FIXP_SGL;
171typedef LONG FIXP_DBL;
172
173/* macros for compile-time conversion of constant float values to fixedpoint */
174#define FL2FXCONST_SPC FL2FXCONST_DBL
175
176#define MINVAL_DBL_CONST MINVAL_DBL
177#define MINVAL_SGL_CONST MINVAL_SGL
178
179#define FL2FXCONST_SGL(val)                                                  \
180  (FIXP_SGL)(                                                                \
181      ((val) >= 0)                                                           \
182          ? ((((double)(val) * (FRACT_FIX_SCALE) + 0.5) >=                   \
183              (double)(MAXVAL_SGL))                                          \
184                 ? (SHORT)(MAXVAL_SGL)                                       \
185                 : (SHORT)((double)(val) * (double)(FRACT_FIX_SCALE) + 0.5)) \
186          : ((((double)(val) * (FRACT_FIX_SCALE)-0.5) <=                     \
187              (double)(MINVAL_SGL_CONST))                                    \
188                 ? (SHORT)(MINVAL_SGL_CONST)                                 \
189                 : (SHORT)((double)(val) * (double)(FRACT_FIX_SCALE)-0.5)))
190
191#define FL2FXCONST_DBL(val)                                                  \
192  (FIXP_DBL)(                                                                \
193      ((val) >= 0)                                                           \
194          ? ((((double)(val) * (DFRACT_FIX_SCALE) + 0.5) >=                  \
195              (double)(MAXVAL_DBL))                                          \
196                 ? (LONG)(MAXVAL_DBL)                                        \
197                 : (LONG)((double)(val) * (double)(DFRACT_FIX_SCALE) + 0.5)) \
198          : ((((double)(val) * (DFRACT_FIX_SCALE)-0.5) <=                    \
199              (double)(MINVAL_DBL_CONST))                                    \
200                 ? (LONG)(MINVAL_DBL_CONST)                                  \
201                 : (LONG)((double)(val) * (double)(DFRACT_FIX_SCALE)-0.5)))
202
203/* macros for runtime conversion of float values to integer fixedpoint. NO
204 * OVERFLOW CHECK!!! */
205#define FL2FX_SPC FL2FX_DBL
206#define FL2FX_SGL(val)                                             \
207  ((val) > 0.0f ? (SHORT)((val) * (float)(FRACT_FIX_SCALE) + 0.5f) \
208                : (SHORT)((val) * (float)(FRACT_FIX_SCALE)-0.5f))
209#define FL2FX_DBL(val)                                             \
210  ((val) > 0.0f ? (LONG)((val) * (float)(DFRACT_FIX_SCALE) + 0.5f) \
211                : (LONG)((val) * (float)(DFRACT_FIX_SCALE)-0.5f))
212
213/* macros for runtime conversion of fixedpoint values to other fixedpoint. NO
214 * ROUNDING!!! */
215#define FX_ACC2FX_SGL(val) ((FIXP_SGL)((val) >> (ACCU_BITS - FRACT_BITS)))
216#define FX_ACC2FX_DBL(val) ((FIXP_DBL)((val) >> (ACCU_BITS - DFRACT_BITS)))
217#define FX_SGL2FX_ACC(val) ((FIXP_ACC)((LONG)(val) << (ACCU_BITS - FRACT_BITS)))
218#define FX_SGL2FX_DBL(val) \
219  ((FIXP_DBL)((LONG)(val) << (DFRACT_BITS - FRACT_BITS)))
220#define FX_DBL2FX_SGL(val) ((FIXP_SGL)((val) >> (DFRACT_BITS - FRACT_BITS)))
221
222/* ############################################################# */
223
224/* macros for runtime conversion of integer fixedpoint values to float. */
225
226/* #define FX_DBL2FL(val)  ((float)(pow(2.,-31.)*(float)val)) */ /* version #1
227                                                                  */
228#define FX_DBL2FL(val)                                                      \
229  ((float)((double)(val) / (double)DFRACT_FIX_SCALE)) /* version #2 -       \
230                                                         identical to class \
231                                                         dfract cast from   \
232                                                         dfract to float */
233#define FX_DBL2DOUBLE(val) (((double)(val) / (double)DFRACT_FIX_SCALE))
234
235/* ############################################################# */
236#include "fixmul.h"
237
238FDK_INLINE LONG fMult(SHORT a, SHORT b) { return fixmul_SS(a, b); }
239FDK_INLINE LONG fMult(SHORT a, LONG b) { return fixmul_SD(a, b); }
240FDK_INLINE LONG fMult(LONG a, SHORT b) { return fixmul_DS(a, b); }
241FDK_INLINE LONG fMult(LONG a, LONG b) { return fixmul_DD(a, b); }
242FDK_INLINE LONG fPow2(LONG a) { return fixpow2_D(a); }
243FDK_INLINE LONG fPow2(SHORT a) { return fixpow2_S(a); }
244
245FDK_INLINE LONG fMultDiv2(SHORT a, SHORT b) { return fixmuldiv2_SS(a, b); }
246FDK_INLINE LONG fMultDiv2(SHORT a, LONG b) { return fixmuldiv2_SD(a, b); }
247FDK_INLINE LONG fMultDiv2(LONG a, SHORT b) { return fixmuldiv2_DS(a, b); }
248FDK_INLINE LONG fMultDiv2(LONG a, LONG b) { return fixmuldiv2_DD(a, b); }
249FDK_INLINE LONG fPow2Div2(LONG a) { return fixpow2div2_D(a); }
250FDK_INLINE LONG fPow2Div2(SHORT a) { return fixpow2div2_S(a); }
251
252FDK_INLINE LONG fMultDiv2BitExact(LONG a, LONG b) {
253  return fixmuldiv2BitExact_DD(a, b);
254}
255FDK_INLINE LONG fMultDiv2BitExact(SHORT a, LONG b) {
256  return fixmuldiv2BitExact_SD(a, b);
257}
258FDK_INLINE LONG fMultDiv2BitExact(LONG a, SHORT b) {
259  return fixmuldiv2BitExact_DS(a, b);
260}
261FDK_INLINE LONG fMultBitExact(LONG a, LONG b) {
262  return fixmulBitExact_DD(a, b);
263}
264FDK_INLINE LONG fMultBitExact(SHORT a, LONG b) {
265  return fixmulBitExact_SD(a, b);
266}
267FDK_INLINE LONG fMultBitExact(LONG a, SHORT b) {
268  return fixmulBitExact_DS(a, b);
269}
270
271/* ********************************************************************************
272 */
273#include "abs.h"
274
275FDK_INLINE FIXP_DBL fAbs(FIXP_DBL x) { return fixabs_D(x); }
276FDK_INLINE FIXP_SGL fAbs(FIXP_SGL x) { return fixabs_S(x); }
277
278#if !defined(__LP64__)
279FDK_INLINE INT fAbs(INT x) { return fixabs_I(x); }
280#endif
281
282  /* ********************************************************************************
283   */
284
285#include "clz.h"
286
287FDK_INLINE INT fNormz(INT64 x) {
288  INT clz = fixnormz_D((INT)(x >> 32));
289  if (clz == 32) clz += fixnormz_D((INT)x);
290  return clz;
291}
292FDK_INLINE INT fNormz(FIXP_DBL x) { return fixnormz_D(x); }
293FDK_INLINE INT fNormz(FIXP_SGL x) { return fixnormz_S(x); }
294FDK_INLINE INT fNorm(FIXP_DBL x) { return fixnorm_D(x); }
295FDK_INLINE INT fNorm(FIXP_SGL x) { return fixnorm_S(x); }
296
297  /* ********************************************************************************
298   */
299  /* ********************************************************************************
300   */
301  /* ********************************************************************************
302   */
303
304#include "clz.h"
305#define fixp_abs(x) fAbs(x)
306#define fixMin(a, b) fMin(a, b)
307#define fixMax(a, b) fMax(a, b)
308#define CntLeadingZeros(x) fixnormz_D(x)
309#define CountLeadingBits(x) fixnorm_D(x)
310
311#include "fixmadd.h"
312
313/* y = (x+0.5*a*b) */
314FDK_INLINE FIXP_DBL fMultAddDiv2(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b) {
315  return fixmadddiv2_DD(x, a, b);
316}
317FDK_INLINE FIXP_DBL fMultAddDiv2(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b) {
318  return fixmadddiv2_SD(x, a, b);
319}
320FDK_INLINE FIXP_DBL fMultAddDiv2(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b) {
321  return fixmadddiv2_DS(x, a, b);
322}
323FDK_INLINE FIXP_DBL fMultAddDiv2(FIXP_DBL x, FIXP_SGL a, FIXP_SGL b) {
324  return fixmadddiv2_SS(x, a, b);
325}
326
327FDK_INLINE FIXP_DBL fPow2AddDiv2(FIXP_DBL x, FIXP_DBL a) {
328  return fixpadddiv2_D(x, a);
329}
330FDK_INLINE FIXP_DBL fPow2AddDiv2(FIXP_DBL x, FIXP_SGL a) {
331  return fixpadddiv2_S(x, a);
332}
333
334/* y = 2*(x+0.5*a*b) = (2x+a*b) */
335FDK_INLINE FIXP_DBL fMultAdd(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b) {
336  return fixmadd_DD(x, a, b);
337}
338inline FIXP_DBL fMultAdd(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b) {
339  return fixmadd_SD(x, a, b);
340}
341inline FIXP_DBL fMultAdd(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b) {
342  return fixmadd_DS(x, a, b);
343}
344inline FIXP_DBL fMultAdd(FIXP_DBL x, FIXP_SGL a, FIXP_SGL b) {
345  return fixmadd_SS(x, a, b);
346}
347
348inline FIXP_DBL fPow2Add(FIXP_DBL x, FIXP_DBL a) { return fixpadd_D(x, a); }
349inline FIXP_DBL fPow2Add(FIXP_DBL x, FIXP_SGL a) { return fixpadd_S(x, a); }
350
351/* y = (x-0.5*a*b) */
352inline FIXP_DBL fMultSubDiv2(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b) {
353  return fixmsubdiv2_DD(x, a, b);
354}
355inline FIXP_DBL fMultSubDiv2(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b) {
356  return fixmsubdiv2_SD(x, a, b);
357}
358inline FIXP_DBL fMultSubDiv2(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b) {
359  return fixmsubdiv2_DS(x, a, b);
360}
361inline FIXP_DBL fMultSubDiv2(FIXP_DBL x, FIXP_SGL a, FIXP_SGL b) {
362  return fixmsubdiv2_SS(x, a, b);
363}
364
365/* y = 2*(x-0.5*a*b) = (2*x-a*b) */
366FDK_INLINE FIXP_DBL fMultSub(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b) {
367  return fixmsub_DD(x, a, b);
368}
369inline FIXP_DBL fMultSub(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b) {
370  return fixmsub_SD(x, a, b);
371}
372inline FIXP_DBL fMultSub(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b) {
373  return fixmsub_DS(x, a, b);
374}
375inline FIXP_DBL fMultSub(FIXP_DBL x, FIXP_SGL a, FIXP_SGL b) {
376  return fixmsub_SS(x, a, b);
377}
378
379FDK_INLINE FIXP_DBL fMultAddDiv2BitExact(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b) {
380  return fixmadddiv2BitExact_DD(x, a, b);
381}
382FDK_INLINE FIXP_DBL fMultAddDiv2BitExact(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b) {
383  return fixmadddiv2BitExact_SD(x, a, b);
384}
385FDK_INLINE FIXP_DBL fMultAddDiv2BitExact(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b) {
386  return fixmadddiv2BitExact_DS(x, a, b);
387}
388FDK_INLINE FIXP_DBL fMultSubDiv2BitExact(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b) {
389  return fixmsubdiv2BitExact_DD(x, a, b);
390}
391FDK_INLINE FIXP_DBL fMultSubDiv2BitExact(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b) {
392  return fixmsubdiv2BitExact_SD(x, a, b);
393}
394FDK_INLINE FIXP_DBL fMultSubDiv2BitExact(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b) {
395  return fixmsubdiv2BitExact_DS(x, a, b);
396}
397
398#include "fixminmax.h"
399
400FDK_INLINE FIXP_DBL fMin(FIXP_DBL a, FIXP_DBL b) { return fixmin_D(a, b); }
401FDK_INLINE FIXP_DBL fMax(FIXP_DBL a, FIXP_DBL b) { return fixmax_D(a, b); }
402
403FDK_INLINE FIXP_SGL fMin(FIXP_SGL a, FIXP_SGL b) { return fixmin_S(a, b); }
404FDK_INLINE FIXP_SGL fMax(FIXP_SGL a, FIXP_SGL b) { return fixmax_S(a, b); }
405
406#if !defined(__LP64__)
407FDK_INLINE INT fMax(INT a, INT b) { return fixmax_I(a, b); }
408FDK_INLINE INT fMin(INT a, INT b) { return fixmin_I(a, b); }
409#if !defined(_MSC_VER) && defined(__x86_64__)
410FDK_INLINE SHORT fMax(SHORT a, SHORT b) { return fixmax_S(a, b); }
411FDK_INLINE SHORT fMin(SHORT a, SHORT b) { return fixmin_S(a, b); }
412#endif
413#endif
414
415inline UINT fMax(UINT a, UINT b) { return fixmax_UI(a, b); }
416inline UINT fMin(UINT a, UINT b) { return fixmin_UI(a, b); }
417
418inline UCHAR fMax(UCHAR a, UCHAR b) {
419  return (UCHAR)fixmax_UI((UINT)a, (UINT)b);
420}
421inline UCHAR fMin(UCHAR a, UCHAR b) {
422  return (UCHAR)fixmin_UI((UINT)a, (UINT)b);
423}
424
425/* Complex data types */
426typedef shouldBeUnion {
427  /* vector representation for arithmetic */
428  struct {
429    FIXP_SGL re;
430    FIXP_SGL im;
431  } v;
432  /* word representation for memory move */
433  LONG w;
434}
435FIXP_SPK;
436
437typedef shouldBeUnion {
438  /* vector representation for arithmetic */
439  struct {
440    FIXP_DBL re;
441    FIXP_DBL im;
442  } v;
443  /* word representation for memory move */
444  INT64 w;
445}
446FIXP_DPK;
447
448#include "fixmul.h"
449#include "fixmadd.h"
450#include "cplx_mul.h"
451#include "fixpoint_math.h"
452
453#endif
454