1/* ----------------------------------------------------------------------------- 2Software License for The Fraunhofer FDK AAC Codec Library for Android 3 4© Copyright 1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten 5Forschung e.V. All rights reserved. 6 7 1. INTRODUCTION 8The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software 9that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding 10scheme for digital audio. This FDK AAC Codec software is intended to be used on 11a wide variety of Android devices. 12 13AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient 14general perceptual audio codecs. AAC-ELD is considered the best-performing 15full-bandwidth communications codec by independent studies and is widely 16deployed. AAC has been standardized by ISO and IEC as part of the MPEG 17specifications. 18 19Patent licenses for necessary patent claims for the FDK AAC Codec (including 20those of Fraunhofer) may be obtained through Via Licensing 21(www.vialicensing.com) or through the respective patent owners individually for 22the purpose of encoding or decoding bit streams in products that are compliant 23with the ISO/IEC MPEG audio standards. Please note that most manufacturers of 24Android devices already license these patent claims through Via Licensing or 25directly from the patent owners, and therefore FDK AAC Codec software may 26already be covered under those patent licenses when it is used for those 27licensed purposes only. 28 29Commercially-licensed AAC software libraries, including floating-point versions 30with enhanced sound quality, are also available from Fraunhofer. Users are 31encouraged to check the Fraunhofer website for additional applications 32information and documentation. 33 342. COPYRIGHT LICENSE 35 36Redistribution and use in source and binary forms, with or without modification, 37are permitted without payment of copyright license fees provided that you 38satisfy the following conditions: 39 40You must retain the complete text of this software license in redistributions of 41the FDK AAC Codec or your modifications thereto in source code form. 42 43You must retain the complete text of this software license in the documentation 44and/or other materials provided with redistributions of the FDK AAC Codec or 45your modifications thereto in binary form. You must make available free of 46charge copies of the complete source code of the FDK AAC Codec and your 47modifications thereto to recipients of copies in binary form. 48 49The name of Fraunhofer may not be used to endorse or promote products derived 50from this library without prior written permission. 51 52You may not charge copyright license fees for anyone to use, copy or distribute 53the FDK AAC Codec software or your modifications thereto. 54 55Your modified versions of the FDK AAC Codec must carry prominent notices stating 56that you changed the software and the date of any change. For modified versions 57of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android" 58must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK 59AAC Codec Library for Android." 60 613. NO PATENT LICENSE 62 63NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without 64limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE. 65Fraunhofer provides no warranty of patent non-infringement with respect to this 66software. 67 68You may use this FDK AAC Codec software or modifications thereto only for 69purposes that are authorized by appropriate patent licenses. 70 714. DISCLAIMER 72 73This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright 74holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, 75including but not limited to the implied warranties of merchantability and 76fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 77CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, 78or consequential damages, including but not limited to procurement of substitute 79goods or services; loss of use, data, or profits, or business interruption, 80however caused and on any theory of liability, whether in contract, strict 81liability, or tort (including negligence), arising in any way out of the use of 82this software, even if advised of the possibility of such damage. 83 845. CONTACT INFORMATION 85 86Fraunhofer Institute for Integrated Circuits IIS 87Attention: Audio and Multimedia Departments - FDK AAC LL 88Am Wolfsmantel 33 8991058 Erlangen, Germany 90 91www.iis.fraunhofer.de/amm 92amm-info@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 112static UINT InitSegmentBitfield(UINT *pNumSegment, 113 SCHAR *pRemainingBitsInSegment, 114 UINT *pSegmentBitfield, 115 UCHAR *pNumWordForBitfield, 116 USHORT *pNumBitValidInLastWord); 117 118static void InitNonPCWSideInformationForCurrentSet(H_HCR_INFO pHcr); 119 120static INT ModuloValue(INT input, INT bufferlength); 121 122static void ClearBitFromBitfield(STATEFUNC *ptrState, UINT offset, 123 UINT *pBitfield); 124 125/*--------------------------------------------------------------------------------------------- 126 description: This function decodes all non-priority codewords (non-PCWs) by 127using a state-machine. 128-------------------------------------------------------------------------------------------- 129*/ 130void 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 353following sets. If a segment is decoded empty, the according bit from the 354Bitfield is removed. 355----------------------------------------------------------------------------------------------- 356 return: numValidSegment = the number of valid segments 357-------------------------------------------------------------------------------------------- 358*/ 359static 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 = ((*pNumSegment - 1) >> THIRTYTWO_LOG_DIV_TWO_LOG) + 1; 371 372 /* loop over all words, which are completely used or only partial */ 373 /* bit in pSegmentBitfield is zero if segment is empty; bit in 374 * pSegmentBitfield is one if segment is not empty */ 375 numValidSegment = 0; 376 *pNumBitValidInLastWord = *pNumSegment; 377 378 /* loop over words */ 379 for (bitfieldWord = 0; bitfieldWord < *pNumWordForBitfield - 1; 380 bitfieldWord++) { 381 tempWord = 0xFFFFFFFF; /* set ones */ 382 r = bitfieldWord << THIRTYTWO_LOG_DIV_TWO_LOG; 383 for (i = 0; i < NUMBER_OF_BIT_IN_WORD; i++) { 384 if (pRemainingBitsInSegment[r + i] == 0) { 385 tempWord = tempWord & ~(1 << (NUMBER_OF_BIT_IN_WORD - 1 - 386 i)); /* set a zero at bit number 387 (NUMBER_OF_BIT_IN_WORD-1-i) in 388 tempWord */ 389 } else { 390 numValidSegment += 1; /* count segments which are not empty */ 391 } 392 } 393 pSegmentBitfield[bitfieldWord] = tempWord; /* store result */ 394 *pNumBitValidInLastWord -= NUMBER_OF_BIT_IN_WORD; /* calculate number of 395 zeros on LSB side in 396 the last word */ 397 } 398 399 /* calculate last word: prepare special tempWord */ 400 tempWord = 0xFFFFFFFF; 401 for (i = 0; i < (NUMBER_OF_BIT_IN_WORD - *pNumBitValidInLastWord); i++) { 402 tempWord = tempWord & ~(1 << i); /* clear bit i in tempWord */ 403 } 404 405 /* calculate last word */ 406 r = bitfieldWord << THIRTYTWO_LOG_DIV_TWO_LOG; 407 for (i = 0; i < *pNumBitValidInLastWord; i++) { 408 if (pRemainingBitsInSegment[r + i] == 0) { 409 tempWord = tempWord & ~(1 << (NUMBER_OF_BIT_IN_WORD - 1 - 410 i)); /* set a zero at bit number 411 (NUMBER_OF_BIT_IN_WORD-1-i) in 412 tempWord */ 413 } else { 414 numValidSegment += 1; /* count segments which are not empty */ 415 } 416 } 417 pSegmentBitfield[bitfieldWord] = tempWord; /* store result */ 418 419 return numValidSegment; 420} 421 422/*--------------------------------------------------------------------------------------------- 423 description: This function sets up sideinfo for the non-PCW decoder (for the 424current set). 425---------------------------------------------------------------------------------------------*/ 426static void InitNonPCWSideInformationForCurrentSet(H_HCR_INFO pHcr) { 427 USHORT i, k; 428 UCHAR codebookDim; 429 UINT startNode; 430 431 UCHAR *pCodebook = pHcr->nonPcwSideinfo.pCodebook; 432 UINT *iNode = pHcr->nonPcwSideinfo.iNode; 433 UCHAR *pCntSign = pHcr->nonPcwSideinfo.pCntSign; 434 USHORT *iResultPointer = pHcr->nonPcwSideinfo.iResultPointer; 435 UINT *pEscapeSequenceInfo = pHcr->nonPcwSideinfo.pEscapeSequenceInfo; 436 SCHAR *pSta = pHcr->nonPcwSideinfo.pSta; 437 USHORT *pNumExtendedSortedCodewordInSection = 438 pHcr->sectionInfo.pNumExtendedSortedCodewordInSection; 439 int numExtendedSortedCodewordInSectionIdx = 440 pHcr->sectionInfo.numExtendedSortedCodewordInSectionIdx; 441 UCHAR *pExtendedSortedCodebook = pHcr->sectionInfo.pExtendedSortedCodebook; 442 int extendedSortedCodebookIdx = pHcr->sectionInfo.extendedSortedCodebookIdx; 443 USHORT *pNumExtendedSortedSectionsInSets = 444 pHcr->sectionInfo.pNumExtendedSortedSectionsInSets; 445 int numExtendedSortedSectionsInSetsIdx = 446 pHcr->sectionInfo.numExtendedSortedSectionsInSetsIdx; 447 int quantizedSpectralCoefficientsIdx = 448 pHcr->decInOut.quantizedSpectralCoefficientsIdx; 449 const UCHAR *pCbDimension = aDimCb; 450 int iterationCounter = 0; 451 452 /* loop over number of extended sorted sections in the current set so all 453 * codewords sideinfo variables within this set can be prepared for decoding 454 */ 455 for (i = pNumExtendedSortedSectionsInSets[numExtendedSortedSectionsInSetsIdx]; 456 i != 0; i--) { 457 codebookDim = 458 pCbDimension[pExtendedSortedCodebook[extendedSortedCodebookIdx]]; 459 startNode = *aHuffTable[pExtendedSortedCodebook[extendedSortedCodebookIdx]]; 460 461 for (k = pNumExtendedSortedCodewordInSection 462 [numExtendedSortedCodewordInSectionIdx]; 463 k != 0; k--) { 464 iterationCounter++; 465 if (iterationCounter > (1024 >> 2)) { 466 return; 467 } 468 *pSta++ = aCodebook2StartInt 469 [pExtendedSortedCodebook[extendedSortedCodebookIdx]]; 470 *pCodebook++ = pExtendedSortedCodebook[extendedSortedCodebookIdx]; 471 *iNode++ = startNode; 472 *pCntSign++ = 0; 473 *iResultPointer++ = quantizedSpectralCoefficientsIdx; 474 *pEscapeSequenceInfo++ = 0; 475 quantizedSpectralCoefficientsIdx += 476 codebookDim; /* update pointer by codebookDim --> point to next 477 starting value for writing out */ 478 if (quantizedSpectralCoefficientsIdx >= 1024) { 479 return; 480 } 481 } 482 numExtendedSortedCodewordInSectionIdx++; /* inc ptr for next ext sort sec in 483 current set */ 484 extendedSortedCodebookIdx++; /* inc ptr for next ext sort sec in current set 485 */ 486 if (numExtendedSortedCodewordInSectionIdx >= (MAX_SFB_HCR + MAX_HCR_SETS) || 487 extendedSortedCodebookIdx >= (MAX_SFB_HCR + MAX_HCR_SETS)) { 488 return; 489 } 490 } 491 numExtendedSortedSectionsInSetsIdx++; /* inc ptr for next set of non-PCWs */ 492 if (numExtendedSortedCodewordInSectionIdx >= (MAX_SFB_HCR + MAX_HCR_SETS)) { 493 return; 494 } 495 496 /* Write back indexes */ 497 pHcr->sectionInfo.numExtendedSortedCodewordInSectionIdx = 498 numExtendedSortedCodewordInSectionIdx; 499 pHcr->sectionInfo.extendedSortedCodebookIdx = extendedSortedCodebookIdx; 500 pHcr->sectionInfo.numExtendedSortedSectionsInSetsIdx = 501 numExtendedSortedSectionsInSetsIdx; 502 pHcr->sectionInfo.numExtendedSortedCodewordInSectionIdx = 503 numExtendedSortedCodewordInSectionIdx; 504 pHcr->decInOut.quantizedSpectralCoefficientsIdx = 505 quantizedSpectralCoefficientsIdx; 506} 507 508/*--------------------------------------------------------------------------------------------- 509 description: This function returns the input value if the value is in the 510 range of bufferlength. If <input> is smaller, one bufferlength 511is added, if <input> is bigger one bufferlength is subtracted. 512----------------------------------------------------------------------------------------------- 513 return: modulo result 514-------------------------------------------------------------------------------------------- 515*/ 516static INT ModuloValue(INT input, INT bufferlength) { 517 if (input > (bufferlength - 1)) { 518 return (input - bufferlength); 519 } 520 if (input < 0) { 521 return (input + bufferlength); 522 } 523 return input; 524} 525 526/*--------------------------------------------------------------------------------------------- 527 description: This function clears a bit from current bitfield and 528 switches off the statemachine. 529 530 A bit is cleared in two cases: 531 a) a codeword is decoded, then a bit is cleared in codeword 532bitfield b) a segment is decoded empty, then a bit is cleared in segment 533bitfield 534-------------------------------------------------------------------------------------------- 535*/ 536static void ClearBitFromBitfield(STATEFUNC *ptrState, UINT offset, 537 UINT *pBitfield) { 538 UINT numBitfieldWord; 539 UINT numBitfieldBit; 540 541 /* get both values needed for clearing the bit */ 542 numBitfieldWord = offset >> THIRTYTWO_LOG_DIV_TWO_LOG; /* int = wordNr */ 543 numBitfieldBit = offset - (numBitfieldWord 544 << THIRTYTWO_LOG_DIV_TWO_LOG); /* fract = bitNr */ 545 546 /* clear a bit in bitfield */ 547 pBitfield[numBitfieldWord] = 548 pBitfield[numBitfieldWord] & 549 ~(1 << (NUMBER_OF_BIT_IN_WORD - 1 - numBitfieldBit)); 550 551 /* switch off state machine because codeword is decoded and/or because segment 552 * is empty */ 553 *ptrState = NULL; 554} 555 556/* ========================================================================================= 557 the states of the statemachine 558 ========================================================================================= 559 */ 560 561/*--------------------------------------------------------------------------------------------- 562 description: Decodes the body of a codeword. This State is used for 563codebooks 1,2,5 and 6. No sign bits are decoded, because the table of the 564quantized spectral values has got a valid sign at the quantized spectral lines. 565----------------------------------------------------------------------------------------------- 566 output: Two or four quantizes spectral values written at position 567where pResultPointr points to 568----------------------------------------------------------------------------------------------- 569 return: 0 570-------------------------------------------------------------------------------------------- 571*/ 572UINT Hcr_State_BODY_ONLY(HANDLE_FDK_BITSTREAM bs, void *ptr) { 573 H_HCR_INFO pHcr = (H_HCR_INFO)ptr; 574 UINT *pSegmentBitfield; 575 UINT *pCodewordBitfield; 576 UINT segmentOffset; 577 FIXP_DBL *pResultBase; 578 UINT *iNode; 579 USHORT *iResultPointer; 580 UINT codewordOffset; 581 UINT branchNode; 582 UINT branchValue; 583 UINT iQSC; 584 UINT treeNode; 585 UCHAR carryBit; 586 INT *pLeftStartOfSegment; 587 INT *pRightStartOfSegment; 588 SCHAR *pRemainingBitsInSegment; 589 UCHAR readDirection; 590 UCHAR *pCodebook; 591 UCHAR dimCntr; 592 const UINT *pCurrentTree; 593 const UCHAR *pCbDimension; 594 const SCHAR *pQuantVal; 595 const SCHAR *pQuantValBase; 596 597 pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment; 598 pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment; 599 pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment; 600 readDirection = pHcr->segmentInfo.readDirection; 601 pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield; 602 pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield; 603 segmentOffset = pHcr->segmentInfo.segmentOffset; 604 605 pCodebook = pHcr->nonPcwSideinfo.pCodebook; 606 iNode = pHcr->nonPcwSideinfo.iNode; 607 pResultBase = pHcr->nonPcwSideinfo.pResultBase; 608 iResultPointer = pHcr->nonPcwSideinfo.iResultPointer; 609 codewordOffset = pHcr->nonPcwSideinfo.codewordOffset; 610 611 pCbDimension = aDimCb; 612 613 treeNode = iNode[codewordOffset]; 614 pCurrentTree = aHuffTable[pCodebook[codewordOffset]]; 615 616 for (; pRemainingBitsInSegment[segmentOffset] > 0; 617 pRemainingBitsInSegment[segmentOffset] -= 1) { 618 carryBit = HcrGetABitFromBitstream(bs, &pLeftStartOfSegment[segmentOffset], 619 &pRightStartOfSegment[segmentOffset], 620 readDirection); 621 622 CarryBitToBranchValue(carryBit, /* make a step in decoding tree */ 623 treeNode, &branchValue, &branchNode); 624 625 /* if end of branch reached write out lines and count bits needed for sign, 626 * otherwise store node in codeword sideinfo */ 627 if ((branchNode & TEST_BIT_10) == 628 TEST_BIT_10) { /* test bit 10 ; ==> body is complete */ 629 pQuantValBase = aQuantTable[pCodebook[codewordOffset]]; /* get base 630 address of 631 quantized 632 values 633 belonging to 634 current 635 codebook */ 636 pQuantVal = pQuantValBase + branchValue; /* set pointer to first valid 637 line [of 2 or 4 quantized 638 values] */ 639 640 iQSC = iResultPointer[codewordOffset]; /* get position of first line for 641 writing out result */ 642 643 for (dimCntr = pCbDimension[pCodebook[codewordOffset]]; dimCntr != 0; 644 dimCntr--) { 645 pResultBase[iQSC++] = 646 (FIXP_DBL)*pQuantVal++; /* write out 2 or 4 lines into 647 spectrum; no Sign bits 648 available in this state */ 649 } 650 651 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 652 pCodewordBitfield); /* clear a bit in bitfield and 653 switch off statemachine */ 654 pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of 655 for loop counter (see 656 above) is done here */ 657 break; /* end of branch in tree reached i.e. a whole nonPCW-Body is 658 decoded */ 659 } else { /* body is not decoded completely: */ 660 treeNode = *( 661 pCurrentTree + 662 branchValue); /* update treeNode for further step in decoding tree */ 663 } 664 } 665 iNode[codewordOffset] = treeNode; /* store updated treeNode because maybe 666 decoding of codeword body not finished 667 yet */ 668 669 if (pRemainingBitsInSegment[segmentOffset] <= 0) { 670 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 671 pSegmentBitfield); /* clear a bit in bitfield and 672 switch off statemachine */ 673 674 if (pRemainingBitsInSegment[segmentOffset] < 0) { 675 pHcr->decInOut.errorLog |= STATE_ERROR_BODY_ONLY; 676 return BODY_ONLY; 677 } 678 } 679 680 return STOP_THIS_STATE; 681} 682 683/*--------------------------------------------------------------------------------------------- 684 description: Decodes the codeword body, writes out result and counts the 685number of quantized spectral values, which are different form zero. For those 686values sign bits are needed. 687 688 If sign bit counter cntSign is different from zero, switch to 689next state to decode sign Bits there. If sign bit counter cntSign is zero, no 690sign bits are needed and codeword is decoded. 691----------------------------------------------------------------------------------------------- 692 output: Two or four written quantizes spectral values written at 693position where pResultPointr points to. The signs of those lines may be wrong. 694If the signs [on just one signle sign] is wrong, the next state will correct it. 695----------------------------------------------------------------------------------------------- 696 return: 0 697-------------------------------------------------------------------------------------------- 698*/ 699UINT Hcr_State_BODY_SIGN__BODY(HANDLE_FDK_BITSTREAM bs, void *ptr) { 700 H_HCR_INFO pHcr = (H_HCR_INFO)ptr; 701 SCHAR *pRemainingBitsInSegment; 702 INT *pLeftStartOfSegment; 703 INT *pRightStartOfSegment; 704 UCHAR readDirection; 705 UINT *pSegmentBitfield; 706 UINT *pCodewordBitfield; 707 UINT segmentOffset; 708 709 UCHAR *pCodebook; 710 UINT *iNode; 711 UCHAR *pCntSign; 712 FIXP_DBL *pResultBase; 713 USHORT *iResultPointer; 714 UINT codewordOffset; 715 716 UINT iQSC; 717 UINT cntSign; 718 UCHAR dimCntr; 719 UCHAR carryBit; 720 SCHAR *pSta; 721 UINT treeNode; 722 UINT branchValue; 723 UINT branchNode; 724 const UCHAR *pCbDimension; 725 const UINT *pCurrentTree; 726 const SCHAR *pQuantValBase; 727 const SCHAR *pQuantVal; 728 729 pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment; 730 pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment; 731 pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment; 732 readDirection = pHcr->segmentInfo.readDirection; 733 pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield; 734 pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield; 735 segmentOffset = pHcr->segmentInfo.segmentOffset; 736 737 pCodebook = pHcr->nonPcwSideinfo.pCodebook; 738 iNode = pHcr->nonPcwSideinfo.iNode; 739 pCntSign = pHcr->nonPcwSideinfo.pCntSign; 740 pResultBase = pHcr->nonPcwSideinfo.pResultBase; 741 iResultPointer = pHcr->nonPcwSideinfo.iResultPointer; 742 codewordOffset = pHcr->nonPcwSideinfo.codewordOffset; 743 pSta = pHcr->nonPcwSideinfo.pSta; 744 745 pCbDimension = aDimCb; 746 747 treeNode = iNode[codewordOffset]; 748 pCurrentTree = aHuffTable[pCodebook[codewordOffset]]; 749 750 for (; pRemainingBitsInSegment[segmentOffset] > 0; 751 pRemainingBitsInSegment[segmentOffset] -= 1) { 752 carryBit = HcrGetABitFromBitstream(bs, &pLeftStartOfSegment[segmentOffset], 753 &pRightStartOfSegment[segmentOffset], 754 readDirection); 755 756 CarryBitToBranchValue(carryBit, /* make a step in decoding tree */ 757 treeNode, &branchValue, &branchNode); 758 759 /* if end of branch reached write out lines and count bits needed for sign, 760 * otherwise store node in codeword sideinfo */ 761 if ((branchNode & TEST_BIT_10) == 762 TEST_BIT_10) { /* test bit 10 ; if set body complete */ 763 /* body completely decoded; branchValue is valid, set pQuantVal to first 764 * (of two or four) quantized spectral coefficients */ 765 pQuantValBase = aQuantTable[pCodebook[codewordOffset]]; /* get base 766 address of 767 quantized 768 values 769 belonging to 770 current 771 codebook */ 772 pQuantVal = pQuantValBase + branchValue; /* set pointer to first valid 773 line [of 2 or 4 quantized 774 values] */ 775 776 iQSC = iResultPointer[codewordOffset]; /* get position of first line for 777 writing result */ 778 779 /* codeword decoding result is written out here: Write out 2 or 4 780 * quantized spectral values with probably */ 781 /* wrong sign and count number of values which are different from zero for 782 * sign bit decoding [which happens in next state] */ 783 cntSign = 0; 784 for (dimCntr = pCbDimension[pCodebook[codewordOffset]]; dimCntr != 0; 785 dimCntr--) { 786 pResultBase[iQSC++] = 787 (FIXP_DBL)*pQuantVal; /* write quant. spec. coef. into spectrum */ 788 if (*pQuantVal++ != 0) { 789 cntSign += 1; 790 } 791 } 792 793 if (cntSign == 0) { 794 ClearBitFromBitfield( 795 &(pHcr->nonPcwSideinfo.pState), segmentOffset, 796 pCodewordBitfield); /* clear a bit in bitfield and switch off 797 statemachine */ 798 } else { 799 pCntSign[codewordOffset] = cntSign; /* write sign count result into 800 codewordsideinfo of current 801 codeword */ 802 pSta[codewordOffset] = BODY_SIGN__SIGN; /* change state */ 803 pHcr->nonPcwSideinfo.pState = 804 aStateConstant2State[pSta[codewordOffset]]; /* get state from 805 separate array of 806 cw-sideinfo */ 807 } 808 pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of 809 for loop counter (see 810 above) is done here */ 811 break; /* end of branch in tree reached i.e. a whole nonPCW-Body is 812 decoded */ 813 } else { /* body is not decoded completely: */ 814 treeNode = *( 815 pCurrentTree + 816 branchValue); /* update treeNode for further step in decoding tree */ 817 } 818 } 819 iNode[codewordOffset] = treeNode; /* store updated treeNode because maybe 820 decoding of codeword body not finished 821 yet */ 822 823 if (pRemainingBitsInSegment[segmentOffset] <= 0) { 824 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 825 pSegmentBitfield); /* clear a bit in bitfield and 826 switch off statemachine */ 827 828 if (pRemainingBitsInSegment[segmentOffset] < 0) { 829 pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN__BODY; 830 return BODY_SIGN__BODY; 831 } 832 } 833 834 return STOP_THIS_STATE; 835} 836 837/*--------------------------------------------------------------------------------------------- 838 description: This state decodes the sign bits belonging to a codeword. The 839state is called as often in different "trials" until pCntSgn[codewordOffset] is 840zero. 841----------------------------------------------------------------------------------------------- 842 output: The two or four quantizes spectral values (written in previous 843state) have now the correct sign. 844----------------------------------------------------------------------------------------------- 845 return: 0 846-------------------------------------------------------------------------------------------- 847*/ 848UINT Hcr_State_BODY_SIGN__SIGN(HANDLE_FDK_BITSTREAM bs, void *ptr) { 849 H_HCR_INFO pHcr = (H_HCR_INFO)ptr; 850 SCHAR *pRemainingBitsInSegment; 851 INT *pLeftStartOfSegment; 852 INT *pRightStartOfSegment; 853 UCHAR readDirection; 854 UINT *pSegmentBitfield; 855 UINT *pCodewordBitfield; 856 UINT segmentOffset; 857 858 UCHAR *pCntSign; 859 FIXP_DBL *pResultBase; 860 USHORT *iResultPointer; 861 UINT codewordOffset; 862 863 UCHAR carryBit; 864 UINT iQSC; 865 UCHAR cntSign; 866 867 pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment; 868 pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment; 869 pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment; 870 readDirection = pHcr->segmentInfo.readDirection; 871 pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield; 872 pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield; 873 segmentOffset = pHcr->segmentInfo.segmentOffset; 874 875 /*pCodebook = */ 876 pCntSign = pHcr->nonPcwSideinfo.pCntSign; 877 pResultBase = pHcr->nonPcwSideinfo.pResultBase; 878 iResultPointer = pHcr->nonPcwSideinfo.iResultPointer; 879 codewordOffset = pHcr->nonPcwSideinfo.codewordOffset; 880 881 iQSC = iResultPointer[codewordOffset]; 882 cntSign = pCntSign[codewordOffset]; 883 884 /* loop for sign bit decoding */ 885 for (; pRemainingBitsInSegment[segmentOffset] > 0; 886 pRemainingBitsInSegment[segmentOffset] -= 1) { 887 carryBit = HcrGetABitFromBitstream(bs, &pLeftStartOfSegment[segmentOffset], 888 &pRightStartOfSegment[segmentOffset], 889 readDirection); 890 cntSign -= 891 1; /* decrement sign counter because one sign bit has been read */ 892 893 /* search for a line (which was decoded in previous state) which is not 894 * zero. [This value will get a sign] */ 895 while (pResultBase[iQSC] == (FIXP_DBL)0) { 896 if (++iQSC >= 1024) { /* points to current value different from zero */ 897 return BODY_SIGN__SIGN; 898 } 899 } 900 901 /* put sign together with line; if carryBit is zero, the sign is ok already; 902 * no write operation necessary in this case */ 903 if (carryBit != 0) { 904 pResultBase[iQSC] = -pResultBase[iQSC]; /* carryBit = 1 --> minus */ 905 } 906 907 iQSC++; /* update pointer to next (maybe valid) value */ 908 909 if (cntSign == 0) { /* if (cntSign==0) ==> set state CODEWORD_DECODED */ 910 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 911 pCodewordBitfield); /* clear a bit in bitfield and 912 switch off statemachine */ 913 pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of 914 for loop counter (see 915 above) is done here */ 916 break; /* whole nonPCW-Body and according sign bits are decoded */ 917 } 918 } 919 pCntSign[codewordOffset] = cntSign; 920 iResultPointer[codewordOffset] = iQSC; /* store updated pResultPointer */ 921 922 if (pRemainingBitsInSegment[segmentOffset] <= 0) { 923 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 924 pSegmentBitfield); /* clear a bit in bitfield and 925 switch off statemachine */ 926 927 if (pRemainingBitsInSegment[segmentOffset] < 0) { 928 pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN__SIGN; 929 return BODY_SIGN__SIGN; 930 } 931 } 932 933 return STOP_THIS_STATE; 934} 935 936/*--------------------------------------------------------------------------------------------- 937 description: Decodes the codeword body in case of codebook is 11. Writes 938out resulting two or four lines [with probably wrong sign] and counts the number 939of lines, which are different form zero. This information is needed in next 940 state where sign bits will be decoded, if necessary. 941 If sign bit counter cntSign is zero, no sign bits are needed 942and codeword is decoded completely. 943----------------------------------------------------------------------------------------------- 944 output: Two lines (quantizes spectral coefficients) which are probably 945wrong. The sign may be wrong and if one or two values is/are 16, the following 946states will decode the escape sequence to correct the values which are wirtten 947here. 948----------------------------------------------------------------------------------------------- 949 return: 0 950-------------------------------------------------------------------------------------------- 951*/ 952UINT Hcr_State_BODY_SIGN_ESC__BODY(HANDLE_FDK_BITSTREAM bs, void *ptr) { 953 H_HCR_INFO pHcr = (H_HCR_INFO)ptr; 954 SCHAR *pRemainingBitsInSegment; 955 INT *pLeftStartOfSegment; 956 INT *pRightStartOfSegment; 957 UCHAR readDirection; 958 UINT *pSegmentBitfield; 959 UINT *pCodewordBitfield; 960 UINT segmentOffset; 961 962 UINT *iNode; 963 UCHAR *pCntSign; 964 FIXP_DBL *pResultBase; 965 USHORT *iResultPointer; 966 UINT codewordOffset; 967 968 UCHAR carryBit; 969 UINT iQSC; 970 UINT cntSign; 971 UINT dimCntr; 972 UINT treeNode; 973 SCHAR *pSta; 974 UINT branchNode; 975 UINT branchValue; 976 const UINT *pCurrentTree; 977 const SCHAR *pQuantValBase; 978 const SCHAR *pQuantVal; 979 980 pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment; 981 pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment; 982 pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment; 983 readDirection = pHcr->segmentInfo.readDirection; 984 pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield; 985 pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield; 986 segmentOffset = pHcr->segmentInfo.segmentOffset; 987 988 iNode = pHcr->nonPcwSideinfo.iNode; 989 pCntSign = pHcr->nonPcwSideinfo.pCntSign; 990 pResultBase = pHcr->nonPcwSideinfo.pResultBase; 991 iResultPointer = pHcr->nonPcwSideinfo.iResultPointer; 992 codewordOffset = pHcr->nonPcwSideinfo.codewordOffset; 993 pSta = pHcr->nonPcwSideinfo.pSta; 994 995 treeNode = iNode[codewordOffset]; 996 pCurrentTree = aHuffTable[ESCAPE_CODEBOOK]; 997 998 for (; pRemainingBitsInSegment[segmentOffset] > 0; 999 pRemainingBitsInSegment[segmentOffset] -= 1) { 1000 carryBit = HcrGetABitFromBitstream(bs, &pLeftStartOfSegment[segmentOffset], 1001 &pRightStartOfSegment[segmentOffset], 1002 readDirection); 1003 1004 /* make a step in tree */ 1005 CarryBitToBranchValue(carryBit, treeNode, &branchValue, &branchNode); 1006 1007 /* if end of branch reached write out lines and count bits needed for sign, 1008 * otherwise store node in codeword sideinfo */ 1009 if ((branchNode & TEST_BIT_10) == 1010 TEST_BIT_10) { /* test bit 10 ; if set body complete */ 1011 1012 /* body completely decoded; branchValue is valid */ 1013 /* set pQuantVol to first (of two or four) quantized spectral coefficients 1014 */ 1015 pQuantValBase = aQuantTable[ESCAPE_CODEBOOK]; /* get base address of 1016 quantized values 1017 belonging to current 1018 codebook */ 1019 pQuantVal = pQuantValBase + branchValue; /* set pointer to first valid 1020 line [of 2 or 4 quantized 1021 values] */ 1022 1023 /* make backup from original resultPointer in node storage for state 1024 * BODY_SIGN_ESC__SIGN */ 1025 iNode[codewordOffset] = iResultPointer[codewordOffset]; 1026 1027 /* get position of first line for writing result */ 1028 iQSC = iResultPointer[codewordOffset]; 1029 1030 /* codeword decoding result is written out here: Write out 2 or 4 1031 * quantized spectral values with probably */ 1032 /* wrong sign and count number of values which are different from zero for 1033 * sign bit decoding [which happens in next state] */ 1034 cntSign = 0; 1035 1036 for (dimCntr = DIMENSION_OF_ESCAPE_CODEBOOK; dimCntr != 0; dimCntr--) { 1037 pResultBase[iQSC++] = 1038 (FIXP_DBL)*pQuantVal; /* write quant. spec. coef. into spectrum */ 1039 if (*pQuantVal++ != 0) { 1040 cntSign += 1; 1041 } 1042 } 1043 1044 if (cntSign == 0) { 1045 ClearBitFromBitfield( 1046 &(pHcr->nonPcwSideinfo.pState), segmentOffset, 1047 pCodewordBitfield); /* clear a bit in bitfield and switch off 1048 statemachine */ 1049 /* codeword decoded */ 1050 } else { 1051 /* write sign count result into codewordsideinfo of current codeword */ 1052 pCntSign[codewordOffset] = cntSign; 1053 pSta[codewordOffset] = BODY_SIGN_ESC__SIGN; /* change state */ 1054 pHcr->nonPcwSideinfo.pState = 1055 aStateConstant2State[pSta[codewordOffset]]; /* get state from 1056 separate array of 1057 cw-sideinfo */ 1058 } 1059 pRemainingBitsInSegment[segmentOffset] -= 1; /* the last reinitialzation 1060 of for loop counter (see 1061 above) is done here */ 1062 break; /* end of branch in tree reached i.e. a whole nonPCW-Body is 1063 decoded */ 1064 } else { /* body is not decoded completely: */ 1065 /* update treeNode for further step in decoding tree and store updated 1066 * treeNode because maybe no more bits left in segment */ 1067 treeNode = *(pCurrentTree + branchValue); 1068 iNode[codewordOffset] = treeNode; 1069 } 1070 } 1071 1072 if (pRemainingBitsInSegment[segmentOffset] <= 0) { 1073 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 1074 pSegmentBitfield); /* clear a bit in bitfield and 1075 switch off statemachine */ 1076 1077 if (pRemainingBitsInSegment[segmentOffset] < 0) { 1078 pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN_ESC__BODY; 1079 return BODY_SIGN_ESC__BODY; 1080 } 1081 } 1082 1083 return STOP_THIS_STATE; 1084} 1085 1086/*--------------------------------------------------------------------------------------------- 1087 description: This state decodes the sign bits, if a codeword of codebook 11 1088needs some. A flag named 'flagB' in codeword sideinfo is set, if the second line 1089of quantized spectral values is 16. The 'flagB' is used in case of decoding of a 1090escape sequence is necessary as far as the second line is concerned. 1091 1092 If only the first line needs an escape sequence, the flagB is 1093cleared. If only the second line needs an escape sequence, the flagB is not 1094used. 1095 1096 For storing sideinfo in case of escape sequence decoding one 1097single word can be used for both escape sequences because they are decoded not 1098at the same time: 1099 1100 1101 bit 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 11024 3 2 1 0 1103 ===== == == =========== =========== 1104=================================== ^ ^ ^ ^ ^ 1105^ | | | | | | res. flagA flagB 1106escapePrefixUp escapePrefixDown escapeWord 1107 1108----------------------------------------------------------------------------------------------- 1109 output: Two lines with correct sign. If one or two values is/are 16, 1110the lines are not valid, otherwise they are. 1111----------------------------------------------------------------------------------------------- 1112 return: 0 1113-------------------------------------------------------------------------------------------- 1114*/ 1115UINT Hcr_State_BODY_SIGN_ESC__SIGN(HANDLE_FDK_BITSTREAM bs, void *ptr) { 1116 H_HCR_INFO pHcr = (H_HCR_INFO)ptr; 1117 SCHAR *pRemainingBitsInSegment; 1118 INT *pLeftStartOfSegment; 1119 INT *pRightStartOfSegment; 1120 UCHAR readDirection; 1121 UINT *pSegmentBitfield; 1122 UINT *pCodewordBitfield; 1123 UINT segmentOffset; 1124 1125 UINT *iNode; 1126 UCHAR *pCntSign; 1127 FIXP_DBL *pResultBase; 1128 USHORT *iResultPointer; 1129 UINT *pEscapeSequenceInfo; 1130 UINT codewordOffset; 1131 1132 UINT iQSC; 1133 UCHAR cntSign; 1134 UINT flagA; 1135 UINT flagB; 1136 UINT flags; 1137 UCHAR carryBit; 1138 SCHAR *pSta; 1139 1140 pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment; 1141 pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment; 1142 pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment; 1143 readDirection = pHcr->segmentInfo.readDirection; 1144 pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield; 1145 pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield; 1146 segmentOffset = pHcr->segmentInfo.segmentOffset; 1147 1148 iNode = pHcr->nonPcwSideinfo.iNode; 1149 pCntSign = pHcr->nonPcwSideinfo.pCntSign; 1150 pResultBase = pHcr->nonPcwSideinfo.pResultBase; 1151 iResultPointer = pHcr->nonPcwSideinfo.iResultPointer; 1152 pEscapeSequenceInfo = pHcr->nonPcwSideinfo.pEscapeSequenceInfo; 1153 codewordOffset = pHcr->nonPcwSideinfo.codewordOffset; 1154 pSta = pHcr->nonPcwSideinfo.pSta; 1155 1156 iQSC = iResultPointer[codewordOffset]; 1157 cntSign = pCntSign[codewordOffset]; 1158 1159 /* loop for sign bit decoding */ 1160 for (; pRemainingBitsInSegment[segmentOffset] > 0; 1161 pRemainingBitsInSegment[segmentOffset] -= 1) { 1162 carryBit = HcrGetABitFromBitstream(bs, &pLeftStartOfSegment[segmentOffset], 1163 &pRightStartOfSegment[segmentOffset], 1164 readDirection); 1165 1166 /* decrement sign counter because one sign bit has been read */ 1167 cntSign -= 1; 1168 pCntSign[codewordOffset] = cntSign; 1169 1170 /* get a quantized spectral value (which was decoded in previous state) 1171 * which is not zero. [This value will get a sign] */ 1172 while (pResultBase[iQSC] == (FIXP_DBL)0) { 1173 if (++iQSC >= 1024) { 1174 return BODY_SIGN_ESC__SIGN; 1175 } 1176 } 1177 iResultPointer[codewordOffset] = iQSC; 1178 1179 /* put negative sign together with quantized spectral value; if carryBit is 1180 * zero, the sign is ok already; no write operation necessary in this case 1181 */ 1182 if (carryBit != 0) { 1183 pResultBase[iQSC] = -pResultBase[iQSC]; /* carryBit = 1 --> minus */ 1184 } 1185 iQSC++; /* update index to next (maybe valid) value */ 1186 iResultPointer[codewordOffset] = iQSC; 1187 1188 if (cntSign == 0) { 1189 /* all sign bits are decoded now */ 1190 pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of 1191 for loop counter (see 1192 above) is done here */ 1193 1194 /* check decoded values if codeword is decoded: Check if one or two escape 1195 * sequences 16 follow */ 1196 1197 /* step 0 */ 1198 /* restore pointer to first decoded quantized value [ = original 1199 * pResultPointr] from index iNode prepared in State_BODY_SIGN_ESC__BODY 1200 */ 1201 iQSC = iNode[codewordOffset]; 1202 1203 /* step 1 */ 1204 /* test first value if escape sequence follows */ 1205 flagA = 0; /* for first possible escape sequence */ 1206 if (fixp_abs(pResultBase[iQSC++]) == (FIXP_DBL)ESCAPE_VALUE) { 1207 flagA = 1; 1208 } 1209 1210 /* step 2 */ 1211 /* test second value if escape sequence follows */ 1212 flagB = 0; /* for second possible escape sequence */ 1213 if (fixp_abs(pResultBase[iQSC]) == (FIXP_DBL)ESCAPE_VALUE) { 1214 flagB = 1; 1215 } 1216 1217 /* step 3 */ 1218 /* evaluate flag result and go on if necessary */ 1219 if (!flagA && !flagB) { 1220 ClearBitFromBitfield( 1221 &(pHcr->nonPcwSideinfo.pState), segmentOffset, 1222 pCodewordBitfield); /* clear a bit in bitfield and switch off 1223 statemachine */ 1224 } else { 1225 /* at least one of two lines is 16 */ 1226 /* store both flags at correct positions in non PCW codeword sideinfo 1227 * pEscapeSequenceInfo[codewordOffset] */ 1228 flags = flagA << POSITION_OF_FLAG_A; 1229 flags |= (flagB << POSITION_OF_FLAG_B); 1230 pEscapeSequenceInfo[codewordOffset] = flags; 1231 1232 /* set next state */ 1233 pSta[codewordOffset] = BODY_SIGN_ESC__ESC_PREFIX; 1234 pHcr->nonPcwSideinfo.pState = 1235 aStateConstant2State[pSta[codewordOffset]]; /* get state from 1236 separate array of 1237 cw-sideinfo */ 1238 1239 /* set result pointer to the first line of the two decoded lines */ 1240 iResultPointer[codewordOffset] = iNode[codewordOffset]; 1241 1242 if (!flagA && flagB) { 1243 /* update pResultPointr ==> state Stat_BODY_SIGN_ESC__ESC_WORD writes 1244 * to correct position. Second value is the one and only escape value 1245 */ 1246 iQSC = iResultPointer[codewordOffset]; 1247 iQSC++; 1248 iResultPointer[codewordOffset] = iQSC; 1249 } 1250 1251 } /* at least one of two lines is 16 */ 1252 break; /* nonPCW-Body at cb 11 and according sign bits are decoded */ 1253 1254 } /* if ( cntSign == 0 ) */ 1255 } /* loop over remaining Bits in segment */ 1256 1257 if (pRemainingBitsInSegment[segmentOffset] <= 0) { 1258 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 1259 pSegmentBitfield); /* clear a bit in bitfield and 1260 switch off statemachine */ 1261 1262 if (pRemainingBitsInSegment[segmentOffset] < 0) { 1263 pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN_ESC__SIGN; 1264 return BODY_SIGN_ESC__SIGN; 1265 } 1266 } 1267 return STOP_THIS_STATE; 1268} 1269 1270/*--------------------------------------------------------------------------------------------- 1271 description: Decode escape prefix of first or second escape sequence. The 1272escape prefix consists of ones. The following zero is also decoded here. 1273----------------------------------------------------------------------------------------------- 1274 output: If the single separator-zero which follows the 1275escape-prefix-ones is not yet decoded: The value 'escapePrefixUp' in word 1276pEscapeSequenceInfo[codewordOffset] is updated. 1277 1278 If the single separator-zero which follows the 1279escape-prefix-ones is decoded: Two updated values 'escapePrefixUp' and 1280'escapePrefixDown' in word pEscapeSequenceInfo[codewordOffset]. This State is 1281finished. Switch to next state. 1282----------------------------------------------------------------------------------------------- 1283 return: 0 1284-------------------------------------------------------------------------------------------- 1285*/ 1286UINT Hcr_State_BODY_SIGN_ESC__ESC_PREFIX(HANDLE_FDK_BITSTREAM bs, void *ptr) { 1287 H_HCR_INFO pHcr = (H_HCR_INFO)ptr; 1288 SCHAR *pRemainingBitsInSegment; 1289 INT *pLeftStartOfSegment; 1290 INT *pRightStartOfSegment; 1291 UCHAR readDirection; 1292 UINT *pSegmentBitfield; 1293 UINT segmentOffset; 1294 UINT *pEscapeSequenceInfo; 1295 UINT codewordOffset; 1296 UCHAR carryBit; 1297 UINT escapePrefixUp; 1298 SCHAR *pSta; 1299 1300 pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment; 1301 pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment; 1302 pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment; 1303 readDirection = pHcr->segmentInfo.readDirection; 1304 pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield; 1305 segmentOffset = pHcr->segmentInfo.segmentOffset; 1306 pEscapeSequenceInfo = pHcr->nonPcwSideinfo.pEscapeSequenceInfo; 1307 codewordOffset = pHcr->nonPcwSideinfo.codewordOffset; 1308 pSta = pHcr->nonPcwSideinfo.pSta; 1309 1310 escapePrefixUp = 1311 (pEscapeSequenceInfo[codewordOffset] & MASK_ESCAPE_PREFIX_UP) >> 1312 LSB_ESCAPE_PREFIX_UP; 1313 1314 /* decode escape prefix */ 1315 for (; pRemainingBitsInSegment[segmentOffset] > 0; 1316 pRemainingBitsInSegment[segmentOffset] -= 1) { 1317 carryBit = HcrGetABitFromBitstream(bs, &pLeftStartOfSegment[segmentOffset], 1318 &pRightStartOfSegment[segmentOffset], 1319 readDirection); 1320 1321 /* count ones and store sum in escapePrefixUp */ 1322 if (carryBit == 1) { 1323 escapePrefixUp += 1; /* update conter for ones */ 1324 1325 /* store updated counter in sideinfo of current codeword */ 1326 pEscapeSequenceInfo[codewordOffset] &= 1327 ~MASK_ESCAPE_PREFIX_UP; /* delete old escapePrefixUp */ 1328 escapePrefixUp <<= LSB_ESCAPE_PREFIX_UP; /* shift to correct position */ 1329 pEscapeSequenceInfo[codewordOffset] |= 1330 escapePrefixUp; /* insert new escapePrefixUp */ 1331 escapePrefixUp >>= LSB_ESCAPE_PREFIX_UP; /* shift back down */ 1332 } else { /* separator [zero] reached */ 1333 pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of 1334 for loop counter (see 1335 above) is done here */ 1336 escapePrefixUp += 1337 4; /* if escape_separator '0' appears, add 4 and ==> break */ 1338 1339 /* store escapePrefixUp in pEscapeSequenceInfo[codewordOffset] at bit 1340 * position escapePrefixUp */ 1341 pEscapeSequenceInfo[codewordOffset] &= 1342 ~MASK_ESCAPE_PREFIX_UP; /* delete old escapePrefixUp */ 1343 escapePrefixUp <<= LSB_ESCAPE_PREFIX_UP; /* shift to correct position */ 1344 pEscapeSequenceInfo[codewordOffset] |= 1345 escapePrefixUp; /* insert new escapePrefixUp */ 1346 escapePrefixUp >>= LSB_ESCAPE_PREFIX_UP; /* shift back down */ 1347 1348 /* store escapePrefixUp in pEscapeSequenceInfo[codewordOffset] at bit 1349 * position escapePrefixDown */ 1350 pEscapeSequenceInfo[codewordOffset] &= 1351 ~MASK_ESCAPE_PREFIX_DOWN; /* delete old escapePrefixDown */ 1352 escapePrefixUp <<= LSB_ESCAPE_PREFIX_DOWN; /* shift to correct position */ 1353 pEscapeSequenceInfo[codewordOffset] |= 1354 escapePrefixUp; /* insert new escapePrefixDown */ 1355 1356 pSta[codewordOffset] = BODY_SIGN_ESC__ESC_WORD; /* set next state */ 1357 pHcr->nonPcwSideinfo.pState = 1358 aStateConstant2State[pSta[codewordOffset]]; /* get state from separate 1359 array of cw-sideinfo */ 1360 break; 1361 } 1362 } 1363 1364 if (pRemainingBitsInSegment[segmentOffset] <= 0) { 1365 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 1366 pSegmentBitfield); /* clear a bit in bitfield and 1367 switch off statemachine */ 1368 1369 if (pRemainingBitsInSegment[segmentOffset] < 0) { 1370 pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN_ESC__ESC_PREFIX; 1371 return BODY_SIGN_ESC__ESC_PREFIX; 1372 } 1373 } 1374 1375 return STOP_THIS_STATE; 1376} 1377 1378/*--------------------------------------------------------------------------------------------- 1379 description: Decode escapeWord of escape sequence. If the escape sequence 1380is decoded completely, assemble quantized-spectral-escape-coefficient and 1381replace the previous decoded 16 by the new value. Test flagB. If flagB is set, 1382the second escape sequence must be decoded. If flagB is not set, the codeword is 1383decoded and the state machine is switched off. 1384----------------------------------------------------------------------------------------------- 1385 output: Two lines with valid sign. At least one of both lines has got 1386the correct value. 1387----------------------------------------------------------------------------------------------- 1388 return: 0 1389-------------------------------------------------------------------------------------------- 1390*/ 1391UINT Hcr_State_BODY_SIGN_ESC__ESC_WORD(HANDLE_FDK_BITSTREAM bs, void *ptr) { 1392 H_HCR_INFO pHcr = (H_HCR_INFO)ptr; 1393 SCHAR *pRemainingBitsInSegment; 1394 INT *pLeftStartOfSegment; 1395 INT *pRightStartOfSegment; 1396 UCHAR readDirection; 1397 UINT *pSegmentBitfield; 1398 UINT *pCodewordBitfield; 1399 UINT segmentOffset; 1400 1401 FIXP_DBL *pResultBase; 1402 USHORT *iResultPointer; 1403 UINT *pEscapeSequenceInfo; 1404 UINT codewordOffset; 1405 1406 UINT escapeWord; 1407 UINT escapePrefixDown; 1408 UINT escapePrefixUp; 1409 UCHAR carryBit; 1410 UINT iQSC; 1411 INT sign; 1412 UINT flagA; 1413 UINT flagB; 1414 SCHAR *pSta; 1415 1416 pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment; 1417 pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment; 1418 pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment; 1419 readDirection = pHcr->segmentInfo.readDirection; 1420 pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield; 1421 pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield; 1422 segmentOffset = pHcr->segmentInfo.segmentOffset; 1423 1424 pResultBase = pHcr->nonPcwSideinfo.pResultBase; 1425 iResultPointer = pHcr->nonPcwSideinfo.iResultPointer; 1426 pEscapeSequenceInfo = pHcr->nonPcwSideinfo.pEscapeSequenceInfo; 1427 codewordOffset = pHcr->nonPcwSideinfo.codewordOffset; 1428 pSta = pHcr->nonPcwSideinfo.pSta; 1429 1430 escapeWord = pEscapeSequenceInfo[codewordOffset] & MASK_ESCAPE_WORD; 1431 escapePrefixDown = 1432 (pEscapeSequenceInfo[codewordOffset] & MASK_ESCAPE_PREFIX_DOWN) >> 1433 LSB_ESCAPE_PREFIX_DOWN; 1434 1435 /* decode escape word */ 1436 for (; pRemainingBitsInSegment[segmentOffset] > 0; 1437 pRemainingBitsInSegment[segmentOffset] -= 1) { 1438 carryBit = HcrGetABitFromBitstream(bs, &pLeftStartOfSegment[segmentOffset], 1439 &pRightStartOfSegment[segmentOffset], 1440 readDirection); 1441 1442 /* build escape word */ 1443 escapeWord <<= 1444 1; /* left shift previous decoded part of escapeWord by on bit */ 1445 escapeWord = escapeWord | carryBit; /* assemble escape word by bitwise or */ 1446 1447 /* decrement counter for length of escape word because one more bit was 1448 * decoded */ 1449 escapePrefixDown -= 1; 1450 1451 /* store updated escapePrefixDown */ 1452 pEscapeSequenceInfo[codewordOffset] &= 1453 ~MASK_ESCAPE_PREFIX_DOWN; /* delete old escapePrefixDown */ 1454 escapePrefixDown <<= LSB_ESCAPE_PREFIX_DOWN; /* shift to correct position */ 1455 pEscapeSequenceInfo[codewordOffset] |= 1456 escapePrefixDown; /* insert new escapePrefixDown */ 1457 escapePrefixDown >>= LSB_ESCAPE_PREFIX_DOWN; /* shift back */ 1458 1459 /* store updated escapeWord */ 1460 pEscapeSequenceInfo[codewordOffset] &= 1461 ~MASK_ESCAPE_WORD; /* delete old escapeWord */ 1462 pEscapeSequenceInfo[codewordOffset] |= 1463 escapeWord; /* insert new escapeWord */ 1464 1465 if (escapePrefixDown == 0) { 1466 pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of 1467 for loop counter (see 1468 above) is done here */ 1469 1470 /* escape sequence decoded. Assemble escape-line and replace original line 1471 */ 1472 1473 /* step 0 */ 1474 /* derive sign */ 1475 iQSC = iResultPointer[codewordOffset]; 1476 sign = (pResultBase[iQSC] >= (FIXP_DBL)0) 1477 ? 1 1478 : -1; /* get sign of escape value 16 */ 1479 1480 /* step 1 */ 1481 /* get escapePrefixUp */ 1482 escapePrefixUp = 1483 (pEscapeSequenceInfo[codewordOffset] & MASK_ESCAPE_PREFIX_UP) >> 1484 LSB_ESCAPE_PREFIX_UP; 1485 1486 /* step 2 */ 1487 /* calculate escape value */ 1488 pResultBase[iQSC] = 1489 (FIXP_DBL)(sign * (((INT)1 << escapePrefixUp) + (INT)escapeWord)); 1490 1491 /* get both flags from sideinfo (flags are not shifted to the 1492 * lsb-position) */ 1493 flagA = pEscapeSequenceInfo[codewordOffset] & MASK_FLAG_A; 1494 flagB = pEscapeSequenceInfo[codewordOffset] & MASK_FLAG_B; 1495 1496 /* step 3 */ 1497 /* clear the whole escape sideinfo word */ 1498 pEscapeSequenceInfo[codewordOffset] = 0; 1499 1500 /* change state in dependence of flag flagB */ 1501 if (flagA != 0) { 1502 /* first escape sequence decoded; previous decoded 16 has been replaced 1503 * by valid line */ 1504 1505 /* clear flagA in sideinfo word because this escape sequence has already 1506 * beed decoded */ 1507 pEscapeSequenceInfo[codewordOffset] &= ~MASK_FLAG_A; 1508 1509 if (flagB == 0) { 1510 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 1511 pCodewordBitfield); /* clear a bit in bitfield 1512 and switch off 1513 statemachine */ 1514 } else { 1515 /* updated pointer to next and last 16 */ 1516 iQSC++; 1517 iResultPointer[codewordOffset] = iQSC; 1518 1519 /* change state */ 1520 pSta[codewordOffset] = BODY_SIGN_ESC__ESC_PREFIX; 1521 pHcr->nonPcwSideinfo.pState = 1522 aStateConstant2State[pSta[codewordOffset]]; /* get state from 1523 separate array of 1524 cw-sideinfo */ 1525 } 1526 } else { 1527 ClearBitFromBitfield( 1528 &(pHcr->nonPcwSideinfo.pState), segmentOffset, 1529 pCodewordBitfield); /* clear a bit in bitfield and switch off 1530 statemachine */ 1531 } 1532 break; 1533 } 1534 } 1535 1536 if (pRemainingBitsInSegment[segmentOffset] <= 0) { 1537 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset, 1538 pSegmentBitfield); /* clear a bit in bitfield and 1539 switch off statemachine */ 1540 1541 if (pRemainingBitsInSegment[segmentOffset] < 0) { 1542 pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN_ESC__ESC_WORD; 1543 return BODY_SIGN_ESC__ESC_WORD; 1544 } 1545 } 1546 1547 return STOP_THIS_STATE; 1548} 1549