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 /**************************** AAC decoder library ****************************** 96 97 Author(s): Manuel Jander 98 99 Description: 100 101 *******************************************************************************/ 102 103 #include "aacdecoder_lib.h" 104 105 #include "aac_ram.h" 106 #include "aacdecoder.h" 107 #include "tpdec_lib.h" 108 #include "FDK_core.h" /* FDK_tools version info */ 109 110 #include "sbrdecoder.h" 111 112 #include "conceal.h" 113 114 #include "aacdec_drc.h" 115 116 #include "sac_dec_lib.h" 117 118 #include "pcm_utils.h" 119 120 /* Decoder library info */ 121 #define AACDECODER_LIB_VL0 3 122 #define AACDECODER_LIB_VL1 0 123 #define AACDECODER_LIB_VL2 0 124 #define AACDECODER_LIB_TITLE "AAC Decoder Lib" 125 #ifdef __ANDROID__ 126 #define AACDECODER_LIB_BUILD_DATE "" 127 #define AACDECODER_LIB_BUILD_TIME "" 128 #else 129 #define AACDECODER_LIB_BUILD_DATE __DATE__ 130 #define AACDECODER_LIB_BUILD_TIME __TIME__ 131 #endif 132 133 static AAC_DECODER_ERROR setConcealMethod(const HANDLE_AACDECODER self, 134 const INT method); 135 136 static void aacDecoder_setMetadataExpiry(const HANDLE_AACDECODER self, 137 const INT value) { 138 /* check decoder handle */ 139 if (self != NULL) { 140 INT mdExpFrame = 0; /* default: disable */ 141 142 if ((value > 0) && 143 (self->streamInfo.aacSamplesPerFrame > 144 0)) { /* Determine the corresponding number of frames: */ 145 FIXP_DBL frameTime = fDivNorm(self->streamInfo.aacSampleRate, 146 self->streamInfo.aacSamplesPerFrame * 1000); 147 mdExpFrame = fMultIceil(frameTime, value); 148 } 149 150 /* Configure DRC module */ 151 aacDecoder_drcSetParam(self->hDrcInfo, DRC_DATA_EXPIRY_FRAME, mdExpFrame); 152 153 /* Configure PCM downmix module */ 154 pcmDmx_SetParam(self->hPcmUtils, DMX_BS_DATA_EXPIRY_FRAME, mdExpFrame); 155 } 156 } 157 158 LINKSPEC_CPP AAC_DECODER_ERROR 159 aacDecoder_GetFreeBytes(const HANDLE_AACDECODER self, UINT *pFreeBytes) { 160 /* reset free bytes */ 161 *pFreeBytes = 0; 162 163 /* check handle */ 164 if (!self) return AAC_DEC_INVALID_HANDLE; 165 166 /* return nr of free bytes */ 167 HANDLE_FDK_BITSTREAM hBs = transportDec_GetBitstream(self->hInput, 0); 168 *pFreeBytes = FDKgetFreeBits(hBs) >> 3; 169 170 /* success */ 171 return AAC_DEC_OK; 172 } 173 174 /** 175 * Config Decoder using a CSAudioSpecificConfig struct. 176 */ 177 static LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_Config( 178 HANDLE_AACDECODER self, const CSAudioSpecificConfig *pAscStruct, 179 UCHAR configMode, UCHAR *configChanged) { 180 AAC_DECODER_ERROR err; 181 182 /* Initialize AAC core decoder, and update self->streaminfo */ 183 err = CAacDecoder_Init(self, pAscStruct, configMode, configChanged); 184 185 if (!FDK_chMapDescr_isValid(&self->mapDescr)) { 186 return AAC_DEC_UNSUPPORTED_CHANNELCONFIG; 187 } 188 189 return err; 190 } 191 192 LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_ConfigRaw(HANDLE_AACDECODER self, 193 UCHAR *conf[], 194 const UINT length[]) { 195 AAC_DECODER_ERROR err = AAC_DEC_OK; 196 TRANSPORTDEC_ERROR errTp; 197 UINT layer, nrOfLayers = self->nrOfLayers; 198 199 for (layer = 0; layer < nrOfLayers; layer++) { 200 if (length[layer] > 0) { 201 errTp = transportDec_OutOfBandConfig(self->hInput, conf[layer], 202 length[layer], layer); 203 if (errTp != TRANSPORTDEC_OK) { 204 switch (errTp) { 205 case TRANSPORTDEC_NEED_TO_RESTART: 206 err = AAC_DEC_NEED_TO_RESTART; 207 break; 208 case TRANSPORTDEC_UNSUPPORTED_FORMAT: 209 err = AAC_DEC_UNSUPPORTED_FORMAT; 210 break; 211 default: 212 err = AAC_DEC_UNKNOWN; 213 break; 214 } 215 /* if baselayer is OK we continue decoding */ 216 if (layer >= 1) { 217 self->nrOfLayers = layer; 218 err = AAC_DEC_OK; 219 } 220 break; 221 } 222 } 223 } 224 225 return err; 226 } 227 228 LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_RawISOBMFFData(HANDLE_AACDECODER self, 229 UCHAR *buffer, 230 UINT length) { 231 FDK_BITSTREAM bs; 232 HANDLE_FDK_BITSTREAM hBs = &bs; 233 AAC_DECODER_ERROR err = AAC_DEC_OK; 234 235 if (length < 8) return AAC_DEC_UNKNOWN; 236 237 while (length >= 8) { 238 UINT size = 239 (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]; 240 DRC_DEC_ERROR uniDrcErr = DRC_DEC_OK; 241 242 if (length < size) return AAC_DEC_UNKNOWN; 243 if (size <= 8) return AAC_DEC_UNKNOWN; 244 245 FDKinitBitStream(hBs, buffer + 8, 0x10000000, (size - 8) * 8); 246 247 if ((buffer[4] == 'l') && (buffer[5] == 'u') && (buffer[6] == 'd') && 248 (buffer[7] == 't')) { 249 uniDrcErr = FDK_drcDec_ReadLoudnessBox(self->hUniDrcDecoder, hBs); 250 } else if ((buffer[4] == 'd') && (buffer[5] == 'm') && (buffer[6] == 'i') && 251 (buffer[7] == 'x')) { 252 uniDrcErr = 253 FDK_drcDec_ReadDownmixInstructions_Box(self->hUniDrcDecoder, hBs); 254 } else if ((buffer[4] == 'u') && (buffer[5] == 'd') && (buffer[6] == 'i') && 255 (buffer[7] == '2')) { 256 uniDrcErr = 257 FDK_drcDec_ReadUniDrcInstructions_Box(self->hUniDrcDecoder, hBs); 258 } else if ((buffer[4] == 'u') && (buffer[5] == 'd') && (buffer[6] == 'c') && 259 (buffer[7] == '2')) { 260 uniDrcErr = 261 FDK_drcDec_ReadUniDrcCoefficients_Box(self->hUniDrcDecoder, hBs); 262 } 263 264 if (uniDrcErr != DRC_DEC_OK) err = AAC_DEC_UNKNOWN; 265 266 buffer += size; 267 length -= size; 268 } 269 270 return err; 271 } 272 273 static INT aacDecoder_ConfigCallback(void *handle, 274 const CSAudioSpecificConfig *pAscStruct, 275 UCHAR configMode, UCHAR *configChanged) { 276 HANDLE_AACDECODER self = (HANDLE_AACDECODER)handle; 277 AAC_DECODER_ERROR err = AAC_DEC_OK; 278 TRANSPORTDEC_ERROR errTp; 279 280 FDK_ASSERT(self != NULL); 281 { 282 { err = aacDecoder_Config(self, pAscStruct, configMode, configChanged); } 283 } 284 if (err == AAC_DEC_OK) { 285 /* 286 revert concealment method if either 287 - Interpolation concealment might not be meaningful 288 - Interpolation concealment is not implemented 289 */ 290 if ((self->flags[0] & (AC_LD | AC_ELD) && 291 (self->concealMethodUser == ConcealMethodNone) && 292 CConcealment_GetDelay(&self->concealCommonData) > 293 0) /* might not be meaningful but allow if user has set it 294 expicitly */ 295 || (self->flags[0] & (AC_USAC | AC_RSVD50 | AC_RSV603DA) && 296 CConcealment_GetDelay(&self->concealCommonData) > 297 0) /* not implemented */ 298 ) { 299 /* Revert to error concealment method Noise Substitution. 300 Because interpolation is not implemented for USAC or 301 the additional delay is unwanted for low delay codecs. */ 302 setConcealMethod(self, 1); 303 } 304 aacDecoder_setMetadataExpiry(self, self->metadataExpiry); 305 errTp = TRANSPORTDEC_OK; 306 } else { 307 if (err == AAC_DEC_NEED_TO_RESTART) { 308 errTp = TRANSPORTDEC_NEED_TO_RESTART; 309 } else if (IS_INIT_ERROR(err)) { 310 errTp = TRANSPORTDEC_UNSUPPORTED_FORMAT; 311 } /* Fatal errors */ 312 else { 313 errTp = TRANSPORTDEC_UNKOWN_ERROR; 314 } 315 } 316 317 return errTp; 318 } 319 320 static INT aacDecoder_FreeMemCallback(void *handle, 321 const CSAudioSpecificConfig *pAscStruct) { 322 TRANSPORTDEC_ERROR errTp = TRANSPORTDEC_OK; 323 HANDLE_AACDECODER self = (HANDLE_AACDECODER)handle; 324 325 const int subStreamIndex = 0; 326 327 FDK_ASSERT(self != NULL); 328 329 if (CAacDecoder_FreeMem(self, subStreamIndex) != AAC_DEC_OK) { 330 errTp = TRANSPORTDEC_UNKOWN_ERROR; 331 } 332 333 /* free Ram_SbrDecoder and Ram_SbrDecChannel */ 334 if (self->hSbrDecoder != NULL) { 335 if (sbrDecoder_FreeMem(&self->hSbrDecoder) != SBRDEC_OK) { 336 errTp = TRANSPORTDEC_UNKOWN_ERROR; 337 } 338 } 339 340 /* free pSpatialDec and mpsData */ 341 if (self->pMpegSurroundDecoder != NULL) { 342 if (mpegSurroundDecoder_FreeMem( 343 (CMpegSurroundDecoder *)self->pMpegSurroundDecoder) != MPS_OK) { 344 errTp = TRANSPORTDEC_UNKOWN_ERROR; 345 } 346 } 347 348 /* free persistent qmf domain buffer, QmfWorkBufferCore3, QmfWorkBufferCore4, 349 * QmfWorkBufferCore5 and configuration variables */ 350 FDK_QmfDomain_FreeMem(&self->qmfDomain); 351 352 return errTp; 353 } 354 355 static INT aacDecoder_CtrlCFGChangeCallback( 356 void *handle, const CCtrlCFGChange *pCtrlCFGChangeStruct) { 357 TRANSPORTDEC_ERROR errTp = TRANSPORTDEC_OK; 358 HANDLE_AACDECODER self = (HANDLE_AACDECODER)handle; 359 360 if (self != NULL) { 361 CAacDecoder_CtrlCFGChange( 362 self, pCtrlCFGChangeStruct->flushStatus, pCtrlCFGChangeStruct->flushCnt, 363 pCtrlCFGChangeStruct->buildUpStatus, pCtrlCFGChangeStruct->buildUpCnt); 364 } else { 365 errTp = TRANSPORTDEC_UNKOWN_ERROR; 366 } 367 368 return errTp; 369 } 370 371 static INT aacDecoder_SscCallback(void *handle, HANDLE_FDK_BITSTREAM hBs, 372 const AUDIO_OBJECT_TYPE coreCodec, 373 const INT samplingRate, 374 const INT stereoConfigIndex, 375 const INT coreSbrFrameLengthIndex, 376 const INT configBytes, const UCHAR configMode, 377 UCHAR *configChanged) { 378 SACDEC_ERROR err; 379 TRANSPORTDEC_ERROR errTp; 380 HANDLE_AACDECODER hAacDecoder = (HANDLE_AACDECODER)handle; 381 382 err = mpegSurroundDecoder_Config( 383 (CMpegSurroundDecoder *)hAacDecoder->pMpegSurroundDecoder, hBs, coreCodec, 384 samplingRate, stereoConfigIndex, coreSbrFrameLengthIndex, configBytes, 385 configMode, configChanged); 386 387 switch (err) { 388 case MPS_UNSUPPORTED_CONFIG: 389 /* MPS found but invalid or not decodable by this instance */ 390 /* We switch off MPS and keep going */ 391 hAacDecoder->mpsEnableCurr = 0; 392 hAacDecoder->mpsApplicable = 0; 393 errTp = TRANSPORTDEC_OK; 394 break; 395 case MPS_PARSE_ERROR: 396 /* MPS found but invalid or not decodable by this instance */ 397 hAacDecoder->mpsEnableCurr = 0; 398 hAacDecoder->mpsApplicable = 0; 399 if ((coreCodec == AOT_USAC) || (coreCodec == AOT_DRM_USAC) || 400 IS_LOWDELAY(coreCodec)) { 401 errTp = TRANSPORTDEC_PARSE_ERROR; 402 } else { 403 errTp = TRANSPORTDEC_OK; 404 } 405 break; 406 case MPS_OK: 407 hAacDecoder->mpsApplicable = 1; 408 errTp = TRANSPORTDEC_OK; 409 break; 410 default: 411 /* especially Parsing error is critical for transport layer */ 412 hAacDecoder->mpsApplicable = 0; 413 errTp = TRANSPORTDEC_UNKOWN_ERROR; 414 } 415 416 return (INT)errTp; 417 } 418 419 static INT aacDecoder_UniDrcCallback(void *handle, HANDLE_FDK_BITSTREAM hBs, 420 const INT fullPayloadLength, 421 const INT payloadType, 422 const INT subStreamIndex, 423 const INT payloadStart, 424 const AUDIO_OBJECT_TYPE aot) { 425 DRC_DEC_ERROR err = DRC_DEC_OK; 426 TRANSPORTDEC_ERROR errTp; 427 HANDLE_AACDECODER hAacDecoder = (HANDLE_AACDECODER)handle; 428 DRC_DEC_CODEC_MODE drcDecCodecMode = DRC_DEC_CODEC_MODE_UNDEFINED; 429 430 if (subStreamIndex != 0) { 431 return TRANSPORTDEC_OK; 432 } 433 434 else if (aot == AOT_USAC) { 435 drcDecCodecMode = DRC_DEC_MPEG_D_USAC; 436 } 437 438 err = FDK_drcDec_SetCodecMode(hAacDecoder->hUniDrcDecoder, drcDecCodecMode); 439 if (err) return (INT)TRANSPORTDEC_UNKOWN_ERROR; 440 441 if (payloadType == 0) /* uniDrcConfig */ 442 { 443 err = FDK_drcDec_ReadUniDrcConfig(hAacDecoder->hUniDrcDecoder, hBs); 444 } else /* loudnessInfoSet */ 445 { 446 err = FDK_drcDec_ReadLoudnessInfoSet(hAacDecoder->hUniDrcDecoder, hBs); 447 hAacDecoder->loudnessInfoSetPosition[1] = payloadStart; 448 hAacDecoder->loudnessInfoSetPosition[2] = fullPayloadLength; 449 } 450 451 if (err == DRC_DEC_OK) 452 errTp = TRANSPORTDEC_OK; 453 else 454 errTp = TRANSPORTDEC_UNKOWN_ERROR; 455 456 return (INT)errTp; 457 } 458 459 LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_AncDataInit(HANDLE_AACDECODER self, 460 UCHAR *buffer, int size) { 461 CAncData *ancData = &self->ancData; 462 463 return CAacDecoder_AncDataInit(ancData, buffer, size); 464 } 465 466 LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_AncDataGet(HANDLE_AACDECODER self, 467 int index, UCHAR **ptr, 468 int *size) { 469 CAncData *ancData = &self->ancData; 470 471 return CAacDecoder_AncDataGet(ancData, index, ptr, size); 472 } 473 474 /* If MPS is present in stream, but not supported by this instance, we'll 475 have to switch off MPS and use QMF synthesis in the SBR module if required */ 476 static int isSupportedMpsConfig(AUDIO_OBJECT_TYPE aot, 477 unsigned int numInChannels, 478 unsigned int fMpsPresent) { 479 LIB_INFO libInfo[FDK_MODULE_LAST]; 480 UINT mpsCaps; 481 int isSupportedCfg = 1; 482 483 FDKinitLibInfo(libInfo); 484 485 mpegSurroundDecoder_GetLibInfo(libInfo); 486 487 mpsCaps = FDKlibInfo_getCapabilities(libInfo, FDK_MPSDEC); 488 489 if (!(mpsCaps & CAPF_MPS_LD) && IS_LOWDELAY(aot)) { 490 /* We got an LD AOT but MPS decoder does not support LD. */ 491 isSupportedCfg = 0; 492 } 493 if ((mpsCaps & CAPF_MPS_LD) && IS_LOWDELAY(aot) && !fMpsPresent) { 494 /* We got an LD AOT and the MPS decoder supports it. 495 * But LD-MPS is not explicitly signaled. */ 496 isSupportedCfg = 0; 497 } 498 if (!(mpsCaps & CAPF_MPS_USAC) && IS_USAC(aot)) { 499 /* We got an USAC AOT but MPS decoder does not support USAC. */ 500 isSupportedCfg = 0; 501 } 502 if (!(mpsCaps & CAPF_MPS_STD) && !IS_LOWDELAY(aot) && !IS_USAC(aot)) { 503 /* We got an GA AOT but MPS decoder does not support it. */ 504 isSupportedCfg = 0; 505 } 506 /* Check whether the MPS modul supports the given number of input channels: */ 507 switch (numInChannels) { 508 case 1: 509 if (!(mpsCaps & CAPF_MPS_1CH_IN)) { 510 /* We got a one channel input to MPS decoder but it does not support it. 511 */ 512 isSupportedCfg = 0; 513 } 514 break; 515 case 2: 516 if (!(mpsCaps & CAPF_MPS_2CH_IN)) { 517 /* We got a two channel input to MPS decoder but it does not support it. 518 */ 519 isSupportedCfg = 0; 520 } 521 break; 522 case 5: 523 case 6: 524 if (!(mpsCaps & CAPF_MPS_6CH_IN)) { 525 /* We got a six channel input to MPS decoder but it does not support it. 526 */ 527 isSupportedCfg = 0; 528 } 529 break; 530 default: 531 isSupportedCfg = 0; 532 } 533 534 return (isSupportedCfg); 535 } 536 537 static AAC_DECODER_ERROR setConcealMethod( 538 const HANDLE_AACDECODER self, /*!< Handle of the decoder instance */ 539 const INT method) { 540 AAC_DECODER_ERROR errorStatus = AAC_DEC_OK; 541 CConcealParams *pConcealData = NULL; 542 int method_revert = 0; 543 HANDLE_SBRDECODER hSbrDec = NULL; 544 HANDLE_AAC_DRC hDrcInfo = NULL; 545 HANDLE_PCM_DOWNMIX hPcmDmx = NULL; 546 CConcealmentMethod backupMethod = ConcealMethodNone; 547 int backupDelay = 0; 548 int bsDelay = 0; 549 550 /* check decoder handle */ 551 if (self != NULL) { 552 pConcealData = &self->concealCommonData; 553 hSbrDec = self->hSbrDecoder; 554 hDrcInfo = self->hDrcInfo; 555 hPcmDmx = self->hPcmUtils; 556 if (self->flags[0] & (AC_USAC | AC_RSVD50 | AC_RSV603DA) && method >= 2) { 557 /* Interpolation concealment is not implemented for USAC/RSVD50 */ 558 /* errorStatus = AAC_DEC_SET_PARAM_FAIL; 559 goto bail; */ 560 method_revert = 1; 561 } 562 if (self->flags[0] & (AC_USAC | AC_RSVD50 | AC_RSV603DA) && method >= 2) { 563 /* Interpolation concealment is not implemented for USAC/RSVD50 */ 564 errorStatus = AAC_DEC_SET_PARAM_FAIL; 565 goto bail; 566 } 567 } 568 569 /* Get current method/delay */ 570 backupMethod = CConcealment_GetMethod(pConcealData); 571 backupDelay = CConcealment_GetDelay(pConcealData); 572 573 /* Be sure to set AAC and SBR concealment method simultaneously! */ 574 errorStatus = CConcealment_SetParams( 575 pConcealData, 576 (method_revert == 0) ? (int)method : (int)1, // concealMethod 577 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED, // concealFadeOutSlope 578 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED, // concealFadeInSlope 579 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED, // concealMuteRelease 580 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED // concealComfNoiseLevel 581 ); 582 if ((errorStatus != AAC_DEC_OK) && (errorStatus != AAC_DEC_INVALID_HANDLE)) { 583 goto bail; 584 } 585 586 /* Get new delay */ 587 bsDelay = CConcealment_GetDelay(pConcealData); 588 589 { 590 SBR_ERROR sbrErr = SBRDEC_OK; 591 592 /* set SBR bitstream delay */ 593 sbrErr = sbrDecoder_SetParam(hSbrDec, SBR_SYSTEM_BITSTREAM_DELAY, bsDelay); 594 595 switch (sbrErr) { 596 case SBRDEC_OK: 597 case SBRDEC_NOT_INITIALIZED: 598 if (self != NULL) { 599 /* save the param value and set later 600 (when SBR has been initialized) */ 601 self->sbrParams.bsDelay = bsDelay; 602 } 603 break; 604 default: 605 errorStatus = AAC_DEC_SET_PARAM_FAIL; 606 goto bail; 607 } 608 } 609 610 errorStatus = aacDecoder_drcSetParam(hDrcInfo, DRC_BS_DELAY, bsDelay); 611 if ((errorStatus != AAC_DEC_OK) && (errorStatus != AAC_DEC_INVALID_HANDLE)) { 612 goto bail; 613 } 614 615 if (errorStatus == AAC_DEC_OK) { 616 PCMDMX_ERROR err = pcmDmx_SetParam(hPcmDmx, DMX_BS_DATA_DELAY, bsDelay); 617 switch (err) { 618 case PCMDMX_INVALID_HANDLE: 619 errorStatus = AAC_DEC_INVALID_HANDLE; 620 case PCMDMX_OK: 621 break; 622 default: 623 errorStatus = AAC_DEC_SET_PARAM_FAIL; 624 goto bail; 625 } 626 } 627 628 bail: 629 if ((errorStatus != AAC_DEC_OK) && (errorStatus != AAC_DEC_INVALID_HANDLE)) { 630 /* Revert to the initial state */ 631 CConcealment_SetParams( 632 pConcealData, (int)backupMethod, AACDEC_CONCEAL_PARAM_NOT_SPECIFIED, 633 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED, AACDEC_CONCEAL_PARAM_NOT_SPECIFIED, 634 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED); 635 /* Revert SBR bitstream delay */ 636 sbrDecoder_SetParam(hSbrDec, SBR_SYSTEM_BITSTREAM_DELAY, backupDelay); 637 /* Revert DRC bitstream delay */ 638 aacDecoder_drcSetParam(hDrcInfo, DRC_BS_DELAY, backupDelay); 639 /* Revert PCM mixdown bitstream delay */ 640 pcmDmx_SetParam(hPcmDmx, DMX_BS_DATA_DELAY, backupDelay); 641 } 642 643 return errorStatus; 644 } 645 646 LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_SetParam( 647 const HANDLE_AACDECODER self, /*!< Handle of the decoder instance */ 648 const AACDEC_PARAM param, /*!< Parameter to set */ 649 const INT value) /*!< Parameter valued */ 650 { 651 AAC_DECODER_ERROR errorStatus = AAC_DEC_OK; 652 HANDLE_TRANSPORTDEC hTpDec = NULL; 653 TRANSPORTDEC_ERROR errTp = TRANSPORTDEC_OK; 654 HANDLE_AAC_DRC hDrcInfo = NULL; 655 HANDLE_PCM_DOWNMIX hPcmDmx = NULL; 656 PCMDMX_ERROR dmxErr = PCMDMX_OK; 657 TDLimiterPtr hPcmTdl = NULL; 658 DRC_DEC_ERROR uniDrcErr = DRC_DEC_OK; 659 660 /* check decoder handle */ 661 if (self != NULL) { 662 hTpDec = self->hInput; 663 hDrcInfo = self->hDrcInfo; 664 hPcmDmx = self->hPcmUtils; 665 hPcmTdl = self->hLimiter; 666 } else { 667 errorStatus = AAC_DEC_INVALID_HANDLE; 668 goto bail; 669 } 670 671 /* configure the subsystems */ 672 switch (param) { 673 case AAC_PCM_MIN_OUTPUT_CHANNELS: 674 if (value < -1 || value > (8)) { 675 return AAC_DEC_SET_PARAM_FAIL; 676 } 677 dmxErr = pcmDmx_SetParam(hPcmDmx, MIN_NUMBER_OF_OUTPUT_CHANNELS, value); 678 break; 679 680 case AAC_PCM_MAX_OUTPUT_CHANNELS: 681 if (value < -1 || value > (8)) { 682 return AAC_DEC_SET_PARAM_FAIL; 683 } 684 dmxErr = pcmDmx_SetParam(hPcmDmx, MAX_NUMBER_OF_OUTPUT_CHANNELS, value); 685 686 if (dmxErr != PCMDMX_OK) { 687 goto bail; 688 } 689 errorStatus = 690 aacDecoder_drcSetParam(hDrcInfo, MAX_OUTPUT_CHANNELS, value); 691 if (value > 0) { 692 uniDrcErr = FDK_drcDec_SetParam(self->hUniDrcDecoder, 693 DRC_DEC_TARGET_CHANNEL_COUNT_REQUESTED, 694 (FIXP_DBL)value); 695 } 696 break; 697 698 case AAC_PCM_DUAL_CHANNEL_OUTPUT_MODE: 699 dmxErr = pcmDmx_SetParam(hPcmDmx, DMX_DUAL_CHANNEL_MODE, value); 700 break; 701 702 case AAC_PCM_LIMITER_ENABLE: 703 if (value < -2 || value > 1) { 704 return AAC_DEC_SET_PARAM_FAIL; 705 } 706 self->limiterEnableUser = value; 707 break; 708 709 case AAC_PCM_LIMITER_ATTACK_TIME: 710 if (value <= 0) { /* module function converts value to unsigned */ 711 return AAC_DEC_SET_PARAM_FAIL; 712 } 713 switch (pcmLimiter_SetAttack(hPcmTdl, value)) { 714 case TDLIMIT_OK: 715 break; 716 case TDLIMIT_INVALID_HANDLE: 717 return AAC_DEC_INVALID_HANDLE; 718 case TDLIMIT_INVALID_PARAMETER: 719 default: 720 return AAC_DEC_SET_PARAM_FAIL; 721 } 722 break; 723 724 case AAC_PCM_LIMITER_RELEAS_TIME: 725 if (value <= 0) { /* module function converts value to unsigned */ 726 return AAC_DEC_SET_PARAM_FAIL; 727 } 728 switch (pcmLimiter_SetRelease(hPcmTdl, value)) { 729 case TDLIMIT_OK: 730 break; 731 case TDLIMIT_INVALID_HANDLE: 732 return AAC_DEC_INVALID_HANDLE; 733 case TDLIMIT_INVALID_PARAMETER: 734 default: 735 return AAC_DEC_SET_PARAM_FAIL; 736 } 737 break; 738 739 case AAC_METADATA_PROFILE: { 740 DMX_PROFILE_TYPE dmxProfile; 741 INT mdExpiry = -1; /* in ms (-1: don't change) */ 742 743 switch ((AAC_MD_PROFILE)value) { 744 case AAC_MD_PROFILE_MPEG_STANDARD: 745 dmxProfile = DMX_PRFL_STANDARD; 746 break; 747 case AAC_MD_PROFILE_MPEG_LEGACY: 748 dmxProfile = DMX_PRFL_MATRIX_MIX; 749 break; 750 case AAC_MD_PROFILE_MPEG_LEGACY_PRIO: 751 dmxProfile = DMX_PRFL_FORCE_MATRIX_MIX; 752 break; 753 case AAC_MD_PROFILE_ARIB_JAPAN: 754 dmxProfile = DMX_PRFL_ARIB_JAPAN; 755 mdExpiry = 550; /* ms */ 756 break; 757 default: 758 return AAC_DEC_SET_PARAM_FAIL; 759 } 760 dmxErr = pcmDmx_SetParam(hPcmDmx, DMX_PROFILE_SETTING, (INT)dmxProfile); 761 if (dmxErr != PCMDMX_OK) { 762 goto bail; 763 } 764 if ((self != NULL) && (mdExpiry >= 0)) { 765 self->metadataExpiry = mdExpiry; 766 /* Determine the corresponding number of frames and configure all 767 * related modules. */ 768 aacDecoder_setMetadataExpiry(self, mdExpiry); 769 } 770 } break; 771 772 case AAC_METADATA_EXPIRY_TIME: 773 if (value < 0) { 774 return AAC_DEC_SET_PARAM_FAIL; 775 } 776 if (self != NULL) { 777 self->metadataExpiry = value; 778 /* Determine the corresponding number of frames and configure all 779 * related modules. */ 780 aacDecoder_setMetadataExpiry(self, value); 781 } 782 break; 783 784 case AAC_PCM_OUTPUT_CHANNEL_MAPPING: 785 if (value < 0 || value > 1) { 786 return AAC_DEC_SET_PARAM_FAIL; 787 } 788 /* CAUTION: The given value must be inverted to match the logic! */ 789 FDK_chMapDescr_setPassThrough(&self->mapDescr, !value); 790 break; 791 792 case AAC_QMF_LOWPOWER: 793 if (value < -1 || value > 1) { 794 return AAC_DEC_SET_PARAM_FAIL; 795 } 796 797 /** 798 * Set QMF mode (might be overriden) 799 * 0:HQ (complex) 800 * 1:LP (partially complex) 801 */ 802 self->qmfModeUser = (QMF_MODE)value; 803 break; 804 805 case AAC_DRC_ATTENUATION_FACTOR: 806 /* DRC compression factor (where 0 is no and 127 is max compression) */ 807 errorStatus = aacDecoder_drcSetParam(hDrcInfo, DRC_CUT_SCALE, value); 808 break; 809 810 case AAC_DRC_BOOST_FACTOR: 811 /* DRC boost factor (where 0 is no and 127 is max boost) */ 812 errorStatus = aacDecoder_drcSetParam(hDrcInfo, DRC_BOOST_SCALE, value); 813 break; 814 815 case AAC_DRC_REFERENCE_LEVEL: 816 if ((value >= 0) && 817 ((value < 40) || (value > 127))) /* allowed range: -10 to -31.75 dB */ 818 return AAC_DEC_SET_PARAM_FAIL; 819 /* DRC target reference level quantized in 0.25dB steps using values 820 [40..127]. Negative values switch off loudness normalisation. Negative 821 values also switch off MPEG-4 DRC, while MPEG-D DRC can be separately 822 switched on/off with AAC_UNIDRC_SET_EFFECT */ 823 errorStatus = aacDecoder_drcSetParam(hDrcInfo, TARGET_REF_LEVEL, value); 824 uniDrcErr = FDK_drcDec_SetParam(self->hUniDrcDecoder, 825 DRC_DEC_LOUDNESS_NORMALIZATION_ON, 826 (FIXP_DBL)(value >= 0)); 827 /* set target loudness also for MPEG-D DRC */ 828 self->defaultTargetLoudness = (SCHAR)value; 829 break; 830 831 case AAC_DRC_HEAVY_COMPRESSION: 832 /* Don't need to overwrite cut/boost values */ 833 errorStatus = 834 aacDecoder_drcSetParam(hDrcInfo, APPLY_HEAVY_COMPRESSION, value); 835 break; 836 837 case AAC_DRC_DEFAULT_PRESENTATION_MODE: 838 /* DRC default presentation mode */ 839 errorStatus = 840 aacDecoder_drcSetParam(hDrcInfo, DEFAULT_PRESENTATION_MODE, value); 841 break; 842 843 case AAC_DRC_ENC_TARGET_LEVEL: 844 /* Encoder target level for light (i.e. not heavy) compression: 845 Target reference level assumed at encoder for deriving limiting gains 846 */ 847 errorStatus = 848 aacDecoder_drcSetParam(hDrcInfo, ENCODER_TARGET_LEVEL, value); 849 break; 850 851 case AAC_UNIDRC_SET_EFFECT: 852 if ((value < -1) || (value > 6)) return AAC_DEC_SET_PARAM_FAIL; 853 uniDrcErr = FDK_drcDec_SetParam(self->hUniDrcDecoder, DRC_DEC_EFFECT_TYPE, 854 (FIXP_DBL)value); 855 break; 856 case AAC_TPDEC_CLEAR_BUFFER: 857 errTp = transportDec_SetParam(hTpDec, TPDEC_PARAM_RESET, 1); 858 self->streamInfo.numLostAccessUnits = 0; 859 self->streamInfo.numBadBytes = 0; 860 self->streamInfo.numTotalBytes = 0; 861 /* aacDecoder_SignalInterruption(self); */ 862 break; 863 case AAC_CONCEAL_METHOD: 864 /* Changing the concealment method can introduce additional bitstream 865 delay. And that in turn affects sub libraries and modules which makes 866 the whole thing quite complex. So the complete changing routine is 867 packed into a helper function which keeps all modules and libs in a 868 consistent state even in the case an error occures. */ 869 errorStatus = setConcealMethod(self, value); 870 if (errorStatus == AAC_DEC_OK) { 871 self->concealMethodUser = (CConcealmentMethod)value; 872 } 873 break; 874 875 default: 876 return AAC_DEC_SET_PARAM_FAIL; 877 } /* switch(param) */ 878 879 bail: 880 881 if (errorStatus == AAC_DEC_OK) { 882 /* Check error code returned by DMX module library: */ 883 switch (dmxErr) { 884 case PCMDMX_OK: 885 break; 886 case PCMDMX_INVALID_HANDLE: 887 errorStatus = AAC_DEC_INVALID_HANDLE; 888 break; 889 default: 890 errorStatus = AAC_DEC_SET_PARAM_FAIL; 891 } 892 } 893 894 if (errTp != TRANSPORTDEC_OK && errorStatus == AAC_DEC_OK) { 895 errorStatus = AAC_DEC_SET_PARAM_FAIL; 896 } 897 898 if (errorStatus == AAC_DEC_OK) { 899 /* Check error code returned by MPEG-D DRC decoder library: */ 900 switch (uniDrcErr) { 901 case 0: 902 break; 903 case -9998: 904 errorStatus = AAC_DEC_INVALID_HANDLE; 905 break; 906 default: 907 errorStatus = AAC_DEC_SET_PARAM_FAIL; 908 break; 909 } 910 } 911 912 return (errorStatus); 913 } 914 LINKSPEC_CPP HANDLE_AACDECODER aacDecoder_Open(TRANSPORT_TYPE transportFmt, 915 UINT nrOfLayers) { 916 AAC_DECODER_INSTANCE *aacDec = NULL; 917 HANDLE_TRANSPORTDEC pIn; 918 int err = 0; 919 int stereoConfigIndex = -1; 920 921 UINT nrOfLayers_min = fMin(nrOfLayers, (UINT)1); 922 923 /* Allocate transport layer struct. */ 924 pIn = transportDec_Open(transportFmt, TP_FLAG_MPEG4, nrOfLayers_min); 925 if (pIn == NULL) { 926 return NULL; 927 } 928 929 /* Allocate AAC decoder core struct. */ 930 aacDec = CAacDecoder_Open(transportFmt); 931 932 if (aacDec == NULL) { 933 transportDec_Close(&pIn); 934 goto bail; 935 } 936 aacDec->hInput = pIn; 937 938 aacDec->nrOfLayers = nrOfLayers_min; 939 940 /* Setup channel mapping descriptor. */ 941 FDK_chMapDescr_init(&aacDec->mapDescr, NULL, 0, 0); 942 943 /* Register Config Update callback. */ 944 transportDec_RegisterAscCallback(pIn, aacDecoder_ConfigCallback, 945 (void *)aacDec); 946 947 /* Register Free Memory callback. */ 948 transportDec_RegisterFreeMemCallback(pIn, aacDecoder_FreeMemCallback, 949 (void *)aacDec); 950 951 /* Register config switch control callback. */ 952 transportDec_RegisterCtrlCFGChangeCallback( 953 pIn, aacDecoder_CtrlCFGChangeCallback, (void *)aacDec); 954 955 FDKmemclear(&aacDec->qmfDomain, sizeof(FDK_QMF_DOMAIN)); 956 /* open SBR decoder */ 957 if (SBRDEC_OK != sbrDecoder_Open(&aacDec->hSbrDecoder, &aacDec->qmfDomain)) { 958 err = -1; 959 goto bail; 960 } 961 aacDec->qmfModeUser = NOT_DEFINED; 962 transportDec_RegisterSbrCallback(aacDec->hInput, (cbSbr_t)sbrDecoder_Header, 963 (void *)aacDec->hSbrDecoder); 964 965 if (mpegSurroundDecoder_Open( 966 (CMpegSurroundDecoder **)&aacDec->pMpegSurroundDecoder, 967 stereoConfigIndex, &aacDec->qmfDomain)) { 968 err = -1; 969 goto bail; 970 } 971 /* Set MPEG Surround defaults */ 972 aacDec->mpsEnableUser = 0; 973 aacDec->mpsEnableCurr = 0; 974 aacDec->mpsApplicable = 0; 975 aacDec->mpsOutputMode = (SCHAR)SACDEC_OUT_MODE_NORMAL; 976 transportDec_RegisterSscCallback(pIn, aacDecoder_SscCallback, (void *)aacDec); 977 978 { 979 if (FDK_drcDec_Open(&(aacDec->hUniDrcDecoder), DRC_DEC_ALL) != 0) { 980 err = -1; 981 goto bail; 982 } 983 } 984 985 transportDec_RegisterUniDrcConfigCallback(pIn, aacDecoder_UniDrcCallback, 986 (void *)aacDec, 987 aacDec->loudnessInfoSetPosition); 988 aacDec->defaultTargetLoudness = (SCHAR)96; 989 990 pcmDmx_Open(&aacDec->hPcmUtils); 991 if (aacDec->hPcmUtils == NULL) { 992 err = -1; 993 goto bail; 994 } 995 996 aacDec->hLimiter = 997 pcmLimiter_Create(TDL_ATTACK_DEFAULT_MS, TDL_RELEASE_DEFAULT_MS, 998 (FIXP_DBL)MAXVAL_DBL, (8), 96000); 999 if (NULL == aacDec->hLimiter) { 1000 err = -1; 1001 goto bail; 1002 } 1003 aacDec->limiterEnableUser = (UCHAR)-1; 1004 aacDec->limiterEnableCurr = 0; 1005 1006 /* Assure that all modules have same delay */ 1007 if (setConcealMethod(aacDec, 1008 CConcealment_GetMethod(&aacDec->concealCommonData))) { 1009 err = -1; 1010 goto bail; 1011 } 1012 1013 bail: 1014 if (err == -1) { 1015 aacDecoder_Close(aacDec); 1016 aacDec = NULL; 1017 } 1018 return aacDec; 1019 } 1020 1021 LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_Fill(HANDLE_AACDECODER self, 1022 UCHAR *pBuffer[], 1023 const UINT bufferSize[], 1024 UINT *pBytesValid) { 1025 TRANSPORTDEC_ERROR tpErr; 1026 /* loop counter for layers; if not TT_MP4_RAWPACKETS used as index for only 1027 available layer */ 1028 INT layer = 0; 1029 INT nrOfLayers = self->nrOfLayers; 1030 1031 { 1032 for (layer = 0; layer < nrOfLayers; layer++) { 1033 { 1034 tpErr = transportDec_FillData(self->hInput, pBuffer[layer], 1035 bufferSize[layer], &pBytesValid[layer], 1036 layer); 1037 if (tpErr != TRANSPORTDEC_OK) { 1038 return AAC_DEC_UNKNOWN; /* Must be an internal error */ 1039 } 1040 } 1041 } 1042 } 1043 1044 return AAC_DEC_OK; 1045 } 1046 1047 static void aacDecoder_SignalInterruption(HANDLE_AACDECODER self) { 1048 CAacDecoder_SignalInterruption(self); 1049 1050 if (self->hSbrDecoder != NULL) { 1051 sbrDecoder_SetParam(self->hSbrDecoder, SBR_BS_INTERRUPTION, 1); 1052 } 1053 if (self->mpsEnableUser) { 1054 mpegSurroundDecoder_SetParam( 1055 (CMpegSurroundDecoder *)self->pMpegSurroundDecoder, 1056 SACDEC_BS_INTERRUPTION, 1); 1057 } 1058 } 1059 1060 static void aacDecoder_UpdateBitStreamCounters(CStreamInfo *pSi, 1061 HANDLE_FDK_BITSTREAM hBs, 1062 INT nBits, 1063 AAC_DECODER_ERROR ErrorStatus) { 1064 /* calculate bit difference (amount of bits moved forward) */ 1065 nBits = nBits - (INT)FDKgetValidBits(hBs); 1066 1067 /* Note: The amount of bits consumed might become negative when parsing a 1068 bit stream with several sub frames, and we find out at the last sub frame 1069 that the total frame length does not match the sum of sub frame length. 1070 If this happens, the transport decoder might want to rewind to the supposed 1071 ending of the transport frame, and this position might be before the last 1072 access unit beginning. */ 1073 1074 /* Calc bitrate. */ 1075 if (pSi->frameSize > 0) { 1076 /* bitRate = nBits * sampleRate / frameSize */ 1077 int ratio_e = 0; 1078 FIXP_DBL ratio_m = fDivNorm(pSi->sampleRate, pSi->frameSize, &ratio_e); 1079 pSi->bitRate = (INT)fMultNorm(nBits, DFRACT_BITS - 1, ratio_m, ratio_e, 1080 DFRACT_BITS - 1); 1081 } 1082 1083 /* bit/byte counters */ 1084 { 1085 INT nBytes; 1086 1087 nBytes = nBits >> 3; 1088 pSi->numTotalBytes += nBytes; 1089 if (IS_OUTPUT_VALID(ErrorStatus)) { 1090 pSi->numTotalAccessUnits++; 1091 } 1092 if (IS_DECODE_ERROR(ErrorStatus)) { 1093 pSi->numBadBytes += nBytes; 1094 pSi->numBadAccessUnits++; 1095 } 1096 } 1097 } 1098 1099 static INT aacDecoder_EstimateNumberOfLostFrames(HANDLE_AACDECODER self) { 1100 INT n; 1101 1102 transportDec_GetMissingAccessUnitCount(&n, self->hInput); 1103 1104 return n; 1105 } 1106 1107 LINKSPEC_CPP AAC_DECODER_ERROR 1108 aacDecoder_DecodeFrame(HANDLE_AACDECODER self, INT_PCM *pTimeData_extern, 1109 const INT timeDataSize_extern, const UINT flags) { 1110 AAC_DECODER_ERROR ErrorStatus; 1111 INT layer; 1112 INT nBits; 1113 HANDLE_FDK_BITSTREAM hBs; 1114 int fTpInterruption = 0; /* Transport originated interruption detection. */ 1115 int fTpConceal = 0; /* Transport originated concealment. */ 1116 INT_PCM *pTimeData = NULL; 1117 INT timeDataSize = 0; 1118 UINT accessUnit = 0; 1119 UINT numAccessUnits = 1; 1120 UINT numPrerollAU = 0; 1121 int fEndAuNotAdjusted = 0; /* The end of the access unit was not adjusted */ 1122 int applyCrossfade = 1; /* flag indicates if flushing was possible */ 1123 FIXP_PCM *pTimeDataFixpPcm; /* Signal buffer for decoding process before PCM 1124 processing */ 1125 INT timeDataFixpPcmSize; 1126 PCM_DEC *pTimeDataPcmPost; /* Signal buffer for PCM post-processing */ 1127 INT timeDataPcmPostSize; 1128 1129 if (self == NULL) { 1130 return AAC_DEC_INVALID_HANDLE; 1131 } 1132 1133 pTimeData = self->pcmOutputBuffer; 1134 timeDataSize = sizeof(self->pcmOutputBuffer) / sizeof(*self->pcmOutputBuffer); 1135 1136 if (flags & AACDEC_INTR) { 1137 self->streamInfo.numLostAccessUnits = 0; 1138 } 1139 hBs = transportDec_GetBitstream(self->hInput, 0); 1140 1141 /* Get current bits position for bitrate calculation. */ 1142 nBits = FDKgetValidBits(hBs); 1143 1144 if (flags & AACDEC_CLRHIST) { 1145 if (self->flags[0] & AC_USAC) { 1146 /* 1) store AudioSpecificConfig always in AudioSpecificConfig_Parse() */ 1147 /* 2) free memory of dynamic allocated data */ 1148 CSAudioSpecificConfig asc; 1149 transportDec_GetAsc(self->hInput, 0, &asc); 1150 aacDecoder_FreeMemCallback(self, &asc); 1151 self->streamInfo.numChannels = 0; 1152 /* 3) restore AudioSpecificConfig */ 1153 transportDec_OutOfBandConfig(self->hInput, asc.config, 1154 (asc.configBits + 7) >> 3, 0); 1155 } 1156 } 1157 1158 if (!((flags & (AACDEC_CONCEAL | AACDEC_FLUSH)) || 1159 (self->flushStatus == AACDEC_RSV60_DASH_IPF_ATSC_FLUSH_ON) || 1160 (self->flushStatus == AACDEC_USAC_DASH_IPF_FLUSH_ON) || 1161 (self->buildUpStatus == AACDEC_RSV60_BUILD_UP_IDLE_IN_BAND))) { 1162 TRANSPORTDEC_ERROR err; 1163 1164 for (layer = 0; layer < self->nrOfLayers; layer++) { 1165 err = transportDec_ReadAccessUnit(self->hInput, layer); 1166 if (err != TRANSPORTDEC_OK) { 1167 switch (err) { 1168 case TRANSPORTDEC_NOT_ENOUGH_BITS: 1169 ErrorStatus = AAC_DEC_NOT_ENOUGH_BITS; 1170 goto bail; 1171 case TRANSPORTDEC_SYNC_ERROR: 1172 self->streamInfo.numLostAccessUnits = 1173 aacDecoder_EstimateNumberOfLostFrames(self); 1174 fTpInterruption = 1; 1175 break; 1176 case TRANSPORTDEC_NEED_TO_RESTART: 1177 ErrorStatus = AAC_DEC_NEED_TO_RESTART; 1178 goto bail; 1179 case TRANSPORTDEC_CRC_ERROR: 1180 fTpConceal = 1; 1181 break; 1182 case TRANSPORTDEC_UNSUPPORTED_FORMAT: 1183 ErrorStatus = AAC_DEC_UNSUPPORTED_FORMAT; 1184 goto bail; 1185 default: 1186 ErrorStatus = AAC_DEC_UNKNOWN; 1187 goto bail; 1188 } 1189 } 1190 } 1191 } else { 1192 if (self->streamInfo.numLostAccessUnits > 0) { 1193 self->streamInfo.numLostAccessUnits--; 1194 } 1195 } 1196 1197 self->frameOK = 1; 1198 1199 UINT prerollAUOffset[AACDEC_MAX_NUM_PREROLL_AU]; 1200 UINT prerollAULength[AACDEC_MAX_NUM_PREROLL_AU]; 1201 for (int i = 0; i < AACDEC_MAX_NUM_PREROLL_AU + 1; i++) 1202 self->prerollAULength[i] = 0; 1203 1204 INT auStartAnchor; 1205 HANDLE_FDK_BITSTREAM hBsAu; 1206 1207 /* Process preroll frames and current frame */ 1208 do { 1209 if (!(flags & (AACDEC_CONCEAL | AACDEC_FLUSH)) && 1210 (self->flushStatus != AACDEC_RSV60_CFG_CHANGE_ATSC_FLUSH_ON) && 1211 (accessUnit == 0) && 1212 (self->hasAudioPreRoll || 1213 (self->buildUpStatus == AACDEC_RSV60_BUILD_UP_IDLE_IN_BAND)) && 1214 !fTpInterruption && 1215 !fTpConceal /* Bit stream pointer needs to be at the beginning of a 1216 (valid) AU. */ 1217 ) { 1218 ErrorStatus = CAacDecoder_PreRollExtensionPayloadParse( 1219 self, &numPrerollAU, prerollAUOffset, prerollAULength); 1220 1221 if (ErrorStatus != AAC_DEC_OK) { 1222 switch (ErrorStatus) { 1223 case AAC_DEC_NOT_ENOUGH_BITS: 1224 goto bail; 1225 case AAC_DEC_PARSE_ERROR: 1226 self->frameOK = 0; 1227 break; 1228 default: 1229 break; 1230 } 1231 } 1232 1233 numAccessUnits += numPrerollAU; 1234 } 1235 1236 hBsAu = transportDec_GetBitstream(self->hInput, 0); 1237 auStartAnchor = (INT)FDKgetValidBits(hBsAu); 1238 1239 self->accessUnit = accessUnit; 1240 if (accessUnit < numPrerollAU) { 1241 FDKpushFor(hBsAu, prerollAUOffset[accessUnit]); 1242 } 1243 1244 /* Signal bit stream interruption to other modules if required. */ 1245 if (fTpInterruption || (flags & AACDEC_INTR)) { 1246 aacDecoder_SignalInterruption(self); 1247 if (!(flags & AACDEC_INTR)) { 1248 ErrorStatus = AAC_DEC_TRANSPORT_SYNC_ERROR; 1249 goto bail; 1250 } 1251 } 1252 1253 /* Clearing core data will be done in CAacDecoder_DecodeFrame() below. 1254 Tell other modules to clear states if required. */ 1255 if (flags & AACDEC_CLRHIST) { 1256 if (!(self->flags[0] & AC_USAC)) { 1257 sbrDecoder_SetParam(self->hSbrDecoder, SBR_CLEAR_HISTORY, 1); 1258 mpegSurroundDecoder_SetParam( 1259 (CMpegSurroundDecoder *)self->pMpegSurroundDecoder, 1260 SACDEC_CLEAR_HISTORY, 1); 1261 if (FDK_QmfDomain_ClearPersistentMemory(&self->qmfDomain) != 0) { 1262 ErrorStatus = AAC_DEC_UNKNOWN; 1263 goto bail; 1264 } 1265 } 1266 } 1267 1268 /* Empty bit buffer in case of flush request. */ 1269 if (flags & AACDEC_FLUSH && !(flags & AACDEC_CONCEAL)) { 1270 if (!self->flushStatus) { 1271 transportDec_SetParam(self->hInput, TPDEC_PARAM_RESET, 1); 1272 self->streamInfo.numLostAccessUnits = 0; 1273 self->streamInfo.numBadBytes = 0; 1274 self->streamInfo.numTotalBytes = 0; 1275 } 1276 } 1277 /* Reset the output delay field. The modules will add their figures one 1278 * after another. */ 1279 self->streamInfo.outputDelay = 0; 1280 1281 if (self->limiterEnableUser == (UCHAR)-2) { 1282 /* Enable limiter only for RSVD60. */ 1283 self->limiterEnableCurr = (self->flags[0] & AC_RSV603DA) ? 1 : 0; 1284 } else if (self->limiterEnableUser == (UCHAR)-1) { 1285 /* Enable limiter for all non-lowdelay AOT's. */ 1286 self->limiterEnableCurr = (self->flags[0] & (AC_LD | AC_ELD)) ? 0 : 1; 1287 } else { 1288 /* Use limiter configuration as requested. */ 1289 self->limiterEnableCurr = self->limiterEnableUser; 1290 } 1291 /* reset limiter gain on a per frame basis */ 1292 self->extGain[0] = FL2FXCONST_DBL(1.0f / (float)(1 << TDL_GAIN_SCALING)); 1293 1294 pTimeDataFixpPcm = pTimeData; 1295 timeDataFixpPcmSize = timeDataSize; 1296 1297 ErrorStatus = CAacDecoder_DecodeFrame( 1298 self, 1299 flags | (fTpConceal ? AACDEC_CONCEAL : 0) | 1300 ((self->flushStatus && !(flags & AACDEC_CONCEAL)) ? AACDEC_FLUSH 1301 : 0), 1302 pTimeDataFixpPcm + 0, timeDataFixpPcmSize, 1303 self->streamInfo.aacSamplesPerFrame + 0); 1304 1305 /* if flushing for USAC DASH IPF was not possible go on with decoding 1306 * preroll */ 1307 if ((self->flags[0] & AC_USAC) && 1308 (self->flushStatus == AACDEC_USAC_DASH_IPF_FLUSH_ON) && 1309 !(flags & AACDEC_CONCEAL) && (ErrorStatus != AAC_DEC_OK)) { 1310 applyCrossfade = 0; 1311 } else /* USAC DASH IPF flushing possible begin */ 1312 { 1313 if (!((flags & (AACDEC_CONCEAL | AACDEC_FLUSH)) || fTpConceal || 1314 self->flushStatus) && 1315 (!(IS_OUTPUT_VALID(ErrorStatus)) || !(accessUnit < numPrerollAU))) { 1316 TRANSPORTDEC_ERROR tpErr; 1317 tpErr = transportDec_EndAccessUnit(self->hInput); 1318 if (tpErr != TRANSPORTDEC_OK) { 1319 self->frameOK = 0; 1320 } 1321 } else { /* while preroll processing later possibly an error in the 1322 renderer part occurrs */ 1323 if (IS_OUTPUT_VALID(ErrorStatus)) { 1324 fEndAuNotAdjusted = 1; 1325 } 1326 } 1327 1328 /* If the current pTimeDataFixpPcm does not contain a valid signal, there 1329 * nothing else we can do, so bail. */ 1330 if (!IS_OUTPUT_VALID(ErrorStatus)) { 1331 goto bail; 1332 } 1333 1334 { 1335 self->streamInfo.sampleRate = self->streamInfo.aacSampleRate; 1336 self->streamInfo.frameSize = self->streamInfo.aacSamplesPerFrame; 1337 } 1338 1339 self->streamInfo.numChannels = self->streamInfo.aacNumChannels; 1340 1341 { 1342 FDK_Delay_Apply(&self->usacResidualDelay, 1343 pTimeDataFixpPcm + 1344 1 * (self->streamInfo.aacSamplesPerFrame + 0) + 0, 1345 self->streamInfo.frameSize, 0); 1346 } 1347 1348 /* Setting of internal MPS state; may be reset in CAacDecoder_SyncQmfMode 1349 if decoder is unable to decode with user defined qmfMode */ 1350 if (!(self->flags[0] & (AC_USAC | AC_RSVD50 | AC_RSV603DA | AC_ELD))) { 1351 self->mpsEnableCurr = 1352 (self->mpsEnableUser && 1353 isSupportedMpsConfig(self->streamInfo.aot, 1354 self->streamInfo.numChannels, 1355 (self->flags[0] & AC_MPS_PRESENT) ? 1 : 0)); 1356 } 1357 1358 if (!self->qmfDomain.globalConf.qmfDomainExplicitConfig && 1359 self->mpsEnableCurr) { 1360 /* if not done yet, allocate full MPEG Surround decoder instance */ 1361 if (mpegSurroundDecoder_IsFullMpegSurroundDecoderInstanceAvailable( 1362 (CMpegSurroundDecoder *)self->pMpegSurroundDecoder) == 1363 SAC_INSTANCE_NOT_FULL_AVAILABLE) { 1364 if (mpegSurroundDecoder_Open( 1365 (CMpegSurroundDecoder **)&self->pMpegSurroundDecoder, -1, 1366 &self->qmfDomain)) { 1367 return AAC_DEC_OUT_OF_MEMORY; 1368 } 1369 } 1370 } 1371 1372 CAacDecoder_SyncQmfMode(self); 1373 1374 if (!self->qmfDomain.globalConf.qmfDomainExplicitConfig && 1375 self->mpsEnableCurr) { 1376 SAC_INPUT_CONFIG sac_interface = (self->sbrEnabled && self->hSbrDecoder) 1377 ? SAC_INTERFACE_QMF 1378 : SAC_INTERFACE_TIME; 1379 /* needs to be done before first SBR apply. */ 1380 mpegSurroundDecoder_ConfigureQmfDomain( 1381 (CMpegSurroundDecoder *)self->pMpegSurroundDecoder, sac_interface, 1382 (UINT)self->streamInfo.aacSampleRate, self->streamInfo.aot); 1383 self->qmfDomain.globalConf.nQmfTimeSlots_requested = 1384 self->streamInfo.aacSamplesPerFrame / 1385 self->qmfDomain.globalConf.nBandsAnalysis_requested; 1386 } 1387 1388 self->qmfDomain.globalConf.TDinput = pTimeData; 1389 1390 switch (FDK_QmfDomain_Configure(&self->qmfDomain)) { 1391 default: 1392 case QMF_DOMAIN_INIT_ERROR: 1393 ErrorStatus = AAC_DEC_UNKNOWN; 1394 goto bail; 1395 case QMF_DOMAIN_OUT_OF_MEMORY: 1396 ErrorStatus = AAC_DEC_OUT_OF_MEMORY; 1397 goto bail; 1398 case QMF_DOMAIN_OK: 1399 break; 1400 } 1401 1402 /* sbr decoder */ 1403 1404 if ((ErrorStatus != AAC_DEC_OK) || (flags & AACDEC_CONCEAL) || 1405 self->pAacDecoderStaticChannelInfo[0]->concealmentInfo.concealState > 1406 ConcealState_FadeIn) { 1407 self->frameOK = 0; /* if an error has occured do concealment in the SBR 1408 decoder too */ 1409 } 1410 1411 if (self->sbrEnabled && (!(self->flags[0] & AC_USAC_SCFGI3))) { 1412 SBR_ERROR sbrError = SBRDEC_OK; 1413 int chIdx, numCoreChannel = self->streamInfo.numChannels; 1414 1415 /* set params */ 1416 sbrDecoder_SetParam(self->hSbrDecoder, SBR_SYSTEM_BITSTREAM_DELAY, 1417 self->sbrParams.bsDelay); 1418 sbrDecoder_SetParam( 1419 self->hSbrDecoder, SBR_FLUSH_DATA, 1420 (flags & AACDEC_FLUSH) | 1421 ((self->flushStatus && !(flags & AACDEC_CONCEAL)) ? AACDEC_FLUSH 1422 : 0)); 1423 1424 if (self->streamInfo.aot == AOT_ER_AAC_ELD) { 1425 /* Configure QMF */ 1426 sbrDecoder_SetParam(self->hSbrDecoder, SBR_LD_QMF_TIME_ALIGN, 1427 (self->flags[0] & AC_MPS_PRESENT) ? 1 : 0); 1428 } 1429 1430 { 1431 PCMDMX_ERROR dmxErr; 1432 INT maxOutCh = 0; 1433 1434 dmxErr = pcmDmx_GetParam(self->hPcmUtils, 1435 MAX_NUMBER_OF_OUTPUT_CHANNELS, &maxOutCh); 1436 if ((dmxErr == PCMDMX_OK) && (maxOutCh == 1)) { 1437 /* Disable PS processing if we have to create a mono output signal. 1438 */ 1439 self->psPossible = 0; 1440 } 1441 } 1442 1443 sbrDecoder_SetParam(self->hSbrDecoder, SBR_SKIP_QMF, 1444 (self->mpsEnableCurr) ? 2 : 0); 1445 1446 INT_PCM *input; 1447 input = (INT_PCM *)self->workBufferCore2; 1448 FDKmemcpy(input, pTimeData, 1449 sizeof(INT_PCM) * (self->streamInfo.numChannels) * 1450 (self->streamInfo.frameSize)); 1451 1452 /* apply SBR processing */ 1453 sbrError = sbrDecoder_Apply(self->hSbrDecoder, input, pTimeData, 1454 timeDataSize, &self->streamInfo.numChannels, 1455 &self->streamInfo.sampleRate, 1456 &self->mapDescr, self->chMapIndex, 1457 self->frameOK, &self->psPossible); 1458 1459 if (sbrError == SBRDEC_OK) { 1460 /* Update data in streaminfo structure. Assume that the SBR upsampling 1461 factor is either 1, 2, 8/3 or 4. Maximum upsampling factor is 4 1462 (CELP+SBR or USAC 4:1 SBR) */ 1463 self->flags[0] |= AC_SBR_PRESENT; 1464 if (self->streamInfo.aacSampleRate != self->streamInfo.sampleRate) { 1465 if (self->streamInfo.aacSampleRate >> 2 == 1466 self->streamInfo.sampleRate) { 1467 self->streamInfo.frameSize = 1468 self->streamInfo.aacSamplesPerFrame >> 2; 1469 self->streamInfo.outputDelay = self->streamInfo.outputDelay >> 2; 1470 } else if (self->streamInfo.aacSampleRate >> 1 == 1471 self->streamInfo.sampleRate) { 1472 self->streamInfo.frameSize = 1473 self->streamInfo.aacSamplesPerFrame >> 1; 1474 self->streamInfo.outputDelay = self->streamInfo.outputDelay >> 1; 1475 } else if (self->streamInfo.aacSampleRate << 1 == 1476 self->streamInfo.sampleRate) { 1477 self->streamInfo.frameSize = self->streamInfo.aacSamplesPerFrame 1478 << 1; 1479 self->streamInfo.outputDelay = self->streamInfo.outputDelay << 1; 1480 } else if (self->streamInfo.aacSampleRate << 2 == 1481 self->streamInfo.sampleRate) { 1482 self->streamInfo.frameSize = self->streamInfo.aacSamplesPerFrame 1483 << 2; 1484 self->streamInfo.outputDelay = self->streamInfo.outputDelay << 2; 1485 } else if (self->streamInfo.frameSize == 768) { 1486 self->streamInfo.frameSize = 1487 (self->streamInfo.aacSamplesPerFrame << 3) / 3; 1488 self->streamInfo.outputDelay = 1489 (self->streamInfo.outputDelay << 3) / 3; 1490 } else { 1491 ErrorStatus = AAC_DEC_SET_PARAM_FAIL; 1492 goto bail; 1493 } 1494 } else { 1495 self->streamInfo.frameSize = self->streamInfo.aacSamplesPerFrame; 1496 } 1497 self->streamInfo.outputDelay += 1498 sbrDecoder_GetDelay(self->hSbrDecoder); 1499 1500 if (self->psPossible) { 1501 self->flags[0] |= AC_PS_PRESENT; 1502 } 1503 for (chIdx = numCoreChannel; chIdx < self->streamInfo.numChannels; 1504 chIdx += 1) { 1505 self->channelType[chIdx] = ACT_FRONT; 1506 self->channelIndices[chIdx] = chIdx; 1507 } 1508 } 1509 if (sbrError == SBRDEC_OUTPUT_BUFFER_TOO_SMALL) { 1510 ErrorStatus = AAC_DEC_OUTPUT_BUFFER_TOO_SMALL; 1511 goto bail; 1512 } 1513 } 1514 1515 if (self->mpsEnableCurr) { 1516 int err, sac_interface, nChannels, frameSize; 1517 1518 nChannels = self->streamInfo.numChannels; 1519 frameSize = self->streamInfo.frameSize; 1520 sac_interface = SAC_INTERFACE_TIME; 1521 1522 if (self->sbrEnabled && self->hSbrDecoder) 1523 sac_interface = SAC_INTERFACE_QMF; 1524 if (self->streamInfo.aot == AOT_USAC) { 1525 if (self->flags[0] & AC_USAC_SCFGI3) { 1526 sac_interface = SAC_INTERFACE_TIME; 1527 } 1528 } 1529 err = mpegSurroundDecoder_SetParam( 1530 (CMpegSurroundDecoder *)self->pMpegSurroundDecoder, 1531 SACDEC_INTERFACE, sac_interface); 1532 1533 if (err == 0) { 1534 err = mpegSurroundDecoder_Apply( 1535 (CMpegSurroundDecoder *)self->pMpegSurroundDecoder, 1536 (INT_PCM *)self->workBufferCore2, pTimeData, timeDataSize, 1537 self->streamInfo.aacSamplesPerFrame, &nChannels, &frameSize, 1538 self->streamInfo.sampleRate, self->streamInfo.aot, 1539 self->channelType, self->channelIndices, &self->mapDescr); 1540 } 1541 1542 if (err == MPS_OUTPUT_BUFFER_TOO_SMALL) { 1543 ErrorStatus = AAC_DEC_OUTPUT_BUFFER_TOO_SMALL; 1544 goto bail; 1545 } 1546 if (err == 0) { 1547 /* Update output parameter */ 1548 self->streamInfo.numChannels = nChannels; 1549 self->streamInfo.frameSize = frameSize; 1550 self->streamInfo.outputDelay += mpegSurroundDecoder_GetDelay( 1551 (CMpegSurroundDecoder *)self->pMpegSurroundDecoder); 1552 /* Save current parameter for possible concealment of next frame */ 1553 self->mpsOutChannelsLast = nChannels; 1554 self->mpsFrameSizeLast = frameSize; 1555 } else if ((self->mpsOutChannelsLast > 0) && 1556 (self->mpsFrameSizeLast > 0)) { 1557 /* Restore parameters of last frame ... */ 1558 self->streamInfo.numChannels = self->mpsOutChannelsLast; 1559 self->streamInfo.frameSize = self->mpsFrameSizeLast; 1560 /* ... and clear output buffer so that potentially corrupted data does 1561 * not reach the framework. */ 1562 FDKmemclear(pTimeData, self->mpsOutChannelsLast * 1563 self->mpsFrameSizeLast * sizeof(INT_PCM)); 1564 /* Additionally proclaim that this frame had errors during decoding. 1565 */ 1566 ErrorStatus = AAC_DEC_DECODE_FRAME_ERROR; 1567 } else { 1568 ErrorStatus = AAC_DEC_UNKNOWN; /* no output */ 1569 } 1570 } 1571 1572 /* SBR decoder for Unified Stereo Config (stereoConfigIndex == 3) */ 1573 1574 if (self->sbrEnabled && (self->flags[0] & AC_USAC_SCFGI3)) { 1575 SBR_ERROR sbrError = SBRDEC_OK; 1576 1577 /* set params */ 1578 sbrDecoder_SetParam(self->hSbrDecoder, SBR_SYSTEM_BITSTREAM_DELAY, 1579 self->sbrParams.bsDelay); 1580 1581 sbrDecoder_SetParam(self->hSbrDecoder, SBR_SKIP_QMF, 1); 1582 1583 /* apply SBR processing */ 1584 sbrError = sbrDecoder_Apply(self->hSbrDecoder, pTimeData, pTimeData, 1585 timeDataSize, &self->streamInfo.numChannels, 1586 &self->streamInfo.sampleRate, 1587 &self->mapDescr, self->chMapIndex, 1588 self->frameOK, &self->psPossible); 1589 1590 if (sbrError == SBRDEC_OK) { 1591 /* Update data in streaminfo structure. Assume that the SBR upsampling 1592 * factor is either 1,2 or 4 */ 1593 self->flags[0] |= AC_SBR_PRESENT; 1594 if (self->streamInfo.aacSampleRate != self->streamInfo.sampleRate) { 1595 if (self->streamInfo.frameSize == 768) { 1596 self->streamInfo.frameSize = 1597 (self->streamInfo.aacSamplesPerFrame * 8) / 3; 1598 } else if (self->streamInfo.aacSampleRate << 2 == 1599 self->streamInfo.sampleRate) { 1600 self->streamInfo.frameSize = self->streamInfo.aacSamplesPerFrame 1601 << 2; 1602 } else { 1603 self->streamInfo.frameSize = self->streamInfo.aacSamplesPerFrame 1604 << 1; 1605 } 1606 } 1607 1608 self->flags[0] &= ~AC_PS_PRESENT; 1609 } 1610 if (sbrError == SBRDEC_OUTPUT_BUFFER_TOO_SMALL) { 1611 ErrorStatus = AAC_DEC_OUTPUT_BUFFER_TOO_SMALL; 1612 goto bail; 1613 } 1614 } 1615 1616 /* Use dedicated memory for PCM postprocessing */ 1617 pTimeDataPcmPost = self->pTimeData2; 1618 timeDataPcmPostSize = self->timeData2Size; 1619 1620 { 1621 const int size = 1622 self->streamInfo.frameSize * self->streamInfo.numChannels; 1623 FDK_ASSERT(timeDataPcmPostSize >= size); 1624 for (int i = 0; i < size; i++) { 1625 pTimeDataPcmPost[i] = 1626 (PCM_DEC)FX_PCM2PCM_DEC(pTimeData[i]) >> PCM_OUT_HEADROOM; 1627 } 1628 } 1629 1630 { 1631 if ((FDK_drcDec_GetParam(self->hUniDrcDecoder, DRC_DEC_IS_ACTIVE)) && 1632 !(self->flags[0] & AC_RSV603DA)) { 1633 /* Apply DRC gains*/ 1634 int ch, drcDelay = 0; 1635 int needsDeinterleaving = 0; 1636 FIXP_DBL *drcWorkBuffer = NULL; 1637 FIXP_DBL channelGain[(8)]; 1638 int reverseInChannelMap[(8)]; 1639 int reverseOutChannelMap[(8)]; 1640 int numDrcOutChannels = FDK_drcDec_GetParam( 1641 self->hUniDrcDecoder, DRC_DEC_TARGET_CHANNEL_COUNT_SELECTED); 1642 FDKmemclear(channelGain, sizeof(channelGain)); 1643 for (ch = 0; ch < (8); ch++) { 1644 reverseInChannelMap[ch] = ch; 1645 reverseOutChannelMap[ch] = ch; 1646 } 1647 1648 /* If SBR and/or MPS is active, the DRC gains are aligned to the QMF 1649 domain signal before the QMF synthesis. Therefore the DRC gains 1650 need to be delayed by the QMF synthesis delay. */ 1651 if (self->sbrEnabled) drcDelay = 257; 1652 if (self->mpsEnableCurr) drcDelay = 257; 1653 /* Take into account concealment delay */ 1654 drcDelay += CConcealment_GetDelay(&self->concealCommonData) * 1655 self->streamInfo.frameSize; 1656 1657 for (ch = 0; ch < self->streamInfo.numChannels; ch++) { 1658 UCHAR mapValue = FDK_chMapDescr_getMapValue( 1659 &self->mapDescr, (UCHAR)ch, self->chMapIndex); 1660 if (mapValue < (8)) reverseInChannelMap[mapValue] = ch; 1661 } 1662 for (ch = 0; ch < (int)numDrcOutChannels; ch++) { 1663 UCHAR mapValue = FDK_chMapDescr_getMapValue( 1664 &self->mapDescr, (UCHAR)ch, numDrcOutChannels); 1665 if (mapValue < (8)) reverseOutChannelMap[mapValue] = ch; 1666 } 1667 1668 /* The output of SBR and MPS is interleaved. Deinterleaving may be 1669 * necessary for FDK_drcDec_ProcessTime, which accepts deinterleaved 1670 * audio only. */ 1671 if ((self->streamInfo.numChannels > 1) && 1672 (0 || (self->sbrEnabled) || (self->mpsEnableCurr))) { 1673 /* interleaving/deinterleaving is performed on upper part of 1674 * pTimeDataPcmPost. Check if this buffer is large enough. */ 1675 if (timeDataPcmPostSize < 1676 (INT)(2 * self->streamInfo.numChannels * 1677 self->streamInfo.frameSize * sizeof(PCM_DEC))) { 1678 ErrorStatus = AAC_DEC_UNKNOWN; 1679 goto bail; 1680 } 1681 needsDeinterleaving = 1; 1682 drcWorkBuffer = 1683 (FIXP_DBL *)pTimeDataPcmPost + 1684 self->streamInfo.numChannels * self->streamInfo.frameSize; 1685 FDK_deinterleave( 1686 pTimeDataPcmPost, drcWorkBuffer, self->streamInfo.numChannels, 1687 self->streamInfo.frameSize, self->streamInfo.frameSize); 1688 } else { 1689 drcWorkBuffer = (FIXP_DBL *)pTimeDataPcmPost; 1690 } 1691 1692 /* prepare Loudness Normalisation gain */ 1693 FDK_drcDec_SetParam(self->hUniDrcDecoder, DRC_DEC_TARGET_LOUDNESS, 1694 (INT)-self->defaultTargetLoudness * 1695 FL2FXCONST_DBL(1.0f / (float)(1 << 9))); 1696 FDK_drcDec_SetChannelGains(self->hUniDrcDecoder, 1697 self->streamInfo.numChannels, 1698 self->streamInfo.frameSize, channelGain, 1699 drcWorkBuffer, self->streamInfo.frameSize); 1700 FDK_drcDec_Preprocess(self->hUniDrcDecoder); 1701 1702 /* apply DRC1 gain sequence */ 1703 for (ch = 0; ch < self->streamInfo.numChannels; ch++) { 1704 FDK_drcDec_ProcessTime(self->hUniDrcDecoder, drcDelay, DRC_DEC_DRC1, 1705 ch, reverseInChannelMap[ch] - ch, 1, 1706 drcWorkBuffer, self->streamInfo.frameSize); 1707 } 1708 /* apply downmix */ 1709 FDK_drcDec_ApplyDownmix( 1710 self->hUniDrcDecoder, reverseInChannelMap, reverseOutChannelMap, 1711 drcWorkBuffer, 1712 &self->streamInfo.numChannels); /* self->streamInfo.numChannels 1713 may change here */ 1714 /* apply DRC2/3 gain sequence */ 1715 for (ch = 0; ch < self->streamInfo.numChannels; ch++) { 1716 FDK_drcDec_ProcessTime(self->hUniDrcDecoder, drcDelay, 1717 DRC_DEC_DRC2_DRC3, ch, 1718 reverseOutChannelMap[ch] - ch, 1, 1719 drcWorkBuffer, self->streamInfo.frameSize); 1720 } 1721 1722 if (needsDeinterleaving) { 1723 FDK_interleave( 1724 drcWorkBuffer, pTimeDataPcmPost, self->streamInfo.numChannels, 1725 self->streamInfo.frameSize, self->streamInfo.frameSize); 1726 } 1727 } 1728 } 1729 1730 if (self->streamInfo.extAot != AOT_AAC_SLS) { 1731 INT pcmLimiterScale = 0; 1732 PCMDMX_ERROR dmxErr = PCMDMX_OK; 1733 if (flags & (AACDEC_INTR)) { 1734 /* delete data from the past (e.g. mixdown coeficients) */ 1735 pcmDmx_Reset(self->hPcmUtils, PCMDMX_RESET_BS_DATA); 1736 } 1737 if (flags & (AACDEC_CLRHIST)) { 1738 if (!(self->flags[0] & AC_USAC)) { 1739 /* delete data from the past (e.g. mixdown coeficients) */ 1740 pcmDmx_Reset(self->hPcmUtils, PCMDMX_RESET_BS_DATA); 1741 } 1742 } 1743 1744 INT interleaved = 0; 1745 interleaved |= (self->sbrEnabled) ? 1 : 0; 1746 interleaved |= (self->mpsEnableCurr) ? 1 : 0; 1747 1748 /* do PCM post processing */ 1749 dmxErr = pcmDmx_ApplyFrame( 1750 self->hPcmUtils, pTimeDataPcmPost, timeDataFixpPcmSize, 1751 self->streamInfo.frameSize, &self->streamInfo.numChannels, 1752 interleaved, self->channelType, self->channelIndices, 1753 &self->mapDescr, 1754 (self->limiterEnableCurr) ? &pcmLimiterScale : NULL); 1755 if (dmxErr == PCMDMX_OUTPUT_BUFFER_TOO_SMALL) { 1756 ErrorStatus = AAC_DEC_OUTPUT_BUFFER_TOO_SMALL; 1757 goto bail; 1758 } 1759 if ((ErrorStatus == AAC_DEC_OK) && (dmxErr == PCMDMX_INVALID_MODE)) { 1760 /* Announce the framework that the current combination of channel 1761 * configuration and downmix settings are not know to produce a 1762 * predictable behavior and thus maybe produce strange output. */ 1763 ErrorStatus = AAC_DEC_DECODE_FRAME_ERROR; 1764 } 1765 1766 if (flags & AACDEC_CLRHIST) { 1767 if (!(self->flags[0] & AC_USAC)) { 1768 /* Delete the delayed signal. */ 1769 pcmLimiter_Reset(self->hLimiter); 1770 } 1771 } 1772 1773 if (self->limiterEnableCurr) { 1774 /* use workBufferCore2 buffer for interleaving */ 1775 PCM_LIM *pInterleaveBuffer; 1776 int blockLength = self->streamInfo.frameSize; 1777 1778 /* Set actual signal parameters */ 1779 pcmLimiter_SetNChannels(self->hLimiter, self->streamInfo.numChannels); 1780 pcmLimiter_SetSampleRate(self->hLimiter, self->streamInfo.sampleRate); 1781 pcmLimiterScale += PCM_OUT_HEADROOM; 1782 1783 if ((self->streamInfo.numChannels == 1) || (self->sbrEnabled) || 1784 (self->mpsEnableCurr)) { 1785 pInterleaveBuffer = (PCM_LIM *)pTimeDataPcmPost; 1786 } else { 1787 pInterleaveBuffer = (PCM_LIM *)pTimeData; 1788 /* applyLimiter requests for interleaved data */ 1789 /* Interleave ouput buffer */ 1790 FDK_interleave(pTimeDataPcmPost, pInterleaveBuffer, 1791 self->streamInfo.numChannels, blockLength, 1792 self->streamInfo.frameSize); 1793 } 1794 1795 pcmLimiter_Apply(self->hLimiter, pInterleaveBuffer, pTimeData, 1796 self->extGain, &pcmLimiterScale, 1, 1797 self->extGainDelay, self->streamInfo.frameSize); 1798 1799 { 1800 /* Announce the additional limiter output delay */ 1801 self->streamInfo.outputDelay += pcmLimiter_GetDelay(self->hLimiter); 1802 } 1803 } else { 1804 /* If numChannels = 1 we do not need interleaving. The same applies if 1805 SBR or MPS are used, since their output is interleaved already 1806 (resampled or not) */ 1807 if ((self->streamInfo.numChannels == 1) || (self->sbrEnabled) || 1808 (self->mpsEnableCurr)) { 1809 scaleValuesSaturate( 1810 pTimeData, pTimeDataPcmPost, 1811 self->streamInfo.frameSize * self->streamInfo.numChannels, 1812 PCM_OUT_HEADROOM); 1813 1814 } else { 1815 scaleValuesSaturate( 1816 (INT_PCM *)self->workBufferCore2, pTimeDataPcmPost, 1817 self->streamInfo.frameSize * self->streamInfo.numChannels, 1818 PCM_OUT_HEADROOM); 1819 /* Interleave ouput buffer */ 1820 FDK_interleave((INT_PCM *)self->workBufferCore2, pTimeData, 1821 self->streamInfo.numChannels, 1822 self->streamInfo.frameSize, 1823 self->streamInfo.frameSize); 1824 } 1825 } 1826 } /* if (self->streamInfo.extAot != AOT_AAC_SLS)*/ 1827 1828 if (self->flags[0] & AC_USAC) { 1829 if (self->flushStatus == AACDEC_USAC_DASH_IPF_FLUSH_ON && 1830 !(flags & AACDEC_CONCEAL)) { 1831 CAacDecoder_PrepareCrossFade(pTimeData, self->pTimeDataFlush, 1832 self->streamInfo.numChannels, 1833 self->streamInfo.frameSize, 1); 1834 } 1835 1836 /* prepare crossfade buffer for fade in */ 1837 if (!applyCrossfade && self->applyCrossfade && 1838 !(flags & AACDEC_CONCEAL)) { 1839 for (int ch = 0; ch < self->streamInfo.numChannels; ch++) { 1840 for (int i = 0; i < TIME_DATA_FLUSH_SIZE; i++) { 1841 self->pTimeDataFlush[ch][i] = 0; 1842 } 1843 } 1844 applyCrossfade = 1; 1845 } 1846 1847 if (applyCrossfade && self->applyCrossfade && 1848 !(accessUnit < numPrerollAU) && 1849 (self->buildUpStatus == AACDEC_USAC_BUILD_UP_ON)) { 1850 CAacDecoder_ApplyCrossFade(pTimeData, self->pTimeDataFlush, 1851 self->streamInfo.numChannels, 1852 self->streamInfo.frameSize, 1); 1853 self->applyCrossfade = 0; 1854 } 1855 } 1856 1857 /* Signal interruption to take effect in next frame. */ 1858 if ((flags & AACDEC_FLUSH || self->flushStatus) && 1859 !(flags & AACDEC_CONCEAL)) { 1860 aacDecoder_SignalInterruption(self); 1861 } 1862 1863 /* Update externally visible copy of flags */ 1864 self->streamInfo.flags = self->flags[0]; 1865 1866 } /* USAC DASH IPF flushing possible end */ 1867 if (accessUnit < numPrerollAU) { 1868 FDKpushBack(hBsAu, auStartAnchor - FDKgetValidBits(hBsAu)); 1869 } else { 1870 if ((self->buildUpStatus == AACDEC_RSV60_BUILD_UP_ON) || 1871 (self->buildUpStatus == AACDEC_RSV60_BUILD_UP_ON_IN_BAND) || 1872 (self->buildUpStatus == AACDEC_USAC_BUILD_UP_ON)) { 1873 self->buildUpCnt--; 1874 1875 if (self->buildUpCnt < 0) { 1876 self->buildUpStatus = 0; 1877 } 1878 } 1879 1880 if (self->flags[0] & AC_USAC) { 1881 if (self->flushStatus == AACDEC_USAC_DASH_IPF_FLUSH_ON && 1882 !(flags & AACDEC_CONCEAL)) { 1883 self->streamInfo.frameSize = 0; 1884 } 1885 } 1886 } 1887 1888 if (self->flushStatus != AACDEC_USAC_DASH_IPF_FLUSH_ON) { 1889 accessUnit++; 1890 } 1891 } while ((accessUnit < numAccessUnits) || 1892 ((self->flushStatus == AACDEC_USAC_DASH_IPF_FLUSH_ON) && 1893 !(flags & AACDEC_CONCEAL))); 1894 1895 bail: 1896 1897 /* error in renderer part occurred, ErrorStatus was set to invalid output */ 1898 if (fEndAuNotAdjusted && !IS_OUTPUT_VALID(ErrorStatus) && 1899 (accessUnit < numPrerollAU)) { 1900 transportDec_EndAccessUnit(self->hInput); 1901 } 1902 1903 /* Update Statistics */ 1904 aacDecoder_UpdateBitStreamCounters(&self->streamInfo, hBs, nBits, 1905 ErrorStatus); 1906 if (((self->streamInfo.numChannels <= 0) || 1907 (self->streamInfo.frameSize <= 0) || 1908 (self->streamInfo.sampleRate <= 0)) && 1909 IS_OUTPUT_VALID(ErrorStatus)) { 1910 /* Ensure consistency of IS_OUTPUT_VALID() macro. */ 1911 ErrorStatus = AAC_DEC_UNKNOWN; 1912 } 1913 1914 /* Check whether external output buffer is large enough. */ 1915 if (timeDataSize_extern < 1916 self->streamInfo.numChannels * self->streamInfo.frameSize) { 1917 ErrorStatus = AAC_DEC_OUTPUT_BUFFER_TOO_SMALL; 1918 } 1919 1920 /* Update external output buffer. */ 1921 if (IS_OUTPUT_VALID(ErrorStatus)) { 1922 FDKmemcpy(pTimeData_extern, pTimeData, 1923 self->streamInfo.numChannels * self->streamInfo.frameSize * 1924 sizeof(*pTimeData)); 1925 } else { 1926 FDKmemclear(pTimeData_extern, 1927 timeDataSize_extern * sizeof(*pTimeData_extern)); 1928 } 1929 1930 return ErrorStatus; 1931 } 1932 1933 LINKSPEC_CPP void aacDecoder_Close(HANDLE_AACDECODER self) { 1934 if (self == NULL) return; 1935 1936 if (self->hLimiter != NULL) { 1937 pcmLimiter_Destroy(self->hLimiter); 1938 } 1939 1940 if (self->hPcmUtils != NULL) { 1941 pcmDmx_Close(&self->hPcmUtils); 1942 } 1943 1944 FDK_drcDec_Close(&self->hUniDrcDecoder); 1945 1946 if (self->pMpegSurroundDecoder != NULL) { 1947 mpegSurroundDecoder_Close( 1948 (CMpegSurroundDecoder *)self->pMpegSurroundDecoder); 1949 } 1950 1951 if (self->hSbrDecoder != NULL) { 1952 sbrDecoder_Close(&self->hSbrDecoder); 1953 } 1954 1955 if (self->hInput != NULL) { 1956 transportDec_Close(&self->hInput); 1957 } 1958 1959 CAacDecoder_Close(self); 1960 } 1961 1962 LINKSPEC_CPP CStreamInfo *aacDecoder_GetStreamInfo(HANDLE_AACDECODER self) { 1963 return CAacDecoder_GetStreamInfo(self); 1964 } 1965 1966 LINKSPEC_CPP INT aacDecoder_GetLibInfo(LIB_INFO *info) { 1967 int i; 1968 1969 if (info == NULL) { 1970 return -1; 1971 } 1972 1973 sbrDecoder_GetLibInfo(info); 1974 mpegSurroundDecoder_GetLibInfo(info); 1975 transportDec_GetLibInfo(info); 1976 FDK_toolsGetLibInfo(info); 1977 pcmDmx_GetLibInfo(info); 1978 pcmLimiter_GetLibInfo(info); 1979 FDK_drcDec_GetLibInfo(info); 1980 1981 /* search for next free tab */ 1982 for (i = 0; i < FDK_MODULE_LAST; i++) { 1983 if (info[i].module_id == FDK_NONE) break; 1984 } 1985 if (i == FDK_MODULE_LAST) { 1986 return -1; 1987 } 1988 info += i; 1989 1990 info->module_id = FDK_AACDEC; 1991 /* build own library info */ 1992 info->version = 1993 LIB_VERSION(AACDECODER_LIB_VL0, AACDECODER_LIB_VL1, AACDECODER_LIB_VL2); 1994 LIB_VERSION_STRING(info); 1995 info->build_date = AACDECODER_LIB_BUILD_DATE; 1996 info->build_time = AACDECODER_LIB_BUILD_TIME; 1997 info->title = AACDECODER_LIB_TITLE; 1998 1999 /* Set flags */ 2000 info->flags = 0 | CAPF_AAC_LC | CAPF_ER_AAC_LC | CAPF_ER_AAC_SCAL | 2001 CAPF_AAC_VCB11 | CAPF_AAC_HCR | CAPF_AAC_RVLC | CAPF_ER_AAC_LD | 2002 CAPF_ER_AAC_ELD | CAPF_AAC_CONCEALMENT | CAPF_AAC_DRC | 2003 CAPF_AAC_MPEG4 | CAPF_AAC_DRM_BSFORMAT | CAPF_AAC_1024 | 2004 CAPF_AAC_960 | CAPF_AAC_512 | CAPF_AAC_480 | 2005 CAPF_AAC_ELD_DOWNSCALE 2006 2007 | CAPF_AAC_USAC | CAPF_ER_AAC_ELDV2 | CAPF_AAC_UNIDRC; 2008 /* End of flags */ 2009 2010 return 0; 2011 } 2012