1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3 
4 © Copyright  1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten
5 Forschung e.V. All rights reserved.
6 
7  1.    INTRODUCTION
8 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9 that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10 scheme for digital audio. This FDK AAC Codec software is intended to be used on
11 a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14 general perceptual audio codecs. AAC-ELD is considered the best-performing
15 full-bandwidth communications codec by independent studies and is widely
16 deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17 specifications.
18 
19 Patent licenses for necessary patent claims for the FDK AAC Codec (including
20 those of Fraunhofer) may be obtained through Via Licensing
21 (www.vialicensing.com) or through the respective patent owners individually for
22 the purpose of encoding or decoding bit streams in products that are compliant
23 with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24 Android devices already license these patent claims through Via Licensing or
25 directly from the patent owners, and therefore FDK AAC Codec software may
26 already be covered under those patent licenses when it is used for those
27 licensed purposes only.
28 
29 Commercially-licensed AAC software libraries, including floating-point versions
30 with enhanced sound quality, are also available from Fraunhofer. Users are
31 encouraged to check the Fraunhofer website for additional applications
32 information and documentation.
33 
34 2.    COPYRIGHT LICENSE
35 
36 Redistribution and use in source and binary forms, with or without modification,
37 are permitted without payment of copyright license fees provided that you
38 satisfy the following conditions:
39 
40 You must retain the complete text of this software license in redistributions of
41 the FDK AAC Codec or your modifications thereto in source code form.
42 
43 You must retain the complete text of this software license in the documentation
44 and/or other materials provided with redistributions of the FDK AAC Codec or
45 your modifications thereto in binary form. You must make available free of
46 charge copies of the complete source code of the FDK AAC Codec and your
47 modifications thereto to recipients of copies in binary form.
48 
49 The name of Fraunhofer may not be used to endorse or promote products derived
50 from this library without prior written permission.
51 
52 You may not charge copyright license fees for anyone to use, copy or distribute
53 the FDK AAC Codec software or your modifications thereto.
54 
55 Your modified versions of the FDK AAC Codec must carry prominent notices stating
56 that you changed the software and the date of any change. For modified versions
57 of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58 must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59 AAC Codec Library for Android."
60 
61 3.    NO PATENT LICENSE
62 
63 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64 limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65 Fraunhofer provides no warranty of patent non-infringement with respect to this
66 software.
67 
68 You may use this FDK AAC Codec software or modifications thereto only for
69 purposes that are authorized by appropriate patent licenses.
70 
71 4.    DISCLAIMER
72 
73 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74 holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75 including but not limited to the implied warranties of merchantability and
76 fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78 or consequential damages, including but not limited to procurement of substitute
79 goods or services; loss of use, data, or profits, or business interruption,
80 however caused and on any theory of liability, whether in contract, strict
81 liability, or tort (including negligence), arising in any way out of the use of
82 this software, even if advised of the possibility of such damage.
83 
84 5.    CONTACT INFORMATION
85 
86 Fraunhofer Institute for Integrated Circuits IIS
87 Attention: Audio and Multimedia Departments - FDK AAC LL
88 Am Wolfsmantel 33
89 91058 Erlangen, Germany
90 
91 www.iis.fraunhofer.de/amm
92 amm-info@iis.fraunhofer.de
93 ----------------------------------------------------------------------------- */
94 
95 /******************* Library for basic calculation routines ********************
96 
97    Author(s):
98 
99    Description: Scaling operations
100 
101 *******************************************************************************/
102 
103 #include "common_fix.h"
104 
105 #include "genericStds.h"
106 
107 /**************************************************
108  * Inline definitions
109  **************************************************/
110 
111 #include "scale.h"
112 
113 #if defined(__mips__)
114 #include "mips/scale_mips.cpp"
115 
116 #elif defined(__arm__)
117 #include "arm/scale_arm.cpp"
118 
119 #endif
120 
121 #ifndef FUNCTION_scaleValues_SGL
122 /*!
123  *
124  *  \brief  Multiply input vector by \f$ 2^{scalefactor} \f$
125  *  \param len    must be larger than 4
126  *  \return void
127  *
128  */
129 #define FUNCTION_scaleValues_SGL
scaleValues(FIXP_SGL * vector,INT len,INT scalefactor)130 void scaleValues(FIXP_SGL *vector, /*!< Vector */
131                  INT len,          /*!< Length */
132                  INT scalefactor   /*!< Scalefactor */
133 ) {
134   INT i;
135 
136   /* Return if scalefactor is Zero */
137   if (scalefactor == 0) return;
138 
139   if (scalefactor > 0) {
140     scalefactor = fixmin_I(scalefactor, (INT)(FRACT_BITS - 1));
141     for (i = len & 3; i--;) {
142       *(vector++) <<= scalefactor;
143     }
144     for (i = len >> 2; i--;) {
145       *(vector++) <<= scalefactor;
146       *(vector++) <<= scalefactor;
147       *(vector++) <<= scalefactor;
148       *(vector++) <<= scalefactor;
149     }
150   } else {
151     INT negScalefactor = fixmin_I(-scalefactor, (INT)FRACT_BITS - 1);
152     for (i = len & 3; i--;) {
153       *(vector++) >>= negScalefactor;
154     }
155     for (i = len >> 2; i--;) {
156       *(vector++) >>= negScalefactor;
157       *(vector++) >>= negScalefactor;
158       *(vector++) >>= negScalefactor;
159       *(vector++) >>= negScalefactor;
160     }
161   }
162 }
163 #endif
164 
165 #ifndef FUNCTION_scaleValues_DBL
166 /*!
167  *
168  *  \brief  Multiply input vector by \f$ 2^{scalefactor} \f$
169  *  \param len must be larger than 4
170  *  \return void
171  *
172  */
173 #define FUNCTION_scaleValues_DBL
174 SCALE_INLINE
scaleValues(FIXP_DBL * vector,INT len,INT scalefactor)175 void scaleValues(FIXP_DBL *vector, /*!< Vector */
176                  INT len,          /*!< Length */
177                  INT scalefactor   /*!< Scalefactor */
178 ) {
179   INT i;
180 
181   /* Return if scalefactor is Zero */
182   if (scalefactor == 0) return;
183 
184   if (scalefactor > 0) {
185     scalefactor = fixmin_I(scalefactor, (INT)DFRACT_BITS - 1);
186     for (i = len & 3; i--;) {
187       *(vector++) <<= scalefactor;
188     }
189     for (i = len >> 2; i--;) {
190       *(vector++) <<= scalefactor;
191       *(vector++) <<= scalefactor;
192       *(vector++) <<= scalefactor;
193       *(vector++) <<= scalefactor;
194     }
195   } else {
196     INT negScalefactor = fixmin_I(-scalefactor, (INT)DFRACT_BITS - 1);
197     for (i = len & 3; i--;) {
198       *(vector++) >>= negScalefactor;
199     }
200     for (i = len >> 2; i--;) {
201       *(vector++) >>= negScalefactor;
202       *(vector++) >>= negScalefactor;
203       *(vector++) >>= negScalefactor;
204       *(vector++) >>= negScalefactor;
205     }
206   }
207 }
208 #endif
209 
210 #ifndef FUNCTION_scaleValuesSaturate_DBL
211 /*!
212  *
213  *  \brief  Multiply input vector by \f$ 2^{scalefactor} \f$
214  *  \param vector      source/destination buffer
215  *  \param len         length of vector
216  *  \param scalefactor amount of shifts to be applied
217  *  \return void
218  *
219  */
220 #define FUNCTION_scaleValuesSaturate_DBL
221 SCALE_INLINE
scaleValuesSaturate(FIXP_DBL * vector,INT len,INT scalefactor)222 void scaleValuesSaturate(FIXP_DBL *vector, /*!< Vector */
223                          INT len,          /*!< Length */
224                          INT scalefactor   /*!< Scalefactor */
225 ) {
226   INT i;
227 
228   /* Return if scalefactor is Zero */
229   if (scalefactor == 0) return;
230 
231   scalefactor = fixmax_I(fixmin_I(scalefactor, (INT)DFRACT_BITS - 1),
232                          (INT) - (DFRACT_BITS - 1));
233 
234   for (i = 0; i < len; i++) {
235     vector[i] = scaleValueSaturate(vector[i], scalefactor);
236   }
237 }
238 #endif /* FUNCTION_scaleValuesSaturate_DBL */
239 
240 #ifndef FUNCTION_scaleValuesSaturate_DBL_DBL
241 /*!
242  *
243  *  \brief  Multiply input vector by \f$ 2^{scalefactor} \f$
244  *  \param dst         destination buffer
245  *  \param src         source buffer
246  *  \param len         length of vector
247  *  \param scalefactor amount of shifts to be applied
248  *  \return void
249  *
250  */
251 #define FUNCTION_scaleValuesSaturate_DBL_DBL
252 SCALE_INLINE
scaleValuesSaturate(FIXP_DBL * dst,FIXP_DBL * src,INT len,INT scalefactor)253 void scaleValuesSaturate(FIXP_DBL *dst,  /*!< Output */
254                          FIXP_DBL *src,  /*!< Input   */
255                          INT len,        /*!< Length */
256                          INT scalefactor /*!< Scalefactor */
257 ) {
258   INT i;
259 
260   /* Return if scalefactor is Zero */
261   if (scalefactor == 0) {
262     FDKmemmove(dst, src, len * sizeof(FIXP_DBL));
263     return;
264   }
265 
266   scalefactor = fixmax_I(fixmin_I(scalefactor, (INT)DFRACT_BITS - 1),
267                          (INT) - (DFRACT_BITS - 1));
268 
269   for (i = 0; i < len; i++) {
270     dst[i] = scaleValueSaturate(src[i], scalefactor);
271   }
272 }
273 #endif /* FUNCTION_scaleValuesSaturate_DBL_DBL */
274 
275 #ifndef FUNCTION_scaleValuesSaturate_SGL_DBL
276 /*!
277  *
278  *  \brief  Multiply input vector by \f$ 2^{scalefactor} \f$
279  *  \param dst         destination buffer (FIXP_SGL)
280  *  \param src         source buffer (FIXP_DBL)
281  *  \param len         length of vector
282  *  \param scalefactor amount of shifts to be applied
283  *  \return void
284  *
285  */
286 #define FUNCTION_scaleValuesSaturate_SGL_DBL
287 SCALE_INLINE
scaleValuesSaturate(FIXP_SGL * dst,FIXP_DBL * src,INT len,INT scalefactor)288 void scaleValuesSaturate(FIXP_SGL *dst,   /*!< Output */
289                          FIXP_DBL *src,   /*!< Input   */
290                          INT len,         /*!< Length */
291                          INT scalefactor) /*!< Scalefactor */
292 {
293   INT i;
294   scalefactor = fixmax_I(fixmin_I(scalefactor, (INT)DFRACT_BITS - 1),
295                          (INT) - (DFRACT_BITS - 1));
296 
297   for (i = 0; i < len; i++) {
298     dst[i] = FX_DBL2FX_SGL(fAddSaturate(scaleValueSaturate(src[i], scalefactor),
299                                         (FIXP_DBL)0x8000));
300   }
301 }
302 #endif /* FUNCTION_scaleValuesSaturate_SGL_DBL */
303 
304 #ifndef FUNCTION_scaleValuesSaturate_SGL
305 /*!
306  *
307  *  \brief  Multiply input vector by \f$ 2^{scalefactor} \f$
308  *  \param vector      source/destination buffer
309  *  \param len         length of vector
310  *  \param scalefactor amount of shifts to be applied
311  *  \return void
312  *
313  */
314 #define FUNCTION_scaleValuesSaturate_SGL
315 SCALE_INLINE
scaleValuesSaturate(FIXP_SGL * vector,INT len,INT scalefactor)316 void scaleValuesSaturate(FIXP_SGL *vector, /*!< Vector */
317                          INT len,          /*!< Length */
318                          INT scalefactor   /*!< Scalefactor */
319 ) {
320   INT i;
321 
322   /* Return if scalefactor is Zero */
323   if (scalefactor == 0) return;
324 
325   scalefactor = fixmax_I(fixmin_I(scalefactor, (INT)DFRACT_BITS - 1),
326                          (INT) - (DFRACT_BITS - 1));
327 
328   for (i = 0; i < len; i++) {
329     vector[i] = FX_DBL2FX_SGL(
330         scaleValueSaturate(FX_SGL2FX_DBL(vector[i]), scalefactor));
331   }
332 }
333 #endif /* FUNCTION_scaleValuesSaturate_SGL */
334 
335 #ifndef FUNCTION_scaleValuesSaturate_SGL_SGL
336 /*!
337  *
338  *  \brief  Multiply input vector by \f$ 2^{scalefactor} \f$
339  *  \param dst         destination buffer
340  *  \param src         source buffer
341  *  \param len         length of vector
342  *  \param scalefactor amount of shifts to be applied
343  *  \return void
344  *
345  */
346 #define FUNCTION_scaleValuesSaturate_SGL_SGL
347 SCALE_INLINE
scaleValuesSaturate(FIXP_SGL * dst,FIXP_SGL * src,INT len,INT scalefactor)348 void scaleValuesSaturate(FIXP_SGL *dst,  /*!< Output */
349                          FIXP_SGL *src,  /*!< Input */
350                          INT len,        /*!< Length */
351                          INT scalefactor /*!< Scalefactor */
352 ) {
353   INT i;
354 
355   /* Return if scalefactor is Zero */
356   if (scalefactor == 0) {
357     FDKmemmove(dst, src, len * sizeof(FIXP_SGL));
358     return;
359   }
360 
361   scalefactor = fixmax_I(fixmin_I(scalefactor, (INT)DFRACT_BITS - 1),
362                          (INT) - (DFRACT_BITS - 1));
363 
364   for (i = 0; i < len; i++) {
365     dst[i] =
366         FX_DBL2FX_SGL(scaleValueSaturate(FX_SGL2FX_DBL(src[i]), scalefactor));
367   }
368 }
369 #endif /* FUNCTION_scaleValuesSaturate_SGL_SGL */
370 
371 #ifndef FUNCTION_scaleValues_DBLDBL
372 /*!
373  *
374  *  \brief  Multiply input vector src by \f$ 2^{scalefactor} \f$
375  *          and place result into dst
376  *  \param dst detination buffer
377  *  \param src source buffer
378  *  \param len must be larger than 4
379  *  \param scalefactor amount of left shifts to be applied
380  *  \return void
381  *
382  */
383 #define FUNCTION_scaleValues_DBLDBL
384 SCALE_INLINE
scaleValues(FIXP_DBL * dst,const FIXP_DBL * src,INT len,INT scalefactor)385 void scaleValues(FIXP_DBL *dst,       /*!< dst Vector */
386                  const FIXP_DBL *src, /*!< src Vector */
387                  INT len,             /*!< Length */
388                  INT scalefactor      /*!< Scalefactor */
389 ) {
390   INT i;
391 
392   /* Return if scalefactor is Zero */
393   if (scalefactor == 0) {
394     if (dst != src) FDKmemmove(dst, src, len * sizeof(FIXP_DBL));
395   } else {
396     if (scalefactor > 0) {
397       scalefactor = fixmin_I(scalefactor, (INT)DFRACT_BITS - 1);
398       for (i = len & 3; i--;) {
399         *(dst++) = *(src++) << scalefactor;
400       }
401       for (i = len >> 2; i--;) {
402         *(dst++) = *(src++) << scalefactor;
403         *(dst++) = *(src++) << scalefactor;
404         *(dst++) = *(src++) << scalefactor;
405         *(dst++) = *(src++) << scalefactor;
406       }
407     } else {
408       INT negScalefactor = fixmin_I(-scalefactor, (INT)DFRACT_BITS - 1);
409       for (i = len & 3; i--;) {
410         *(dst++) = *(src++) >> negScalefactor;
411       }
412       for (i = len >> 2; i--;) {
413         *(dst++) = *(src++) >> negScalefactor;
414         *(dst++) = *(src++) >> negScalefactor;
415         *(dst++) = *(src++) >> negScalefactor;
416         *(dst++) = *(src++) >> negScalefactor;
417       }
418     }
419   }
420 }
421 #endif
422 
423 #if (SAMPLE_BITS == 16)
424 #ifndef FUNCTION_scaleValues_PCMDBL
425 /*!
426  *
427  *  \brief  Multiply input vector src by \f$ 2^{scalefactor} \f$
428  *          and place result into dst
429  *  \param dst detination buffer
430  *  \param src source buffer
431  *  \param len must be larger than 4
432  *  \param scalefactor amount of left shifts to be applied
433  *  \return void
434  *
435  */
436 #define FUNCTION_scaleValues_PCMDBL
437 SCALE_INLINE
scaleValues(FIXP_PCM * dst,const FIXP_DBL * src,INT len,INT scalefactor)438 void scaleValues(FIXP_PCM *dst,       /*!< dst Vector */
439                  const FIXP_DBL *src, /*!< src Vector */
440                  INT len,             /*!< Length */
441                  INT scalefactor      /*!< Scalefactor */
442 ) {
443   INT i;
444 
445   scalefactor -= DFRACT_BITS - SAMPLE_BITS;
446 
447   /* Return if scalefactor is Zero */
448   {
449     if (scalefactor > 0) {
450       scalefactor = fixmin_I(scalefactor, (INT)DFRACT_BITS - 1);
451       for (i = len & 3; i--;) {
452         *(dst++) = (FIXP_PCM)(*(src++) << scalefactor);
453       }
454       for (i = len >> 2; i--;) {
455         *(dst++) = (FIXP_PCM)(*(src++) << scalefactor);
456         *(dst++) = (FIXP_PCM)(*(src++) << scalefactor);
457         *(dst++) = (FIXP_PCM)(*(src++) << scalefactor);
458         *(dst++) = (FIXP_PCM)(*(src++) << scalefactor);
459       }
460     } else {
461       INT negScalefactor = fixmin_I(-scalefactor, (INT)DFRACT_BITS - 1);
462       for (i = len & 3; i--;) {
463         *(dst++) = (FIXP_PCM)(*(src++) >> negScalefactor);
464       }
465       for (i = len >> 2; i--;) {
466         *(dst++) = (FIXP_PCM)(*(src++) >> negScalefactor);
467         *(dst++) = (FIXP_PCM)(*(src++) >> negScalefactor);
468         *(dst++) = (FIXP_PCM)(*(src++) >> negScalefactor);
469         *(dst++) = (FIXP_PCM)(*(src++) >> negScalefactor);
470       }
471     }
472   }
473 }
474 #endif
475 #endif /* (SAMPLE_BITS == 16) */
476 
477 #ifndef FUNCTION_scaleValues_SGLSGL
478 /*!
479  *
480  *  \brief  Multiply input vector src by \f$ 2^{scalefactor} \f$
481  *          and place result into dst
482  *  \param dst detination buffer
483  *  \param src source buffer
484  *  \param len must be larger than 4
485  *  \param scalefactor amount of left shifts to be applied
486  *  \return void
487  *
488  */
489 #define FUNCTION_scaleValues_SGLSGL
490 SCALE_INLINE
scaleValues(FIXP_SGL * dst,const FIXP_SGL * src,INT len,INT scalefactor)491 void scaleValues(FIXP_SGL *dst,       /*!< dst Vector */
492                  const FIXP_SGL *src, /*!< src Vector */
493                  INT len,             /*!< Length */
494                  INT scalefactor      /*!< Scalefactor */
495 ) {
496   INT i;
497 
498   /* Return if scalefactor is Zero */
499   if (scalefactor == 0) {
500     if (dst != src) FDKmemmove(dst, src, len * sizeof(FIXP_DBL));
501   } else {
502     if (scalefactor > 0) {
503       scalefactor = fixmin_I(scalefactor, (INT)DFRACT_BITS - 1);
504       for (i = len & 3; i--;) {
505         *(dst++) = *(src++) << scalefactor;
506       }
507       for (i = len >> 2; i--;) {
508         *(dst++) = *(src++) << scalefactor;
509         *(dst++) = *(src++) << scalefactor;
510         *(dst++) = *(src++) << scalefactor;
511         *(dst++) = *(src++) << scalefactor;
512       }
513     } else {
514       INT negScalefactor = fixmin_I(-scalefactor, (INT)DFRACT_BITS - 1);
515       for (i = len & 3; i--;) {
516         *(dst++) = *(src++) >> negScalefactor;
517       }
518       for (i = len >> 2; i--;) {
519         *(dst++) = *(src++) >> negScalefactor;
520         *(dst++) = *(src++) >> negScalefactor;
521         *(dst++) = *(src++) >> negScalefactor;
522         *(dst++) = *(src++) >> negScalefactor;
523       }
524     }
525   }
526 }
527 #endif
528 
529 #ifndef FUNCTION_scaleValuesWithFactor_DBL
530 /*!
531  *
532  *  \brief  Multiply input vector by \f$ 2^{scalefactor} \f$
533  *  \param len must be larger than 4
534  *  \return void
535  *
536  */
537 #define FUNCTION_scaleValuesWithFactor_DBL
538 SCALE_INLINE
scaleValuesWithFactor(FIXP_DBL * vector,FIXP_DBL factor,INT len,INT scalefactor)539 void scaleValuesWithFactor(FIXP_DBL *vector, FIXP_DBL factor, INT len,
540                            INT scalefactor) {
541   INT i;
542 
543   /* Compensate fMultDiv2 */
544   scalefactor++;
545 
546   if (scalefactor > 0) {
547     scalefactor = fixmin_I(scalefactor, (INT)DFRACT_BITS - 1);
548     for (i = len & 3; i--;) {
549       *vector = fMultDiv2(*vector, factor) << scalefactor;
550       vector++;
551     }
552     for (i = len >> 2; i--;) {
553       *vector = fMultDiv2(*vector, factor) << scalefactor;
554       vector++;
555       *vector = fMultDiv2(*vector, factor) << scalefactor;
556       vector++;
557       *vector = fMultDiv2(*vector, factor) << scalefactor;
558       vector++;
559       *vector = fMultDiv2(*vector, factor) << scalefactor;
560       vector++;
561     }
562   } else {
563     INT negScalefactor = fixmin_I(-scalefactor, (INT)DFRACT_BITS - 1);
564     for (i = len & 3; i--;) {
565       *vector = fMultDiv2(*vector, factor) >> negScalefactor;
566       vector++;
567     }
568     for (i = len >> 2; i--;) {
569       *vector = fMultDiv2(*vector, factor) >> negScalefactor;
570       vector++;
571       *vector = fMultDiv2(*vector, factor) >> negScalefactor;
572       vector++;
573       *vector = fMultDiv2(*vector, factor) >> negScalefactor;
574       vector++;
575       *vector = fMultDiv2(*vector, factor) >> negScalefactor;
576       vector++;
577     }
578   }
579 }
580 #endif /* FUNCTION_scaleValuesWithFactor_DBL */
581 
582   /*******************************************
583 
584   IMPORTANT NOTE for usage of getScalefactor()
585 
586   If the input array contains negative values too, then these functions may
587   sometimes return the actual maximum value minus 1, due to the nature of the
588   applied algorithm. So be careful with possible fractional -1 values that may
589   lead to overflows when being fPow2()'ed.
590 
591   ********************************************/
592 
593 #ifndef FUNCTION_getScalefactorShort
594 /*!
595  *
596  *  \brief Calculate max possible scale factor for input vector of shorts
597  *
598  *  \return Maximum scale factor / possible left shift
599  *
600  */
601 #define FUNCTION_getScalefactorShort
602 SCALE_INLINE
getScalefactorShort(const SHORT * vector,INT len)603 INT getScalefactorShort(const SHORT *vector, /*!< Pointer to input vector */
604                         INT len              /*!< Length of input vector */
605 ) {
606   INT i;
607   SHORT temp, maxVal = 0;
608 
609   for (i = len; i != 0; i--) {
610     temp = (SHORT)(*vector++);
611     maxVal |= (temp ^ (temp >> (SHORT_BITS - 1)));
612   }
613 
614   return fixmax_I((INT)0, (INT)(fixnormz_D((INT)maxVal) - (INT)1 -
615                                 (INT)(DFRACT_BITS - SHORT_BITS)));
616 }
617 #endif
618 
619 #ifndef FUNCTION_getScalefactorPCM
620 /*!
621  *
622  *  \brief Calculate max possible scale factor for input vector of shorts
623  *
624  *  \return Maximum scale factor
625  *
626  */
627 #define FUNCTION_getScalefactorPCM
628 SCALE_INLINE
getScalefactorPCM(const INT_PCM * vector,INT len,INT stride)629 INT getScalefactorPCM(const INT_PCM *vector, /*!< Pointer to input vector */
630                       INT len,               /*!< Length of input vector */
631                       INT stride) {
632   INT i;
633   INT_PCM temp, maxVal = 0;
634 
635   for (i = len; i != 0; i--) {
636     temp = (INT_PCM)(*vector);
637     vector += stride;
638     maxVal |= (temp ^ (temp >> ((sizeof(INT_PCM) * 8) - 1)));
639   }
640   return fixmax_I((INT)0, (INT)(fixnormz_D((INT)maxVal) - (INT)1 -
641                                 (INT)(DFRACT_BITS - SAMPLE_BITS)));
642 }
643 #endif
644 
645 #ifndef FUNCTION_getScalefactorShort
646 /*!
647  *
648  *  \brief Calculate max possible scale factor for input vector of shorts
649  *  \param stride, item increment between vector members.
650  *  \return Maximum scale factor
651  *
652  */
653 #define FUNCTION_getScalefactorShort
654 SCALE_INLINE
getScalefactorShort(const SHORT * vector,INT len,INT stride)655 INT getScalefactorShort(const SHORT *vector, /*!< Pointer to input vector */
656                         INT len,             /*!< Length of input vector */
657                         INT stride) {
658   INT i;
659   SHORT temp, maxVal = 0;
660 
661   for (i = len; i != 0; i--) {
662     temp = (SHORT)(*vector);
663     vector += stride;
664     maxVal |= (temp ^ (temp >> (SHORT_BITS - 1)));
665   }
666 
667   return fixmax_I((INT)0, (INT)(fixnormz_D((INT)maxVal) - (INT)1 -
668                                 (INT)(DFRACT_BITS - SHORT_BITS)));
669 }
670 #endif
671 
672 #ifndef FUNCTION_getScalefactor_DBL
673 /*!
674  *
675  *  \brief Calculate max possible scale factor for input vector
676  *
677  *  \return Maximum scale factor
678  *
679  *  This function can constitute a significant amount of computational
680  * complexity - very much depending on the bitrate. Since it is a rather small
681  * function, effective assembler optimization might be possible.
682  *
683  *  If all data is 0xFFFF.FFFF or 0x0000.0000 function returns 31
684  *  Note: You can skip data normalization only if return value is 0
685  *
686  */
687 #define FUNCTION_getScalefactor_DBL
688 SCALE_INLINE
getScalefactor(const FIXP_DBL * vector,INT len)689 INT getScalefactor(const FIXP_DBL *vector, /*!< Pointer to input vector */
690                    INT len)                /*!< Length of input vector */
691 {
692   INT i;
693   FIXP_DBL temp, maxVal = (FIXP_DBL)0;
694 
695   for (i = len; i != 0; i--) {
696     temp = (LONG)(*vector++);
697     maxVal |= (FIXP_DBL)((LONG)temp ^ (LONG)(temp >> (DFRACT_BITS - 1)));
698   }
699 
700   return fixmax_I((INT)0, (INT)(fixnormz_D(maxVal) - 1));
701 }
702 #endif
703 
704 #ifndef FUNCTION_getScalefactor_SGL
705 #define FUNCTION_getScalefactor_SGL
706 SCALE_INLINE
getScalefactor(const FIXP_SGL * vector,INT len)707 INT getScalefactor(const FIXP_SGL *vector, /*!< Pointer to input vector */
708                    INT len)                /*!< Length of input vector */
709 {
710   INT i;
711   SHORT temp, maxVal = (FIXP_SGL)0;
712 
713   for (i = len; i != 0; i--) {
714     temp = (SHORT)(*vector++);
715     maxVal |= (temp ^ (temp >> (FRACT_BITS - 1)));
716   }
717 
718   return fixmax_I((INT)0, (INT)(fixnormz_S((FIXP_SGL)maxVal)) - 1);
719 }
720 #endif
721