1 2 /* ----------------------------------------------------------------------------------------------------------- 3 Software License for The Fraunhofer FDK AAC Codec Library for Android 4 5 Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Frderung der angewandten Forschung e.V. 6 All rights reserved. 7 8 1. INTRODUCTION 9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements 10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. 11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices. 12 13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual 14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by 15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part 16 of the MPEG specifications. 17 18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) 19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners 20 individually for the purpose of encoding or decoding bit streams in products that are compliant with 21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license 22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec 23 software may already be covered under those patent licenses when it is used for those licensed purposes only. 24 25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, 26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional 27 applications information and documentation. 28 29 2. COPYRIGHT LICENSE 30 31 Redistribution and use in source and binary forms, with or without modification, are permitted without 32 payment of copyright license fees provided that you satisfy the following conditions: 33 34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or 35 your modifications thereto in source code form. 36 37 You must retain the complete text of this software license in the documentation and/or other materials 38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. 39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your 40 modifications thereto to recipients of copies in binary form. 41 42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without 43 prior written permission. 44 45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec 46 software or your modifications thereto. 47 48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software 49 and the date of any change. For modified versions of the FDK AAC Codec, the term 50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term 51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." 52 53 3. NO PATENT LICENSE 54 55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, 56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with 57 respect to this software. 58 59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized 60 by appropriate patent licenses. 61 62 4. DISCLAIMER 63 64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors 65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties 66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, 68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits, 69 or business interruption, however caused and on any theory of liability, whether in contract, strict 70 liability, or tort (including negligence), arising in any way out of the use of this software, even if 71 advised of the possibility of such damage. 72 73 5. CONTACT INFORMATION 74 75 Fraunhofer Institute for Integrated Circuits IIS 76 Attention: Audio and Multimedia Departments - FDK AAC LL 77 Am Wolfsmantel 33 78 91058 Erlangen, Germany 79 80 www.iis.fraunhofer.de/amm 81 amm-info (at) iis.fraunhofer.de 82 ----------------------------------------------------------------------------------------------------------- */ 83 84 /*! 85 \file 86 \brief SBR decoder frontend 87 This module provides a frontend to the SBR decoder. The function openSBR() is called for 88 initialization. The function sbrDecoder_Apply() is called for each frame. sbr_Apply() will call the 89 required functions to decode the raw SBR data (provided by env_extr.cpp), to decode the envelope data and noise floor levels [decodeSbrData()], 90 and to finally apply SBR to the current frame [sbr_dec()]. 91 92 \sa sbrDecoder_Apply(), \ref documentationOverview 93 */ 94 95 /*! 96 \page documentationOverview Overview of important information resources and source code documentation 97 98 The primary source code documentation is based on generated and cross-referenced HTML files using 99 <a HREF="http://www.doxygen.org">doxygen</a>. As part of this documentation 100 you can find more extensive descriptions about key concepts and algorithms at the following locations: 101 102 <h2>Programming</h2> 103 104 \li Buffer management: sbrDecoder_Apply() and sbr_dec() 105 \li Internal scale factors to maximize SNR on fixed point processors: #QMF_SCALE_FACTOR 106 \li Special mantissa-exponent format: Created in requantizeEnvelopeData() and used in calculateSbrEnvelope() 107 108 <h2>Algorithmic details</h2> 109 \li About the SBR data format: \ref SBR_HEADER_ELEMENT and \ref SBR_STANDARD_ELEMENT 110 \li Details about the bitstream decoder: env_extr.cpp 111 \li Details about the QMF filterbank and the provided polyphase implementation: qmf_dec.cpp 112 \li Details about the transposer: lpp_tran.cpp 113 \li Details about the envelope adjuster: env_calc.cpp 114 115 */ 116 117 #include "sbrdecoder.h" 118 119 #include "FDK_bitstream.h" 120 121 #include "sbrdec_freq_sca.h" 122 #include "env_extr.h" 123 #include "sbr_dec.h" 124 #include "env_dec.h" 125 #include "sbr_crc.h" 126 #include "sbr_ram.h" 127 #include "sbr_rom.h" 128 #include "lpp_tran.h" 129 #include "transcendent.h" 130 131 132 #include "sbrdec_drc.h" 133 134 #include "psbitdec.h" 135 136 137 /* Decoder library info */ 138 #define SBRDECODER_LIB_VL0 2 139 #define SBRDECODER_LIB_VL1 2 140 #define SBRDECODER_LIB_VL2 3 141 #define SBRDECODER_LIB_TITLE "SBR Decoder" 142 #define SBRDECODER_LIB_BUILD_DATE __DATE__ 143 #define SBRDECODER_LIB_BUILD_TIME __TIME__ 144 145 146 147 148 static UCHAR getHeaderSlot( UCHAR currentSlot, UCHAR hdrSlotUsage[(1)+1] ) 149 { 150 UINT occupied = 0; 151 int s; 152 UCHAR slot = hdrSlotUsage[currentSlot]; 153 154 FDK_ASSERT((1)+1 < 32); 155 156 for (s = 0; s < (1)+1; s++) { 157 if ( (hdrSlotUsage[s] == slot) 158 && (s != slot) ) { 159 occupied = 1; 160 break; 161 } 162 } 163 164 if (occupied) { 165 occupied = 0; 166 167 for (s = 0; s < (1)+1; s++) { 168 occupied |= 1 << hdrSlotUsage[s]; 169 } 170 for (s = 0; s < (1)+1; s++) { 171 if ( !(occupied & 0x1) ) { 172 slot = s; 173 break; 174 } 175 occupied >>= 1; 176 } 177 } 178 179 return slot; 180 } 181 182 static void copySbrHeader( HANDLE_SBR_HEADER_DATA hDst, const HANDLE_SBR_HEADER_DATA hSrc ) 183 { 184 /* copy the whole header memory (including pointers) */ 185 FDKmemcpy( hDst, hSrc, sizeof(SBR_HEADER_DATA) ); 186 187 /* update pointers */ 188 hDst->freqBandData.freqBandTable[0] = hDst->freqBandData.freqBandTableLo; 189 hDst->freqBandData.freqBandTable[1] = hDst->freqBandData.freqBandTableHi; 190 } 191 192 193 /*! 194 \brief Reset SBR decoder. 195 196 Reset should only be called if SBR has been sucessfully detected by 197 an appropriate checkForPayload() function. 198 199 \return Error code. 200 */ 201 static 202 SBR_ERROR sbrDecoder_ResetElement ( 203 HANDLE_SBRDECODER self, 204 int sampleRateIn, 205 int sampleRateOut, 206 int samplesPerFrame, 207 const MP4_ELEMENT_ID elementID, 208 const int elementIndex, 209 const int overlap 210 ) 211 { 212 SBR_ERROR sbrError = SBRDEC_OK; 213 HANDLE_SBR_HEADER_DATA hSbrHeader; 214 UINT qmfFlags = 0; 215 216 int i, synDownsampleFac; 217 218 /* Check in/out samplerates */ 219 if ( sampleRateIn < 6400 220 || sampleRateIn > 48000 221 ) 222 { 223 sbrError = SBRDEC_UNSUPPORTED_CONFIG; 224 goto bail; 225 } 226 227 if ( sampleRateOut > 96000 ) 228 { 229 sbrError = SBRDEC_UNSUPPORTED_CONFIG; 230 goto bail; 231 } 232 233 /* Set QMF mode flags */ 234 if (self->flags & SBRDEC_LOW_POWER) 235 qmfFlags |= QMF_FLAG_LP; 236 237 if (self->coreCodec == AOT_ER_AAC_ELD) { 238 if (self->flags & SBRDEC_LD_MPS_QMF) { 239 qmfFlags |= QMF_FLAG_MPSLDFB; 240 } else { 241 qmfFlags |= QMF_FLAG_CLDFB; 242 } 243 } 244 245 /* Set downsampling factor for synthesis filter bank */ 246 if (sampleRateOut == 0) 247 { 248 /* no single rate mode */ 249 sampleRateOut = sampleRateIn<<1; /* In case of implicit signalling, assume dual rate SBR */ 250 } 251 252 if ( sampleRateIn == sampleRateOut ) { 253 synDownsampleFac = 2; 254 } else { 255 synDownsampleFac = 1; 256 } 257 258 self->synDownsampleFac = synDownsampleFac; 259 self->sampleRateOut = sampleRateOut; 260 261 { 262 int i; 263 264 for (i = 0; i < (1)+1; i++) 265 { 266 hSbrHeader = &(self->sbrHeader[elementIndex][i]); 267 268 /* init a default header such that we can at least do upsampling later */ 269 sbrError = initHeaderData( 270 hSbrHeader, 271 sampleRateIn, 272 sampleRateOut, 273 samplesPerFrame, 274 self->flags 275 ); 276 } 277 } 278 279 if (sbrError != SBRDEC_OK) { 280 goto bail; 281 } 282 283 /* Init SBR channels going to be assigned to a SBR element */ 284 { 285 int ch; 286 287 for (ch=0; ch<self->pSbrElement[elementIndex]->nChannels; ch++) 288 { 289 /* and create sbrDec */ 290 sbrError = createSbrDec (self->pSbrElement[elementIndex]->pSbrChannel[ch], 291 hSbrHeader, 292 &self->pSbrElement[elementIndex]->transposerSettings, 293 synDownsampleFac, 294 qmfFlags, 295 self->flags, 296 overlap, 297 ch ); 298 299 if (sbrError != SBRDEC_OK) { 300 goto bail; 301 } 302 } 303 } 304 305 //FDKmemclear(sbr_OverlapBuffer, sizeof(sbr_OverlapBuffer)); 306 307 if (self->numSbrElements == 1) { 308 switch ( self->coreCodec ) { 309 case AOT_AAC_LC: 310 case AOT_SBR: 311 case AOT_PS: 312 case AOT_ER_AAC_SCAL: 313 case AOT_DRM_AAC: 314 case AOT_DRM_SURROUND: 315 if (CreatePsDec ( &self->hParametricStereoDec, samplesPerFrame )) { 316 sbrError = SBRDEC_CREATE_ERROR; 317 goto bail; 318 } 319 break; 320 default: 321 break; 322 } 323 } 324 325 /* Init frame delay slot handling */ 326 self->pSbrElement[elementIndex]->useFrameSlot = 0; 327 for (i = 0; i < ((1)+1); i++) { 328 self->pSbrElement[elementIndex]->useHeaderSlot[i] = i; 329 } 330 331 bail: 332 333 return sbrError; 334 } 335 336 337 SBR_ERROR sbrDecoder_Open ( HANDLE_SBRDECODER * pSelf ) 338 { 339 HANDLE_SBRDECODER self = NULL; 340 SBR_ERROR sbrError = SBRDEC_OK; 341 342 /* Get memory for this instance */ 343 self = GetRam_SbrDecoder(); 344 if (self == NULL) { 345 sbrError = SBRDEC_MEM_ALLOC_FAILED; 346 goto bail; 347 } 348 349 self->workBuffer1 = GetRam_SbrDecWorkBuffer1(); 350 self->workBuffer2 = GetRam_SbrDecWorkBuffer2(); 351 352 if ( self->workBuffer1 == NULL 353 || self->workBuffer2 == NULL ) 354 { 355 sbrError = SBRDEC_MEM_ALLOC_FAILED; 356 goto bail; 357 } 358 359 /* 360 Already zero because of calloc 361 self->numSbrElements = 0; 362 self->numSbrChannels = 0; 363 self->codecFrameSize = 0; 364 */ 365 366 self->numDelayFrames = (1); /* set to the max value by default */ 367 368 *pSelf = self; 369 370 bail: 371 return sbrError; 372 } 373 374 /** 375 * \brief determine if the given core codec AOT can be processed or not. 376 * \param coreCodec core codec audio object type. 377 * \return 1 if SBR can be processed, 0 if SBR cannot be processed/applied. 378 */ 379 static 380 int sbrDecoder_isCoreCodecValid(AUDIO_OBJECT_TYPE coreCodec) 381 { 382 switch (coreCodec) { 383 case AOT_AAC_LC: 384 case AOT_SBR: 385 case AOT_PS: 386 case AOT_ER_AAC_SCAL: 387 case AOT_ER_AAC_ELD: 388 return 1; 389 default: 390 return 0; 391 } 392 } 393 394 static 395 void sbrDecoder_DestroyElement ( 396 HANDLE_SBRDECODER self, 397 const int elementIndex 398 ) 399 { 400 if (self->pSbrElement[elementIndex] != NULL) { 401 int ch; 402 403 for (ch=0; ch<SBRDEC_MAX_CH_PER_ELEMENT; ch++) { 404 if (self->pSbrElement[elementIndex]->pSbrChannel[ch] != NULL) { 405 deleteSbrDec( self->pSbrElement[elementIndex]->pSbrChannel[ch] ); 406 FreeRam_SbrDecChannel( &self->pSbrElement[elementIndex]->pSbrChannel[ch] ); 407 self->numSbrChannels -= 1; 408 } 409 } 410 FreeRam_SbrDecElement( &self->pSbrElement[elementIndex] ); 411 self->numSbrElements -= 1; 412 } 413 } 414 415 416 SBR_ERROR sbrDecoder_InitElement ( 417 HANDLE_SBRDECODER self, 418 const int sampleRateIn, 419 const int sampleRateOut, 420 const int samplesPerFrame, 421 const AUDIO_OBJECT_TYPE coreCodec, 422 const MP4_ELEMENT_ID elementID, 423 const int elementIndex 424 ) 425 { 426 SBR_ERROR sbrError = SBRDEC_OK; 427 int chCnt=0; 428 int nSbrElementsStart = self->numSbrElements; 429 430 /* Check core codec AOT */ 431 if (! sbrDecoder_isCoreCodecValid(coreCodec) || elementIndex >= (4)) { 432 sbrError = SBRDEC_UNSUPPORTED_CONFIG; 433 goto bail; 434 } 435 436 if ( elementID != ID_SCE && elementID != ID_CPE && elementID != ID_LFE ) 437 { 438 sbrError = SBRDEC_UNSUPPORTED_CONFIG; 439 goto bail; 440 } 441 442 if ( self->sampleRateIn == sampleRateIn 443 && self->codecFrameSize == samplesPerFrame 444 && self->coreCodec == coreCodec 445 && self->pSbrElement[elementIndex] != NULL 446 && self->pSbrElement[elementIndex]->elementID == elementID 447 ) 448 { 449 /* Nothing to do */ 450 return SBRDEC_OK; 451 } 452 453 self->sampleRateIn = sampleRateIn; 454 self->codecFrameSize = samplesPerFrame; 455 self->coreCodec = coreCodec; 456 457 self->flags = 0; 458 self->flags |= (coreCodec == AOT_ER_AAC_ELD) ? SBRDEC_ELD_GRID : 0; 459 460 /* Init SBR elements */ 461 { 462 int elChannels, ch; 463 464 if (self->pSbrElement[elementIndex] == NULL) { 465 self->pSbrElement[elementIndex] = GetRam_SbrDecElement(elementIndex); 466 if (self->pSbrElement[elementIndex] == NULL) { 467 sbrError = SBRDEC_MEM_ALLOC_FAILED; 468 goto bail; 469 } 470 self->numSbrElements ++; 471 } else { 472 self->numSbrChannels -= self->pSbrElement[elementIndex]->nChannels; 473 } 474 475 /* Save element ID for sanity checks and to have a fallback for concealment. */ 476 self->pSbrElement[elementIndex]->elementID = elementID; 477 478 /* Determine amount of channels for this element */ 479 switch (elementID) { 480 case ID_NONE: 481 case ID_CPE: elChannels=2; 482 break; 483 case ID_LFE: 484 case ID_SCE: elChannels=1; 485 break; 486 default: elChannels=0; 487 break; 488 } 489 490 /* Handle case of Parametric Stereo */ 491 if ( elementIndex == 0 && elementID == ID_SCE ) { 492 switch (coreCodec) { 493 case AOT_AAC_LC: 494 case AOT_SBR: 495 case AOT_PS: 496 case AOT_ER_AAC_SCAL: 497 case AOT_DRM_AAC: 498 case AOT_DRM_SURROUND: 499 elChannels = 2; 500 break; 501 default: 502 break; 503 } 504 } 505 506 self->pSbrElement[elementIndex]->nChannels = elChannels; 507 508 for (ch=0; ch<elChannels; ch++) 509 { 510 if (self->pSbrElement[elementIndex]->pSbrChannel[ch] == NULL) { 511 self->pSbrElement[elementIndex]->pSbrChannel[ch] = GetRam_SbrDecChannel(chCnt); 512 if (self->pSbrElement[elementIndex]->pSbrChannel[ch] == NULL) { 513 sbrError = SBRDEC_MEM_ALLOC_FAILED; 514 goto bail; 515 } 516 } 517 self->numSbrChannels ++; 518 519 sbrDecoder_drcInitChannel( &self->pSbrElement[elementIndex]->pSbrChannel[ch]->SbrDec.sbrDrcChannel ); 520 521 /* Add reference pointer to workbuffers. */ 522 self->pSbrElement[elementIndex]->pSbrChannel[ch]->SbrDec.WorkBuffer1 = self->workBuffer1; 523 self->pSbrElement[elementIndex]->pSbrChannel[ch]->SbrDec.WorkBuffer2 = self->workBuffer2; 524 chCnt++; 525 } 526 if (elChannels == 1 && self->pSbrElement[elementIndex]->pSbrChannel[ch] != NULL) { 527 deleteSbrDec( self->pSbrElement[elementIndex]->pSbrChannel[ch] ); 528 FreeRam_SbrDecChannel( &self->pSbrElement[elementIndex]->pSbrChannel[ch] ); 529 } 530 } 531 532 /* clear error flags for all delay slots */ 533 FDKmemclear(self->pSbrElement[elementIndex]->frameErrorFlag, ((1)+1)*sizeof(UCHAR)); 534 535 /* Initialize this instance */ 536 sbrError = sbrDecoder_ResetElement( 537 self, 538 sampleRateIn, 539 sampleRateOut, 540 samplesPerFrame, 541 elementID, 542 elementIndex, 543 (coreCodec == AOT_ER_AAC_ELD) ? 0 : (6) 544 ); 545 546 547 548 bail: 549 if (sbrError != SBRDEC_OK) { 550 if (nSbrElementsStart < self->numSbrElements) { 551 /* Free the memory allocated for this element */ 552 sbrDecoder_DestroyElement( self, elementIndex ); 553 } else if (self->pSbrElement[elementIndex] != NULL) { 554 /* Set error flag to trigger concealment */ 555 self->pSbrElement[elementIndex]->frameErrorFlag[self->pSbrElement[elementIndex]->useFrameSlot] = 1; 556 } 557 } 558 559 return sbrError; 560 } 561 562 /** 563 * \brief Apply decoded SBR header for one element. 564 * \param self SBR decoder instance handle 565 * \param hSbrHeader SBR header handle to be processed. 566 * \param hSbrChannel pointer array to the SBR element channels corresponding to the SBR header. 567 * \param headerStatus header status value returned from SBR header parser. 568 * \param numElementChannels amount of channels for the SBR element whos header is to be processed. 569 */ 570 static 571 SBR_ERROR sbrDecoder_HeaderUpdate( 572 HANDLE_SBRDECODER self, 573 HANDLE_SBR_HEADER_DATA hSbrHeader, 574 SBR_HEADER_STATUS headerStatus, 575 HANDLE_SBR_CHANNEL hSbrChannel[], 576 const int numElementChannels 577 ) 578 { 579 SBR_ERROR errorStatus = SBRDEC_OK; 580 581 /* 582 change of control data, reset decoder 583 */ 584 errorStatus = resetFreqBandTables(hSbrHeader, self->flags); 585 586 if (errorStatus == SBRDEC_OK) { 587 if (hSbrHeader->syncState == UPSAMPLING && headerStatus != HEADER_RESET) 588 { 589 /* As the default header would limit the frequency range, 590 lowSubband and highSubband must be patched. */ 591 hSbrHeader->freqBandData.lowSubband = hSbrHeader->numberOfAnalysisBands; 592 hSbrHeader->freqBandData.highSubband = hSbrHeader->numberOfAnalysisBands; 593 } 594 595 /* Trigger a reset before processing this slot */ 596 hSbrHeader->status |= SBRDEC_HDR_STAT_RESET; 597 } 598 599 return errorStatus; 600 } 601 602 INT sbrDecoder_Header ( 603 HANDLE_SBRDECODER self, 604 HANDLE_FDK_BITSTREAM hBs, 605 const INT sampleRateIn, 606 const INT sampleRateOut, 607 const INT samplesPerFrame, 608 const AUDIO_OBJECT_TYPE coreCodec, 609 const MP4_ELEMENT_ID elementID, 610 const INT elementIndex 611 ) 612 { 613 SBR_HEADER_STATUS headerStatus; 614 HANDLE_SBR_HEADER_DATA hSbrHeader; 615 SBR_ERROR sbrError = SBRDEC_OK; 616 int headerIndex; 617 618 if ( self == NULL || elementIndex > (4) ) 619 { 620 return SBRDEC_UNSUPPORTED_CONFIG; 621 } 622 623 if (! sbrDecoder_isCoreCodecValid(coreCodec)) { 624 return SBRDEC_UNSUPPORTED_CONFIG; 625 } 626 627 sbrError = sbrDecoder_InitElement( 628 self, 629 sampleRateIn, 630 sampleRateOut, 631 samplesPerFrame, 632 coreCodec, 633 elementID, 634 elementIndex 635 ); 636 637 if (sbrError != SBRDEC_OK) { 638 goto bail; 639 } 640 641 headerIndex = getHeaderSlot(self->pSbrElement[elementIndex]->useFrameSlot, 642 self->pSbrElement[elementIndex]->useHeaderSlot); 643 hSbrHeader = &(self->sbrHeader[elementIndex][headerIndex]); 644 645 headerStatus = sbrGetHeaderData ( hSbrHeader, 646 hBs, 647 self->flags, 648 0); 649 650 651 { 652 SBR_DECODER_ELEMENT *pSbrElement; 653 654 pSbrElement = self->pSbrElement[elementIndex]; 655 656 /* Sanity check */ 657 if (pSbrElement != NULL) { 658 if ( (elementID == ID_CPE && pSbrElement->nChannels != 2) 659 || (elementID != ID_CPE && pSbrElement->nChannels != 1) ) 660 { 661 return SBRDEC_UNSUPPORTED_CONFIG; 662 } 663 if ( headerStatus == HEADER_RESET ) { 664 665 sbrError = sbrDecoder_HeaderUpdate( 666 self, 667 hSbrHeader, 668 headerStatus, 669 pSbrElement->pSbrChannel, 670 pSbrElement->nChannels 671 ); 672 673 if (sbrError == SBRDEC_OK) { 674 hSbrHeader->syncState = SBR_HEADER; 675 hSbrHeader->status |= SBRDEC_HDR_STAT_UPDATE; 676 } 677 /* else { 678 Since we already have overwritten the old SBR header the only way out is UPSAMPLING! 679 This will be prepared in the next step. 680 } */ 681 } 682 } 683 } 684 bail: 685 return sbrError; 686 } 687 688 689 SBR_ERROR sbrDecoder_SetParam (HANDLE_SBRDECODER self, 690 const SBRDEC_PARAM param, 691 const INT value ) 692 { 693 SBR_ERROR errorStatus = SBRDEC_OK; 694 695 /* configure the subsystems */ 696 switch (param) 697 { 698 case SBR_SYSTEM_BITSTREAM_DELAY: 699 if (value < 0 || value > (1)) { 700 errorStatus = SBRDEC_SET_PARAM_FAIL; 701 break; 702 } 703 if (self == NULL) { 704 errorStatus = SBRDEC_NOT_INITIALIZED; 705 } else { 706 self->numDelayFrames = (UCHAR)value; 707 } 708 break; 709 case SBR_QMF_MODE: 710 if (self == NULL) { 711 errorStatus = SBRDEC_NOT_INITIALIZED; 712 } else { 713 if (value == 1) { 714 self->flags |= SBRDEC_LOW_POWER; 715 } else { 716 self->flags &= ~SBRDEC_LOW_POWER; 717 } 718 } 719 break; 720 case SBR_LD_QMF_TIME_ALIGN: 721 if (self == NULL) { 722 errorStatus = SBRDEC_NOT_INITIALIZED; 723 } else { 724 if (value == 1) { 725 self->flags |= SBRDEC_LD_MPS_QMF; 726 } else { 727 self->flags &= ~SBRDEC_LD_MPS_QMF; 728 } 729 } 730 break; 731 case SBR_BS_INTERRUPTION: 732 { 733 int elementIndex; 734 735 if (self == NULL) { 736 errorStatus = SBRDEC_NOT_INITIALIZED; 737 break; 738 } 739 740 /* Loop over SBR elements */ 741 for (elementIndex = 0; elementIndex < self->numSbrElements; elementIndex++) 742 { 743 HANDLE_SBR_HEADER_DATA hSbrHeader; 744 int headerIndex = getHeaderSlot(self->pSbrElement[elementIndex]->useFrameSlot, 745 self->pSbrElement[elementIndex]->useHeaderSlot); 746 747 hSbrHeader = &(self->sbrHeader[elementIndex][headerIndex]); 748 749 /* Set sync state UPSAMPLING for the corresponding slot. 750 This switches off bitstream parsing until a new header arrives. */ 751 hSbrHeader->syncState = UPSAMPLING; 752 hSbrHeader->status |= SBRDEC_HDR_STAT_UPDATE; 753 } 754 } 755 break; 756 default: 757 errorStatus = SBRDEC_SET_PARAM_FAIL; 758 break; 759 } /* switch(param) */ 760 761 return (errorStatus); 762 } 763 764 static 765 SBRDEC_DRC_CHANNEL * sbrDecoder_drcGetChannel( const HANDLE_SBRDECODER self, const INT channel ) 766 { 767 SBRDEC_DRC_CHANNEL *pSbrDrcChannelData = NULL; 768 int elementIndex, elChanIdx=0, numCh=0; 769 770 for (elementIndex = 0; (elementIndex < (4)) && (numCh <= channel); elementIndex++) 771 { 772 SBR_DECODER_ELEMENT *pSbrElement = self->pSbrElement[elementIndex]; 773 int c, elChannels; 774 775 elChanIdx = 0; 776 if (pSbrElement == NULL) break; 777 778 /* Determine amount of channels for this element */ 779 switch (pSbrElement->elementID) { 780 case ID_CPE: elChannels = 2; 781 break; 782 case ID_LFE: 783 case ID_SCE: elChannels = 1; 784 break; 785 case ID_NONE: 786 default: elChannels = 0; 787 break; 788 } 789 790 /* Limit with actual allocated element channels */ 791 elChannels = FDKmin(elChannels, pSbrElement->nChannels); 792 793 for (c = 0; (c < elChannels) && (numCh <= channel); c++) { 794 if (pSbrElement->pSbrChannel[elChanIdx] != NULL) { 795 numCh++; 796 elChanIdx++; 797 } 798 } 799 } 800 elementIndex -= 1; 801 elChanIdx -= 1; 802 803 if (elChanIdx < 0 || elementIndex < 0) { 804 return NULL; 805 } 806 807 if ( self->pSbrElement[elementIndex] != NULL ) { 808 if ( self->pSbrElement[elementIndex]->pSbrChannel[elChanIdx] != NULL ) 809 { 810 pSbrDrcChannelData = &self->pSbrElement[elementIndex]->pSbrChannel[elChanIdx]->SbrDec.sbrDrcChannel; 811 } 812 } 813 814 return (pSbrDrcChannelData); 815 } 816 817 SBR_ERROR sbrDecoder_drcFeedChannel ( HANDLE_SBRDECODER self, 818 INT ch, 819 UINT numBands, 820 FIXP_DBL *pNextFact_mag, 821 INT nextFact_exp, 822 SHORT drcInterpolationScheme, 823 UCHAR winSequence, 824 USHORT *pBandTop ) 825 { 826 SBRDEC_DRC_CHANNEL *pSbrDrcChannelData = NULL; 827 int band, isValidData = 0; 828 829 if (self == NULL) { 830 return SBRDEC_NOT_INITIALIZED; 831 } 832 if (ch > (6) || pNextFact_mag == NULL) { 833 return SBRDEC_SET_PARAM_FAIL; 834 } 835 836 /* Search for gain values different to 1.0f */ 837 for (band = 0; band < numBands; band += 1) { 838 if ( !((pNextFact_mag[band] == FL2FXCONST_DBL(0.5)) && (nextFact_exp == 1)) 839 && !((pNextFact_mag[band] == (FIXP_DBL)MAXVAL_DBL) && (nextFact_exp == 0)) ) { 840 isValidData = 1; 841 break; 842 } 843 } 844 845 /* Find the right SBR channel */ 846 pSbrDrcChannelData = sbrDecoder_drcGetChannel( self, ch ); 847 848 if ( pSbrDrcChannelData != NULL ) { 849 if ( pSbrDrcChannelData->enable || isValidData ) 850 { /* Activate processing only with real and valid data */ 851 int i; 852 853 pSbrDrcChannelData->enable = 1; 854 pSbrDrcChannelData->numBandsNext = numBands; 855 856 pSbrDrcChannelData->winSequenceNext = winSequence; 857 pSbrDrcChannelData->drcInterpolationSchemeNext = drcInterpolationScheme; 858 pSbrDrcChannelData->nextFact_exp = nextFact_exp; 859 860 for (i = 0; i < (int)numBands; i++) { 861 pSbrDrcChannelData->bandTopNext[i] = pBandTop[i]; 862 pSbrDrcChannelData->nextFact_mag[i] = pNextFact_mag[i]; 863 } 864 } 865 } 866 867 return SBRDEC_OK; 868 } 869 870 871 void sbrDecoder_drcDisable ( HANDLE_SBRDECODER self, 872 INT ch ) 873 { 874 SBRDEC_DRC_CHANNEL *pSbrDrcChannelData = NULL; 875 876 if ( (self == NULL) 877 || (ch > (6)) 878 || (self->numSbrElements == 0) 879 || (self->numSbrChannels == 0) ) { 880 return; 881 } 882 883 /* Find the right SBR channel */ 884 pSbrDrcChannelData = sbrDecoder_drcGetChannel( self, ch ); 885 886 if ( pSbrDrcChannelData != NULL ) { 887 sbrDecoder_drcInitChannel( pSbrDrcChannelData ); 888 } 889 } 890 891 892 893 SBR_ERROR sbrDecoder_Parse( 894 HANDLE_SBRDECODER self, 895 HANDLE_FDK_BITSTREAM hBs, 896 int *count, 897 int bsPayLen, 898 int crcFlag, 899 MP4_ELEMENT_ID prevElement, 900 int elementIndex, 901 int fGlobalIndependencyFlag 902 ) 903 { 904 SBR_DECODER_ELEMENT *hSbrElement; 905 HANDLE_SBR_HEADER_DATA hSbrHeader; 906 HANDLE_SBR_CHANNEL *pSbrChannel; 907 908 SBR_FRAME_DATA *hFrameDataLeft; 909 SBR_FRAME_DATA *hFrameDataRight; 910 911 SBR_ERROR errorStatus = SBRDEC_OK; 912 SBR_SYNC_STATE initialSyncState; 913 SBR_HEADER_STATUS headerStatus = HEADER_NOT_PRESENT; 914 915 INT startPos; 916 INT CRCLen = 0; 917 918 int stereo; 919 int fDoDecodeSbrData = 1; 920 921 int lastSlot, lastHdrSlot = 0, thisHdrSlot; 922 923 /* Remember start position of SBR element */ 924 startPos = FDKgetValidBits(hBs); 925 926 /* SBR sanity checks */ 927 if ( self == NULL || self->pSbrElement[elementIndex] == NULL ) { 928 errorStatus = SBRDEC_NOT_INITIALIZED; 929 goto bail; 930 } 931 932 hSbrElement = self->pSbrElement[elementIndex]; 933 934 lastSlot = (hSbrElement->useFrameSlot > 0) ? hSbrElement->useFrameSlot-1 : self->numDelayFrames; 935 lastHdrSlot = hSbrElement->useHeaderSlot[lastSlot]; 936 thisHdrSlot = getHeaderSlot( hSbrElement->useFrameSlot, hSbrElement->useHeaderSlot ); /* Get a free header slot not used by frames not processed yet. */ 937 938 /* Assign the free slot to store a new header if there is one. */ 939 hSbrHeader = &self->sbrHeader[elementIndex][thisHdrSlot]; 940 941 pSbrChannel = hSbrElement->pSbrChannel; 942 stereo = (hSbrElement->elementID == ID_CPE) ? 1 : 0; 943 944 hFrameDataLeft = &self->pSbrElement[elementIndex]->pSbrChannel[0]->frameData[hSbrElement->useFrameSlot]; 945 hFrameDataRight = &self->pSbrElement[elementIndex]->pSbrChannel[1]->frameData[hSbrElement->useFrameSlot]; 946 947 initialSyncState = hSbrHeader->syncState; 948 949 /* reset PS flag; will be set after PS was found */ 950 self->flags &= ~SBRDEC_PS_DECODED; 951 952 if (hSbrHeader->status & SBRDEC_HDR_STAT_UPDATE) { 953 /* Got a new header from extern (e.g. from an ASC) */ 954 headerStatus = HEADER_OK; 955 hSbrHeader->status &= ~SBRDEC_HDR_STAT_UPDATE; 956 } 957 else if (thisHdrSlot != lastHdrSlot) { 958 /* Copy the last header into this slot otherwise the 959 header compare will trigger more HEADER_RESETs than needed. */ 960 copySbrHeader( hSbrHeader, &self->sbrHeader[elementIndex][lastHdrSlot] ); 961 } 962 963 /* 964 Check if bit stream data is valid and matches the element context 965 */ 966 if ( ((prevElement != ID_SCE) && (prevElement != ID_CPE)) || prevElement != hSbrElement->elementID) { 967 /* In case of LFE we also land here, since there is no LFE SBR element (do upsampling only) */ 968 fDoDecodeSbrData = 0; 969 } 970 971 if (fDoDecodeSbrData) 972 { 973 if ((INT)FDKgetValidBits(hBs) <= 0) { 974 fDoDecodeSbrData = 0; 975 } 976 } 977 978 /* 979 SBR CRC-check 980 */ 981 if (fDoDecodeSbrData) 982 { 983 if (crcFlag == 1) { 984 switch (self->coreCodec) { 985 case AOT_ER_AAC_ELD: 986 FDKpushFor (hBs, 10); 987 /* check sbrcrc later: we don't know the payload length now */ 988 break; 989 default: 990 CRCLen = bsPayLen - 10; /* change: 0 => i */ 991 if (CRCLen < 0) { 992 fDoDecodeSbrData = 0; 993 } else { 994 fDoDecodeSbrData = SbrCrcCheck (hBs, CRCLen); 995 } 996 break; 997 } 998 } 999 } /* if (fDoDecodeSbrData) */ 1000 1001 /* 1002 Read in the header data and issue a reset if change occured 1003 */ 1004 if (fDoDecodeSbrData) 1005 { 1006 int sbrHeaderPresent; 1007 1008 { 1009 sbrHeaderPresent = FDKreadBit(hBs); 1010 } 1011 1012 if ( sbrHeaderPresent ) { 1013 headerStatus = sbrGetHeaderData (hSbrHeader, 1014 hBs, 1015 self->flags, 1016 1); 1017 } 1018 1019 if (headerStatus == HEADER_RESET) 1020 { 1021 errorStatus = sbrDecoder_HeaderUpdate( 1022 self, 1023 hSbrHeader, 1024 headerStatus, 1025 pSbrChannel, 1026 hSbrElement->nChannels 1027 ); 1028 1029 if (errorStatus == SBRDEC_OK) { 1030 hSbrHeader->syncState = SBR_HEADER; 1031 } else { 1032 hSbrHeader->syncState = SBR_NOT_INITIALIZED; 1033 } 1034 } 1035 1036 if (errorStatus != SBRDEC_OK) { 1037 fDoDecodeSbrData = 0; 1038 } 1039 } /* if (fDoDecodeSbrData) */ 1040 1041 /* 1042 Print debugging output only if state has changed 1043 */ 1044 1045 /* read frame data */ 1046 if ((hSbrHeader->syncState >= SBR_HEADER) && fDoDecodeSbrData) { 1047 int sbrFrameOk; 1048 /* read the SBR element data */ 1049 if (stereo) { 1050 sbrFrameOk = sbrGetChannelPairElement(hSbrHeader, 1051 hFrameDataLeft, 1052 hFrameDataRight, 1053 hBs, 1054 self->flags, 1055 self->pSbrElement[elementIndex]->transposerSettings.overlap); 1056 } 1057 else { 1058 if (self->hParametricStereoDec != NULL) { 1059 /* update slot index for PS bitstream parsing */ 1060 self->hParametricStereoDec->bsLastSlot = self->hParametricStereoDec->bsReadSlot; 1061 self->hParametricStereoDec->bsReadSlot = hSbrElement->useFrameSlot; 1062 } 1063 sbrFrameOk = sbrGetSingleChannelElement(hSbrHeader, 1064 hFrameDataLeft, 1065 hBs, 1066 self->hParametricStereoDec, 1067 self->flags, 1068 self->pSbrElement[elementIndex]->transposerSettings.overlap); 1069 } 1070 if (!sbrFrameOk) { 1071 fDoDecodeSbrData = 0; 1072 } 1073 else { 1074 INT valBits; 1075 1076 if (bsPayLen > 0) { 1077 valBits = bsPayLen - ((INT)startPos - (INT)FDKgetValidBits(hBs)); 1078 } else { 1079 valBits = (INT)FDKgetValidBits(hBs); 1080 } 1081 1082 if ( crcFlag == 1 ) { 1083 switch (self->coreCodec) { 1084 case AOT_ER_AAC_ELD: 1085 { 1086 /* late crc check for eld */ 1087 INT payloadbits = (INT)startPos - (INT)FDKgetValidBits(hBs) - startPos; 1088 INT crcLen = payloadbits - 10; 1089 FDKpushBack(hBs, payloadbits); 1090 fDoDecodeSbrData = SbrCrcCheck (hBs, crcLen); 1091 FDKpushFor(hBs, crcLen); 1092 } 1093 break; 1094 default: 1095 break; 1096 } 1097 } 1098 1099 /* sanity check of remaining bits */ 1100 if (valBits < 0) { 1101 fDoDecodeSbrData = 0; 1102 } else { 1103 switch (self->coreCodec) { 1104 case AOT_SBR: 1105 case AOT_PS: 1106 case AOT_AAC_LC: 1107 { 1108 /* This sanity check is only meaningful with General Audio bitstreams */ 1109 int alignBits = valBits & 0x7; 1110 1111 if (valBits > alignBits) { 1112 fDoDecodeSbrData = 0; 1113 } 1114 } 1115 break; 1116 default: 1117 /* No sanity check available */ 1118 break; 1119 } 1120 } 1121 } 1122 } 1123 1124 if (!fDoDecodeSbrData) { 1125 /* Set error flag for this slot to trigger concealment */ 1126 self->pSbrElement[elementIndex]->frameErrorFlag[hSbrElement->useFrameSlot] = 1; 1127 errorStatus = SBRDEC_PARSE_ERROR; 1128 } else { 1129 /* Everything seems to be ok so clear the error flag */ 1130 self->pSbrElement[elementIndex]->frameErrorFlag[hSbrElement->useFrameSlot] = 0; 1131 } 1132 1133 if (!stereo) { 1134 /* Turn coupling off explicitely to avoid access to absent right frame data 1135 that might occur with corrupt bitstreams. */ 1136 hFrameDataLeft->coupling = COUPLING_OFF; 1137 } 1138 1139 bail: 1140 if (errorStatus == SBRDEC_OK) { 1141 if (headerStatus == HEADER_NOT_PRESENT) { 1142 /* Use the old header for this frame */ 1143 hSbrElement->useHeaderSlot[hSbrElement->useFrameSlot] = lastHdrSlot; 1144 } else { 1145 /* Use the new header for this frame */ 1146 hSbrElement->useHeaderSlot[hSbrElement->useFrameSlot] = thisHdrSlot; 1147 } 1148 1149 /* Move frame pointer to the next slot which is up to be decoded/applied next */ 1150 hSbrElement->useFrameSlot = (hSbrElement->useFrameSlot+1) % (self->numDelayFrames+1); 1151 } 1152 1153 *count -= startPos - FDKgetValidBits(hBs); 1154 1155 return errorStatus; 1156 } 1157 1158 1159 /** 1160 * \brief Render one SBR element into time domain signal. 1161 * \param self SBR decoder handle 1162 * \param timeData pointer to output buffer 1163 * \param interleaved flag indicating interleaved channel output 1164 * \param channelMapping pointer to UCHAR array where next 2 channel offsets are stored. 1165 * \param elementIndex enumerating index of the SBR element to render. 1166 * \param numInChannels number of channels from core coder (reading stride). 1167 * \param numOutChannels pointer to a location to return number of output channels. 1168 * \param psPossible flag indicating if PS is possible or not. 1169 * \return SBRDEC_OK if successfull, else error code 1170 */ 1171 static SBR_ERROR 1172 sbrDecoder_DecodeElement ( 1173 HANDLE_SBRDECODER self, 1174 INT_PCM *timeData, 1175 const int interleaved, 1176 const UCHAR *channelMapping, 1177 const int elementIndex, 1178 const int numInChannels, 1179 int *numOutChannels, 1180 const int psPossible 1181 ) 1182 { 1183 SBR_DECODER_ELEMENT *hSbrElement = self->pSbrElement[elementIndex]; 1184 HANDLE_SBR_CHANNEL *pSbrChannel = self->pSbrElement[elementIndex]->pSbrChannel; 1185 HANDLE_SBR_HEADER_DATA hSbrHeader = &self->sbrHeader[elementIndex][hSbrElement->useHeaderSlot[hSbrElement->useFrameSlot]]; 1186 HANDLE_PS_DEC h_ps_d = self->hParametricStereoDec; 1187 1188 /* get memory for frame data from scratch */ 1189 SBR_FRAME_DATA *hFrameDataLeft = &hSbrElement->pSbrChannel[0]->frameData[hSbrElement->useFrameSlot]; 1190 SBR_FRAME_DATA *hFrameDataRight = &hSbrElement->pSbrChannel[1]->frameData[hSbrElement->useFrameSlot]; 1191 1192 SBR_ERROR errorStatus = SBRDEC_OK; 1193 1194 1195 INT strideIn, strideOut, offset0, offset1; 1196 INT codecFrameSize = self->codecFrameSize; 1197 1198 int stereo = (hSbrElement->elementID == ID_CPE) ? 1 : 0; 1199 int numElementChannels = hSbrElement->nChannels; /* Number of channels of the current SBR element */ 1200 1201 /* Update the header error flag */ 1202 hSbrHeader->frameErrorFlag = hSbrElement->frameErrorFlag[hSbrElement->useFrameSlot]; 1203 1204 /* 1205 Prepare filterbank for upsampling if no valid bit stream data is available. 1206 */ 1207 if ( hSbrHeader->syncState == SBR_NOT_INITIALIZED ) 1208 { 1209 errorStatus = initHeaderData( 1210 hSbrHeader, 1211 self->sampleRateIn, 1212 self->sampleRateOut, 1213 codecFrameSize, 1214 self->flags 1215 ); 1216 1217 if (errorStatus != SBRDEC_OK) { 1218 return errorStatus; 1219 } 1220 1221 hSbrHeader->syncState = UPSAMPLING; 1222 1223 errorStatus = sbrDecoder_HeaderUpdate( 1224 self, 1225 hSbrHeader, 1226 HEADER_NOT_PRESENT, 1227 pSbrChannel, 1228 hSbrElement->nChannels 1229 ); 1230 1231 if (errorStatus != SBRDEC_OK) { 1232 hSbrHeader->syncState = SBR_NOT_INITIALIZED; 1233 return errorStatus; 1234 } 1235 } 1236 1237 /* reset */ 1238 if (hSbrHeader->status & SBRDEC_HDR_STAT_RESET) { 1239 int ch; 1240 for (ch = 0 ; ch < numElementChannels; ch++) { 1241 SBR_ERROR errorStatusTmp = SBRDEC_OK; 1242 1243 errorStatusTmp = resetSbrDec ( 1244 &pSbrChannel[ch]->SbrDec, 1245 hSbrHeader, 1246 &pSbrChannel[ch]->prevFrameData, 1247 self->flags & SBRDEC_LOW_POWER, 1248 self->synDownsampleFac 1249 ); 1250 1251 if (errorStatusTmp != SBRDEC_OK) { 1252 errorStatus = errorStatusTmp; 1253 } 1254 } 1255 hSbrHeader->status &= ~SBRDEC_HDR_STAT_RESET; 1256 } 1257 1258 /* decoding */ 1259 if ( (hSbrHeader->syncState == SBR_ACTIVE) 1260 || ((hSbrHeader->syncState == SBR_HEADER) && (hSbrHeader->frameErrorFlag == 0)) ) 1261 { 1262 errorStatus = SBRDEC_OK; 1263 1264 decodeSbrData (hSbrHeader, 1265 hFrameDataLeft, 1266 &pSbrChannel[0]->prevFrameData, 1267 (stereo) ? hFrameDataRight : NULL, 1268 (stereo) ? &pSbrChannel[1]->prevFrameData : NULL); 1269 1270 1271 /* Now we have a full parameter set and can do parameter 1272 based concealment instead of plain upsampling. */ 1273 hSbrHeader->syncState = SBR_ACTIVE; 1274 } 1275 1276 /* decode PS data if available */ 1277 if (h_ps_d != NULL && psPossible) { 1278 int applyPs = 1; 1279 1280 /* define which frame delay line slot to process */ 1281 h_ps_d->processSlot = hSbrElement->useFrameSlot; 1282 1283 applyPs = DecodePs(h_ps_d, hSbrHeader->frameErrorFlag); 1284 self->flags |= (applyPs) ? SBRDEC_PS_DECODED : 0; 1285 } 1286 1287 /* Set strides for reading and writing */ 1288 if (interleaved) { 1289 strideIn = numInChannels; 1290 if ( psPossible ) 1291 strideOut = (numInChannels < 2) ? 2 : numInChannels; 1292 else 1293 strideOut = numInChannels; 1294 offset0 = channelMapping[0]; 1295 offset1 = channelMapping[1]; 1296 } else { 1297 strideIn = 1; 1298 strideOut = 1; 1299 offset0 = channelMapping[0]*2*codecFrameSize; 1300 offset1 = channelMapping[1]*2*codecFrameSize; 1301 } 1302 1303 /* use same buffers for left and right channel and apply PS per timeslot */ 1304 /* Process left channel */ 1305 //FDKprintf("self->codecFrameSize %d\t%d\n",self->codecFrameSize,self->sampleRateIn); 1306 sbr_dec (&pSbrChannel[0]->SbrDec, 1307 timeData + offset0, 1308 timeData + offset0, 1309 &pSbrChannel[1]->SbrDec, 1310 timeData + offset1, 1311 strideIn, 1312 strideOut, 1313 hSbrHeader, 1314 hFrameDataLeft, 1315 &pSbrChannel[0]->prevFrameData, 1316 (hSbrHeader->syncState == SBR_ACTIVE), 1317 h_ps_d, 1318 self->flags 1319 ); 1320 1321 if (stereo) { 1322 /* Process right channel */ 1323 sbr_dec (&pSbrChannel[1]->SbrDec, 1324 timeData + offset1, 1325 timeData + offset1, 1326 NULL, 1327 NULL, 1328 strideIn, 1329 strideOut, 1330 hSbrHeader, 1331 hFrameDataRight, 1332 &pSbrChannel[1]->prevFrameData, 1333 (hSbrHeader->syncState == SBR_ACTIVE), 1334 NULL, 1335 self->flags 1336 ); 1337 } 1338 1339 if (h_ps_d != NULL) { 1340 /* save PS status for next run */ 1341 h_ps_d->psDecodedPrv = (self->flags & SBRDEC_PS_DECODED) ? 1 : 0 ; 1342 } 1343 1344 if ( psPossible 1345 ) 1346 { 1347 FDK_ASSERT(strideOut > 1); 1348 if ( !(self->flags & SBRDEC_PS_DECODED) ) { 1349 /* A decoder which is able to decode PS has to produce a stereo output even if no PS data is availble. */ 1350 /* So copy left channel to right channel. */ 1351 if (interleaved) { 1352 INT_PCM *ptr; 1353 INT i; 1354 FDK_ASSERT(strideOut == 2); 1355 1356 ptr = timeData; 1357 for (i = codecFrameSize; i--; ) 1358 { 1359 INT_PCM tmp; /* This temporal variable is required because some compilers can't do *ptr++ = *ptr++ correctly. */ 1360 tmp = *ptr++; *ptr++ = tmp; 1361 tmp = *ptr++; *ptr++ = tmp; 1362 } 1363 } else { 1364 FDKmemcpy( timeData+2*codecFrameSize, timeData, 2*codecFrameSize*sizeof(INT_PCM) ); 1365 } 1366 } 1367 *numOutChannels = 2; /* Output minimum two channels when PS is enabled. */ 1368 } 1369 1370 return errorStatus; 1371 } 1372 1373 1374 SBR_ERROR sbrDecoder_Apply ( HANDLE_SBRDECODER self, 1375 INT_PCM *timeData, 1376 int *numChannels, 1377 int *sampleRate, 1378 const UCHAR channelMapping[(6)], 1379 const int interleaved, 1380 const int coreDecodedOk, 1381 UCHAR *psDecoded ) 1382 { 1383 SBR_ERROR errorStatus = SBRDEC_OK; 1384 1385 int psPossible = 0; 1386 int sbrElementNum; 1387 int numCoreChannels = *numChannels; 1388 int numSbrChannels = 0; 1389 1390 psPossible = *psDecoded; 1391 1392 if (self->numSbrElements < 1) { 1393 /* exit immediately to avoid access violations */ 1394 return SBRDEC_CREATE_ERROR; 1395 } 1396 1397 /* Sanity check of allocated SBR elements. */ 1398 for (sbrElementNum=0; sbrElementNum<self->numSbrElements; sbrElementNum++) { 1399 if (self->pSbrElement[sbrElementNum] == NULL) { 1400 return SBRDEC_CREATE_ERROR; 1401 } 1402 } 1403 1404 if (self->numSbrElements != 1 || self->pSbrElement[0]->elementID != ID_SCE) { 1405 psPossible = 0; 1406 } 1407 1408 1409 /* In case of non-interleaved time domain data and upsampling, make room for bigger SBR output. */ 1410 if (self->synDownsampleFac == 1 && interleaved == 0) { 1411 int c, outputFrameSize; 1412 1413 outputFrameSize = 1414 self->pSbrElement[0]->pSbrChannel[0]->SbrDec.SynthesisQMF.no_channels 1415 * self->pSbrElement[0]->pSbrChannel[0]->SbrDec.SynthesisQMF.no_col; 1416 1417 for (c=numCoreChannels-1; c>0; c--) { 1418 FDKmemmove(timeData + c*outputFrameSize, timeData + c*self->codecFrameSize , self->codecFrameSize*sizeof(INT_PCM)); 1419 } 1420 } 1421 1422 1423 /* Make sure that even if no SBR data was found/parsed *psDecoded is returned 1 if psPossible was 0. */ 1424 if (psPossible == 0) { 1425 self->flags &= ~SBRDEC_PS_DECODED; 1426 } 1427 1428 /* Loop over SBR elements */ 1429 for (sbrElementNum = 0; sbrElementNum<self->numSbrElements; sbrElementNum++) 1430 { 1431 int numElementChan; 1432 1433 if (psPossible && self->pSbrElement[sbrElementNum]->pSbrChannel[1] == NULL) { 1434 errorStatus = SBRDEC_UNSUPPORTED_CONFIG; 1435 goto bail; 1436 } 1437 1438 numElementChan = (self->pSbrElement[sbrElementNum]->elementID == ID_CPE) ? 2 : 1; 1439 1440 /* If core signal is bad then force upsampling */ 1441 if ( ! coreDecodedOk ) { 1442 self->pSbrElement[sbrElementNum]->frameErrorFlag[self->pSbrElement[sbrElementNum]->useFrameSlot] = 1; 1443 } 1444 1445 errorStatus = sbrDecoder_DecodeElement ( 1446 self, 1447 timeData, 1448 interleaved, 1449 channelMapping, 1450 sbrElementNum, 1451 numCoreChannels, 1452 &numElementChan, 1453 psPossible 1454 ); 1455 1456 if (errorStatus != SBRDEC_OK) { 1457 goto bail; 1458 } 1459 1460 numSbrChannels += numElementChan; 1461 channelMapping += numElementChan; 1462 1463 if (numSbrChannels >= numCoreChannels) { 1464 break; 1465 } 1466 } 1467 1468 /* Update numChannels and samplerate */ 1469 *numChannels = numSbrChannels; 1470 *sampleRate = self->sampleRateOut; 1471 *psDecoded = (self->flags & SBRDEC_PS_DECODED) ? 1 : 0; 1472 1473 1474 1475 bail: 1476 1477 return errorStatus; 1478 } 1479 1480 1481 SBR_ERROR sbrDecoder_Close ( HANDLE_SBRDECODER *pSelf ) 1482 { 1483 HANDLE_SBRDECODER self = *pSelf; 1484 int i; 1485 1486 if (self != NULL) 1487 { 1488 if (self->hParametricStereoDec != NULL) { 1489 DeletePsDec ( &self->hParametricStereoDec ); 1490 } 1491 1492 if (self->workBuffer1 != NULL) { 1493 FreeRam_SbrDecWorkBuffer1(&self->workBuffer1); 1494 } 1495 if (self->workBuffer2 != NULL) { 1496 FreeRam_SbrDecWorkBuffer2(&self->workBuffer2); 1497 } 1498 1499 for (i = 0; i < (4); i++) { 1500 sbrDecoder_DestroyElement( self, i ); 1501 } 1502 1503 FreeRam_SbrDecoder(pSelf); 1504 } 1505 1506 return SBRDEC_OK; 1507 } 1508 1509 1510 INT sbrDecoder_GetLibInfo( LIB_INFO *info ) 1511 { 1512 int i; 1513 1514 if (info == NULL) { 1515 return -1; 1516 } 1517 1518 /* search for next free tab */ 1519 for (i = 0; i < FDK_MODULE_LAST; i++) { 1520 if (info[i].module_id == FDK_NONE) 1521 break; 1522 } 1523 if (i == FDK_MODULE_LAST) 1524 return -1; 1525 info += i; 1526 1527 info->module_id = FDK_SBRDEC; 1528 info->version = LIB_VERSION(SBRDECODER_LIB_VL0, SBRDECODER_LIB_VL1, SBRDECODER_LIB_VL2); 1529 LIB_VERSION_STRING(info); 1530 info->build_date = (char *)SBRDECODER_LIB_BUILD_DATE; 1531 info->build_time = (char *)SBRDECODER_LIB_BUILD_TIME; 1532 info->title = (char *)SBRDECODER_LIB_TITLE; 1533 1534 /* Set flags */ 1535 info->flags = 0 1536 | CAPF_SBR_HQ 1537 | CAPF_SBR_LP 1538 | CAPF_SBR_PS_MPEG 1539 | CAPF_SBR_CONCEALMENT 1540 | CAPF_SBR_DRC 1541 ; 1542 /* End of flags */ 1543 1544 return 0; 1545 } 1546 1547