1 /* ----------------------------------------------------------------------------- 2 Software License for The Fraunhofer FDK AAC Codec Library for Android 3 4 Copyright 1995 - 2018 Fraunhofer-Gesellschaft zur Frderung 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 (at) iis.fraunhofer.de 93 ----------------------------------------------------------------------------- */ 94 95 /**************************** SBR decoder library ****************************** 96 97 Author(s): Christian Griebel 98 99 Description: Dynamic range control (DRC) decoder tool for SBR 100 101 *******************************************************************************/ 102 103 #include "sbrdec_drc.h" 104 105 /* DRC - Offset table for QMF interpolation. Shifted by one index position. 106 The table defines the (short) window borders rounded to the nearest QMF 107 timeslot. It has the size 16 because it is accessed with the 108 drcInterpolationScheme that is read from the bitstream with 4 bit. */ 109 static const UCHAR winBorderToColMappingTab[2][16] = { 110 /*-1, 0, 1, 2, 3, 4, 5, 6, 7, 8 */ 111 {0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 32, 32, 32, 32, 32, 112 32}, /* 1024 framing */ 113 {0, 0, 4, 8, 11, 15, 19, 23, 26, 30, 30, 30, 30, 30, 30, 114 30} /* 960 framing */ 115 }; 116 117 /*! 118 \brief Initialize DRC QMF factors 119 120 \hDrcData Handle to DRC channel data. 121 122 \return none 123 */ 124 void sbrDecoder_drcInitChannel(HANDLE_SBR_DRC_CHANNEL hDrcData) { 125 int band; 126 127 if (hDrcData == NULL) { 128 return; 129 } 130 131 for (band = 0; band < (64); band++) { 132 hDrcData->prevFact_mag[band] = FL2FXCONST_DBL(0.5f); 133 } 134 135 for (band = 0; band < SBRDEC_MAX_DRC_BANDS; band++) { 136 hDrcData->currFact_mag[band] = FL2FXCONST_DBL(0.5f); 137 hDrcData->nextFact_mag[band] = FL2FXCONST_DBL(0.5f); 138 } 139 140 hDrcData->prevFact_exp = 1; 141 hDrcData->currFact_exp = 1; 142 hDrcData->nextFact_exp = 1; 143 144 hDrcData->numBandsCurr = 1; 145 hDrcData->numBandsNext = 1; 146 147 hDrcData->winSequenceCurr = 0; 148 hDrcData->winSequenceNext = 0; 149 150 hDrcData->drcInterpolationSchemeCurr = 0; 151 hDrcData->drcInterpolationSchemeNext = 0; 152 153 hDrcData->enable = 0; 154 } 155 156 /*! 157 \brief Swap DRC QMF scaling factors after they have been applied. 158 159 \hDrcData Handle to DRC channel data. 160 161 \return none 162 */ 163 void sbrDecoder_drcUpdateChannel(HANDLE_SBR_DRC_CHANNEL hDrcData) { 164 if (hDrcData == NULL) { 165 return; 166 } 167 if (hDrcData->enable != 1) { 168 return; 169 } 170 171 /* swap previous data */ 172 FDKmemcpy(hDrcData->currFact_mag, hDrcData->nextFact_mag, 173 SBRDEC_MAX_DRC_BANDS * sizeof(FIXP_DBL)); 174 175 hDrcData->currFact_exp = hDrcData->nextFact_exp; 176 177 hDrcData->numBandsCurr = hDrcData->numBandsNext; 178 179 FDKmemcpy(hDrcData->bandTopCurr, hDrcData->bandTopNext, 180 SBRDEC_MAX_DRC_BANDS * sizeof(USHORT)); 181 182 hDrcData->drcInterpolationSchemeCurr = hDrcData->drcInterpolationSchemeNext; 183 184 hDrcData->winSequenceCurr = hDrcData->winSequenceNext; 185 } 186 187 /*! 188 \brief Apply DRC factors slot based. 189 190 \hDrcData Handle to DRC channel data. 191 \qmfRealSlot Pointer to real valued QMF data of one time slot. 192 \qmfImagSlot Pointer to the imaginary QMF data of one time slot. 193 \col Number of the time slot. 194 \numQmfSubSamples Total number of time slots for one frame. 195 \scaleFactor Pointer to the out scale factor of the time slot. 196 197 \return None. 198 */ 199 void sbrDecoder_drcApplySlot(HANDLE_SBR_DRC_CHANNEL hDrcData, 200 FIXP_DBL *qmfRealSlot, FIXP_DBL *qmfImagSlot, 201 int col, int numQmfSubSamples, int maxShift) { 202 const UCHAR *winBorderToColMap; 203 204 int band, bottomMdct, topMdct, bin, useLP; 205 int indx = numQmfSubSamples - (numQmfSubSamples >> 1) - 10; /* l_border */ 206 int frameLenFlag = (numQmfSubSamples == 30) ? 1 : 0; 207 int frameSize = (frameLenFlag == 1) ? 960 : 1024; 208 209 const FIXP_DBL *fact_mag = NULL; 210 INT fact_exp = 0; 211 UINT numBands = 0; 212 USHORT *bandTop = NULL; 213 int shortDrc = 0; 214 215 FIXP_DBL alphaValue = FL2FXCONST_DBL(0.0f); 216 217 if (hDrcData == NULL) { 218 return; 219 } 220 if (hDrcData->enable != 1) { 221 return; 222 } 223 224 winBorderToColMap = winBorderToColMappingTab[frameLenFlag]; 225 226 useLP = (qmfImagSlot == NULL) ? 1 : 0; 227 228 col += indx; 229 bottomMdct = 0; 230 231 /* get respective data and calc interpolation factor */ 232 if (col < (numQmfSubSamples >> 1)) { /* first half of current frame */ 233 if (hDrcData->winSequenceCurr != 2) { /* long window */ 234 int j = col + (numQmfSubSamples >> 1); 235 236 if (hDrcData->drcInterpolationSchemeCurr == 0) { 237 INT k = (frameLenFlag) ? 0x4444445 : 0x4000000; 238 239 alphaValue = (FIXP_DBL)(j * k); 240 } else { 241 if (j >= (int)winBorderToColMap[hDrcData->drcInterpolationSchemeCurr]) { 242 alphaValue = (FIXP_DBL)MAXVAL_DBL; 243 } 244 } 245 } else { /* short windows */ 246 shortDrc = 1; 247 } 248 249 fact_mag = hDrcData->currFact_mag; 250 fact_exp = hDrcData->currFact_exp; 251 numBands = hDrcData->numBandsCurr; 252 bandTop = hDrcData->bandTopCurr; 253 } else if (col < numQmfSubSamples) { /* second half of current frame */ 254 if (hDrcData->winSequenceNext != 2) { /* next: long window */ 255 int j = col - (numQmfSubSamples >> 1); 256 257 if (hDrcData->drcInterpolationSchemeNext == 0) { 258 INT k = (frameLenFlag) ? 0x4444445 : 0x4000000; 259 260 alphaValue = (FIXP_DBL)(j * k); 261 } else { 262 if (j >= (int)winBorderToColMap[hDrcData->drcInterpolationSchemeNext]) { 263 alphaValue = (FIXP_DBL)MAXVAL_DBL; 264 } 265 } 266 267 fact_mag = hDrcData->nextFact_mag; 268 fact_exp = hDrcData->nextFact_exp; 269 numBands = hDrcData->numBandsNext; 270 bandTop = hDrcData->bandTopNext; 271 } else { /* next: short windows */ 272 if (hDrcData->winSequenceCurr != 2) { /* current: long window */ 273 alphaValue = (FIXP_DBL)0; 274 275 fact_mag = hDrcData->nextFact_mag; 276 fact_exp = hDrcData->nextFact_exp; 277 numBands = hDrcData->numBandsNext; 278 bandTop = hDrcData->bandTopNext; 279 } else { /* current: short windows */ 280 shortDrc = 1; 281 282 fact_mag = hDrcData->currFact_mag; 283 fact_exp = hDrcData->currFact_exp; 284 numBands = hDrcData->numBandsCurr; 285 bandTop = hDrcData->bandTopCurr; 286 } 287 } 288 } else { /* first half of next frame */ 289 if (hDrcData->winSequenceNext != 2) { /* long window */ 290 int j = col - (numQmfSubSamples >> 1); 291 292 if (hDrcData->drcInterpolationSchemeNext == 0) { 293 INT k = (frameLenFlag) ? 0x4444445 : 0x4000000; 294 295 alphaValue = (FIXP_DBL)(j * k); 296 } else { 297 if (j >= (int)winBorderToColMap[hDrcData->drcInterpolationSchemeNext]) { 298 alphaValue = (FIXP_DBL)MAXVAL_DBL; 299 } 300 } 301 } else { /* short windows */ 302 shortDrc = 1; 303 } 304 305 fact_mag = hDrcData->nextFact_mag; 306 fact_exp = hDrcData->nextFact_exp; 307 numBands = hDrcData->numBandsNext; 308 bandTop = hDrcData->bandTopNext; 309 310 col -= numQmfSubSamples; 311 } 312 313 /* process bands */ 314 for (band = 0; band < (int)numBands; band++) { 315 int bottomQmf, topQmf; 316 317 FIXP_DBL drcFact_mag = (FIXP_DBL)MAXVAL_DBL; 318 319 topMdct = (bandTop[band] + 1) << 2; 320 321 if (!shortDrc) { /* long window */ 322 if (frameLenFlag) { 323 /* 960 framing */ 324 bottomQmf = fMultIfloor((FIXP_DBL)0x4444445, bottomMdct); 325 topQmf = fMultIfloor((FIXP_DBL)0x4444445, topMdct); 326 327 topMdct = 30 * topQmf; 328 } else { 329 /* 1024 framing */ 330 topMdct &= ~0x1f; 331 332 bottomQmf = bottomMdct >> 5; 333 topQmf = topMdct >> 5; 334 } 335 336 if (band == ((int)numBands - 1)) { 337 topQmf = (64); 338 } 339 340 for (bin = bottomQmf; bin < topQmf; bin++) { 341 FIXP_DBL drcFact1_mag = hDrcData->prevFact_mag[bin]; 342 FIXP_DBL drcFact2_mag = fact_mag[band]; 343 344 /* normalize scale factors */ 345 if (hDrcData->prevFact_exp < maxShift) { 346 drcFact1_mag >>= maxShift - hDrcData->prevFact_exp; 347 } 348 if (fact_exp < maxShift) { 349 drcFact2_mag >>= maxShift - fact_exp; 350 } 351 352 /* interpolate */ 353 if (alphaValue == (FIXP_DBL)0) { 354 drcFact_mag = drcFact1_mag; 355 } else if (alphaValue == (FIXP_DBL)MAXVAL_DBL) { 356 drcFact_mag = drcFact2_mag; 357 } else { 358 drcFact_mag = 359 fMult(alphaValue, drcFact2_mag) + 360 fMult(((FIXP_DBL)MAXVAL_DBL - alphaValue), drcFact1_mag); 361 } 362 363 /* apply scaling */ 364 qmfRealSlot[bin] = fMult(qmfRealSlot[bin], drcFact_mag); 365 if (!useLP) { 366 qmfImagSlot[bin] = fMult(qmfImagSlot[bin], drcFact_mag); 367 } 368 369 /* save previous factors */ 370 if (col == (numQmfSubSamples >> 1) - 1) { 371 hDrcData->prevFact_mag[bin] = fact_mag[band]; 372 } 373 } 374 } else { /* short windows */ 375 unsigned startWinIdx, stopWinIdx; 376 int startCol, stopCol; 377 FIXP_DBL invFrameSizeDiv8 = 378 (frameLenFlag) ? (FIXP_DBL)0x1111112 : (FIXP_DBL)0x1000000; 379 380 /* limit top at the frame borders */ 381 if (topMdct < 0) { 382 topMdct = 0; 383 } 384 if (topMdct >= frameSize) { 385 topMdct = frameSize - 1; 386 } 387 388 if (frameLenFlag) { 389 /* 960 framing */ 390 topMdct = fMultIfloor((FIXP_DBL)0x78000000, 391 fMultIfloor((FIXP_DBL)0x22222223, topMdct) << 2); 392 393 startWinIdx = fMultIfloor(invFrameSizeDiv8, bottomMdct) + 394 1; /* winBorderToColMap table has offset of 1 */ 395 stopWinIdx = fMultIceil(invFrameSizeDiv8 - (FIXP_DBL)1, topMdct) + 1; 396 } else { 397 /* 1024 framing */ 398 topMdct &= ~0x03; 399 400 startWinIdx = fMultIfloor(invFrameSizeDiv8, bottomMdct) + 1; 401 stopWinIdx = fMultIceil(invFrameSizeDiv8, topMdct) + 1; 402 } 403 404 /* startCol is truncated to the nearest corresponding start subsample in 405 the QMF of the short window bottom is present in:*/ 406 startCol = (int)winBorderToColMap[startWinIdx]; 407 408 /* stopCol is rounded upwards to the nearest corresponding stop subsample 409 in the QMF of the short window top is present in. */ 410 stopCol = (int)winBorderToColMap[stopWinIdx]; 411 412 bottomQmf = fMultIfloor(invFrameSizeDiv8, 413 ((bottomMdct % (numQmfSubSamples << 2)) << 5)); 414 topQmf = fMultIfloor(invFrameSizeDiv8, 415 ((topMdct % (numQmfSubSamples << 2)) << 5)); 416 417 /* extend last band */ 418 if (band == ((int)numBands - 1)) { 419 topQmf = (64); 420 stopCol = numQmfSubSamples; 421 stopWinIdx = 10; 422 } 423 424 if (topQmf == 0) { 425 if (frameLenFlag) { 426 FIXP_DBL rem = fMult(invFrameSizeDiv8, 427 (FIXP_DBL)(topMdct << (DFRACT_BITS - 12))); 428 if ((LONG)rem & (LONG)0x1F) { 429 stopWinIdx -= 1; 430 stopCol = (int)winBorderToColMap[stopWinIdx]; 431 } 432 } 433 topQmf = (64); 434 } 435 436 /* save previous factors */ 437 if (stopCol == numQmfSubSamples) { 438 int tmpBottom = bottomQmf; 439 440 if ((int)winBorderToColMap[8] > startCol) { 441 tmpBottom = 0; /* band starts in previous short window */ 442 } 443 444 for (bin = tmpBottom; bin < topQmf; bin++) { 445 hDrcData->prevFact_mag[bin] = fact_mag[band]; 446 } 447 } 448 449 /* apply */ 450 if ((col >= startCol) && (col < stopCol)) { 451 if (col >= (int)winBorderToColMap[startWinIdx + 1]) { 452 bottomQmf = 0; /* band starts in previous short window */ 453 } 454 if (col < (int)winBorderToColMap[stopWinIdx - 1]) { 455 topQmf = (64); /* band ends in next short window */ 456 } 457 458 drcFact_mag = fact_mag[band]; 459 460 /* normalize scale factor */ 461 if (fact_exp < maxShift) { 462 drcFact_mag >>= maxShift - fact_exp; 463 } 464 465 /* apply scaling */ 466 for (bin = bottomQmf; bin < topQmf; bin++) { 467 qmfRealSlot[bin] = fMult(qmfRealSlot[bin], drcFact_mag); 468 if (!useLP) { 469 qmfImagSlot[bin] = fMult(qmfImagSlot[bin], drcFact_mag); 470 } 471 } 472 } 473 } 474 475 bottomMdct = topMdct; 476 } /* end of bands loop */ 477 478 if (col == (numQmfSubSamples >> 1) - 1) { 479 hDrcData->prevFact_exp = fact_exp; 480 } 481 } 482 483 /*! 484 \brief Apply DRC factors frame based. 485 486 \hDrcData Handle to DRC channel data. 487 \qmfRealSlot Pointer to real valued QMF data of the whole frame. 488 \qmfImagSlot Pointer to the imaginary QMF data of the whole frame. 489 \numQmfSubSamples Total number of time slots for one frame. 490 \scaleFactor Pointer to the out scale factor of the frame. 491 492 \return None. 493 */ 494 void sbrDecoder_drcApply(HANDLE_SBR_DRC_CHANNEL hDrcData, 495 FIXP_DBL **QmfBufferReal, FIXP_DBL **QmfBufferImag, 496 int numQmfSubSamples, int *scaleFactor) { 497 int col; 498 int maxShift = 0; 499 500 if (hDrcData == NULL) { 501 return; 502 } 503 if (hDrcData->enable == 0) { 504 return; /* Avoid changing the scaleFactor even though the processing is 505 disabled. */ 506 } 507 508 /* get max scale factor */ 509 if (hDrcData->prevFact_exp > maxShift) { 510 maxShift = hDrcData->prevFact_exp; 511 } 512 if (hDrcData->currFact_exp > maxShift) { 513 maxShift = hDrcData->currFact_exp; 514 } 515 if (hDrcData->nextFact_exp > maxShift) { 516 maxShift = hDrcData->nextFact_exp; 517 } 518 519 for (col = 0; col < numQmfSubSamples; col++) { 520 FIXP_DBL *qmfSlotReal = QmfBufferReal[col]; 521 FIXP_DBL *qmfSlotImag = (QmfBufferImag == NULL) ? NULL : QmfBufferImag[col]; 522 523 sbrDecoder_drcApplySlot(hDrcData, qmfSlotReal, qmfSlotImag, col, 524 numQmfSubSamples, maxShift); 525 } 526 527 *scaleFactor += maxShift; 528 } 529