1 2 /* ----------------------------------------------------------------------------------------------------------- 3 Software License for The Fraunhofer FDK AAC Codec Library for Android 4 5 Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Frderung 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 (at) iis.fraunhofer.de 82 ----------------------------------------------------------------------------------------------------------- */ 83 84 /***************************** MPEG-4 AAC Encoder ************************** 85 86 Initial author: M. Werner 87 contents/description: Band/Line energy calculations 88 89 ******************************************************************************/ 90 91 #include "band_nrg.h" 92 93 94 /***************************************************************************** 95 functionname: FDKaacEnc_CalcSfbMaxScaleSpec 96 description: 97 input: 98 output: 99 *****************************************************************************/ 100 void 101 FDKaacEnc_CalcSfbMaxScaleSpec(const FIXP_DBL *RESTRICT mdctSpectrum, 102 const INT *RESTRICT bandOffset, 103 INT *RESTRICT sfbMaxScaleSpec, 104 const INT numBands) 105 { 106 INT i,j; 107 FIXP_DBL maxSpc, tmp; 108 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); 114 } 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 */ 117 } 118 } 119 120 /***************************************************************************** 121 functionname: FDKaacEnc_CheckBandEnergyOptim 122 description: 123 input: 124 output: 125 *****************************************************************************/ 126 FIXP_DBL 127 FDKaacEnc_CheckBandEnergyOptim(const FIXP_DBL *RESTRICT mdctSpectrum, 128 INT *RESTRICT sfbMaxScaleSpec, 129 const INT *RESTRICT bandOffset, 130 const INT numBands, 131 FIXP_DBL *RESTRICT bandEnergy, 132 FIXP_DBL *RESTRICT bandEnergyLdData, 133 INT minSpecShift) 134 { 135 INT i, j, scale, nr = 0; 136 FIXP_DBL maxNrgLd = FL2FXCONST_DBL(-1.0f); 137 FIXP_DBL maxNrg = 0; 138 FIXP_DBL spec; 139 140 for(i=0; i<numBands; i++) { 141 scale = fixMax(0, sfbMaxScaleSpec[i]-4); 142 FIXP_DBL tmp = 0; 143 for (j=bandOffset[i]; j<bandOffset[i+1]; j++){ 144 spec = mdctSpectrum[j]<<scale; 145 tmp = fPow2AddDiv2(tmp, spec); 146 } 147 bandEnergy[i] = tmp<<1; 148 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); 153 } 154 /* find index of maxNrg */ 155 if (bandEnergyLdData[i] > maxNrgLd) { 156 maxNrgLd = bandEnergyLdData[i]; 157 nr = i; 158 } 159 } 160 161 /* return unscaled maxNrg*/ 162 scale = fixMax(0,sfbMaxScaleSpec[nr]-4); 163 scale = fixMax(2*(minSpecShift-scale),-(DFRACT_BITS-1)); 164 165 maxNrg = scaleValue(bandEnergy[nr], scale); 166 167 return maxNrg; 168 } 169 170 /***************************************************************************** 171 functionname: FDKaacEnc_CalcBandEnergyOptimLong 172 description: 173 input: 174 output: 175 *****************************************************************************/ 176 INT 177 FDKaacEnc_CalcBandEnergyOptimLong(const FIXP_DBL *RESTRICT mdctSpectrum, 178 INT *RESTRICT sfbMaxScaleSpec, 179 const INT *RESTRICT bandOffset, 180 const INT numBands, 181 FIXP_DBL *RESTRICT bandEnergy, 182 FIXP_DBL *RESTRICT bandEnergyLdData) 183 { 184 INT i, j, shiftBits = 0; 185 FIXP_DBL maxNrgLd = FL2FXCONST_DBL(0.0f); 186 187 FIXP_DBL spec; 188 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); 197 } 198 } else { 199 INT shift = -leadingBits; 200 for (j=bandOffset[i];j<bandOffset[i+1];j++){ 201 spec = mdctSpectrum[j]>>shift; 202 tmp = fPow2AddDiv2(tmp, spec); 203 } 204 } 205 bandEnergy[i] = tmp<<1; 206 } 207 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); 212 213 bandEnergyLdData[i] = (bandEnergyLdData[i] >= ((FL2FXCONST_DBL(-1.f)>>1) + (scaleDiff>>1))) 214 ? bandEnergyLdData[i]-scaleDiff : FL2FXCONST_DBL(-1.f); 215 /* find maxNrgLd */ 216 maxNrgLd = fixMax(maxNrgLd, bandEnergyLdData[i]); 217 } 218 219 if (maxNrgLd<=(FIXP_DBL)0) 220 { 221 for(i=numBands; i--!=0; ) 222 { 223 INT scale = fixMin((sfbMaxScaleSpec[i]-4)<<1,(DFRACT_BITS-1)); 224 bandEnergy[i] = scaleValue(bandEnergy[i], -scale); 225 } 226 return 0; 227 } 228 else 229 { /* scale down NRGs */ 230 while (maxNrgLd>FL2FXCONST_DBL(0.0f)) 231 { 232 maxNrgLd -= FL2FXCONST_DBL(2.0/64); 233 shiftBits++; 234 } 235 for(i=numBands; i--!=0; ) 236 { 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); 240 } 241 return shiftBits; 242 } 243 } 244 245 246 /***************************************************************************** 247 functionname: FDKaacEnc_CalcBandEnergyOptimShort 248 description: 249 input: 250 output: 251 *****************************************************************************/ 252 void 253 FDKaacEnc_CalcBandEnergyOptimShort(const FIXP_DBL *RESTRICT mdctSpectrum, 254 INT *RESTRICT sfbMaxScaleSpec, 255 const INT *RESTRICT bandOffset, 256 const INT numBands, 257 FIXP_DBL *RESTRICT bandEnergy) 258 { 259 INT i, j; 260 261 for(i=0; i<numBands; i++) 262 { 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++) 266 { 267 FIXP_DBL spec = scaleValue(mdctSpectrum[j],leadingBits); 268 tmp = fPow2AddDiv2(tmp, spec); 269 } 270 bandEnergy[i] = tmp; 271 } 272 273 for(i=0; i<numBands; i++) 274 { 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); 278 } 279 } 280 281 282 /***************************************************************************** 283 functionname: FDKaacEnc_CalcBandNrgMSOpt 284 description: 285 input: 286 output: 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, 293 const INT numBands, 294 FIXP_DBL *RESTRICT bandEnergyMid, 295 FIXP_DBL *RESTRICT bandEnergySide, 296 INT calcLdData, 297 FIXP_DBL *RESTRICT bandEnergyMidLdData, 298 FIXP_DBL *RESTRICT bandEnergySideLdData) 299 { 300 INT i, j, minScale; 301 FIXP_DBL NrgMid, NrgSide, specm, specs; 302 303 for (i=0; i<numBands; i++) { 304 305 NrgMid = NrgSide = FL2FXCONST_DBL(0.0); 306 minScale = fixMin(sfbMaxScaleSpecLeft[i], sfbMaxScaleSpecRight[i])-4; 307 minScale = fixMax(0, minScale); 308 309 if (minScale > 0) { 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); 317 } 318 } else { 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); 326 } 327 } 328 bandEnergyMid[i] = NrgMid<<1; 329 bandEnergySide[i] = NrgSide<<1; 330 } 331 332 if(calcLdData) { 333 LdDataVector(bandEnergyMid, bandEnergyMidLdData, numBands); 334 LdDataVector(bandEnergySide, bandEnergySideLdData, numBands); 335 } 336 337 for (i=0; i<numBands; i++) 338 { 339 INT minScale = fixMin(sfbMaxScaleSpecLeft[i], sfbMaxScaleSpecRight[i]); 340 INT scale = fixMax(0, 2*(minScale-4)); 341 342 if (calcLdData) 343 { 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 */ 346 347 int minus = scale*FL2FXCONST_DBL(1.0/64); 348 349 if (bandEnergyMidLdData[i] != FL2FXCONST_DBL(-1.0f)) 350 bandEnergyMidLdData[i] -= minus; 351 352 if (bandEnergySideLdData[i] != FL2FXCONST_DBL(-1.0f)) 353 bandEnergySideLdData[i] -= minus; 354 } 355 scale = fixMin(scale, (DFRACT_BITS-1)); 356 bandEnergyMid[i] >>= scale; 357 bandEnergySide[i] >>= scale; 358 } 359 } 360