1
2/* -----------------------------------------------------------------------------------------------------------
3Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5� Copyright  1995 - 2013 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
6  All rights reserved.
7
8 1.    INTRODUCTION
9The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12
13AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16of the MPEG specifications.
17
18Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20individually for the purpose of encoding or decoding bit streams in products that are compliant with
21the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23software may already be covered under those patent licenses when it is used for those licensed purposes only.
24
25Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27applications information and documentation.
28
292.    COPYRIGHT LICENSE
30
31Redistribution and use in source and binary forms, with or without modification, are permitted without
32payment of copyright license fees provided that you satisfy the following conditions:
33
34You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35your modifications thereto in source code form.
36
37You must retain the complete text of this software license in the documentation and/or other materials
38provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40modifications thereto to recipients of copies in binary form.
41
42The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43prior written permission.
44
45You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46software or your modifications thereto.
47
48Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49and 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."
52
533.    NO PATENT LICENSE
54
55NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57respect to this software.
58
59You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60by appropriate patent licenses.
61
624.    DISCLAIMER
63
64This 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
66of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69or business interruption, however caused and on any theory of liability, whether in contract, strict
70liability, or tort (including negligence), arising in any way out of the use of this software, even if
71advised of the possibility of such damage.
72
735.    CONTACT INFORMATION
74
75Fraunhofer Institute for Integrated Circuits IIS
76Attention: Audio and Multimedia Departments - FDK AAC LL
77Am Wolfsmantel 33
7891058 Erlangen, Germany
79
80www.iis.fraunhofer.de/amm
81amm-info@iis.fraunhofer.de
82----------------------------------------------------------------------------------------------------------- */
83
84/***************************  Fraunhofer IIS FDK Tools  **********************
85
86   Author(s):
87   Description: Scaling operations
88
89******************************************************************************/
90
91#include "common_fix.h"
92
93#include "genericStds.h"
94
95/**************************************************
96 * Inline definitions
97 **************************************************/
98
99#define SCALE_INLINE inline
100
101
102#if defined(__mips__)	/* cppp replaced: elif */
103#include "mips/scale.cpp"
104
105#elif defined(__arm__)
106#include "arm/scale_arm.cpp"
107
108#endif
109
110#ifndef FUNCTION_scaleValues_SGL
111/*!
112 *
113 *  \brief  Multiply input vector by \f$ 2^{scalefactor} \f$
114 *  \param len    must be larger than 4
115 *  \return void
116 *
117 */
118#define FUNCTION_scaleValues_SGL
119SCALE_INLINE
120void scaleValues(FIXP_SGL *vector,  /*!< Vector */
121                 INT len,            /*!< Length */
122                 INT scalefactor     /*!< Scalefactor */
123                 )
124{
125  INT i;
126
127  /* Return if scalefactor is Zero */
128  if (scalefactor==0) return;
129
130  if(scalefactor > 0){
131    scalefactor = fixmin_I(scalefactor,(INT)(DFRACT_BITS-1));
132    for (i = len&3; i--; )
133    {
134      *(vector++) <<= scalefactor;
135    }
136    for (i = len>>2; i--; )
137    {
138      *(vector++) <<= scalefactor;
139      *(vector++) <<= scalefactor;
140      *(vector++) <<= scalefactor;
141      *(vector++) <<= scalefactor;
142    }
143  } else {
144    INT negScalefactor = fixmin_I(-scalefactor,(INT)DFRACT_BITS-1);
145    for (i = len&3; i--; )
146    {
147      *(vector++) >>= negScalefactor;
148    }
149    for (i = len>>2; i--; )
150    {
151      *(vector++) >>= negScalefactor;
152      *(vector++) >>= negScalefactor;
153      *(vector++) >>= negScalefactor;
154      *(vector++) >>= negScalefactor;
155    }
156  }
157}
158#endif
159
160#ifndef FUNCTION_scaleValues_DBL
161/*!
162 *
163 *  \brief  Multiply input vector by \f$ 2^{scalefactor} \f$
164 *  \param len must be larger than 4
165 *  \return void
166 *
167 */
168#define FUNCTION_scaleValues_DBL
169SCALE_INLINE
170void scaleValues(FIXP_DBL *vector,    /*!< Vector */
171                 INT len,             /*!< Length */
172                 INT scalefactor      /*!< Scalefactor */
173                )
174{
175  INT i;
176
177  /* Return if scalefactor is Zero */
178  if (scalefactor==0) return;
179
180  if(scalefactor > 0){
181    scalefactor = fixmin_I(scalefactor,(INT)DFRACT_BITS-1);
182    for (i = len&3; i--; )
183    {
184      *(vector++) <<= scalefactor;
185    }
186    for (i = len>>2; i--; )
187    {
188      *(vector++) <<= scalefactor;
189      *(vector++) <<= scalefactor;
190      *(vector++) <<= scalefactor;
191      *(vector++) <<= scalefactor;
192    }
193  } else {
194    INT negScalefactor = fixmin_I(-scalefactor,(INT)DFRACT_BITS-1);
195    for (i = len&3; i--; )
196    {
197      *(vector++) >>= negScalefactor;
198    }
199    for (i = len>>2; i--; )
200    {
201      *(vector++) >>= negScalefactor;
202      *(vector++) >>= negScalefactor;
203      *(vector++) >>= negScalefactor;
204      *(vector++) >>= negScalefactor;
205    }
206  }
207}
208#endif
209
210#ifndef FUNCTION_scaleValues_DBLDBL
211/*!
212 *
213 *  \brief  Multiply input vector src by \f$ 2^{scalefactor} \f$
214 *          and place result into dst
215 *  \param dst detination buffer
216 *  \param src source buffer
217 *  \param len must be larger than 4
218 *  \param scalefactor amount of left shifts to be applied
219 *  \return void
220 *
221 */
222#define FUNCTION_scaleValues_DBLDBL
223SCALE_INLINE
224void scaleValues(FIXP_DBL *dst,       /*!< dst Vector */
225                 const FIXP_DBL *src, /*!< src Vector */
226                 INT len,             /*!< Length */
227                 INT scalefactor      /*!< Scalefactor */
228                )
229{
230  INT i;
231
232  /* Return if scalefactor is Zero */
233  if (scalefactor==0) {
234	if (dst != src)
235      FDKmemmove(dst, src, len*sizeof(FIXP_DBL));
236  }
237  else {
238
239    if(scalefactor > 0){
240      scalefactor = fixmin_I(scalefactor,(INT)DFRACT_BITS-1);
241      for (i = len&3; i--; )
242      {
243        *(dst++) = *(src++) << scalefactor;
244      }
245      for (i = len>>2; i--; )
246      {
247        *(dst++) = *(src++) << scalefactor;
248        *(dst++) = *(src++) << scalefactor;
249        *(dst++) = *(src++) << scalefactor;
250        *(dst++) = *(src++) << scalefactor;
251      }
252    } else {
253      INT negScalefactor = fixmin_I(-scalefactor,(INT)DFRACT_BITS-1);
254      for (i = len&3; i--; )
255      {
256        *(dst++) = *(src++) >> negScalefactor;
257      }
258      for (i = len>>2; i--; )
259      {
260        *(dst++) = *(src++) >> negScalefactor;
261        *(dst++) = *(src++) >> negScalefactor;
262        *(dst++) = *(src++) >> negScalefactor;
263        *(dst++) = *(src++) >> negScalefactor;
264      }
265    }
266  }
267}
268#endif
269
270#ifndef FUNCTION_scaleValuesWithFactor_DBL
271/*!
272 *
273 *  \brief  Multiply input vector by \f$ 2^{scalefactor} \f$
274 *  \param len must be larger than 4
275 *  \return void
276 *
277 */
278#define FUNCTION_scaleValuesWithFactor_DBL
279SCALE_INLINE
280void scaleValuesWithFactor(
281        FIXP_DBL *vector,
282        FIXP_DBL factor,
283        INT len,
284        INT scalefactor
285        )
286{
287  INT i;
288
289  /* Compensate fMultDiv2 */
290  scalefactor++;
291
292  if(scalefactor > 0){
293    scalefactor = fixmin_I(scalefactor,(INT)DFRACT_BITS-1);
294    for (i = len&3; i--; )
295    {
296      *vector = fMultDiv2(*vector, factor) << scalefactor;
297      vector++;
298    }
299    for (i = len>>2; i--; )
300    {
301      *vector = fMultDiv2(*vector, factor) << scalefactor; vector++;
302      *vector = fMultDiv2(*vector, factor) << scalefactor; vector++;
303      *vector = fMultDiv2(*vector, factor) << scalefactor; vector++;
304      *vector = fMultDiv2(*vector, factor) << scalefactor; vector++;
305    }
306  } else {
307    INT negScalefactor = fixmin_I(-scalefactor,(INT)DFRACT_BITS-1);
308    for (i = len&3; i--; )
309    {
310      *vector = fMultDiv2(*vector, factor) >> negScalefactor;
311      vector++;
312    }
313    for (i = len>>2; i--; )
314    {
315      *vector = fMultDiv2(*vector, factor) >> negScalefactor; vector++;
316      *vector = fMultDiv2(*vector, factor) >> negScalefactor; vector++;
317      *vector = fMultDiv2(*vector, factor) >> negScalefactor; vector++;
318      *vector = fMultDiv2(*vector, factor) >> negScalefactor; vector++;
319    }
320  }
321}
322#endif /* FUNCTION_scaleValuesWithFactor_DBL */
323
324
325/*******************************************
326
327IMPORTANT NOTE for usage of getScalefactor()
328
329If the input array contains negative values too, then these functions may sometimes return
330the actual maximum value minus 1, due to the nature of the applied algorithm.
331So be careful with possible fractional -1 values that may lead to overflows when being fPow2()'ed.
332
333********************************************/
334
335
336
337#ifndef FUNCTION_getScalefactorShort
338/*!
339 *
340 *  \brief Calculate max possible scale factor for input vector of shorts
341 *
342 *  \return Maximum scale factor / possible left shift
343 *
344 */
345#define FUNCTION_getScalefactorShort
346SCALE_INLINE
347INT getScalefactorShort(const SHORT *vector, /*!< Pointer to input vector */
348                        INT len              /*!< Length of input vector */
349                       )
350{
351  INT i;
352  SHORT temp, maxVal = 0;
353
354  for(i=len;i!=0;i--){
355    temp = (SHORT)(*vector++);
356    maxVal |= (temp^(temp>>(SHORT_BITS-1)));
357  }
358
359  return fixmax_I((INT)0,(INT)(fixnormz_D((INT)maxVal) - (INT)1 - (INT)(DFRACT_BITS - SHORT_BITS)));
360}
361#endif
362
363#ifndef FUNCTION_getScalefactorPCM
364/*!
365 *
366 *  \brief Calculate max possible scale factor for input vector of shorts
367 *
368 *  \return Maximum scale factor
369 *
370 */
371#define FUNCTION_getScalefactorPCM
372SCALE_INLINE
373INT getScalefactorPCM(const INT_PCM *vector, /*!< Pointer to input vector */
374                      INT len,               /*!< Length of input vector */
375                      INT stride
376                      )
377{
378  INT i;
379  INT_PCM temp, maxVal = 0;
380
381  for(i=len;i!=0;i--){
382    temp = (INT_PCM)(*vector); vector+=stride;
383    maxVal |= (temp^(temp>>((sizeof(INT_PCM)*8)-1)));
384  }
385  return fixmax_I((INT)0,(INT)(fixnormz_D((INT)maxVal) - (INT)1 - (INT)(DFRACT_BITS - SAMPLE_BITS)));
386}
387#endif
388
389#ifndef FUNCTION_getScalefactorShort
390/*!
391 *
392 *  \brief Calculate max possible scale factor for input vector of shorts
393 *  \param stride, item increment between vector members.
394 *  \return Maximum scale factor
395 *
396 */
397#define FUNCTION_getScalefactorShort
398SCALE_INLINE
399INT getScalefactorShort(const SHORT *vector, /*!< Pointer to input vector */
400                        INT len,             /*!< Length of input vector */
401                        INT stride
402                       )
403{
404  INT i;
405  SHORT temp, maxVal = 0;
406
407  for(i=len;i!=0;i--){
408    temp = (SHORT)(*vector); vector+=stride;
409    maxVal |= (temp^(temp>>(SHORT_BITS-1)));
410  }
411
412  return fixmax_I((INT)0,(INT)(fixnormz_D((INT)maxVal) - (INT)1 - (INT)(DFRACT_BITS - SHORT_BITS)));
413}
414#endif
415
416#ifndef FUNCTION_getScalefactor_DBL
417/*!
418 *
419 *  \brief Calculate max possible scale factor for input vector
420 *
421 *  \return Maximum scale factor
422 *
423 *  This function can constitute a significant amount of computational complexity - very much depending on the
424 *  bitrate. Since it is a rather small function, effective assembler optimization might be possible.
425 *
426 */
427#define FUNCTION_getScalefactor_DBL
428SCALE_INLINE
429INT getScalefactor(const FIXP_DBL *vector, /*!< Pointer to input vector */
430                   INT len)                /*!< Length of input vector */
431{
432  INT i;
433  FIXP_DBL temp, maxVal = (FIXP_DBL)0;
434
435  for(i=len;i!=0;i--){
436    temp = (LONG)(*vector++);
437    maxVal |= (FIXP_DBL)((LONG)temp^(LONG)(temp>>(DFRACT_BITS-1)));
438  }
439
440  return fixmax_I((INT)0,(INT)(fixnormz_D(maxVal) - 1));
441}
442#endif
443
444#ifndef FUNCTION_getScalefactor_SGL
445#define FUNCTION_getScalefactor_SGL
446SCALE_INLINE
447INT getScalefactor(const FIXP_SGL *vector, /*!< Pointer to input vector */
448                   INT len)                /*!< Length of input vector */
449{
450  INT i;
451  SHORT temp, maxVal = (FIXP_SGL)0;
452
453  for(i=len;i!=0;i--){
454    temp = (SHORT)(*vector++);
455    maxVal |= (temp^(temp>>(FRACT_BITS-1)));
456  }
457
458  return fixmax_I((INT)0,(INT)(fixnormz_D(FX_SGL2FX_DBL((FIXP_SGL)maxVal)) - 1));
459}
460#endif
461
462