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 #include "env_est.h" 85 #include "tran_det.h" 86 87 #include "qmf.h" 88 89 #include "fram_gen.h" 90 #include "bit_sbr.h" 91 #include "cmondata.h" 92 #include "sbr_ram.h" 93 94 95 #include "genericStds.h" 96 97 #define QUANT_ERROR_THRES 200 98 #define Y_NRG_SCALE 5 /* noCols = 32 -> shift(5) */ 99 100 101 static const UCHAR panTable[2][10] = { { 0, 2, 4, 6, 8,12,16,20,24}, 102 { 0, 2, 4, 8,12, 0, 0, 0, 0 } }; 103 static const UCHAR maxIndex[2] = {9, 5}; 104 105 106 /***************************************************************************/ 107 /*! 108 109 \brief Calculates energy form real and imaginary part of 110 the QMF subsamples 111 112 \return none 113 114 ****************************************************************************/ 115 LNK_SECTION_CODE_L1 116 static void 117 FDKsbrEnc_getEnergyFromCplxQmfData(FIXP_DBL **RESTRICT energyValues,/*!< the result of the operation */ 118 FIXP_DBL **RESTRICT realValues, /*!< the real part of the QMF subsamples */ 119 FIXP_DBL **RESTRICT imagValues, /*!< the imaginary part of the QMF subsamples */ 120 INT numberBands, /*!< number of QMF bands */ 121 INT numberCols, /*!< number of QMF subsamples */ 122 INT *qmfScale, /*!< sclefactor of QMF subsamples */ 123 INT *energyScale) /*!< scalefactor of energies */ 124 { 125 int j, k; 126 int scale; 127 FIXP_DBL max_val = FL2FXCONST_DBL(0.0f); 128 129 /* Get Scratch buffer */ 130 C_ALLOC_SCRATCH_START(tmpNrg, FIXP_DBL, QMF_CHANNELS*QMF_MAX_TIME_SLOTS/2); 131 132 FDK_ASSERT(numberBands <= QMF_CHANNELS); 133 FDK_ASSERT(numberCols <= QMF_MAX_TIME_SLOTS); 134 135 /* Get max possible scaling of QMF data */ 136 scale = DFRACT_BITS; 137 for (k=0; k<numberCols; k++) { 138 scale = fixMin(scale, fixMin(getScalefactor(realValues[k], numberBands), getScalefactor(imagValues[k], numberBands))); 139 } 140 141 /* Tweak scaling stability for zero signal to non-zero signal transitions */ 142 if (scale >= DFRACT_BITS-1) { 143 scale = (FRACT_BITS-1-*qmfScale); 144 } 145 /* prevent scaling of QFM values to -1.f */ 146 scale = fixMax(0,scale-1); 147 148 /* Update QMF scale */ 149 *qmfScale += scale; 150 151 /* 152 Calculate energy of each time slot pair, max energy 153 and shift QMF values as far as possible to the left. 154 */ 155 { 156 FIXP_DBL *nrgValues = tmpNrg; 157 for (k=0; k<numberCols; k+=2) 158 { 159 /* Load band vector addresses of 2 consecutive timeslots */ 160 FIXP_DBL *RESTRICT r0 = realValues[k]; 161 FIXP_DBL *RESTRICT i0 = imagValues[k]; 162 FIXP_DBL *RESTRICT r1 = realValues[k+1]; 163 FIXP_DBL *RESTRICT i1 = imagValues[k+1]; 164 for (j=0; j<numberBands; j++) 165 { 166 FIXP_DBL energy; 167 FIXP_DBL tr0,tr1,ti0,ti1; 168 169 /* Read QMF values of 2 timeslots */ 170 tr0 = r0[j]; tr1 = r1[j]; ti0 = i0[j]; ti1 = i1[j]; 171 172 /* Scale QMF Values and Calc Energy of both timeslots */ 173 tr0 <<= scale; 174 ti0 <<= scale; 175 energy = fPow2AddDiv2(fPow2Div2(tr0), ti0) >> 1; 176 177 tr1 <<= scale; 178 ti1 <<= scale; 179 energy += fPow2AddDiv2(fPow2Div2(tr1), ti1) >> 1; 180 181 /* Write timeslot pair energy to scratch */ 182 *nrgValues++ = energy; 183 max_val = fixMax(max_val, energy); 184 185 /* Write back scaled QMF values */ 186 r0[j] = tr0; r1[j] = tr1; i0[j] = ti0; i1[j] = ti1; 187 } 188 } 189 } 190 /* energyScale: scalefactor energies of current frame */ 191 *energyScale = 2*(*qmfScale)-1; /* if qmfScale > 0: nr of right shifts otherwise nr of left shifts */ 192 193 /* Scale timeslot pair energies and write to output buffer */ 194 scale = CountLeadingBits(max_val); 195 { 196 FIXP_DBL *nrgValues = tmpNrg; 197 for (k=0; k<numberCols>>1; k++) { 198 scaleValues(energyValues[k], nrgValues, numberBands, scale); 199 nrgValues += numberBands; 200 } 201 *energyScale += scale; 202 } 203 204 /* Free Scratch buffer */ 205 C_ALLOC_SCRATCH_END(tmpNrg, FIXP_DBL, QMF_CHANNELS*QMF_MAX_TIME_SLOTS/2); 206 } 207 208 LNK_SECTION_CODE_L1 209 static void 210 FDKsbrEnc_getEnergyFromCplxQmfDataFull(FIXP_DBL **RESTRICT energyValues,/*!< the result of the operation */ 211 FIXP_DBL **RESTRICT realValues, /*!< the real part of the QMF subsamples */ 212 FIXP_DBL **RESTRICT imagValues, /*!< the imaginary part of the QMF subsamples */ 213 int numberBands, /*!< number of QMF bands */ 214 int numberCols, /*!< number of QMF subsamples */ 215 int *qmfScale, /*!< sclefactor of QMF subsamples */ 216 int *energyScale) /*!< scalefactor of energies */ 217 { 218 int j, k; 219 int scale; 220 FIXP_DBL max_val = FL2FXCONST_DBL(0.0f); 221 222 /* Get Scratch buffer */ 223 C_ALLOC_SCRATCH_START(tmpNrg, FIXP_DBL, QMF_MAX_TIME_SLOTS*QMF_CHANNELS/2); 224 225 FDK_ASSERT(numberBands <= QMF_CHANNELS); 226 FDK_ASSERT(numberCols <= QMF_MAX_TIME_SLOTS/2); 227 228 /* Get max possible scaling of QMF data */ 229 scale = DFRACT_BITS; 230 for (k=0; k<numberCols; k++) { 231 scale = fixMin(scale, fixMin(getScalefactor(realValues[k], numberBands), getScalefactor(imagValues[k], numberBands))); 232 } 233 234 /* Tweak scaling stability for zero signal to non-zero signal transitions */ 235 if (scale >= DFRACT_BITS-1) { 236 scale = (FRACT_BITS-1-*qmfScale); 237 } 238 /* prevent scaling of QFM values to -1.f */ 239 scale = fixMax(0,scale-1); 240 241 /* Update QMF scale */ 242 *qmfScale += scale; 243 244 /* 245 Calculate energy of each time slot pair, max energy 246 and shift QMF values as far as possible to the left. 247 */ 248 { 249 FIXP_DBL *nrgValues = tmpNrg; 250 for (k=0; k<numberCols; k++) 251 { 252 /* Load band vector addresses of 2 consecutive timeslots */ 253 FIXP_DBL *RESTRICT r0 = realValues[k]; 254 FIXP_DBL *RESTRICT i0 = imagValues[k]; 255 for (j=0; j<numberBands; j++) 256 { 257 FIXP_DBL energy; 258 FIXP_DBL tr0,ti0; 259 260 /* Read QMF values of 2 timeslots */ 261 tr0 = r0[j]; ti0 = i0[j]; 262 263 /* Scale QMF Values and Calc Energy of both timeslots */ 264 tr0 <<= scale; 265 ti0 <<= scale; 266 energy = fPow2AddDiv2(fPow2Div2(tr0), ti0); 267 *nrgValues++ = energy; 268 269 max_val = fixMax(max_val, energy); 270 271 /* Write back scaled QMF values */ 272 r0[j] = tr0; i0[j] = ti0; 273 } 274 } 275 } 276 /* energyScale: scalefactor energies of current frame */ 277 *energyScale = 2*(*qmfScale)-1; /* if qmfScale > 0: nr of right shifts otherwise nr of left shifts */ 278 279 /* Scale timeslot pair energies and write to output buffer */ 280 scale = CountLeadingBits(max_val); 281 { 282 FIXP_DBL *nrgValues = tmpNrg; 283 for (k=0; k<numberCols; k++) { 284 scaleValues(energyValues[k], nrgValues, numberBands, scale); 285 nrgValues += numberBands; 286 } 287 *energyScale += scale; 288 } 289 290 /* Free Scratch buffer */ 291 C_ALLOC_SCRATCH_END(tmpNrg, FIXP_DBL, QMF_MAX_TIME_SLOTS*QMF_CHANNELS/2); 292 } 293 294 /***************************************************************************/ 295 /*! 296 297 \brief Quantisation of the panorama value (balance) 298 299 \return the quantized pan value 300 301 ****************************************************************************/ 302 static INT 303 mapPanorama(INT nrgVal, /*! integer value of the energy */ 304 INT ampRes, /*! amplitude resolution [1.5/3dB] */ 305 INT *quantError /*! quantization error of energy val*/ 306 ) 307 { 308 int i; 309 INT min_val, val; 310 UCHAR panIndex; 311 INT sign; 312 313 sign = nrgVal > 0 ? 1 : -1; 314 315 nrgVal *= sign; 316 317 min_val = FDK_INT_MAX; 318 panIndex = 0; 319 for (i = 0; i < maxIndex[ampRes]; i++) { 320 val = fixp_abs ((nrgVal - (INT)panTable[ampRes][i])); 321 322 if (val < min_val) { 323 min_val = val; 324 panIndex = i; 325 } 326 } 327 328 *quantError=min_val; 329 330 return panTable[ampRes][maxIndex[ampRes]-1] + sign * panTable[ampRes][panIndex]; 331 } 332 333 334 /***************************************************************************/ 335 /*! 336 337 \brief Quantisation of the noise floor levels 338 339 \return void 340 341 ****************************************************************************/ 342 static void 343 sbrNoiseFloorLevelsQuantisation(SCHAR *RESTRICT iNoiseLevels, /*! quantized noise levels */ 344 FIXP_DBL *RESTRICT NoiseLevels, /*! the noise levels */ 345 INT coupling /*! the coupling flag */ 346 ) 347 { 348 INT i; 349 INT tmp, dummy; 350 351 /* Quantisation, similar to sfb quant... */ 352 for (i = 0; i < MAX_NUM_NOISE_VALUES; i++) { 353 /* tmp = NoiseLevels[i] > (PFLOAT)30.0f ? 30: (INT) (NoiseLevels[i] + (PFLOAT)0.5); */ 354 /* 30>>6 = 0.46875 */ 355 if ((FIXP_DBL)NoiseLevels[i] > FL2FXCONST_DBL(0.46875f)) { 356 tmp = 30; 357 } 358 else { 359 /* tmp = (INT)((FIXP_DBL)NoiseLevels[i] + (FL2FXCONST_DBL(0.5f)>>(*/ /* FRACT_BITS+ */ /* 6-1)));*/ 360 /* tmp = tmp >> (DFRACT_BITS-1-6); */ /* conversion to integer happens here */ 361 /* rounding is done by shifting one bit less than necessary to the right, adding '1' and then shifting the final bit */ 362 tmp = ((((INT)NoiseLevels[i])>>(DFRACT_BITS-1-LD_DATA_SHIFT)) ); /* conversion to integer */ 363 if (tmp != 0) 364 tmp += 1; 365 } 366 367 if (coupling) { 368 tmp = tmp < -30 ? -30 : tmp; 369 tmp = mapPanorama (tmp,1,&dummy); 370 } 371 iNoiseLevels[i] = tmp; 372 } 373 } 374 375 /***************************************************************************/ 376 /*! 377 378 \brief Calculation of noise floor for coupling 379 380 \return void 381 382 ****************************************************************************/ 383 static void 384 coupleNoiseFloor(FIXP_DBL *RESTRICT noise_level_left, /*! noise level left (modified)*/ 385 FIXP_DBL *RESTRICT noise_level_right /*! noise level right (modified)*/ 386 ) 387 { 388 FIXP_DBL cmpValLeft,cmpValRight; 389 INT i; 390 FIXP_DBL temp1,temp2; 391 392 for (i = 0; i < MAX_NUM_NOISE_VALUES; i++) { 393 394 /* Calculation of the power function using ld64: 395 z = x^y; 396 z' = CalcLd64(z) = y*CalcLd64(x)/64; 397 z = CalcInvLd64(z'); 398 */ 399 cmpValLeft = NOISE_FLOOR_OFFSET_64 - noise_level_left[i]; 400 cmpValRight = NOISE_FLOOR_OFFSET_64 - noise_level_right[i]; 401 402 if (cmpValRight < FL2FXCONST_DBL(0.0f)) { 403 temp1 = CalcInvLdData(NOISE_FLOOR_OFFSET_64 - noise_level_right[i]); 404 } 405 else { 406 temp1 = CalcInvLdData(NOISE_FLOOR_OFFSET_64 - noise_level_right[i]); 407 temp1 = temp1 << (DFRACT_BITS-1-LD_DATA_SHIFT-1); /* INT to fract conversion of result, if input of CalcInvLdData is positiv */ 408 } 409 410 if (cmpValLeft < FL2FXCONST_DBL(0.0f)) { 411 temp2 = CalcInvLdData(NOISE_FLOOR_OFFSET_64 - noise_level_left[i]); 412 } 413 else { 414 temp2 = CalcInvLdData(NOISE_FLOOR_OFFSET_64 - noise_level_left[i]); 415 temp2 = temp2 << (DFRACT_BITS-1-LD_DATA_SHIFT-1); /* INT to fract conversion of result, if input of CalcInvLdData is positiv */ 416 } 417 418 419 if ((cmpValLeft < FL2FXCONST_DBL(0.0f)) && (cmpValRight < FL2FXCONST_DBL(0.0f))) { 420 noise_level_left[i] = NOISE_FLOOR_OFFSET_64 - (CalcLdData(((temp1>>1) + (temp2>>1)))); /* no scaling needed! both values are dfract */ 421 noise_level_right[i] = CalcLdData(temp2) - CalcLdData(temp1); 422 } 423 424 if ((cmpValLeft >= FL2FXCONST_DBL(0.0f)) && (cmpValRight >= FL2FXCONST_DBL(0.0f))) { 425 noise_level_left[i] = NOISE_FLOOR_OFFSET_64 - (CalcLdData(((temp1>>1) + (temp2>>1))) + FL2FXCONST_DBL(0.109375f)); /* scaled with 7/64 */ 426 noise_level_right[i] = CalcLdData(temp2) - CalcLdData(temp1); 427 } 428 429 if ((cmpValLeft >= FL2FXCONST_DBL(0.0f)) && (cmpValRight < FL2FXCONST_DBL(0.0f))) { 430 noise_level_left[i] = NOISE_FLOOR_OFFSET_64 - (CalcLdData(((temp1>>(7+1)) + (temp2>>1))) + FL2FXCONST_DBL(0.109375f)); /* scaled with 7/64 */ 431 noise_level_right[i] = (CalcLdData(temp2) + FL2FXCONST_DBL(0.109375f)) - CalcLdData(temp1); 432 } 433 434 if ((cmpValLeft < FL2FXCONST_DBL(0.0f)) && (cmpValRight >= FL2FXCONST_DBL(0.0f))) { 435 noise_level_left[i] = NOISE_FLOOR_OFFSET_64 - (CalcLdData(((temp1>>1) + (temp2>>(7+1)))) + FL2FXCONST_DBL(0.109375f)); /* scaled with 7/64 */ 436 noise_level_right[i] = CalcLdData(temp2) - (CalcLdData(temp1) + FL2FXCONST_DBL(0.109375f)); /* scaled with 7/64 */ 437 } 438 } 439 } 440 441 /***************************************************************************/ 442 /*! 443 444 \brief Calculation of energy starting in lower band (li) up to upper band (ui) 445 over slots (start_pos) to (stop_pos) 446 447 \return void 448 449 ****************************************************************************/ 450 static FIXP_DBL 451 getEnvSfbEnergy(INT li, /*! lower band */ 452 INT ui, /*! upper band */ 453 INT start_pos, /*! start slot */ 454 INT stop_pos, /*! stop slot */ 455 INT border_pos, /*! slots scaling border */ 456 FIXP_DBL **YBuffer, /*! sfb energy buffer */ 457 INT YBufferSzShift, /*! Energy buffer index scale */ 458 INT scaleNrg0, /*! scaling of lower slots */ 459 INT scaleNrg1) /*! scaling of upper slots */ 460 { 461 /* use dynamic scaling for outer energy loop; 462 energies are critical and every bit is important */ 463 int sc0, sc1, k, l; 464 465 FIXP_DBL nrgSum, nrg1, nrg2, accu1, accu2; 466 INT dynScale, dynScale1, dynScale2; 467 if(ui-li==0) dynScale = DFRACT_BITS-1; 468 else 469 dynScale = CalcLdInt(ui-li)>>(DFRACT_BITS-1-LD_DATA_SHIFT); 470 471 sc0 = fixMin(scaleNrg0,Y_NRG_SCALE); sc1 = fixMin(scaleNrg1,Y_NRG_SCALE); 472 /* dynScale{1,2} is set such that the right shift below is positive */ 473 dynScale1 = fixMin((scaleNrg0-sc0),dynScale); 474 dynScale2 = fixMin((scaleNrg1-sc1),dynScale); 475 nrgSum = accu1 = accu2 = (FIXP_DBL)0; 476 477 for (k = li; k < ui; k++) { 478 nrg1 = nrg2 = (FIXP_DBL)0; 479 for (l = start_pos; l < border_pos; l++) { 480 nrg1 += YBuffer[l>>YBufferSzShift][k] >> sc0; 481 } 482 for (; l < stop_pos; l++) { 483 nrg2 += YBuffer[l>>YBufferSzShift][k] >> sc1; 484 } 485 accu1 += (nrg1>>dynScale1); 486 accu2 += (nrg2>>dynScale2); 487 } 488 /* This shift factor is always positive. See comment above. */ 489 nrgSum += ( accu1 >> fixMin((scaleNrg0-sc0-dynScale1),(DFRACT_BITS-1)) ) 490 + ( accu2 >> fixMin((scaleNrg1-sc1-dynScale2),(DFRACT_BITS-1)) ); 491 492 return nrgSum; 493 } 494 495 /***************************************************************************/ 496 /*! 497 498 \brief Energy compensation in missing harmonic mode 499 500 \return void 501 502 ****************************************************************************/ 503 static FIXP_DBL 504 mhLoweringEnergy(FIXP_DBL nrg, INT M) 505 { 506 /* 507 Compensating for the fact that we in the decoder map the "average energy to every QMF 508 band, and use this when we calculate the boost-factor. Since the mapped energy isn't 509 the average energy but the maximum energy in case of missing harmonic creation, we will 510 in the boost function calculate that too much limiting has been applied and hence we will 511 boost the signal although it isn't called for. Hence we need to compensate for this by 512 lowering the transmitted energy values for the sines so they will get the correct level 513 after the boost is applied. 514 */ 515 if(M > 2){ 516 INT tmpScale; 517 tmpScale = CountLeadingBits(nrg); 518 nrg <<= tmpScale; 519 nrg = fMult(nrg, FL2FXCONST_DBL(0.398107267f)); /* The maximum boost is 1.584893, so the maximum attenuation should be square(1/1.584893) = 0.398107267 */ 520 nrg >>= tmpScale; 521 } 522 else{ 523 if(M > 1){ 524 nrg >>= 1; 525 } 526 } 527 528 return nrg; 529 } 530 531 /***************************************************************************/ 532 /*! 533 534 \brief Energy compensation in none missing harmonic mode 535 536 \return void 537 538 ****************************************************************************/ 539 static FIXP_DBL 540 nmhLoweringEnergy(FIXP_DBL nrg, FIXP_DBL nrgSum, INT M) 541 { 542 if (nrg>FL2FXCONST_DBL(0)) { 543 int sc=0; 544 /* gain = nrgSum / (nrg*(M+1)) */ 545 FIXP_DBL gain = fMult(fDivNorm(nrgSum, nrg, &sc), GetInvInt(M+1)); 546 547 /* reduce nrg if gain smaller 1.f */ 548 if ( !((sc>=0) && ( gain > ((FIXP_DBL)MAXVAL_DBL>>sc) )) ) { 549 nrg = fMult(scaleValue(gain,sc), nrg); 550 } 551 } 552 return nrg; 553 } 554 555 /***************************************************************************/ 556 /*! 557 558 \brief calculates the envelope values from the energies, depending on 559 framing and stereo mode 560 561 \return void 562 563 ****************************************************************************/ 564 static void 565 calculateSbrEnvelope (FIXP_DBL **RESTRICT YBufferLeft, /*! energy buffer left */ 566 FIXP_DBL **RESTRICT YBufferRight, /*! energy buffer right */ 567 int *RESTRICT YBufferScaleLeft, /*! scale energy buffer left */ 568 int *RESTRICT YBufferScaleRight, /*! scale energy buffer right */ 569 const SBR_FRAME_INFO *frame_info, /*! frame info vector */ 570 SCHAR *RESTRICT sfb_nrgLeft, /*! sfb energy buffer left */ 571 SCHAR *RESTRICT sfb_nrgRight, /*! sfb energy buffer right */ 572 HANDLE_SBR_CONFIG_DATA h_con, /*! handle to config data */ 573 HANDLE_ENV_CHANNEL h_sbr, /*! envelope channel handle */ 574 SBR_STEREO_MODE stereoMode, /*! stereo coding mode */ 575 INT* maxQuantError, /*! maximum quantization error, for panorama. */ 576 int YBufferSzShift) /*! Energy buffer index scale */ 577 578 { 579 int i, j, m = 0; 580 INT no_of_bands, start_pos, stop_pos, li, ui; 581 FREQ_RES freq_res; 582 583 INT ca = 2 - h_sbr->encEnvData.init_sbr_amp_res; 584 INT oneBitLess = 0; 585 if (ca == 2) 586 oneBitLess = 1; /* LD_DATA_SHIFT => ld64 scaling; one bit less for rounding */ 587 588 INT quantError; 589 INT nEnvelopes = frame_info->nEnvelopes; 590 INT short_env = frame_info->shortEnv - 1; 591 INT timeStep = h_sbr->sbrExtractEnvelope.time_step; 592 INT commonScale,scaleLeft0,scaleLeft1; 593 INT scaleRight0=0,scaleRight1=0; 594 595 commonScale = fixMin(YBufferScaleLeft[0],YBufferScaleLeft[1]); 596 597 if (stereoMode == SBR_COUPLING) { 598 commonScale = fixMin(commonScale,YBufferScaleRight[0]); 599 commonScale = fixMin(commonScale,YBufferScaleRight[1]); 600 } 601 602 commonScale = commonScale - 7; 603 604 scaleLeft0 = YBufferScaleLeft[0] - commonScale; 605 scaleLeft1 = YBufferScaleLeft[1] - commonScale ; 606 FDK_ASSERT ((scaleLeft0 >= 0) && (scaleLeft1 >= 0)); 607 608 if (stereoMode == SBR_COUPLING) { 609 scaleRight0 = YBufferScaleRight[0] - commonScale; 610 scaleRight1 = YBufferScaleRight[1] - commonScale; 611 FDK_ASSERT ((scaleRight0 >= 0) && (scaleRight1 >= 0)); 612 *maxQuantError = 0; 613 } 614 615 for (i = 0; i < nEnvelopes; i++) { 616 617 FIXP_DBL pNrgLeft[QMF_MAX_TIME_SLOTS]; 618 FIXP_DBL pNrgRight[QMF_MAX_TIME_SLOTS]; 619 FIXP_DBL envNrgLeft = FL2FXCONST_DBL(0.0f); 620 FIXP_DBL envNrgRight = FL2FXCONST_DBL(0.0f); 621 int missingHarmonic[QMF_MAX_TIME_SLOTS]; 622 int count[QMF_MAX_TIME_SLOTS]; 623 624 start_pos = timeStep * frame_info->borders[i]; 625 stop_pos = timeStep * frame_info->borders[i + 1]; 626 freq_res = frame_info->freqRes[i]; 627 no_of_bands = h_con->nSfb[freq_res]; 628 629 if (i == short_env) { 630 stop_pos -= fixMax(2, timeStep); /* consider at least 2 QMF slots less for short envelopes (envelopes just before transients) */ 631 } 632 633 for (j = 0; j < no_of_bands; j++) { 634 FIXP_DBL nrgLeft = FL2FXCONST_DBL(0.0f); 635 FIXP_DBL nrgRight = FL2FXCONST_DBL(0.0f); 636 637 li = h_con->freqBandTable[freq_res][j]; 638 ui = h_con->freqBandTable[freq_res][j + 1]; 639 640 if(freq_res == FREQ_RES_HIGH){ 641 if(j == 0 && ui-li > 1){ 642 li++; 643 } 644 } 645 else{ 646 if(j == 0 && ui-li > 2){ 647 li++; 648 } 649 } 650 651 /* 652 Find out whether a sine will be missing in the scale-factor 653 band that we're currently processing. 654 */ 655 missingHarmonic[j] = 0; 656 657 if(h_sbr->encEnvData.addHarmonicFlag){ 658 659 if(freq_res == FREQ_RES_HIGH){ 660 if(h_sbr->encEnvData.addHarmonic[j]){ /*A missing sine in the current band*/ 661 missingHarmonic[j] = 1; 662 } 663 } 664 else{ 665 INT i; 666 INT startBandHigh = 0; 667 INT stopBandHigh = 0; 668 669 while(h_con->freqBandTable[FREQ_RES_HIGH][startBandHigh] < h_con->freqBandTable[FREQ_RES_LOW][j]) 670 startBandHigh++; 671 while(h_con->freqBandTable[FREQ_RES_HIGH][stopBandHigh] < h_con->freqBandTable[FREQ_RES_LOW][j + 1]) 672 stopBandHigh++; 673 674 for(i = startBandHigh; i<stopBandHigh; i++){ 675 if(h_sbr->encEnvData.addHarmonic[i]){ 676 missingHarmonic[j] = 1; 677 } 678 } 679 } 680 } 681 682 /* 683 If a sine is missing in a scalefactorband, with more than one qmf channel 684 use the nrg from the channel with the largest nrg rather than the mean. 685 Compensate for the boost calculation in the decdoder. 686 */ 687 int border_pos = fixMin(stop_pos, h_sbr->sbrExtractEnvelope.YBufferWriteOffset<<YBufferSzShift); 688 689 if(missingHarmonic[j]){ 690 691 int k; 692 count[j] = stop_pos - start_pos; 693 nrgLeft = FL2FXCONST_DBL(0.0f); 694 695 for (k = li; k < ui; k++) { 696 FIXP_DBL tmpNrg; 697 tmpNrg = getEnvSfbEnergy(k, 698 k+1, 699 start_pos, 700 stop_pos, 701 border_pos, 702 YBufferLeft, 703 YBufferSzShift, 704 scaleLeft0, 705 scaleLeft1); 706 707 nrgLeft = fixMax(nrgLeft, tmpNrg); 708 } 709 710 /* Energy lowering compensation */ 711 nrgLeft = mhLoweringEnergy(nrgLeft, ui-li); 712 713 if (stereoMode == SBR_COUPLING) { 714 715 nrgRight = FL2FXCONST_DBL(0.0f); 716 717 for (k = li; k < ui; k++) { 718 FIXP_DBL tmpNrg; 719 tmpNrg = getEnvSfbEnergy(k, 720 k+1, 721 start_pos, 722 stop_pos, 723 border_pos, 724 YBufferRight, 725 YBufferSzShift, 726 scaleRight0, 727 scaleRight1); 728 729 nrgRight = fixMax(nrgRight, tmpNrg); 730 } 731 732 /* Energy lowering compensation */ 733 nrgRight = mhLoweringEnergy(nrgRight, ui-li); 734 } 735 } /* end missingHarmonic */ 736 else{ 737 count[j] = (stop_pos - start_pos) * (ui - li); 738 739 nrgLeft = getEnvSfbEnergy(li, 740 ui, 741 start_pos, 742 stop_pos, 743 border_pos, 744 YBufferLeft, 745 YBufferSzShift, 746 scaleLeft0, 747 scaleLeft1); 748 749 if (stereoMode == SBR_COUPLING) { 750 nrgRight = getEnvSfbEnergy(li, 751 ui, 752 start_pos, 753 stop_pos, 754 border_pos, 755 YBufferRight, 756 YBufferSzShift, 757 scaleRight0, 758 scaleRight1); 759 } 760 } /* !missingHarmonic */ 761 762 /* save energies */ 763 pNrgLeft[j] = nrgLeft; 764 pNrgRight[j] = nrgRight; 765 envNrgLeft += nrgLeft; 766 envNrgRight += nrgRight; 767 768 } /* j */ 769 770 for (j = 0; j < no_of_bands; j++) { 771 772 FIXP_DBL nrgLeft2 = FL2FXCONST_DBL(0.0f); 773 FIXP_DBL nrgLeft = pNrgLeft[j]; 774 FIXP_DBL nrgRight = pNrgRight[j]; 775 776 /* None missing harmonic Energy lowering compensation */ 777 if(!missingHarmonic[j] && h_sbr->fLevelProtect) { 778 /* in case of missing energy in base band, 779 reduce reference energy to prevent overflows in decoder output */ 780 nrgLeft = nmhLoweringEnergy(nrgLeft, envNrgLeft, no_of_bands); 781 if (stereoMode == SBR_COUPLING) { 782 nrgRight = nmhLoweringEnergy(nrgRight, envNrgRight, no_of_bands); 783 } 784 } 785 786 if (stereoMode == SBR_COUPLING) { 787 /* calc operation later with log */ 788 nrgLeft2 = nrgLeft; 789 nrgLeft = (nrgRight + nrgLeft) >> 1; 790 } 791 792 /* nrgLeft = f20_log2(nrgLeft / (PFLOAT)(count * h_sbr->sbrQmf.no_channels))+(PFLOAT)44; */ 793 /* If nrgLeft == 0 then the Log calculations below do fail. */ 794 if (nrgLeft > FL2FXCONST_DBL(0.0f)) 795 { 796 FIXP_DBL tmp0,tmp1,tmp2,tmp3; 797 INT tmpScale; 798 799 tmpScale = CountLeadingBits(nrgLeft); 800 nrgLeft = nrgLeft << tmpScale; 801 802 tmp0 = CalcLdData(nrgLeft); /* scaled by 1/64 */ 803 tmp1 = ((FIXP_DBL) (commonScale+tmpScale)) << (DFRACT_BITS-1-LD_DATA_SHIFT-1); /* scaled by 1/64 */ 804 tmp2 = ((FIXP_DBL)(count[j]*h_con->noQmfBands)) << (DFRACT_BITS-1-14-1); 805 tmp2 = CalcLdData(tmp2); /* scaled by 1/64 */ 806 tmp3 = FL2FXCONST_DBL(0.6875f-0.21875f-0.015625f)>>1; /* scaled by 1/64 */ 807 808 nrgLeft = ((tmp0-tmp2)>>1) + (tmp3 - tmp1); 809 } else { 810 nrgLeft = FL2FXCONST_DBL(-1.0f); 811 } 812 813 /* ld64 to integer conversion */ 814 nrgLeft = fixMin(fixMax(nrgLeft,FL2FXCONST_DBL(0.0f)),FL2FXCONST_DBL(0.5f)); 815 nrgLeft = (FIXP_DBL)(LONG)nrgLeft >> (DFRACT_BITS-1-LD_DATA_SHIFT-1-oneBitLess-1); 816 sfb_nrgLeft[m] = ((INT)nrgLeft+1)>>1; /* rounding */ 817 818 if (stereoMode == SBR_COUPLING) { 819 FIXP_DBL scaleFract; 820 821 if (nrgRight != FL2FXCONST_DBL(0.0f)) { 822 int sc0 = CountLeadingBits(nrgLeft2); 823 int sc1 = CountLeadingBits(nrgRight); 824 825 scaleFract = ((FIXP_DBL)(sc0-sc1)) << (DFRACT_BITS-1-LD_DATA_SHIFT); /* scale value in ld64 representation */ 826 nrgRight = CalcLdData(nrgLeft2<<sc0) - CalcLdData(nrgRight<<sc1) - scaleFract; 827 } 828 else 829 nrgRight = FL2FXCONST_DBL(0.5f); /* ld64(4294967296.0f) */ 830 831 /* ld64 to integer conversion */ 832 nrgRight = (FIXP_DBL)(LONG)(nrgRight) >> (DFRACT_BITS-1-LD_DATA_SHIFT-1-oneBitLess); 833 nrgRight = (nrgRight+(FIXP_DBL)1)>>1; /* rounding */ 834 835 sfb_nrgRight[m] = mapPanorama (nrgRight,h_sbr->encEnvData.init_sbr_amp_res,&quantError); 836 837 *maxQuantError = fixMax(quantError, *maxQuantError); 838 } 839 840 m++; 841 } /* j */ 842 843 /* Do energy compensation for sines that are present in two 844 QMF-bands in the original, but will only occur in one band in 845 the decoder due to the synthetic sine coding.*/ 846 if (h_con->useParametricCoding) { 847 m-=no_of_bands; 848 for (j = 0; j < no_of_bands; j++) { 849 if (freq_res==FREQ_RES_HIGH && h_sbr->sbrExtractEnvelope.envelopeCompensation[j]){ 850 sfb_nrgLeft[m] -= (ca * fixp_abs((INT)h_sbr->sbrExtractEnvelope.envelopeCompensation[j])); 851 } 852 sfb_nrgLeft[m] = fixMax(0, sfb_nrgLeft[m]); 853 m++; 854 } 855 } /* useParametricCoding */ 856 857 } /* i*/ 858 } 859 860 /***************************************************************************/ 861 /*! 862 863 \brief calculates the noise floor and the envelope values from the 864 energies, depending on framing and stereo mode 865 866 FDKsbrEnc_extractSbrEnvelope is the main function for encoding and writing the 867 envelope and the noise floor. The function includes the following processes: 868 869 -Analysis subband filtering. 870 -Encoding SA and pan parameters (if enabled). 871 -Transient detection. 872 873 ****************************************************************************/ 874 875 LNK_SECTION_CODE_L1 876 void 877 FDKsbrEnc_extractSbrEnvelope1 ( 878 HANDLE_SBR_CONFIG_DATA h_con, /*! handle to config data */ 879 HANDLE_SBR_HEADER_DATA sbrHeaderData, 880 HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData, 881 HANDLE_ENV_CHANNEL hEnvChan, 882 HANDLE_COMMON_DATA hCmonData, 883 SBR_ENV_TEMP_DATA *eData, 884 SBR_FRAME_TEMP_DATA *fData 885 ) 886 { 887 888 HANDLE_SBR_EXTRACT_ENVELOPE sbrExtrEnv = &hEnvChan->sbrExtractEnvelope; 889 890 if (sbrExtrEnv->YBufferSzShift == 0) 891 FDKsbrEnc_getEnergyFromCplxQmfDataFull(&sbrExtrEnv->YBuffer[sbrExtrEnv->YBufferWriteOffset], 892 sbrExtrEnv->rBuffer + sbrExtrEnv->rBufferReadOffset, 893 sbrExtrEnv->iBuffer + sbrExtrEnv->rBufferReadOffset, 894 h_con->noQmfBands, 895 sbrExtrEnv->no_cols, 896 &hEnvChan->qmfScale, 897 &sbrExtrEnv->YBufferScale[1]); 898 else 899 FDKsbrEnc_getEnergyFromCplxQmfData(&sbrExtrEnv->YBuffer[sbrExtrEnv->YBufferWriteOffset], 900 sbrExtrEnv->rBuffer + sbrExtrEnv->rBufferReadOffset, 901 sbrExtrEnv->iBuffer + sbrExtrEnv->rBufferReadOffset, 902 h_con->noQmfBands, 903 sbrExtrEnv->no_cols, 904 &hEnvChan->qmfScale, 905 &sbrExtrEnv->YBufferScale[1]); 906 907 908 909 /* 910 Precalculation of Tonality Quotas COEFF Transform OK 911 */ 912 FDKsbrEnc_CalculateTonalityQuotas(&hEnvChan->TonCorr, 913 sbrExtrEnv->rBuffer, 914 sbrExtrEnv->iBuffer, 915 h_con->freqBandTable[HI][h_con->nSfb[HI]], 916 hEnvChan->qmfScale); 917 918 919 920 /* 921 Transient detection COEFF Transform OK 922 */ 923 FDKsbrEnc_transientDetect(&hEnvChan->sbrTransientDetector, 924 sbrExtrEnv->YBuffer, 925 sbrExtrEnv->YBufferScale, 926 eData->transient_info, 927 sbrExtrEnv->YBufferWriteOffset, 928 sbrExtrEnv->YBufferSzShift, 929 sbrExtrEnv->time_step, 930 hEnvChan->SbrEnvFrame.frameMiddleSlot); 931 932 933 934 /* 935 Generate flags for 2 env in a FIXFIX-frame. 936 Remove this function to get always 1 env per FIXFIX-frame. 937 */ 938 939 /* 940 frame Splitter COEFF Transform OK 941 */ 942 FDKsbrEnc_frameSplitter(sbrExtrEnv->YBuffer, 943 sbrExtrEnv->YBufferScale, 944 &hEnvChan->sbrTransientDetector, 945 h_con->freqBandTable[1], 946 eData->transient_info, 947 sbrExtrEnv->YBufferWriteOffset, 948 sbrExtrEnv->YBufferSzShift, 949 h_con->nSfb[1], 950 sbrExtrEnv->time_step, 951 sbrExtrEnv->no_cols); 952 953 954 } 955 956 /***************************************************************************/ 957 /*! 958 959 \brief calculates the noise floor and the envelope values from the 960 energies, depending on framing and stereo mode 961 962 FDKsbrEnc_extractSbrEnvelope is the main function for encoding and writing the 963 envelope and the noise floor. The function includes the following processes: 964 965 -Determine time/frequency division of current granule. 966 -Sending transient info to bitstream. 967 -Set amp_res to 1.5 dB if the current frame contains only one envelope. 968 -Lock dynamic bandwidth frequency change if the next envelope not starts on a 969 frame boundary. 970 -MDCT transposer (needed to detect where harmonics will be missing). 971 -Spectrum Estimation (used for pulse train and missing harmonics detection). 972 -Pulse train detection. 973 -Inverse Filtering detection. 974 -Waveform Coding. 975 -Missing Harmonics detection. 976 -Extract envelope of current frame. 977 -Noise floor estimation. 978 -Noise floor quantisation and coding. 979 -Encode envelope of current frame. 980 -Send the encoded data to the bitstream. 981 -Write to bitstream. 982 983 ****************************************************************************/ 984 985 LNK_SECTION_CODE_L1 986 void 987 FDKsbrEnc_extractSbrEnvelope2 ( 988 HANDLE_SBR_CONFIG_DATA h_con, /*! handle to config data */ 989 HANDLE_SBR_HEADER_DATA sbrHeaderData, 990 HANDLE_PARAMETRIC_STEREO hParametricStereo, 991 HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData, 992 HANDLE_ENV_CHANNEL h_envChan0, 993 HANDLE_ENV_CHANNEL h_envChan1, 994 HANDLE_COMMON_DATA hCmonData, 995 SBR_ENV_TEMP_DATA *eData, 996 SBR_FRAME_TEMP_DATA *fData, 997 int clearOutput 998 ) 999 { 1000 HANDLE_ENV_CHANNEL h_envChan[MAX_NUM_CHANNELS] = {h_envChan0, h_envChan1}; 1001 int ch, i, j, c, YSzShift = h_envChan[0]->sbrExtractEnvelope.YBufferSzShift; 1002 1003 SBR_STEREO_MODE stereoMode = h_con->stereoMode; 1004 int nChannels = h_con->nChannels; 1005 const int *v_tuning; 1006 static const int v_tuningHEAAC[6] = { 0, 2, 4, 0, 0, 0 }; 1007 1008 static const int v_tuningELD[6] = { 0, 2, 3, 0, 0, 0 }; 1009 1010 if (h_con->sbrSyntaxFlags & SBR_SYNTAX_LOW_DELAY) 1011 v_tuning = v_tuningELD; 1012 else 1013 v_tuning = v_tuningHEAAC; 1014 1015 1016 /* 1017 Select stereo mode. 1018 */ 1019 if (stereoMode == SBR_COUPLING) { 1020 if (eData[0].transient_info[1] && eData[1].transient_info[1]) { 1021 eData[0].transient_info[0] = fixMin(eData[1].transient_info[0], eData[0].transient_info[0]); 1022 eData[1].transient_info[0] = eData[0].transient_info[0]; 1023 } 1024 else { 1025 if (eData[0].transient_info[1] && !eData[1].transient_info[1]) { 1026 eData[1].transient_info[0] = eData[0].transient_info[0]; 1027 } 1028 else { 1029 if (!eData[0].transient_info[1] && eData[1].transient_info[1]) 1030 eData[0].transient_info[0] = eData[1].transient_info[0]; 1031 else { 1032 eData[0].transient_info[0] = fixMax(eData[1].transient_info[0], eData[0].transient_info[0]); 1033 eData[1].transient_info[0] = eData[0].transient_info[0]; 1034 } 1035 } 1036 } 1037 } 1038 1039 /* 1040 Determine time/frequency division of current granule 1041 */ 1042 eData[0].frame_info = FDKsbrEnc_frameInfoGenerator(&h_envChan[0]->SbrEnvFrame, 1043 eData[0].transient_info, 1044 h_envChan[0]->sbrExtractEnvelope.pre_transient_info, 1045 h_envChan[0]->encEnvData.ldGrid, 1046 v_tuning); 1047 1048 h_envChan[0]->encEnvData.hSbrBSGrid = &h_envChan[0]->SbrEnvFrame.SbrGrid; 1049 1050 /* AAC LD patch for transient prediction */ 1051 if (h_envChan[0]->encEnvData.ldGrid && eData[0].transient_info[2]) { 1052 /* if next frame will start with transient, set shortEnv to numEnvelopes(shortend Envelope = shortEnv-1)*/ 1053 h_envChan[0]->SbrEnvFrame.SbrFrameInfo.shortEnv = h_envChan[0]->SbrEnvFrame.SbrFrameInfo.nEnvelopes; 1054 } 1055 1056 1057 switch (stereoMode) { 1058 case SBR_LEFT_RIGHT: 1059 case SBR_SWITCH_LRC: 1060 eData[1].frame_info = FDKsbrEnc_frameInfoGenerator(&h_envChan[1]->SbrEnvFrame, 1061 eData[1].transient_info, 1062 h_envChan[1]->sbrExtractEnvelope.pre_transient_info, 1063 h_envChan[1]->encEnvData.ldGrid, 1064 v_tuning); 1065 1066 h_envChan[1]->encEnvData.hSbrBSGrid = &h_envChan[1]->SbrEnvFrame.SbrGrid; 1067 1068 if (h_envChan[1]->encEnvData.ldGrid && eData[1].transient_info[2]) { 1069 /* if next frame will start with transient, set shortEnv to numEnvelopes(shortend Envelope = shortEnv-1)*/ 1070 h_envChan[1]->SbrEnvFrame.SbrFrameInfo.shortEnv = h_envChan[1]->SbrEnvFrame.SbrFrameInfo.nEnvelopes; 1071 } 1072 1073 /* compare left and right frame_infos */ 1074 if (eData[0].frame_info->nEnvelopes != eData[1].frame_info->nEnvelopes) { 1075 stereoMode = SBR_LEFT_RIGHT; 1076 } else { 1077 for (i = 0; i < eData[0].frame_info->nEnvelopes + 1; i++) { 1078 if (eData[0].frame_info->borders[i] != eData[1].frame_info->borders[i]) { 1079 stereoMode = SBR_LEFT_RIGHT; 1080 break; 1081 } 1082 } 1083 for (i = 0; i < eData[0].frame_info->nEnvelopes; i++) { 1084 if (eData[0].frame_info->freqRes[i] != eData[1].frame_info->freqRes[i]) { 1085 stereoMode = SBR_LEFT_RIGHT; 1086 break; 1087 } 1088 } 1089 if (eData[0].frame_info->shortEnv != eData[1].frame_info->shortEnv) { 1090 stereoMode = SBR_LEFT_RIGHT; 1091 } 1092 } 1093 break; 1094 case SBR_COUPLING: 1095 eData[1].frame_info = eData[0].frame_info; 1096 h_envChan[1]->encEnvData.hSbrBSGrid = &h_envChan[0]->SbrEnvFrame.SbrGrid; 1097 break; 1098 case SBR_MONO: 1099 /* nothing to do */ 1100 break; 1101 default: 1102 FDK_ASSERT (0); 1103 } 1104 1105 1106 for (ch = 0; ch < nChannels;ch++) 1107 { 1108 HANDLE_ENV_CHANNEL hEnvChan = h_envChan[ch]; 1109 HANDLE_SBR_EXTRACT_ENVELOPE sbrExtrEnv = &hEnvChan->sbrExtractEnvelope; 1110 SBR_ENV_TEMP_DATA *ed = &eData[ch]; 1111 1112 1113 /* 1114 Send transient info to bitstream and store for next call 1115 */ 1116 sbrExtrEnv->pre_transient_info[0] = ed->transient_info[0];/* tran_pos */ 1117 sbrExtrEnv->pre_transient_info[1] = ed->transient_info[1];/* tran_flag */ 1118 hEnvChan->encEnvData.noOfEnvelopes = ed->nEnvelopes = ed->frame_info->nEnvelopes; /* number of envelopes of current frame */ 1119 1120 /* 1121 Check if the current frame is divided into one envelope only. If so, set the amplitude 1122 resolution to 1.5 dB, otherwise may set back to chosen value 1123 */ 1124 if( ( hEnvChan->encEnvData.hSbrBSGrid->frameClass == FIXFIX ) 1125 && ( ed->nEnvelopes == 1 ) ) 1126 { 1127 1128 if (hEnvChan->encEnvData.ldGrid) 1129 hEnvChan->encEnvData.currentAmpResFF = (AMP_RES)h_con->initAmpResFF; 1130 else 1131 hEnvChan->encEnvData.currentAmpResFF = SBR_AMP_RES_1_5; 1132 1133 if ( hEnvChan->encEnvData.currentAmpResFF != hEnvChan->encEnvData.init_sbr_amp_res) { 1134 1135 FDKsbrEnc_InitSbrHuffmanTables(&hEnvChan->encEnvData, 1136 &hEnvChan->sbrCodeEnvelope, 1137 &hEnvChan->sbrCodeNoiseFloor, 1138 hEnvChan->encEnvData.currentAmpResFF); 1139 } 1140 } 1141 else { 1142 if(sbrHeaderData->sbr_amp_res != hEnvChan->encEnvData.init_sbr_amp_res ) { 1143 1144 FDKsbrEnc_InitSbrHuffmanTables(&hEnvChan->encEnvData, 1145 &hEnvChan->sbrCodeEnvelope, 1146 &hEnvChan->sbrCodeNoiseFloor, 1147 sbrHeaderData->sbr_amp_res); 1148 } 1149 } 1150 1151 if (!clearOutput) { 1152 1153 /* 1154 Tonality correction parameter extraction (inverse filtering level, noise floor additional sines). 1155 */ 1156 FDKsbrEnc_TonCorrParamExtr(&hEnvChan->TonCorr, 1157 hEnvChan->encEnvData.sbr_invf_mode_vec, 1158 ed->noiseFloor, 1159 &hEnvChan->encEnvData.addHarmonicFlag, 1160 hEnvChan->encEnvData.addHarmonic, 1161 sbrExtrEnv->envelopeCompensation, 1162 ed->frame_info, 1163 ed->transient_info, 1164 h_con->freqBandTable[HI], 1165 h_con->nSfb[HI], 1166 hEnvChan->encEnvData.sbr_xpos_mode, 1167 h_con->sbrSyntaxFlags); 1168 1169 } 1170 1171 /* Low energy in low band fix */ 1172 if ( hEnvChan->sbrTransientDetector.prevLowBandEnergy < hEnvChan->sbrTransientDetector.prevHighBandEnergy && hEnvChan->sbrTransientDetector.prevHighBandEnergy > FL2FX_DBL(0.03)) 1173 { 1174 int i; 1175 1176 hEnvChan->fLevelProtect = 1; 1177 1178 for (i=0; i<MAX_NUM_NOISE_VALUES; i++) 1179 hEnvChan->encEnvData.sbr_invf_mode_vec[i] = INVF_HIGH_LEVEL; 1180 } else { 1181 hEnvChan->fLevelProtect = 0; 1182 } 1183 1184 hEnvChan->encEnvData.sbr_invf_mode = hEnvChan->encEnvData.sbr_invf_mode_vec[0]; 1185 1186 hEnvChan->encEnvData.noOfnoisebands = hEnvChan->TonCorr.sbrNoiseFloorEstimate.noNoiseBands; 1187 1188 1189 } /* ch */ 1190 1191 1192 1193 /* 1194 Save number of scf bands per envelope 1195 */ 1196 for (ch = 0; ch < nChannels;ch++) { 1197 for (i = 0; i < eData[ch].nEnvelopes; i++){ 1198 h_envChan[ch]->encEnvData.noScfBands[i] = 1199 (eData[ch].frame_info->freqRes[i] == FREQ_RES_HIGH ? h_con->nSfb[FREQ_RES_HIGH] : h_con->nSfb[FREQ_RES_LOW]); 1200 } 1201 } 1202 1203 /* 1204 Extract envelope of current frame. 1205 */ 1206 switch (stereoMode) { 1207 case SBR_MONO: 1208 calculateSbrEnvelope (h_envChan[0]->sbrExtractEnvelope.YBuffer, NULL, 1209 h_envChan[0]->sbrExtractEnvelope.YBufferScale, NULL, 1210 eData[0].frame_info, eData[0].sfb_nrg, NULL, 1211 h_con, h_envChan[0], SBR_MONO, NULL, YSzShift); 1212 break; 1213 case SBR_LEFT_RIGHT: 1214 calculateSbrEnvelope (h_envChan[0]->sbrExtractEnvelope.YBuffer, NULL, 1215 h_envChan[0]->sbrExtractEnvelope.YBufferScale, NULL, 1216 eData[0].frame_info, eData[0].sfb_nrg, NULL, 1217 h_con, h_envChan[0], SBR_MONO, NULL, YSzShift); 1218 calculateSbrEnvelope (h_envChan[1]->sbrExtractEnvelope.YBuffer, NULL, 1219 h_envChan[1]->sbrExtractEnvelope.YBufferScale, NULL, 1220 eData[1].frame_info,eData[1].sfb_nrg, NULL, 1221 h_con, h_envChan[1], SBR_MONO, NULL, YSzShift); 1222 break; 1223 case SBR_COUPLING: 1224 calculateSbrEnvelope (h_envChan[0]->sbrExtractEnvelope.YBuffer, h_envChan[1]->sbrExtractEnvelope.YBuffer, 1225 h_envChan[0]->sbrExtractEnvelope.YBufferScale, h_envChan[1]->sbrExtractEnvelope.YBufferScale, 1226 eData[0].frame_info, eData[0].sfb_nrg, eData[1].sfb_nrg, 1227 h_con, h_envChan[0], SBR_COUPLING, &fData->maxQuantError, YSzShift); 1228 break; 1229 case SBR_SWITCH_LRC: 1230 calculateSbrEnvelope (h_envChan[0]->sbrExtractEnvelope.YBuffer, NULL, 1231 h_envChan[0]->sbrExtractEnvelope.YBufferScale, NULL, 1232 eData[0].frame_info, eData[0].sfb_nrg, NULL, 1233 h_con, h_envChan[0], SBR_MONO, NULL, YSzShift); 1234 calculateSbrEnvelope (h_envChan[1]->sbrExtractEnvelope.YBuffer, NULL, 1235 h_envChan[1]->sbrExtractEnvelope.YBufferScale, NULL, 1236 eData[1].frame_info, eData[1].sfb_nrg, NULL, 1237 h_con, h_envChan[1], SBR_MONO,NULL, YSzShift); 1238 calculateSbrEnvelope (h_envChan[0]->sbrExtractEnvelope.YBuffer, h_envChan[1]->sbrExtractEnvelope.YBuffer, 1239 h_envChan[0]->sbrExtractEnvelope.YBufferScale, h_envChan[1]->sbrExtractEnvelope.YBufferScale, 1240 eData[0].frame_info, eData[0].sfb_nrg_coupling, eData[1].sfb_nrg_coupling, 1241 h_con, h_envChan[0], SBR_COUPLING, &fData->maxQuantError, YSzShift); 1242 break; 1243 } 1244 1245 1246 1247 /* 1248 Noise floor quantisation and coding. 1249 */ 1250 1251 switch (stereoMode) { 1252 case SBR_MONO: 1253 sbrNoiseFloorLevelsQuantisation(eData[0].noise_level, eData[0].noiseFloor, 0); 1254 1255 FDKsbrEnc_codeEnvelope(eData[0].noise_level, fData->res, 1256 &h_envChan[0]->sbrCodeNoiseFloor, 1257 h_envChan[0]->encEnvData.domain_vec_noise, 0, 1258 (eData[0].frame_info->nEnvelopes > 1 ? 2 : 1), 0, 1259 sbrBitstreamData->HeaderActive); 1260 1261 break; 1262 case SBR_LEFT_RIGHT: 1263 sbrNoiseFloorLevelsQuantisation(eData[0].noise_level,eData[0].noiseFloor, 0); 1264 1265 FDKsbrEnc_codeEnvelope (eData[0].noise_level, fData->res, 1266 &h_envChan[0]->sbrCodeNoiseFloor, 1267 h_envChan[0]->encEnvData.domain_vec_noise, 0, 1268 (eData[0].frame_info->nEnvelopes > 1 ? 2 : 1), 0, 1269 sbrBitstreamData->HeaderActive); 1270 1271 sbrNoiseFloorLevelsQuantisation(eData[1].noise_level,eData[1].noiseFloor, 0); 1272 1273 FDKsbrEnc_codeEnvelope (eData[1].noise_level, fData->res, 1274 &h_envChan[1]->sbrCodeNoiseFloor, 1275 h_envChan[1]->encEnvData.domain_vec_noise, 0, 1276 (eData[1].frame_info->nEnvelopes > 1 ? 2 : 1), 0, 1277 sbrBitstreamData->HeaderActive); 1278 1279 break; 1280 1281 case SBR_COUPLING: 1282 coupleNoiseFloor(eData[0].noiseFloor,eData[1].noiseFloor); 1283 1284 sbrNoiseFloorLevelsQuantisation(eData[0].noise_level,eData[0].noiseFloor, 0); 1285 1286 FDKsbrEnc_codeEnvelope (eData[0].noise_level, fData->res, 1287 &h_envChan[0]->sbrCodeNoiseFloor, 1288 h_envChan[0]->encEnvData.domain_vec_noise, 1, 1289 (eData[0].frame_info->nEnvelopes > 1 ? 2 : 1), 0, 1290 sbrBitstreamData->HeaderActive); 1291 1292 sbrNoiseFloorLevelsQuantisation(eData[1].noise_level,eData[1].noiseFloor, 1); 1293 1294 FDKsbrEnc_codeEnvelope (eData[1].noise_level, fData->res, 1295 &h_envChan[1]->sbrCodeNoiseFloor, 1296 h_envChan[1]->encEnvData.domain_vec_noise, 1, 1297 (eData[1].frame_info->nEnvelopes > 1 ? 2 : 1), 1, 1298 sbrBitstreamData->HeaderActive); 1299 1300 break; 1301 case SBR_SWITCH_LRC: 1302 sbrNoiseFloorLevelsQuantisation(eData[0].noise_level,eData[0].noiseFloor, 0); 1303 sbrNoiseFloorLevelsQuantisation(eData[1].noise_level,eData[1].noiseFloor, 0); 1304 coupleNoiseFloor(eData[0].noiseFloor,eData[1].noiseFloor); 1305 sbrNoiseFloorLevelsQuantisation(eData[0].noise_level_coupling,eData[0].noiseFloor, 0); 1306 sbrNoiseFloorLevelsQuantisation(eData[1].noise_level_coupling,eData[1].noiseFloor, 1); 1307 break; 1308 } 1309 1310 1311 1312 /* 1313 Encode envelope of current frame. 1314 */ 1315 switch (stereoMode) { 1316 case SBR_MONO: 1317 sbrHeaderData->coupling = 0; 1318 h_envChan[0]->encEnvData.balance = 0; 1319 FDKsbrEnc_codeEnvelope (eData[0].sfb_nrg, eData[0].frame_info->freqRes, 1320 &h_envChan[0]->sbrCodeEnvelope, 1321 h_envChan[0]->encEnvData.domain_vec, 1322 sbrHeaderData->coupling, 1323 eData[0].frame_info->nEnvelopes, 0, 1324 sbrBitstreamData->HeaderActive); 1325 break; 1326 case SBR_LEFT_RIGHT: 1327 sbrHeaderData->coupling = 0; 1328 1329 h_envChan[0]->encEnvData.balance = 0; 1330 h_envChan[1]->encEnvData.balance = 0; 1331 1332 1333 FDKsbrEnc_codeEnvelope (eData[0].sfb_nrg, eData[0].frame_info->freqRes, 1334 &h_envChan[0]->sbrCodeEnvelope, 1335 h_envChan[0]->encEnvData.domain_vec, 1336 sbrHeaderData->coupling, 1337 eData[0].frame_info->nEnvelopes, 0, 1338 sbrBitstreamData->HeaderActive); 1339 FDKsbrEnc_codeEnvelope (eData[1].sfb_nrg, eData[1].frame_info->freqRes, 1340 &h_envChan[1]->sbrCodeEnvelope, 1341 h_envChan[1]->encEnvData.domain_vec, 1342 sbrHeaderData->coupling, 1343 eData[1].frame_info->nEnvelopes, 0, 1344 sbrBitstreamData->HeaderActive); 1345 break; 1346 case SBR_COUPLING: 1347 sbrHeaderData->coupling = 1; 1348 h_envChan[0]->encEnvData.balance = 0; 1349 h_envChan[1]->encEnvData.balance = 1; 1350 1351 FDKsbrEnc_codeEnvelope (eData[0].sfb_nrg, eData[0].frame_info->freqRes, 1352 &h_envChan[0]->sbrCodeEnvelope, 1353 h_envChan[0]->encEnvData.domain_vec, 1354 sbrHeaderData->coupling, 1355 eData[0].frame_info->nEnvelopes, 0, 1356 sbrBitstreamData->HeaderActive); 1357 FDKsbrEnc_codeEnvelope (eData[1].sfb_nrg, eData[1].frame_info->freqRes, 1358 &h_envChan[1]->sbrCodeEnvelope, 1359 h_envChan[1]->encEnvData.domain_vec, 1360 sbrHeaderData->coupling, 1361 eData[1].frame_info->nEnvelopes, 1, 1362 sbrBitstreamData->HeaderActive); 1363 break; 1364 case SBR_SWITCH_LRC: 1365 { 1366 INT payloadbitsLR; 1367 INT payloadbitsCOUPLING; 1368 1369 SCHAR sfbNrgPrevTemp[MAX_NUM_CHANNELS][MAX_FREQ_COEFFS]; 1370 SCHAR noisePrevTemp[MAX_NUM_CHANNELS][MAX_NUM_NOISE_COEFFS]; 1371 INT upDateNrgTemp[MAX_NUM_CHANNELS]; 1372 INT upDateNoiseTemp[MAX_NUM_CHANNELS]; 1373 INT domainVecTemp[MAX_NUM_CHANNELS][MAX_ENVELOPES]; 1374 INT domainVecNoiseTemp[MAX_NUM_CHANNELS][MAX_ENVELOPES]; 1375 1376 INT tempFlagRight = 0; 1377 INT tempFlagLeft = 0; 1378 1379 /* 1380 Store previous values, in order to be able to "undo" what is being done. 1381 */ 1382 1383 for(ch = 0; ch < nChannels;ch++){ 1384 FDKmemcpy (sfbNrgPrevTemp[ch], h_envChan[ch]->sbrCodeEnvelope.sfb_nrg_prev, 1385 MAX_FREQ_COEFFS * sizeof (SCHAR)); 1386 1387 FDKmemcpy (noisePrevTemp[ch], h_envChan[ch]->sbrCodeNoiseFloor.sfb_nrg_prev, 1388 MAX_NUM_NOISE_COEFFS * sizeof (SCHAR)); 1389 1390 upDateNrgTemp[ch] = h_envChan[ch]->sbrCodeEnvelope.upDate; 1391 upDateNoiseTemp[ch] = h_envChan[ch]->sbrCodeNoiseFloor.upDate; 1392 1393 /* 1394 forbid time coding in the first envelope in case of a different 1395 previous stereomode 1396 */ 1397 if(sbrHeaderData->prev_coupling){ 1398 h_envChan[ch]->sbrCodeEnvelope.upDate = 0; 1399 h_envChan[ch]->sbrCodeNoiseFloor.upDate = 0; 1400 } 1401 } /* ch */ 1402 1403 1404 /* 1405 Code ordinary Left/Right stereo 1406 */ 1407 FDKsbrEnc_codeEnvelope (eData[0].sfb_nrg, eData[0].frame_info->freqRes, 1408 &h_envChan[0]->sbrCodeEnvelope, 1409 h_envChan[0]->encEnvData.domain_vec, 0, 1410 eData[0].frame_info->nEnvelopes, 0, 1411 sbrBitstreamData->HeaderActive); 1412 FDKsbrEnc_codeEnvelope (eData[1].sfb_nrg, eData[1].frame_info->freqRes, 1413 &h_envChan[1]->sbrCodeEnvelope, 1414 h_envChan[1]->encEnvData.domain_vec, 0, 1415 eData[1].frame_info->nEnvelopes, 0, 1416 sbrBitstreamData->HeaderActive); 1417 1418 c = 0; 1419 for (i = 0; i < eData[0].nEnvelopes; i++) { 1420 for (j = 0; j < h_envChan[0]->encEnvData.noScfBands[i]; j++) 1421 { 1422 h_envChan[0]->encEnvData.ienvelope[i][j] = eData[0].sfb_nrg[c]; 1423 h_envChan[1]->encEnvData.ienvelope[i][j] = eData[1].sfb_nrg[c]; 1424 c++; 1425 } 1426 } 1427 1428 1429 1430 FDKsbrEnc_codeEnvelope (eData[0].noise_level, fData->res, 1431 &h_envChan[0]->sbrCodeNoiseFloor, 1432 h_envChan[0]->encEnvData.domain_vec_noise, 0, 1433 (eData[0].frame_info->nEnvelopes > 1 ? 2 : 1), 0, 1434 sbrBitstreamData->HeaderActive); 1435 1436 1437 for (i = 0; i < MAX_NUM_NOISE_VALUES; i++) 1438 h_envChan[0]->encEnvData.sbr_noise_levels[i] = eData[0].noise_level[i]; 1439 1440 1441 FDKsbrEnc_codeEnvelope (eData[1].noise_level, fData->res, 1442 &h_envChan[1]->sbrCodeNoiseFloor, 1443 h_envChan[1]->encEnvData.domain_vec_noise, 0, 1444 (eData[1].frame_info->nEnvelopes > 1 ? 2 : 1), 0, 1445 sbrBitstreamData->HeaderActive); 1446 1447 for (i = 0; i < MAX_NUM_NOISE_VALUES; i++) 1448 h_envChan[1]->encEnvData.sbr_noise_levels[i] = eData[1].noise_level[i]; 1449 1450 1451 sbrHeaderData->coupling = 0; 1452 h_envChan[0]->encEnvData.balance = 0; 1453 h_envChan[1]->encEnvData.balance = 0; 1454 1455 payloadbitsLR = FDKsbrEnc_CountSbrChannelPairElement (sbrHeaderData, 1456 hParametricStereo, 1457 sbrBitstreamData, 1458 &h_envChan[0]->encEnvData, 1459 &h_envChan[1]->encEnvData, 1460 hCmonData, 1461 h_con->sbrSyntaxFlags); 1462 1463 /* 1464 swap saved stored with current values 1465 */ 1466 for(ch = 0; ch < nChannels;ch++){ 1467 INT itmp; 1468 for(i=0;i<MAX_FREQ_COEFFS;i++){ 1469 /* 1470 swap sfb energies 1471 */ 1472 itmp = h_envChan[ch]->sbrCodeEnvelope.sfb_nrg_prev[i]; 1473 h_envChan[ch]->sbrCodeEnvelope.sfb_nrg_prev[i]=sfbNrgPrevTemp[ch][i]; 1474 sfbNrgPrevTemp[ch][i]=itmp; 1475 } 1476 for(i=0;i<MAX_NUM_NOISE_COEFFS;i++){ 1477 /* 1478 swap noise energies 1479 */ 1480 itmp = h_envChan[ch]->sbrCodeNoiseFloor.sfb_nrg_prev[i]; 1481 h_envChan[ch]->sbrCodeNoiseFloor.sfb_nrg_prev[i]=noisePrevTemp[ch][i]; 1482 noisePrevTemp[ch][i]=itmp; 1483 } 1484 /* swap update flags */ 1485 itmp = h_envChan[ch]->sbrCodeEnvelope.upDate; 1486 h_envChan[ch]->sbrCodeEnvelope.upDate=upDateNrgTemp[ch]; 1487 upDateNrgTemp[ch] = itmp; 1488 1489 itmp = h_envChan[ch]->sbrCodeNoiseFloor.upDate; 1490 h_envChan[ch]->sbrCodeNoiseFloor.upDate=upDateNoiseTemp[ch]; 1491 upDateNoiseTemp[ch]=itmp; 1492 1493 /* 1494 save domain vecs 1495 */ 1496 FDKmemcpy(domainVecTemp[ch],h_envChan[ch]->encEnvData.domain_vec,sizeof(INT)*MAX_ENVELOPES); 1497 FDKmemcpy(domainVecNoiseTemp[ch],h_envChan[ch]->encEnvData.domain_vec_noise,sizeof(INT)*MAX_ENVELOPES); 1498 1499 /* 1500 forbid time coding in the first envelope in case of a different 1501 previous stereomode 1502 */ 1503 1504 if(!sbrHeaderData->prev_coupling){ 1505 h_envChan[ch]->sbrCodeEnvelope.upDate = 0; 1506 h_envChan[ch]->sbrCodeNoiseFloor.upDate = 0; 1507 } 1508 } /* ch */ 1509 1510 1511 /* 1512 Coupling 1513 */ 1514 1515 FDKsbrEnc_codeEnvelope (eData[0].sfb_nrg_coupling, eData[0].frame_info->freqRes, 1516 &h_envChan[0]->sbrCodeEnvelope, 1517 h_envChan[0]->encEnvData.domain_vec, 1, 1518 eData[0].frame_info->nEnvelopes, 0, 1519 sbrBitstreamData->HeaderActive); 1520 1521 FDKsbrEnc_codeEnvelope (eData[1].sfb_nrg_coupling, eData[1].frame_info->freqRes, 1522 &h_envChan[1]->sbrCodeEnvelope, 1523 h_envChan[1]->encEnvData.domain_vec, 1, 1524 eData[1].frame_info->nEnvelopes, 1, 1525 sbrBitstreamData->HeaderActive); 1526 1527 1528 c = 0; 1529 for (i = 0; i < eData[0].nEnvelopes; i++) { 1530 for (j = 0; j < h_envChan[0]->encEnvData.noScfBands[i]; j++) { 1531 h_envChan[0]->encEnvData.ienvelope[i][j] = eData[0].sfb_nrg_coupling[c]; 1532 h_envChan[1]->encEnvData.ienvelope[i][j] = eData[1].sfb_nrg_coupling[c]; 1533 c++; 1534 } 1535 } 1536 1537 FDKsbrEnc_codeEnvelope (eData[0].noise_level_coupling, fData->res, 1538 &h_envChan[0]->sbrCodeNoiseFloor, 1539 h_envChan[0]->encEnvData.domain_vec_noise, 1, 1540 (eData[0].frame_info->nEnvelopes > 1 ? 2 : 1), 0, 1541 sbrBitstreamData->HeaderActive); 1542 1543 for (i = 0; i < MAX_NUM_NOISE_VALUES; i++) 1544 h_envChan[0]->encEnvData.sbr_noise_levels[i] = eData[0].noise_level_coupling[i]; 1545 1546 1547 FDKsbrEnc_codeEnvelope (eData[1].noise_level_coupling, fData->res, 1548 &h_envChan[1]->sbrCodeNoiseFloor, 1549 h_envChan[1]->encEnvData.domain_vec_noise, 1, 1550 (eData[1].frame_info->nEnvelopes > 1 ? 2 : 1), 1, 1551 sbrBitstreamData->HeaderActive); 1552 1553 for (i = 0; i < MAX_NUM_NOISE_VALUES; i++) 1554 h_envChan[1]->encEnvData.sbr_noise_levels[i] = eData[1].noise_level_coupling[i]; 1555 1556 sbrHeaderData->coupling = 1; 1557 1558 h_envChan[0]->encEnvData.balance = 0; 1559 h_envChan[1]->encEnvData.balance = 1; 1560 1561 tempFlagLeft = h_envChan[0]->encEnvData.addHarmonicFlag; 1562 tempFlagRight = h_envChan[1]->encEnvData.addHarmonicFlag; 1563 1564 payloadbitsCOUPLING = 1565 FDKsbrEnc_CountSbrChannelPairElement (sbrHeaderData, 1566 hParametricStereo, 1567 sbrBitstreamData, 1568 &h_envChan[0]->encEnvData, 1569 &h_envChan[1]->encEnvData, 1570 hCmonData, 1571 h_con->sbrSyntaxFlags); 1572 1573 1574 h_envChan[0]->encEnvData.addHarmonicFlag = tempFlagLeft; 1575 h_envChan[1]->encEnvData.addHarmonicFlag = tempFlagRight; 1576 1577 if (payloadbitsCOUPLING < payloadbitsLR) { 1578 1579 /* 1580 copy coded coupling envelope and noise data to l/r 1581 */ 1582 for(ch = 0; ch < nChannels;ch++){ 1583 SBR_ENV_TEMP_DATA *ed = &eData[ch]; 1584 FDKmemcpy (ed->sfb_nrg, ed->sfb_nrg_coupling, 1585 MAX_NUM_ENVELOPE_VALUES * sizeof (SCHAR)); 1586 FDKmemcpy (ed->noise_level, ed->noise_level_coupling, 1587 MAX_NUM_NOISE_VALUES * sizeof (SCHAR)); 1588 } 1589 1590 sbrHeaderData->coupling = 1; 1591 h_envChan[0]->encEnvData.balance = 0; 1592 h_envChan[1]->encEnvData.balance = 1; 1593 } 1594 else{ 1595 /* 1596 restore saved l/r items 1597 */ 1598 for(ch = 0; ch < nChannels;ch++){ 1599 1600 FDKmemcpy (h_envChan[ch]->sbrCodeEnvelope.sfb_nrg_prev, 1601 sfbNrgPrevTemp[ch], MAX_FREQ_COEFFS * sizeof (SCHAR)); 1602 1603 h_envChan[ch]->sbrCodeEnvelope.upDate = upDateNrgTemp[ch]; 1604 1605 FDKmemcpy (h_envChan[ch]->sbrCodeNoiseFloor.sfb_nrg_prev, 1606 noisePrevTemp[ch], MAX_NUM_NOISE_COEFFS * sizeof (SCHAR)); 1607 1608 FDKmemcpy (h_envChan[ch]->encEnvData.domain_vec,domainVecTemp[ch],sizeof(INT)*MAX_ENVELOPES); 1609 FDKmemcpy (h_envChan[ch]->encEnvData.domain_vec_noise,domainVecNoiseTemp[ch],sizeof(INT)*MAX_ENVELOPES); 1610 1611 h_envChan[ch]->sbrCodeNoiseFloor.upDate = upDateNoiseTemp[ch]; 1612 } 1613 1614 sbrHeaderData->coupling = 0; 1615 h_envChan[0]->encEnvData.balance = 0; 1616 h_envChan[1]->encEnvData.balance = 0; 1617 } 1618 } 1619 break; 1620 } /* switch */ 1621 1622 1623 /* tell the envelope encoders how long it has been, since we last sent 1624 a frame starting with a dF-coded envelope */ 1625 if (stereoMode == SBR_MONO ) { 1626 if (h_envChan[0]->encEnvData.domain_vec[0] == TIME) 1627 h_envChan[0]->sbrCodeEnvelope.dF_edge_incr_fac++; 1628 else 1629 h_envChan[0]->sbrCodeEnvelope.dF_edge_incr_fac = 0; 1630 } 1631 else { 1632 if (h_envChan[0]->encEnvData.domain_vec[0] == TIME || 1633 h_envChan[1]->encEnvData.domain_vec[0] == TIME) { 1634 h_envChan[0]->sbrCodeEnvelope.dF_edge_incr_fac++; 1635 h_envChan[1]->sbrCodeEnvelope.dF_edge_incr_fac++; 1636 } 1637 else { 1638 h_envChan[0]->sbrCodeEnvelope.dF_edge_incr_fac = 0; 1639 h_envChan[1]->sbrCodeEnvelope.dF_edge_incr_fac = 0; 1640 } 1641 } 1642 1643 /* 1644 Send the encoded data to the bitstream 1645 */ 1646 for(ch = 0; ch < nChannels;ch++){ 1647 SBR_ENV_TEMP_DATA *ed = &eData[ch]; 1648 c = 0; 1649 for (i = 0; i < ed->nEnvelopes; i++) { 1650 for (j = 0; j < h_envChan[ch]->encEnvData.noScfBands[i]; j++) { 1651 h_envChan[ch]->encEnvData.ienvelope[i][j] = ed->sfb_nrg[c]; 1652 1653 c++; 1654 } 1655 } 1656 for (i = 0; i < MAX_NUM_NOISE_VALUES; i++){ 1657 h_envChan[ch]->encEnvData.sbr_noise_levels[i] = ed->noise_level[i]; 1658 } 1659 }/* ch */ 1660 1661 1662 /* 1663 Write bitstream 1664 */ 1665 if (nChannels == 2) { 1666 FDKsbrEnc_WriteEnvChannelPairElement(sbrHeaderData, 1667 hParametricStereo, 1668 sbrBitstreamData, 1669 &h_envChan[0]->encEnvData, 1670 &h_envChan[1]->encEnvData, 1671 hCmonData, 1672 h_con->sbrSyntaxFlags); 1673 } 1674 else { 1675 FDKsbrEnc_WriteEnvSingleChannelElement(sbrHeaderData, 1676 hParametricStereo, 1677 sbrBitstreamData, 1678 &h_envChan[0]->encEnvData, 1679 hCmonData, 1680 h_con->sbrSyntaxFlags); 1681 } 1682 1683 /* 1684 * Update buffers. 1685 */ 1686 for (ch=0; ch<nChannels; ch++) 1687 { 1688 int YBufferLength = h_envChan[ch]->sbrExtractEnvelope.no_cols >> h_envChan[ch]->sbrExtractEnvelope.YBufferSzShift; 1689 for (i = 0; i < h_envChan[ch]->sbrExtractEnvelope.YBufferWriteOffset; i++) { 1690 FDKmemcpy(h_envChan[ch]->sbrExtractEnvelope.YBuffer[i], 1691 h_envChan[ch]->sbrExtractEnvelope.YBuffer[i + YBufferLength], 1692 sizeof(FIXP_DBL)*QMF_CHANNELS); 1693 } 1694 h_envChan[ch]->sbrExtractEnvelope.YBufferScale[0] = h_envChan[ch]->sbrExtractEnvelope.YBufferScale[1]; 1695 } 1696 1697 sbrHeaderData->prev_coupling = sbrHeaderData->coupling; 1698 } 1699 1700 /***************************************************************************/ 1701 /*! 1702 1703 \brief creates an envelope extractor handle 1704 1705 \return error status 1706 1707 ****************************************************************************/ 1708 INT 1709 FDKsbrEnc_CreateExtractSbrEnvelope (HANDLE_SBR_EXTRACT_ENVELOPE hSbrCut, 1710 INT channel 1711 ,INT chInEl 1712 ,UCHAR* dynamic_RAM 1713 ) 1714 { 1715 INT i; 1716 FIXP_DBL* YBuffer = GetRam_Sbr_envYBuffer(channel); 1717 1718 FDKmemclear(hSbrCut,sizeof(SBR_EXTRACT_ENVELOPE)); 1719 hSbrCut->p_YBuffer = YBuffer; 1720 1721 1722 for (i = 0; i < (QMF_MAX_TIME_SLOTS>>1); i++) { 1723 hSbrCut->YBuffer[i] = YBuffer + (i*QMF_CHANNELS); 1724 } 1725 FIXP_DBL *YBufferDyn = GetRam_Sbr_envYBuffer(chInEl, dynamic_RAM); 1726 INT n=0; 1727 for (; i < QMF_MAX_TIME_SLOTS; i++,n++) { 1728 hSbrCut->YBuffer[i] = YBufferDyn + (n*QMF_CHANNELS); 1729 } 1730 1731 FIXP_DBL* rBuffer = GetRam_Sbr_envRBuffer(0, dynamic_RAM); 1732 FIXP_DBL* iBuffer = GetRam_Sbr_envIBuffer(0, dynamic_RAM); 1733 1734 for (i = 0; i < QMF_MAX_TIME_SLOTS; i++) { 1735 hSbrCut->rBuffer[i] = rBuffer + (i*QMF_CHANNELS); 1736 hSbrCut->iBuffer[i] = iBuffer + (i*QMF_CHANNELS); 1737 } 1738 1739 return 0; 1740 } 1741 1742 1743 /***************************************************************************/ 1744 /*! 1745 1746 \brief Initialize an envelope extractor instance. 1747 1748 \return error status 1749 1750 ****************************************************************************/ 1751 INT 1752 FDKsbrEnc_InitExtractSbrEnvelope (HANDLE_SBR_EXTRACT_ENVELOPE hSbrCut, 1753 int no_cols, 1754 int no_rows, 1755 int start_index, 1756 int time_slots, 1757 int time_step, 1758 int tran_off, 1759 ULONG statesInitFlag 1760 ,int chInEl 1761 ,UCHAR* dynamic_RAM 1762 ,UINT sbrSyntaxFlags 1763 ) 1764 { 1765 int YBufferLength, rBufferLength; 1766 int i; 1767 1768 if (sbrSyntaxFlags & SBR_SYNTAX_LOW_DELAY) { 1769 int off = TRANSIENT_OFFSET_LD; 1770 #ifndef FULL_DELAY 1771 hSbrCut->YBufferWriteOffset = (no_cols>>1)+off*time_step; 1772 #else 1773 hSbrCut->YBufferWriteOffset = no_cols+off*time_step; 1774 #endif 1775 } else 1776 { 1777 hSbrCut->YBufferWriteOffset = tran_off*time_step; 1778 } 1779 hSbrCut->rBufferReadOffset = 0; 1780 1781 1782 YBufferLength = hSbrCut->YBufferWriteOffset + no_cols; 1783 rBufferLength = no_cols; 1784 1785 hSbrCut->pre_transient_info[0] = 0; 1786 hSbrCut->pre_transient_info[1] = 0; 1787 1788 1789 hSbrCut->no_cols = no_cols; 1790 hSbrCut->no_rows = no_rows; 1791 hSbrCut->start_index = start_index; 1792 1793 hSbrCut->time_slots = time_slots; 1794 hSbrCut->time_step = time_step; 1795 1796 FDK_ASSERT(no_rows <= QMF_CHANNELS); 1797 1798 /* Use half the Energy values if time step is 2 or greater */ 1799 if (time_step >= 2) 1800 hSbrCut->YBufferSzShift = 1; 1801 else 1802 hSbrCut->YBufferSzShift = 0; 1803 1804 YBufferLength >>= hSbrCut->YBufferSzShift; 1805 hSbrCut->YBufferWriteOffset >>= hSbrCut->YBufferSzShift; 1806 1807 FDK_ASSERT(YBufferLength<=QMF_MAX_TIME_SLOTS); 1808 1809 FIXP_DBL *YBufferDyn = GetRam_Sbr_envYBuffer(chInEl, dynamic_RAM); 1810 INT n=0; 1811 for (i=(QMF_MAX_TIME_SLOTS>>1); i < QMF_MAX_TIME_SLOTS; i++,n++) { 1812 hSbrCut->YBuffer[i] = YBufferDyn + (n*QMF_CHANNELS); 1813 } 1814 1815 if(statesInitFlag) { 1816 for (i=0; i<YBufferLength; i++) { 1817 FDKmemclear( hSbrCut->YBuffer[i],QMF_CHANNELS*sizeof(FIXP_DBL)); 1818 } 1819 } 1820 1821 for (i = 0; i < rBufferLength; i++) { 1822 FDKmemclear( hSbrCut->rBuffer[i],QMF_CHANNELS*sizeof(FIXP_DBL)); 1823 FDKmemclear( hSbrCut->iBuffer[i],QMF_CHANNELS*sizeof(FIXP_DBL)); 1824 } 1825 1826 FDKmemclear (hSbrCut->envelopeCompensation,sizeof(UCHAR)*MAX_FREQ_COEFFS); 1827 1828 if(statesInitFlag) { 1829 hSbrCut->YBufferScale[0] = hSbrCut->YBufferScale[1] = FRACT_BITS-1; 1830 } 1831 1832 return (0); 1833 } 1834 1835 1836 1837 1838 /***************************************************************************/ 1839 /*! 1840 1841 \brief deinitializes an envelope extractor handle 1842 1843 \return void 1844 1845 ****************************************************************************/ 1846 1847 void 1848 FDKsbrEnc_deleteExtractSbrEnvelope (HANDLE_SBR_EXTRACT_ENVELOPE hSbrCut) 1849 { 1850 1851 if (hSbrCut) { 1852 FreeRam_Sbr_envYBuffer(&hSbrCut->p_YBuffer); 1853 } 1854 } 1855 1856 INT 1857 FDKsbrEnc_GetEnvEstDelay(HANDLE_SBR_EXTRACT_ENVELOPE hSbr) 1858 { 1859 return hSbr->no_rows*((hSbr->YBufferWriteOffset)*2 /* mult 2 because nrg's are grouped half */ 1860 - hSbr->rBufferReadOffset ); /* in reference hold half spec and calc nrg's on overlapped spec */ 1861 1862 } 1863 1864 1865 1866 1867