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 /**************************** AAC decoder library ****************************** 96 97 Author(s): Robert Weidner (DSP Solutions) 98 99 Description: HCR Decoder: Prepare decoding of non-PCWs, segmentation- and 100 bitfield-handling, HCR-Statemachine 101 102 *******************************************************************************/ 103 104 #include "aacdec_hcrs.h" 105 106 #include "aacdec_hcr.h" 107 108 #include "aacdec_hcr_bit.h" 109 #include "aac_rom.h" 110 #include "aac_ram.h" 111 112 static UINT InitSegmentBitfield(UINT *pNumSegment, 113 SCHAR *pRemainingBitsInSegment, 114 UINT *pSegmentBitfield, 115 UCHAR *pNumWordForBitfield, 116 USHORT *pNumBitValidInLastWord); 117 118 static void InitNonPCWSideInformationForCurrentSet(H_HCR_INFO pHcr); 119 120 static INT ModuloValue(INT input, INT bufferlength); 121 122 static void ClearBitFromBitfield(STATEFUNC *ptrState, UINT offset, 123 UINT *pBitfield); 124 125 /*--------------------------------------------------------------------------------------------- 126 description: This function decodes all non-priority codewords (non-PCWs) by 127 using a state-machine. 128 -------------------------------------------------------------------------------------------- 129 */ 130 void DecodeNonPCWs(HANDLE_FDK_BITSTREAM bs, H_HCR_INFO pHcr) { 131 UINT numValidSegment; 132 INT segmentOffset; 133 INT codewordOffsetBase; 134 INT codewordOffset; 135 UINT trial; 136 137 UINT *pNumSegment; 138 SCHAR *pRemainingBitsInSegment; 139 UINT *pSegmentBitfield; 140 UCHAR *pNumWordForBitfield; 141 USHORT *pNumBitValidInLastWord; 142 UINT *pCodewordBitfield; 143 INT bitfieldWord; 144 INT bitInWord; 145 UINT tempWord; 146 UINT interMediateWord; 147 INT tempBit; 148 INT carry; 149 150 UINT numCodeword; 151 UCHAR numSet; 152 UCHAR currentSet; 153 UINT codewordInSet; 154 UINT remainingCodewordsInSet; 155 SCHAR *pSta; 156 UINT ret; 157 158 pNumSegment = &(pHcr->segmentInfo.numSegment); 159 pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment; 160 pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield; 161 pNumWordForBitfield = &(pHcr->segmentInfo.numWordForBitfield); 162 pNumBitValidInLastWord = &(pHcr->segmentInfo.pNumBitValidInLastWord); 163 pSta = pHcr->nonPcwSideinfo.pSta; 164 165 numValidSegment = InitSegmentBitfield(pNumSegment, pRemainingBitsInSegment, 166 pSegmentBitfield, pNumWordForBitfield, 167 pNumBitValidInLastWord); 168 169 if (numValidSegment != 0) { 170 numCodeword = pHcr->sectionInfo.numCodeword; 171 numSet = ((numCodeword - 1) / *pNumSegment) + 1; 172 173 pHcr->segmentInfo.readDirection = FROM_RIGHT_TO_LEFT; 174 175 /* Process sets subsequently */ 176 for (currentSet = 1; currentSet < numSet; currentSet++) { 177 /* step 1 */ 178 numCodeword -= 179 *pNumSegment; /* number of remaining non PCWs [for all sets] */ 180 if (numCodeword < *pNumSegment) { 181 codewordInSet = numCodeword; /* for last set */ 182 } else { 183 codewordInSet = *pNumSegment; /* for all sets except last set */ 184 } 185 186 /* step 2 */ 187 /* prepare array 'CodewordBitfield'; as much ones are written from left in 188 * all words, as much decodedCodewordInSetCounter nonPCWs exist in this 189 * set */ 190 tempWord = 0xFFFFFFFF; 191 pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield; 192 193 for (bitfieldWord = *pNumWordForBitfield; bitfieldWord != 0; 194 bitfieldWord--) { /* loop over all used words */ 195 if (codewordInSet > NUMBER_OF_BIT_IN_WORD) { /* more codewords than 196 number of bits => fill 197 ones */ 198 /* fill a whole word with ones */ 199 *pCodewordBitfield++ = tempWord; 200 codewordInSet -= NUMBER_OF_BIT_IN_WORD; /* subtract number of bits */ 201 } else { 202 /* prepare last tempWord */ 203 for (remainingCodewordsInSet = codewordInSet; 204 remainingCodewordsInSet < NUMBER_OF_BIT_IN_WORD; 205 remainingCodewordsInSet++) { 206 tempWord = 207 tempWord & 208 ~(1 209 << (NUMBER_OF_BIT_IN_WORD - 1 - 210 remainingCodewordsInSet)); /* set a zero at bit number 211 (NUMBER_OF_BIT_IN_WORD-1-i) 212 in tempWord */ 213 } 214 *pCodewordBitfield++ = tempWord; 215 tempWord = 0x00000000; 216 } 217 } 218 pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield; 219 220 /* step 3 */ 221 /* build non-PCW sideinfo for each non-PCW of the current set */ 222 InitNonPCWSideInformationForCurrentSet(pHcr); 223 224 /* step 4 */ 225 /* decode all non-PCWs belonging to this set */ 226 227 /* loop over trials */ 228 codewordOffsetBase = 0; 229 for (trial = *pNumSegment; trial > 0; trial--) { 230 /* loop over number of words in bitfields */ 231 segmentOffset = 0; /* start at zero in every segment */ 232 pHcr->segmentInfo.segmentOffset = 233 segmentOffset; /* store in structure for states */ 234 codewordOffset = codewordOffsetBase; 235 pHcr->nonPcwSideinfo.codewordOffset = 236 codewordOffset; /* store in structure for states */ 237 238 for (bitfieldWord = 0; bitfieldWord < *pNumWordForBitfield; 239 bitfieldWord++) { 240 /* derive tempWord with bitwise and */ 241 tempWord = 242 pSegmentBitfield[bitfieldWord] & pCodewordBitfield[bitfieldWord]; 243 244 /* if tempWord is not zero, decode something */ 245 if (tempWord != 0) { 246 /* loop over all bits in tempWord; start state machine if & is true 247 */ 248 for (bitInWord = NUMBER_OF_BIT_IN_WORD; bitInWord > 0; 249 bitInWord--) { 250 interMediateWord = ((UINT)1 << (bitInWord - 1)); 251 if ((tempWord & interMediateWord) == interMediateWord) { 252 /* get state and start state machine */ 253 pHcr->nonPcwSideinfo.pState = 254 aStateConstant2State[pSta[codewordOffset]]; 255 256 while (pHcr->nonPcwSideinfo.pState) { 257 ret = ((STATEFUNC)pHcr->nonPcwSideinfo.pState)(bs, pHcr); 258 if (ret != 0) { 259 return; 260 } 261 } 262 } 263 264 /* update both offsets */ 265 segmentOffset += 1; /* add NUMBER_OF_BIT_IN_WORD times one */ 266 pHcr->segmentInfo.segmentOffset = segmentOffset; 267 codewordOffset += 1; /* add NUMBER_OF_BIT_IN_WORD times one */ 268 codewordOffset = 269 ModuloValue(codewordOffset, 270 *pNumSegment); /* index of the current codeword 271 lies within modulo range */ 272 pHcr->nonPcwSideinfo.codewordOffset = codewordOffset; 273 } 274 } else { 275 segmentOffset += 276 NUMBER_OF_BIT_IN_WORD; /* add NUMBER_OF_BIT_IN_WORD at once */ 277 pHcr->segmentInfo.segmentOffset = segmentOffset; 278 codewordOffset += 279 NUMBER_OF_BIT_IN_WORD; /* add NUMBER_OF_BIT_IN_WORD at once */ 280 codewordOffset = ModuloValue( 281 codewordOffset, 282 *pNumSegment); /* index of the current codeword lies within 283 modulo range */ 284 pHcr->nonPcwSideinfo.codewordOffset = codewordOffset; 285 } 286 } /* end of bitfield word loop */ 287 288 /* decrement codeword - pointer */ 289 codewordOffsetBase -= 1; 290 codewordOffsetBase = 291 ModuloValue(codewordOffsetBase, *pNumSegment); /* index of the 292 current codeword 293 base lies within 294 modulo range */ 295 296 /* rotate numSegment bits in codewordBitfield */ 297 /* rotation of *numSegment bits in bitfield of codewords 298 * (circle-rotation) */ 299 /* get last valid bit */ 300 tempBit = pCodewordBitfield[*pNumWordForBitfield - 1] & 301 (1 << (NUMBER_OF_BIT_IN_WORD - *pNumBitValidInLastWord)); 302 tempBit = tempBit >> (NUMBER_OF_BIT_IN_WORD - *pNumBitValidInLastWord); 303 304 /* write zero into place where tempBit was fetched from */ 305 pCodewordBitfield[*pNumWordForBitfield - 1] = 306 pCodewordBitfield[*pNumWordForBitfield - 1] & 307 ~(1 << (NUMBER_OF_BIT_IN_WORD - *pNumBitValidInLastWord)); 308 309 /* rotate last valid word */ 310 pCodewordBitfield[*pNumWordForBitfield - 1] = 311 pCodewordBitfield[*pNumWordForBitfield - 1] >> 1; 312 313 /* transfare carry bit 0 from current word into bitposition 31 from next 314 * word and rotate current word */ 315 for (bitfieldWord = *pNumWordForBitfield - 2; bitfieldWord > -1; 316 bitfieldWord--) { 317 /* get carry (=bit at position 0) from current word */ 318 carry = pCodewordBitfield[bitfieldWord] & 1; 319 320 /* put the carry bit at position 31 into word right from current word 321 */ 322 pCodewordBitfield[bitfieldWord + 1] = 323 pCodewordBitfield[bitfieldWord + 1] | 324 (carry << (NUMBER_OF_BIT_IN_WORD - 1)); 325 326 /* shift current word */ 327 pCodewordBitfield[bitfieldWord] = 328 pCodewordBitfield[bitfieldWord] >> 1; 329 } 330 331 /* put tempBit into free bit-position 31 from first word */ 332 pCodewordBitfield[0] = 333 pCodewordBitfield[0] | (tempBit << (NUMBER_OF_BIT_IN_WORD - 1)); 334 335 } /* end of trial loop */ 336 337 /* toggle read direction */ 338 pHcr->segmentInfo.readDirection = 339 ToggleReadDirection(pHcr->segmentInfo.readDirection); 340 } 341 /* end of set loop */ 342 343 /* all non-PCWs of this spectrum are decoded */ 344 } 345 346 /* all PCWs and all non PCWs are decoded. They are unbacksorted in output 347 * buffer. Here is the Interface with comparing QSCs to asm decoding */ 348 } 349 350 /*--------------------------------------------------------------------------------------------- 351 description: This function prepares the bitfield used for the 352 segments. The list is set up once to be used in all 353 following sets. If a segment is decoded empty, the according bit from the 354 Bitfield is removed. 355 ----------------------------------------------------------------------------------------------- 356 return: numValidSegment = the number of valid segments 357 -------------------------------------------------------------------------------------------- 358 */ 359 static UINT InitSegmentBitfield(UINT *pNumSegment, 360 SCHAR *pRemainingBitsInSegment, 361 UINT *pSegmentBitfield, 362 UCHAR *pNumWordForBitfield, 363 USHORT *pNumBitValidInLastWord) { 364 SHORT i; 365 USHORT r; 366 UCHAR bitfieldWord; 367 UINT tempWord; 368 USHORT numValidSegment; 369 370 *pNumWordForBitfield = 371 (*pNumSegment == 0) 372 ? 0 373 : ((*pNumSegment - 1) >> THIRTYTWO_LOG_DIV_TWO_LOG) + 1; 374 375 /* loop over all words, which are completely used or only partial */ 376 /* bit in pSegmentBitfield is zero if segment is empty; bit in 377 * pSegmentBitfield is one if segment is not empty */ 378 numValidSegment = 0; 379 *pNumBitValidInLastWord = *pNumSegment; 380 381 /* loop over words */ 382 for (bitfieldWord = 0; bitfieldWord < *pNumWordForBitfield - 1; 383 bitfieldWord++) { 384 tempWord = 0xFFFFFFFF; /* set ones */ 385 r = bitfieldWord << THIRTYTWO_LOG_DIV_TWO_LOG; 386 for (i = 0; i < NUMBER_OF_BIT_IN_WORD; i++) { 387 if (pRemainingBitsInSegment[r + i] == 0) { 388 tempWord = tempWord & ~(1 << (NUMBER_OF_BIT_IN_WORD - 1 - 389 i)); /* set a zero at bit number 390 (NUMBER_OF_BIT_IN_WORD-1-i) in 391 tempWord */ 392 } else { 393 numValidSegment += 1; /* count segments which are not empty */ 394 } 395 } 396 pSegmentBitfield[bitfieldWord] = tempWord; /* store result */ 397 *pNumBitValidInLastWord -= NUMBER_OF_BIT_IN_WORD; /* calculate number of 398 zeros on LSB side in 399 the last word */ 400 } 401 402 /* calculate last word: prepare special tempWord */ 403 tempWord = 0xFFFFFFFF; 404 for (i = 0; i < (NUMBER_OF_BIT_IN_WORD - *pNumBitValidInLastWord); i++) { 405 tempWord = tempWord & ~(1 << i); /* clear bit i in tempWord */ 406 } 407 408 /* calculate last word */ 409 r = bitfieldWord << THIRTYTWO_LOG_DIV_TWO_LOG; 410 for (i = 0; i < *pNumBitValidInLastWord; i++) { 411 if (pRemainingBitsInSegment[r + i] == 0) { 412 tempWord = tempWord & ~(1 << (NUMBER_OF_BIT_IN_WORD - 1 - 413 i)); /* set a zero at bit number 414 (NUMBER_OF_BIT_IN_WORD-1-i) in 415 tempWord */ 416 } else { 417 numValidSegment += 1; /* count segments which are not empty */ 418 } 419 } 420 pSegmentBitfield[bitfieldWord] = tempWord; /* store result */ 421 422 return numValidSegment; 423 } 424 425 /*--------------------------------------------------------------------------------------------- 426 description: This function sets up sideinfo for the non-PCW decoder (for the 427 current set). 428 ---------------------------------------------------------------------------------------------*/ 429 static void InitNonPCWSideInformationForCurrentSet(H_HCR_INFO pHcr) { 430 USHORT i, k; 431 UCHAR codebookDim; 432 UINT startNode; 433 434 UCHAR *pCodebook = pHcr->nonPcwSideinfo.pCodebook; 435 UINT *iNode = pHcr->nonPcwSideinfo.iNode; 436 UCHAR *pCntSign = pHcr->nonPcwSideinfo.pCntSign; 437 USHORT *iResultPointer = pHcr->nonPcwSideinfo.iResultPointer; 438 UINT *pEscapeSequenceInfo = pHcr->nonPcwSideinfo.pEscapeSequenceInfo; 439 SCHAR *pSta = pHcr->nonPcwSideinfo.pSta; 440 USHORT *pNumExtendedSortedCodewordInSection = 441 pHcr->sectionInfo.pNumExtendedSortedCodewordInSection; 442 int numExtendedSortedCodewordInSectionIdx = 443 pHcr->sectionInfo.numExtendedSortedCodewordInSectionIdx; 444 UCHAR *pExtendedSortedCodebook = pHcr->sectionInfo.pExtendedSortedCodebook; 445 int extendedSortedCodebookIdx = pHcr->sectionInfo.extendedSortedCodebookIdx; 446 USHORT *pNumExtendedSortedSectionsInSets = 447 pHcr->sectionInfo.pNumExtendedSortedSectionsInSets; 448 int numExtendedSortedSectionsInSetsIdx = 449 pHcr->sectionInfo.numExtendedSortedSectionsInSetsIdx; 450 int quantizedSpectralCoefficientsIdx = 451 pHcr->decInOut.quantizedSpectralCoefficientsIdx; 452 const UCHAR *pCbDimension = aDimCb; 453 int iterationCounter = 0; 454 455 /* loop over number of extended sorted sections in the current set so all 456 * codewords sideinfo variables within this set can be prepared for decoding 457 */ 458 for (i = pNumExtendedSortedSectionsInSets[numExtendedSortedSectionsInSetsIdx]; 459 i != 0; i--) { 460 codebookDim = 461 pCbDimension[pExtendedSortedCodebook[extendedSortedCodebookIdx]]; 462 startNode = *aHuffTable[pExtendedSortedCodebook[extendedSortedCodebookIdx]]; 463 464 for (k = pNumExtendedSortedCodewordInSection 465 [numExtendedSortedCodewordInSectionIdx]; 466 k != 0; k--) { 467 iterationCounter++; 468 if (iterationCounter > (1024 >> 2)) { 469 return; 470 } 471 *pSta++ = aCodebook2StartInt 472 [pExtendedSortedCodebook[extendedSortedCodebookIdx]]; 473 *pCodebook++ = pExtendedSortedCodebook[extendedSortedCodebookIdx]; 474 *iNode++ = startNode; 475 *pCntSign++ = 0; 476 *iResultPointer++ = quantizedSpectralCoefficientsIdx; 477 *pEscapeSequenceInfo++ = 0; 478 quantizedSpectralCoefficientsIdx += 479 codebookDim; /* update pointer by codebookDim --> point to next 480 starting value for writing out */ 481 if (quantizedSpectralCoefficientsIdx >= 1024) { 482 return; 483 } 484 } 485 numExtendedSortedCodewordInSectionIdx++; /* inc ptr for next ext sort sec in 486 current set */ 487 extendedSortedCodebookIdx++; /* inc ptr for next ext sort sec in current set 488 */ 489 if (numExtendedSortedCodewordInSectionIdx >= (MAX_SFB_HCR + MAX_HCR_SETS) || 490 extendedSortedCodebookIdx >= (MAX_SFB_HCR + MAX_HCR_SETS)) { 491 return; 492 } 493 } 494 numExtendedSortedSectionsInSetsIdx++; /* inc ptr for next set of non-PCWs */ 495 if (numExtendedSortedCodewordInSectionIdx >= (MAX_SFB_HCR + MAX_HCR_SETS)) { 496 return; 497 } 498 499 /* Write back indexes */ 500 pHcr->sectionInfo.numExtendedSortedCodewordInSectionIdx = 501 numExtendedSortedCodewordInSectionIdx; 502 pHcr->sectionInfo.extendedSortedCodebookIdx = extendedSortedCodebookIdx; 503 pHcr->sectionInfo.numExtendedSortedSectionsInSetsIdx = 504 numExtendedSortedSectionsInSetsIdx; 505 pHcr->sectionInfo.numExtendedSortedCodewordInSectionIdx = 506 numExtendedSortedCodewordInSectionIdx; 507 pHcr->decInOut.quantizedSpectralCoefficientsIdx = 508 quantizedSpectralCoefficientsIdx; 509 } 510 511 /*--------------------------------------------------------------------------------------------- 512 description: This function returns the input value if the value is in the 513 range of bufferlength. If <input> is smaller, one bufferlength 514 is added, if <input> is bigger one bufferlength is subtracted. 515 ----------------------------------------------------------------------------------------------- 516 return: modulo result 517 -------------------------------------------------------------------------------------------- 518 */ 519 static INT ModuloValue(INT input, INT bufferlength) { 520 if (input > (bufferlength - 1)) { 521 return (input - bufferlength); 522 } 523 if (input < 0) { 524 return (input + bufferlength); 525 } 526 return input; 527 } 528 529 /*--------------------------------------------------------------------------------------------- 530 description: This function clears a bit from current bitfield and 531 switches off the statemachine. 532 533 A bit is cleared in two cases: 534 a) a codeword is decoded, then a bit is cleared in codeword 535 bitfield b) a segment is decoded empty, then a bit is cleared in segment 536 bitfield 537 -------------------------------------------------------------------------------------------- 538 */ 539 static void ClearBitFromBitfield(STATEFUNC *ptrState, UINT offset, 540 UINT *pBitfield) { 541 UINT numBitfieldWord; 542 UINT numBitfieldBit; 543 544 /* get both values needed for clearing the bit */ 545 numBitfieldWord = offset >> THIRTYTWO_LOG_DIV_TWO_LOG; /* int = wordNr */ 546 numBitfieldBit = offset - (numBitfieldWord 547 << THIRTYTWO_LOG_DIV_TWO_LOG); /* fract = bitNr */ 548 549 /* clear a bit in bitfield */ 550 pBitfield[numBitfieldWord] = 551 pBitfield[numBitfieldWord] & 552 ~(1 << (NUMBER_OF_BIT_IN_WORD - 1 - numBitfieldBit)); 553 554 /* switch off state machine because codeword is decoded and/or because segment 555 * is empty */ 556 *ptrState = NULL; 557 } 558 559 /* ========================================================================================= 560 the states of the statemachine 561 ========================================================================================= 562 */ 563 564 /*--------------------------------------------------------------------------------------------- 565 description: Decodes the body of a codeword. This State is used for 566 codebooks 1,2,5 and 6. No sign bits are decoded, because the table of the 567 quantized spectral values has got a valid sign at the quantized spectral lines. 568 ----------------------------------------------------------------------------------------------- 569 output: Two or four quantizes spectral values written at position 570 where pResultPointr points to 571 ----------------------------------------------------------------------------------------------- 572 return: 0 573 -------------------------------------------------------------------------------------------- 574 */ 575 UINT Hcr_State_BODY_ONLY(HANDLE_FDK_BITSTREAM bs, void *ptr) { 576 H_HCR_INFO pHcr = (H_HCR_INFO)ptr; 577 UINT *pSegmentBitfield; 578 UINT *pCodewordBitfield; 579 UINT segmentOffset; 580 FIXP_DBL *pResultBase; 581 UINT *iNode; 582 USHORT *iResultPointer; 583 UINT codewordOffset; 584 UINT branchNode; 585 UINT branchValue; 586 UINT iQSC; 587 UINT treeNode; 588 UCHAR carryBit; 589 INT *pLeftStartOfSegment; 590 INT *pRightStartOfSegment; 591 SCHAR *pRemainingBitsInSegment; 592 UCHAR readDirection; 593 UCHAR *pCodebook; 594 UCHAR dimCntr; 595 const UINT *pCurrentTree; 596 const UCHAR *pCbDimension; 597 const SCHAR *pQuantVal; 598 const SCHAR *pQuantValBase; 599 600 pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment; 601 pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment; 602 pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment; 603 readDirection = pHcr->segmentInfo.readDirection; 604 pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield; 605 pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield; 606 segmentOffset = pHcr->segmentInfo.segmentOffset; 607 608 pCodebook = pHcr->nonPcwSideinfo.pCodebook; 609 iNode = pHcr->nonPcwSideinfo.iNode; 610 pResultBase = pHcr->nonPcwSideinfo.pResultBase; 611 iResultPointer = pHcr->nonPcwSideinfo.iResultPointer; 612 codewordOffset = pHcr->nonPcwSideinfo.codewordOffset; 613 614 pCbDimension = aDimCb; 615 616 treeNode = iNode[codewordOffset]; 617 pCurrentTree = aHuffTable[pCodebook[codewordOffset]]; 618 619 for (; pRemainingBitsInSegment[segmentOffset] > 0; 620 pRemainingBitsInSegment[segmentOffset] -= 1) { 621 carryBit = HcrGetABitFromBitstream( 622 bs, pHcr->decInOut.bitstreamAnchor, &pLeftStartOfSegment[segmentOffset], 623 &pRightStartOfSegment[segmentOffset], readDirection); 624 625 CarryBitToBranchValue(carryBit, /* make a step in decoding tree */ 626 treeNode, &branchValue, &branchNode); 627 628 /* if end of branch reached write out lines and count bits needed for sign, 629 * otherwise store node in codeword sideinfo */ 630 if ((branchNode & TEST_BIT_10) == 631 TEST_BIT_10) { /* test bit 10 ; ==> body is complete */ 632 pQuantValBase = aQuantTable[pCodebook[codewordOffset]]; /* get base 633 address of 634 quantized 635 values 636 belonging to 637 current 638 codebook */ 639 pQuantVal = pQuantValBase + branchValue; /* set pointer to first valid 640 line [of 2 or 4 quantized 641 values] */ 642 643 iQSC = iResultPointer[codewordOffset]; /* get position of first line for 644 writing out result */ 645 646 for (dimCntr = pCbDimension[pCodebook[codewordOffset]]; dimCntr != 0; 647 dimCntr--) { 648 pResultBase[iQSC++] = 649 (FIXP_DBL)*pQuantVal++; /* write out 2 or 4 lines into 650 spectrum; no Sign bits 651 available in this state */ 652 } 653 654 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 655 pCodewordBitfield); /* clear a bit in bitfield and 656 switch off statemachine */ 657 pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of 658 for loop counter (see 659 above) is done here */ 660 break; /* end of branch in tree reached i.e. a whole nonPCW-Body is 661 decoded */ 662 } else { /* body is not decoded completely: */ 663 treeNode = *( 664 pCurrentTree + 665 branchValue); /* update treeNode for further step in decoding tree */ 666 } 667 } 668 iNode[codewordOffset] = treeNode; /* store updated treeNode because maybe 669 decoding of codeword body not finished 670 yet */ 671 672 if (pRemainingBitsInSegment[segmentOffset] <= 0) { 673 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 674 pSegmentBitfield); /* clear a bit in bitfield and 675 switch off statemachine */ 676 677 if (pRemainingBitsInSegment[segmentOffset] < 0) { 678 pHcr->decInOut.errorLog |= STATE_ERROR_BODY_ONLY; 679 return BODY_ONLY; 680 } 681 } 682 683 return STOP_THIS_STATE; 684 } 685 686 /*--------------------------------------------------------------------------------------------- 687 description: Decodes the codeword body, writes out result and counts the 688 number of quantized spectral values, which are different form zero. For those 689 values sign bits are needed. 690 691 If sign bit counter cntSign is different from zero, switch to 692 next state to decode sign Bits there. If sign bit counter cntSign is zero, no 693 sign bits are needed and codeword is decoded. 694 ----------------------------------------------------------------------------------------------- 695 output: Two or four written quantizes spectral values written at 696 position where pResultPointr points to. The signs of those lines may be wrong. 697 If the signs [on just one signle sign] is wrong, the next state will correct it. 698 ----------------------------------------------------------------------------------------------- 699 return: 0 700 -------------------------------------------------------------------------------------------- 701 */ 702 UINT Hcr_State_BODY_SIGN__BODY(HANDLE_FDK_BITSTREAM bs, void *ptr) { 703 H_HCR_INFO pHcr = (H_HCR_INFO)ptr; 704 SCHAR *pRemainingBitsInSegment; 705 INT *pLeftStartOfSegment; 706 INT *pRightStartOfSegment; 707 UCHAR readDirection; 708 UINT *pSegmentBitfield; 709 UINT *pCodewordBitfield; 710 UINT segmentOffset; 711 712 UCHAR *pCodebook; 713 UINT *iNode; 714 UCHAR *pCntSign; 715 FIXP_DBL *pResultBase; 716 USHORT *iResultPointer; 717 UINT codewordOffset; 718 719 UINT iQSC; 720 UINT cntSign; 721 UCHAR dimCntr; 722 UCHAR carryBit; 723 SCHAR *pSta; 724 UINT treeNode; 725 UINT branchValue; 726 UINT branchNode; 727 const UCHAR *pCbDimension; 728 const UINT *pCurrentTree; 729 const SCHAR *pQuantValBase; 730 const SCHAR *pQuantVal; 731 732 pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment; 733 pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment; 734 pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment; 735 readDirection = pHcr->segmentInfo.readDirection; 736 pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield; 737 pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield; 738 segmentOffset = pHcr->segmentInfo.segmentOffset; 739 740 pCodebook = pHcr->nonPcwSideinfo.pCodebook; 741 iNode = pHcr->nonPcwSideinfo.iNode; 742 pCntSign = pHcr->nonPcwSideinfo.pCntSign; 743 pResultBase = pHcr->nonPcwSideinfo.pResultBase; 744 iResultPointer = pHcr->nonPcwSideinfo.iResultPointer; 745 codewordOffset = pHcr->nonPcwSideinfo.codewordOffset; 746 pSta = pHcr->nonPcwSideinfo.pSta; 747 748 pCbDimension = aDimCb; 749 750 treeNode = iNode[codewordOffset]; 751 pCurrentTree = aHuffTable[pCodebook[codewordOffset]]; 752 753 for (; pRemainingBitsInSegment[segmentOffset] > 0; 754 pRemainingBitsInSegment[segmentOffset] -= 1) { 755 carryBit = HcrGetABitFromBitstream( 756 bs, pHcr->decInOut.bitstreamAnchor, &pLeftStartOfSegment[segmentOffset], 757 &pRightStartOfSegment[segmentOffset], readDirection); 758 759 CarryBitToBranchValue(carryBit, /* make a step in decoding tree */ 760 treeNode, &branchValue, &branchNode); 761 762 /* if end of branch reached write out lines and count bits needed for sign, 763 * otherwise store node in codeword sideinfo */ 764 if ((branchNode & TEST_BIT_10) == 765 TEST_BIT_10) { /* test bit 10 ; if set body complete */ 766 /* body completely decoded; branchValue is valid, set pQuantVal to first 767 * (of two or four) quantized spectral coefficients */ 768 pQuantValBase = aQuantTable[pCodebook[codewordOffset]]; /* get base 769 address of 770 quantized 771 values 772 belonging to 773 current 774 codebook */ 775 pQuantVal = pQuantValBase + branchValue; /* set pointer to first valid 776 line [of 2 or 4 quantized 777 values] */ 778 779 iQSC = iResultPointer[codewordOffset]; /* get position of first line for 780 writing result */ 781 782 /* codeword decoding result is written out here: Write out 2 or 4 783 * quantized spectral values with probably */ 784 /* wrong sign and count number of values which are different from zero for 785 * sign bit decoding [which happens in next state] */ 786 cntSign = 0; 787 for (dimCntr = pCbDimension[pCodebook[codewordOffset]]; dimCntr != 0; 788 dimCntr--) { 789 pResultBase[iQSC++] = 790 (FIXP_DBL)*pQuantVal; /* write quant. spec. coef. into spectrum */ 791 if (*pQuantVal++ != 0) { 792 cntSign += 1; 793 } 794 } 795 796 if (cntSign == 0) { 797 ClearBitFromBitfield( 798 &(pHcr->nonPcwSideinfo.pState), segmentOffset, 799 pCodewordBitfield); /* clear a bit in bitfield and switch off 800 statemachine */ 801 } else { 802 pCntSign[codewordOffset] = cntSign; /* write sign count result into 803 codewordsideinfo of current 804 codeword */ 805 pSta[codewordOffset] = BODY_SIGN__SIGN; /* change state */ 806 pHcr->nonPcwSideinfo.pState = 807 aStateConstant2State[pSta[codewordOffset]]; /* get state from 808 separate array of 809 cw-sideinfo */ 810 } 811 pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of 812 for loop counter (see 813 above) is done here */ 814 break; /* end of branch in tree reached i.e. a whole nonPCW-Body is 815 decoded */ 816 } else { /* body is not decoded completely: */ 817 treeNode = *( 818 pCurrentTree + 819 branchValue); /* update treeNode for further step in decoding tree */ 820 } 821 } 822 iNode[codewordOffset] = treeNode; /* store updated treeNode because maybe 823 decoding of codeword body not finished 824 yet */ 825 826 if (pRemainingBitsInSegment[segmentOffset] <= 0) { 827 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 828 pSegmentBitfield); /* clear a bit in bitfield and 829 switch off statemachine */ 830 831 if (pRemainingBitsInSegment[segmentOffset] < 0) { 832 pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN__BODY; 833 return BODY_SIGN__BODY; 834 } 835 } 836 837 return STOP_THIS_STATE; 838 } 839 840 /*--------------------------------------------------------------------------------------------- 841 description: This state decodes the sign bits belonging to a codeword. The 842 state is called as often in different "trials" until pCntSgn[codewordOffset] is 843 zero. 844 ----------------------------------------------------------------------------------------------- 845 output: The two or four quantizes spectral values (written in previous 846 state) have now the correct sign. 847 ----------------------------------------------------------------------------------------------- 848 return: 0 849 -------------------------------------------------------------------------------------------- 850 */ 851 UINT Hcr_State_BODY_SIGN__SIGN(HANDLE_FDK_BITSTREAM bs, void *ptr) { 852 H_HCR_INFO pHcr = (H_HCR_INFO)ptr; 853 SCHAR *pRemainingBitsInSegment; 854 INT *pLeftStartOfSegment; 855 INT *pRightStartOfSegment; 856 UCHAR readDirection; 857 UINT *pSegmentBitfield; 858 UINT *pCodewordBitfield; 859 UINT segmentOffset; 860 861 UCHAR *pCntSign; 862 FIXP_DBL *pResultBase; 863 USHORT *iResultPointer; 864 UINT codewordOffset; 865 866 UCHAR carryBit; 867 UINT iQSC; 868 UCHAR cntSign; 869 870 pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment; 871 pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment; 872 pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment; 873 readDirection = pHcr->segmentInfo.readDirection; 874 pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield; 875 pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield; 876 segmentOffset = pHcr->segmentInfo.segmentOffset; 877 878 /*pCodebook = */ 879 pCntSign = pHcr->nonPcwSideinfo.pCntSign; 880 pResultBase = pHcr->nonPcwSideinfo.pResultBase; 881 iResultPointer = pHcr->nonPcwSideinfo.iResultPointer; 882 codewordOffset = pHcr->nonPcwSideinfo.codewordOffset; 883 884 iQSC = iResultPointer[codewordOffset]; 885 cntSign = pCntSign[codewordOffset]; 886 887 /* loop for sign bit decoding */ 888 for (; pRemainingBitsInSegment[segmentOffset] > 0; 889 pRemainingBitsInSegment[segmentOffset] -= 1) { 890 carryBit = HcrGetABitFromBitstream( 891 bs, pHcr->decInOut.bitstreamAnchor, &pLeftStartOfSegment[segmentOffset], 892 &pRightStartOfSegment[segmentOffset], readDirection); 893 cntSign -= 894 1; /* decrement sign counter because one sign bit has been read */ 895 896 /* search for a line (which was decoded in previous state) which is not 897 * zero. [This value will get a sign] */ 898 while (pResultBase[iQSC] == (FIXP_DBL)0) { 899 if (++iQSC >= 1024) { /* points to current value different from zero */ 900 return BODY_SIGN__SIGN; 901 } 902 } 903 904 /* put sign together with line; if carryBit is zero, the sign is ok already; 905 * no write operation necessary in this case */ 906 if (carryBit != 0) { 907 pResultBase[iQSC] = -pResultBase[iQSC]; /* carryBit = 1 --> minus */ 908 } 909 910 iQSC++; /* update pointer to next (maybe valid) value */ 911 912 if (cntSign == 0) { /* if (cntSign==0) ==> set state CODEWORD_DECODED */ 913 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 914 pCodewordBitfield); /* clear a bit in bitfield and 915 switch off statemachine */ 916 pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of 917 for loop counter (see 918 above) is done here */ 919 break; /* whole nonPCW-Body and according sign bits are decoded */ 920 } 921 } 922 pCntSign[codewordOffset] = cntSign; 923 iResultPointer[codewordOffset] = iQSC; /* store updated pResultPointer */ 924 925 if (pRemainingBitsInSegment[segmentOffset] <= 0) { 926 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 927 pSegmentBitfield); /* clear a bit in bitfield and 928 switch off statemachine */ 929 930 if (pRemainingBitsInSegment[segmentOffset] < 0) { 931 pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN__SIGN; 932 return BODY_SIGN__SIGN; 933 } 934 } 935 936 return STOP_THIS_STATE; 937 } 938 939 /*--------------------------------------------------------------------------------------------- 940 description: Decodes the codeword body in case of codebook is 11. Writes 941 out resulting two or four lines [with probably wrong sign] and counts the number 942 of lines, which are different form zero. This information is needed in next 943 state where sign bits will be decoded, if necessary. 944 If sign bit counter cntSign is zero, no sign bits are needed 945 and codeword is decoded completely. 946 ----------------------------------------------------------------------------------------------- 947 output: Two lines (quantizes spectral coefficients) which are probably 948 wrong. The sign may be wrong and if one or two values is/are 16, the following 949 states will decode the escape sequence to correct the values which are wirtten 950 here. 951 ----------------------------------------------------------------------------------------------- 952 return: 0 953 -------------------------------------------------------------------------------------------- 954 */ 955 UINT Hcr_State_BODY_SIGN_ESC__BODY(HANDLE_FDK_BITSTREAM bs, void *ptr) { 956 H_HCR_INFO pHcr = (H_HCR_INFO)ptr; 957 SCHAR *pRemainingBitsInSegment; 958 INT *pLeftStartOfSegment; 959 INT *pRightStartOfSegment; 960 UCHAR readDirection; 961 UINT *pSegmentBitfield; 962 UINT *pCodewordBitfield; 963 UINT segmentOffset; 964 965 UINT *iNode; 966 UCHAR *pCntSign; 967 FIXP_DBL *pResultBase; 968 USHORT *iResultPointer; 969 UINT codewordOffset; 970 971 UCHAR carryBit; 972 UINT iQSC; 973 UINT cntSign; 974 UINT dimCntr; 975 UINT treeNode; 976 SCHAR *pSta; 977 UINT branchNode; 978 UINT branchValue; 979 const UINT *pCurrentTree; 980 const SCHAR *pQuantValBase; 981 const SCHAR *pQuantVal; 982 983 pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment; 984 pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment; 985 pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment; 986 readDirection = pHcr->segmentInfo.readDirection; 987 pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield; 988 pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield; 989 segmentOffset = pHcr->segmentInfo.segmentOffset; 990 991 iNode = pHcr->nonPcwSideinfo.iNode; 992 pCntSign = pHcr->nonPcwSideinfo.pCntSign; 993 pResultBase = pHcr->nonPcwSideinfo.pResultBase; 994 iResultPointer = pHcr->nonPcwSideinfo.iResultPointer; 995 codewordOffset = pHcr->nonPcwSideinfo.codewordOffset; 996 pSta = pHcr->nonPcwSideinfo.pSta; 997 998 treeNode = iNode[codewordOffset]; 999 pCurrentTree = aHuffTable[ESCAPE_CODEBOOK]; 1000 1001 for (; pRemainingBitsInSegment[segmentOffset] > 0; 1002 pRemainingBitsInSegment[segmentOffset] -= 1) { 1003 carryBit = HcrGetABitFromBitstream( 1004 bs, pHcr->decInOut.bitstreamAnchor, &pLeftStartOfSegment[segmentOffset], 1005 &pRightStartOfSegment[segmentOffset], readDirection); 1006 1007 /* make a step in tree */ 1008 CarryBitToBranchValue(carryBit, treeNode, &branchValue, &branchNode); 1009 1010 /* if end of branch reached write out lines and count bits needed for sign, 1011 * otherwise store node in codeword sideinfo */ 1012 if ((branchNode & TEST_BIT_10) == 1013 TEST_BIT_10) { /* test bit 10 ; if set body complete */ 1014 1015 /* body completely decoded; branchValue is valid */ 1016 /* set pQuantVol to first (of two or four) quantized spectral coefficients 1017 */ 1018 pQuantValBase = aQuantTable[ESCAPE_CODEBOOK]; /* get base address of 1019 quantized values 1020 belonging to current 1021 codebook */ 1022 pQuantVal = pQuantValBase + branchValue; /* set pointer to first valid 1023 line [of 2 or 4 quantized 1024 values] */ 1025 1026 /* make backup from original resultPointer in node storage for state 1027 * BODY_SIGN_ESC__SIGN */ 1028 iNode[codewordOffset] = iResultPointer[codewordOffset]; 1029 1030 /* get position of first line for writing result */ 1031 iQSC = iResultPointer[codewordOffset]; 1032 1033 /* codeword decoding result is written out here: Write out 2 or 4 1034 * quantized spectral values with probably */ 1035 /* wrong sign and count number of values which are different from zero for 1036 * sign bit decoding [which happens in next state] */ 1037 cntSign = 0; 1038 1039 for (dimCntr = DIMENSION_OF_ESCAPE_CODEBOOK; dimCntr != 0; dimCntr--) { 1040 pResultBase[iQSC++] = 1041 (FIXP_DBL)*pQuantVal; /* write quant. spec. coef. into spectrum */ 1042 if (*pQuantVal++ != 0) { 1043 cntSign += 1; 1044 } 1045 } 1046 1047 if (cntSign == 0) { 1048 ClearBitFromBitfield( 1049 &(pHcr->nonPcwSideinfo.pState), segmentOffset, 1050 pCodewordBitfield); /* clear a bit in bitfield and switch off 1051 statemachine */ 1052 /* codeword decoded */ 1053 } else { 1054 /* write sign count result into codewordsideinfo of current codeword */ 1055 pCntSign[codewordOffset] = cntSign; 1056 pSta[codewordOffset] = BODY_SIGN_ESC__SIGN; /* change state */ 1057 pHcr->nonPcwSideinfo.pState = 1058 aStateConstant2State[pSta[codewordOffset]]; /* get state from 1059 separate array of 1060 cw-sideinfo */ 1061 } 1062 pRemainingBitsInSegment[segmentOffset] -= 1; /* the last reinitialzation 1063 of for loop counter (see 1064 above) is done here */ 1065 break; /* end of branch in tree reached i.e. a whole nonPCW-Body is 1066 decoded */ 1067 } else { /* body is not decoded completely: */ 1068 /* update treeNode for further step in decoding tree and store updated 1069 * treeNode because maybe no more bits left in segment */ 1070 treeNode = *(pCurrentTree + branchValue); 1071 iNode[codewordOffset] = treeNode; 1072 } 1073 } 1074 1075 if (pRemainingBitsInSegment[segmentOffset] <= 0) { 1076 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 1077 pSegmentBitfield); /* clear a bit in bitfield and 1078 switch off statemachine */ 1079 1080 if (pRemainingBitsInSegment[segmentOffset] < 0) { 1081 pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN_ESC__BODY; 1082 return BODY_SIGN_ESC__BODY; 1083 } 1084 } 1085 1086 return STOP_THIS_STATE; 1087 } 1088 1089 /*--------------------------------------------------------------------------------------------- 1090 description: This state decodes the sign bits, if a codeword of codebook 11 1091 needs some. A flag named 'flagB' in codeword sideinfo is set, if the second line 1092 of quantized spectral values is 16. The 'flagB' is used in case of decoding of a 1093 escape sequence is necessary as far as the second line is concerned. 1094 1095 If only the first line needs an escape sequence, the flagB is 1096 cleared. If only the second line needs an escape sequence, the flagB is not 1097 used. 1098 1099 For storing sideinfo in case of escape sequence decoding one 1100 single word can be used for both escape sequences because they are decoded not 1101 at the same time: 1102 1103 1104 bit 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 1105 4 3 2 1 0 1106 ===== == == =========== =========== 1107 =================================== ^ ^ ^ ^ ^ 1108 ^ | | | | | | res. flagA flagB 1109 escapePrefixUp escapePrefixDown escapeWord 1110 1111 ----------------------------------------------------------------------------------------------- 1112 output: Two lines with correct sign. If one or two values is/are 16, 1113 the lines are not valid, otherwise they are. 1114 ----------------------------------------------------------------------------------------------- 1115 return: 0 1116 -------------------------------------------------------------------------------------------- 1117 */ 1118 UINT Hcr_State_BODY_SIGN_ESC__SIGN(HANDLE_FDK_BITSTREAM bs, void *ptr) { 1119 H_HCR_INFO pHcr = (H_HCR_INFO)ptr; 1120 SCHAR *pRemainingBitsInSegment; 1121 INT *pLeftStartOfSegment; 1122 INT *pRightStartOfSegment; 1123 UCHAR readDirection; 1124 UINT *pSegmentBitfield; 1125 UINT *pCodewordBitfield; 1126 UINT segmentOffset; 1127 1128 UINT *iNode; 1129 UCHAR *pCntSign; 1130 FIXP_DBL *pResultBase; 1131 USHORT *iResultPointer; 1132 UINT *pEscapeSequenceInfo; 1133 UINT codewordOffset; 1134 1135 UINT iQSC; 1136 UCHAR cntSign; 1137 UINT flagA; 1138 UINT flagB; 1139 UINT flags; 1140 UCHAR carryBit; 1141 SCHAR *pSta; 1142 1143 pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment; 1144 pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment; 1145 pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment; 1146 readDirection = pHcr->segmentInfo.readDirection; 1147 pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield; 1148 pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield; 1149 segmentOffset = pHcr->segmentInfo.segmentOffset; 1150 1151 iNode = pHcr->nonPcwSideinfo.iNode; 1152 pCntSign = pHcr->nonPcwSideinfo.pCntSign; 1153 pResultBase = pHcr->nonPcwSideinfo.pResultBase; 1154 iResultPointer = pHcr->nonPcwSideinfo.iResultPointer; 1155 pEscapeSequenceInfo = pHcr->nonPcwSideinfo.pEscapeSequenceInfo; 1156 codewordOffset = pHcr->nonPcwSideinfo.codewordOffset; 1157 pSta = pHcr->nonPcwSideinfo.pSta; 1158 1159 iQSC = iResultPointer[codewordOffset]; 1160 cntSign = pCntSign[codewordOffset]; 1161 1162 /* loop for sign bit decoding */ 1163 for (; pRemainingBitsInSegment[segmentOffset] > 0; 1164 pRemainingBitsInSegment[segmentOffset] -= 1) { 1165 carryBit = HcrGetABitFromBitstream( 1166 bs, pHcr->decInOut.bitstreamAnchor, &pLeftStartOfSegment[segmentOffset], 1167 &pRightStartOfSegment[segmentOffset], readDirection); 1168 1169 /* decrement sign counter because one sign bit has been read */ 1170 cntSign -= 1; 1171 pCntSign[codewordOffset] = cntSign; 1172 1173 /* get a quantized spectral value (which was decoded in previous state) 1174 * which is not zero. [This value will get a sign] */ 1175 while (pResultBase[iQSC] == (FIXP_DBL)0) { 1176 if (++iQSC >= 1024) { 1177 return BODY_SIGN_ESC__SIGN; 1178 } 1179 } 1180 iResultPointer[codewordOffset] = iQSC; 1181 1182 /* put negative sign together with quantized spectral value; if carryBit is 1183 * zero, the sign is ok already; no write operation necessary in this case 1184 */ 1185 if (carryBit != 0) { 1186 pResultBase[iQSC] = -pResultBase[iQSC]; /* carryBit = 1 --> minus */ 1187 } 1188 iQSC++; /* update index to next (maybe valid) value */ 1189 iResultPointer[codewordOffset] = iQSC; 1190 1191 if (cntSign == 0) { 1192 /* all sign bits are decoded now */ 1193 pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of 1194 for loop counter (see 1195 above) is done here */ 1196 1197 /* check decoded values if codeword is decoded: Check if one or two escape 1198 * sequences 16 follow */ 1199 1200 /* step 0 */ 1201 /* restore pointer to first decoded quantized value [ = original 1202 * pResultPointr] from index iNode prepared in State_BODY_SIGN_ESC__BODY 1203 */ 1204 iQSC = iNode[codewordOffset]; 1205 1206 /* step 1 */ 1207 /* test first value if escape sequence follows */ 1208 flagA = 0; /* for first possible escape sequence */ 1209 if (fixp_abs(pResultBase[iQSC++]) == (FIXP_DBL)ESCAPE_VALUE) { 1210 flagA = 1; 1211 } 1212 1213 /* step 2 */ 1214 /* test second value if escape sequence follows */ 1215 flagB = 0; /* for second possible escape sequence */ 1216 if (fixp_abs(pResultBase[iQSC]) == (FIXP_DBL)ESCAPE_VALUE) { 1217 flagB = 1; 1218 } 1219 1220 /* step 3 */ 1221 /* evaluate flag result and go on if necessary */ 1222 if (!flagA && !flagB) { 1223 ClearBitFromBitfield( 1224 &(pHcr->nonPcwSideinfo.pState), segmentOffset, 1225 pCodewordBitfield); /* clear a bit in bitfield and switch off 1226 statemachine */ 1227 } else { 1228 /* at least one of two lines is 16 */ 1229 /* store both flags at correct positions in non PCW codeword sideinfo 1230 * pEscapeSequenceInfo[codewordOffset] */ 1231 flags = flagA << POSITION_OF_FLAG_A; 1232 flags |= (flagB << POSITION_OF_FLAG_B); 1233 pEscapeSequenceInfo[codewordOffset] = flags; 1234 1235 /* set next state */ 1236 pSta[codewordOffset] = BODY_SIGN_ESC__ESC_PREFIX; 1237 pHcr->nonPcwSideinfo.pState = 1238 aStateConstant2State[pSta[codewordOffset]]; /* get state from 1239 separate array of 1240 cw-sideinfo */ 1241 1242 /* set result pointer to the first line of the two decoded lines */ 1243 iResultPointer[codewordOffset] = iNode[codewordOffset]; 1244 1245 if (!flagA && flagB) { 1246 /* update pResultPointr ==> state Stat_BODY_SIGN_ESC__ESC_WORD writes 1247 * to correct position. Second value is the one and only escape value 1248 */ 1249 iQSC = iResultPointer[codewordOffset]; 1250 iQSC++; 1251 iResultPointer[codewordOffset] = iQSC; 1252 } 1253 1254 } /* at least one of two lines is 16 */ 1255 break; /* nonPCW-Body at cb 11 and according sign bits are decoded */ 1256 1257 } /* if ( cntSign == 0 ) */ 1258 } /* loop over remaining Bits in segment */ 1259 1260 if (pRemainingBitsInSegment[segmentOffset] <= 0) { 1261 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 1262 pSegmentBitfield); /* clear a bit in bitfield and 1263 switch off statemachine */ 1264 1265 if (pRemainingBitsInSegment[segmentOffset] < 0) { 1266 pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN_ESC__SIGN; 1267 return BODY_SIGN_ESC__SIGN; 1268 } 1269 } 1270 return STOP_THIS_STATE; 1271 } 1272 1273 /*--------------------------------------------------------------------------------------------- 1274 description: Decode escape prefix of first or second escape sequence. The 1275 escape prefix consists of ones. The following zero is also decoded here. 1276 ----------------------------------------------------------------------------------------------- 1277 output: If the single separator-zero which follows the 1278 escape-prefix-ones is not yet decoded: The value 'escapePrefixUp' in word 1279 pEscapeSequenceInfo[codewordOffset] is updated. 1280 1281 If the single separator-zero which follows the 1282 escape-prefix-ones is decoded: Two updated values 'escapePrefixUp' and 1283 'escapePrefixDown' in word pEscapeSequenceInfo[codewordOffset]. This State is 1284 finished. Switch to next state. 1285 ----------------------------------------------------------------------------------------------- 1286 return: 0 1287 -------------------------------------------------------------------------------------------- 1288 */ 1289 UINT Hcr_State_BODY_SIGN_ESC__ESC_PREFIX(HANDLE_FDK_BITSTREAM bs, void *ptr) { 1290 H_HCR_INFO pHcr = (H_HCR_INFO)ptr; 1291 SCHAR *pRemainingBitsInSegment; 1292 INT *pLeftStartOfSegment; 1293 INT *pRightStartOfSegment; 1294 UCHAR readDirection; 1295 UINT *pSegmentBitfield; 1296 UINT segmentOffset; 1297 UINT *pEscapeSequenceInfo; 1298 UINT codewordOffset; 1299 UCHAR carryBit; 1300 UINT escapePrefixUp; 1301 SCHAR *pSta; 1302 1303 pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment; 1304 pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment; 1305 pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment; 1306 readDirection = pHcr->segmentInfo.readDirection; 1307 pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield; 1308 segmentOffset = pHcr->segmentInfo.segmentOffset; 1309 pEscapeSequenceInfo = pHcr->nonPcwSideinfo.pEscapeSequenceInfo; 1310 codewordOffset = pHcr->nonPcwSideinfo.codewordOffset; 1311 pSta = pHcr->nonPcwSideinfo.pSta; 1312 1313 escapePrefixUp = 1314 (pEscapeSequenceInfo[codewordOffset] & MASK_ESCAPE_PREFIX_UP) >> 1315 LSB_ESCAPE_PREFIX_UP; 1316 1317 /* decode escape prefix */ 1318 for (; pRemainingBitsInSegment[segmentOffset] > 0; 1319 pRemainingBitsInSegment[segmentOffset] -= 1) { 1320 carryBit = HcrGetABitFromBitstream( 1321 bs, pHcr->decInOut.bitstreamAnchor, &pLeftStartOfSegment[segmentOffset], 1322 &pRightStartOfSegment[segmentOffset], readDirection); 1323 1324 /* count ones and store sum in escapePrefixUp */ 1325 if (carryBit == 1) { 1326 escapePrefixUp += 1; /* update conter for ones */ 1327 1328 /* store updated counter in sideinfo of current codeword */ 1329 pEscapeSequenceInfo[codewordOffset] &= 1330 ~MASK_ESCAPE_PREFIX_UP; /* delete old escapePrefixUp */ 1331 escapePrefixUp <<= LSB_ESCAPE_PREFIX_UP; /* shift to correct position */ 1332 pEscapeSequenceInfo[codewordOffset] |= 1333 escapePrefixUp; /* insert new escapePrefixUp */ 1334 escapePrefixUp >>= LSB_ESCAPE_PREFIX_UP; /* shift back down */ 1335 } else { /* separator [zero] reached */ 1336 pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of 1337 for loop counter (see 1338 above) is done here */ 1339 escapePrefixUp += 1340 4; /* if escape_separator '0' appears, add 4 and ==> break */ 1341 1342 /* store escapePrefixUp in pEscapeSequenceInfo[codewordOffset] at bit 1343 * position escapePrefixUp */ 1344 pEscapeSequenceInfo[codewordOffset] &= 1345 ~MASK_ESCAPE_PREFIX_UP; /* delete old escapePrefixUp */ 1346 escapePrefixUp <<= LSB_ESCAPE_PREFIX_UP; /* shift to correct position */ 1347 pEscapeSequenceInfo[codewordOffset] |= 1348 escapePrefixUp; /* insert new escapePrefixUp */ 1349 escapePrefixUp >>= LSB_ESCAPE_PREFIX_UP; /* shift back down */ 1350 1351 /* store escapePrefixUp in pEscapeSequenceInfo[codewordOffset] at bit 1352 * position escapePrefixDown */ 1353 pEscapeSequenceInfo[codewordOffset] &= 1354 ~MASK_ESCAPE_PREFIX_DOWN; /* delete old escapePrefixDown */ 1355 escapePrefixUp <<= LSB_ESCAPE_PREFIX_DOWN; /* shift to correct position */ 1356 pEscapeSequenceInfo[codewordOffset] |= 1357 escapePrefixUp; /* insert new escapePrefixDown */ 1358 1359 pSta[codewordOffset] = BODY_SIGN_ESC__ESC_WORD; /* set next state */ 1360 pHcr->nonPcwSideinfo.pState = 1361 aStateConstant2State[pSta[codewordOffset]]; /* get state from separate 1362 array of cw-sideinfo */ 1363 break; 1364 } 1365 } 1366 1367 if (pRemainingBitsInSegment[segmentOffset] <= 0) { 1368 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 1369 pSegmentBitfield); /* clear a bit in bitfield and 1370 switch off statemachine */ 1371 1372 if (pRemainingBitsInSegment[segmentOffset] < 0) { 1373 pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN_ESC__ESC_PREFIX; 1374 return BODY_SIGN_ESC__ESC_PREFIX; 1375 } 1376 } 1377 1378 return STOP_THIS_STATE; 1379 } 1380 1381 /*--------------------------------------------------------------------------------------------- 1382 description: Decode escapeWord of escape sequence. If the escape sequence 1383 is decoded completely, assemble quantized-spectral-escape-coefficient and 1384 replace the previous decoded 16 by the new value. Test flagB. If flagB is set, 1385 the second escape sequence must be decoded. If flagB is not set, the codeword is 1386 decoded and the state machine is switched off. 1387 ----------------------------------------------------------------------------------------------- 1388 output: Two lines with valid sign. At least one of both lines has got 1389 the correct value. 1390 ----------------------------------------------------------------------------------------------- 1391 return: 0 1392 -------------------------------------------------------------------------------------------- 1393 */ 1394 UINT Hcr_State_BODY_SIGN_ESC__ESC_WORD(HANDLE_FDK_BITSTREAM bs, void *ptr) { 1395 H_HCR_INFO pHcr = (H_HCR_INFO)ptr; 1396 SCHAR *pRemainingBitsInSegment; 1397 INT *pLeftStartOfSegment; 1398 INT *pRightStartOfSegment; 1399 UCHAR readDirection; 1400 UINT *pSegmentBitfield; 1401 UINT *pCodewordBitfield; 1402 UINT segmentOffset; 1403 1404 FIXP_DBL *pResultBase; 1405 USHORT *iResultPointer; 1406 UINT *pEscapeSequenceInfo; 1407 UINT codewordOffset; 1408 1409 UINT escapeWord; 1410 UINT escapePrefixDown; 1411 UINT escapePrefixUp; 1412 UCHAR carryBit; 1413 UINT iQSC; 1414 INT sign; 1415 UINT flagA; 1416 UINT flagB; 1417 SCHAR *pSta; 1418 1419 pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment; 1420 pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment; 1421 pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment; 1422 readDirection = pHcr->segmentInfo.readDirection; 1423 pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield; 1424 pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield; 1425 segmentOffset = pHcr->segmentInfo.segmentOffset; 1426 1427 pResultBase = pHcr->nonPcwSideinfo.pResultBase; 1428 iResultPointer = pHcr->nonPcwSideinfo.iResultPointer; 1429 pEscapeSequenceInfo = pHcr->nonPcwSideinfo.pEscapeSequenceInfo; 1430 codewordOffset = pHcr->nonPcwSideinfo.codewordOffset; 1431 pSta = pHcr->nonPcwSideinfo.pSta; 1432 1433 escapeWord = pEscapeSequenceInfo[codewordOffset] & MASK_ESCAPE_WORD; 1434 escapePrefixDown = 1435 (pEscapeSequenceInfo[codewordOffset] & MASK_ESCAPE_PREFIX_DOWN) >> 1436 LSB_ESCAPE_PREFIX_DOWN; 1437 1438 /* decode escape word */ 1439 for (; pRemainingBitsInSegment[segmentOffset] > 0; 1440 pRemainingBitsInSegment[segmentOffset] -= 1) { 1441 carryBit = HcrGetABitFromBitstream( 1442 bs, pHcr->decInOut.bitstreamAnchor, &pLeftStartOfSegment[segmentOffset], 1443 &pRightStartOfSegment[segmentOffset], readDirection); 1444 1445 /* build escape word */ 1446 escapeWord <<= 1447 1; /* left shift previous decoded part of escapeWord by on bit */ 1448 escapeWord = escapeWord | carryBit; /* assemble escape word by bitwise or */ 1449 1450 /* decrement counter for length of escape word because one more bit was 1451 * decoded */ 1452 escapePrefixDown -= 1; 1453 1454 /* store updated escapePrefixDown */ 1455 pEscapeSequenceInfo[codewordOffset] &= 1456 ~MASK_ESCAPE_PREFIX_DOWN; /* delete old escapePrefixDown */ 1457 escapePrefixDown <<= LSB_ESCAPE_PREFIX_DOWN; /* shift to correct position */ 1458 pEscapeSequenceInfo[codewordOffset] |= 1459 escapePrefixDown; /* insert new escapePrefixDown */ 1460 escapePrefixDown >>= LSB_ESCAPE_PREFIX_DOWN; /* shift back */ 1461 1462 /* store updated escapeWord */ 1463 pEscapeSequenceInfo[codewordOffset] &= 1464 ~MASK_ESCAPE_WORD; /* delete old escapeWord */ 1465 pEscapeSequenceInfo[codewordOffset] |= 1466 escapeWord; /* insert new escapeWord */ 1467 1468 if (escapePrefixDown == 0) { 1469 pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of 1470 for loop counter (see 1471 above) is done here */ 1472 1473 /* escape sequence decoded. Assemble escape-line and replace original line 1474 */ 1475 1476 /* step 0 */ 1477 /* derive sign */ 1478 iQSC = iResultPointer[codewordOffset]; 1479 sign = (pResultBase[iQSC] >= (FIXP_DBL)0) 1480 ? 1 1481 : -1; /* get sign of escape value 16 */ 1482 1483 /* step 1 */ 1484 /* get escapePrefixUp */ 1485 escapePrefixUp = 1486 (pEscapeSequenceInfo[codewordOffset] & MASK_ESCAPE_PREFIX_UP) >> 1487 LSB_ESCAPE_PREFIX_UP; 1488 1489 /* step 2 */ 1490 /* calculate escape value */ 1491 pResultBase[iQSC] = 1492 (FIXP_DBL)(sign * (((INT)1 << escapePrefixUp) + (INT)escapeWord)); 1493 1494 /* get both flags from sideinfo (flags are not shifted to the 1495 * lsb-position) */ 1496 flagA = pEscapeSequenceInfo[codewordOffset] & MASK_FLAG_A; 1497 flagB = pEscapeSequenceInfo[codewordOffset] & MASK_FLAG_B; 1498 1499 /* step 3 */ 1500 /* clear the whole escape sideinfo word */ 1501 pEscapeSequenceInfo[codewordOffset] = 0; 1502 1503 /* change state in dependence of flag flagB */ 1504 if (flagA != 0) { 1505 /* first escape sequence decoded; previous decoded 16 has been replaced 1506 * by valid line */ 1507 1508 /* clear flagA in sideinfo word because this escape sequence has already 1509 * beed decoded */ 1510 pEscapeSequenceInfo[codewordOffset] &= ~MASK_FLAG_A; 1511 1512 if (flagB == 0) { 1513 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 1514 pCodewordBitfield); /* clear a bit in bitfield 1515 and switch off 1516 statemachine */ 1517 } else { 1518 /* updated pointer to next and last 16 */ 1519 iQSC++; 1520 iResultPointer[codewordOffset] = iQSC; 1521 1522 /* change state */ 1523 pSta[codewordOffset] = BODY_SIGN_ESC__ESC_PREFIX; 1524 pHcr->nonPcwSideinfo.pState = 1525 aStateConstant2State[pSta[codewordOffset]]; /* get state from 1526 separate array of 1527 cw-sideinfo */ 1528 } 1529 } else { 1530 ClearBitFromBitfield( 1531 &(pHcr->nonPcwSideinfo.pState), segmentOffset, 1532 pCodewordBitfield); /* clear a bit in bitfield and switch off 1533 statemachine */ 1534 } 1535 break; 1536 } 1537 } 1538 1539 if (pRemainingBitsInSegment[segmentOffset] <= 0) { 1540 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 1541 pSegmentBitfield); /* clear a bit in bitfield and 1542 switch off statemachine */ 1543 1544 if (pRemainingBitsInSegment[segmentOffset] < 0) { 1545 pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN_ESC__ESC_WORD; 1546 return BODY_SIGN_ESC__ESC_WORD; 1547 } 1548 } 1549 1550 return STOP_THIS_STATE; 1551 } 1552