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 Envelope extraction 87 The functions provided by this module are mostly called by applySBR(). After it is 88 determined that there is valid SBR data, sbrGetHeaderData() might be called if the current 89 SBR data contains an \ref SBR_HEADER_ELEMENT as opposed to a \ref SBR_STANDARD_ELEMENT. This function 90 may return various error codes as defined in #SBR_HEADER_STATUS . Most importantly it returns HEADER_RESET when decoder 91 settings need to be recalculated according to the SBR specifications. In that case applySBR() 92 will initiatite the required re-configuration. 93 94 The header data is stored in a #SBR_HEADER_DATA structure. 95 96 The actual SBR data for the current frame is decoded into SBR_FRAME_DATA stuctures by sbrGetChannelPairElement() 97 [for stereo streams] and sbrGetSingleChannelElement() [for mono streams]. There is no fractional arithmetic involved. 98 99 Once the information is extracted, the data needs to be further prepared before the actual decoding process. 100 This is done in decodeSbrData(). 101 102 \sa Description of buffer management in applySBR(). \ref documentationOverview 103 104 <h1>About the SBR data format:</h1> 105 106 Each frame includes SBR data (side chain information), and can be either the \ref SBR_HEADER_ELEMENT or the \ref SBR_STANDARD_ELEMENT. 107 Parts of the data can be protected by a CRC checksum. 108 109 \anchor SBR_HEADER_ELEMENT <h2>The SBR_HEADER_ELEMENT</h2> 110 111 The SBR_HEADER_ELEMENT can be transmitted with every frame, however, it typically is send every second or so. It contains fundamental 112 information such as SBR sampling frequency and frequency range as well as control signals that do not require frequent changes. It also 113 includes the \ref SBR_STANDARD_ELEMENT. 114 115 Depending on the changes between the information in a current SBR_HEADER_ELEMENT and the previous SBR_HEADER_ELEMENT, the SBR decoder might need 116 to be reset and reconfigured (e.g. new tables need to be calculated). 117 118 \anchor SBR_STANDARD_ELEMENT <h2>The SBR_STANDARD_ELEMENT</h2> 119 120 This data can be subdivided into "side info" and "raw data", where side info is defined as signals needed to decode the raw data 121 and some decoder tuning signals. Raw data is referred to as PCM and Huffman coded envelope and noise floor estimates. The side info also 122 includes information about the time-frequency grid for the current frame. 123 124 \sa \ref documentationOverview 125 */ 126 127 #include "env_extr.h" 128 129 #include "sbr_ram.h" 130 #include "sbr_rom.h" 131 #include "huff_dec.h" 132 133 134 #include "psbitdec.h" 135 136 #define DRM_PARAMETRIC_STEREO 0 137 #define EXTENSION_ID_PS_CODING 2 138 139 140 static int extractFrameInfo (HANDLE_FDK_BITSTREAM hBs, 141 HANDLE_SBR_HEADER_DATA hHeaderData, 142 HANDLE_SBR_FRAME_DATA h_frame_data, 143 const UINT nrOfChannels, 144 const UINT flags 145 ); 146 147 148 static int sbrGetEnvelope (HANDLE_SBR_HEADER_DATA hHeaderData, 149 HANDLE_SBR_FRAME_DATA h_frame_data, 150 HANDLE_FDK_BITSTREAM hBs, 151 const UINT flags); 152 153 static void sbrGetDirectionControlData (HANDLE_SBR_FRAME_DATA hFrameData, 154 HANDLE_FDK_BITSTREAM hBs); 155 156 static void sbrGetNoiseFloorData (HANDLE_SBR_HEADER_DATA hHeaderData, 157 HANDLE_SBR_FRAME_DATA h_frame_data, 158 HANDLE_FDK_BITSTREAM hBs); 159 160 static int checkFrameInfo (FRAME_INFO *pFrameInfo, int numberOfTimeSlots, int overlap, int timeStep); 161 162 SBR_ERROR 163 initHeaderData ( 164 HANDLE_SBR_HEADER_DATA hHeaderData, 165 const int sampleRateIn, 166 const int sampleRateOut, 167 const int samplesPerFrame, 168 const UINT flags 169 ) 170 { 171 HANDLE_FREQ_BAND_DATA hFreq = &hHeaderData->freqBandData; 172 SBR_ERROR sbrError = SBRDEC_OK; 173 int numAnalysisBands; 174 175 if ( sampleRateIn == sampleRateOut ) { 176 hHeaderData->sbrProcSmplRate = sampleRateOut<<1; 177 numAnalysisBands = 32; 178 } else { 179 hHeaderData->sbrProcSmplRate = sampleRateOut; 180 if ( (sampleRateOut>>1) == sampleRateIn) { 181 /* 1:2 */ 182 numAnalysisBands = 32; 183 } else if ( (sampleRateOut>>2) == sampleRateIn ) { 184 /* 1:4 */ 185 numAnalysisBands = 32; 186 } else if ( (sampleRateOut*3)>>3 == (sampleRateIn*8)>>3 ) { 187 /* 3:8, 3/4 core frame length */ 188 numAnalysisBands = 24; 189 } else { 190 sbrError = SBRDEC_UNSUPPORTED_CONFIG; 191 goto bail; 192 } 193 } 194 195 /* Fill in default values first */ 196 hHeaderData->syncState = SBR_NOT_INITIALIZED; 197 hHeaderData->status = 0; 198 hHeaderData->frameErrorFlag = 0; 199 200 hHeaderData->bs_info.ampResolution = 1; 201 hHeaderData->bs_info.xover_band = 0; 202 hHeaderData->bs_info.sbr_preprocessing = 0; 203 204 hHeaderData->bs_data.startFreq = 5; 205 hHeaderData->bs_data.stopFreq = 0; 206 hHeaderData->bs_data.freqScale = 2; 207 hHeaderData->bs_data.alterScale = 1; 208 hHeaderData->bs_data.noise_bands = 2; 209 hHeaderData->bs_data.limiterBands = 2; 210 hHeaderData->bs_data.limiterGains = 2; 211 hHeaderData->bs_data.interpolFreq = 1; 212 hHeaderData->bs_data.smoothingLength = 1; 213 214 hHeaderData->timeStep = (flags & SBRDEC_ELD_GRID) ? 1 : 2; 215 216 /* Setup pointers to frequency band tables */ 217 hFreq->freqBandTable[0] = hFreq->freqBandTableLo; 218 hFreq->freqBandTable[1] = hFreq->freqBandTableHi; 219 220 /* Patch some entries */ 221 if (sampleRateOut > 24000) { /* Trigger an error if SBR is going to be processed without */ 222 hHeaderData->bs_data.startFreq = 7; /* having read these frequency values from bit stream before. */ 223 hHeaderData->bs_data.stopFreq = 3; 224 } 225 226 /* One SBR timeslot corresponds to the amount of samples equal to the amount of analysis bands, divided by the timestep. */ 227 hHeaderData->numberTimeSlots = (samplesPerFrame/numAnalysisBands) >> (hHeaderData->timeStep - 1); 228 if (hHeaderData->numberTimeSlots > (16)) { 229 sbrError = SBRDEC_UNSUPPORTED_CONFIG; 230 } 231 232 hHeaderData->numberOfAnalysisBands = numAnalysisBands; 233 234 bail: 235 return sbrError; 236 } 237 238 239 /*! 240 \brief Initialize the SBR_PREV_FRAME_DATA struct 241 */ 242 void 243 initSbrPrevFrameData (HANDLE_SBR_PREV_FRAME_DATA h_prev_data, /*!< handle to struct SBR_PREV_FRAME_DATA */ 244 int timeSlots) /*!< Framelength in SBR-timeslots */ 245 { 246 int i; 247 248 /* Set previous energy and noise levels to 0 for the case 249 that decoding starts in the middle of a bitstream */ 250 for (i=0; i < MAX_FREQ_COEFFS; i++) 251 h_prev_data->sfb_nrg_prev[i] = (FIXP_DBL)0; 252 for (i=0; i < MAX_NOISE_COEFFS; i++) 253 h_prev_data->prevNoiseLevel[i] = (FIXP_DBL)0; 254 for (i=0; i < MAX_INVF_BANDS; i++) 255 h_prev_data->sbr_invf_mode[i] = INVF_OFF; 256 257 h_prev_data->stopPos = timeSlots; 258 h_prev_data->coupling = COUPLING_OFF; 259 h_prev_data->ampRes = 0; 260 } 261 262 263 /*! 264 \brief Read header data from bitstream 265 266 \return error status - 0 if ok 267 */ 268 SBR_HEADER_STATUS 269 sbrGetHeaderData (HANDLE_SBR_HEADER_DATA hHeaderData, 270 HANDLE_FDK_BITSTREAM hBs, 271 const UINT flags, 272 const int fIsSbrData) 273 { 274 SBR_HEADER_DATA_BS *pBsData; 275 SBR_HEADER_DATA_BS lastHeader; 276 SBR_HEADER_DATA_BS_INFO lastInfo; 277 int headerExtra1=0, headerExtra2=0; 278 279 /* Copy SBR bit stream header to temporary header */ 280 lastHeader = hHeaderData->bs_data; 281 lastInfo = hHeaderData->bs_info; 282 283 /* Read new header from bitstream */ 284 { 285 pBsData = &hHeaderData->bs_data; 286 } 287 288 { 289 hHeaderData->bs_info.ampResolution = FDKreadBits (hBs, 1); 290 } 291 292 pBsData->startFreq = FDKreadBits (hBs, 4); 293 pBsData->stopFreq = FDKreadBits (hBs, 4); 294 295 { 296 hHeaderData->bs_info.xover_band = FDKreadBits (hBs, 3); 297 FDKreadBits (hBs, 2); 298 } 299 300 headerExtra1 = FDKreadBits (hBs, 1); 301 headerExtra2 = FDKreadBits (hBs, 1); 302 303 /* Handle extra header information */ 304 if( headerExtra1) 305 { 306 pBsData->freqScale = FDKreadBits (hBs, 2); 307 pBsData->alterScale = FDKreadBits (hBs, 1); 308 pBsData->noise_bands = FDKreadBits (hBs, 2); 309 } 310 else { 311 pBsData->freqScale = 2; 312 pBsData->alterScale = 1; 313 pBsData->noise_bands = 2; 314 } 315 316 if (headerExtra2) { 317 pBsData->limiterBands = FDKreadBits (hBs, 2); 318 pBsData->limiterGains = FDKreadBits (hBs, 2); 319 pBsData->interpolFreq = FDKreadBits (hBs, 1); 320 pBsData->smoothingLength = FDKreadBits (hBs, 1); 321 } 322 else { 323 pBsData->limiterBands = 2; 324 pBsData->limiterGains = 2; 325 pBsData->interpolFreq = 1; 326 pBsData->smoothingLength = 1; 327 } 328 329 /* Look for new settings. IEC 14496-3, 4.6.18.3.1 */ 330 if(hHeaderData->syncState != SBR_ACTIVE || 331 lastHeader.startFreq != pBsData->startFreq || 332 lastHeader.stopFreq != pBsData->stopFreq || 333 lastHeader.freqScale != pBsData->freqScale || 334 lastHeader.alterScale != pBsData->alterScale || 335 lastHeader.noise_bands != pBsData->noise_bands || 336 lastInfo.xover_band != hHeaderData->bs_info.xover_band) { 337 return HEADER_RESET; /* New settings */ 338 } 339 340 return HEADER_OK; 341 } 342 343 /*! 344 \brief Get missing harmonics parameters (only used for AAC+SBR) 345 346 \return error status - 0 if ok 347 */ 348 int 349 sbrGetSyntheticCodedData(HANDLE_SBR_HEADER_DATA hHeaderData, 350 HANDLE_SBR_FRAME_DATA hFrameData, 351 HANDLE_FDK_BITSTREAM hBs) 352 { 353 int i, bitsRead = 0; 354 355 int flag = FDKreadBits(hBs,1); 356 bitsRead++; 357 358 if(flag){ 359 for(i=0;i<hHeaderData->freqBandData.nSfb[1];i++){ 360 hFrameData->addHarmonics[i] = FDKreadBits (hBs, 1 ); 361 bitsRead++; 362 } 363 } 364 else { 365 for(i=0; i<MAX_FREQ_COEFFS; i++) 366 hFrameData->addHarmonics[i] = 0; 367 } 368 return(bitsRead); 369 } 370 371 /*! 372 \brief Reads extension data from the bitstream 373 374 The bitstream format allows up to 4 kinds of extended data element. 375 Extended data may contain several elements, each identified by a 2-bit-ID. 376 So far, no extended data elements are defined hence the first 2 parameters 377 are unused. The data should be skipped in order to update the number 378 of read bits for the consistency check in applySBR(). 379 */ 380 static int extractExtendedData( 381 HANDLE_SBR_HEADER_DATA hHeaderData, /*!< handle to SBR header */ 382 HANDLE_FDK_BITSTREAM hBs /*!< Handle to the bit buffer */ 383 ,HANDLE_PS_DEC hParametricStereoDec /*!< Parametric Stereo Decoder */ 384 ) { 385 INT nBitsLeft; 386 int extended_data; 387 int i, frameOk = 1; 388 389 390 extended_data = FDKreadBits(hBs, 1); 391 392 if (extended_data) { 393 int cnt; 394 int bPsRead = 0; 395 396 cnt = FDKreadBits(hBs, 4); 397 if (cnt == (1<<4)-1) 398 cnt += FDKreadBits(hBs, 8); 399 400 401 nBitsLeft = 8 * cnt; 402 403 /* sanity check for cnt */ 404 if (nBitsLeft > (INT)FDKgetValidBits(hBs)) { 405 /* limit nBitsLeft */ 406 nBitsLeft = (INT)FDKgetValidBits(hBs); 407 /* set frame error */ 408 frameOk = 0; 409 } 410 411 while (nBitsLeft > 7) { 412 int extension_id = FDKreadBits(hBs, 2); 413 nBitsLeft -= 2; 414 415 switch(extension_id) { 416 417 418 419 case EXTENSION_ID_PS_CODING: 420 421 /* Read PS data from bitstream */ 422 423 if (hParametricStereoDec != NULL) { 424 if(bPsRead && !hParametricStereoDec->bsData[hParametricStereoDec->bsReadSlot].mpeg.bPsHeaderValid) { 425 cnt = nBitsLeft >> 3; /* number of remaining bytes */ 426 for (i=0; i<cnt; i++) 427 FDKreadBits(hBs, 8); 428 nBitsLeft -= cnt * 8; 429 } else { 430 nBitsLeft -= ReadPsData(hParametricStereoDec, hBs, nBitsLeft); 431 bPsRead = 1; 432 } 433 } 434 435 /* parametric stereo detected, could set channelMode accordingly here */ 436 /* */ 437 /* "The usage of this parametric stereo extension to HE-AAC is */ 438 /* signalled implicitly in the bitstream. Hence, if an sbr_extension() */ 439 /* with bs_extension_id==EXTENSION_ID_PS is found in the SBR part of */ 440 /* the bitstream, a decoder supporting the combination of SBR and PS */ 441 /* shall operate the PS tool to generate a stereo output signal." */ 442 /* source: ISO/IEC 14496-3:2001/FDAM 2:2004(E) */ 443 444 break; 445 446 447 default: 448 cnt = nBitsLeft >> 3; /* number of remaining bytes */ 449 for (i=0; i<cnt; i++) 450 FDKreadBits(hBs, 8); 451 nBitsLeft -= cnt * 8; 452 break; 453 } 454 } 455 456 if (nBitsLeft < 0) { 457 frameOk = 0; 458 goto bail; 459 } 460 else { 461 /* Read fill bits for byte alignment */ 462 FDKreadBits(hBs, nBitsLeft); 463 } 464 } 465 466 bail: 467 return (frameOk); 468 } 469 470 471 /*! 472 \brief Read bitstream elements of one channel 473 474 \return SbrFrameOK: 1=ok, 0=error 475 */ 476 int 477 sbrGetSingleChannelElement (HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */ 478 HANDLE_SBR_FRAME_DATA hFrameData, /*!< Control data of current frame */ 479 HANDLE_FDK_BITSTREAM hBs, /*!< Handle to struct BIT_BUF */ 480 HANDLE_PS_DEC hParametricStereoDec, /*!< Handle to PS decoder */ 481 const UINT flags, 482 const int overlap 483 ) 484 { 485 int i; 486 487 488 hFrameData->coupling = COUPLING_OFF; 489 490 { 491 /* Reserved bits */ 492 if (FDKreadBits(hBs, 1)) { /* bs_data_extra */ 493 FDKreadBits(hBs, 4); 494 if (flags & SBRDEC_SYNTAX_SCAL) { 495 FDKreadBits(hBs, 4); 496 } 497 } 498 } 499 500 if (flags & SBRDEC_SYNTAX_SCAL) { 501 FDKreadBits (hBs, 1); /* bs_coupling */ 502 } 503 504 /* 505 Grid control 506 */ 507 if ( !extractFrameInfo ( hBs, hHeaderData, hFrameData, 1, flags) ) 508 return 0; 509 510 if ( !checkFrameInfo (&hFrameData->frameInfo, hHeaderData->numberTimeSlots, overlap, hHeaderData->timeStep) ) 511 return 0; 512 513 514 /* 515 Fetch domain vectors (time or frequency direction for delta-coding) 516 */ 517 sbrGetDirectionControlData (hFrameData, hBs); 518 519 for (i=0; i<hHeaderData->freqBandData.nInvfBands; i++) { 520 hFrameData->sbr_invf_mode[i] = 521 (INVF_MODE) FDKreadBits (hBs, 2); 522 } 523 524 525 526 /* raw data */ 527 if ( !sbrGetEnvelope (hHeaderData, hFrameData, hBs, flags) ) 528 return 0; 529 530 531 sbrGetNoiseFloorData (hHeaderData, hFrameData, hBs); 532 533 sbrGetSyntheticCodedData(hHeaderData, hFrameData, hBs); 534 535 { 536 /* sbr extended data */ 537 if (! extractExtendedData( 538 hHeaderData, 539 hBs 540 ,hParametricStereoDec 541 )) { 542 return 0; 543 } 544 } 545 546 return 1; 547 } 548 549 550 551 /*! 552 \brief Read bitstream elements of a channel pair 553 \return SbrFrameOK 554 */ 555 int 556 sbrGetChannelPairElement (HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */ 557 HANDLE_SBR_FRAME_DATA hFrameDataLeft, /*!< Dynamic control data for first channel */ 558 HANDLE_SBR_FRAME_DATA hFrameDataRight,/*!< Dynamic control data for second channel */ 559 HANDLE_FDK_BITSTREAM hBs, /*!< handle to struct BIT_BUF */ 560 const UINT flags, 561 const int overlap ) 562 { 563 int i, bit; 564 565 566 /* Reserved bits */ 567 if (FDKreadBits(hBs, 1)) { /* bs_data_extra */ 568 FDKreadBits(hBs, 4); 569 FDKreadBits(hBs, 4); 570 } 571 572 /* Read coupling flag */ 573 bit = FDKreadBits (hBs, 1); 574 575 if (bit) { 576 hFrameDataLeft->coupling = COUPLING_LEVEL; 577 hFrameDataRight->coupling = COUPLING_BAL; 578 } 579 else { 580 hFrameDataLeft->coupling = COUPLING_OFF; 581 hFrameDataRight->coupling = COUPLING_OFF; 582 } 583 584 585 /* 586 Grid control 587 */ 588 if ( !extractFrameInfo (hBs, hHeaderData, hFrameDataLeft, 2, flags) ) 589 return 0; 590 591 if ( !checkFrameInfo (&hFrameDataLeft->frameInfo, hHeaderData->numberTimeSlots, overlap, hHeaderData->timeStep) ) 592 return 0; 593 594 if (hFrameDataLeft->coupling) { 595 FDKmemcpy (&hFrameDataRight->frameInfo, &hFrameDataLeft->frameInfo, sizeof(FRAME_INFO)); 596 hFrameDataRight->ampResolutionCurrentFrame = hFrameDataLeft->ampResolutionCurrentFrame; 597 } 598 else { 599 if ( !extractFrameInfo (hBs, hHeaderData, hFrameDataRight, 2, flags) ) 600 return 0; 601 602 if ( !checkFrameInfo (&hFrameDataRight->frameInfo, hHeaderData->numberTimeSlots, overlap, hHeaderData->timeStep) ) 603 return 0; 604 } 605 606 /* 607 Fetch domain vectors (time or frequency direction for delta-coding) 608 */ 609 sbrGetDirectionControlData (hFrameDataLeft, hBs); 610 sbrGetDirectionControlData (hFrameDataRight, hBs); 611 612 for (i=0; i<hHeaderData->freqBandData.nInvfBands; i++) { 613 hFrameDataLeft->sbr_invf_mode[i] = (INVF_MODE) FDKreadBits (hBs, 2); 614 } 615 616 if (hFrameDataLeft->coupling) { 617 for (i=0; i<hHeaderData->freqBandData.nInvfBands; i++) { 618 hFrameDataRight->sbr_invf_mode[i] = hFrameDataLeft->sbr_invf_mode[i]; 619 } 620 621 622 if ( !sbrGetEnvelope (hHeaderData, hFrameDataLeft, hBs, flags) ) { 623 return 0; 624 } 625 626 sbrGetNoiseFloorData (hHeaderData, hFrameDataLeft, hBs); 627 628 if ( !sbrGetEnvelope (hHeaderData, hFrameDataRight, hBs, flags) ) { 629 return 0; 630 } 631 } 632 else { 633 634 for (i=0; i<hHeaderData->freqBandData.nInvfBands; i++) { 635 hFrameDataRight->sbr_invf_mode[i] = (INVF_MODE) FDKreadBits (hBs, 2); 636 } 637 638 639 640 if ( !sbrGetEnvelope (hHeaderData, hFrameDataLeft, hBs, flags) ) 641 return 0; 642 643 if ( !sbrGetEnvelope (hHeaderData, hFrameDataRight, hBs, flags) ) 644 return 0; 645 646 sbrGetNoiseFloorData (hHeaderData, hFrameDataLeft, hBs); 647 648 } 649 sbrGetNoiseFloorData (hHeaderData, hFrameDataRight, hBs); 650 651 sbrGetSyntheticCodedData(hHeaderData, hFrameDataLeft, hBs); 652 sbrGetSyntheticCodedData(hHeaderData, hFrameDataRight, hBs); 653 654 { 655 if (! extractExtendedData( 656 hHeaderData, 657 hBs 658 ,NULL 659 ) ) { 660 return 0; 661 } 662 } 663 664 return 1; 665 } 666 667 668 669 670 /*! 671 \brief Read direction control data from bitstream 672 */ 673 void 674 sbrGetDirectionControlData (HANDLE_SBR_FRAME_DATA h_frame_data, /*!< handle to struct SBR_FRAME_DATA */ 675 HANDLE_FDK_BITSTREAM hBs) /*!< handle to struct BIT_BUF */ 676 { 677 int i; 678 679 for (i = 0; i < h_frame_data->frameInfo.nEnvelopes; i++) { 680 h_frame_data->domain_vec[i] = FDKreadBits (hBs, 1); 681 } 682 683 for (i = 0; i < h_frame_data->frameInfo.nNoiseEnvelopes; i++) { 684 h_frame_data->domain_vec_noise[i] = FDKreadBits (hBs, 1); 685 } 686 } 687 688 689 690 /*! 691 \brief Read noise-floor-level data from bitstream 692 */ 693 void 694 sbrGetNoiseFloorData (HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */ 695 HANDLE_SBR_FRAME_DATA h_frame_data, /*!< handle to struct SBR_FRAME_DATA */ 696 HANDLE_FDK_BITSTREAM hBs) /*!< handle to struct BIT_BUF */ 697 { 698 int i,j; 699 int delta; 700 COUPLING_MODE coupling; 701 int noNoiseBands = hHeaderData->freqBandData.nNfb; 702 703 Huffman hcb_noiseF; 704 Huffman hcb_noise; 705 int envDataTableCompFactor; 706 707 coupling = h_frame_data->coupling; 708 709 710 /* 711 Select huffman codebook depending on coupling mode 712 */ 713 if (coupling == COUPLING_BAL) { 714 hcb_noise = (Huffman)&FDK_sbrDecoder_sbr_huffBook_NoiseBalance11T; 715 hcb_noiseF = (Huffman)&FDK_sbrDecoder_sbr_huffBook_EnvBalance11F; /* "sbr_huffBook_NoiseBalance11F" */ 716 envDataTableCompFactor = 1; 717 } 718 else { 719 hcb_noise = (Huffman)&FDK_sbrDecoder_sbr_huffBook_NoiseLevel11T; 720 hcb_noiseF = (Huffman)&FDK_sbrDecoder_sbr_huffBook_EnvLevel11F; /* "sbr_huffBook_NoiseLevel11F" */ 721 envDataTableCompFactor = 0; 722 } 723 724 /* 725 Read raw noise-envelope data 726 */ 727 for (i=0; i<h_frame_data->frameInfo.nNoiseEnvelopes; i++) { 728 729 730 if (h_frame_data->domain_vec_noise[i] == 0) { 731 if (coupling == COUPLING_BAL) { 732 h_frame_data->sbrNoiseFloorLevel[i*noNoiseBands] = 733 (FIXP_SGL) (((int)FDKreadBits (hBs, 5)) << envDataTableCompFactor); 734 } 735 else { 736 h_frame_data->sbrNoiseFloorLevel[i*noNoiseBands] = 737 (FIXP_SGL) (int)FDKreadBits (hBs, 5); 738 } 739 740 for (j = 1; j < noNoiseBands; j++) { 741 delta = DecodeHuffmanCW(hcb_noiseF, hBs); 742 h_frame_data->sbrNoiseFloorLevel[i*noNoiseBands+j] = (FIXP_SGL) (delta << envDataTableCompFactor); 743 } 744 } 745 else { 746 for (j = 0; j < noNoiseBands; j++) { 747 delta = DecodeHuffmanCW(hcb_noise, hBs); 748 h_frame_data->sbrNoiseFloorLevel[i*noNoiseBands+j] = (FIXP_SGL) (delta << envDataTableCompFactor); 749 } 750 } 751 } 752 } 753 754 755 /*! 756 \brief Read envelope data from bitstream 757 */ 758 static int 759 sbrGetEnvelope (HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */ 760 HANDLE_SBR_FRAME_DATA h_frame_data, /*!< handle to struct SBR_FRAME_DATA */ 761 HANDLE_FDK_BITSTREAM hBs, /*!< handle to struct BIT_BUF */ 762 const UINT flags) 763 { 764 int i, j; 765 UCHAR no_band[MAX_ENVELOPES]; 766 int delta = 0; 767 int offset = 0; 768 COUPLING_MODE coupling = h_frame_data->coupling; 769 int ampRes = hHeaderData->bs_info.ampResolution; 770 int nEnvelopes = h_frame_data->frameInfo.nEnvelopes; 771 int envDataTableCompFactor; 772 int start_bits, start_bits_balance; 773 Huffman hcb_t, hcb_f; 774 775 h_frame_data->nScaleFactors = 0; 776 777 if ( (h_frame_data->frameInfo.frameClass == 0) && (nEnvelopes == 1) ) { 778 if (flags & SBRDEC_ELD_GRID) 779 ampRes = h_frame_data->ampResolutionCurrentFrame; 780 else 781 ampRes = 0; 782 } 783 h_frame_data->ampResolutionCurrentFrame = ampRes; 784 785 /* 786 Set number of bits for first value depending on amplitude resolution 787 */ 788 if(ampRes == 1) 789 { 790 start_bits = 6; 791 start_bits_balance = 5; 792 } 793 else 794 { 795 start_bits = 7; 796 start_bits_balance = 6; 797 } 798 799 /* 800 Calculate number of values for each envelope and alltogether 801 */ 802 for (i = 0; i < nEnvelopes; i++) { 803 no_band[i] = hHeaderData->freqBandData.nSfb[h_frame_data->frameInfo.freqRes[i]]; 804 h_frame_data->nScaleFactors += no_band[i]; 805 } 806 if (h_frame_data->nScaleFactors > MAX_NUM_ENVELOPE_VALUES) 807 return 0; 808 809 /* 810 Select Huffman codebook depending on coupling mode and amplitude resolution 811 */ 812 if (coupling == COUPLING_BAL) { 813 envDataTableCompFactor = 1; 814 if (ampRes == 0) { 815 hcb_t = (Huffman)&FDK_sbrDecoder_sbr_huffBook_EnvBalance10T; 816 hcb_f = (Huffman)&FDK_sbrDecoder_sbr_huffBook_EnvBalance10F; 817 } 818 else { 819 hcb_t = (Huffman)&FDK_sbrDecoder_sbr_huffBook_EnvBalance11T; 820 hcb_f = (Huffman)&FDK_sbrDecoder_sbr_huffBook_EnvBalance11F; 821 } 822 } 823 else { 824 envDataTableCompFactor = 0; 825 if (ampRes == 0) { 826 hcb_t = (Huffman)&FDK_sbrDecoder_sbr_huffBook_EnvLevel10T; 827 hcb_f = (Huffman)&FDK_sbrDecoder_sbr_huffBook_EnvLevel10F; 828 } 829 else { 830 hcb_t = (Huffman)&FDK_sbrDecoder_sbr_huffBook_EnvLevel11T; 831 hcb_f = (Huffman)&FDK_sbrDecoder_sbr_huffBook_EnvLevel11F; 832 } 833 } 834 835 /* 836 Now read raw envelope data 837 */ 838 for (j = 0, offset = 0; j < nEnvelopes; j++) { 839 840 841 if (h_frame_data->domain_vec[j] == 0) { 842 if (coupling == COUPLING_BAL) { 843 h_frame_data->iEnvelope[offset] = 844 (FIXP_SGL) (( (int)FDKreadBits(hBs, start_bits_balance)) << envDataTableCompFactor); 845 } 846 else { 847 h_frame_data->iEnvelope[offset] = 848 (FIXP_SGL) (int)FDKreadBits (hBs, start_bits); 849 } 850 } 851 852 for (i = (1 - h_frame_data->domain_vec[j]); i < no_band[j]; i++) { 853 854 if (h_frame_data->domain_vec[j] == 0) { 855 delta = DecodeHuffmanCW(hcb_f, hBs); 856 } 857 else { 858 delta = DecodeHuffmanCW(hcb_t, hBs); 859 } 860 861 h_frame_data->iEnvelope[offset + i] = (FIXP_SGL) (delta << envDataTableCompFactor); 862 } 863 offset += no_band[j]; 864 } 865 866 #if ENV_EXP_FRACT 867 /* Convert from int to scaled fract (ENV_EXP_FRACT bits for the fractional part) */ 868 for (i = 0; i < h_frame_data->nScaleFactors; i++) { 869 h_frame_data->iEnvelope[i] <<= ENV_EXP_FRACT; 870 } 871 #endif 872 873 return 1; 874 } 875 876 877 //static const FRAME_INFO v_frame_info1_8 = { 0, 1, {0, 8}, {1}, -1, 1, {0, 8} }; 878 static const FRAME_INFO v_frame_info2_8 = { 0, 2, {0, 4, 8}, {1, 1}, -1, 2, {0, 4, 8} }; 879 static const FRAME_INFO v_frame_info4_8 = { 0, 4, {0, 2, 4, 6, 8}, {1, 1, 1, 1}, -1, 2, {0, 4, 8} }; 880 881 /***************************************************************************/ 882 /*! 883 \brief Generates frame info for FIXFIXonly frame class used for low delay version 884 885 \return nothing 886 ****************************************************************************/ 887 static void generateFixFixOnly ( FRAME_INFO *hSbrFrameInfo, 888 int tranPosInternal, 889 int numberTimeSlots 890 ) 891 { 892 int nEnv, i, tranIdx; 893 const int *pTable; 894 895 switch (numberTimeSlots) { 896 case 8: 897 pTable = FDK_sbrDecoder_envelopeTable_8[tranPosInternal]; 898 break; 899 case 15: 900 pTable = FDK_sbrDecoder_envelopeTable_15[tranPosInternal]; 901 break; 902 case 16: 903 pTable = FDK_sbrDecoder_envelopeTable_16[tranPosInternal]; 904 break; 905 default: 906 FDK_ASSERT(0); 907 } 908 909 /* look number of envelopes in table */ 910 nEnv = pTable[0]; 911 /* look up envelope distribution in table */ 912 for (i=1; i<nEnv; i++) 913 hSbrFrameInfo->borders[i] = pTable[i+2]; 914 /* open and close frame border */ 915 hSbrFrameInfo->borders[0] = 0; 916 hSbrFrameInfo->borders[nEnv] = numberTimeSlots; 917 hSbrFrameInfo->nEnvelopes = nEnv; 918 919 /* transient idx */ 920 tranIdx = hSbrFrameInfo->tranEnv = pTable[1]; 921 922 /* add noise floors */ 923 hSbrFrameInfo->bordersNoise[0] = 0; 924 hSbrFrameInfo->bordersNoise[1] = hSbrFrameInfo->borders[tranIdx?tranIdx:1]; 925 hSbrFrameInfo->bordersNoise[2] = numberTimeSlots; 926 /* nEnv is always > 1, so nNoiseEnvelopes is always 2 (IEC 14496-3 4.6.19.3.2) */ 927 hSbrFrameInfo->nNoiseEnvelopes = 2; 928 } 929 930 /*! 931 \brief Extracts LowDelaySBR control data from the bitstream. 932 933 \return zero for bitstream error, one for correct. 934 */ 935 static int 936 extractLowDelayGrid (HANDLE_FDK_BITSTREAM hBitBuf, /*!< bitbuffer handle */ 937 HANDLE_SBR_HEADER_DATA hHeaderData, 938 HANDLE_SBR_FRAME_DATA h_frame_data, /*!< contains the FRAME_INFO struct to be filled */ 939 int timeSlots 940 ) 941 { 942 FRAME_INFO * pFrameInfo = &h_frame_data->frameInfo; 943 INT numberTimeSlots = hHeaderData->numberTimeSlots; 944 INT temp = 0, k; 945 946 /* FIXFIXonly framing case */ 947 h_frame_data->frameInfo.frameClass = 0; 948 949 /* get the transient position from the bitstream */ 950 switch (timeSlots){ 951 case 8: 952 /* 3bit transient position (temp={0;..;7}) */ 953 temp = FDKreadBits( hBitBuf, 3); 954 break; 955 956 case 16: 957 case 15: 958 /* 4bit transient position (temp={0;..;15}) */ 959 temp = FDKreadBits( hBitBuf, 4); 960 break; 961 962 default: 963 return 0; 964 } 965 966 /* calculate borders according to the transient position */ 967 generateFixFixOnly ( pFrameInfo, 968 temp, 969 numberTimeSlots 970 ); 971 972 /* decode freq res: */ 973 for (k = 0; k < pFrameInfo->nEnvelopes; k++) { 974 pFrameInfo->freqRes[k] = (UCHAR) FDKreadBits (hBitBuf, 1); /* f = F [1 bits] */ 975 } 976 977 978 return 1; 979 } 980 981 /*! 982 \brief Extract the frame information (structure FRAME_INFO) from the bitstream 983 \return Zero for bitstream error, one for correct. 984 */ 985 int 986 extractFrameInfo ( HANDLE_FDK_BITSTREAM hBs, /*!< bitbuffer handle */ 987 HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */ 988 HANDLE_SBR_FRAME_DATA h_frame_data, /*!< pointer to memory where the frame-info will be stored */ 989 const UINT nrOfChannels, 990 const UINT flags 991 ) 992 { 993 FRAME_INFO * pFrameInfo = &h_frame_data->frameInfo; 994 int numberTimeSlots = hHeaderData->numberTimeSlots; 995 int pointer_bits = 0, nEnv = 0, b = 0, border, i, n = 0, 996 k, p, aL, aR, nL, nR, 997 temp = 0, staticFreqRes; 998 UCHAR frameClass; 999 1000 if (flags & SBRDEC_ELD_GRID) { 1001 /* CODEC_AACLD (LD+SBR) only uses the normal 0 Grid for non-transient Frames and the LowDelayGrid for transient Frames */ 1002 frameClass = FDKreadBits (hBs, 1); /* frameClass = [1 bit] */ 1003 if ( frameClass == 1 ) { 1004 /* if frameClass == 1, extract LowDelaySbrGrid, otherwise extract normal SBR-Grid for FIXIFX */ 1005 /* extract the AACLD-Sbr-Grid */ 1006 pFrameInfo->frameClass = frameClass; 1007 extractLowDelayGrid (hBs, hHeaderData, h_frame_data, numberTimeSlots); 1008 return 1; 1009 } 1010 } else 1011 { 1012 frameClass = FDKreadBits (hBs, 2); /* frameClass = C [2 bits] */ 1013 } 1014 1015 1016 switch (frameClass) { 1017 case 0: 1018 temp = FDKreadBits (hBs, 2); /* E [2 bits ] */ 1019 nEnv = (int) (1 << temp); /* E -> e */ 1020 1021 if ((flags & SBRDEC_ELD_GRID) && (nEnv == 1)) 1022 h_frame_data->ampResolutionCurrentFrame = FDKreadBits( hBs, 1); /* new ELD Syntax 07-11-09 */ 1023 1024 staticFreqRes = FDKreadBits (hBs, 1); 1025 1026 { 1027 if (nEnv > MAX_ENVELOPES_HEAAC) 1028 return 0; 1029 } 1030 1031 b = nEnv + 1; 1032 switch (nEnv) { 1033 case 1: 1034 switch (numberTimeSlots) { 1035 case 15: 1036 FDKmemcpy (pFrameInfo, &FDK_sbrDecoder_sbr_frame_info1_15, sizeof(FRAME_INFO)); 1037 break; 1038 case 16: 1039 FDKmemcpy (pFrameInfo, &FDK_sbrDecoder_sbr_frame_info1_16, sizeof(FRAME_INFO)); 1040 break; 1041 default: 1042 FDK_ASSERT(0); 1043 } 1044 break; 1045 case 2: 1046 switch (numberTimeSlots) { 1047 case 15: 1048 FDKmemcpy (pFrameInfo, &FDK_sbrDecoder_sbr_frame_info2_15, sizeof(FRAME_INFO)); 1049 break; 1050 case 16: 1051 FDKmemcpy (pFrameInfo, &FDK_sbrDecoder_sbr_frame_info2_16, sizeof(FRAME_INFO)); 1052 break; 1053 default: 1054 FDK_ASSERT(0); 1055 } 1056 break; 1057 case 4: 1058 switch (numberTimeSlots) { 1059 case 15: 1060 FDKmemcpy (pFrameInfo, &FDK_sbrDecoder_sbr_frame_info4_15, sizeof(FRAME_INFO)); 1061 break; 1062 case 16: 1063 FDKmemcpy (pFrameInfo, &FDK_sbrDecoder_sbr_frame_info4_16, sizeof(FRAME_INFO)); 1064 break; 1065 default: 1066 FDK_ASSERT(0); 1067 } 1068 break; 1069 case 8: 1070 #if (MAX_ENVELOPES >= 8) 1071 switch (numberTimeSlots) { 1072 case 15: 1073 FDKmemcpy (pFrameInfo, &FDK_sbrDecoder_sbr_frame_info8_15, sizeof(FRAME_INFO)); 1074 break; 1075 case 16: 1076 FDKmemcpy (pFrameInfo, &FDK_sbrDecoder_sbr_frame_info8_16, sizeof(FRAME_INFO)); 1077 break; 1078 default: 1079 FDK_ASSERT(0); 1080 } 1081 break; 1082 #else 1083 return 0; 1084 #endif 1085 } 1086 /* Apply correct freqRes (High is default) */ 1087 if (!staticFreqRes) { 1088 for (i = 0; i < nEnv ; i++) 1089 pFrameInfo->freqRes[i] = 0; 1090 } 1091 1092 break; 1093 case 1: 1094 case 2: 1095 temp = FDKreadBits (hBs, 2); /* A [2 bits] */ 1096 1097 n = FDKreadBits (hBs, 2); /* n = N [2 bits] */ 1098 1099 nEnv = n + 1; /* # envelopes */ 1100 b = nEnv + 1; /* # borders */ 1101 1102 break; 1103 } 1104 1105 switch (frameClass) { 1106 case 1: 1107 /* Decode borders: */ 1108 pFrameInfo->borders[0] = 0; /* first border */ 1109 border = temp + numberTimeSlots; /* A -> aR */ 1110 i = b-1; /* frame info index for last border */ 1111 pFrameInfo->borders[i] = border; /* last border */ 1112 1113 for (k = 0; k < n; k++) { 1114 temp = FDKreadBits (hBs, 2);/* R [2 bits] */ 1115 border -= (2 * temp + 2); /* R -> r */ 1116 pFrameInfo->borders[--i] = border; 1117 } 1118 1119 1120 /* Decode pointer: */ 1121 pointer_bits = DFRACT_BITS - 1 - CountLeadingBits((FIXP_DBL)(n+1)); 1122 p = FDKreadBits (hBs, pointer_bits); /* p = P [pointer_bits bits] */ 1123 1124 if (p > n+1) 1125 return 0; 1126 1127 pFrameInfo->tranEnv = p ? n + 2 - p : -1; 1128 1129 1130 /* Decode freq res: */ 1131 for (k = n; k >= 0; k--) { 1132 pFrameInfo->freqRes[k] = FDKreadBits (hBs, 1); /* f = F [1 bits] */ 1133 } 1134 1135 1136 /* Calculate noise floor middle border: */ 1137 if (p == 0 || p == 1) 1138 pFrameInfo->bordersNoise[1] = pFrameInfo->borders[n]; 1139 else 1140 pFrameInfo->bordersNoise[1] = pFrameInfo->borders[pFrameInfo->tranEnv]; 1141 1142 break; 1143 1144 case 2: 1145 /* Decode borders: */ 1146 border = temp; /* A -> aL */ 1147 pFrameInfo->borders[0] = border; /* first border */ 1148 1149 for (k = 1; k <= n; k++) { 1150 temp = FDKreadBits (hBs, 2);/* R [2 bits] */ 1151 border += (2 * temp + 2); /* R -> r */ 1152 pFrameInfo->borders[k] = border; 1153 } 1154 pFrameInfo->borders[k] = numberTimeSlots; /* last border */ 1155 1156 1157 /* Decode pointer: */ 1158 pointer_bits = DFRACT_BITS - 1 - CountLeadingBits((FIXP_DBL)(n+1)); 1159 p = FDKreadBits (hBs, pointer_bits); /* p = P [pointer_bits bits] */ 1160 if (p > n+1) 1161 return 0; 1162 1163 if (p == 0 || p == 1) 1164 pFrameInfo->tranEnv = -1; 1165 else 1166 pFrameInfo->tranEnv = p - 1; 1167 1168 1169 1170 /* Decode freq res: */ 1171 for (k = 0; k <= n; k++) { 1172 pFrameInfo->freqRes[k] = FDKreadBits(hBs, 1); /* f = F [1 bits] */ 1173 } 1174 1175 1176 1177 /* Calculate noise floor middle border: */ 1178 switch (p) { 1179 case 0: 1180 pFrameInfo->bordersNoise[1] = pFrameInfo->borders[1]; 1181 break; 1182 case 1: 1183 pFrameInfo->bordersNoise[1] = pFrameInfo->borders[n]; 1184 break; 1185 default: 1186 pFrameInfo->bordersNoise[1] = pFrameInfo->borders[pFrameInfo->tranEnv]; 1187 break; 1188 } 1189 1190 break; 1191 1192 case 3: 1193 /* v_ctrlSignal = [frameClass,aL,aR,nL,nR,v_rL,v_rR,p,v_fLR]; */ 1194 1195 aL = FDKreadBits (hBs, 2); /* AL [2 bits], AL -> aL */ 1196 1197 aR = FDKreadBits (hBs, 2) + numberTimeSlots; /* AR [2 bits], AR -> aR */ 1198 1199 nL = FDKreadBits (hBs, 2); /* nL = NL [2 bits] */ 1200 1201 nR = FDKreadBits (hBs, 2); /* nR = NR [2 bits] */ 1202 1203 1204 1205 /*------------------------------------------------------------------------- 1206 Calculate help variables 1207 --------------------------------------------------------------------------*/ 1208 1209 /* general: */ 1210 nEnv = nL + nR + 1; /* # envelopes */ 1211 if (nEnv > MAX_ENVELOPES) 1212 return 0; 1213 b = nEnv + 1; /* # borders */ 1214 1215 1216 1217 /*------------------------------------------------------------------------- 1218 Decode envelopes 1219 --------------------------------------------------------------------------*/ 1220 1221 1222 /* L-borders: */ 1223 border = aL; /* first border */ 1224 pFrameInfo->borders[0] = border; 1225 1226 for (k = 1; k <= nL; k++) { 1227 temp = FDKreadBits (hBs, 2);/* R [2 bits] */ 1228 border += (2 * temp + 2); /* R -> r */ 1229 pFrameInfo->borders[k] = border; 1230 } 1231 1232 1233 /* R-borders: */ 1234 border = aR; /* last border */ 1235 i = nEnv; 1236 1237 pFrameInfo->borders[i] = border; 1238 1239 for (k = 0; k < nR; k++) { 1240 temp = FDKreadBits (hBs, 2);/* R [2 bits] */ 1241 border -= (2 * temp + 2); /* R -> r */ 1242 pFrameInfo->borders[--i] = border; 1243 } 1244 1245 1246 /* decode pointer: */ 1247 pointer_bits = DFRACT_BITS - 1 - CountLeadingBits((FIXP_DBL)(nL+nR+1)); 1248 p = FDKreadBits (hBs, pointer_bits); /* p = P [pointer_bits bits] */ 1249 1250 if (p > nL+nR+1) 1251 return 0; 1252 1253 pFrameInfo->tranEnv = p ? b - p : -1; 1254 1255 1256 1257 /* decode freq res: */ 1258 for (k = 0; k < nEnv; k++) { 1259 pFrameInfo->freqRes[k] = FDKreadBits(hBs, 1); /* f = F [1 bits] */ 1260 } 1261 1262 1263 1264 /*------------------------------------------------------------------------- 1265 Decode noise floors 1266 --------------------------------------------------------------------------*/ 1267 pFrameInfo->bordersNoise[0] = aL; 1268 1269 if (nEnv == 1) { 1270 /* 1 noise floor envelope: */ 1271 pFrameInfo->bordersNoise[1] = aR; 1272 } 1273 else { 1274 /* 2 noise floor envelopes */ 1275 if (p == 0 || p == 1) 1276 pFrameInfo->bordersNoise[1] = pFrameInfo->borders[nEnv - 1]; 1277 else 1278 pFrameInfo->bordersNoise[1] = pFrameInfo->borders[pFrameInfo->tranEnv]; 1279 pFrameInfo->bordersNoise[2] = aR; 1280 } 1281 break; 1282 } 1283 1284 1285 /* 1286 Store number of envelopes, noise floor envelopes and frame class 1287 */ 1288 pFrameInfo->nEnvelopes = nEnv; 1289 1290 if (nEnv == 1) 1291 pFrameInfo->nNoiseEnvelopes = 1; 1292 else 1293 pFrameInfo->nNoiseEnvelopes = 2; 1294 1295 pFrameInfo->frameClass = frameClass; 1296 1297 if (pFrameInfo->frameClass == 2 || pFrameInfo->frameClass == 1) { 1298 /* calculate noise floor first and last borders: */ 1299 pFrameInfo->bordersNoise[0] = pFrameInfo->borders[0]; 1300 pFrameInfo->bordersNoise[pFrameInfo->nNoiseEnvelopes] = pFrameInfo->borders[nEnv]; 1301 } 1302 1303 1304 return 1; 1305 } 1306 1307 1308 /*! 1309 \brief Check if the frameInfo vector has reasonable values. 1310 \return Zero for error, one for correct 1311 */ 1312 static int 1313 checkFrameInfo (FRAME_INFO * pFrameInfo, /*!< pointer to frameInfo */ 1314 int numberOfTimeSlots, /*!< QMF time slots per frame */ 1315 int overlap, /*!< Amount of overlap QMF time slots */ 1316 int timeStep) /*!< QMF slots to SBR slots step factor */ 1317 { 1318 int maxPos,i,j; 1319 int startPos; 1320 int stopPos; 1321 int tranEnv; 1322 int startPosNoise; 1323 int stopPosNoise; 1324 int nEnvelopes = pFrameInfo->nEnvelopes; 1325 int nNoiseEnvelopes = pFrameInfo->nNoiseEnvelopes; 1326 1327 if(nEnvelopes < 1 || nEnvelopes > MAX_ENVELOPES) 1328 return 0; 1329 1330 if(nNoiseEnvelopes > MAX_NOISE_ENVELOPES) 1331 return 0; 1332 1333 startPos = pFrameInfo->borders[0]; 1334 stopPos = pFrameInfo->borders[nEnvelopes]; 1335 tranEnv = pFrameInfo->tranEnv; 1336 startPosNoise = pFrameInfo->bordersNoise[0]; 1337 stopPosNoise = pFrameInfo->bordersNoise[nNoiseEnvelopes]; 1338 1339 if (overlap < 0 || overlap > (6)) { 1340 return 0; 1341 } 1342 if (timeStep < 1 || timeStep > 2) { 1343 return 0; 1344 } 1345 maxPos = numberOfTimeSlots + (overlap/timeStep); 1346 1347 /* Check that the start and stop positions of the frame are reasonable values. */ 1348 if( (startPos < 0) || (startPos >= stopPos) ) 1349 return 0; 1350 if( startPos > maxPos-numberOfTimeSlots ) /* First env. must start in or directly after the overlap buffer */ 1351 return 0; 1352 if( stopPos < numberOfTimeSlots ) /* One complete frame must be ready for output after processing */ 1353 return 0; 1354 if(stopPos > maxPos) 1355 return 0; 1356 1357 /* Check that the start border for every envelope is strictly later in time */ 1358 for(i=0;i<nEnvelopes;i++) { 1359 if(pFrameInfo->borders[i] >= pFrameInfo->borders[i+1]) 1360 return 0; 1361 } 1362 1363 /* Check that the envelope to be shortened is actually among the envelopes */ 1364 if(tranEnv>nEnvelopes) 1365 return 0; 1366 1367 1368 /* Check the noise borders */ 1369 if(nEnvelopes==1 && nNoiseEnvelopes>1) 1370 return 0; 1371 1372 if(startPos != startPosNoise || stopPos != stopPosNoise) 1373 return 0; 1374 1375 1376 /* Check that the start border for every noise-envelope is strictly later in time*/ 1377 for(i=0; i<nNoiseEnvelopes; i++) { 1378 if(pFrameInfo->bordersNoise[i] >= pFrameInfo->bordersNoise[i+1]) 1379 return 0; 1380 } 1381 1382 /* Check that every noise border is the same as an envelope border*/ 1383 for(i=0; i<nNoiseEnvelopes; i++) { 1384 startPosNoise = pFrameInfo->bordersNoise[i]; 1385 1386 for(j=0; j<nEnvelopes; j++) { 1387 if(pFrameInfo->borders[j] == startPosNoise) 1388 break; 1389 } 1390 if(j==nEnvelopes) 1391 return 0; 1392 } 1393 1394 return 1; 1395 } 1396