Imported Debian version 0.1.3.1
[deb_fdk-aac.git] / libFDK / src / scale.cpp
1
2 /* -----------------------------------------------------------------------------------------------------------
3 Software 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
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.
12
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.
17
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.
24
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.
28
29 2. COPYRIGHT LICENSE
30
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:
33
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.
36
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.
41
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44
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.
47
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."
52
53 3. NO PATENT LICENSE
54
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.
58
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61
62 4. DISCLAIMER
63
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.
72
73 5. CONTACT INFORMATION
74
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79
80 www.iis.fraunhofer.de/amm
81 amm-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
119 SCALE_INLINE
120 void 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
169 SCALE_INLINE
170 void 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
223 SCALE_INLINE
224 void 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
279 SCALE_INLINE
280 void 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
327 IMPORTANT NOTE for usage of getScalefactor()
328
329 If the input array contains negative values too, then these functions may sometimes return
330 the actual maximum value minus 1, due to the nature of the applied algorithm.
331 So 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
346 SCALE_INLINE
347 INT 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
372 SCALE_INLINE
373 INT 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
398 SCALE_INLINE
399 INT 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
428 SCALE_INLINE
429 INT 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
446 SCALE_INLINE
447 INT 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