1 2 /* ----------------------------------------------------------------------------------------------------------- 3 Software License for The Fraunhofer FDK AAC Codec Library for Android 4 5 Copyright 1995 - 2012 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 1 140 #define SBRDECODER_LIB_VL2 2 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 > 24000 221 ) 222 { 223 sbrError = SBRDEC_UNSUPPORTED_CONFIG; 224 goto bail; 225 } 226 227 if ( sampleRateOut > 48000 ) 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 /* Loop over SBR elements */ 735 for (elementIndex = 0; elementIndex < self->numSbrElements; elementIndex++) 736 { 737 HANDLE_SBR_HEADER_DATA hSbrHeader; 738 int headerIndex = getHeaderSlot(self->pSbrElement[elementIndex]->useFrameSlot, 739 self->pSbrElement[elementIndex]->useHeaderSlot); 740 741 hSbrHeader = &(self->sbrHeader[elementIndex][headerIndex]); 742 743 /* Set sync state UPSAMPLING for the corresponding slot. 744 This switches off bitstream parsing until a new header arrives. */ 745 hSbrHeader->syncState = UPSAMPLING; 746 hSbrHeader->status |= SBRDEC_HDR_STAT_UPDATE; 747 } 748 } 749 break; 750 default: 751 errorStatus = SBRDEC_SET_PARAM_FAIL; 752 break; 753 } /* switch(param) */ 754 755 return (errorStatus); 756 } 757 758 static 759 SBRDEC_DRC_CHANNEL * sbrDecoder_drcGetChannel( const HANDLE_SBRDECODER self, const INT channel ) 760 { 761 SBRDEC_DRC_CHANNEL *pSbrDrcChannelData = NULL; 762 int elementIndex, elChanIdx=0, numCh=0; 763 764 for (elementIndex = 0; (elementIndex < (4)) && (numCh <= channel); elementIndex++) 765 { 766 SBR_DECODER_ELEMENT *pSbrElement = self->pSbrElement[elementIndex]; 767 int c, elChannels; 768 769 elChanIdx = 0; 770 if (pSbrElement == NULL) break; 771 772 /* Determine amount of channels for this element */ 773 switch (pSbrElement->elementID) { 774 case ID_CPE: elChannels = 2; 775 break; 776 case ID_LFE: 777 case ID_SCE: elChannels = 1; 778 break; 779 case ID_NONE: 780 default: elChannels = 0; 781 break; 782 } 783 784 /* Limit with actual allocated element channels */ 785 elChannels = FDKmin(elChannels, pSbrElement->nChannels); 786 787 for (c = 0; (c < elChannels) && (numCh <= channel); c++) { 788 if (pSbrElement->pSbrChannel[elChanIdx] != NULL) { 789 numCh++; 790 elChanIdx++; 791 } 792 } 793 } 794 elementIndex -= 1; 795 elChanIdx -= 1; 796 797 if (elChanIdx < 0 || elementIndex < 0) { 798 return NULL; 799 } 800 801 if ( self->pSbrElement[elementIndex] != NULL ) { 802 if ( self->pSbrElement[elementIndex]->pSbrChannel[elChanIdx] != NULL ) 803 { 804 pSbrDrcChannelData = &self->pSbrElement[elementIndex]->pSbrChannel[elChanIdx]->SbrDec.sbrDrcChannel; 805 } 806 } 807 808 return (pSbrDrcChannelData); 809 } 810 811 SBR_ERROR sbrDecoder_drcFeedChannel ( HANDLE_SBRDECODER self, 812 INT ch, 813 UINT numBands, 814 FIXP_DBL *pNextFact_mag, 815 INT nextFact_exp, 816 SHORT drcInterpolationScheme, 817 UCHAR winSequence, 818 USHORT *pBandTop ) 819 { 820 SBRDEC_DRC_CHANNEL *pSbrDrcChannelData = NULL; 821 822 if (self == NULL) { 823 return SBRDEC_NOT_INITIALIZED; 824 } 825 if (ch > (6) || pNextFact_mag == NULL) { 826 return SBRDEC_SET_PARAM_FAIL; 827 } 828 829 /* Find the right SBR channel */ 830 pSbrDrcChannelData = sbrDecoder_drcGetChannel( self, ch ); 831 832 if ( pSbrDrcChannelData != NULL ) { 833 int i; 834 835 pSbrDrcChannelData->enable = 1; 836 pSbrDrcChannelData->numBandsNext = numBands; 837 838 pSbrDrcChannelData->winSequenceNext = winSequence; 839 pSbrDrcChannelData->drcInterpolationSchemeNext = drcInterpolationScheme; 840 pSbrDrcChannelData->nextFact_exp = nextFact_exp; 841 842 for (i = 0; i < (int)numBands; i++) { 843 pSbrDrcChannelData->bandTopNext[i] = pBandTop[i]; 844 pSbrDrcChannelData->nextFact_mag[i] = pNextFact_mag[i]; 845 } 846 } 847 848 return SBRDEC_OK; 849 } 850 851 852 void sbrDecoder_drcDisable ( HANDLE_SBRDECODER self, 853 INT ch ) 854 { 855 SBRDEC_DRC_CHANNEL *pSbrDrcChannelData = NULL; 856 857 if ( (self == NULL) 858 || (ch > (6)) 859 || (self->numSbrElements == 0) 860 || (self->numSbrChannels == 0) ) { 861 return; 862 } 863 864 /* Find the right SBR channel */ 865 pSbrDrcChannelData = sbrDecoder_drcGetChannel( self, ch ); 866 867 if ( pSbrDrcChannelData != NULL ) { 868 pSbrDrcChannelData->enable = 0; 869 } 870 } 871 872 873 874 SBR_ERROR sbrDecoder_Parse( 875 HANDLE_SBRDECODER self, 876 HANDLE_FDK_BITSTREAM hBs, 877 int *count, 878 int bsPayLen, 879 int crcFlag, 880 MP4_ELEMENT_ID prevElement, 881 int elementIndex, 882 int fGlobalIndependencyFlag 883 ) 884 { 885 SBR_DECODER_ELEMENT *hSbrElement; 886 HANDLE_SBR_HEADER_DATA hSbrHeader; 887 HANDLE_SBR_CHANNEL *pSbrChannel; 888 889 SBR_FRAME_DATA *hFrameDataLeft; 890 SBR_FRAME_DATA *hFrameDataRight; 891 892 SBR_ERROR errorStatus = SBRDEC_OK; 893 SBR_SYNC_STATE initialSyncState; 894 SBR_HEADER_STATUS headerStatus = HEADER_NOT_PRESENT; 895 896 INT startPos; 897 INT CRCLen = 0; 898 899 int stereo; 900 int fDoDecodeSbrData = 1; 901 902 int lastSlot, lastHdrSlot = 0, thisHdrSlot; 903 904 /* Remember start position of SBR element */ 905 startPos = FDKgetValidBits(hBs); 906 907 /* SBR sanity checks */ 908 if ( self == NULL || self->pSbrElement[elementIndex] == NULL ) { 909 errorStatus = SBRDEC_NOT_INITIALIZED; 910 goto bail; 911 } 912 913 hSbrElement = self->pSbrElement[elementIndex]; 914 915 lastSlot = (hSbrElement->useFrameSlot > 0) ? hSbrElement->useFrameSlot-1 : self->numDelayFrames; 916 lastHdrSlot = hSbrElement->useHeaderSlot[lastSlot]; 917 thisHdrSlot = getHeaderSlot( hSbrElement->useFrameSlot, hSbrElement->useHeaderSlot ); /* Get a free header slot not used by frames not processed yet. */ 918 919 /* Assign the free slot to store a new header if there is one. */ 920 hSbrHeader = &self->sbrHeader[elementIndex][thisHdrSlot]; 921 922 pSbrChannel = hSbrElement->pSbrChannel; 923 stereo = (hSbrElement->elementID == ID_CPE) ? 1 : 0; 924 925 hFrameDataLeft = &self->pSbrElement[elementIndex]->pSbrChannel[0]->frameData[hSbrElement->useFrameSlot]; 926 hFrameDataRight = &self->pSbrElement[elementIndex]->pSbrChannel[1]->frameData[hSbrElement->useFrameSlot]; 927 928 initialSyncState = hSbrHeader->syncState; 929 930 /* reset PS flag; will be set after PS was found */ 931 self->flags &= ~SBRDEC_PS_DECODED; 932 933 if (hSbrHeader->status & SBRDEC_HDR_STAT_UPDATE) { 934 /* Got a new header from extern (e.g. from an ASC) */ 935 headerStatus = HEADER_OK; 936 hSbrHeader->status &= ~SBRDEC_HDR_STAT_UPDATE; 937 } 938 else if (thisHdrSlot != lastHdrSlot) { 939 /* Copy the last header into this slot otherwise the 940 header compare will trigger more HEADER_RESETs than needed. */ 941 copySbrHeader( hSbrHeader, &self->sbrHeader[elementIndex][lastHdrSlot] ); 942 } 943 944 /* 945 Check if bit stream data is valid and matches the element context 946 */ 947 if ( ((prevElement != ID_SCE) && (prevElement != ID_CPE)) || prevElement != hSbrElement->elementID) { 948 /* In case of LFE we also land here, since there is no LFE SBR element (do upsampling only) */ 949 fDoDecodeSbrData = 0; 950 } 951 952 if (fDoDecodeSbrData) 953 { 954 if ((INT)FDKgetValidBits(hBs) <= 0) { 955 fDoDecodeSbrData = 0; 956 } 957 } 958 959 /* 960 SBR CRC-check 961 */ 962 if (fDoDecodeSbrData) 963 { 964 if (crcFlag == 1) { 965 switch (self->coreCodec) { 966 case AOT_ER_AAC_ELD: 967 FDKpushFor (hBs, 10); 968 /* check sbrcrc later: we don't know the payload length now */ 969 break; 970 default: 971 CRCLen = bsPayLen - 10; /* change: 0 => i */ 972 if (CRCLen < 0) { 973 fDoDecodeSbrData = 0; 974 } else { 975 fDoDecodeSbrData = SbrCrcCheck (hBs, CRCLen); 976 } 977 break; 978 } 979 } 980 } /* if (fDoDecodeSbrData) */ 981 982 /* 983 Read in the header data and issue a reset if change occured 984 */ 985 if (fDoDecodeSbrData) 986 { 987 int sbrHeaderPresent; 988 989 { 990 sbrHeaderPresent = FDKreadBit(hBs); 991 } 992 993 if ( sbrHeaderPresent ) { 994 headerStatus = sbrGetHeaderData (hSbrHeader, 995 hBs, 996 self->flags, 997 1); 998 } 999 1000 if (headerStatus == HEADER_RESET) 1001 { 1002 errorStatus = sbrDecoder_HeaderUpdate( 1003 self, 1004 hSbrHeader, 1005 headerStatus, 1006 pSbrChannel, 1007 hSbrElement->nChannels 1008 ); 1009 1010 if (errorStatus == SBRDEC_OK) { 1011 hSbrHeader->syncState = SBR_HEADER; 1012 } else { 1013 hSbrHeader->syncState = SBR_NOT_INITIALIZED; 1014 } 1015 } 1016 1017 if (errorStatus != SBRDEC_OK) { 1018 fDoDecodeSbrData = 0; 1019 } 1020 } /* if (fDoDecodeSbrData) */ 1021 1022 /* 1023 Print debugging output only if state has changed 1024 */ 1025 1026 /* read frame data */ 1027 if ((hSbrHeader->syncState >= SBR_HEADER) && fDoDecodeSbrData) { 1028 int sbrFrameOk; 1029 /* read the SBR element data */ 1030 if (stereo) { 1031 sbrFrameOk = sbrGetChannelPairElement(hSbrHeader, 1032 hFrameDataLeft, 1033 hFrameDataRight, 1034 hBs, 1035 self->flags, 1036 self->pSbrElement[elementIndex]->transposerSettings.overlap); 1037 } 1038 else { 1039 if (self->hParametricStereoDec != NULL) { 1040 /* update slot index for PS bitstream parsing */ 1041 self->hParametricStereoDec->bsLastSlot = self->hParametricStereoDec->bsReadSlot; 1042 self->hParametricStereoDec->bsReadSlot = hSbrElement->useFrameSlot; 1043 } 1044 sbrFrameOk = sbrGetSingleChannelElement(hSbrHeader, 1045 hFrameDataLeft, 1046 hBs, 1047 self->hParametricStereoDec, 1048 self->flags, 1049 self->pSbrElement[elementIndex]->transposerSettings.overlap); 1050 } 1051 if (!sbrFrameOk) { 1052 fDoDecodeSbrData = 0; 1053 } 1054 else { 1055 INT valBits; 1056 1057 if (bsPayLen > 0) { 1058 valBits = bsPayLen - ((INT)startPos - (INT)FDKgetValidBits(hBs)); 1059 } else { 1060 valBits = (INT)FDKgetValidBits(hBs); 1061 } 1062 1063 if ( crcFlag == 1 ) { 1064 switch (self->coreCodec) { 1065 case AOT_ER_AAC_ELD: 1066 { 1067 /* late crc check for eld */ 1068 INT payloadbits = (INT)startPos - (INT)FDKgetValidBits(hBs) - startPos; 1069 INT crcLen = payloadbits - 10; 1070 FDKpushBack(hBs, payloadbits); 1071 fDoDecodeSbrData = SbrCrcCheck (hBs, crcLen); 1072 FDKpushFor(hBs, crcLen); 1073 } 1074 break; 1075 default: 1076 break; 1077 } 1078 } 1079 1080 /* sanity check of remaining bits */ 1081 if (valBits < 0) { 1082 fDoDecodeSbrData = 0; 1083 } else { 1084 switch (self->coreCodec) { 1085 case AOT_SBR: 1086 case AOT_PS: 1087 case AOT_AAC_LC: 1088 { 1089 /* This sanity check is only meaningful with General Audio bitstreams */ 1090 int alignBits = valBits & 0x7; 1091 1092 if (valBits > alignBits) { 1093 fDoDecodeSbrData = 0; 1094 } 1095 } 1096 break; 1097 default: 1098 /* No sanity check available */ 1099 break; 1100 } 1101 } 1102 } 1103 } 1104 1105 if (!fDoDecodeSbrData) { 1106 /* Set error flag for this slot to trigger concealment */ 1107 self->pSbrElement[elementIndex]->frameErrorFlag[hSbrElement->useFrameSlot] = 1; 1108 errorStatus = SBRDEC_PARSE_ERROR; 1109 } else { 1110 /* Everything seems to be ok so clear the error flag */ 1111 self->pSbrElement[elementIndex]->frameErrorFlag[hSbrElement->useFrameSlot] = 0; 1112 } 1113 1114 if (!stereo) { 1115 /* Turn coupling off explicitely to avoid access to absent right frame data 1116 that might occur with corrupt bitstreams. */ 1117 hFrameDataLeft->coupling = COUPLING_OFF; 1118 } 1119 1120 bail: 1121 if (errorStatus == SBRDEC_OK) { 1122 if (headerStatus == HEADER_NOT_PRESENT) { 1123 /* Use the old header for this frame */ 1124 hSbrElement->useHeaderSlot[hSbrElement->useFrameSlot] = lastHdrSlot; 1125 } else { 1126 /* Use the new header for this frame */ 1127 hSbrElement->useHeaderSlot[hSbrElement->useFrameSlot] = thisHdrSlot; 1128 } 1129 1130 /* Move frame pointer to the next slot which is up to be decoded/applied next */ 1131 hSbrElement->useFrameSlot = (hSbrElement->useFrameSlot+1) % (self->numDelayFrames+1); 1132 } 1133 1134 *count -= startPos - FDKgetValidBits(hBs); 1135 1136 return errorStatus; 1137 } 1138 1139 1140 /** 1141 * \brief Render one SBR element into time domain signal. 1142 * \param self SBR decoder handle 1143 * \param timeData pointer to output buffer 1144 * \param interleaved flag indicating interleaved channel output 1145 * \param channelMapping pointer to UCHAR array where next 2 channel offsets are stored. 1146 * \param elementIndex enumerating index of the SBR element to render. 1147 * \param numInChannels number of channels from core coder (reading stride). 1148 * \param numOutChannels pointer to a location to return number of output channels. 1149 * \param psPossible flag indicating if PS is possible or not. 1150 * \return SBRDEC_OK if successfull, else error code 1151 */ 1152 static SBR_ERROR 1153 sbrDecoder_DecodeElement ( 1154 HANDLE_SBRDECODER self, 1155 INT_PCM *timeData, 1156 const int interleaved, 1157 const UCHAR *channelMapping, 1158 const int elementIndex, 1159 const int numInChannels, 1160 int *numOutChannels, 1161 const int psPossible 1162 ) 1163 { 1164 SBR_DECODER_ELEMENT *hSbrElement = self->pSbrElement[elementIndex]; 1165 HANDLE_SBR_CHANNEL *pSbrChannel = self->pSbrElement[elementIndex]->pSbrChannel; 1166 HANDLE_SBR_HEADER_DATA hSbrHeader = &self->sbrHeader[elementIndex][hSbrElement->useHeaderSlot[hSbrElement->useFrameSlot]]; 1167 HANDLE_PS_DEC h_ps_d = self->hParametricStereoDec; 1168 1169 /* get memory for frame data from scratch */ 1170 SBR_FRAME_DATA *hFrameDataLeft = &hSbrElement->pSbrChannel[0]->frameData[hSbrElement->useFrameSlot]; 1171 SBR_FRAME_DATA *hFrameDataRight = &hSbrElement->pSbrChannel[1]->frameData[hSbrElement->useFrameSlot]; 1172 1173 SBR_ERROR errorStatus = SBRDEC_OK; 1174 1175 1176 INT strideIn, strideOut, offset0, offset1; 1177 INT codecFrameSize = self->codecFrameSize; 1178 1179 int stereo = (hSbrElement->elementID == ID_CPE) ? 1 : 0; 1180 int numElementChannels = hSbrElement->nChannels; /* Number of channels of the current SBR element */ 1181 1182 /* Update the header error flag */ 1183 hSbrHeader->frameErrorFlag = hSbrElement->frameErrorFlag[hSbrElement->useFrameSlot]; 1184 1185 /* 1186 Prepare filterbank for upsampling if no valid bit stream data is available. 1187 */ 1188 if ( hSbrHeader->syncState == SBR_NOT_INITIALIZED ) 1189 { 1190 errorStatus = initHeaderData( 1191 hSbrHeader, 1192 self->sampleRateIn, 1193 self->sampleRateOut, 1194 codecFrameSize, 1195 self->flags 1196 ); 1197 1198 if (errorStatus != SBRDEC_OK) { 1199 return errorStatus; 1200 } 1201 1202 hSbrHeader->syncState = UPSAMPLING; 1203 1204 errorStatus = sbrDecoder_HeaderUpdate( 1205 self, 1206 hSbrHeader, 1207 HEADER_NOT_PRESENT, 1208 pSbrChannel, 1209 hSbrElement->nChannels 1210 ); 1211 1212 if (errorStatus != SBRDEC_OK) { 1213 hSbrHeader->syncState = SBR_NOT_INITIALIZED; 1214 return errorStatus; 1215 } 1216 } 1217 1218 /* reset */ 1219 if (hSbrHeader->status & SBRDEC_HDR_STAT_RESET) { 1220 int ch; 1221 for (ch = 0 ; ch < numElementChannels; ch++) { 1222 SBR_ERROR errorStatusTmp = SBRDEC_OK; 1223 1224 errorStatusTmp = resetSbrDec ( 1225 &pSbrChannel[ch]->SbrDec, 1226 hSbrHeader, 1227 &pSbrChannel[ch]->prevFrameData, 1228 self->flags & SBRDEC_LOW_POWER, 1229 self->synDownsampleFac 1230 ); 1231 1232 if (errorStatusTmp != SBRDEC_OK) { 1233 errorStatus = errorStatusTmp; 1234 } 1235 } 1236 hSbrHeader->status &= ~SBRDEC_HDR_STAT_RESET; 1237 } 1238 1239 /* decoding */ 1240 if ( (hSbrHeader->syncState == SBR_ACTIVE) 1241 || ((hSbrHeader->syncState == SBR_HEADER) && (hSbrHeader->frameErrorFlag == 0)) ) 1242 { 1243 errorStatus = SBRDEC_OK; 1244 1245 decodeSbrData (hSbrHeader, 1246 hFrameDataLeft, 1247 &pSbrChannel[0]->prevFrameData, 1248 (stereo) ? hFrameDataRight : NULL, 1249 (stereo) ? &pSbrChannel[1]->prevFrameData : NULL); 1250 1251 1252 /* Now we have a full parameter set and can do parameter 1253 based concealment instead of plain upsampling. */ 1254 hSbrHeader->syncState = SBR_ACTIVE; 1255 } 1256 1257 /* decode PS data if available */ 1258 if (h_ps_d != NULL && psPossible) { 1259 int applyPs = 1; 1260 1261 /* define which frame delay line slot to process */ 1262 h_ps_d->processSlot = hSbrElement->useFrameSlot; 1263 1264 applyPs = DecodePs(h_ps_d, hSbrHeader->frameErrorFlag); 1265 self->flags |= (applyPs) ? SBRDEC_PS_DECODED : 0; 1266 } 1267 1268 /* Set strides for reading and writing */ 1269 if (interleaved) { 1270 strideIn = numInChannels; 1271 if ( psPossible ) 1272 strideOut = (numInChannels < 2) ? 2 : numInChannels; 1273 else 1274 strideOut = numInChannels; 1275 offset0 = channelMapping[0]; 1276 offset1 = channelMapping[1]; 1277 } else { 1278 strideIn = 1; 1279 strideOut = 1; 1280 offset0 = channelMapping[0]*2*codecFrameSize; 1281 offset1 = channelMapping[1]*2*codecFrameSize; 1282 } 1283 1284 /* use same buffers for left and right channel and apply PS per timeslot */ 1285 /* Process left channel */ 1286 //FDKprintf("self->codecFrameSize %d\t%d\n",self->codecFrameSize,self->sampleRateIn); 1287 sbr_dec (&pSbrChannel[0]->SbrDec, 1288 timeData + offset0, 1289 timeData + offset0, 1290 &pSbrChannel[1]->SbrDec, 1291 timeData + offset1, 1292 strideIn, 1293 strideOut, 1294 hSbrHeader, 1295 hFrameDataLeft, 1296 &pSbrChannel[0]->prevFrameData, 1297 (hSbrHeader->syncState == SBR_ACTIVE), 1298 h_ps_d, 1299 self->flags 1300 ); 1301 1302 if (stereo) { 1303 /* Process right channel */ 1304 sbr_dec (&pSbrChannel[1]->SbrDec, 1305 timeData + offset1, 1306 timeData + offset1, 1307 NULL, 1308 NULL, 1309 strideIn, 1310 strideOut, 1311 hSbrHeader, 1312 hFrameDataRight, 1313 &pSbrChannel[1]->prevFrameData, 1314 (hSbrHeader->syncState == SBR_ACTIVE), 1315 NULL, 1316 self->flags 1317 ); 1318 } 1319 1320 if (h_ps_d != NULL) { 1321 /* save PS status for next run */ 1322 h_ps_d->psDecodedPrv = (self->flags & SBRDEC_PS_DECODED) ? 1 : 0 ; 1323 } 1324 1325 if ( psPossible 1326 ) 1327 { 1328 FDK_ASSERT(strideOut > 1); 1329 if ( !(self->flags & SBRDEC_PS_DECODED) ) { 1330 /* A decoder which is able to decode PS has to produce a stereo output even if no PS data is availble. */ 1331 /* So copy left channel to right channel. */ 1332 if (interleaved) { 1333 INT_PCM *ptr; 1334 INT i; 1335 FDK_ASSERT(strideOut == 2); 1336 1337 ptr = timeData; 1338 for (i = codecFrameSize; i--; ) 1339 { 1340 INT_PCM tmp; /* This temporal variable is required because some compilers can't do *ptr++ = *ptr++ correctly. */ 1341 tmp = *ptr++; *ptr++ = tmp; 1342 tmp = *ptr++; *ptr++ = tmp; 1343 } 1344 } else { 1345 FDKmemcpy( timeData+2*codecFrameSize, timeData, 2*codecFrameSize*sizeof(INT_PCM) ); 1346 } 1347 } 1348 *numOutChannels = 2; /* Output minimum two channels when PS is enabled. */ 1349 } 1350 1351 return errorStatus; 1352 } 1353 1354 1355 SBR_ERROR sbrDecoder_Apply ( HANDLE_SBRDECODER self, 1356 INT_PCM *timeData, 1357 int *numChannels, 1358 int *sampleRate, 1359 const UCHAR channelMapping[(6)], 1360 const int interleaved, 1361 const int coreDecodedOk, 1362 UCHAR *psDecoded ) 1363 { 1364 SBR_ERROR errorStatus = SBRDEC_OK; 1365 1366 int psPossible = 0; 1367 int sbrElementNum; 1368 int numCoreChannels = *numChannels; 1369 int numSbrChannels = 0; 1370 1371 psPossible = *psDecoded; 1372 1373 if (self->numSbrElements < 1) { 1374 /* exit immediately to avoid access violations */ 1375 return SBRDEC_CREATE_ERROR; 1376 } 1377 1378 /* Sanity check of allocated SBR elements. */ 1379 for (sbrElementNum=0; sbrElementNum<self->numSbrElements; sbrElementNum++) { 1380 if (self->pSbrElement[sbrElementNum] == NULL) { 1381 return SBRDEC_CREATE_ERROR; 1382 } 1383 } 1384 1385 if (self->numSbrElements != 1 || self->pSbrElement[0]->elementID != ID_SCE) { 1386 psPossible = 0; 1387 } 1388 1389 1390 /* In case of non-interleaved time domain data and upsampling, make room for bigger SBR output. */ 1391 if (self->synDownsampleFac == 1 && interleaved == 0) { 1392 int c, outputFrameSize; 1393 1394 outputFrameSize = 1395 self->pSbrElement[0]->pSbrChannel[0]->SbrDec.SynthesisQMF.no_channels 1396 * self->pSbrElement[0]->pSbrChannel[0]->SbrDec.SynthesisQMF.no_col; 1397 1398 for (c=numCoreChannels-1; c>0; c--) { 1399 FDKmemmove(timeData + c*outputFrameSize, timeData + c*self->codecFrameSize , self->codecFrameSize*sizeof(INT_PCM)); 1400 } 1401 } 1402 1403 1404 /* Make sure that even if no SBR data was found/parsed *psDecoded is returned 1 if psPossible was 0. */ 1405 if (psPossible == 0) { 1406 self->flags &= ~SBRDEC_PS_DECODED; 1407 } 1408 1409 /* Loop over SBR elements */ 1410 for (sbrElementNum = 0; sbrElementNum<self->numSbrElements; sbrElementNum++) 1411 { 1412 int numElementChan; 1413 1414 if (psPossible && self->pSbrElement[sbrElementNum]->pSbrChannel[1] == NULL) { 1415 errorStatus = SBRDEC_UNSUPPORTED_CONFIG; 1416 goto bail; 1417 } 1418 1419 numElementChan = (self->pSbrElement[sbrElementNum]->elementID == ID_CPE) ? 2 : 1; 1420 1421 /* If core signal is bad then force upsampling */ 1422 if ( ! coreDecodedOk ) { 1423 self->pSbrElement[sbrElementNum]->frameErrorFlag[self->pSbrElement[sbrElementNum]->useFrameSlot] = 1; 1424 } 1425 1426 errorStatus = sbrDecoder_DecodeElement ( 1427 self, 1428 timeData, 1429 interleaved, 1430 channelMapping, 1431 sbrElementNum, 1432 numCoreChannels, 1433 &numElementChan, 1434 psPossible 1435 ); 1436 1437 if (errorStatus != SBRDEC_OK) { 1438 goto bail; 1439 } 1440 1441 numSbrChannels += numElementChan; 1442 channelMapping += numElementChan; 1443 1444 if (numSbrChannels >= numCoreChannels) { 1445 break; 1446 } 1447 } 1448 1449 /* Update numChannels and samplerate */ 1450 *numChannels = numSbrChannels; 1451 *sampleRate = self->sampleRateOut; 1452 *psDecoded = (self->flags & SBRDEC_PS_DECODED) ? 1 : 0; 1453 1454 1455 1456 bail: 1457 1458 return errorStatus; 1459 } 1460 1461 1462 SBR_ERROR sbrDecoder_Close ( HANDLE_SBRDECODER *pSelf ) 1463 { 1464 HANDLE_SBRDECODER self = *pSelf; 1465 int i; 1466 1467 if (self != NULL) 1468 { 1469 if (self->hParametricStereoDec != NULL) { 1470 DeletePsDec ( &self->hParametricStereoDec ); 1471 } 1472 1473 if (self->workBuffer1 != NULL) { 1474 FreeRam_SbrDecWorkBuffer1(&self->workBuffer1); 1475 } 1476 if (self->workBuffer2 != NULL) { 1477 FreeRam_SbrDecWorkBuffer2(&self->workBuffer2); 1478 } 1479 1480 for (i = 0; i < (4); i++) { 1481 sbrDecoder_DestroyElement( self, i ); 1482 } 1483 1484 FreeRam_SbrDecoder(pSelf); 1485 } 1486 1487 return SBRDEC_OK; 1488 } 1489 1490 1491 INT sbrDecoder_GetLibInfo( LIB_INFO *info ) 1492 { 1493 int i; 1494 1495 if (info == NULL) { 1496 return -1; 1497 } 1498 1499 /* search for next free tab */ 1500 for (i = 0; i < FDK_MODULE_LAST; i++) { 1501 if (info[i].module_id == FDK_NONE) 1502 break; 1503 } 1504 if (i == FDK_MODULE_LAST) 1505 return -1; 1506 info += i; 1507 1508 info->module_id = FDK_SBRDEC; 1509 info->version = LIB_VERSION(SBRDECODER_LIB_VL0, SBRDECODER_LIB_VL1, SBRDECODER_LIB_VL2); 1510 LIB_VERSION_STRING(info); 1511 info->build_date = (char *)SBRDECODER_LIB_BUILD_DATE; 1512 info->build_time = (char *)SBRDECODER_LIB_BUILD_TIME; 1513 info->title = (char *)SBRDECODER_LIB_TITLE; 1514 1515 /* Set flags */ 1516 info->flags = 0 1517 | CAPF_SBR_HQ 1518 | CAPF_SBR_LP 1519 | CAPF_SBR_PS_MPEG 1520 | CAPF_SBR_CONCEALMENT 1521 | CAPF_SBR_DRC 1522 ; 1523 /* End of flags */ 1524 1525 return 0; 1526 } 1527 1528