1 /* ----------------------------------------------------------------------------- 2 Software License for The Fraunhofer FDK AAC Codec Library for Android 3 4 Copyright 1995 - 2018 Fraunhofer-Gesellschaft zur Frderung der angewandten 5 Forschung e.V. All rights reserved. 6 7 1. INTRODUCTION 8 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software 9 that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding 10 scheme for digital audio. This FDK AAC Codec software is intended to be used on 11 a wide variety of Android devices. 12 13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient 14 general perceptual audio codecs. AAC-ELD is considered the best-performing 15 full-bandwidth communications codec by independent studies and is widely 16 deployed. AAC has been standardized by ISO and IEC as part of the MPEG 17 specifications. 18 19 Patent licenses for necessary patent claims for the FDK AAC Codec (including 20 those of Fraunhofer) may be obtained through Via Licensing 21 (www.vialicensing.com) or through the respective patent owners individually for 22 the purpose of encoding or decoding bit streams in products that are compliant 23 with the ISO/IEC MPEG audio standards. Please note that most manufacturers of 24 Android devices already license these patent claims through Via Licensing or 25 directly from the patent owners, and therefore FDK AAC Codec software may 26 already be covered under those patent licenses when it is used for those 27 licensed purposes only. 28 29 Commercially-licensed AAC software libraries, including floating-point versions 30 with enhanced sound quality, are also available from Fraunhofer. Users are 31 encouraged to check the Fraunhofer website for additional applications 32 information and documentation. 33 34 2. COPYRIGHT LICENSE 35 36 Redistribution and use in source and binary forms, with or without modification, 37 are permitted without payment of copyright license fees provided that you 38 satisfy the following conditions: 39 40 You must retain the complete text of this software license in redistributions of 41 the FDK AAC Codec or your modifications thereto in source code form. 42 43 You must retain the complete text of this software license in the documentation 44 and/or other materials provided with redistributions of the FDK AAC Codec or 45 your modifications thereto in binary form. You must make available free of 46 charge copies of the complete source code of the FDK AAC Codec and your 47 modifications thereto to recipients of copies in binary form. 48 49 The name of Fraunhofer may not be used to endorse or promote products derived 50 from this library without prior written permission. 51 52 You may not charge copyright license fees for anyone to use, copy or distribute 53 the FDK AAC Codec software or your modifications thereto. 54 55 Your modified versions of the FDK AAC Codec must carry prominent notices stating 56 that you changed the software and the date of any change. For modified versions 57 of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android" 58 must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK 59 AAC Codec Library for Android." 60 61 3. NO PATENT LICENSE 62 63 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without 64 limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE. 65 Fraunhofer provides no warranty of patent non-infringement with respect to this 66 software. 67 68 You may use this FDK AAC Codec software or modifications thereto only for 69 purposes that are authorized by appropriate patent licenses. 70 71 4. DISCLAIMER 72 73 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright 74 holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, 75 including but not limited to the implied warranties of merchantability and 76 fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 77 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, 78 or consequential damages, including but not limited to procurement of substitute 79 goods or services; loss of use, data, or profits, or business interruption, 80 however caused and on any theory of liability, whether in contract, strict 81 liability, or tort (including negligence), arising in any way out of the use of 82 this software, even if advised of the possibility of such damage. 83 84 5. CONTACT INFORMATION 85 86 Fraunhofer Institute for Integrated Circuits IIS 87 Attention: Audio and Multimedia Departments - FDK AAC LL 88 Am Wolfsmantel 33 89 91058 Erlangen, Germany 90 91 www.iis.fraunhofer.de/amm 92 amm-info (at) iis.fraunhofer.de 93 ----------------------------------------------------------------------------- */ 94 95 /**************************** SBR decoder library ****************************** 96 97 Author(s): 98 99 Description: 100 101 *******************************************************************************/ 102 103 /*! 104 \file 105 \brief envelope decoding 106 This module provides envelope decoding and error concealment algorithms. The 107 main entry point is decodeSbrData(). 108 109 \sa decodeSbrData(),\ref documentationOverview 110 */ 111 112 #include "env_dec.h" 113 114 #include "env_extr.h" 115 #include "transcendent.h" 116 117 #include "genericStds.h" 118 119 static void decodeEnvelope(HANDLE_SBR_HEADER_DATA hHeaderData, 120 HANDLE_SBR_FRAME_DATA h_sbr_data, 121 HANDLE_SBR_PREV_FRAME_DATA h_prev_data, 122 HANDLE_SBR_PREV_FRAME_DATA h_prev_data_otherChannel); 123 static void sbr_envelope_unmapping(HANDLE_SBR_HEADER_DATA hHeaderData, 124 HANDLE_SBR_FRAME_DATA h_data_left, 125 HANDLE_SBR_FRAME_DATA h_data_right); 126 static void requantizeEnvelopeData(HANDLE_SBR_FRAME_DATA h_sbr_data, 127 int ampResolution); 128 static void deltaToLinearPcmEnvelopeDecoding( 129 HANDLE_SBR_HEADER_DATA hHeaderData, HANDLE_SBR_FRAME_DATA h_sbr_data, 130 HANDLE_SBR_PREV_FRAME_DATA h_prev_data); 131 static void decodeNoiseFloorlevels(HANDLE_SBR_HEADER_DATA hHeaderData, 132 HANDLE_SBR_FRAME_DATA h_sbr_data, 133 HANDLE_SBR_PREV_FRAME_DATA h_prev_data); 134 static void timeCompensateFirstEnvelope(HANDLE_SBR_HEADER_DATA hHeaderData, 135 HANDLE_SBR_FRAME_DATA h_sbr_data, 136 HANDLE_SBR_PREV_FRAME_DATA h_prev_data); 137 static int checkEnvelopeData(HANDLE_SBR_HEADER_DATA hHeaderData, 138 HANDLE_SBR_FRAME_DATA h_sbr_data, 139 HANDLE_SBR_PREV_FRAME_DATA h_prev_data); 140 141 #define SBR_ENERGY_PAN_OFFSET (12 << ENV_EXP_FRACT) 142 #define SBR_MAX_ENERGY (35 << ENV_EXP_FRACT) 143 144 #define DECAY (1 << ENV_EXP_FRACT) 145 146 #if ENV_EXP_FRACT 147 #define DECAY_COUPLING \ 148 (1 << (ENV_EXP_FRACT - 1)) /*!< corresponds to a value of 0.5 */ 149 #else 150 #define DECAY_COUPLING \ 151 1 /*!< If the energy data is not shifted, use 1 instead of 0.5 */ 152 #endif 153 154 /*! 155 \brief Convert table index 156 */ 157 static int indexLow2High(int offset, /*!< mapping factor */ 158 int index, /*!< index to scalefactor band */ 159 int res) /*!< frequency resolution */ 160 { 161 if (res == 0) { 162 if (offset >= 0) { 163 if (index < offset) 164 return (index); 165 else 166 return (2 * index - offset); 167 } else { 168 offset = -offset; 169 if (index < offset) 170 return (2 * index + index); 171 else 172 return (2 * index + offset); 173 } 174 } else 175 return (index); 176 } 177 178 /*! 179 \brief Update previous envelope value for delta-coding 180 181 The current envelope values needs to be stored for delta-coding 182 in the next frame. The stored envelope is always represented with 183 the high frequency resolution. If the current envelope uses the 184 low frequency resolution, the energy value will be mapped to the 185 corresponding high-res bands. 186 */ 187 static void mapLowResEnergyVal( 188 FIXP_SGL currVal, /*!< current energy value */ 189 FIXP_SGL *prevData, /*!< pointer to previous data vector */ 190 int offset, /*!< mapping factor */ 191 int index, /*!< index to scalefactor band */ 192 int res) /*!< frequeny resolution */ 193 { 194 if (res == 0) { 195 if (offset >= 0) { 196 if (index < offset) 197 prevData[index] = currVal; 198 else { 199 prevData[2 * index - offset] = currVal; 200 prevData[2 * index + 1 - offset] = currVal; 201 } 202 } else { 203 offset = -offset; 204 if (index < offset) { 205 prevData[3 * index] = currVal; 206 prevData[3 * index + 1] = currVal; 207 prevData[3 * index + 2] = currVal; 208 } else { 209 prevData[2 * index + offset] = currVal; 210 prevData[2 * index + 1 + offset] = currVal; 211 } 212 } 213 } else 214 prevData[index] = currVal; 215 } 216 217 /*! 218 \brief Convert raw envelope and noisefloor data to energy levels 219 220 This function is being called by sbrDecoder_ParseElement() and provides two 221 important algorithms: 222 223 First the function decodes envelopes and noise floor levels as described in 224 requantizeEnvelopeData() and sbr_envelope_unmapping(). The function also 225 implements concealment algorithms in case there are errors within the sbr 226 data. For both operations fractional arithmetic is used. Therefore you might 227 encounter different output values on your target system compared to the 228 reference implementation. 229 */ 230 void decodeSbrData( 231 HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */ 232 HANDLE_SBR_FRAME_DATA 233 h_data_left, /*!< pointer to left channel frame data */ 234 HANDLE_SBR_PREV_FRAME_DATA 235 h_prev_data_left, /*!< pointer to left channel previous frame data */ 236 HANDLE_SBR_FRAME_DATA 237 h_data_right, /*!< pointer to right channel frame data */ 238 HANDLE_SBR_PREV_FRAME_DATA 239 h_prev_data_right) /*!< pointer to right channel previous frame data */ 240 { 241 FIXP_SGL tempSfbNrgPrev[MAX_FREQ_COEFFS]; 242 int errLeft; 243 244 /* Save previous energy values to be able to reuse them later for concealment. 245 */ 246 FDKmemcpy(tempSfbNrgPrev, h_prev_data_left->sfb_nrg_prev, 247 MAX_FREQ_COEFFS * sizeof(FIXP_SGL)); 248 249 if (hHeaderData->frameErrorFlag || hHeaderData->bs_info.pvc_mode == 0) { 250 decodeEnvelope(hHeaderData, h_data_left, h_prev_data_left, 251 h_prev_data_right); 252 } else { 253 FDK_ASSERT(h_data_right == NULL); 254 } 255 decodeNoiseFloorlevels(hHeaderData, h_data_left, h_prev_data_left); 256 257 if (h_data_right != NULL) { 258 errLeft = hHeaderData->frameErrorFlag; 259 decodeEnvelope(hHeaderData, h_data_right, h_prev_data_right, 260 h_prev_data_left); 261 decodeNoiseFloorlevels(hHeaderData, h_data_right, h_prev_data_right); 262 263 if (!errLeft && hHeaderData->frameErrorFlag) { 264 /* If an error occurs in the right channel where the left channel seemed 265 ok, we apply concealment also on the left channel. This ensures that 266 the coupling modes of both channels match and that we have the same 267 number of envelopes in coupling mode. However, as the left channel has 268 already been processed before, the resulting energy levels are not the 269 same as if the left channel had been concealed during the first call of 270 decodeEnvelope(). 271 */ 272 /* Restore previous energy values for concealment, because the values have 273 been overwritten by the first call of decodeEnvelope(). */ 274 FDKmemcpy(h_prev_data_left->sfb_nrg_prev, tempSfbNrgPrev, 275 MAX_FREQ_COEFFS * sizeof(FIXP_SGL)); 276 /* Do concealment */ 277 decodeEnvelope(hHeaderData, h_data_left, h_prev_data_left, 278 h_prev_data_right); 279 } 280 281 if (h_data_left->coupling) { 282 sbr_envelope_unmapping(hHeaderData, h_data_left, h_data_right); 283 } 284 } 285 286 /* Display the data for debugging: */ 287 } 288 289 /*! 290 \brief Convert from coupled channels to independent L/R data 291 */ 292 static void sbr_envelope_unmapping( 293 HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */ 294 HANDLE_SBR_FRAME_DATA h_data_left, /*!< pointer to left channel */ 295 HANDLE_SBR_FRAME_DATA h_data_right) /*!< pointer to right channel */ 296 { 297 int i; 298 FIXP_SGL tempL_m, tempR_m, tempRplus1_m, newL_m, newR_m; 299 SCHAR tempL_e, tempR_e, tempRplus1_e, newL_e, newR_e; 300 301 /* 1. Unmap (already dequantized) coupled envelope energies */ 302 303 for (i = 0; i < h_data_left->nScaleFactors; i++) { 304 tempR_m = (FIXP_SGL)((LONG)h_data_right->iEnvelope[i] & MASK_M); 305 tempR_e = (SCHAR)((LONG)h_data_right->iEnvelope[i] & MASK_E); 306 307 tempR_e -= (18 + NRG_EXP_OFFSET); /* -18 = ld(UNMAPPING_SCALE / 308 h_data_right->nChannels) */ 309 tempL_m = (FIXP_SGL)((LONG)h_data_left->iEnvelope[i] & MASK_M); 310 tempL_e = (SCHAR)((LONG)h_data_left->iEnvelope[i] & MASK_E); 311 312 tempL_e -= NRG_EXP_OFFSET; 313 314 /* Calculate tempRight+1 */ 315 FDK_add_MantExp(tempR_m, tempR_e, FL2FXCONST_SGL(0.5f), 1, /* 1.0 */ 316 &tempRplus1_m, &tempRplus1_e); 317 318 FDK_divide_MantExp(tempL_m, tempL_e + 1, /* 2 * tempLeft */ 319 tempRplus1_m, tempRplus1_e, &newR_m, &newR_e); 320 321 if (newR_m >= ((FIXP_SGL)MAXVAL_SGL - ROUNDING)) { 322 newR_m >>= 1; 323 newR_e += 1; 324 } 325 326 newL_m = FX_DBL2FX_SGL(fMult(tempR_m, newR_m)); 327 newL_e = tempR_e + newR_e; 328 329 h_data_right->iEnvelope[i] = 330 ((FIXP_SGL)((SHORT)(FIXP_SGL)(newR_m + ROUNDING) & MASK_M)) + 331 (FIXP_SGL)((SHORT)(FIXP_SGL)(newR_e + NRG_EXP_OFFSET) & MASK_E); 332 h_data_left->iEnvelope[i] = 333 ((FIXP_SGL)((SHORT)(FIXP_SGL)(newL_m + ROUNDING) & MASK_M)) + 334 (FIXP_SGL)((SHORT)(FIXP_SGL)(newL_e + NRG_EXP_OFFSET) & MASK_E); 335 } 336 337 /* 2. Dequantize and unmap coupled noise floor levels */ 338 339 for (i = 0; i < hHeaderData->freqBandData.nNfb * 340 h_data_left->frameInfo.nNoiseEnvelopes; 341 i++) { 342 tempL_e = (SCHAR)(6 - (LONG)h_data_left->sbrNoiseFloorLevel[i]); 343 tempR_e = (SCHAR)((LONG)h_data_right->sbrNoiseFloorLevel[i] - 344 12) /*SBR_ENERGY_PAN_OFFSET*/; 345 346 /* Calculate tempR+1 */ 347 FDK_add_MantExp(FL2FXCONST_SGL(0.5f), 1 + tempR_e, /* tempR */ 348 FL2FXCONST_SGL(0.5f), 1, /* 1.0 */ 349 &tempRplus1_m, &tempRplus1_e); 350 351 /* Calculate 2*tempLeft/(tempR+1) */ 352 FDK_divide_MantExp(FL2FXCONST_SGL(0.5f), tempL_e + 2, /* 2 * tempLeft */ 353 tempRplus1_m, tempRplus1_e, &newR_m, &newR_e); 354 355 /* if (newR_m >= ((FIXP_SGL)MAXVAL_SGL - ROUNDING)) { 356 newR_m >>= 1; 357 newR_e += 1; 358 } */ 359 360 /* L = tempR * R */ 361 newL_m = newR_m; 362 newL_e = newR_e + tempR_e; 363 h_data_right->sbrNoiseFloorLevel[i] = 364 ((FIXP_SGL)((SHORT)(FIXP_SGL)(newR_m + ROUNDING) & MASK_M)) + 365 (FIXP_SGL)((SHORT)(FIXP_SGL)(newR_e + NOISE_EXP_OFFSET) & MASK_E); 366 h_data_left->sbrNoiseFloorLevel[i] = 367 ((FIXP_SGL)((SHORT)(FIXP_SGL)(newL_m + ROUNDING) & MASK_M)) + 368 (FIXP_SGL)((SHORT)(FIXP_SGL)(newL_e + NOISE_EXP_OFFSET) & MASK_E); 369 } 370 } 371 372 /*! 373 \brief Simple alternative to the real SBR concealment 374 375 If the real frameInfo is not available due to a frame loss, a replacement will 376 be constructed with 1 envelope spanning the whole frame (FIX-FIX). 377 The delta-coded energies are set to negative values, resulting in a fade-down. 378 In case of coupling, the balance-channel will move towards the center. 379 */ 380 static void leanSbrConcealment( 381 HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */ 382 HANDLE_SBR_FRAME_DATA h_sbr_data, /*!< pointer to current data */ 383 HANDLE_SBR_PREV_FRAME_DATA h_prev_data /*!< pointer to data of last frame */ 384 ) { 385 FIXP_SGL target; /* targeted level for sfb_nrg_prev during fade-down */ 386 FIXP_SGL step; /* speed of fade */ 387 int i; 388 389 int currentStartPos = 390 fMax(0, h_prev_data->stopPos - hHeaderData->numberTimeSlots); 391 int currentStopPos = hHeaderData->numberTimeSlots; 392 393 /* Use some settings of the previous frame */ 394 h_sbr_data->ampResolutionCurrentFrame = h_prev_data->ampRes; 395 h_sbr_data->coupling = h_prev_data->coupling; 396 for (i = 0; i < MAX_INVF_BANDS; i++) 397 h_sbr_data->sbr_invf_mode[i] = h_prev_data->sbr_invf_mode[i]; 398 399 /* Generate concealing control data */ 400 401 h_sbr_data->frameInfo.nEnvelopes = 1; 402 h_sbr_data->frameInfo.borders[0] = currentStartPos; 403 h_sbr_data->frameInfo.borders[1] = currentStopPos; 404 h_sbr_data->frameInfo.freqRes[0] = 1; 405 h_sbr_data->frameInfo.tranEnv = -1; /* no transient */ 406 h_sbr_data->frameInfo.nNoiseEnvelopes = 1; 407 h_sbr_data->frameInfo.bordersNoise[0] = currentStartPos; 408 h_sbr_data->frameInfo.bordersNoise[1] = currentStopPos; 409 410 h_sbr_data->nScaleFactors = hHeaderData->freqBandData.nSfb[1]; 411 412 /* Generate fake envelope data */ 413 414 h_sbr_data->domain_vec[0] = 1; 415 416 if (h_sbr_data->coupling == COUPLING_BAL) { 417 target = (FIXP_SGL)SBR_ENERGY_PAN_OFFSET; 418 step = (FIXP_SGL)DECAY_COUPLING; 419 } else { 420 target = FL2FXCONST_SGL(0.0f); 421 step = (FIXP_SGL)DECAY; 422 } 423 if (hHeaderData->bs_info.ampResolution == 0) { 424 target <<= 1; 425 step <<= 1; 426 } 427 428 for (i = 0; i < h_sbr_data->nScaleFactors; i++) { 429 if (h_prev_data->sfb_nrg_prev[i] > target) 430 h_sbr_data->iEnvelope[i] = -step; 431 else 432 h_sbr_data->iEnvelope[i] = step; 433 } 434 435 /* Noisefloor levels are always cleared ... */ 436 437 h_sbr_data->domain_vec_noise[0] = 1; 438 for (i = 0; i < hHeaderData->freqBandData.nNfb; i++) 439 h_sbr_data->sbrNoiseFloorLevel[i] = FL2FXCONST_SGL(0.0f); 440 441 /* ... and so are the sines */ 442 FDKmemclear(h_sbr_data->addHarmonics, 443 sizeof(ULONG) * ADD_HARMONICS_FLAGS_SIZE); 444 } 445 446 /*! 447 \brief Build reference energies and noise levels from bitstream elements 448 */ 449 static void decodeEnvelope( 450 HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */ 451 HANDLE_SBR_FRAME_DATA h_sbr_data, /*!< pointer to current data */ 452 HANDLE_SBR_PREV_FRAME_DATA 453 h_prev_data, /*!< pointer to data of last frame */ 454 HANDLE_SBR_PREV_FRAME_DATA 455 otherChannel /*!< other channel's last frame data */ 456 ) { 457 int i; 458 int fFrameError = hHeaderData->frameErrorFlag; 459 FIXP_SGL tempSfbNrgPrev[MAX_FREQ_COEFFS]; 460 461 if (!fFrameError) { 462 /* 463 To avoid distortions after bad frames, set the error flag if delta coding 464 in time occurs. However, SBR can take a little longer to come up again. 465 */ 466 if (h_prev_data->frameErrorFlag) { 467 if (h_sbr_data->domain_vec[0] != 0) { 468 fFrameError = 1; 469 } 470 } else { 471 /* Check that the previous stop position and the current start position 472 match. (Could be done in checkFrameInfo(), but the previous frame data 473 is not available there) */ 474 if (h_sbr_data->frameInfo.borders[0] != 475 h_prev_data->stopPos - hHeaderData->numberTimeSlots) { 476 /* Both the previous as well as the current frame are flagged to be ok, 477 * but they do not match! */ 478 if (h_sbr_data->domain_vec[0] == 1) { 479 /* Prefer concealment over delta-time coding between the mismatching 480 * frames */ 481 fFrameError = 1; 482 } else { 483 /* Close the gap in time by triggering timeCompensateFirstEnvelope() 484 */ 485 fFrameError = 1; 486 } 487 } 488 } 489 } 490 491 if (fFrameError) /* Error is detected */ 492 { 493 leanSbrConcealment(hHeaderData, h_sbr_data, h_prev_data); 494 495 /* decode the envelope data to linear PCM */ 496 deltaToLinearPcmEnvelopeDecoding(hHeaderData, h_sbr_data, h_prev_data); 497 } else /*Do a temporary dummy decoding and check that the envelope values are 498 within limits */ 499 { 500 if (h_prev_data->frameErrorFlag) { 501 timeCompensateFirstEnvelope(hHeaderData, h_sbr_data, h_prev_data); 502 if (h_sbr_data->coupling != h_prev_data->coupling) { 503 /* 504 Coupling mode has changed during concealment. 505 The stored energy levels need to be converted. 506 */ 507 for (i = 0; i < hHeaderData->freqBandData.nSfb[1]; i++) { 508 /* Former Level-Channel will be used for both channels */ 509 if (h_prev_data->coupling == COUPLING_BAL) 510 h_prev_data->sfb_nrg_prev[i] = otherChannel->sfb_nrg_prev[i]; 511 /* Former L/R will be combined as the new Level-Channel */ 512 else if (h_sbr_data->coupling == COUPLING_LEVEL) 513 h_prev_data->sfb_nrg_prev[i] = (h_prev_data->sfb_nrg_prev[i] + 514 otherChannel->sfb_nrg_prev[i]) >> 515 1; 516 else if (h_sbr_data->coupling == COUPLING_BAL) 517 h_prev_data->sfb_nrg_prev[i] = (FIXP_SGL)SBR_ENERGY_PAN_OFFSET; 518 } 519 } 520 } 521 FDKmemcpy(tempSfbNrgPrev, h_prev_data->sfb_nrg_prev, 522 MAX_FREQ_COEFFS * sizeof(FIXP_SGL)); 523 524 deltaToLinearPcmEnvelopeDecoding(hHeaderData, h_sbr_data, h_prev_data); 525 526 fFrameError = checkEnvelopeData(hHeaderData, h_sbr_data, h_prev_data); 527 528 if (fFrameError) { 529 hHeaderData->frameErrorFlag = 1; 530 FDKmemcpy(h_prev_data->sfb_nrg_prev, tempSfbNrgPrev, 531 MAX_FREQ_COEFFS * sizeof(FIXP_SGL)); 532 decodeEnvelope(hHeaderData, h_sbr_data, h_prev_data, otherChannel); 533 return; 534 } 535 } 536 537 requantizeEnvelopeData(h_sbr_data, h_sbr_data->ampResolutionCurrentFrame); 538 539 hHeaderData->frameErrorFlag = fFrameError; 540 } 541 542 /*! 543 \brief Verify that envelope energies are within the allowed range 544 \return 0 if all is fine, 1 if an envelope value was too high 545 */ 546 static int checkEnvelopeData( 547 HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */ 548 HANDLE_SBR_FRAME_DATA h_sbr_data, /*!< pointer to current data */ 549 HANDLE_SBR_PREV_FRAME_DATA h_prev_data /*!< pointer to data of last frame */ 550 ) { 551 FIXP_SGL *iEnvelope = h_sbr_data->iEnvelope; 552 FIXP_SGL *sfb_nrg_prev = h_prev_data->sfb_nrg_prev; 553 int i = 0, errorFlag = 0; 554 FIXP_SGL sbr_max_energy = (h_sbr_data->ampResolutionCurrentFrame == 1) 555 ? SBR_MAX_ENERGY 556 : (SBR_MAX_ENERGY << 1); 557 558 /* 559 Range check for current energies 560 */ 561 for (i = 0; i < h_sbr_data->nScaleFactors; i++) { 562 if (iEnvelope[i] > sbr_max_energy) { 563 errorFlag = 1; 564 } 565 if (iEnvelope[i] < FL2FXCONST_SGL(0.0f)) { 566 errorFlag = 1; 567 /* iEnvelope[i] = FL2FXCONST_SGL(0.0f); */ 568 } 569 } 570 571 /* 572 Range check for previous energies 573 */ 574 for (i = 0; i < hHeaderData->freqBandData.nSfb[1]; i++) { 575 sfb_nrg_prev[i] = fixMax(sfb_nrg_prev[i], FL2FXCONST_SGL(0.0f)); 576 sfb_nrg_prev[i] = fixMin(sfb_nrg_prev[i], sbr_max_energy); 577 } 578 579 return (errorFlag); 580 } 581 582 /*! 583 \brief Verify that the noise levels are within the allowed range 584 585 The function is equivalent to checkEnvelopeData(). 586 When the noise-levels are being decoded, it is already too late for 587 concealment. Therefore the noise levels are simply limited here. 588 */ 589 static void limitNoiseLevels( 590 HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */ 591 HANDLE_SBR_FRAME_DATA h_sbr_data) /*!< pointer to current data */ 592 { 593 int i; 594 int nNfb = hHeaderData->freqBandData.nNfb; 595 596 /* 597 Set range limits. The exact values depend on the coupling mode. 598 However this limitation is primarily intended to avoid unlimited 599 accumulation of the delta-coded noise levels. 600 */ 601 #define lowerLimit \ 602 ((FIXP_SGL)0) /* lowerLimit actually refers to the _highest_ noise energy */ 603 #define upperLimit \ 604 ((FIXP_SGL)35) /* upperLimit actually refers to the _lowest_ noise energy */ 605 606 /* 607 Range check for current noise levels 608 */ 609 for (i = 0; i < h_sbr_data->frameInfo.nNoiseEnvelopes * nNfb; i++) { 610 h_sbr_data->sbrNoiseFloorLevel[i] = 611 fixMin(h_sbr_data->sbrNoiseFloorLevel[i], upperLimit); 612 h_sbr_data->sbrNoiseFloorLevel[i] = 613 fixMax(h_sbr_data->sbrNoiseFloorLevel[i], lowerLimit); 614 } 615 } 616 617 /*! 618 \brief Compensate for the wrong timing that might occur after a frame error. 619 */ 620 static void timeCompensateFirstEnvelope( 621 HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */ 622 HANDLE_SBR_FRAME_DATA h_sbr_data, /*!< pointer to actual data */ 623 HANDLE_SBR_PREV_FRAME_DATA 624 h_prev_data) /*!< pointer to data of last frame */ 625 { 626 int i, nScalefactors; 627 FRAME_INFO *pFrameInfo = &h_sbr_data->frameInfo; 628 UCHAR *nSfb = hHeaderData->freqBandData.nSfb; 629 int estimatedStartPos = 630 fMax(0, h_prev_data->stopPos - hHeaderData->numberTimeSlots); 631 int refLen, newLen, shift; 632 FIXP_SGL deltaExp; 633 634 /* Original length of first envelope according to bitstream */ 635 refLen = pFrameInfo->borders[1] - pFrameInfo->borders[0]; 636 /* Corrected length of first envelope (concealing can make the first envelope 637 * longer) */ 638 newLen = pFrameInfo->borders[1] - estimatedStartPos; 639 640 if (newLen <= 0) { 641 /* An envelope length of <= 0 would not work, so we don't use it. 642 May occur if the previous frame was flagged bad due to a mismatch 643 of the old and new frame infos. */ 644 newLen = refLen; 645 estimatedStartPos = pFrameInfo->borders[0]; 646 } 647 648 deltaExp = FDK_getNumOctavesDiv8(newLen, refLen); 649 650 /* Shift by -3 to rescale ld-table, ampRes-1 to enable coarser steps */ 651 shift = (FRACT_BITS - 1 - ENV_EXP_FRACT - 1 + 652 h_sbr_data->ampResolutionCurrentFrame - 3); 653 deltaExp = deltaExp >> shift; 654 pFrameInfo->borders[0] = estimatedStartPos; 655 pFrameInfo->bordersNoise[0] = estimatedStartPos; 656 657 if (h_sbr_data->coupling != COUPLING_BAL) { 658 nScalefactors = (pFrameInfo->freqRes[0]) ? nSfb[1] : nSfb[0]; 659 660 for (i = 0; i < nScalefactors; i++) 661 h_sbr_data->iEnvelope[i] = h_sbr_data->iEnvelope[i] + deltaExp; 662 } 663 } 664 665 /*! 666 \brief Convert each envelope value from logarithmic to linear domain 667 668 Energy levels are transmitted in powers of 2, i.e. only the exponent 669 is extracted from the bitstream. 670 Therefore, normally only integer exponents can occur. However during 671 fading (in case of a corrupt bitstream), a fractional part can also 672 occur. The data in the array iEnvelope is shifted left by ENV_EXP_FRACT 673 compared to an integer representation so that numbers smaller than 1 674 can be represented. 675 676 This function calculates a mantissa corresponding to the fractional 677 part of the exponent for each reference energy. The array iEnvelope 678 is converted in place to save memory. Input and output data must 679 be interpreted differently, as shown in the below figure: 680 681 \image html EnvelopeData.png 682 683 The data is then used in calculateSbrEnvelope(). 684 */ 685 static void requantizeEnvelopeData(HANDLE_SBR_FRAME_DATA h_sbr_data, 686 int ampResolution) { 687 int i; 688 FIXP_SGL mantissa; 689 int ampShift = 1 - ampResolution; 690 int exponent; 691 692 /* In case that ENV_EXP_FRACT is changed to something else but 0 or 8, 693 the initialization of this array has to be adapted! 694 */ 695 #if ENV_EXP_FRACT 696 static const FIXP_SGL pow2[ENV_EXP_FRACT] = { 697 FL2FXCONST_SGL(0.5f * pow(2.0f, pow(0.5f, 1))), /* 0.7071 */ 698 FL2FXCONST_SGL(0.5f * pow(2.0f, pow(0.5f, 2))), /* 0.5946 */ 699 FL2FXCONST_SGL(0.5f * pow(2.0f, pow(0.5f, 3))), 700 FL2FXCONST_SGL(0.5f * pow(2.0f, pow(0.5f, 4))), 701 FL2FXCONST_SGL(0.5f * pow(2.0f, pow(0.5f, 5))), 702 FL2FXCONST_SGL(0.5f * pow(2.0f, pow(0.5f, 6))), 703 FL2FXCONST_SGL(0.5f * pow(2.0f, pow(0.5f, 7))), 704 FL2FXCONST_SGL(0.5f * pow(2.0f, pow(0.5f, 8))) /* 0.5013 */ 705 }; 706 707 int bit, mask; 708 #endif 709 710 for (i = 0; i < h_sbr_data->nScaleFactors; i++) { 711 exponent = (LONG)h_sbr_data->iEnvelope[i]; 712 713 #if ENV_EXP_FRACT 714 715 exponent = exponent >> ampShift; 716 mantissa = 0.5f; 717 718 /* Amplify mantissa according to the fractional part of the 719 exponent (result will be between 0.500000 and 0.999999) 720 */ 721 mask = 1; /* begin with lowest bit of exponent */ 722 723 for (bit = ENV_EXP_FRACT - 1; bit >= 0; bit--) { 724 if (exponent & mask) { 725 /* The current bit of the exponent is set, 726 multiply mantissa with the corresponding factor: */ 727 mantissa = (FIXP_SGL)((mantissa * pow2[bit]) << 1); 728 } 729 /* Advance to next bit */ 730 mask = mask << 1; 731 } 732 733 /* Make integer part of exponent right aligned */ 734 exponent = exponent >> ENV_EXP_FRACT; 735 736 #else 737 /* In case of the high amplitude resolution, 1 bit of the exponent gets lost 738 by the shift. This will be compensated by a mantissa of 0.5*sqrt(2) 739 instead of 0.5 if that bit is 1. */ 740 mantissa = (exponent & ampShift) ? FL2FXCONST_SGL(0.707106781186548f) 741 : FL2FXCONST_SGL(0.5f); 742 exponent = exponent >> ampShift; 743 #endif 744 745 /* 746 Mantissa was set to 0.5 (instead of 1.0, therefore increase exponent by 747 1). Multiply by L=nChannels=64 by increasing exponent by another 6. 748 => Increase exponent by 7 749 */ 750 exponent += 7 + NRG_EXP_OFFSET; 751 752 /* Combine mantissa and exponent and write back the result */ 753 h_sbr_data->iEnvelope[i] = 754 ((FIXP_SGL)((SHORT)(FIXP_SGL)mantissa & MASK_M)) + 755 (FIXP_SGL)((SHORT)(FIXP_SGL)exponent & MASK_E); 756 } 757 } 758 759 /*! 760 \brief Build new reference energies from old ones and delta coded data 761 */ 762 static void deltaToLinearPcmEnvelopeDecoding( 763 HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */ 764 HANDLE_SBR_FRAME_DATA h_sbr_data, /*!< pointer to current data */ 765 HANDLE_SBR_PREV_FRAME_DATA h_prev_data) /*!< pointer to previous data */ 766 { 767 int i, domain, no_of_bands, band, freqRes; 768 769 FIXP_SGL *sfb_nrg_prev = h_prev_data->sfb_nrg_prev; 770 FIXP_SGL *ptr_nrg = h_sbr_data->iEnvelope; 771 772 int offset = 773 2 * hHeaderData->freqBandData.nSfb[0] - hHeaderData->freqBandData.nSfb[1]; 774 775 for (i = 0; i < h_sbr_data->frameInfo.nEnvelopes; i++) { 776 domain = h_sbr_data->domain_vec[i]; 777 freqRes = h_sbr_data->frameInfo.freqRes[i]; 778 779 FDK_ASSERT(freqRes >= 0 && freqRes <= 1); 780 781 no_of_bands = hHeaderData->freqBandData.nSfb[freqRes]; 782 783 FDK_ASSERT(no_of_bands < (64)); 784 785 if (domain == 0) { 786 mapLowResEnergyVal(*ptr_nrg, sfb_nrg_prev, offset, 0, freqRes); 787 ptr_nrg++; 788 for (band = 1; band < no_of_bands; band++) { 789 *ptr_nrg = *ptr_nrg + *(ptr_nrg - 1); 790 mapLowResEnergyVal(*ptr_nrg, sfb_nrg_prev, offset, band, freqRes); 791 ptr_nrg++; 792 } 793 } else { 794 for (band = 0; band < no_of_bands; band++) { 795 *ptr_nrg = 796 *ptr_nrg + sfb_nrg_prev[indexLow2High(offset, band, freqRes)]; 797 mapLowResEnergyVal(*ptr_nrg, sfb_nrg_prev, offset, band, freqRes); 798 ptr_nrg++; 799 } 800 } 801 } 802 } 803 804 /*! 805 \brief Build new noise levels from old ones and delta coded data 806 */ 807 static void decodeNoiseFloorlevels( 808 HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */ 809 HANDLE_SBR_FRAME_DATA h_sbr_data, /*!< pointer to current data */ 810 HANDLE_SBR_PREV_FRAME_DATA h_prev_data) /*!< pointer to previous data */ 811 { 812 int i; 813 int nNfb = hHeaderData->freqBandData.nNfb; 814 int nNoiseFloorEnvelopes = h_sbr_data->frameInfo.nNoiseEnvelopes; 815 816 /* Decode first noise envelope */ 817 818 if (h_sbr_data->domain_vec_noise[0] == 0) { 819 FIXP_SGL noiseLevel = h_sbr_data->sbrNoiseFloorLevel[0]; 820 for (i = 1; i < nNfb; i++) { 821 noiseLevel += h_sbr_data->sbrNoiseFloorLevel[i]; 822 h_sbr_data->sbrNoiseFloorLevel[i] = noiseLevel; 823 } 824 } else { 825 for (i = 0; i < nNfb; i++) { 826 h_sbr_data->sbrNoiseFloorLevel[i] += h_prev_data->prevNoiseLevel[i]; 827 } 828 } 829 830 /* If present, decode the second noise envelope 831 Note: nNoiseFloorEnvelopes can only be 1 or 2 */ 832 833 if (nNoiseFloorEnvelopes > 1) { 834 if (h_sbr_data->domain_vec_noise[1] == 0) { 835 FIXP_SGL noiseLevel = h_sbr_data->sbrNoiseFloorLevel[nNfb]; 836 for (i = nNfb + 1; i < 2 * nNfb; i++) { 837 noiseLevel += h_sbr_data->sbrNoiseFloorLevel[i]; 838 h_sbr_data->sbrNoiseFloorLevel[i] = noiseLevel; 839 } 840 } else { 841 for (i = 0; i < nNfb; i++) { 842 h_sbr_data->sbrNoiseFloorLevel[i + nNfb] += 843 h_sbr_data->sbrNoiseFloorLevel[i]; 844 } 845 } 846 } 847 848 limitNoiseLevels(hHeaderData, h_sbr_data); 849 850 /* Update prevNoiseLevel with the last noise envelope */ 851 for (i = 0; i < nNfb; i++) 852 h_prev_data->prevNoiseLevel[i] = 853 h_sbr_data->sbrNoiseFloorLevel[i + nNfb * (nNoiseFloorEnvelopes - 1)]; 854 855 /* Requantize the noise floor levels in COUPLING_OFF-mode */ 856 if (!h_sbr_data->coupling) { 857 int nf_e; 858 859 for (i = 0; i < nNoiseFloorEnvelopes * nNfb; i++) { 860 nf_e = 6 - (LONG)h_sbr_data->sbrNoiseFloorLevel[i] + 1 + NOISE_EXP_OFFSET; 861 /* +1 to compensate for a mantissa of 0.5 instead of 1.0 */ 862 863 h_sbr_data->sbrNoiseFloorLevel[i] = 864 (FIXP_SGL)(((LONG)FL2FXCONST_SGL(0.5f)) + /* mantissa */ 865 (nf_e & MASK_E)); /* exponent */ 866 } 867 } 868 } 869