Imported Debian version 0.1.3.1
[deb_fdk-aac.git] / libAACdec / src / aacdec_hcrs.cpp
1
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5 © Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
6 All rights reserved.
7
8 1. INTRODUCTION
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
17
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
24
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
28
29 2. COPYRIGHT LICENSE
30
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
33
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
36
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
41
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
47
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and the date of any change. For modified versions of the FDK AAC Codec, the term
50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
52
53 3. NO PATENT LICENSE
54
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
58
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61
62 4. DISCLAIMER
63
64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
72
73 5. CONTACT INFORMATION
74
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
83
84 /***************************** MPEG-4 AAC Decoder ***************************
85
86 Author(s): Robert Weidner (DSP Solutions)
87 Description: HCR Decoder: Prepare decoding of non-PCWs, segmentation- and
88 bitfield-handling, HCR-Statemachine
89
90 *******************************************************************************/
91
92 #include "aacdec_hcrs.h"
93
94
95 #include "aacdec_hcr.h"
96
97 #include "aacdec_hcr_bit.h"
98 #include "aac_rom.h"
99 #include "aac_ram.h"
100
101
102 static UINT InitSegmentBitfield(UINT *pNumSegment,
103 SCHAR *pRemainingBitsInSegment,
104 UINT *pSegmentBitfield,
105 UCHAR *pNumWordForBitfield,
106 USHORT *pNumBitValidInLastWord);
107
108 static void InitNonPCWSideInformationForCurrentSet(H_HCR_INFO pHcr);
109
110 static INT ModuloValue(INT input, INT bufferlength);
111
112 static void ClearBitFromBitfield(STATEFUNC *ptrState,
113 UINT offset,
114 UINT *pBitfield);
115
116
117 /*---------------------------------------------------------------------------------------------
118 description: This function decodes all non-priority codewords (non-PCWs) by using a
119 state-machine.
120 -------------------------------------------------------------------------------------------- */
121 void DecodeNonPCWs(HANDLE_FDK_BITSTREAM bs, H_HCR_INFO pHcr)
122 {
123 UINT numValidSegment;
124 INT segmentOffset;
125 INT codewordOffsetBase;
126 INT codewordOffset;
127 UINT trial;
128
129 UINT *pNumSegment;
130 SCHAR *pRemainingBitsInSegment;
131 UINT *pSegmentBitfield;
132 UCHAR *pNumWordForBitfield;
133 USHORT *pNumBitValidInLastWord;
134 UINT *pCodewordBitfield;
135 INT bitfieldWord;
136 INT bitInWord;
137 UINT tempWord;
138 UINT interMediateWord;
139 INT tempBit;
140 INT carry;
141
142 UINT numCodeword;
143 UCHAR numSet;
144 UCHAR currentSet;
145 UINT codewordInSet;
146 UINT remainingCodewordsInSet;
147 SCHAR *pSta;
148 UINT ret;
149
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;
156
157 numValidSegment = InitSegmentBitfield(pNumSegment,
158 pRemainingBitsInSegment,
159 pSegmentBitfield,
160 pNumWordForBitfield,
161 pNumBitValidInLastWord);
162
163 if ( numValidSegment != 0 ) {
164 numCodeword = pHcr->sectionInfo.numCodeword;
165 numSet = ((numCodeword - 1) / *pNumSegment) + 1;
166
167
168 pHcr->segmentInfo.readDirection = FROM_RIGHT_TO_LEFT;
169
170 /* Process sets subsequently */
171 for ( currentSet = 1; currentSet < numSet ; currentSet++ ) {
172
173
174
175 /* step 1 */
176 numCodeword -= *pNumSegment; /* number of remaining non PCWs [for all sets] */
177 if ( numCodeword < *pNumSegment ) {
178 codewordInSet = numCodeword; /* for last set */
179 }
180 else {
181 codewordInSet = *pNumSegment; /* for all sets except last set */
182 }
183
184 /* step 2 */
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;
188
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 */
194 }
195 else {
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 */
199 }
200 *pCodewordBitfield++ = tempWord;
201 tempWord = 0x00000000;
202 }
203 }
204 pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield;
205
206 /* step 3 */
207 /* build non-PCW sideinfo for each non-PCW of the current set */
208 InitNonPCWSideInformationForCurrentSet(pHcr);
209
210 /* step 4 */
211 /* decode all non-PCWs belonging to this set */
212
213 /* loop over trials */
214 codewordOffsetBase = 0;
215 for ( trial = *pNumSegment; trial > 0; trial-- ) {
216
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 */
222
223 for ( bitfieldWord=0; bitfieldWord < *pNumWordForBitfield; bitfieldWord++ ) {
224
225 /* derive tempWord with bitwise and */
226 tempWord = pSegmentBitfield[bitfieldWord] & pCodewordBitfield[bitfieldWord];
227
228 /* if tempWord is not zero, decode something */
229 if ( tempWord != 0 ) {
230
231
232 /* loop over all bits in tempWord; start state machine if & is true */
233 for ( bitInWord = NUMBER_OF_BIT_IN_WORD; bitInWord > 0; bitInWord-- ) {
234
235 interMediateWord = ((UINT)1 << (bitInWord-1) );
236 if ( ( tempWord & interMediateWord ) == interMediateWord ) {
237
238 /* get state and start state machine */
239 pHcr->nonPcwSideinfo.pState = aStateConstant2State[pSta[codewordOffset]];
240
241 while(pHcr->nonPcwSideinfo.pState) {
242 ret = ((STATEFUNC) pHcr->nonPcwSideinfo.pState)(bs, pHcr);
243 #if STATE_MACHINE_ERROR_CHECK
244 if ( ret != 0 ) {
245 return;
246 }
247 #endif
248 }
249 }
250
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;
257 }
258 }
259 else {
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;
265 }
266 } /* end of bitfield word loop */
267
268 /* decrement codeword - pointer */
269 codewordOffsetBase -= 1;
270 codewordOffsetBase = ModuloValue(codewordOffsetBase,*pNumSegment); /* index of the current codeword base lies within modulo range */
271
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);
277
278 /* write zero into place where tempBit was fetched from */
279 pCodewordBitfield[*pNumWordForBitfield-1] = pCodewordBitfield[*pNumWordForBitfield-1] & ~(1 << (NUMBER_OF_BIT_IN_WORD - *pNumBitValidInLastWord));
280
281 /* rotate last valid word */
282 pCodewordBitfield[*pNumWordForBitfield-1] = pCodewordBitfield[*pNumWordForBitfield-1] >> 1;
283
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;
288
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));
291
292 /* shift current word */
293 pCodewordBitfield[bitfieldWord] = pCodewordBitfield[bitfieldWord] >> 1;
294 }
295
296 /* put tempBit into free bit-position 31 from first word */
297 pCodewordBitfield[0] = pCodewordBitfield[0] | (tempBit << (NUMBER_OF_BIT_IN_WORD-1));
298
299 } /* end of trial loop */
300
301 /* toggle read direction */
302 pHcr->segmentInfo.readDirection = ToggleReadDirection(pHcr->segmentInfo.readDirection);
303
304 }
305 /* end of set loop */
306
307 /* all non-PCWs of this spectrum are decoded */
308 }
309
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 */
311 }
312
313
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)
326 {
327 SHORT i;
328 USHORT r;
329 UCHAR bitfieldWord;
330 UINT tempWord;
331 USHORT numValidSegment;
332
333 *pNumWordForBitfield = ((*pNumSegment-1) >> THIRTYTWO_LOG_DIV_TWO_LOG) + 1;
334
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 */
337 numValidSegment = 0;
338 *pNumBitValidInLastWord = *pNumSegment;
339
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 */
347 }
348 else {
349 numValidSegment += 1; /* count segments which are not empty */
350 }
351 }
352 pSegmentBitfield[bitfieldWord] = tempWord; /* store result */
353 *pNumBitValidInLastWord -= NUMBER_OF_BIT_IN_WORD; /* calculate number of zeros on LSB side in the last word */
354 }
355
356
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 */
361 }
362
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 */
368 }
369 else {
370 numValidSegment += 1; /* count segments which are not empty */
371 }
372 }
373 pSegmentBitfield[bitfieldWord] = tempWord; /* store result */
374
375
376
377 return numValidSegment;
378 }
379
380
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)
385 {
386 USHORT i,k;
387 UCHAR codebookDim;
388 UINT startNode;
389
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;
406
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-- ) {
409
410 codebookDim = pCbDimension[pExtendedSortedCodebook[extendedSortedCodebookIdx]];
411 startNode = *aHuffTable[pExtendedSortedCodebook[extendedSortedCodebookIdx]];
412
413 for ( k = pNumExtendedSortedCodewordInSection[numExtendedSortedCodewordInSectionIdx]; k != 0; k-- ) {
414 iterationCounter++;
415 if (iterationCounter > (1024>>2)) {
416 return;
417 }
418 *pSta++ = aCodebook2StartInt[pExtendedSortedCodebook[extendedSortedCodebookIdx]];
419 *pCodebook++ = pExtendedSortedCodebook[extendedSortedCodebookIdx];
420 *iNode++ = startNode;
421 *pCntSign++ = 0;
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) {
426 return;
427 }
428 }
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)) {
432 return;
433 }
434 }
435 numExtendedSortedSectionsInSetsIdx++; /* inc ptr for next set of non-PCWs */
436 if (numExtendedSortedCodewordInSectionIdx >= (MAX_SFB_HCR+MAX_HCR_SETS)) {
437 return;
438 }
439
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;
446 }
447
448
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)
457 {
458 if ( input > (bufferlength - 1) ) {
459 return (input - bufferlength);
460 }
461 if ( input < 0 ) {
462 return (input + bufferlength);
463 }
464 return input;
465 }
466
467
468 /*---------------------------------------------------------------------------------------------
469 description: This function clears a bit from current bitfield and
470 switches off the statemachine.
471
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,
477 UINT offset,
478 UINT *pBitfield)
479 {
480 UINT numBitfieldWord;
481 UINT numBitfieldBit;
482
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 */
486
487 /* clear a bit in bitfield */
488 pBitfield[numBitfieldWord] = pBitfield[numBitfieldWord] & ~(1 << (NUMBER_OF_BIT_IN_WORD-1 - numBitfieldBit));
489
490 /* switch off state machine because codeword is decoded and/or because segment is empty */
491 *ptrState = NULL;
492 }
493
494
495
496 /* =========================================================================================
497 the states of the statemachine
498 ========================================================================================= */
499
500
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
507 points to
508 -----------------------------------------------------------------------------------------------
509 return: 0
510 -------------------------------------------------------------------------------------------- */
511 UINT Hcr_State_BODY_ONLY(HANDLE_FDK_BITSTREAM bs, void *ptr)
512 {
513 H_HCR_INFO pHcr = (H_HCR_INFO)ptr;
514 UINT *pSegmentBitfield;
515 UINT *pCodewordBitfield;
516 UINT segmentOffset;
517 FIXP_DBL *pResultBase;
518 UINT *iNode;
519 USHORT *iResultPointer;
520 UINT codewordOffset;
521 UINT branchNode;
522 UINT branchValue;
523 UINT iQSC;
524 UINT treeNode;
525 UCHAR carryBit;
526 USHORT *pLeftStartOfSegment;
527 USHORT *pRightStartOfSegment;
528 SCHAR *pRemainingBitsInSegment;
529 UCHAR readDirection;
530 UCHAR *pCodebook;
531 UCHAR dimCntr;
532 const UINT *pCurrentTree;
533 const UCHAR *pCbDimension;
534 const SCHAR *pQuantVal;
535 const SCHAR *pQuantValBase;
536
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;
544
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;
550
551 pCbDimension = pHcr->tableInfo.pCbDimension;
552
553 treeNode = iNode[codewordOffset];
554 pCurrentTree = aHuffTable[pCodebook[codewordOffset]];
555
556
557 for ( ; pRemainingBitsInSegment[segmentOffset] > 0 ; pRemainingBitsInSegment[segmentOffset] -= 1 ) {
558
559 carryBit = HcrGetABitFromBitstream( bs,
560 &pLeftStartOfSegment[segmentOffset],
561 &pRightStartOfSegment[segmentOffset],
562 readDirection);
563
564 CarryBitToBranchValue(carryBit, /* make a step in decoding tree */
565 treeNode,
566 &branchValue,
567 &branchNode);
568
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] */
573
574 iQSC = iResultPointer[codewordOffset]; /* get position of first line for writing out result */
575
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 */
578 }
579
580 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState),
581 segmentOffset,
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 */
585 }
586 else { /* body is not decoded completely: */
587 treeNode = *(pCurrentTree + branchValue); /* update treeNode for further step in decoding tree */
588 }
589 }
590 iNode[codewordOffset] = treeNode; /* store updated treeNode because maybe decoding of codeword body not finished yet */
591
592 if ( pRemainingBitsInSegment[segmentOffset] <= 0 ) {
593 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState),
594 segmentOffset,
595 pSegmentBitfield); /* clear a bit in bitfield and switch off statemachine */
596
597 #if STATE_MACHINE_ERROR_CHECK
598 if ( pRemainingBitsInSegment[segmentOffset] < 0 ) {
599 pHcr->decInOut.errorLog |= STATE_ERROR_BODY_ONLY;
600 return BODY_ONLY;
601 }
602 #endif
603 }
604
605 return STOP_THIS_STATE;
606 }
607
608
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
612 needed.
613
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
617 decoded.
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 -----------------------------------------------------------------------------------------------
623 return: 0
624 -------------------------------------------------------------------------------------------- */
625 UINT Hcr_State_BODY_SIGN__BODY(HANDLE_FDK_BITSTREAM bs, void *ptr)
626 {
627 H_HCR_INFO pHcr = (H_HCR_INFO)ptr;
628 SCHAR *pRemainingBitsInSegment;
629 USHORT *pLeftStartOfSegment;
630 USHORT *pRightStartOfSegment;
631 UCHAR readDirection;
632 UINT *pSegmentBitfield;
633 UINT *pCodewordBitfield;
634 UINT segmentOffset;
635
636 UCHAR *pCodebook;
637 UINT *iNode;
638 UCHAR *pCntSign;
639 FIXP_DBL *pResultBase;
640 USHORT *iResultPointer;
641 UINT codewordOffset;
642
643 UINT iQSC;
644 UINT cntSign;
645 UCHAR dimCntr;
646 UCHAR carryBit;
647 SCHAR *pSta;
648 UINT treeNode;
649 UINT branchValue;
650 UINT branchNode;
651 const UCHAR *pCbDimension;
652 const UINT *pCurrentTree;
653 const SCHAR *pQuantValBase;
654 const SCHAR *pQuantVal;
655
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;
663
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;
671
672 pCbDimension = pHcr->tableInfo.pCbDimension;
673
674 treeNode = iNode[codewordOffset];
675 pCurrentTree = aHuffTable[pCodebook[codewordOffset]];
676
677
678 for ( ; pRemainingBitsInSegment[segmentOffset] > 0 ; pRemainingBitsInSegment[segmentOffset] -= 1 ) {
679
680 carryBit = HcrGetABitFromBitstream( bs,
681 &pLeftStartOfSegment[segmentOffset],
682 &pRightStartOfSegment[segmentOffset],
683 readDirection);
684
685 CarryBitToBranchValue(carryBit, /* make a step in decoding tree */
686 treeNode,
687 &branchValue,
688 &branchNode);
689
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] */
695
696 iQSC = iResultPointer[codewordOffset]; /* get position of first line for writing result */
697
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] */
700 cntSign = 0;
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 ) {
704 cntSign += 1;
705 }
706 }
707
708 if ( cntSign == 0 ) {
709 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState),
710 segmentOffset,
711 pCodewordBitfield); /* clear a bit in bitfield and switch off statemachine */
712 }
713 else {
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 */
717 }
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 */
720 }
721 else {/* body is not decoded completely: */
722 treeNode = *(pCurrentTree + branchValue); /* update treeNode for further step in decoding tree */
723 }
724 }
725 iNode[codewordOffset] = treeNode; /* store updated treeNode because maybe decoding of codeword body not finished yet */
726
727 if ( pRemainingBitsInSegment[segmentOffset] <= 0 ) {
728 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState),
729 segmentOffset,
730 pSegmentBitfield); /* clear a bit in bitfield and switch off statemachine */
731
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;
736 }
737 #endif
738 }
739
740 return STOP_THIS_STATE;
741 }
742
743
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 -----------------------------------------------------------------------------------------------
751 return: 0
752 -------------------------------------------------------------------------------------------- */
753 UINT Hcr_State_BODY_SIGN__SIGN(HANDLE_FDK_BITSTREAM bs, void *ptr)
754 {
755 H_HCR_INFO pHcr = (H_HCR_INFO)ptr;
756 SCHAR *pRemainingBitsInSegment;
757 USHORT *pLeftStartOfSegment;
758 USHORT *pRightStartOfSegment;
759 UCHAR readDirection;
760 UINT *pSegmentBitfield;
761 UINT *pCodewordBitfield;
762 UINT segmentOffset;
763
764 UCHAR *pCntSign;
765 FIXP_DBL *pResultBase;
766 USHORT *iResultPointer;
767 UINT codewordOffset;
768 UCHAR carryBit;
769 UINT iQSC;
770 UCHAR cntSign;
771
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;
779
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];
786
787
788
789 /* loop for sign bit decoding */
790 for ( ; pRemainingBitsInSegment[segmentOffset] > 0 ; pRemainingBitsInSegment[segmentOffset] -= 1 ) {
791
792 carryBit = HcrGetABitFromBitstream( bs,
793 &pLeftStartOfSegment[segmentOffset],
794 &pRightStartOfSegment[segmentOffset],
795 readDirection);
796 cntSign -= 1; /* decrement sign counter because one sign bit has been read */
797
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 */
801 if (iQSC >= 1024) {
802 return BODY_SIGN__SIGN;
803 }
804 }
805
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 */
809 }
810
811 iQSC++; /* update pointer to next (maybe valid) value */
812
813 if ( cntSign == 0 ) { /* if (cntSign==0) ==> set state CODEWORD_DECODED */
814 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState),
815 segmentOffset,
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 */
819 }
820 }
821 pCntSign[codewordOffset] = cntSign;
822 iResultPointer[codewordOffset] = iQSC; /* store updated pResultPointer */
823
824 if ( pRemainingBitsInSegment[segmentOffset] <= 0 ) {
825 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState),
826 segmentOffset,
827 pSegmentBitfield); /* clear a bit in bitfield and switch off statemachine */
828
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;
833 }
834 #endif
835 }
836
837 return STOP_THIS_STATE;
838 }
839
840
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
847 decoded completely.
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 -----------------------------------------------------------------------------------------------
853 return: 0
854 -------------------------------------------------------------------------------------------- */
855 UINT Hcr_State_BODY_SIGN_ESC__BODY(HANDLE_FDK_BITSTREAM bs, void *ptr)
856 {
857 H_HCR_INFO pHcr = (H_HCR_INFO)ptr;
858 SCHAR *pRemainingBitsInSegment;
859 USHORT *pLeftStartOfSegment;
860 USHORT *pRightStartOfSegment;
861 UCHAR readDirection;
862 UINT *pSegmentBitfield;
863 UINT *pCodewordBitfield;
864 UINT segmentOffset;
865
866 UINT *iNode;
867 UCHAR *pCntSign;
868 FIXP_DBL *pResultBase;
869 USHORT *iResultPointer;
870 UINT codewordOffset;
871
872 UCHAR carryBit;
873 UINT iQSC;
874 UINT cntSign;
875 UINT dimCntr;
876 UINT treeNode;
877 SCHAR *pSta;
878 UINT branchNode;
879 UINT branchValue;
880 const UINT *pCurrentTree;
881 const SCHAR *pQuantValBase;
882 const SCHAR *pQuantVal;
883
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;
891
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;
898
899 treeNode = iNode[codewordOffset];
900 pCurrentTree = aHuffTable[ESCAPE_CODEBOOK];
901
902
903 for ( ; pRemainingBitsInSegment[segmentOffset] > 0 ; pRemainingBitsInSegment[segmentOffset] -= 1 ) {
904
905 carryBit = HcrGetABitFromBitstream( bs,
906 &pLeftStartOfSegment[segmentOffset],
907 &pRightStartOfSegment[segmentOffset],
908 readDirection);
909
910 /* make a step in tree */
911 CarryBitToBranchValue(carryBit,
912 treeNode,
913 &branchValue,
914 &branchNode);
915
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 */
918
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] */
923
924 /* make backup from original resultPointer in node storage for state BODY_SIGN_ESC__SIGN */
925 iNode[codewordOffset] = iResultPointer[codewordOffset];
926
927 /* get position of first line for writing result */
928 iQSC = iResultPointer[codewordOffset];
929
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] */
932 cntSign = 0;
933
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 ) {
937 cntSign += 1;
938 }
939 }
940
941 if ( cntSign == 0 ) {
942 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState),
943 segmentOffset,
944 pCodewordBitfield); /* clear a bit in bitfield and switch off statemachine */
945 /* codeword decoded */
946 }
947 else {
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 */
952 }
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 */
955 }
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;
960 }
961 }
962
963 if ( pRemainingBitsInSegment[segmentOffset] <= 0 ) {
964 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState),
965 segmentOffset,
966 pSegmentBitfield); /* clear a bit in bitfield and switch off statemachine */
967
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;
972 }
973 #endif
974 }
975
976 return STOP_THIS_STATE;
977 }
978
979
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.
985
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.
988
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
991 same time:
992
993
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 ===== == == =========== =========== ===================================
996 ^ ^ ^ ^ ^ ^
997 | | | | | |
998 res. flagA flagB escapePrefixUp escapePrefixDown escapeWord
999
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 -----------------------------------------------------------------------------------------------
1004 return: 0
1005 -------------------------------------------------------------------------------------------- */
1006 UINT Hcr_State_BODY_SIGN_ESC__SIGN(HANDLE_FDK_BITSTREAM bs, void *ptr)
1007 {
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;
1015 UINT segmentOffset;
1016
1017 UINT *iNode;
1018 UCHAR *pCntSign;
1019 FIXP_DBL *pResultBase;
1020 USHORT *iResultPointer;
1021 UINT *pEscapeSequenceInfo;
1022 UINT codewordOffset;
1023
1024 UINT iQSC;
1025 UCHAR cntSign;
1026 UINT flagA;
1027 UINT flagB;
1028 UINT flags;
1029 UCHAR carryBit;
1030 SCHAR *pSta;
1031
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;
1039
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;
1047
1048 iQSC = iResultPointer[codewordOffset];
1049 cntSign = pCntSign[codewordOffset];
1050
1051
1052 /* loop for sign bit decoding */
1053 for ( ; pRemainingBitsInSegment[segmentOffset] > 0 ; pRemainingBitsInSegment[segmentOffset] -= 1 ) {
1054
1055 carryBit = HcrGetABitFromBitstream( bs,
1056 &pLeftStartOfSegment[segmentOffset],
1057 &pRightStartOfSegment[segmentOffset],
1058 readDirection);
1059
1060 /* decrement sign counter because one sign bit has been read */
1061 cntSign -= 1;
1062 pCntSign[codewordOffset] = cntSign;
1063
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 ) {
1066 iQSC++;
1067 }
1068 iResultPointer[codewordOffset] = iQSC;
1069
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 */
1073 }
1074 iQSC++; /* update index to next (maybe valid) value */
1075 iResultPointer[codewordOffset] = iQSC;
1076
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 */
1080
1081 /* check decoded values if codeword is decoded: Check if one or two escape sequences 16 follow */
1082
1083 /* step 0 */
1084 /* restore pointer to first decoded quantized value [ = original pResultPointr] from index iNode prepared in State_BODY_SIGN_ESC__BODY */
1085 iQSC = iNode[codewordOffset];
1086
1087 /* step 1 */
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 ) {
1091 flagA = 1;
1092 }
1093
1094 /* step 2 */
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 ) {
1098 flagB = 1;
1099 }
1100
1101
1102 /* step 3 */
1103 /* evaluate flag result and go on if necessary */
1104 if ( !flagA && !flagB ) {
1105 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState),
1106 segmentOffset,
1107 pCodewordBitfield); /* clear a bit in bitfield and switch off statemachine */
1108 }
1109 else {
1110 /* at least one of two lines is 16 */
1111 /* store both flags at correct positions in non PCW codeword sideinfo pEscapeSequenceInfo[codewordOffset] */
1112 flags = 0;
1113 flags = flagA << POSITION_OF_FLAG_A;
1114 flags |= (flagB << POSITION_OF_FLAG_B);
1115 pEscapeSequenceInfo[codewordOffset] = flags;
1116
1117
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 */
1121
1122 /* set result pointer to the first line of the two decoded lines */
1123 iResultPointer[codewordOffset] = iNode[codewordOffset];
1124
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];
1128 iQSC++;
1129 iResultPointer[codewordOffset] = iQSC;
1130 }
1131
1132 } /* at least one of two lines is 16 */
1133 break; /* nonPCW-Body at cb 11 and according sign bits are decoded */
1134
1135 } /* if ( cntSign == 0 ) */
1136 } /* loop over remaining Bits in segment */
1137
1138 if ( pRemainingBitsInSegment[segmentOffset] <= 0 ) {
1139 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState),
1140 segmentOffset,
1141 pSegmentBitfield); /* clear a bit in bitfield and switch off statemachine */
1142
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;
1147 }
1148 #endif
1149
1150 }
1151 return STOP_THIS_STATE;
1152 }
1153
1154
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.
1161
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 -----------------------------------------------------------------------------------------------
1166 return: 0
1167 -------------------------------------------------------------------------------------------- */
1168 UINT Hcr_State_BODY_SIGN_ESC__ESC_PREFIX(HANDLE_FDK_BITSTREAM bs, void *ptr)
1169 {
1170 H_HCR_INFO pHcr = (H_HCR_INFO)ptr;
1171 SCHAR *pRemainingBitsInSegment;
1172 USHORT *pLeftStartOfSegment;
1173 USHORT *pRightStartOfSegment;
1174 UCHAR readDirection;
1175 UINT *pSegmentBitfield;
1176 UINT segmentOffset;
1177 UINT *pEscapeSequenceInfo;
1178 UINT codewordOffset;
1179 UCHAR carryBit;
1180 UINT escapePrefixUp;
1181 SCHAR *pSta;
1182
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;
1192
1193 escapePrefixUp = (pEscapeSequenceInfo[codewordOffset] & MASK_ESCAPE_PREFIX_UP) >> LSB_ESCAPE_PREFIX_UP;
1194
1195
1196 /* decode escape prefix */
1197 for ( ; pRemainingBitsInSegment[segmentOffset] > 0 ; pRemainingBitsInSegment[segmentOffset] -= 1 ) {
1198
1199 carryBit = HcrGetABitFromBitstream( bs,
1200 &pLeftStartOfSegment[segmentOffset],
1201 &pRightStartOfSegment[segmentOffset],
1202 readDirection);
1203
1204 /* count ones and store sum in escapePrefixUp */
1205 if ( carryBit == 1 ) {
1206 escapePrefixUp += 1; /* update conter for ones */
1207
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 */
1213 }
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 */
1217
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 */
1223
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 */
1229
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 */
1232 break;
1233 }
1234 }
1235
1236 if ( pRemainingBitsInSegment[segmentOffset] <= 0 ) {
1237 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState),
1238 segmentOffset,
1239 pSegmentBitfield); /* clear a bit in bitfield and switch off statemachine */
1240
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;
1245 }
1246 #endif
1247 }
1248
1249 return STOP_THIS_STATE;
1250 }
1251
1252
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
1259 off.
1260 -----------------------------------------------------------------------------------------------
1261 output: Two lines with valid sign. At least one of both lines has got the correct
1262 value.
1263 -----------------------------------------------------------------------------------------------
1264 return: 0
1265 -------------------------------------------------------------------------------------------- */
1266 UINT Hcr_State_BODY_SIGN_ESC__ESC_WORD(HANDLE_FDK_BITSTREAM bs, void *ptr)
1267 {
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;
1275 UINT segmentOffset;
1276
1277 FIXP_DBL *pResultBase;
1278 USHORT *iResultPointer;
1279 UINT *pEscapeSequenceInfo;
1280 UINT codewordOffset;
1281
1282 UINT escapeWord;
1283 UINT escapePrefixDown;
1284 UINT escapePrefixUp;
1285 UCHAR carryBit;
1286 UINT iQSC;
1287 INT sign;
1288 UINT flagA;
1289 UINT flagB;
1290 SCHAR *pSta;
1291
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;
1299
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;
1305
1306 escapeWord = pEscapeSequenceInfo[codewordOffset] & MASK_ESCAPE_WORD;
1307 escapePrefixDown = (pEscapeSequenceInfo[codewordOffset] & MASK_ESCAPE_PREFIX_DOWN) >> LSB_ESCAPE_PREFIX_DOWN;
1308
1309
1310 /* decode escape word */
1311 for ( ; pRemainingBitsInSegment[segmentOffset] > 0 ; pRemainingBitsInSegment[segmentOffset] -= 1 ) {
1312
1313 carryBit = HcrGetABitFromBitstream( bs,
1314 &pLeftStartOfSegment[segmentOffset],
1315 &pRightStartOfSegment[segmentOffset],
1316 readDirection);
1317
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 */
1321
1322 /* decrement counter for length of escape word because one more bit was decoded */
1323 escapePrefixDown -= 1;
1324
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 */
1330
1331
1332 /* store updated escapeWord */
1333 pEscapeSequenceInfo[codewordOffset] &= ~MASK_ESCAPE_WORD; /* delete old escapeWord */
1334 pEscapeSequenceInfo[codewordOffset] |= escapeWord; /* insert new escapeWord */
1335
1336
1337 if ( escapePrefixDown == 0 ) {
1338 pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of for loop counter (see above) is done here */
1339
1340 /* escape sequence decoded. Assemble escape-line and replace original line */
1341
1342 /* step 0 */
1343 /* derive sign */
1344 iQSC = iResultPointer[codewordOffset];
1345 sign = (pResultBase[iQSC] >= (FIXP_DBL)0) ? 1 : -1; /* get sign of escape value 16 */
1346
1347 /* step 1 */
1348 /* get escapePrefixUp */
1349 escapePrefixUp = (pEscapeSequenceInfo[codewordOffset] & MASK_ESCAPE_PREFIX_UP) >> LSB_ESCAPE_PREFIX_UP;
1350
1351 /* step 2 */
1352 /* calculate escape value */
1353 pResultBase[iQSC] = (FIXP_DBL)(sign * (((INT) 1 << escapePrefixUp) + escapeWord));
1354
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;
1358
1359 /* step 3 */
1360 /* clear the whole escape sideinfo word */
1361 pEscapeSequenceInfo[codewordOffset] = 0;
1362
1363 /* change state in dependence of flag flagB */
1364 if ( flagA != 0 ) {
1365 /* first escape sequence decoded; previous decoded 16 has been replaced by valid line */
1366
1367 /* clear flagA in sideinfo word because this escape sequence has already beed decoded */
1368 pEscapeSequenceInfo[codewordOffset] &= ~MASK_FLAG_A;
1369
1370 if ( flagB == 0 ) {
1371 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState),
1372 segmentOffset,
1373 pCodewordBitfield); /* clear a bit in bitfield and switch off statemachine */
1374 }
1375 else {
1376 /* updated pointer to next and last 16 */
1377 iQSC++;
1378 iResultPointer[codewordOffset] = iQSC;
1379
1380 /* change state */
1381 pSta[codewordOffset] = BODY_SIGN_ESC__ESC_PREFIX;
1382 pHcr->nonPcwSideinfo.pState = aStateConstant2State[pSta[codewordOffset]]; /* get state from separate array of cw-sideinfo */
1383 }
1384 }
1385 else {
1386 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState),
1387 segmentOffset,
1388 pCodewordBitfield); /* clear a bit in bitfield and switch off statemachine */
1389 }
1390 break;
1391 }
1392 }
1393
1394 if ( pRemainingBitsInSegment[segmentOffset] <= 0 ) {
1395 ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState),
1396 segmentOffset,
1397 pSegmentBitfield); /* clear a bit in bitfield and switch off statemachine */
1398
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;
1403 }
1404 #endif
1405 }
1406
1407 return STOP_THIS_STATE;
1408 }
1409