1 2/* ----------------------------------------------------------------------------------------------------------- 3Software License for The Fraunhofer FDK AAC Codec Library for Android 4 5� Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V. 6 All rights reserved. 7 8 1. INTRODUCTION 9The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements 10the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. 11This FDK AAC Codec software is intended to be used on a wide variety of Android devices. 12 13AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual 14audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by 15independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part 16of the MPEG specifications. 17 18Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) 19may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners 20individually for the purpose of encoding or decoding bit streams in products that are compliant with 21the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license 22these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec 23software may already be covered under those patent licenses when it is used for those licensed purposes only. 24 25Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, 26are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional 27applications information and documentation. 28 292. COPYRIGHT LICENSE 30 31Redistribution and use in source and binary forms, with or without modification, are permitted without 32payment of copyright license fees provided that you satisfy the following conditions: 33 34You must retain the complete text of this software license in redistributions of the FDK AAC Codec or 35your modifications thereto in source code form. 36 37You must retain the complete text of this software license in the documentation and/or other materials 38provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. 39You must make available free of charge copies of the complete source code of the FDK AAC Codec and your 40modifications thereto to recipients of copies in binary form. 41 42The name of Fraunhofer may not be used to endorse or promote products derived from this library without 43prior written permission. 44 45You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec 46software or your modifications thereto. 47 48Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software 49and 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 533. NO PATENT LICENSE 54 55NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, 56ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with 57respect to this software. 58 59You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized 60by appropriate patent licenses. 61 624. DISCLAIMER 63 64This 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 66of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 67CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, 68including but not limited to procurement of substitute goods or services; loss of use, data, or profits, 69or business interruption, however caused and on any theory of liability, whether in contract, strict 70liability, or tort (including negligence), arising in any way out of the use of this software, even if 71advised of the possibility of such damage. 72 735. CONTACT INFORMATION 74 75Fraunhofer Institute for Integrated Circuits IIS 76Attention: Audio and Multimedia Departments - FDK AAC LL 77Am Wolfsmantel 33 7891058 Erlangen, Germany 79 80www.iis.fraunhofer.de/amm 81amm-info@iis.fraunhofer.de 82----------------------------------------------------------------------------------------------------------- */ 83 84/***************************** MPEG-4 AAC Decoder ************************** 85 86 Author(s): Manuel Jander 87 Description: 88 89******************************************************************************/ 90 91#include "aacdecoder_lib.h" 92 93#include "aac_ram.h" 94#include "aacdecoder.h" 95#include "tpdec_lib.h" 96#include "FDK_core.h" /* FDK_tools version info */ 97 98 99 #include "sbrdecoder.h" 100 101 102 103 104#include "conceal.h" 105 106 #include "aacdec_drc.h" 107 108 109 110/* Decoder library info */ 111#define AACDECODER_LIB_VL0 2 112#define AACDECODER_LIB_VL1 5 113#define AACDECODER_LIB_VL2 10 114#define AACDECODER_LIB_TITLE "AAC Decoder Lib" 115#ifdef __ANDROID__ 116#define AACDECODER_LIB_BUILD_DATE "" 117#define AACDECODER_LIB_BUILD_TIME "" 118#else 119#define AACDECODER_LIB_BUILD_DATE __DATE__ 120#define AACDECODER_LIB_BUILD_TIME __TIME__ 121#endif 122 123static AAC_DECODER_ERROR 124setConcealMethod ( const HANDLE_AACDECODER self, 125 const INT method ); 126 127 128LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_GetFreeBytes ( const HANDLE_AACDECODER self, UINT *pFreeBytes){ 129 130 /* reset free bytes */ 131 *pFreeBytes = 0; 132 133 /* check handle */ 134 if(!self) 135 return AAC_DEC_INVALID_HANDLE; 136 137 /* return nr of free bytes */ 138 HANDLE_FDK_BITSTREAM hBs = transportDec_GetBitstream(self->hInput, 0); 139 *pFreeBytes = FDKgetFreeBits(hBs) >> 3; 140 141 /* success */ 142 return AAC_DEC_OK; 143} 144 145/** 146 * Config Decoder using a CSAudioSpecificConfig struct. 147 */ 148static 149LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_Config(HANDLE_AACDECODER self, const CSAudioSpecificConfig *pAscStruct) 150{ 151 AAC_DECODER_ERROR err; 152 153 /* Initialize AAC core decoder, and update self->streaminfo */ 154 err = CAacDecoder_Init(self, pAscStruct); 155 156 return err; 157} 158 159LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_ConfigRaw ( 160 HANDLE_AACDECODER self, 161 UCHAR *conf[], 162 const UINT length[] ) 163{ 164 AAC_DECODER_ERROR err = AAC_DEC_OK; 165 TRANSPORTDEC_ERROR errTp; 166 UINT layer, nrOfLayers = self->nrOfLayers; 167 168 for(layer = 0; layer < nrOfLayers; layer++){ 169 if(length[layer] > 0){ 170 errTp = transportDec_OutOfBandConfig(self->hInput, conf[layer], length[layer], layer); 171 if (errTp != TRANSPORTDEC_OK) { 172 switch (errTp) { 173 case TRANSPORTDEC_NEED_TO_RESTART: 174 err = AAC_DEC_NEED_TO_RESTART; 175 break; 176 case TRANSPORTDEC_UNSUPPORTED_FORMAT: 177 err = AAC_DEC_UNSUPPORTED_FORMAT; 178 break; 179 default: 180 err = AAC_DEC_UNKNOWN; 181 break; 182 } 183 /* if baselayer is OK we continue decoding */ 184 if(layer >= 1){ 185 self->nrOfLayers = layer; 186 } 187 break; 188 } 189 } 190 } 191 192 return err; 193} 194 195 196 197static INT aacDecoder_ConfigCallback(void *handle, const CSAudioSpecificConfig *pAscStruct) 198{ 199 HANDLE_AACDECODER self = (HANDLE_AACDECODER)handle; 200 AAC_DECODER_ERROR err = AAC_DEC_OK; 201 TRANSPORTDEC_ERROR errTp; 202 203 { 204 { 205 err = aacDecoder_Config(self, pAscStruct); 206 } 207 } 208 if (err == AAC_DEC_OK) { 209 if ( self->flags & (AC_USAC|AC_RSVD50|AC_LD|AC_ELD) 210 && CConcealment_GetDelay(&self->concealCommonData) > 0 ) 211 { 212 /* Revert to error concealment method Noise Substitution. 213 Because interpolation is not implemented for USAC/RSVD50 or 214 the additional delay is unwanted for low delay codecs. */ 215 setConcealMethod(self, 1); 216#ifdef DEBUG 217 FDKprintf(" Concealment method was reverted to 1 !\n"); 218#endif 219 } 220 errTp = TRANSPORTDEC_OK; 221 } else { 222 if (IS_INIT_ERROR(err)) { 223 errTp = TRANSPORTDEC_UNSUPPORTED_FORMAT; 224 } /* Fatal errors */ 225 else if (err == AAC_DEC_NEED_TO_RESTART) { 226 errTp = TRANSPORTDEC_NEED_TO_RESTART; 227 } else { 228 errTp = TRANSPORTDEC_UNKOWN_ERROR; 229 } 230 } 231 232 return errTp; 233} 234 235 236 237LINKSPEC_CPP AAC_DECODER_ERROR 238aacDecoder_AncDataInit ( HANDLE_AACDECODER self, 239 UCHAR *buffer, 240 int size ) 241{ 242 CAncData *ancData = &self->ancData; 243 244 return CAacDecoder_AncDataInit(ancData, buffer, size); 245} 246 247 248LINKSPEC_CPP AAC_DECODER_ERROR 249aacDecoder_AncDataGet ( HANDLE_AACDECODER self, 250 int index, 251 UCHAR **ptr, 252 int *size ) 253{ 254 CAncData *ancData = &self->ancData; 255 256 return CAacDecoder_AncDataGet(ancData, index, ptr, size); 257} 258 259 260static AAC_DECODER_ERROR 261setConcealMethod ( const HANDLE_AACDECODER self, /*!< Handle of the decoder instance */ 262 const INT method ) 263{ 264 AAC_DECODER_ERROR errorStatus = AAC_DEC_OK; 265 CConcealParams *pConcealData = NULL; 266 HANDLE_SBRDECODER hSbrDec = NULL; 267 HANDLE_AAC_DRC hDrcInfo = NULL; 268 HANDLE_PCM_DOWNMIX hPcmDmx = NULL; 269 CConcealmentMethod backupMethod = ConcealMethodNone; 270 int backupDelay = 0; 271 int bsDelay = 0; 272 273 /* check decoder handle */ 274 if (self != NULL) { 275 pConcealData = &self->concealCommonData; 276 hSbrDec = self->hSbrDecoder; 277 hDrcInfo = self->hDrcInfo; 278 hPcmDmx = self->hPcmUtils; 279 } 280 281 282 /* Get current method/delay */ 283 backupMethod = CConcealment_GetMethod(pConcealData); 284 backupDelay = CConcealment_GetDelay(pConcealData); 285 286 /* Be sure to set AAC and SBR concealment method simultaneously! */ 287 errorStatus = 288 CConcealment_SetParams( 289 pConcealData, 290 (int)method, // concealMethod 291 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED, // concealFadeOutSlope 292 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED, // concealFadeInSlope 293 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED, // concealMuteRelease 294 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED // concealComfNoiseLevel 295 ); 296 if ( (errorStatus != AAC_DEC_OK) 297 && (errorStatus != AAC_DEC_INVALID_HANDLE) ) { 298 goto bail; 299 } 300 301 /* Get new delay */ 302 bsDelay = CConcealment_GetDelay(pConcealData); 303 304 { 305 SBR_ERROR sbrErr = SBRDEC_OK; 306 307 /* set SBR bitstream delay */ 308 sbrErr = sbrDecoder_SetParam ( 309 hSbrDec, 310 SBR_SYSTEM_BITSTREAM_DELAY, 311 bsDelay 312 ); 313 314 switch (sbrErr) { 315 case SBRDEC_OK: 316 case SBRDEC_NOT_INITIALIZED: 317 if (self != NULL) { 318 /* save the param value and set later 319 (when SBR has been initialized) */ 320 self->sbrParams.bsDelay = bsDelay; 321 } 322 break; 323 default: 324 errorStatus = AAC_DEC_SET_PARAM_FAIL; 325 goto bail; 326 } 327 } 328 329 errorStatus = 330 aacDecoder_drcSetParam ( 331 hDrcInfo, 332 DRC_BS_DELAY, 333 bsDelay 334 ); 335 if ( (errorStatus != AAC_DEC_OK) 336 && (errorStatus != AAC_DEC_INVALID_HANDLE) ) { 337 goto bail; 338 } 339 340 if (errorStatus == AAC_DEC_OK) { 341 PCMDMX_ERROR err = 342 pcmDmx_SetParam ( 343 hPcmDmx, 344 DMX_BS_DATA_DELAY, 345 bsDelay 346 ); 347 switch (err) { 348 case PCMDMX_INVALID_HANDLE: 349 errorStatus = AAC_DEC_INVALID_HANDLE; 350 case PCMDMX_OK: 351 break; 352 default: 353 errorStatus = AAC_DEC_SET_PARAM_FAIL; 354 goto bail; 355 } 356 } 357 358 359bail: 360 if ( (errorStatus != AAC_DEC_OK) 361 && (errorStatus != AAC_DEC_INVALID_HANDLE) ) 362 { 363 /* Revert to the initial state */ 364 CConcealment_SetParams ( 365 pConcealData, 366 (int)backupMethod, 367 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED, 368 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED, 369 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED, 370 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED 371 ); 372 /* Revert SBR bitstream delay */ 373 sbrDecoder_SetParam ( 374 hSbrDec, 375 SBR_SYSTEM_BITSTREAM_DELAY, 376 backupDelay 377 ); 378 /* Revert DRC bitstream delay */ 379 aacDecoder_drcSetParam ( 380 hDrcInfo, 381 DRC_BS_DELAY, 382 backupDelay 383 ); 384 /* Revert PCM mixdown bitstream delay */ 385 pcmDmx_SetParam ( 386 hPcmDmx, 387 DMX_BS_DATA_DELAY, 388 backupDelay 389 ); 390 } 391 392 return errorStatus; 393} 394 395 396LINKSPEC_CPP AAC_DECODER_ERROR 397aacDecoder_SetParam ( const HANDLE_AACDECODER self, /*!< Handle of the decoder instance */ 398 const AACDEC_PARAM param, /*!< Parameter to set */ 399 const INT value) /*!< Parameter valued */ 400{ 401 AAC_DECODER_ERROR errorStatus = AAC_DEC_OK; 402 CConcealParams *pConcealData = NULL; 403 HANDLE_AAC_DRC hDrcInfo = NULL; 404 HANDLE_PCM_DOWNMIX hPcmDmx = NULL; 405 TDLimiterPtr hPcmTdl = NULL; 406 407 /* check decoder handle */ 408 if (self != NULL) { 409 pConcealData = &self->concealCommonData; 410 hDrcInfo = self->hDrcInfo; 411 hPcmDmx = self->hPcmUtils; 412 hPcmTdl = self->hLimiter; 413 } else { 414 errorStatus = AAC_DEC_INVALID_HANDLE; 415 } 416 417 /* configure the subsystems */ 418 switch (param) 419 { 420 case AAC_PCM_OUTPUT_INTERLEAVED: 421 if (value < 0 || value > 1) { 422 return AAC_DEC_SET_PARAM_FAIL; 423 } 424 if (self == NULL) { 425 return AAC_DEC_INVALID_HANDLE; 426 } 427 self->outputInterleaved = value; 428 break; 429 430 case AAC_PCM_MIN_OUTPUT_CHANNELS: 431 if (value < -1 || value > (8)) { 432 return AAC_DEC_SET_PARAM_FAIL; 433 } 434 { 435 PCMDMX_ERROR err; 436 437 err = pcmDmx_SetParam ( 438 hPcmDmx, 439 MIN_NUMBER_OF_OUTPUT_CHANNELS, 440 value ); 441 442 switch (err) { 443 case PCMDMX_OK: 444 break; 445 case PCMDMX_INVALID_HANDLE: 446 return AAC_DEC_INVALID_HANDLE; 447 default: 448 return AAC_DEC_SET_PARAM_FAIL; 449 } 450 } 451 break; 452 453 case AAC_PCM_MAX_OUTPUT_CHANNELS: 454 if (value < -1 || value > (8)) { 455 return AAC_DEC_SET_PARAM_FAIL; 456 } 457 { 458 PCMDMX_ERROR err; 459 460 err = pcmDmx_SetParam ( 461 hPcmDmx, 462 MAX_NUMBER_OF_OUTPUT_CHANNELS, 463 value ); 464 465 switch (err) { 466 case PCMDMX_OK: 467 break; 468 case PCMDMX_INVALID_HANDLE: 469 return AAC_DEC_INVALID_HANDLE; 470 default: 471 return AAC_DEC_SET_PARAM_FAIL; 472 } 473 } 474 break; 475 476 case AAC_PCM_DUAL_CHANNEL_OUTPUT_MODE: 477 { 478 PCMDMX_ERROR err; 479 480 err = pcmDmx_SetParam ( 481 hPcmDmx, 482 DMX_DUAL_CHANNEL_MODE, 483 value ); 484 485 switch (err) { 486 case PCMDMX_OK: 487 break; 488 case PCMDMX_INVALID_HANDLE: 489 return AAC_DEC_INVALID_HANDLE; 490 default: 491 return AAC_DEC_SET_PARAM_FAIL; 492 } 493 } 494 break; 495 496 497 case AAC_PCM_LIMITER_ENABLE: 498 if (value < -1 || value > 1) { 499 return AAC_DEC_SET_PARAM_FAIL; 500 } 501 if (self == NULL) { 502 return AAC_DEC_INVALID_HANDLE; 503 } 504 self->limiterEnableUser = value; 505 break; 506 507 case AAC_PCM_LIMITER_ATTACK_TIME: 508 if (value <= 0) { /* module function converts value to unsigned */ 509 return AAC_DEC_SET_PARAM_FAIL; 510 } 511 switch (setLimiterAttack(hPcmTdl, value)) { 512 case TDLIMIT_OK: 513 break; 514 case TDLIMIT_INVALID_HANDLE: 515 return AAC_DEC_INVALID_HANDLE; 516 case TDLIMIT_INVALID_PARAMETER: 517 default: 518 return AAC_DEC_SET_PARAM_FAIL; 519 } 520 break; 521 522 case AAC_PCM_LIMITER_RELEAS_TIME: 523 if (value <= 0) { /* module function converts value to unsigned */ 524 return AAC_DEC_SET_PARAM_FAIL; 525 } 526 switch (setLimiterRelease(hPcmTdl, value)) { 527 case TDLIMIT_OK: 528 break; 529 case TDLIMIT_INVALID_HANDLE: 530 return AAC_DEC_INVALID_HANDLE; 531 case TDLIMIT_INVALID_PARAMETER: 532 default: 533 return AAC_DEC_SET_PARAM_FAIL; 534 } 535 break; 536 537 case AAC_PCM_OUTPUT_CHANNEL_MAPPING: 538 switch (value) { 539 case 0: 540 if (self != NULL) { 541 self->channelOutputMapping = channelMappingTablePassthrough; 542 } 543 break; 544 case 1: 545 if (self != NULL) { 546 self->channelOutputMapping = channelMappingTableWAV; 547 } 548 break; 549 default: 550 errorStatus = AAC_DEC_SET_PARAM_FAIL; 551 break; 552 } 553 break; 554 555 556 case AAC_QMF_LOWPOWER: 557 if (value < -1 || value > 1) { 558 return AAC_DEC_SET_PARAM_FAIL; 559 } 560 if (self == NULL) { 561 return AAC_DEC_INVALID_HANDLE; 562 } 563 564 /** 565 * Set QMF mode (might be overriden) 566 * 0:HQ (complex) 567 * 1:LP (partially complex) 568 */ 569 self->qmfModeUser = (QMF_MODE)value; 570 break; 571 572 573 case AAC_DRC_ATTENUATION_FACTOR: 574 /* DRC compression factor (where 0 is no and 127 is max compression) */ 575 errorStatus = 576 aacDecoder_drcSetParam ( 577 hDrcInfo, 578 DRC_CUT_SCALE, 579 value 580 ); 581 break; 582 583 case AAC_DRC_BOOST_FACTOR: 584 /* DRC boost factor (where 0 is no and 127 is max boost) */ 585 errorStatus = 586 aacDecoder_drcSetParam ( 587 hDrcInfo, 588 DRC_BOOST_SCALE, 589 value 590 ); 591 break; 592 593 case AAC_DRC_REFERENCE_LEVEL: 594 /* DRC reference level quantized in 0.25dB steps using values [0..127] it is '-' for analog scaling */ 595 errorStatus = 596 aacDecoder_drcSetParam ( 597 hDrcInfo, 598 TARGET_REF_LEVEL, 599 value 600 ); 601 break; 602 603 case AAC_DRC_HEAVY_COMPRESSION: 604 /* Don't need to overwrite cut/boost values */ 605 errorStatus = 606 aacDecoder_drcSetParam ( 607 hDrcInfo, 608 APPLY_HEAVY_COMPRESSION, 609 value 610 ); 611 break; 612 613 614 case AAC_TPDEC_CLEAR_BUFFER: 615 transportDec_SetParam(self->hInput, TPDEC_PARAM_RESET, 1); 616 self->streamInfo.numLostAccessUnits = 0; 617 self->streamInfo.numBadBytes = 0; 618 self->streamInfo.numTotalBytes = 0; 619 /* aacDecoder_SignalInterruption(self); */ 620 break; 621 622 case AAC_CONCEAL_METHOD: 623 /* Changing the concealment method can introduce additional bitstream delay. And 624 that in turn affects sub libraries and modules which makes the whole thing quite 625 complex. So the complete changing routine is packed into a helper function which 626 keeps all modules and libs in a consistent state even in the case an error occures. */ 627 errorStatus = setConcealMethod ( self, value ); 628 break; 629 630 default: 631 return AAC_DEC_SET_PARAM_FAIL; 632 } /* switch(param) */ 633 634 return (errorStatus); 635} 636 637 638LINKSPEC_CPP HANDLE_AACDECODER aacDecoder_Open(TRANSPORT_TYPE transportFmt, UINT nrOfLayers) 639{ 640 AAC_DECODER_INSTANCE *aacDec = NULL; 641 HANDLE_TRANSPORTDEC pIn; 642 int err = 0; 643 644 /* Allocate transport layer struct. */ 645 pIn = transportDec_Open(transportFmt, TP_FLAG_MPEG4); 646 if (pIn == NULL) { 647 return NULL; 648 } 649 650 transportDec_SetParam(pIn, TPDEC_PARAM_IGNORE_BUFFERFULLNESS, 1); 651 652 /* Allocate AAC decoder core struct. */ 653 aacDec = CAacDecoder_Open(transportFmt); 654 655 if (aacDec == NULL) { 656 transportDec_Close(&pIn); 657 goto bail; 658 } 659 aacDec->hInput = pIn; 660 661 aacDec->nrOfLayers = nrOfLayers; 662 663 aacDec->channelOutputMapping = channelMappingTableWAV; 664 665 /* Register Config Update callback. */ 666 transportDec_RegisterAscCallback(pIn, aacDecoder_ConfigCallback, (void*)aacDec); 667 668 /* open SBR decoder */ 669 if ( SBRDEC_OK != sbrDecoder_Open ( &aacDec->hSbrDecoder )) { 670 err = -1; 671 goto bail; 672 } 673 aacDec->qmfModeUser = NOT_DEFINED; 674 transportDec_RegisterSbrCallback(aacDec->hInput, (cbSbr_t)sbrDecoder_Header, (void*)aacDec->hSbrDecoder); 675 676 677 pcmDmx_Open( &aacDec->hPcmUtils ); 678 if (aacDec->hPcmUtils == NULL) { 679 err = -1; 680 goto bail; 681 } 682 683 aacDec->hLimiter = createLimiter(TDL_ATTACK_DEFAULT_MS, TDL_RELEASE_DEFAULT_MS, SAMPLE_MAX, (8), 96000); 684 if (NULL == aacDec->hLimiter) { 685 err = -1; 686 goto bail; 687 } 688 aacDec->limiterEnableUser = (UCHAR)-1; 689 aacDec->limiterEnableCurr = 0; 690 691 692 693 /* Assure that all modules have same delay */ 694 if ( setConcealMethod(aacDec, CConcealment_GetMethod(&aacDec->concealCommonData)) ) { 695 err = -1; 696 goto bail; 697 } 698 699bail: 700 if (err == -1) { 701 aacDecoder_Close(aacDec); 702 aacDec = NULL; 703 } 704 return aacDec; 705} 706 707LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_Fill( 708 HANDLE_AACDECODER self, 709 UCHAR *pBuffer[], 710 const UINT bufferSize[], 711 UINT *pBytesValid 712 ) 713{ 714 TRANSPORTDEC_ERROR tpErr; 715 /* loop counter for layers; if not TT_MP4_RAWPACKETS used as index for only 716 available layer */ 717 INT layer = 0; 718 INT nrOfLayers = self->nrOfLayers; 719 720 { 721 for (layer = 0; layer < nrOfLayers; layer++){ 722 { 723 tpErr = transportDec_FillData( self->hInput, pBuffer[layer], bufferSize[layer], &pBytesValid[layer], layer ); 724 if (tpErr != TRANSPORTDEC_OK) { 725 return AAC_DEC_UNKNOWN; /* Must be an internal error */ 726 } 727 } 728 } 729 } 730 731 return AAC_DEC_OK; 732} 733 734 735static void aacDecoder_SignalInterruption(HANDLE_AACDECODER self) 736{ 737 CAacDecoder_SignalInterruption(self); 738 739 if ( self->hSbrDecoder != NULL ) { 740 sbrDecoder_SetParam(self->hSbrDecoder, SBR_BS_INTERRUPTION, 0); 741 } 742} 743 744static void aacDecoder_UpdateBitStreamCounters(CStreamInfo *pSi, HANDLE_FDK_BITSTREAM hBs, int nBits, AAC_DECODER_ERROR ErrorStatus) 745{ 746 /* calculate bit difference (amount of bits moved forward) */ 747 nBits = nBits - FDKgetValidBits(hBs); 748 749 /* Note: The amount of bits consumed might become negative when parsing a 750 bit stream with several sub frames, and we find out at the last sub frame 751 that the total frame length does not match the sum of sub frame length. 752 If this happens, the transport decoder might want to rewind to the supposed 753 ending of the transport frame, and this position might be before the last 754 access unit beginning. */ 755 756 /* Calc bitrate. */ 757 if (pSi->frameSize > 0) { 758 pSi->bitRate = (nBits * pSi->sampleRate)/pSi->frameSize; 759 } 760 761 /* bit/byte counters */ 762 { 763 int nBytes; 764 765 nBytes = nBits>>3; 766 pSi->numTotalBytes += nBytes; 767 if (IS_OUTPUT_VALID(ErrorStatus)) { 768 pSi->numTotalAccessUnits++; 769 } 770 if (IS_DECODE_ERROR(ErrorStatus)) { 771 pSi->numBadBytes += nBytes; 772 pSi->numBadAccessUnits++; 773 } 774 } 775} 776 777static INT aacDecoder_EstimateNumberOfLostFrames(HANDLE_AACDECODER self) 778{ 779 INT n; 780 781 transportDec_GetMissingAccessUnitCount( &n, self->hInput); 782 783 return n; 784} 785 786LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_DecodeFrame( 787 HANDLE_AACDECODER self, 788 INT_PCM *pTimeData, 789 const INT timeDataSize, 790 const UINT flags) 791{ 792 AAC_DECODER_ERROR ErrorStatus; 793 INT layer; 794 INT nBits; 795 INT interleaved = self->outputInterleaved; 796 HANDLE_FDK_BITSTREAM hBs; 797 int fTpInterruption = 0; /* Transport originated interruption detection. */ 798 int fTpConceal = 0; /* Transport originated concealment. */ 799 800 801 if (self == NULL) { 802 return AAC_DEC_INVALID_HANDLE; 803 } 804 805 if (flags & AACDEC_INTR) { 806 self->streamInfo.numLostAccessUnits = 0; 807 } 808 809 hBs = transportDec_GetBitstream(self->hInput, 0); 810 811 /* Get current bits position for bitrate calculation. */ 812 nBits = FDKgetValidBits(hBs); 813 if (! (flags & (AACDEC_CONCEAL | AACDEC_FLUSH) ) ) 814 { 815 TRANSPORTDEC_ERROR err; 816 817 for(layer = 0; layer < self->nrOfLayers; layer++) 818 { 819 err = transportDec_ReadAccessUnit(self->hInput, layer); 820 if (err != TRANSPORTDEC_OK) { 821 switch (err) { 822 case TRANSPORTDEC_NOT_ENOUGH_BITS: 823 ErrorStatus = AAC_DEC_NOT_ENOUGH_BITS; 824 goto bail; 825 case TRANSPORTDEC_SYNC_ERROR: 826 self->streamInfo.numLostAccessUnits = aacDecoder_EstimateNumberOfLostFrames(self); 827 fTpInterruption = 1; 828 break; 829 case TRANSPORTDEC_NEED_TO_RESTART: 830 ErrorStatus = AAC_DEC_NEED_TO_RESTART; 831 goto bail; 832 case TRANSPORTDEC_CRC_ERROR: 833 fTpConceal = 1; 834 break; 835 default: 836 ErrorStatus = AAC_DEC_UNKNOWN; 837 goto bail; 838 } 839 } 840 } 841 } else { 842 if (self->streamInfo.numLostAccessUnits > 0) { 843 self->streamInfo.numLostAccessUnits--; 844 } 845 } 846 847 /* Signal bit stream interruption to other modules if required. */ 848 if ( fTpInterruption || (flags & (AACDEC_INTR|AACDEC_CLRHIST)) ) 849 { 850 sbrDecoder_SetParam(self->hSbrDecoder, SBR_CLEAR_HISTORY, (flags&AACDEC_CLRHIST)); 851 aacDecoder_SignalInterruption(self); 852 if ( ! (flags & AACDEC_INTR) ) { 853 ErrorStatus = AAC_DEC_TRANSPORT_SYNC_ERROR; 854 goto bail; 855 } 856 } 857 858 /* Empty bit buffer in case of flush request. */ 859 if (flags & AACDEC_FLUSH) 860 { 861 transportDec_SetParam(self->hInput, TPDEC_PARAM_RESET, 1); 862 self->streamInfo.numLostAccessUnits = 0; 863 self->streamInfo.numBadBytes = 0; 864 self->streamInfo.numTotalBytes = 0; 865 } 866 /* Reset the output delay field. The modules will add their figures one after another. */ 867 self->streamInfo.outputDelay = 0; 868 869 if (self->limiterEnableUser==(UCHAR)-1) { 870 /* Enbale limiter for all non-lowdelay AOT's. */ 871 self->limiterEnableCurr = ( self->flags & (AC_LD|AC_ELD) ) ? 0 : 1; 872 } 873 else { 874 /* Use limiter configuration as requested. */ 875 self->limiterEnableCurr = self->limiterEnableUser; 876 } 877 /* reset limiter gain on a per frame basis */ 878 self->extGain[0] = FL2FXCONST_DBL(1.0f/(float)(1<<TDL_GAIN_SCALING)); 879 880 881 ErrorStatus = CAacDecoder_DecodeFrame(self, 882 flags | (fTpConceal ? AACDEC_CONCEAL : 0), 883 pTimeData, 884 timeDataSize, 885 interleaved); 886 887 if (!(flags & (AACDEC_CONCEAL|AACDEC_FLUSH))) { 888 TRANSPORTDEC_ERROR tpErr; 889 tpErr = transportDec_EndAccessUnit(self->hInput); 890 if (tpErr != TRANSPORTDEC_OK) { 891 self->frameOK = 0; 892 } 893 } 894 895 /* If the current pTimeData does not contain a valid signal, there nothing else we can do, so bail. */ 896 if ( ! IS_OUTPUT_VALID(ErrorStatus) ) { 897 goto bail; 898 } 899 900 { 901 /* Export data into streaminfo structure */ 902 self->streamInfo.sampleRate = self->streamInfo.aacSampleRate; 903 self->streamInfo.frameSize = self->streamInfo.aacSamplesPerFrame; 904 } 905 self->streamInfo.numChannels = self->streamInfo.aacNumChannels; 906 907 908 909 CAacDecoder_SyncQmfMode(self); 910 911/* sbr decoder */ 912 913 if (ErrorStatus || (flags & AACDEC_CONCEAL) || self->pAacDecoderStaticChannelInfo[0]->concealmentInfo.concealState > ConcealState_FadeIn) 914 { 915 self->frameOK = 0; /* if an error has occured do concealment in the SBR decoder too */ 916 } 917 918 if (self->sbrEnabled) 919 { 920 SBR_ERROR sbrError = SBRDEC_OK; 921 int chOutMapIdx = ((self->chMapIndex==0) && (self->streamInfo.numChannels<7)) ? self->streamInfo.numChannels : self->chMapIndex; 922 923 /* set params */ 924 sbrDecoder_SetParam ( self->hSbrDecoder, 925 SBR_SYSTEM_BITSTREAM_DELAY, 926 self->sbrParams.bsDelay); 927 sbrDecoder_SetParam ( self->hSbrDecoder, 928 SBR_FLUSH_DATA, 929 (flags & AACDEC_FLUSH) ); 930 931 if ( self->streamInfo.aot == AOT_ER_AAC_ELD ) { 932 /* Configure QMF */ 933 sbrDecoder_SetParam ( self->hSbrDecoder, 934 SBR_LD_QMF_TIME_ALIGN, 935 (self->flags & AC_LD_MPS) ? 1 : 0 ); 936 } 937 938 { 939 PCMDMX_ERROR dmxErr; 940 INT maxOutCh = 0; 941 942 dmxErr = pcmDmx_GetParam(self->hPcmUtils, MAX_NUMBER_OF_OUTPUT_CHANNELS, &maxOutCh); 943 if ( (dmxErr == PCMDMX_OK) && (maxOutCh == 1) ) { 944 /* Disable PS processing if we have to create a mono output signal. */ 945 self->psPossible = 0; 946 } 947 } 948 949 950 /* apply SBR processing */ 951 sbrError = sbrDecoder_Apply ( self->hSbrDecoder, 952 pTimeData, 953 &self->streamInfo.numChannels, 954 &self->streamInfo.sampleRate, 955 self->channelOutputMapping[chOutMapIdx], 956 interleaved, 957 self->frameOK, 958 &self->psPossible); 959 960 961 if (sbrError == SBRDEC_OK) { 962 #define UPS_SCALE 2 /* Maximum upsampling factor is 4 (CELP+SBR) */ 963 FIXP_DBL upsampleFactor = FL2FXCONST_DBL(1.0f/(1<<UPS_SCALE)); 964 965 /* Update data in streaminfo structure. Assume that the SBR upsampling factor is either 1 or 2 */ 966 self->flags |= AC_SBR_PRESENT; 967 if (self->streamInfo.aacSampleRate != self->streamInfo.sampleRate) { 968 if (self->streamInfo.frameSize == 768) { 969 upsampleFactor = FL2FXCONST_DBL(8.0f/(3<<UPS_SCALE)); 970 } else { 971 upsampleFactor = FL2FXCONST_DBL(2.0f/(1<<UPS_SCALE)); 972 } 973 } 974 /* Apply upsampling factor to both the core frame length and the core delay */ 975 self->streamInfo.frameSize = (INT)fMult((FIXP_DBL)self->streamInfo.aacSamplesPerFrame<<UPS_SCALE, upsampleFactor); 976 self->streamInfo.outputDelay = (UINT)(INT)fMult((FIXP_DBL)self->streamInfo.outputDelay<<UPS_SCALE, upsampleFactor); 977 self->streamInfo.outputDelay += sbrDecoder_GetDelay( self->hSbrDecoder ); 978 979 if (self->psPossible) { 980 self->flags |= AC_PS_PRESENT; 981 self->channelType[0] = ACT_FRONT; 982 self->channelType[1] = ACT_FRONT; 983 self->channelIndices[0] = 0; 984 self->channelIndices[1] = 1; 985 } 986 } 987 } 988 989 990 { 991 INT pcmLimiterScale = 0; 992 PCMDMX_ERROR dmxErr = PCMDMX_OK; 993 if ( flags & (AACDEC_INTR | AACDEC_CLRHIST) ) { 994 /* delete data from the past (e.g. mixdown coeficients) */ 995 pcmDmx_Reset( self->hPcmUtils, PCMDMX_RESET_BS_DATA ); 996 } 997 /* do PCM post processing */ 998 dmxErr = pcmDmx_ApplyFrame ( 999 self->hPcmUtils, 1000 pTimeData, 1001 self->streamInfo.frameSize, 1002 &self->streamInfo.numChannels, 1003 interleaved, 1004 self->channelType, 1005 self->channelIndices, 1006 self->channelOutputMapping, 1007 (self->limiterEnableCurr) ? &pcmLimiterScale : NULL 1008 ); 1009 if (dmxErr == PCMDMX_INVALID_MODE) { 1010 /* Announce the framework that the current combination of channel configuration and downmix 1011 * settings are not know to produce a predictable behavior and thus maybe produce strange output. */ 1012 ErrorStatus = AAC_DEC_DECODE_FRAME_ERROR; 1013 } 1014 1015 if ( flags & AACDEC_CLRHIST ) { 1016 /* Delete the delayed signal. */ 1017 resetLimiter(self->hLimiter); 1018 } 1019 if (self->limiterEnableCurr) 1020 { 1021 /* Set actual signal parameters */ 1022 setLimiterNChannels(self->hLimiter, self->streamInfo.numChannels); 1023 setLimiterSampleRate(self->hLimiter, self->streamInfo.sampleRate); 1024 1025 applyLimiter( 1026 self->hLimiter, 1027 pTimeData, 1028 self->extGain, 1029 &pcmLimiterScale, 1030 1, 1031 self->extGainDelay, 1032 self->streamInfo.frameSize 1033 ); 1034 1035 /* Announce the additional limiter output delay */ 1036 self->streamInfo.outputDelay += getLimiterDelay(self->hLimiter); 1037 } 1038 } 1039 1040 1041 /* Signal interruption to take effect in next frame. */ 1042 if ( flags & AACDEC_FLUSH ) { 1043 aacDecoder_SignalInterruption(self); 1044 } 1045 1046 /* Update externally visible copy of flags */ 1047 self->streamInfo.flags = self->flags; 1048 1049bail: 1050 1051 /* Update Statistics */ 1052 aacDecoder_UpdateBitStreamCounters(&self->streamInfo, hBs, nBits, ErrorStatus); 1053 1054 return ErrorStatus; 1055} 1056 1057LINKSPEC_CPP void aacDecoder_Close ( HANDLE_AACDECODER self ) 1058{ 1059 if (self == NULL) 1060 return; 1061 1062 1063 if (self->hLimiter != NULL) { 1064 destroyLimiter(self->hLimiter); 1065 } 1066 1067 if (self->hPcmUtils != NULL) { 1068 pcmDmx_Close( &self->hPcmUtils ); 1069 } 1070 1071 1072 1073 if (self->hSbrDecoder != NULL) { 1074 sbrDecoder_Close(&self->hSbrDecoder); 1075 } 1076 1077 if (self->hInput != NULL) { 1078 transportDec_Close(&self->hInput); 1079 } 1080 1081 CAacDecoder_Close(self); 1082} 1083 1084 1085LINKSPEC_CPP CStreamInfo* aacDecoder_GetStreamInfo ( HANDLE_AACDECODER self ) 1086{ 1087 return CAacDecoder_GetStreamInfo(self); 1088} 1089 1090LINKSPEC_CPP INT aacDecoder_GetLibInfo ( LIB_INFO *info ) 1091{ 1092 int i; 1093 1094 if (info == NULL) { 1095 return -1; 1096 } 1097 1098 sbrDecoder_GetLibInfo( info ); 1099 transportDec_GetLibInfo( info ); 1100 FDK_toolsGetLibInfo( info ); 1101 pcmDmx_GetLibInfo( info ); 1102 1103 /* search for next free tab */ 1104 for (i = 0; i < FDK_MODULE_LAST; i++) { 1105 if (info[i].module_id == FDK_NONE) break; 1106 } 1107 if (i == FDK_MODULE_LAST) { 1108 return -1; 1109 } 1110 info += i; 1111 1112 info->module_id = FDK_AACDEC; 1113 /* build own library info */ 1114 info->version = LIB_VERSION(AACDECODER_LIB_VL0, AACDECODER_LIB_VL1, AACDECODER_LIB_VL2); 1115 LIB_VERSION_STRING(info); 1116 info->build_date = AACDECODER_LIB_BUILD_DATE; 1117 info->build_time = AACDECODER_LIB_BUILD_TIME; 1118 info->title = AACDECODER_LIB_TITLE; 1119 1120 /* Set flags */ 1121 info->flags = 0 1122 | CAPF_AAC_LC 1123 | CAPF_AAC_VCB11 1124 | CAPF_AAC_HCR 1125 | CAPF_AAC_RVLC 1126 | CAPF_ER_AAC_LD 1127 | CAPF_ER_AAC_ELD 1128 | CAPF_AAC_CONCEALMENT 1129 | CAPF_AAC_DRC 1130 1131 | CAPF_AAC_MPEG4 1132 1133 1134 | CAPF_AAC_1024 1135 | CAPF_AAC_960 1136 1137 | CAPF_AAC_512 1138 1139 | CAPF_AAC_480 1140 1141 ; 1142 /* End of flags */ 1143 1144 return 0; 1145} 1146 1147 1148 1149 1150