2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
5 © Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and the date of any change. For modified versions of the FDK AAC Codec, the term
50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
73 5. CONTACT INFORMATION
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
78 91058 Erlangen, Germany
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
84 /***************************** MPEG-4 AAC Decoder ***************************
86 Author(s): Robert Weidner (DSP Solutions)
87 Description: HCR Decoder: Prepare decoding of non-PCWs, segmentation- and
88 bitfield-handling, HCR-Statemachine
90 *******************************************************************************/
92 #include "aacdec_hcrs.h"
95 #include "aacdec_hcr.h"
97 #include "aacdec_hcr_bit.h"
102 static UINT
InitSegmentBitfield(UINT
*pNumSegment
,
103 SCHAR
*pRemainingBitsInSegment
,
104 UINT
*pSegmentBitfield
,
105 UCHAR
*pNumWordForBitfield
,
106 USHORT
*pNumBitValidInLastWord
);
108 static void InitNonPCWSideInformationForCurrentSet(H_HCR_INFO pHcr
);
110 static INT
ModuloValue(INT input
, INT bufferlength
);
112 static void ClearBitFromBitfield(STATEFUNC
*ptrState
,
117 /*---------------------------------------------------------------------------------------------
118 description: This function decodes all non-priority codewords (non-PCWs) by using a
120 -------------------------------------------------------------------------------------------- */
121 void DecodeNonPCWs(HANDLE_FDK_BITSTREAM bs
, H_HCR_INFO pHcr
)
123 UINT numValidSegment
;
125 INT codewordOffsetBase
;
130 SCHAR
*pRemainingBitsInSegment
;
131 UINT
*pSegmentBitfield
;
132 UCHAR
*pNumWordForBitfield
;
133 USHORT
*pNumBitValidInLastWord
;
134 UINT
*pCodewordBitfield
;
138 UINT interMediateWord
;
146 UINT remainingCodewordsInSet
;
150 pNumSegment
= &(pHcr
->segmentInfo
.numSegment
);
151 pRemainingBitsInSegment
= pHcr
->segmentInfo
.pRemainingBitsInSegment
;
152 pSegmentBitfield
= pHcr
->segmentInfo
.pSegmentBitfield
;
153 pNumWordForBitfield
= &(pHcr
->segmentInfo
.numWordForBitfield
);
154 pNumBitValidInLastWord
= &(pHcr
->segmentInfo
.pNumBitValidInLastWord
);
155 pSta
= pHcr
->nonPcwSideinfo
.pSta
;
157 numValidSegment
= InitSegmentBitfield(pNumSegment
,
158 pRemainingBitsInSegment
,
161 pNumBitValidInLastWord
);
163 if ( numValidSegment
!= 0 ) {
164 numCodeword
= pHcr
->sectionInfo
.numCodeword
;
165 numSet
= ((numCodeword
- 1) / *pNumSegment
) + 1;
168 pHcr
->segmentInfo
.readDirection
= FROM_RIGHT_TO_LEFT
;
170 /* Process sets subsequently */
171 for ( currentSet
= 1; currentSet
< numSet
; currentSet
++ ) {
176 numCodeword
-= *pNumSegment
; /* number of remaining non PCWs [for all sets] */
177 if ( numCodeword
< *pNumSegment
) {
178 codewordInSet
= numCodeword
; /* for last set */
181 codewordInSet
= *pNumSegment
; /* for all sets except last set */
185 /* prepare array 'CodewordBitfield'; as much ones are written from left in all words, as much decodedCodewordInSetCounter nonPCWs exist in this set */
186 tempWord
= 0xFFFFFFFF;
187 pCodewordBitfield
= pHcr
->segmentInfo
.pCodewordBitfield
;
189 for ( bitfieldWord
= *pNumWordForBitfield
; bitfieldWord
!=0; bitfieldWord
-- ) { /* loop over all used words */
190 if ( codewordInSet
> NUMBER_OF_BIT_IN_WORD
) { /* more codewords than number of bits => fill ones */
191 /* fill a whole word with ones */
192 *pCodewordBitfield
++ = tempWord
;
193 codewordInSet
-= NUMBER_OF_BIT_IN_WORD
; /* subtract number of bits */
196 /* prepare last tempWord */
197 for (remainingCodewordsInSet
= codewordInSet
; remainingCodewordsInSet
< NUMBER_OF_BIT_IN_WORD
; remainingCodewordsInSet
++ ) {
198 tempWord
= tempWord
& ~(1 << (NUMBER_OF_BIT_IN_WORD
-1-remainingCodewordsInSet
)); /* set a zero at bit number (NUMBER_OF_BIT_IN_WORD-1-i) in tempWord */
200 *pCodewordBitfield
++ = tempWord
;
201 tempWord
= 0x00000000;
204 pCodewordBitfield
= pHcr
->segmentInfo
.pCodewordBitfield
;
207 /* build non-PCW sideinfo for each non-PCW of the current set */
208 InitNonPCWSideInformationForCurrentSet(pHcr
);
211 /* decode all non-PCWs belonging to this set */
213 /* loop over trials */
214 codewordOffsetBase
= 0;
215 for ( trial
= *pNumSegment
; trial
> 0; trial
-- ) {
217 /* loop over number of words in bitfields */
218 segmentOffset
= 0; /* start at zero in every segment */
219 pHcr
->segmentInfo
.segmentOffset
= segmentOffset
; /* store in structure for states */
220 codewordOffset
= codewordOffsetBase
;
221 pHcr
->nonPcwSideinfo
.codewordOffset
= codewordOffset
; /* store in structure for states */
223 for ( bitfieldWord
=0; bitfieldWord
< *pNumWordForBitfield
; bitfieldWord
++ ) {
225 /* derive tempWord with bitwise and */
226 tempWord
= pSegmentBitfield
[bitfieldWord
] & pCodewordBitfield
[bitfieldWord
];
228 /* if tempWord is not zero, decode something */
229 if ( tempWord
!= 0 ) {
232 /* loop over all bits in tempWord; start state machine if & is true */
233 for ( bitInWord
= NUMBER_OF_BIT_IN_WORD
; bitInWord
> 0; bitInWord
-- ) {
235 interMediateWord
= ((UINT
)1 << (bitInWord
-1) );
236 if ( ( tempWord
& interMediateWord
) == interMediateWord
) {
238 /* get state and start state machine */
239 pHcr
->nonPcwSideinfo
.pState
= aStateConstant2State
[pSta
[codewordOffset
]];
241 while(pHcr
->nonPcwSideinfo
.pState
) {
242 ret
= ((STATEFUNC
) pHcr
->nonPcwSideinfo
.pState
)(bs
, pHcr
);
243 #if STATE_MACHINE_ERROR_CHECK
251 /* update both offsets */
252 segmentOffset
+= 1; /* add NUMBER_OF_BIT_IN_WORD times one */
253 pHcr
->segmentInfo
.segmentOffset
= segmentOffset
;
254 codewordOffset
+= 1; /* add NUMBER_OF_BIT_IN_WORD times one */
255 codewordOffset
= ModuloValue(codewordOffset
,*pNumSegment
); /* index of the current codeword lies within modulo range */
256 pHcr
->nonPcwSideinfo
.codewordOffset
= codewordOffset
;
260 segmentOffset
+= NUMBER_OF_BIT_IN_WORD
; /* add NUMBER_OF_BIT_IN_WORD at once */
261 pHcr
->segmentInfo
.segmentOffset
= segmentOffset
;
262 codewordOffset
+= NUMBER_OF_BIT_IN_WORD
; /* add NUMBER_OF_BIT_IN_WORD at once */
263 codewordOffset
= ModuloValue(codewordOffset
,*pNumSegment
); /* index of the current codeword lies within modulo range */
264 pHcr
->nonPcwSideinfo
.codewordOffset
= codewordOffset
;
266 } /* end of bitfield word loop */
268 /* decrement codeword - pointer */
269 codewordOffsetBase
-= 1;
270 codewordOffsetBase
= ModuloValue(codewordOffsetBase
,*pNumSegment
); /* index of the current codeword base lies within modulo range */
272 /* rotate numSegment bits in codewordBitfield */
273 /* rotation of *numSegment bits in bitfield of codewords (circle-rotation) */
274 /* get last valid bit */
275 tempBit
= pCodewordBitfield
[*pNumWordForBitfield
-1] & (1 << (NUMBER_OF_BIT_IN_WORD
- *pNumBitValidInLastWord
));
276 tempBit
= tempBit
>> (NUMBER_OF_BIT_IN_WORD
- *pNumBitValidInLastWord
);
278 /* write zero into place where tempBit was fetched from */
279 pCodewordBitfield
[*pNumWordForBitfield
-1] = pCodewordBitfield
[*pNumWordForBitfield
-1] & ~(1 << (NUMBER_OF_BIT_IN_WORD
- *pNumBitValidInLastWord
));
281 /* rotate last valid word */
282 pCodewordBitfield
[*pNumWordForBitfield
-1] = pCodewordBitfield
[*pNumWordForBitfield
-1] >> 1;
284 /* transfare carry bit 0 from current word into bitposition 31 from next word and rotate current word */
285 for ( bitfieldWord
= *pNumWordForBitfield
-2; bitfieldWord
> -1 ; bitfieldWord
-- ) {
286 /* get carry (=bit at position 0) from current word */
287 carry
= pCodewordBitfield
[bitfieldWord
] & 1;
289 /* put the carry bit at position 31 into word right from current word */
290 pCodewordBitfield
[bitfieldWord
+1] = pCodewordBitfield
[bitfieldWord
+1] | (carry
<< (NUMBER_OF_BIT_IN_WORD
-1));
292 /* shift current word */
293 pCodewordBitfield
[bitfieldWord
] = pCodewordBitfield
[bitfieldWord
] >> 1;
296 /* put tempBit into free bit-position 31 from first word */
297 pCodewordBitfield
[0] = pCodewordBitfield
[0] | (tempBit
<< (NUMBER_OF_BIT_IN_WORD
-1));
299 } /* end of trial loop */
301 /* toggle read direction */
302 pHcr
->segmentInfo
.readDirection
= ToggleReadDirection(pHcr
->segmentInfo
.readDirection
);
305 /* end of set loop */
307 /* all non-PCWs of this spectrum are decoded */
310 /* all PCWs and all non PCWs are decoded. They are unbacksorted in output buffer. Here is the Interface with comparing QSCs to asm decoding */
314 /*---------------------------------------------------------------------------------------------
315 description: This function prepares the bitfield used for the
316 segments. The list is set up once to be used in all following sets. If a
317 segment is decoded empty, the according bit from the Bitfield is removed.
318 -----------------------------------------------------------------------------------------------
319 return: numValidSegment = the number of valid segments
320 -------------------------------------------------------------------------------------------- */
321 static UINT
InitSegmentBitfield(UINT
*pNumSegment
,
322 SCHAR
*pRemainingBitsInSegment
,
323 UINT
*pSegmentBitfield
,
324 UCHAR
*pNumWordForBitfield
,
325 USHORT
*pNumBitValidInLastWord
)
331 USHORT numValidSegment
;
333 *pNumWordForBitfield
= ((*pNumSegment
-1) >> THIRTYTWO_LOG_DIV_TWO_LOG
) + 1;
335 /* loop over all words, which are completely used or only partial */
336 /* bit in pSegmentBitfield is zero if segment is empty; bit in pSegmentBitfield is one if segment is not empty */
338 *pNumBitValidInLastWord
= *pNumSegment
;
340 /* loop over words */
341 for ( bitfieldWord
=0; bitfieldWord
< *pNumWordForBitfield
- 1; bitfieldWord
++ ) {
342 tempWord
= 0xFFFFFFFF; /* set ones */
343 r
= bitfieldWord
<< THIRTYTWO_LOG_DIV_TWO_LOG
;
344 for ( i
=0; i
< NUMBER_OF_BIT_IN_WORD
; i
++) {
345 if ( pRemainingBitsInSegment
[r
+ i
] == 0 ) {
346 tempWord
= tempWord
& ~(1 << (NUMBER_OF_BIT_IN_WORD
-1-i
)); /* set a zero at bit number (NUMBER_OF_BIT_IN_WORD-1-i) in tempWord */
349 numValidSegment
+= 1; /* count segments which are not empty */
352 pSegmentBitfield
[bitfieldWord
] = tempWord
; /* store result */
353 *pNumBitValidInLastWord
-= NUMBER_OF_BIT_IN_WORD
; /* calculate number of zeros on LSB side in the last word */
357 /* calculate last word: prepare special tempWord */
358 tempWord
= 0xFFFFFFFF;
359 for ( i
=0; i
< ( NUMBER_OF_BIT_IN_WORD
- *pNumBitValidInLastWord
); i
++ ) {
360 tempWord
= tempWord
& ~(1 << i
); /* clear bit i in tempWord */
363 /* calculate last word */
364 r
= bitfieldWord
<< THIRTYTWO_LOG_DIV_TWO_LOG
;
365 for ( i
=0; i
<*pNumBitValidInLastWord
; i
++) {
366 if ( pRemainingBitsInSegment
[r
+ i
] == 0 ) {
367 tempWord
= tempWord
& ~(1 << (NUMBER_OF_BIT_IN_WORD
-1-i
)); /* set a zero at bit number (NUMBER_OF_BIT_IN_WORD-1-i) in tempWord */
370 numValidSegment
+= 1; /* count segments which are not empty */
373 pSegmentBitfield
[bitfieldWord
] = tempWord
; /* store result */
377 return numValidSegment
;
381 /*---------------------------------------------------------------------------------------------
382 description: This function sets up sideinfo for the non-PCW decoder (for the current set).
383 ---------------------------------------------------------------------------------------------*/
384 static void InitNonPCWSideInformationForCurrentSet(H_HCR_INFO pHcr
)
390 UCHAR
*pCodebook
= pHcr
->nonPcwSideinfo
.pCodebook
;
391 UINT
*iNode
= pHcr
->nonPcwSideinfo
.iNode
;
392 UCHAR
*pCntSign
= pHcr
->nonPcwSideinfo
.pCntSign
;
393 USHORT
*iResultPointer
= pHcr
->nonPcwSideinfo
.iResultPointer
;
394 UINT
*pEscapeSequenceInfo
= pHcr
->nonPcwSideinfo
.pEscapeSequenceInfo
;
395 SCHAR
*pSta
= pHcr
->nonPcwSideinfo
.pSta
;
396 USHORT
*pNumExtendedSortedCodewordInSection
= pHcr
->sectionInfo
.pNumExtendedSortedCodewordInSection
;
397 int numExtendedSortedCodewordInSectionIdx
= pHcr
->sectionInfo
.numExtendedSortedCodewordInSectionIdx
;
398 UCHAR
*pExtendedSortedCodebook
= pHcr
->sectionInfo
.pExtendedSortedCodebook
;
399 int extendedSortedCodebookIdx
= pHcr
->sectionInfo
.extendedSortedCodebookIdx
;
400 USHORT
*pNumExtendedSortedSectionsInSets
= pHcr
->sectionInfo
.pNumExtendedSortedSectionsInSets
;
401 int numExtendedSortedSectionsInSetsIdx
= pHcr
->sectionInfo
.numExtendedSortedSectionsInSetsIdx
;
402 FIXP_DBL
*pQuantizedSpectralCoefficients
= SPEC_LONG(pHcr
->decInOut
.pQuantizedSpectralCoefficientsBase
);
403 int quantizedSpectralCoefficientsIdx
= pHcr
->decInOut
.quantizedSpectralCoefficientsIdx
;
404 const UCHAR
*pCbDimension
= pHcr
->tableInfo
.pCbDimension
;
405 int iterationCounter
= 0;
407 /* loop over number of extended sorted sections in the current set so all codewords sideinfo variables within this set can be prepared for decoding */
408 for ( i
=pNumExtendedSortedSectionsInSets
[numExtendedSortedSectionsInSetsIdx
]; i
!= 0; i
-- ) {
410 codebookDim
= pCbDimension
[pExtendedSortedCodebook
[extendedSortedCodebookIdx
]];
411 startNode
= *aHuffTable
[pExtendedSortedCodebook
[extendedSortedCodebookIdx
]];
413 for ( k
= pNumExtendedSortedCodewordInSection
[numExtendedSortedCodewordInSectionIdx
]; k
!= 0; k
-- ) {
415 if (iterationCounter
> (1024>>2)) {
418 *pSta
++ = aCodebook2StartInt
[pExtendedSortedCodebook
[extendedSortedCodebookIdx
]];
419 *pCodebook
++ = pExtendedSortedCodebook
[extendedSortedCodebookIdx
];
420 *iNode
++ = startNode
;
422 *iResultPointer
++ = quantizedSpectralCoefficientsIdx
;
423 *pEscapeSequenceInfo
++ = 0;
424 quantizedSpectralCoefficientsIdx
+= codebookDim
; /* update pointer by codebookDim --> point to next starting value for writing out */
425 if (quantizedSpectralCoefficientsIdx
>= 1024) {
429 numExtendedSortedCodewordInSectionIdx
++; /* inc ptr for next ext sort sec in current set */
430 extendedSortedCodebookIdx
++; /* inc ptr for next ext sort sec in current set */
431 if (numExtendedSortedCodewordInSectionIdx
>= (MAX_SFB_HCR
+MAX_HCR_SETS
) || extendedSortedCodebookIdx
>= (MAX_SFB_HCR
+MAX_HCR_SETS
)) {
435 numExtendedSortedSectionsInSetsIdx
++; /* inc ptr for next set of non-PCWs */
436 if (numExtendedSortedCodewordInSectionIdx
>= (MAX_SFB_HCR
+MAX_HCR_SETS
)) {
440 /* Write back indexes */
441 pHcr
->sectionInfo
.numExtendedSortedCodewordInSectionIdx
= numExtendedSortedCodewordInSectionIdx
;
442 pHcr
->sectionInfo
.extendedSortedCodebookIdx
= extendedSortedCodebookIdx
;
443 pHcr
->sectionInfo
.numExtendedSortedSectionsInSetsIdx
= numExtendedSortedSectionsInSetsIdx
;
444 pHcr
->sectionInfo
.numExtendedSortedCodewordInSectionIdx
= numExtendedSortedCodewordInSectionIdx
;
445 pHcr
->decInOut
.quantizedSpectralCoefficientsIdx
= quantizedSpectralCoefficientsIdx
;
449 /*---------------------------------------------------------------------------------------------
450 description: This function returns the input value if the value is in the
451 range of bufferlength. If <input> is smaller, one bufferlength is added,
452 if <input> is bigger one bufferlength is subtracted.
453 -----------------------------------------------------------------------------------------------
454 return: modulo result
455 -------------------------------------------------------------------------------------------- */
456 static INT
ModuloValue(INT input
, INT bufferlength
)
458 if ( input
> (bufferlength
- 1) ) {
459 return (input
- bufferlength
);
462 return (input
+ bufferlength
);
468 /*---------------------------------------------------------------------------------------------
469 description: This function clears a bit from current bitfield and
470 switches off the statemachine.
472 A bit is cleared in two cases:
473 a) a codeword is decoded, then a bit is cleared in codeword bitfield
474 b) a segment is decoded empty, then a bit is cleared in segment bitfield
475 -------------------------------------------------------------------------------------------- */
476 static void ClearBitFromBitfield(STATEFUNC
*ptrState
,
480 UINT numBitfieldWord
;
483 /* get both values needed for clearing the bit */
484 numBitfieldWord
= offset
>> THIRTYTWO_LOG_DIV_TWO_LOG
; /* int = wordNr */
485 numBitfieldBit
= offset
- (numBitfieldWord
<< THIRTYTWO_LOG_DIV_TWO_LOG
); /* fract = bitNr */
487 /* clear a bit in bitfield */
488 pBitfield
[numBitfieldWord
] = pBitfield
[numBitfieldWord
] & ~(1 << (NUMBER_OF_BIT_IN_WORD
-1 - numBitfieldBit
));
490 /* switch off state machine because codeword is decoded and/or because segment is empty */
496 /* =========================================================================================
497 the states of the statemachine
498 ========================================================================================= */
501 /*---------------------------------------------------------------------------------------------
502 description: Decodes the body of a codeword. This State is used for codebooks 1,2,5 and 6.
503 No sign bits are decoded, because the table of the quantized spectral values
504 has got a valid sign at the quantized spectral lines.
505 -----------------------------------------------------------------------------------------------
506 output: Two or four quantizes spectral values written at position where pResultPointr
508 -----------------------------------------------------------------------------------------------
510 -------------------------------------------------------------------------------------------- */
511 UINT
Hcr_State_BODY_ONLY(HANDLE_FDK_BITSTREAM bs
, void *ptr
)
513 H_HCR_INFO pHcr
= (H_HCR_INFO
)ptr
;
514 UINT
*pSegmentBitfield
;
515 UINT
*pCodewordBitfield
;
517 FIXP_DBL
*pResultBase
;
519 USHORT
*iResultPointer
;
526 USHORT
*pLeftStartOfSegment
;
527 USHORT
*pRightStartOfSegment
;
528 SCHAR
*pRemainingBitsInSegment
;
532 const UINT
*pCurrentTree
;
533 const UCHAR
*pCbDimension
;
534 const SCHAR
*pQuantVal
;
535 const SCHAR
*pQuantValBase
;
537 pRemainingBitsInSegment
= pHcr
->segmentInfo
.pRemainingBitsInSegment
;
538 pLeftStartOfSegment
= pHcr
->segmentInfo
.pLeftStartOfSegment
;
539 pRightStartOfSegment
= pHcr
->segmentInfo
.pRightStartOfSegment
;
540 readDirection
= pHcr
->segmentInfo
.readDirection
;
541 pSegmentBitfield
= pHcr
->segmentInfo
.pSegmentBitfield
;
542 pCodewordBitfield
= pHcr
->segmentInfo
.pCodewordBitfield
;
543 segmentOffset
= pHcr
->segmentInfo
.segmentOffset
;
545 pCodebook
= pHcr
->nonPcwSideinfo
.pCodebook
;
546 iNode
= pHcr
->nonPcwSideinfo
.iNode
;
547 pResultBase
= pHcr
->nonPcwSideinfo
.pResultBase
;
548 iResultPointer
= pHcr
->nonPcwSideinfo
.iResultPointer
;
549 codewordOffset
= pHcr
->nonPcwSideinfo
.codewordOffset
;
551 pCbDimension
= pHcr
->tableInfo
.pCbDimension
;
553 treeNode
= iNode
[codewordOffset
];
554 pCurrentTree
= aHuffTable
[pCodebook
[codewordOffset
]];
557 for ( ; pRemainingBitsInSegment
[segmentOffset
] > 0 ; pRemainingBitsInSegment
[segmentOffset
] -= 1 ) {
559 carryBit
= HcrGetABitFromBitstream( bs
,
560 &pLeftStartOfSegment
[segmentOffset
],
561 &pRightStartOfSegment
[segmentOffset
],
564 CarryBitToBranchValue(carryBit
, /* make a step in decoding tree */
569 /* if end of branch reached write out lines and count bits needed for sign, otherwise store node in codeword sideinfo */
570 if ((branchNode
& TEST_BIT_10
) == TEST_BIT_10
) { /* test bit 10 ; ==> body is complete */
571 pQuantValBase
= aQuantTable
[pCodebook
[codewordOffset
]]; /* get base address of quantized values belonging to current codebook */
572 pQuantVal
= pQuantValBase
+ branchValue
; /* set pointer to first valid line [of 2 or 4 quantized values] */
574 iQSC
= iResultPointer
[codewordOffset
]; /* get position of first line for writing out result */
576 for ( dimCntr
= pCbDimension
[pCodebook
[codewordOffset
]]; dimCntr
!= 0; dimCntr
-- ) {
577 pResultBase
[iQSC
++] = (FIXP_DBL
)*pQuantVal
++; /* write out 2 or 4 lines into spectrum; no Sign bits available in this state */
580 ClearBitFromBitfield(&(pHcr
->nonPcwSideinfo
.pState
),
582 pCodewordBitfield
); /* clear a bit in bitfield and switch off statemachine */
583 pRemainingBitsInSegment
[segmentOffset
] -= 1; /* last reinitialzation of for loop counter (see above) is done here */
584 break; /* end of branch in tree reached i.e. a whole nonPCW-Body is decoded */
586 else { /* body is not decoded completely: */
587 treeNode
= *(pCurrentTree
+ branchValue
); /* update treeNode for further step in decoding tree */
590 iNode
[codewordOffset
] = treeNode
; /* store updated treeNode because maybe decoding of codeword body not finished yet */
592 if ( pRemainingBitsInSegment
[segmentOffset
] <= 0 ) {
593 ClearBitFromBitfield(&(pHcr
->nonPcwSideinfo
.pState
),
595 pSegmentBitfield
); /* clear a bit in bitfield and switch off statemachine */
597 #if STATE_MACHINE_ERROR_CHECK
598 if ( pRemainingBitsInSegment
[segmentOffset
] < 0 ) {
599 pHcr
->decInOut
.errorLog
|= STATE_ERROR_BODY_ONLY
;
605 return STOP_THIS_STATE
;
609 /*---------------------------------------------------------------------------------------------
610 description: Decodes the codeword body, writes out result and counts the number of quantized
611 spectral values, which are different form zero. For those values sign bits are
614 If sign bit counter cntSign is different from zero, switch to next state to
615 decode sign Bits there.
616 If sign bit counter cntSign is zero, no sign bits are needed and codeword is
618 -----------------------------------------------------------------------------------------------
619 output: Two or four written quantizes spectral values written at position where
620 pResultPointr points to. The signs of those lines may be wrong. If the signs
621 [on just one signle sign] is wrong, the next state will correct it.
622 -----------------------------------------------------------------------------------------------
624 -------------------------------------------------------------------------------------------- */
625 UINT
Hcr_State_BODY_SIGN__BODY(HANDLE_FDK_BITSTREAM bs
, void *ptr
)
627 H_HCR_INFO pHcr
= (H_HCR_INFO
)ptr
;
628 SCHAR
*pRemainingBitsInSegment
;
629 USHORT
*pLeftStartOfSegment
;
630 USHORT
*pRightStartOfSegment
;
632 UINT
*pSegmentBitfield
;
633 UINT
*pCodewordBitfield
;
639 FIXP_DBL
*pResultBase
;
640 USHORT
*iResultPointer
;
651 const UCHAR
*pCbDimension
;
652 const UINT
*pCurrentTree
;
653 const SCHAR
*pQuantValBase
;
654 const SCHAR
*pQuantVal
;
656 pRemainingBitsInSegment
= pHcr
->segmentInfo
.pRemainingBitsInSegment
;
657 pLeftStartOfSegment
= pHcr
->segmentInfo
.pLeftStartOfSegment
;
658 pRightStartOfSegment
= pHcr
->segmentInfo
.pRightStartOfSegment
;
659 readDirection
= pHcr
->segmentInfo
.readDirection
;
660 pSegmentBitfield
= pHcr
->segmentInfo
.pSegmentBitfield
;
661 pCodewordBitfield
= pHcr
->segmentInfo
.pCodewordBitfield
;
662 segmentOffset
= pHcr
->segmentInfo
.segmentOffset
;
664 pCodebook
= pHcr
->nonPcwSideinfo
.pCodebook
;
665 iNode
= pHcr
->nonPcwSideinfo
.iNode
;
666 pCntSign
= pHcr
->nonPcwSideinfo
.pCntSign
;
667 pResultBase
= pHcr
->nonPcwSideinfo
.pResultBase
;
668 iResultPointer
= pHcr
->nonPcwSideinfo
.iResultPointer
;
669 codewordOffset
= pHcr
->nonPcwSideinfo
.codewordOffset
;
670 pSta
= pHcr
->nonPcwSideinfo
.pSta
;
672 pCbDimension
= pHcr
->tableInfo
.pCbDimension
;
674 treeNode
= iNode
[codewordOffset
];
675 pCurrentTree
= aHuffTable
[pCodebook
[codewordOffset
]];
678 for ( ; pRemainingBitsInSegment
[segmentOffset
] > 0 ; pRemainingBitsInSegment
[segmentOffset
] -= 1 ) {
680 carryBit
= HcrGetABitFromBitstream( bs
,
681 &pLeftStartOfSegment
[segmentOffset
],
682 &pRightStartOfSegment
[segmentOffset
],
685 CarryBitToBranchValue(carryBit
, /* make a step in decoding tree */
690 /* if end of branch reached write out lines and count bits needed for sign, otherwise store node in codeword sideinfo */
691 if ((branchNode
& TEST_BIT_10
) == TEST_BIT_10
) { /* test bit 10 ; if set body complete */
692 /* body completely decoded; branchValue is valid, set pQuantVal to first (of two or four) quantized spectral coefficients */
693 pQuantValBase
= aQuantTable
[pCodebook
[codewordOffset
]]; /* get base address of quantized values belonging to current codebook */
694 pQuantVal
= pQuantValBase
+ branchValue
; /* set pointer to first valid line [of 2 or 4 quantized values] */
696 iQSC
= iResultPointer
[codewordOffset
]; /* get position of first line for writing result */
698 /* codeword decoding result is written out here: Write out 2 or 4 quantized spectral values with probably */
699 /* wrong sign and count number of values which are different from zero for sign bit decoding [which happens in next state] */
701 for ( dimCntr
= pCbDimension
[pCodebook
[codewordOffset
]]; dimCntr
!= 0; dimCntr
-- ) {
702 pResultBase
[iQSC
++] = (FIXP_DBL
)*pQuantVal
; /* write quant. spec. coef. into spectrum */
703 if ( *pQuantVal
++ != 0 ) {
708 if ( cntSign
== 0 ) {
709 ClearBitFromBitfield(&(pHcr
->nonPcwSideinfo
.pState
),
711 pCodewordBitfield
); /* clear a bit in bitfield and switch off statemachine */
714 pCntSign
[codewordOffset
] = cntSign
; /* write sign count result into codewordsideinfo of current codeword */
715 pSta
[codewordOffset
] = BODY_SIGN__SIGN
; /* change state */
716 pHcr
->nonPcwSideinfo
.pState
= aStateConstant2State
[pSta
[codewordOffset
]]; /* get state from separate array of cw-sideinfo */
718 pRemainingBitsInSegment
[segmentOffset
] -= 1; /* last reinitialzation of for loop counter (see above) is done here */
719 break; /* end of branch in tree reached i.e. a whole nonPCW-Body is decoded */
721 else {/* body is not decoded completely: */
722 treeNode
= *(pCurrentTree
+ branchValue
); /* update treeNode for further step in decoding tree */
725 iNode
[codewordOffset
] = treeNode
; /* store updated treeNode because maybe decoding of codeword body not finished yet */
727 if ( pRemainingBitsInSegment
[segmentOffset
] <= 0 ) {
728 ClearBitFromBitfield(&(pHcr
->nonPcwSideinfo
.pState
),
730 pSegmentBitfield
); /* clear a bit in bitfield and switch off statemachine */
732 #if STATE_MACHINE_ERROR_CHECK
733 if ( pRemainingBitsInSegment
[segmentOffset
] < 0 ) {
734 pHcr
->decInOut
.errorLog
|= STATE_ERROR_BODY_SIGN__BODY
;
735 return BODY_SIGN__BODY
;
740 return STOP_THIS_STATE
;
744 /*---------------------------------------------------------------------------------------------
745 description: This state decodes the sign bits belonging to a codeword. The state is called
746 as often in different "trials" until pCntSgn[codewordOffset] is zero.
747 -----------------------------------------------------------------------------------------------
748 output: The two or four quantizes spectral values (written in previous state) have
749 now the correct sign.
750 -----------------------------------------------------------------------------------------------
752 -------------------------------------------------------------------------------------------- */
753 UINT
Hcr_State_BODY_SIGN__SIGN(HANDLE_FDK_BITSTREAM bs
, void *ptr
)
755 H_HCR_INFO pHcr
= (H_HCR_INFO
)ptr
;
756 SCHAR
*pRemainingBitsInSegment
;
757 USHORT
*pLeftStartOfSegment
;
758 USHORT
*pRightStartOfSegment
;
760 UINT
*pSegmentBitfield
;
761 UINT
*pCodewordBitfield
;
765 FIXP_DBL
*pResultBase
;
766 USHORT
*iResultPointer
;
772 pRemainingBitsInSegment
= pHcr
->segmentInfo
.pRemainingBitsInSegment
;
773 pLeftStartOfSegment
= pHcr
->segmentInfo
.pLeftStartOfSegment
;
774 pRightStartOfSegment
= pHcr
->segmentInfo
.pRightStartOfSegment
;
775 readDirection
= pHcr
->segmentInfo
.readDirection
;
776 pSegmentBitfield
= pHcr
->segmentInfo
.pSegmentBitfield
;
777 pCodewordBitfield
= pHcr
->segmentInfo
.pCodewordBitfield
;
778 segmentOffset
= pHcr
->segmentInfo
.segmentOffset
;
780 pCntSign
= pHcr
->nonPcwSideinfo
.pCntSign
;
781 pResultBase
= pHcr
->nonPcwSideinfo
.pResultBase
;
782 iResultPointer
= pHcr
->nonPcwSideinfo
.iResultPointer
;
783 codewordOffset
= pHcr
->nonPcwSideinfo
.codewordOffset
;
784 iQSC
= iResultPointer
[codewordOffset
];
785 cntSign
= pCntSign
[codewordOffset
];
789 /* loop for sign bit decoding */
790 for ( ; pRemainingBitsInSegment
[segmentOffset
] > 0 ; pRemainingBitsInSegment
[segmentOffset
] -= 1 ) {
792 carryBit
= HcrGetABitFromBitstream( bs
,
793 &pLeftStartOfSegment
[segmentOffset
],
794 &pRightStartOfSegment
[segmentOffset
],
796 cntSign
-= 1; /* decrement sign counter because one sign bit has been read */
798 /* search for a line (which was decoded in previous state) which is not zero. [This value will get a sign] */
799 while ( pResultBase
[iQSC
] == (FIXP_DBL
)0 ) {
800 iQSC
++; /* points to current value different from zero */
802 return BODY_SIGN__SIGN
;
806 /* put sign together with line; if carryBit is zero, the sign is ok already; no write operation necessary in this case */
807 if ( carryBit
!= 0 ) {
808 pResultBase
[iQSC
] = -pResultBase
[iQSC
]; /* carryBit = 1 --> minus */
811 iQSC
++; /* update pointer to next (maybe valid) value */
813 if ( cntSign
== 0 ) { /* if (cntSign==0) ==> set state CODEWORD_DECODED */
814 ClearBitFromBitfield(&(pHcr
->nonPcwSideinfo
.pState
),
816 pCodewordBitfield
); /* clear a bit in bitfield and switch off statemachine */
817 pRemainingBitsInSegment
[segmentOffset
] -= 1; /* last reinitialzation of for loop counter (see above) is done here */
818 break; /* whole nonPCW-Body and according sign bits are decoded */
821 pCntSign
[codewordOffset
] = cntSign
;
822 iResultPointer
[codewordOffset
] = iQSC
; /* store updated pResultPointer */
824 if ( pRemainingBitsInSegment
[segmentOffset
] <= 0 ) {
825 ClearBitFromBitfield(&(pHcr
->nonPcwSideinfo
.pState
),
827 pSegmentBitfield
); /* clear a bit in bitfield and switch off statemachine */
829 #if STATE_MACHINE_ERROR_CHECK
830 if ( pRemainingBitsInSegment
[segmentOffset
] < 0 ) {
831 pHcr
->decInOut
.errorLog
|= STATE_ERROR_BODY_SIGN__SIGN
;
832 return BODY_SIGN__SIGN
;
837 return STOP_THIS_STATE
;
841 /*---------------------------------------------------------------------------------------------
842 description: Decodes the codeword body in case of codebook is 11. Writes out resulting
843 two or four lines [with probably wrong sign] and counts the number of
844 lines, which are different form zero. This information is needed in next
845 state where sign bits will be decoded, if necessary.
846 If sign bit counter cntSign is zero, no sign bits are needed and codeword is
848 -----------------------------------------------------------------------------------------------
849 output: Two lines (quantizes spectral coefficients) which are probably wrong. The
850 sign may be wrong and if one or two values is/are 16, the following states
851 will decode the escape sequence to correct the values which are wirtten here.
852 -----------------------------------------------------------------------------------------------
854 -------------------------------------------------------------------------------------------- */
855 UINT
Hcr_State_BODY_SIGN_ESC__BODY(HANDLE_FDK_BITSTREAM bs
, void *ptr
)
857 H_HCR_INFO pHcr
= (H_HCR_INFO
)ptr
;
858 SCHAR
*pRemainingBitsInSegment
;
859 USHORT
*pLeftStartOfSegment
;
860 USHORT
*pRightStartOfSegment
;
862 UINT
*pSegmentBitfield
;
863 UINT
*pCodewordBitfield
;
868 FIXP_DBL
*pResultBase
;
869 USHORT
*iResultPointer
;
880 const UINT
*pCurrentTree
;
881 const SCHAR
*pQuantValBase
;
882 const SCHAR
*pQuantVal
;
884 pRemainingBitsInSegment
= pHcr
->segmentInfo
.pRemainingBitsInSegment
;
885 pLeftStartOfSegment
= pHcr
->segmentInfo
.pLeftStartOfSegment
;
886 pRightStartOfSegment
= pHcr
->segmentInfo
.pRightStartOfSegment
;
887 readDirection
= pHcr
->segmentInfo
.readDirection
;
888 pSegmentBitfield
= pHcr
->segmentInfo
.pSegmentBitfield
;
889 pCodewordBitfield
= pHcr
->segmentInfo
.pCodewordBitfield
;
890 segmentOffset
= pHcr
->segmentInfo
.segmentOffset
;
892 iNode
= pHcr
->nonPcwSideinfo
.iNode
;
893 pCntSign
= pHcr
->nonPcwSideinfo
.pCntSign
;
894 pResultBase
= pHcr
->nonPcwSideinfo
.pResultBase
;
895 iResultPointer
= pHcr
->nonPcwSideinfo
.iResultPointer
;
896 codewordOffset
= pHcr
->nonPcwSideinfo
.codewordOffset
;
897 pSta
= pHcr
->nonPcwSideinfo
.pSta
;
899 treeNode
= iNode
[codewordOffset
];
900 pCurrentTree
= aHuffTable
[ESCAPE_CODEBOOK
];
903 for ( ; pRemainingBitsInSegment
[segmentOffset
] > 0 ; pRemainingBitsInSegment
[segmentOffset
] -= 1 ) {
905 carryBit
= HcrGetABitFromBitstream( bs
,
906 &pLeftStartOfSegment
[segmentOffset
],
907 &pRightStartOfSegment
[segmentOffset
],
910 /* make a step in tree */
911 CarryBitToBranchValue(carryBit
,
916 /* if end of branch reached write out lines and count bits needed for sign, otherwise store node in codeword sideinfo */
917 if ((branchNode
& TEST_BIT_10
) == TEST_BIT_10
) { /* test bit 10 ; if set body complete */
919 /* body completely decoded; branchValue is valid */
920 /* set pQuantVol to first (of two or four) quantized spectral coefficients */
921 pQuantValBase
= aQuantTable
[ESCAPE_CODEBOOK
]; /* get base address of quantized values belonging to current codebook */
922 pQuantVal
= pQuantValBase
+ branchValue
; /* set pointer to first valid line [of 2 or 4 quantized values] */
924 /* make backup from original resultPointer in node storage for state BODY_SIGN_ESC__SIGN */
925 iNode
[codewordOffset
] = iResultPointer
[codewordOffset
];
927 /* get position of first line for writing result */
928 iQSC
= iResultPointer
[codewordOffset
];
930 /* codeword decoding result is written out here: Write out 2 or 4 quantized spectral values with probably */
931 /* wrong sign and count number of values which are different from zero for sign bit decoding [which happens in next state] */
934 for ( dimCntr
= DIMENSION_OF_ESCAPE_CODEBOOK
; dimCntr
!= 0; dimCntr
-- ) {
935 pResultBase
[iQSC
++] = (FIXP_DBL
)*pQuantVal
; /* write quant. spec. coef. into spectrum */
936 if ( *pQuantVal
++ != 0 ) {
941 if ( cntSign
== 0 ) {
942 ClearBitFromBitfield(&(pHcr
->nonPcwSideinfo
.pState
),
944 pCodewordBitfield
); /* clear a bit in bitfield and switch off statemachine */
945 /* codeword decoded */
948 /* write sign count result into codewordsideinfo of current codeword */
949 pCntSign
[codewordOffset
] = cntSign
;
950 pSta
[codewordOffset
] = BODY_SIGN_ESC__SIGN
; /* change state */
951 pHcr
->nonPcwSideinfo
.pState
= aStateConstant2State
[pSta
[codewordOffset
]]; /* get state from separate array of cw-sideinfo */
953 pRemainingBitsInSegment
[segmentOffset
] -= 1; /* the last reinitialzation of for loop counter (see above) is done here */
954 break; /* end of branch in tree reached i.e. a whole nonPCW-Body is decoded */
956 else { /* body is not decoded completely: */
957 /* update treeNode for further step in decoding tree and store updated treeNode because maybe no more bits left in segment */
958 treeNode
= *(pCurrentTree
+ branchValue
);
959 iNode
[codewordOffset
] = treeNode
;
963 if ( pRemainingBitsInSegment
[segmentOffset
] <= 0 ) {
964 ClearBitFromBitfield(&(pHcr
->nonPcwSideinfo
.pState
),
966 pSegmentBitfield
); /* clear a bit in bitfield and switch off statemachine */
968 #if STATE_MACHINE_ERROR_CHECK
969 if ( pRemainingBitsInSegment
[segmentOffset
] < 0 ) {
970 pHcr
->decInOut
.errorLog
|= STATE_ERROR_BODY_SIGN_ESC__BODY
;
971 return BODY_SIGN_ESC__BODY
;
976 return STOP_THIS_STATE
;
980 /*---------------------------------------------------------------------------------------------
981 description: This state decodes the sign bits, if a codeword of codebook 11 needs some.
982 A flag named 'flagB' in codeword sideinfo is set, if the second line of
983 quantized spectral values is 16. The 'flagB' is used in case of decoding
984 of a escape sequence is necessary as far as the second line is concerned.
986 If only the first line needs an escape sequence, the flagB is cleared.
987 If only the second line needs an escape sequence, the flagB is not used.
989 For storing sideinfo in case of escape sequence decoding one single word
990 can be used for both escape sequences because they are decoded not at the
994 bit 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
995 ===== == == =========== =========== ===================================
998 res. flagA flagB escapePrefixUp escapePrefixDown escapeWord
1000 -----------------------------------------------------------------------------------------------
1001 output: Two lines with correct sign. If one or two values is/are 16, the lines are
1002 not valid, otherwise they are.
1003 -----------------------------------------------------------------------------------------------
1005 -------------------------------------------------------------------------------------------- */
1006 UINT
Hcr_State_BODY_SIGN_ESC__SIGN(HANDLE_FDK_BITSTREAM bs
, void *ptr
)
1008 H_HCR_INFO pHcr
= (H_HCR_INFO
)ptr
;
1009 SCHAR
*pRemainingBitsInSegment
;
1010 USHORT
*pLeftStartOfSegment
;
1011 USHORT
*pRightStartOfSegment
;
1012 UCHAR readDirection
;
1013 UINT
*pSegmentBitfield
;
1014 UINT
*pCodewordBitfield
;
1019 FIXP_DBL
*pResultBase
;
1020 USHORT
*iResultPointer
;
1021 UINT
*pEscapeSequenceInfo
;
1022 UINT codewordOffset
;
1032 pRemainingBitsInSegment
= pHcr
->segmentInfo
.pRemainingBitsInSegment
;
1033 pLeftStartOfSegment
= pHcr
->segmentInfo
.pLeftStartOfSegment
;
1034 pRightStartOfSegment
= pHcr
->segmentInfo
.pRightStartOfSegment
;
1035 readDirection
= pHcr
->segmentInfo
.readDirection
;
1036 pSegmentBitfield
= pHcr
->segmentInfo
.pSegmentBitfield
;
1037 pCodewordBitfield
= pHcr
->segmentInfo
.pCodewordBitfield
;
1038 segmentOffset
= pHcr
->segmentInfo
.segmentOffset
;
1040 iNode
= pHcr
->nonPcwSideinfo
.iNode
;
1041 pCntSign
= pHcr
->nonPcwSideinfo
.pCntSign
;
1042 pResultBase
= pHcr
->nonPcwSideinfo
.pResultBase
;
1043 iResultPointer
= pHcr
->nonPcwSideinfo
.iResultPointer
;
1044 pEscapeSequenceInfo
= pHcr
->nonPcwSideinfo
.pEscapeSequenceInfo
;
1045 codewordOffset
= pHcr
->nonPcwSideinfo
.codewordOffset
;
1046 pSta
= pHcr
->nonPcwSideinfo
.pSta
;
1048 iQSC
= iResultPointer
[codewordOffset
];
1049 cntSign
= pCntSign
[codewordOffset
];
1052 /* loop for sign bit decoding */
1053 for ( ; pRemainingBitsInSegment
[segmentOffset
] > 0 ; pRemainingBitsInSegment
[segmentOffset
] -= 1 ) {
1055 carryBit
= HcrGetABitFromBitstream( bs
,
1056 &pLeftStartOfSegment
[segmentOffset
],
1057 &pRightStartOfSegment
[segmentOffset
],
1060 /* decrement sign counter because one sign bit has been read */
1062 pCntSign
[codewordOffset
] = cntSign
;
1064 /* get a quantized spectral value (which was decoded in previous state) which is not zero. [This value will get a sign] */
1065 while ( pResultBase
[iQSC
] == (FIXP_DBL
)0 ) {
1068 iResultPointer
[codewordOffset
] = iQSC
;
1070 /* put negative sign together with quantized spectral value; if carryBit is zero, the sign is ok already; no write operation necessary in this case */
1071 if ( carryBit
!= 0 ) {
1072 pResultBase
[iQSC
] = - pResultBase
[iQSC
]; /* carryBit = 1 --> minus */
1074 iQSC
++; /* update index to next (maybe valid) value */
1075 iResultPointer
[codewordOffset
] = iQSC
;
1077 if ( cntSign
== 0 ) {
1078 /* all sign bits are decoded now */
1079 pRemainingBitsInSegment
[segmentOffset
] -= 1; /* last reinitialzation of for loop counter (see above) is done here */
1081 /* check decoded values if codeword is decoded: Check if one or two escape sequences 16 follow */
1084 /* restore pointer to first decoded quantized value [ = original pResultPointr] from index iNode prepared in State_BODY_SIGN_ESC__BODY */
1085 iQSC
= iNode
[codewordOffset
];
1088 /* test first value if escape sequence follows */
1089 flagA
= 0; /* for first possible escape sequence */
1090 if ( fixp_abs(pResultBase
[iQSC
++]) == (FIXP_DBL
)ESCAPE_VALUE
) {
1095 /* test second value if escape sequence follows */
1096 flagB
= 0; /* for second possible escape sequence */
1097 if ( fixp_abs(pResultBase
[iQSC
]) == (FIXP_DBL
)ESCAPE_VALUE
) {
1103 /* evaluate flag result and go on if necessary */
1104 if ( !flagA
&& !flagB
) {
1105 ClearBitFromBitfield(&(pHcr
->nonPcwSideinfo
.pState
),
1107 pCodewordBitfield
); /* clear a bit in bitfield and switch off statemachine */
1110 /* at least one of two lines is 16 */
1111 /* store both flags at correct positions in non PCW codeword sideinfo pEscapeSequenceInfo[codewordOffset] */
1113 flags
= flagA
<< POSITION_OF_FLAG_A
;
1114 flags
|= (flagB
<< POSITION_OF_FLAG_B
);
1115 pEscapeSequenceInfo
[codewordOffset
] = flags
;
1118 /* set next state */
1119 pSta
[codewordOffset
] = BODY_SIGN_ESC__ESC_PREFIX
;
1120 pHcr
->nonPcwSideinfo
.pState
= aStateConstant2State
[pSta
[codewordOffset
]]; /* get state from separate array of cw-sideinfo */
1122 /* set result pointer to the first line of the two decoded lines */
1123 iResultPointer
[codewordOffset
] = iNode
[codewordOffset
];
1125 if ( !flagA
&& flagB
) {
1126 /* update pResultPointr ==> state Stat_BODY_SIGN_ESC__ESC_WORD writes to correct position. Second value is the one and only escape value */
1127 iQSC
= iResultPointer
[codewordOffset
];
1129 iResultPointer
[codewordOffset
] = iQSC
;
1132 } /* at least one of two lines is 16 */
1133 break; /* nonPCW-Body at cb 11 and according sign bits are decoded */
1135 } /* if ( cntSign == 0 ) */
1136 } /* loop over remaining Bits in segment */
1138 if ( pRemainingBitsInSegment
[segmentOffset
] <= 0 ) {
1139 ClearBitFromBitfield(&(pHcr
->nonPcwSideinfo
.pState
),
1141 pSegmentBitfield
); /* clear a bit in bitfield and switch off statemachine */
1143 #if STATE_MACHINE_ERROR_CHECK
1144 if ( pRemainingBitsInSegment
[segmentOffset
] < 0 ) {
1145 pHcr
->decInOut
.errorLog
|= STATE_ERROR_BODY_SIGN_ESC__SIGN
;
1146 return BODY_SIGN_ESC__SIGN
;
1151 return STOP_THIS_STATE
;
1155 /*---------------------------------------------------------------------------------------------
1156 description: Decode escape prefix of first or second escape sequence. The escape prefix
1157 consists of ones. The following zero is also decoded here.
1158 -----------------------------------------------------------------------------------------------
1159 output: If the single separator-zero which follows the escape-prefix-ones is not yet decoded:
1160 The value 'escapePrefixUp' in word pEscapeSequenceInfo[codewordOffset] is updated.
1162 If the single separator-zero which follows the escape-prefix-ones is decoded:
1163 Two updated values 'escapePrefixUp' and 'escapePrefixDown' in word
1164 pEscapeSequenceInfo[codewordOffset]. This State is finished. Switch to next state.
1165 -----------------------------------------------------------------------------------------------
1167 -------------------------------------------------------------------------------------------- */
1168 UINT
Hcr_State_BODY_SIGN_ESC__ESC_PREFIX(HANDLE_FDK_BITSTREAM bs
, void *ptr
)
1170 H_HCR_INFO pHcr
= (H_HCR_INFO
)ptr
;
1171 SCHAR
*pRemainingBitsInSegment
;
1172 USHORT
*pLeftStartOfSegment
;
1173 USHORT
*pRightStartOfSegment
;
1174 UCHAR readDirection
;
1175 UINT
*pSegmentBitfield
;
1177 UINT
*pEscapeSequenceInfo
;
1178 UINT codewordOffset
;
1180 UINT escapePrefixUp
;
1183 pRemainingBitsInSegment
= pHcr
->segmentInfo
.pRemainingBitsInSegment
;
1184 pLeftStartOfSegment
= pHcr
->segmentInfo
.pLeftStartOfSegment
;
1185 pRightStartOfSegment
= pHcr
->segmentInfo
.pRightStartOfSegment
;
1186 readDirection
= pHcr
->segmentInfo
.readDirection
;
1187 pSegmentBitfield
= pHcr
->segmentInfo
.pSegmentBitfield
;
1188 segmentOffset
= pHcr
->segmentInfo
.segmentOffset
;
1189 pEscapeSequenceInfo
= pHcr
->nonPcwSideinfo
.pEscapeSequenceInfo
;
1190 codewordOffset
= pHcr
->nonPcwSideinfo
.codewordOffset
;
1191 pSta
= pHcr
->nonPcwSideinfo
.pSta
;
1193 escapePrefixUp
= (pEscapeSequenceInfo
[codewordOffset
] & MASK_ESCAPE_PREFIX_UP
) >> LSB_ESCAPE_PREFIX_UP
;
1196 /* decode escape prefix */
1197 for ( ; pRemainingBitsInSegment
[segmentOffset
] > 0 ; pRemainingBitsInSegment
[segmentOffset
] -= 1 ) {
1199 carryBit
= HcrGetABitFromBitstream( bs
,
1200 &pLeftStartOfSegment
[segmentOffset
],
1201 &pRightStartOfSegment
[segmentOffset
],
1204 /* count ones and store sum in escapePrefixUp */
1205 if ( carryBit
== 1 ) {
1206 escapePrefixUp
+= 1; /* update conter for ones */
1208 /* store updated counter in sideinfo of current codeword */
1209 pEscapeSequenceInfo
[codewordOffset
] &= ~MASK_ESCAPE_PREFIX_UP
; /* delete old escapePrefixUp */
1210 escapePrefixUp
<<= LSB_ESCAPE_PREFIX_UP
; /* shift to correct position */
1211 pEscapeSequenceInfo
[codewordOffset
] |= escapePrefixUp
; /* insert new escapePrefixUp */
1212 escapePrefixUp
>>= LSB_ESCAPE_PREFIX_UP
; /* shift back down */
1214 else { /* separator [zero] reached */
1215 pRemainingBitsInSegment
[segmentOffset
] -= 1; /* last reinitialzation of for loop counter (see above) is done here */
1216 escapePrefixUp
+= 4; /* if escape_separator '0' appears, add 4 and ==> break */
1218 /* store escapePrefixUp in pEscapeSequenceInfo[codewordOffset] at bit position escapePrefixUp */
1219 pEscapeSequenceInfo
[codewordOffset
] &= ~MASK_ESCAPE_PREFIX_UP
; /* delete old escapePrefixUp */
1220 escapePrefixUp
<<= LSB_ESCAPE_PREFIX_UP
; /* shift to correct position */
1221 pEscapeSequenceInfo
[codewordOffset
] |= escapePrefixUp
; /* insert new escapePrefixUp */
1222 escapePrefixUp
>>= LSB_ESCAPE_PREFIX_UP
; /* shift back down */
1224 /* store escapePrefixUp in pEscapeSequenceInfo[codewordOffset] at bit position escapePrefixDown */
1225 pEscapeSequenceInfo
[codewordOffset
] &= ~MASK_ESCAPE_PREFIX_DOWN
; /* delete old escapePrefixDown */
1226 escapePrefixUp
<<= LSB_ESCAPE_PREFIX_DOWN
; /* shift to correct position */
1227 pEscapeSequenceInfo
[codewordOffset
] |= escapePrefixUp
; /* insert new escapePrefixDown */
1228 escapePrefixUp
>>= LSB_ESCAPE_PREFIX_DOWN
; /* shift back down */
1230 pSta
[codewordOffset
] = BODY_SIGN_ESC__ESC_WORD
; /* set next state */
1231 pHcr
->nonPcwSideinfo
.pState
= aStateConstant2State
[pSta
[codewordOffset
]]; /* get state from separate array of cw-sideinfo */
1236 if ( pRemainingBitsInSegment
[segmentOffset
] <= 0 ) {
1237 ClearBitFromBitfield(&(pHcr
->nonPcwSideinfo
.pState
),
1239 pSegmentBitfield
); /* clear a bit in bitfield and switch off statemachine */
1241 #if STATE_MACHINE_ERROR_CHECK
1242 if ( pRemainingBitsInSegment
[segmentOffset
] < 0 ) {
1243 pHcr
->decInOut
.errorLog
|= STATE_ERROR_BODY_SIGN_ESC__ESC_PREFIX
;
1244 return BODY_SIGN_ESC__ESC_PREFIX
;
1249 return STOP_THIS_STATE
;
1253 /*---------------------------------------------------------------------------------------------
1254 description: Decode escapeWord of escape sequence. If the escape sequence is decoded
1255 completely, assemble quantized-spectral-escape-coefficient and replace the
1256 previous decoded 16 by the new value.
1257 Test flagB. If flagB is set, the second escape sequence must be decoded. If
1258 flagB is not set, the codeword is decoded and the state machine is switched
1260 -----------------------------------------------------------------------------------------------
1261 output: Two lines with valid sign. At least one of both lines has got the correct
1263 -----------------------------------------------------------------------------------------------
1265 -------------------------------------------------------------------------------------------- */
1266 UINT
Hcr_State_BODY_SIGN_ESC__ESC_WORD(HANDLE_FDK_BITSTREAM bs
, void *ptr
)
1268 H_HCR_INFO pHcr
= (H_HCR_INFO
)ptr
;
1269 SCHAR
*pRemainingBitsInSegment
;
1270 USHORT
*pLeftStartOfSegment
;
1271 USHORT
*pRightStartOfSegment
;
1272 UCHAR readDirection
;
1273 UINT
*pSegmentBitfield
;
1274 UINT
*pCodewordBitfield
;
1277 FIXP_DBL
*pResultBase
;
1278 USHORT
*iResultPointer
;
1279 UINT
*pEscapeSequenceInfo
;
1280 UINT codewordOffset
;
1283 UINT escapePrefixDown
;
1284 UINT escapePrefixUp
;
1292 pRemainingBitsInSegment
= pHcr
->segmentInfo
.pRemainingBitsInSegment
;
1293 pLeftStartOfSegment
= pHcr
->segmentInfo
.pLeftStartOfSegment
;
1294 pRightStartOfSegment
= pHcr
->segmentInfo
.pRightStartOfSegment
;
1295 readDirection
= pHcr
->segmentInfo
.readDirection
;
1296 pSegmentBitfield
= pHcr
->segmentInfo
.pSegmentBitfield
;
1297 pCodewordBitfield
= pHcr
->segmentInfo
.pCodewordBitfield
;
1298 segmentOffset
= pHcr
->segmentInfo
.segmentOffset
;
1300 pResultBase
= pHcr
->nonPcwSideinfo
.pResultBase
;
1301 iResultPointer
= pHcr
->nonPcwSideinfo
.iResultPointer
;
1302 pEscapeSequenceInfo
= pHcr
->nonPcwSideinfo
.pEscapeSequenceInfo
;
1303 codewordOffset
= pHcr
->nonPcwSideinfo
.codewordOffset
;
1304 pSta
= pHcr
->nonPcwSideinfo
.pSta
;
1306 escapeWord
= pEscapeSequenceInfo
[codewordOffset
] & MASK_ESCAPE_WORD
;
1307 escapePrefixDown
= (pEscapeSequenceInfo
[codewordOffset
] & MASK_ESCAPE_PREFIX_DOWN
) >> LSB_ESCAPE_PREFIX_DOWN
;
1310 /* decode escape word */
1311 for ( ; pRemainingBitsInSegment
[segmentOffset
] > 0 ; pRemainingBitsInSegment
[segmentOffset
] -= 1 ) {
1313 carryBit
= HcrGetABitFromBitstream( bs
,
1314 &pLeftStartOfSegment
[segmentOffset
],
1315 &pRightStartOfSegment
[segmentOffset
],
1318 /* build escape word */
1319 escapeWord
<<= 1; /* left shift previous decoded part of escapeWord by on bit */
1320 escapeWord
= escapeWord
| carryBit
; /* assemble escape word by bitwise or */
1322 /* decrement counter for length of escape word because one more bit was decoded */
1323 escapePrefixDown
-= 1;
1325 /* store updated escapePrefixDown */
1326 pEscapeSequenceInfo
[codewordOffset
] &= ~MASK_ESCAPE_PREFIX_DOWN
; /* delete old escapePrefixDown */
1327 escapePrefixDown
<<= LSB_ESCAPE_PREFIX_DOWN
; /* shift to correct position */
1328 pEscapeSequenceInfo
[codewordOffset
] |= escapePrefixDown
; /* insert new escapePrefixDown */
1329 escapePrefixDown
>>= LSB_ESCAPE_PREFIX_DOWN
; /* shift back */
1332 /* store updated escapeWord */
1333 pEscapeSequenceInfo
[codewordOffset
] &= ~MASK_ESCAPE_WORD
; /* delete old escapeWord */
1334 pEscapeSequenceInfo
[codewordOffset
] |= escapeWord
; /* insert new escapeWord */
1337 if ( escapePrefixDown
== 0 ) {
1338 pRemainingBitsInSegment
[segmentOffset
] -= 1; /* last reinitialzation of for loop counter (see above) is done here */
1340 /* escape sequence decoded. Assemble escape-line and replace original line */
1344 iQSC
= iResultPointer
[codewordOffset
];
1345 sign
= (pResultBase
[iQSC
] >= (FIXP_DBL
)0) ? 1 : -1; /* get sign of escape value 16 */
1348 /* get escapePrefixUp */
1349 escapePrefixUp
= (pEscapeSequenceInfo
[codewordOffset
] & MASK_ESCAPE_PREFIX_UP
) >> LSB_ESCAPE_PREFIX_UP
;
1352 /* calculate escape value */
1353 pResultBase
[iQSC
] = (FIXP_DBL
)(sign
* (((INT
) 1 << escapePrefixUp
) + escapeWord
));
1355 /* get both flags from sideinfo (flags are not shifted to the lsb-position) */
1356 flagA
= pEscapeSequenceInfo
[codewordOffset
] & MASK_FLAG_A
;
1357 flagB
= pEscapeSequenceInfo
[codewordOffset
] & MASK_FLAG_B
;
1360 /* clear the whole escape sideinfo word */
1361 pEscapeSequenceInfo
[codewordOffset
] = 0;
1363 /* change state in dependence of flag flagB */
1365 /* first escape sequence decoded; previous decoded 16 has been replaced by valid line */
1367 /* clear flagA in sideinfo word because this escape sequence has already beed decoded */
1368 pEscapeSequenceInfo
[codewordOffset
] &= ~MASK_FLAG_A
;
1371 ClearBitFromBitfield(&(pHcr
->nonPcwSideinfo
.pState
),
1373 pCodewordBitfield
); /* clear a bit in bitfield and switch off statemachine */
1376 /* updated pointer to next and last 16 */
1378 iResultPointer
[codewordOffset
] = iQSC
;
1381 pSta
[codewordOffset
] = BODY_SIGN_ESC__ESC_PREFIX
;
1382 pHcr
->nonPcwSideinfo
.pState
= aStateConstant2State
[pSta
[codewordOffset
]]; /* get state from separate array of cw-sideinfo */
1386 ClearBitFromBitfield(&(pHcr
->nonPcwSideinfo
.pState
),
1388 pCodewordBitfield
); /* clear a bit in bitfield and switch off statemachine */
1394 if ( pRemainingBitsInSegment
[segmentOffset
] <= 0 ) {
1395 ClearBitFromBitfield(&(pHcr
->nonPcwSideinfo
.pState
),
1397 pSegmentBitfield
); /* clear a bit in bitfield and switch off statemachine */
1399 #if STATE_MACHINE_ERROR_CHECK
1400 if ( pRemainingBitsInSegment
[segmentOffset
] < 0 ) {
1401 pHcr
->decInOut
.errorLog
|= STATE_ERROR_BODY_SIGN_ESC__ESC_WORD
;
1402 return BODY_SIGN_ESC__ESC_WORD
;
1407 return STOP_THIS_STATE
;