Coverage Report

Created: 2023-03-26 06:07

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