1 2 /* ----------------------------------------------------------------------------------------------------------- 3 Software License for The Fraunhofer FDK AAC Codec Library for Android 4 5 Copyright 1995 - 2015 Fraunhofer-Gesellschaft zur Frderung der angewandten Forschung e.V. 6 All rights reserved. 7 8 1. INTRODUCTION 9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements 10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. 11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices. 12 13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual 14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by 15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part 16 of the MPEG specifications. 17 18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) 19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners 20 individually for the purpose of encoding or decoding bit streams in products that are compliant with 21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license 22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec 23 software may already be covered under those patent licenses when it is used for those licensed purposes only. 24 25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, 26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional 27 applications information and documentation. 28 29 2. COPYRIGHT LICENSE 30 31 Redistribution and use in source and binary forms, with or without modification, are permitted without 32 payment of copyright license fees provided that you satisfy the following conditions: 33 34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or 35 your modifications thereto in source code form. 36 37 You must retain the complete text of this software license in the documentation and/or other materials 38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. 39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your 40 modifications thereto to recipients of copies in binary form. 41 42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without 43 prior written permission. 44 45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec 46 software or your modifications thereto. 47 48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software 49 and the date of any change. For modified versions of the FDK AAC Codec, the term 50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term 51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." 52 53 3. NO PATENT LICENSE 54 55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, 56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with 57 respect to this software. 58 59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized 60 by appropriate patent licenses. 61 62 4. DISCLAIMER 63 64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors 65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties 66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, 68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits, 69 or business interruption, however caused and on any theory of liability, whether in contract, strict 70 liability, or tort (including negligence), arising in any way out of the use of this software, even if 71 advised of the possibility of such damage. 72 73 5. CONTACT INFORMATION 74 75 Fraunhofer Institute for Integrated Circuits IIS 76 Attention: Audio and Multimedia Departments - FDK AAC LL 77 Am Wolfsmantel 33 78 91058 Erlangen, Germany 79 80 www.iis.fraunhofer.de/amm 81 amm-info (at) iis.fraunhofer.de 82 ----------------------------------------------------------------------------------------------------------- */ 83 84 /*! 85 \file 86 \brief SBR bit writing routines 87 */ 88 89 90 #include "bit_sbr.h" 91 92 #include "code_env.h" 93 #include "cmondata.h" 94 #include "sbr.h" 95 96 #include "ps_main.h" 97 98 typedef enum { 99 SBR_ID_SCE = 1, 100 SBR_ID_CPE 101 } SBR_ELEMENT_TYPE; 102 103 104 static INT encodeSbrData (HANDLE_SBR_ENV_DATA sbrEnvDataLeft, 105 HANDLE_SBR_ENV_DATA sbrEnvDataRight, 106 HANDLE_PARAMETRIC_STEREO hParametricStereo, 107 HANDLE_COMMON_DATA cmonData, 108 SBR_ELEMENT_TYPE sbrElem, 109 INT coupling, 110 UINT sbrSyntaxFlags); 111 112 static INT encodeSbrHeader (HANDLE_SBR_HEADER_DATA sbrHeaderData, 113 HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData, 114 HANDLE_COMMON_DATA cmonData); 115 116 117 static INT encodeSbrHeaderData (HANDLE_SBR_HEADER_DATA sbrHeaderData, 118 HANDLE_FDK_BITSTREAM hBitStream); 119 120 static INT encodeSbrSingleChannelElement (HANDLE_SBR_ENV_DATA sbrEnvData, 121 HANDLE_FDK_BITSTREAM hBitStream 122 ,HANDLE_PARAMETRIC_STEREO hParametricStereo 123 ,UINT sbrSyntaxFlags 124 ); 125 126 127 128 static INT encodeSbrChannelPairElement (HANDLE_SBR_ENV_DATA sbrEnvDataLeft, 129 HANDLE_SBR_ENV_DATA sbrEnvDataRight, 130 HANDLE_PARAMETRIC_STEREO hParametricStereo, 131 HANDLE_FDK_BITSTREAM hBitStream, 132 INT coupling); 133 134 135 static INT encodeSbrGrid (HANDLE_SBR_ENV_DATA sbrEnvData, 136 HANDLE_FDK_BITSTREAM hBitStream); 137 138 static int encodeLowDelaySbrGrid ( HANDLE_SBR_ENV_DATA sbrEnvData, 139 HANDLE_FDK_BITSTREAM hBitStream, 140 int transmitFreqs); 141 142 static INT encodeSbrDtdf (HANDLE_SBR_ENV_DATA sbrEnvData, 143 HANDLE_FDK_BITSTREAM hBitStream); 144 145 static INT writeNoiseLevelData (HANDLE_SBR_ENV_DATA sbrEnvData, 146 HANDLE_FDK_BITSTREAM hBitStream, 147 INT coupling); 148 149 static INT writeEnvelopeData (HANDLE_SBR_ENV_DATA sbrEnvData, 150 HANDLE_FDK_BITSTREAM hBitStream, 151 INT coupling); 152 153 static INT writeSyntheticCodingData (HANDLE_SBR_ENV_DATA sbrEnvData, 154 HANDLE_FDK_BITSTREAM hBitStream); 155 156 157 static INT encodeExtendedData (HANDLE_PARAMETRIC_STEREO hParametricStereo, 158 HANDLE_FDK_BITSTREAM hBitStream); 159 160 161 162 static INT getSbrExtendedDataSize (HANDLE_PARAMETRIC_STEREO hParametricStereo); 163 164 /***************************************************************************** 165 166 functionname: FDKsbrEnc_WriteEnvSingleChannelElement 167 description: writes pure SBR single channel data element 168 returns: number of bits written 169 input: 170 output: 171 172 *****************************************************************************/ 173 INT 174 FDKsbrEnc_WriteEnvSingleChannelElement( 175 HANDLE_SBR_HEADER_DATA sbrHeaderData, 176 HANDLE_PARAMETRIC_STEREO hParametricStereo, 177 HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData, 178 HANDLE_SBR_ENV_DATA sbrEnvData, 179 HANDLE_COMMON_DATA cmonData, 180 UINT sbrSyntaxFlags 181 ) 182 183 { 184 INT payloadBits = 0; 185 186 cmonData->sbrHdrBits = 0; 187 cmonData->sbrDataBits = 0; 188 189 /* write pure sbr data */ 190 if (sbrEnvData != NULL) { 191 192 /* write header */ 193 payloadBits += encodeSbrHeader (sbrHeaderData, 194 sbrBitstreamData, 195 cmonData); 196 197 198 /* write data */ 199 payloadBits += encodeSbrData (sbrEnvData, 200 NULL, 201 hParametricStereo, 202 cmonData, 203 SBR_ID_SCE, 204 0, 205 sbrSyntaxFlags); 206 207 } 208 return payloadBits; 209 } 210 211 /***************************************************************************** 212 213 functionname: FDKsbrEnc_WriteEnvChannelPairElement 214 description: writes pure SBR channel pair data element 215 returns: number of bits written 216 input: 217 output: 218 219 *****************************************************************************/ 220 INT 221 FDKsbrEnc_WriteEnvChannelPairElement (HANDLE_SBR_HEADER_DATA sbrHeaderData, 222 HANDLE_PARAMETRIC_STEREO hParametricStereo, 223 HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData, 224 HANDLE_SBR_ENV_DATA sbrEnvDataLeft, 225 HANDLE_SBR_ENV_DATA sbrEnvDataRight, 226 HANDLE_COMMON_DATA cmonData, 227 UINT sbrSyntaxFlags) 228 229 { 230 INT payloadBits = 0; 231 cmonData->sbrHdrBits = 0; 232 cmonData->sbrDataBits = 0; 233 234 /* write pure sbr data */ 235 if ((sbrEnvDataLeft != NULL) && (sbrEnvDataRight != NULL)) { 236 237 /* write header */ 238 payloadBits += encodeSbrHeader (sbrHeaderData, 239 sbrBitstreamData, 240 cmonData); 241 242 /* write data */ 243 payloadBits += encodeSbrData (sbrEnvDataLeft, 244 sbrEnvDataRight, 245 hParametricStereo, 246 cmonData, 247 SBR_ID_CPE, 248 sbrHeaderData->coupling, 249 sbrSyntaxFlags); 250 251 } 252 return payloadBits; 253 } 254 255 INT 256 FDKsbrEnc_CountSbrChannelPairElement (HANDLE_SBR_HEADER_DATA sbrHeaderData, 257 HANDLE_PARAMETRIC_STEREO hParametricStereo, 258 HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData, 259 HANDLE_SBR_ENV_DATA sbrEnvDataLeft, 260 HANDLE_SBR_ENV_DATA sbrEnvDataRight, 261 HANDLE_COMMON_DATA cmonData, 262 UINT sbrSyntaxFlags) 263 { 264 INT payloadBits; 265 INT bitPos = FDKgetValidBits(&cmonData->sbrBitbuf); 266 267 payloadBits = FDKsbrEnc_WriteEnvChannelPairElement(sbrHeaderData, 268 hParametricStereo, 269 sbrBitstreamData, 270 sbrEnvDataLeft, 271 sbrEnvDataRight, 272 cmonData, 273 sbrSyntaxFlags); 274 275 FDKpushBack(&cmonData->sbrBitbuf, (FDKgetValidBits(&cmonData->sbrBitbuf) - bitPos) ); 276 277 return payloadBits; 278 } 279 280 281 void sbrEncoder_GetHeader(SBR_ENCODER *sbrEncoder, 282 HANDLE_FDK_BITSTREAM hBs, 283 INT element_index, 284 int fSendHeaders) 285 { 286 encodeSbrHeaderData (&sbrEncoder->sbrElement[element_index]->sbrHeaderData, hBs); 287 288 if (fSendHeaders == 0) { 289 /* Prevent header being embedded into the SBR payload. */ 290 sbrEncoder->sbrElement[element_index]->sbrBitstreamData.NrSendHeaderData = -1; 291 sbrEncoder->sbrElement[element_index]->sbrBitstreamData.HeaderActive = 0; 292 sbrEncoder->sbrElement[element_index]->sbrBitstreamData.CountSendHeaderData = -1; 293 } 294 } 295 296 297 /***************************************************************************** 298 299 functionname: encodeSbrHeader 300 description: encodes SBR Header information 301 returns: number of bits written 302 input: 303 output: 304 305 *****************************************************************************/ 306 static INT 307 encodeSbrHeader (HANDLE_SBR_HEADER_DATA sbrHeaderData, 308 HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData, 309 HANDLE_COMMON_DATA cmonData) 310 { 311 INT payloadBits = 0; 312 313 if (sbrBitstreamData->HeaderActive) { 314 payloadBits += FDKwriteBits (&cmonData->sbrBitbuf, 1, 1); 315 payloadBits += encodeSbrHeaderData (sbrHeaderData, 316 &cmonData->sbrBitbuf); 317 } 318 else { 319 payloadBits += FDKwriteBits (&cmonData->sbrBitbuf, 0, 1); 320 } 321 322 cmonData->sbrHdrBits = payloadBits; 323 324 return payloadBits; 325 } 326 327 328 329 /***************************************************************************** 330 331 functionname: encodeSbrHeaderData 332 description: writes sbr_header() 333 bs_protocol_version through bs_header_extra_2 334 returns: number of bits written 335 input: 336 output: 337 338 *****************************************************************************/ 339 static INT 340 encodeSbrHeaderData (HANDLE_SBR_HEADER_DATA sbrHeaderData, 341 HANDLE_FDK_BITSTREAM hBitStream) 342 343 { 344 INT payloadBits = 0; 345 if (sbrHeaderData != NULL) { 346 payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_amp_res, 347 SI_SBR_AMP_RES_BITS); 348 payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_start_frequency, 349 SI_SBR_START_FREQ_BITS); 350 payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_stop_frequency, 351 SI_SBR_STOP_FREQ_BITS); 352 payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_xover_band, 353 SI_SBR_XOVER_BAND_BITS); 354 355 payloadBits += FDKwriteBits (hBitStream, 0, 356 SI_SBR_RESERVED_BITS); 357 358 payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->header_extra_1, 359 SI_SBR_HEADER_EXTRA_1_BITS); 360 payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->header_extra_2, 361 SI_SBR_HEADER_EXTRA_2_BITS); 362 363 364 if (sbrHeaderData->header_extra_1) { 365 payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->freqScale, 366 SI_SBR_FREQ_SCALE_BITS); 367 payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->alterScale, 368 SI_SBR_ALTER_SCALE_BITS); 369 payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_noise_bands, 370 SI_SBR_NOISE_BANDS_BITS); 371 } /* sbrHeaderData->header_extra_1 */ 372 373 if (sbrHeaderData->header_extra_2) { 374 payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_limiter_bands, 375 SI_SBR_LIMITER_BANDS_BITS); 376 payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_limiter_gains, 377 SI_SBR_LIMITER_GAINS_BITS); 378 payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_interpol_freq, 379 SI_SBR_INTERPOL_FREQ_BITS); 380 payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_smoothing_length, 381 SI_SBR_SMOOTHING_LENGTH_BITS); 382 383 } /* sbrHeaderData->header_extra_2 */ 384 } /* sbrHeaderData != NULL */ 385 386 return payloadBits; 387 } 388 389 390 /***************************************************************************** 391 392 functionname: encodeSbrData 393 description: encodes sbr Data information 394 returns: number of bits written 395 input: 396 output: 397 398 *****************************************************************************/ 399 static INT 400 encodeSbrData (HANDLE_SBR_ENV_DATA sbrEnvDataLeft, 401 HANDLE_SBR_ENV_DATA sbrEnvDataRight, 402 HANDLE_PARAMETRIC_STEREO hParametricStereo, 403 HANDLE_COMMON_DATA cmonData, 404 SBR_ELEMENT_TYPE sbrElem, 405 INT coupling, 406 UINT sbrSyntaxFlags) 407 { 408 INT payloadBits = 0; 409 410 switch (sbrElem) { 411 case SBR_ID_SCE: 412 payloadBits += encodeSbrSingleChannelElement (sbrEnvDataLeft, &cmonData->sbrBitbuf, hParametricStereo, sbrSyntaxFlags); 413 break; 414 case SBR_ID_CPE: 415 payloadBits += encodeSbrChannelPairElement (sbrEnvDataLeft, sbrEnvDataRight, hParametricStereo, &cmonData->sbrBitbuf, coupling); 416 break; 417 default: 418 /* we never should apply SBR to any other element type */ 419 FDK_ASSERT (0); 420 } 421 422 cmonData->sbrDataBits = payloadBits; 423 424 return payloadBits; 425 } 426 427 #define MODE_FREQ_TANS 1 428 #define MODE_NO_FREQ_TRAN 0 429 #define LD_TRANSMISSION MODE_FREQ_TANS 430 static int encodeFreqs (int mode) { 431 return ((mode & MODE_FREQ_TANS) ? 1 : 0); 432 } 433 434 435 /***************************************************************************** 436 437 functionname: encodeSbrSingleChannelElement 438 description: encodes sbr SCE information 439 returns: number of bits written 440 input: 441 output: 442 443 *****************************************************************************/ 444 static INT 445 encodeSbrSingleChannelElement (HANDLE_SBR_ENV_DATA sbrEnvData, 446 HANDLE_FDK_BITSTREAM hBitStream 447 ,HANDLE_PARAMETRIC_STEREO hParametricStereo 448 ,UINT sbrSyntaxFlags 449 ) 450 { 451 INT i, payloadBits = 0; 452 453 payloadBits += FDKwriteBits (hBitStream, 0, SI_SBR_DATA_EXTRA_BITS); /* no reserved bits */ 454 455 if (sbrEnvData->ldGrid) { 456 if ( sbrEnvData->hSbrBSGrid->frameClass != FIXFIXonly ) { 457 /* encode normal SbrGrid */ 458 payloadBits += encodeSbrGrid (sbrEnvData, hBitStream); 459 } else { 460 /* use FIXFIXonly frame Grid */ 461 payloadBits += encodeLowDelaySbrGrid ( sbrEnvData, hBitStream, encodeFreqs(LD_TRANSMISSION)); 462 } 463 } 464 else 465 { 466 if (sbrSyntaxFlags & SBR_SYNTAX_SCALABLE) { 467 payloadBits += FDKwriteBits (hBitStream, 1, SI_SBR_COUPLING_BITS); 468 } 469 payloadBits += encodeSbrGrid (sbrEnvData, hBitStream); 470 } 471 472 payloadBits += encodeSbrDtdf (sbrEnvData, hBitStream); 473 474 for (i = 0; i < sbrEnvData->noOfnoisebands; i++) { 475 payloadBits += FDKwriteBits (hBitStream, sbrEnvData->sbr_invf_mode_vec[i], SI_SBR_INVF_MODE_BITS); 476 } 477 478 payloadBits += writeEnvelopeData (sbrEnvData, hBitStream, 0); 479 payloadBits += writeNoiseLevelData (sbrEnvData, hBitStream, 0); 480 481 payloadBits += writeSyntheticCodingData (sbrEnvData,hBitStream); 482 483 payloadBits += encodeExtendedData(hParametricStereo, hBitStream); 484 485 return payloadBits; 486 } 487 488 489 /***************************************************************************** 490 491 functionname: encodeSbrChannelPairElement 492 description: encodes sbr CPE information 493 returns: 494 input: 495 output: 496 497 *****************************************************************************/ 498 static INT 499 encodeSbrChannelPairElement (HANDLE_SBR_ENV_DATA sbrEnvDataLeft, 500 HANDLE_SBR_ENV_DATA sbrEnvDataRight, 501 HANDLE_PARAMETRIC_STEREO hParametricStereo, 502 HANDLE_FDK_BITSTREAM hBitStream, 503 INT coupling) 504 { 505 INT payloadBits = 0; 506 INT i = 0; 507 508 payloadBits += FDKwriteBits (hBitStream, 0, SI_SBR_DATA_EXTRA_BITS); /* no reserved bits */ 509 510 payloadBits += FDKwriteBits (hBitStream, coupling, SI_SBR_COUPLING_BITS); 511 512 if (coupling) { 513 if (sbrEnvDataLeft->ldGrid) { 514 if ( sbrEnvDataLeft->hSbrBSGrid->frameClass != FIXFIXonly ) { 515 /* normal SbrGrid */ 516 payloadBits += encodeSbrGrid (sbrEnvDataLeft, hBitStream); 517 518 } else { 519 /* FIXFIXonly frame Grid */ 520 payloadBits += encodeLowDelaySbrGrid ( sbrEnvDataLeft, hBitStream, encodeFreqs(LD_TRANSMISSION)); 521 } 522 } else 523 payloadBits += encodeSbrGrid (sbrEnvDataLeft, hBitStream); 524 525 payloadBits += encodeSbrDtdf (sbrEnvDataLeft, hBitStream); 526 payloadBits += encodeSbrDtdf (sbrEnvDataRight, hBitStream); 527 528 for (i = 0; i < sbrEnvDataLeft->noOfnoisebands; i++) { 529 payloadBits += FDKwriteBits (hBitStream, sbrEnvDataLeft->sbr_invf_mode_vec[i], SI_SBR_INVF_MODE_BITS); 530 } 531 532 payloadBits += writeEnvelopeData (sbrEnvDataLeft, hBitStream,1); 533 payloadBits += writeNoiseLevelData (sbrEnvDataLeft, hBitStream,1); 534 payloadBits += writeEnvelopeData (sbrEnvDataRight, hBitStream,1); 535 payloadBits += writeNoiseLevelData (sbrEnvDataRight, hBitStream,1); 536 537 payloadBits += writeSyntheticCodingData (sbrEnvDataLeft,hBitStream); 538 payloadBits += writeSyntheticCodingData (sbrEnvDataRight,hBitStream); 539 540 } else { /* no coupling */ 541 FDK_ASSERT(sbrEnvDataLeft->ldGrid == sbrEnvDataRight->ldGrid); 542 543 if (sbrEnvDataLeft->ldGrid || sbrEnvDataRight->ldGrid) { 544 /* sbrEnvDataLeft (left channel) */ 545 if ( sbrEnvDataLeft->hSbrBSGrid->frameClass != FIXFIXonly) { 546 /* no FIXFIXonly Frame so we dont need encodeLowDelaySbrGrid */ 547 /* normal SbrGrid */ 548 payloadBits += encodeSbrGrid (sbrEnvDataLeft, hBitStream); 549 550 } else { 551 /* FIXFIXonly frame Grid */ 552 payloadBits += encodeLowDelaySbrGrid ( sbrEnvDataLeft, hBitStream, encodeFreqs(LD_TRANSMISSION)); 553 } 554 555 /* sbrEnvDataRight (right channel) */ 556 if ( sbrEnvDataRight->hSbrBSGrid->frameClass != FIXFIXonly) { 557 /* no FIXFIXonly Frame so we dont need encodeLowDelaySbrGrid */ 558 /* normal SbrGrid */ 559 payloadBits += encodeSbrGrid (sbrEnvDataRight, hBitStream); 560 561 } else { 562 /* FIXFIXonly frame Grid */ 563 payloadBits += encodeLowDelaySbrGrid ( sbrEnvDataRight, hBitStream, encodeFreqs(LD_TRANSMISSION)); 564 } 565 } else 566 { 567 payloadBits += encodeSbrGrid (sbrEnvDataLeft, hBitStream); 568 payloadBits += encodeSbrGrid (sbrEnvDataRight, hBitStream); 569 } 570 payloadBits += encodeSbrDtdf (sbrEnvDataLeft, hBitStream); 571 payloadBits += encodeSbrDtdf (sbrEnvDataRight, hBitStream); 572 573 for (i = 0; i < sbrEnvDataLeft->noOfnoisebands; i++) { 574 payloadBits += FDKwriteBits (hBitStream, sbrEnvDataLeft->sbr_invf_mode_vec[i], 575 SI_SBR_INVF_MODE_BITS); 576 } 577 for (i = 0; i < sbrEnvDataRight->noOfnoisebands; i++) { 578 payloadBits += FDKwriteBits (hBitStream, sbrEnvDataRight->sbr_invf_mode_vec[i], 579 SI_SBR_INVF_MODE_BITS); 580 } 581 582 payloadBits += writeEnvelopeData (sbrEnvDataLeft, hBitStream,0); 583 payloadBits += writeEnvelopeData (sbrEnvDataRight, hBitStream,0); 584 payloadBits += writeNoiseLevelData (sbrEnvDataLeft, hBitStream,0); 585 payloadBits += writeNoiseLevelData (sbrEnvDataRight, hBitStream,0); 586 587 payloadBits += writeSyntheticCodingData (sbrEnvDataLeft,hBitStream); 588 payloadBits += writeSyntheticCodingData (sbrEnvDataRight,hBitStream); 589 590 } /* coupling */ 591 592 payloadBits += encodeExtendedData(hParametricStereo, hBitStream); 593 594 return payloadBits; 595 } 596 597 static INT ceil_ln2(INT x) 598 { 599 INT tmp=-1; 600 while((1<<++tmp) < x); 601 return(tmp); 602 } 603 604 605 /***************************************************************************** 606 607 functionname: encodeSbrGrid 608 description: if hBitStream != NULL writes bits that describes the 609 time/frequency grouping of a frame; else counts them only 610 returns: number of bits written or counted 611 input: 612 output: 613 614 *****************************************************************************/ 615 static INT 616 encodeSbrGrid (HANDLE_SBR_ENV_DATA sbrEnvData, HANDLE_FDK_BITSTREAM hBitStream) 617 { 618 INT payloadBits = 0; 619 INT i, temp; 620 INT bufferFrameStart = sbrEnvData->hSbrBSGrid->bufferFrameStart; 621 INT numberTimeSlots = sbrEnvData->hSbrBSGrid->numberTimeSlots; 622 623 if (sbrEnvData->ldGrid) 624 payloadBits += FDKwriteBits (hBitStream, 625 sbrEnvData->hSbrBSGrid->frameClass, 626 SBR_CLA_BITS_LD); 627 else 628 payloadBits += FDKwriteBits (hBitStream, 629 sbrEnvData->hSbrBSGrid->frameClass, 630 SBR_CLA_BITS); 631 632 switch (sbrEnvData->hSbrBSGrid->frameClass) { 633 case FIXFIXonly: 634 FDK_ASSERT(0 /* Fatal error in encodeSbrGrid! */); 635 break; 636 case FIXFIX: 637 temp = ceil_ln2(sbrEnvData->hSbrBSGrid->bs_num_env); 638 payloadBits += FDKwriteBits (hBitStream, temp, SBR_ENV_BITS); 639 if ((sbrEnvData->ldGrid) && (sbrEnvData->hSbrBSGrid->bs_num_env==1)) 640 payloadBits += FDKwriteBits(hBitStream, sbrEnvData->currentAmpResFF, SI_SBR_AMP_RES_BITS); 641 payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->v_f[0], SBR_RES_BITS); 642 643 break; 644 645 case FIXVAR: 646 case VARFIX: 647 if (sbrEnvData->hSbrBSGrid->frameClass == FIXVAR) 648 temp = sbrEnvData->hSbrBSGrid->bs_abs_bord - (bufferFrameStart + numberTimeSlots); 649 else 650 temp = sbrEnvData->hSbrBSGrid->bs_abs_bord - bufferFrameStart; 651 652 payloadBits += FDKwriteBits (hBitStream, temp, SBR_ABS_BITS); 653 payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->n, SBR_NUM_BITS); 654 655 for (i = 0; i < sbrEnvData->hSbrBSGrid->n; i++) { 656 temp = (sbrEnvData->hSbrBSGrid->bs_rel_bord[i] - 2) >> 1; 657 payloadBits += FDKwriteBits (hBitStream, temp, SBR_REL_BITS); 658 } 659 660 temp = ceil_ln2(sbrEnvData->hSbrBSGrid->n + 2); 661 payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->p, temp); 662 663 for (i = 0; i < sbrEnvData->hSbrBSGrid->n + 1; i++) { 664 payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->v_f[i], 665 SBR_RES_BITS); 666 } 667 break; 668 669 case VARVAR: 670 temp = sbrEnvData->hSbrBSGrid->bs_abs_bord_0 - bufferFrameStart; 671 payloadBits += FDKwriteBits (hBitStream, temp, SBR_ABS_BITS); 672 temp = sbrEnvData->hSbrBSGrid->bs_abs_bord_1 - (bufferFrameStart + numberTimeSlots); 673 payloadBits += FDKwriteBits (hBitStream, temp, SBR_ABS_BITS); 674 675 payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->bs_num_rel_0, SBR_NUM_BITS); 676 payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->bs_num_rel_1, SBR_NUM_BITS); 677 678 for (i = 0; i < sbrEnvData->hSbrBSGrid->bs_num_rel_0; i++) { 679 temp = (sbrEnvData->hSbrBSGrid->bs_rel_bord_0[i] - 2) >> 1; 680 payloadBits += FDKwriteBits (hBitStream, temp, SBR_REL_BITS); 681 } 682 683 for (i = 0; i < sbrEnvData->hSbrBSGrid->bs_num_rel_1; i++) { 684 temp = (sbrEnvData->hSbrBSGrid->bs_rel_bord_1[i] - 2) >> 1; 685 payloadBits += FDKwriteBits (hBitStream, temp, SBR_REL_BITS); 686 } 687 688 temp = ceil_ln2(sbrEnvData->hSbrBSGrid->bs_num_rel_0 + 689 sbrEnvData->hSbrBSGrid->bs_num_rel_1 + 2); 690 payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->p, temp); 691 692 temp = sbrEnvData->hSbrBSGrid->bs_num_rel_0 + 693 sbrEnvData->hSbrBSGrid->bs_num_rel_1 + 1; 694 695 for (i = 0; i < temp; i++) { 696 payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->v_fLR[i], 697 SBR_RES_BITS); 698 } 699 break; 700 } 701 702 return payloadBits; 703 } 704 705 #define SBR_CLA_BITS_LD 1 706 /***************************************************************************** 707 708 functionname: encodeLowDelaySbrGrid 709 description: if hBitStream != NULL writes bits that describes the 710 time/frequency grouping of a frame; 711 else counts them only 712 (this function only write the FIXFIXonly Bitstream data) 713 returns: number of bits written or counted 714 input: 715 output: 716 717 *****************************************************************************/ 718 static int 719 encodeLowDelaySbrGrid ( HANDLE_SBR_ENV_DATA sbrEnvData, 720 HANDLE_FDK_BITSTREAM hBitStream, 721 int transmitFreqs 722 ) 723 { 724 int payloadBits = 0; 725 int i; 726 727 /* write FIXFIXonly Grid */ 728 /* write frameClass [1 bit] for FIXFIXonly Grid */ 729 payloadBits += FDKwriteBits(hBitStream, 1, SBR_CLA_BITS_LD); 730 731 /* absolute Borders are fix: 0,X,X,X,nTimeSlots; so we dont have to transmit them */ 732 /* only transmit the transient position! */ 733 /* with this info (b1) we can reconstruct the Frame on Decoder side : */ 734 /* border[0] = 0; border[1] = b1; border[2]=b1+2; border[3] = nrTimeSlots */ 735 736 /* use 3 or 4bits for transient border (border) */ 737 if (sbrEnvData->hSbrBSGrid->numberTimeSlots == 8) 738 payloadBits += FDKwriteBits ( hBitStream, sbrEnvData->hSbrBSGrid->bs_abs_bord, 3); 739 else 740 payloadBits += FDKwriteBits ( hBitStream, sbrEnvData->hSbrBSGrid->bs_abs_bord, 4); 741 742 if (transmitFreqs) { 743 /* write FreqRes grid */ 744 for (i = 0; i < sbrEnvData->hSbrBSGrid->bs_num_env; i++) { 745 payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->v_f[i], SBR_RES_BITS); 746 } 747 } 748 749 return payloadBits; 750 } 751 752 /***************************************************************************** 753 754 functionname: encodeSbrDtdf 755 description: writes bits that describes the direction of the envelopes of a frame 756 returns: number of bits written 757 input: 758 output: 759 760 *****************************************************************************/ 761 static INT 762 encodeSbrDtdf (HANDLE_SBR_ENV_DATA sbrEnvData, HANDLE_FDK_BITSTREAM hBitStream) 763 { 764 INT i, payloadBits = 0, noOfNoiseEnvelopes; 765 766 noOfNoiseEnvelopes = sbrEnvData->noOfEnvelopes > 1 ? 2 : 1; 767 768 for (i = 0; i < sbrEnvData->noOfEnvelopes; ++i) { 769 payloadBits += FDKwriteBits (hBitStream, sbrEnvData->domain_vec[i], SBR_DIR_BITS); 770 } 771 for (i = 0; i < noOfNoiseEnvelopes; ++i) { 772 payloadBits += FDKwriteBits (hBitStream, sbrEnvData->domain_vec_noise[i], SBR_DIR_BITS); 773 } 774 775 return payloadBits; 776 } 777 778 779 /***************************************************************************** 780 781 functionname: writeNoiseLevelData 782 description: writes bits corresponding to the noise-floor-level 783 returns: number of bits written 784 input: 785 output: 786 787 *****************************************************************************/ 788 static INT 789 writeNoiseLevelData (HANDLE_SBR_ENV_DATA sbrEnvData, HANDLE_FDK_BITSTREAM hBitStream, INT coupling) 790 { 791 INT j, i, payloadBits = 0; 792 INT nNoiseEnvelopes = sbrEnvData->noOfEnvelopes > 1 ? 2 : 1; 793 794 for (i = 0; i < nNoiseEnvelopes; i++) { 795 switch (sbrEnvData->domain_vec_noise[i]) { 796 case FREQ: 797 if (coupling && sbrEnvData->balance) { 798 payloadBits += FDKwriteBits (hBitStream, 799 sbrEnvData->sbr_noise_levels[i * sbrEnvData->noOfnoisebands], 800 sbrEnvData->si_sbr_start_noise_bits_balance); 801 } else { 802 payloadBits += FDKwriteBits (hBitStream, 803 sbrEnvData->sbr_noise_levels[i * sbrEnvData->noOfnoisebands], 804 sbrEnvData->si_sbr_start_noise_bits); 805 } 806 807 for (j = 1 + i * sbrEnvData->noOfnoisebands; j < (sbrEnvData->noOfnoisebands * (1 + i)); j++) { 808 if (coupling) { 809 if (sbrEnvData->balance) { 810 /* coupling && balance */ 811 payloadBits += FDKwriteBits (hBitStream, 812 sbrEnvData->hufftableNoiseBalanceFreqC[sbrEnvData->sbr_noise_levels[j] + 813 CODE_BOOK_SCF_LAV_BALANCE11], 814 sbrEnvData->hufftableNoiseBalanceFreqL[sbrEnvData->sbr_noise_levels[j] + 815 CODE_BOOK_SCF_LAV_BALANCE11]); 816 } else { 817 /* coupling && !balance */ 818 payloadBits += FDKwriteBits (hBitStream, 819 sbrEnvData->hufftableNoiseLevelFreqC[sbrEnvData->sbr_noise_levels[j] + 820 CODE_BOOK_SCF_LAV11], 821 sbrEnvData->hufftableNoiseLevelFreqL[sbrEnvData->sbr_noise_levels[j] + 822 CODE_BOOK_SCF_LAV11]); 823 } 824 } else { 825 /* !coupling */ 826 payloadBits += FDKwriteBits (hBitStream, 827 sbrEnvData->hufftableNoiseFreqC[sbrEnvData->sbr_noise_levels[j] + 828 CODE_BOOK_SCF_LAV11], 829 sbrEnvData->hufftableNoiseFreqL[sbrEnvData->sbr_noise_levels[j] + 830 CODE_BOOK_SCF_LAV11]); 831 } 832 } 833 break; 834 835 case TIME: 836 for (j = i * sbrEnvData->noOfnoisebands; j < (sbrEnvData->noOfnoisebands * (1 + i)); j++) { 837 if (coupling) { 838 if (sbrEnvData->balance) { 839 /* coupling && balance */ 840 payloadBits += FDKwriteBits (hBitStream, 841 sbrEnvData->hufftableNoiseBalanceTimeC[sbrEnvData->sbr_noise_levels[j] + 842 CODE_BOOK_SCF_LAV_BALANCE11], 843 sbrEnvData->hufftableNoiseBalanceTimeL[sbrEnvData->sbr_noise_levels[j] + 844 CODE_BOOK_SCF_LAV_BALANCE11]); 845 } else { 846 /* coupling && !balance */ 847 payloadBits += FDKwriteBits (hBitStream, 848 sbrEnvData->hufftableNoiseLevelTimeC[sbrEnvData->sbr_noise_levels[j] + 849 CODE_BOOK_SCF_LAV11], 850 sbrEnvData->hufftableNoiseLevelTimeL[sbrEnvData->sbr_noise_levels[j] + 851 CODE_BOOK_SCF_LAV11]); 852 } 853 } else { 854 /* !coupling */ 855 payloadBits += FDKwriteBits (hBitStream, 856 sbrEnvData->hufftableNoiseLevelTimeC[sbrEnvData->sbr_noise_levels[j] + 857 CODE_BOOK_SCF_LAV11], 858 sbrEnvData->hufftableNoiseLevelTimeL[sbrEnvData->sbr_noise_levels[j] + 859 CODE_BOOK_SCF_LAV11]); 860 } 861 } 862 break; 863 } 864 } 865 return payloadBits; 866 } 867 868 869 /***************************************************************************** 870 871 functionname: writeEnvelopeData 872 description: writes bits corresponding to the envelope 873 returns: number of bits written 874 input: 875 output: 876 877 *****************************************************************************/ 878 static INT 879 writeEnvelopeData (HANDLE_SBR_ENV_DATA sbrEnvData, HANDLE_FDK_BITSTREAM hBitStream, INT coupling) 880 { 881 INT payloadBits = 0, j, i, delta; 882 883 for (j = 0; j < sbrEnvData->noOfEnvelopes; j++) { /* loop over all envelopes */ 884 if (sbrEnvData->domain_vec[j] == FREQ) { 885 if (coupling && sbrEnvData->balance) { 886 payloadBits += FDKwriteBits (hBitStream, sbrEnvData->ienvelope[j][0], sbrEnvData->si_sbr_start_env_bits_balance); 887 } else { 888 payloadBits += FDKwriteBits (hBitStream, sbrEnvData->ienvelope[j][0], sbrEnvData->si_sbr_start_env_bits); 889 } 890 } 891 892 for (i = 1 - sbrEnvData->domain_vec[j]; i < sbrEnvData->noScfBands[j]; i++) { 893 delta = sbrEnvData->ienvelope[j][i]; 894 if (coupling && sbrEnvData->balance) { 895 FDK_ASSERT (fixp_abs (delta) <= sbrEnvData->codeBookScfLavBalance); 896 } else { 897 FDK_ASSERT (fixp_abs (delta) <= sbrEnvData->codeBookScfLav); 898 } 899 if (coupling) { 900 if (sbrEnvData->balance) { 901 if (sbrEnvData->domain_vec[j]) { 902 /* coupling && balance && TIME */ 903 payloadBits += FDKwriteBits (hBitStream, 904 sbrEnvData->hufftableBalanceTimeC[delta + sbrEnvData->codeBookScfLavBalance], 905 sbrEnvData->hufftableBalanceTimeL[delta + sbrEnvData->codeBookScfLavBalance]); 906 } else { 907 /* coupling && balance && FREQ */ 908 payloadBits += FDKwriteBits (hBitStream, 909 sbrEnvData->hufftableBalanceFreqC[delta + sbrEnvData->codeBookScfLavBalance], 910 sbrEnvData->hufftableBalanceFreqL[delta + sbrEnvData->codeBookScfLavBalance]); 911 } 912 } else { 913 if (sbrEnvData->domain_vec[j]) { 914 /* coupling && !balance && TIME */ 915 payloadBits += FDKwriteBits (hBitStream, 916 sbrEnvData->hufftableLevelTimeC[delta + sbrEnvData->codeBookScfLav], 917 sbrEnvData->hufftableLevelTimeL[delta + sbrEnvData->codeBookScfLav]); 918 } else { 919 /* coupling && !balance && FREQ */ 920 payloadBits += FDKwriteBits (hBitStream, 921 sbrEnvData->hufftableLevelFreqC[delta + sbrEnvData->codeBookScfLav], 922 sbrEnvData->hufftableLevelFreqL[delta + sbrEnvData->codeBookScfLav]); 923 } 924 } 925 } else { 926 if (sbrEnvData->domain_vec[j]) { 927 /* !coupling && TIME */ 928 payloadBits += FDKwriteBits (hBitStream, 929 sbrEnvData->hufftableTimeC[delta + sbrEnvData->codeBookScfLav], 930 sbrEnvData->hufftableTimeL[delta + sbrEnvData->codeBookScfLav]); 931 } else { 932 /* !coupling && FREQ */ 933 payloadBits += FDKwriteBits (hBitStream, 934 sbrEnvData->hufftableFreqC[delta + sbrEnvData->codeBookScfLav], 935 sbrEnvData->hufftableFreqL[delta + sbrEnvData->codeBookScfLav]); 936 } 937 } 938 } 939 } 940 return payloadBits; 941 } 942 943 944 /***************************************************************************** 945 946 functionname: encodeExtendedData 947 description: writes bits corresponding to the extended data 948 returns: number of bits written 949 input: 950 output: 951 952 *****************************************************************************/ 953 static INT encodeExtendedData (HANDLE_PARAMETRIC_STEREO hParametricStereo, 954 HANDLE_FDK_BITSTREAM hBitStream) 955 { 956 INT extDataSize; 957 INT payloadBits = 0; 958 959 extDataSize = getSbrExtendedDataSize(hParametricStereo); 960 961 962 if (extDataSize != 0) { 963 INT maxExtSize = (1<<SI_SBR_EXTENSION_SIZE_BITS) - 1; 964 INT writtenNoBits = 0; /* needed to byte align the extended data */ 965 966 payloadBits += FDKwriteBits (hBitStream, 1, SI_SBR_EXTENDED_DATA_BITS); 967 FDK_ASSERT(extDataSize <= SBR_EXTENDED_DATA_MAX_CNT); 968 969 if (extDataSize < maxExtSize) { 970 payloadBits += FDKwriteBits (hBitStream, extDataSize, SI_SBR_EXTENSION_SIZE_BITS); 971 } else { 972 payloadBits += FDKwriteBits (hBitStream, maxExtSize, SI_SBR_EXTENSION_SIZE_BITS); 973 payloadBits += FDKwriteBits (hBitStream, extDataSize - maxExtSize, SI_SBR_EXTENSION_ESC_COUNT_BITS); 974 } 975 976 /* parametric coding signalled here? */ 977 if(hParametricStereo){ 978 writtenNoBits += FDKwriteBits (hBitStream, EXTENSION_ID_PS_CODING, SI_SBR_EXTENSION_ID_BITS); 979 writtenNoBits += FDKsbrEnc_PSEnc_WritePSData(hParametricStereo, hBitStream); 980 } 981 982 payloadBits += writtenNoBits; 983 984 /* byte alignment */ 985 writtenNoBits = writtenNoBits%8; 986 if(writtenNoBits) 987 payloadBits += FDKwriteBits(hBitStream, 0, (8 - writtenNoBits)); 988 } else { 989 payloadBits += FDKwriteBits (hBitStream, 0, SI_SBR_EXTENDED_DATA_BITS); 990 } 991 992 return payloadBits; 993 } 994 995 996 /***************************************************************************** 997 998 functionname: writeSyntheticCodingData 999 description: writes bits corresponding to the "synthetic-coding"-extension 1000 returns: number of bits written 1001 input: 1002 output: 1003 1004 *****************************************************************************/ 1005 static INT writeSyntheticCodingData (HANDLE_SBR_ENV_DATA sbrEnvData, 1006 HANDLE_FDK_BITSTREAM hBitStream) 1007 1008 { 1009 INT i; 1010 INT payloadBits = 0; 1011 1012 payloadBits += FDKwriteBits (hBitStream, sbrEnvData->addHarmonicFlag, 1); 1013 1014 if (sbrEnvData->addHarmonicFlag) { 1015 for (i = 0; i < sbrEnvData->noHarmonics; i++) { 1016 payloadBits += FDKwriteBits (hBitStream, sbrEnvData->addHarmonic[i], 1); 1017 } 1018 } 1019 1020 return payloadBits; 1021 } 1022 1023 /***************************************************************************** 1024 1025 functionname: getSbrExtendedDataSize 1026 description: counts the number of bits needed for encoding the 1027 extended data (including extension id) 1028 1029 returns: number of bits needed for the extended data 1030 input: 1031 output: 1032 1033 *****************************************************************************/ 1034 static INT 1035 getSbrExtendedDataSize (HANDLE_PARAMETRIC_STEREO hParametricStereo) 1036 { 1037 INT extDataBits = 0; 1038 1039 /* add your new extended data counting methods here */ 1040 1041 /* 1042 no extended data 1043 */ 1044 1045 if(hParametricStereo){ 1046 /* PS extended data */ 1047 extDataBits += SI_SBR_EXTENSION_ID_BITS; 1048 extDataBits += FDKsbrEnc_PSEnc_WritePSData(hParametricStereo, NULL); 1049 } 1050 1051 return (extDataBits+7) >> 3; 1052 } 1053 1054 1055 1056 1057 1058