2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
5 © Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
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.
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.
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.
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.
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:
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.
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.
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
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.
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."
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.
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
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.
73 5. CONTACT INFORMATION
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
78 91058 Erlangen, Germany
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
84 /***************************** MPEG-4 AAC Encoder **************************
86 Initial author: M. Werner
87 contents/description: Band/Line energy calculations
89 ******************************************************************************/
94 /*****************************************************************************
95 functionname: FDKaacEnc_CalcSfbMaxScaleSpec
99 *****************************************************************************/
101 FDKaacEnc_CalcSfbMaxScaleSpec(const FIXP_DBL
*RESTRICT mdctSpectrum
,
102 const INT
*RESTRICT bandOffset
,
103 INT
*RESTRICT sfbMaxScaleSpec
,
107 FIXP_DBL maxSpc
, tmp
;
109 for(i
=0; i
<numBands
; i
++) {
110 maxSpc
= (FIXP_DBL
)0;
111 for (j
=bandOffset
[i
]; j
<bandOffset
[i
+1]; j
++) {
112 tmp
= fixp_abs(mdctSpectrum
[j
]);
113 maxSpc
= fixMax(maxSpc
, tmp
);
115 sfbMaxScaleSpec
[i
] = (maxSpc
==(FIXP_DBL
)0) ? (DFRACT_BITS
-2) : CntLeadingZeros(maxSpc
)-1;
116 /* CountLeadingBits() is not necessary here since test value is always > 0 */
120 /*****************************************************************************
121 functionname: FDKaacEnc_CheckBandEnergyOptim
125 *****************************************************************************/
127 FDKaacEnc_CheckBandEnergyOptim(const FIXP_DBL
*RESTRICT mdctSpectrum
,
128 INT
*RESTRICT sfbMaxScaleSpec
,
129 const INT
*RESTRICT bandOffset
,
131 FIXP_DBL
*RESTRICT bandEnergy
,
132 FIXP_DBL
*RESTRICT bandEnergyLdData
,
135 INT i
, j
, scale
, nr
= 0;
136 FIXP_DBL maxNrgLd
= FL2FXCONST_DBL(-1.0f
);
140 for(i
=0; i
<numBands
; i
++) {
141 scale
= fixMax(0, sfbMaxScaleSpec
[i
]-4);
143 for (j
=bandOffset
[i
]; j
<bandOffset
[i
+1]; j
++){
144 spec
= mdctSpectrum
[j
]<<scale
;
145 tmp
= fPow2AddDiv2(tmp
, spec
);
147 bandEnergy
[i
] = tmp
<<1;
149 /* calculate ld of bandNrg, subtract scaling */
150 bandEnergyLdData
[i
] = CalcLdData(bandEnergy
[i
]);
151 if (bandEnergyLdData
[i
] != FL2FXCONST_DBL(-1.0f
)) {
152 bandEnergyLdData
[i
] -= scale
*FL2FXCONST_DBL(2.0/64);
154 /* find index of maxNrg */
155 if (bandEnergyLdData
[i
] > maxNrgLd
) {
156 maxNrgLd
= bandEnergyLdData
[i
];
161 /* return unscaled maxNrg*/
162 scale
= fixMax(0,sfbMaxScaleSpec
[nr
]-4);
163 scale
= fixMax(2*(minSpecShift
-scale
),-(DFRACT_BITS
-1));
165 maxNrg
= scaleValue(bandEnergy
[nr
], scale
);
170 /*****************************************************************************
171 functionname: FDKaacEnc_CalcBandEnergyOptimLong
175 *****************************************************************************/
177 FDKaacEnc_CalcBandEnergyOptimLong(const FIXP_DBL
*RESTRICT mdctSpectrum
,
178 INT
*RESTRICT sfbMaxScaleSpec
,
179 const INT
*RESTRICT bandOffset
,
181 FIXP_DBL
*RESTRICT bandEnergy
,
182 FIXP_DBL
*RESTRICT bandEnergyLdData
)
184 INT i
, j
, shiftBits
= 0;
185 FIXP_DBL maxNrgLd
= FL2FXCONST_DBL(0.0f
);
189 for(i
=0; i
<numBands
; i
++) {
190 INT leadingBits
= sfbMaxScaleSpec
[i
]-4; /* max sfbWidth = 96 ; 2^7=128 => 7/2 = 4 (spc*spc) */
191 FIXP_DBL tmp
= FL2FXCONST_DBL(0.0);
192 /* don't use scaleValue() here, it increases workload quite sufficiently... */
193 if (leadingBits
>=0) {
194 for (j
=bandOffset
[i
];j
<bandOffset
[i
+1];j
++) {
195 spec
= mdctSpectrum
[j
]<<leadingBits
;
196 tmp
= fPow2AddDiv2(tmp
, spec
);
199 INT shift
= -leadingBits
;
200 for (j
=bandOffset
[i
];j
<bandOffset
[i
+1];j
++){
201 spec
= mdctSpectrum
[j
]>>shift
;
202 tmp
= fPow2AddDiv2(tmp
, spec
);
205 bandEnergy
[i
] = tmp
<<1;
208 /* calculate ld of bandNrg, subtract scaling */
209 LdDataVector(bandEnergy
, bandEnergyLdData
, numBands
);
210 for(i
=numBands
; i
--!=0; ) {
211 FIXP_DBL scaleDiff
= (sfbMaxScaleSpec
[i
]-4)*FL2FXCONST_DBL(2.0/64);
213 bandEnergyLdData
[i
] = (bandEnergyLdData
[i
] >= ((FL2FXCONST_DBL(-1.f
)>>1) + (scaleDiff
>>1)))
214 ? bandEnergyLdData
[i
]-scaleDiff
: FL2FXCONST_DBL(-1.f
);
216 maxNrgLd
= fixMax(maxNrgLd
, bandEnergyLdData
[i
]);
219 if (maxNrgLd
<=(FIXP_DBL
)0)
221 for(i
=numBands
; i
--!=0; )
223 INT scale
= fixMin((sfbMaxScaleSpec
[i
]-4)<<1,(DFRACT_BITS
-1));
224 bandEnergy
[i
] = scaleValue(bandEnergy
[i
], -scale
);
229 { /* scale down NRGs */
230 while (maxNrgLd
>FL2FXCONST_DBL(0.0f
))
232 maxNrgLd
-= FL2FXCONST_DBL(2.0/64);
235 for(i
=numBands
; i
--!=0; )
237 INT scale
= fixMin( ((sfbMaxScaleSpec
[i
]-4)+shiftBits
)<<1, (DFRACT_BITS
-1));
238 bandEnergyLdData
[i
] -= shiftBits
*FL2FXCONST_DBL(2.0/64);
239 bandEnergy
[i
] = scaleValue(bandEnergy
[i
], -scale
);
246 /*****************************************************************************
247 functionname: FDKaacEnc_CalcBandEnergyOptimShort
251 *****************************************************************************/
253 FDKaacEnc_CalcBandEnergyOptimShort(const FIXP_DBL
*RESTRICT mdctSpectrum
,
254 INT
*RESTRICT sfbMaxScaleSpec
,
255 const INT
*RESTRICT bandOffset
,
257 FIXP_DBL
*RESTRICT bandEnergy
)
261 for(i
=0; i
<numBands
; i
++)
263 int leadingBits
= sfbMaxScaleSpec
[i
]-3; /* max sfbWidth = 36 ; 2^6=64 => 6/2 = 3 (spc*spc) */
264 FIXP_DBL tmp
= FL2FXCONST_DBL(0.0);
265 for (j
=bandOffset
[i
];j
<bandOffset
[i
+1];j
++)
267 FIXP_DBL spec
= scaleValue(mdctSpectrum
[j
],leadingBits
);
268 tmp
= fPow2AddDiv2(tmp
, spec
);
273 for(i
=0; i
<numBands
; i
++)
275 INT scale
= (2*(sfbMaxScaleSpec
[i
]-3))-1; /* max sfbWidth = 36 ; 2^6=64 => 6/2 = 3 (spc*spc) */
276 scale
= fixMax(fixMin(scale
,(DFRACT_BITS
-1)),-(DFRACT_BITS
-1));
277 bandEnergy
[i
] = scaleValueSaturate(bandEnergy
[i
], -scale
);
282 /*****************************************************************************
283 functionname: FDKaacEnc_CalcBandNrgMSOpt
287 *****************************************************************************/
288 void FDKaacEnc_CalcBandNrgMSOpt(const FIXP_DBL
*RESTRICT mdctSpectrumLeft
,
289 const FIXP_DBL
*RESTRICT mdctSpectrumRight
,
290 INT
*RESTRICT sfbMaxScaleSpecLeft
,
291 INT
*RESTRICT sfbMaxScaleSpecRight
,
292 const INT
*RESTRICT bandOffset
,
294 FIXP_DBL
*RESTRICT bandEnergyMid
,
295 FIXP_DBL
*RESTRICT bandEnergySide
,
297 FIXP_DBL
*RESTRICT bandEnergyMidLdData
,
298 FIXP_DBL
*RESTRICT bandEnergySideLdData
)
301 FIXP_DBL NrgMid
, NrgSide
, specm
, specs
;
303 for (i
=0; i
<numBands
; i
++) {
305 NrgMid
= NrgSide
= FL2FXCONST_DBL(0.0);
306 minScale
= fixMin(sfbMaxScaleSpecLeft
[i
], sfbMaxScaleSpecRight
[i
])-4;
307 minScale
= fixMax(0, minScale
);
310 for (j
=bandOffset
[i
];j
<bandOffset
[i
+1];j
++) {
311 FIXP_DBL specL
= mdctSpectrumLeft
[j
]<<(minScale
-1);
312 FIXP_DBL specR
= mdctSpectrumRight
[j
]<<(minScale
-1);
313 specm
= specL
+ specR
;
314 specs
= specL
- specR
;
315 NrgMid
= fPow2AddDiv2(NrgMid
, specm
);
316 NrgSide
= fPow2AddDiv2(NrgSide
, specs
);
319 for (j
=bandOffset
[i
];j
<bandOffset
[i
+1];j
++) {
320 FIXP_DBL specL
= mdctSpectrumLeft
[j
]>>1;
321 FIXP_DBL specR
= mdctSpectrumRight
[j
]>>1;
322 specm
= specL
+ specR
;
323 specs
= specL
- specR
;
324 NrgMid
= fPow2AddDiv2(NrgMid
, specm
);
325 NrgSide
= fPow2AddDiv2(NrgSide
, specs
);
328 bandEnergyMid
[i
] = NrgMid
<<1;
329 bandEnergySide
[i
] = NrgSide
<<1;
333 LdDataVector(bandEnergyMid
, bandEnergyMidLdData
, numBands
);
334 LdDataVector(bandEnergySide
, bandEnergySideLdData
, numBands
);
337 for (i
=0; i
<numBands
; i
++)
339 INT minScale
= fixMin(sfbMaxScaleSpecLeft
[i
], sfbMaxScaleSpecRight
[i
]);
340 INT scale
= fixMax(0, 2*(minScale
-4));
344 /* using the minimal scaling of left and right channel can cause very small energies;
345 check ldNrg before subtract scaling multiplication: fract*INT we don't need fMult */
347 int minus
= scale
*FL2FXCONST_DBL(1.0/64);
349 if (bandEnergyMidLdData
[i
] != FL2FXCONST_DBL(-1.0f
))
350 bandEnergyMidLdData
[i
] -= minus
;
352 if (bandEnergySideLdData
[i
] != FL2FXCONST_DBL(-1.0f
))
353 bandEnergySideLdData
[i
] -= minus
;
355 scale
= fixMin(scale
, (DFRACT_BITS
-1));
356 bandEnergyMid
[i
] >>= scale
;
357 bandEnergySide
[i
] >>= scale
;