Coverage Report

Created: 2026-02-14 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aac/libAACdec/src/aacdec_hcrs.cpp
Line
Count
Source
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
4.62k
void DecodeNonPCWs(HANDLE_FDK_BITSTREAM bs, H_HCR_INFO pHcr) {
131
4.62k
  UINT numValidSegment;
132
4.62k
  INT segmentOffset;
133
4.62k
  INT codewordOffsetBase;
134
4.62k
  INT codewordOffset;
135
4.62k
  UINT trial;
136
137
4.62k
  UINT *pNumSegment;
138
4.62k
  SCHAR *pRemainingBitsInSegment;
139
4.62k
  UINT *pSegmentBitfield;
140
4.62k
  UCHAR *pNumWordForBitfield;
141
4.62k
  USHORT *pNumBitValidInLastWord;
142
4.62k
  UINT *pCodewordBitfield;
143
4.62k
  INT bitfieldWord;
144
4.62k
  INT bitInWord;
145
4.62k
  UINT tempWord;
146
4.62k
  UINT interMediateWord;
147
4.62k
  INT tempBit;
148
4.62k
  INT carry;
149
150
4.62k
  UINT numCodeword;
151
4.62k
  UCHAR numSet;
152
4.62k
  UCHAR currentSet;
153
4.62k
  UINT codewordInSet;
154
4.62k
  UINT remainingCodewordsInSet;
155
4.62k
  SCHAR *pSta;
156
4.62k
  UINT ret;
157
158
4.62k
  pNumSegment = &(pHcr->segmentInfo.numSegment);
159
4.62k
  pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment;
160
4.62k
  pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield;
161
4.62k
  pNumWordForBitfield = &(pHcr->segmentInfo.numWordForBitfield);
162
4.62k
  pNumBitValidInLastWord = &(pHcr->segmentInfo.pNumBitValidInLastWord);
163
4.62k
  pSta = pHcr->nonPcwSideinfo.pSta;
164
165
4.62k
  numValidSegment = InitSegmentBitfield(pNumSegment, pRemainingBitsInSegment,
166
4.62k
                                        pSegmentBitfield, pNumWordForBitfield,
167
4.62k
                                        pNumBitValidInLastWord);
168
169
4.62k
  if (numValidSegment != 0) {
170
4.46k
    numCodeword = pHcr->sectionInfo.numCodeword;
171
4.46k
    numSet = ((numCodeword - 1) / *pNumSegment) + 1;
172
173
4.46k
    pHcr->segmentInfo.readDirection = FROM_RIGHT_TO_LEFT;
174
175
    /* Process sets subsequently */
176
4.46k
    numSet = fMin(numSet, (UCHAR)MAX_HCR_SETS);
177
23.9k
    for (currentSet = 1; currentSet < numSet; currentSet++) {
178
179
      /* step 1 */
180
19.8k
      numCodeword -=
181
19.8k
          *pNumSegment; /* number of remaining non PCWs [for all sets] */
182
19.8k
      if (numCodeword < *pNumSegment) {
183
2.82k
        codewordInSet = numCodeword; /* for last set */
184
17.0k
      } else {
185
17.0k
        codewordInSet = *pNumSegment; /* for all sets except last set */
186
17.0k
      }
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
19.8k
      tempWord = 0xFFFFFFFF;
193
19.8k
      pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield;
194
195
42.9k
      for (bitfieldWord = *pNumWordForBitfield; bitfieldWord != 0;
196
23.0k
           bitfieldWord--) { /* loop over all used words */
197
23.0k
        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
1.95k
          *pCodewordBitfield++ = tempWord;
202
1.95k
          codewordInSet -= NUMBER_OF_BIT_IN_WORD; /* subtract number of bits */
203
21.1k
        } else {
204
          /* prepare last tempWord */
205
21.1k
          for (remainingCodewordsInSet = codewordInSet;
206
505k
               remainingCodewordsInSet < NUMBER_OF_BIT_IN_WORD;
207
484k
               remainingCodewordsInSet++) {
208
484k
            tempWord =
209
484k
                tempWord &
210
484k
                ~(1
211
484k
                  << (NUMBER_OF_BIT_IN_WORD - 1 -
212
484k
                      remainingCodewordsInSet)); /* set a zero at bit number
213
                                                    (NUMBER_OF_BIT_IN_WORD-1-i)
214
                                                    in tempWord */
215
484k
          }
216
21.1k
          *pCodewordBitfield++ = tempWord;
217
21.1k
          tempWord = 0x00000000;
218
21.1k
        }
219
23.0k
      }
220
19.8k
      pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield;
221
222
      /* step 3 */
223
      /* build non-PCW sideinfo for each non-PCW of the current set */
224
19.8k
      InitNonPCWSideInformationForCurrentSet(pHcr);
225
226
      /* step 4 */
227
      /* decode all non-PCWs belonging to this set */
228
229
      /* loop over trials */
230
19.8k
      codewordOffsetBase = 0;
231
274k
      for (trial = *pNumSegment; trial > 0; trial--) {
232
        /* loop over number of words in bitfields */
233
255k
        segmentOffset = 0; /* start at zero in every segment */
234
255k
        pHcr->segmentInfo.segmentOffset =
235
255k
            segmentOffset; /* store in structure for states */
236
255k
        codewordOffset = codewordOffsetBase;
237
255k
        pHcr->nonPcwSideinfo.codewordOffset =
238
255k
            codewordOffset; /* store in structure for states */
239
240
1.00M
        for (bitfieldWord = 0; bitfieldWord < *pNumWordForBitfield;
241
746k
             bitfieldWord++) {
242
          /* derive tempWord with bitwise and */
243
746k
          tempWord =
244
746k
              pSegmentBitfield[bitfieldWord] & pCodewordBitfield[bitfieldWord];
245
246
          /* if tempWord is not zero, decode something */
247
746k
          if (tempWord != 0) {
248
            /* loop over all bits in tempWord; start state machine if & is true
249
             */
250
1.31M
            for (bitInWord = NUMBER_OF_BIT_IN_WORD; bitInWord > 0;
251
1.27M
                 bitInWord--) {
252
1.27M
              interMediateWord = ((UINT)1 << (bitInWord - 1));
253
1.27M
              if ((tempWord & interMediateWord) == interMediateWord) {
254
                /* get state and start state machine */
255
170k
                pHcr->nonPcwSideinfo.pState =
256
170k
                    aStateConstant2State[pSta[codewordOffset]];
257
258
403k
                while (pHcr->nonPcwSideinfo.pState) {
259
233k
                  ret = ((STATEFUNC)pHcr->nonPcwSideinfo.pState)(bs, pHcr);
260
233k
                  if (ret != 0) {
261
376
                    return;
262
376
                  }
263
233k
                }
264
170k
              }
265
266
              /* update both offsets */
267
1.27M
              segmentOffset += 1; /* add NUMBER_OF_BIT_IN_WORD times one */
268
1.27M
              pHcr->segmentInfo.segmentOffset = segmentOffset;
269
1.27M
              codewordOffset += 1; /* add NUMBER_OF_BIT_IN_WORD times one */
270
1.27M
              codewordOffset =
271
1.27M
                  ModuloValue(codewordOffset,
272
1.27M
                              *pNumSegment); /* index of the current codeword
273
                                                lies within modulo range */
274
1.27M
              pHcr->nonPcwSideinfo.codewordOffset = codewordOffset;
275
1.27M
            }
276
705k
          } else {
277
705k
            segmentOffset +=
278
705k
                NUMBER_OF_BIT_IN_WORD; /* add NUMBER_OF_BIT_IN_WORD at once */
279
705k
            pHcr->segmentInfo.segmentOffset = segmentOffset;
280
705k
            codewordOffset +=
281
705k
                NUMBER_OF_BIT_IN_WORD; /* add NUMBER_OF_BIT_IN_WORD at once */
282
705k
            codewordOffset = ModuloValue(
283
705k
                codewordOffset,
284
705k
                *pNumSegment); /* index of the current codeword lies within
285
                                  modulo range */
286
705k
            pHcr->nonPcwSideinfo.codewordOffset = codewordOffset;
287
705k
          }
288
746k
        } /* end of bitfield word loop */
289
290
        /* decrement codeword - pointer */
291
255k
        codewordOffsetBase -= 1;
292
255k
        codewordOffsetBase =
293
255k
            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
255k
        tempBit = pCodewordBitfield[*pNumWordForBitfield - 1] &
303
255k
                  (1 << (NUMBER_OF_BIT_IN_WORD - *pNumBitValidInLastWord));
304
255k
        tempBit = tempBit >> (NUMBER_OF_BIT_IN_WORD - *pNumBitValidInLastWord);
305
306
        /* write zero into place where tempBit was fetched from */
307
255k
        pCodewordBitfield[*pNumWordForBitfield - 1] =
308
255k
            pCodewordBitfield[*pNumWordForBitfield - 1] &
309
255k
            ~(1 << (NUMBER_OF_BIT_IN_WORD - *pNumBitValidInLastWord));
310
311
        /* rotate last valid word */
312
255k
        pCodewordBitfield[*pNumWordForBitfield - 1] =
313
255k
            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
745k
        for (bitfieldWord = *pNumWordForBitfield - 2; bitfieldWord > -1;
318
490k
             bitfieldWord--) {
319
          /* get carry (=bit at position 0) from current word */
320
490k
          carry = pCodewordBitfield[bitfieldWord] & 1;
321
322
          /* put the carry bit at position 31 into word right from current word
323
           */
324
490k
          pCodewordBitfield[bitfieldWord + 1] =
325
490k
              pCodewordBitfield[bitfieldWord + 1] |
326
490k
              (carry << (NUMBER_OF_BIT_IN_WORD - 1));
327
328
          /* shift current word */
329
490k
          pCodewordBitfield[bitfieldWord] =
330
490k
              pCodewordBitfield[bitfieldWord] >> 1;
331
490k
        }
332
333
        /* put tempBit into free bit-position 31 from first word */
334
255k
        pCodewordBitfield[0] =
335
255k
            pCodewordBitfield[0] | (tempBit << (NUMBER_OF_BIT_IN_WORD - 1));
336
337
255k
      } /* end of trial loop */
338
339
      /* toggle read direction */
340
19.4k
      pHcr->segmentInfo.readDirection =
341
19.4k
          ToggleReadDirection(pHcr->segmentInfo.readDirection);
342
19.4k
    }
343
    /* end of set loop */
344
345
    /* all non-PCWs of this spectrum are decoded */
346
4.46k
  }
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
4.62k
}
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
4.62k
                                USHORT *pNumBitValidInLastWord) {
366
4.62k
  SHORT i;
367
4.62k
  USHORT r;
368
4.62k
  UCHAR bitfieldWord;
369
4.62k
  UINT tempWord;
370
4.62k
  USHORT numValidSegment;
371
372
4.62k
  *pNumWordForBitfield =
373
4.62k
      (*pNumSegment == 0)
374
4.62k
          ? 0
375
4.62k
          : ((*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
4.62k
  numValidSegment = 0;
381
4.62k
  *pNumBitValidInLastWord = *pNumSegment;
382
383
  /* loop over words */
384
7.98k
  for (bitfieldWord = 0; bitfieldWord < *pNumWordForBitfield - 1;
385
4.62k
       bitfieldWord++) {
386
3.36k
    tempWord = 0xFFFFFFFF; /* set ones */
387
3.36k
    r = bitfieldWord << THIRTYTWO_LOG_DIV_TWO_LOG;
388
111k
    for (i = 0; i < NUMBER_OF_BIT_IN_WORD; i++) {
389
107k
      if (pRemainingBitsInSegment[r + i] == 0) {
390
16.9k
        tempWord = tempWord & ~(1 << (NUMBER_OF_BIT_IN_WORD - 1 -
391
16.9k
                                      i)); /* set a zero at bit number
392
                                              (NUMBER_OF_BIT_IN_WORD-1-i) in
393
                                              tempWord */
394
90.8k
      } else {
395
90.8k
        numValidSegment += 1; /* count segments which are not empty */
396
90.8k
      }
397
107k
    }
398
3.36k
    pSegmentBitfield[bitfieldWord] = tempWord;        /* store result */
399
3.36k
    *pNumBitValidInLastWord -= NUMBER_OF_BIT_IN_WORD; /* calculate number of
400
                                                         zeros on LSB side in
401
                                                         the last word */
402
3.36k
  }
403
404
  /* calculate last word: prepare special tempWord */
405
4.62k
  tempWord = 0xFFFFFFFF;
406
90.3k
  for (i = 0; i < (NUMBER_OF_BIT_IN_WORD - *pNumBitValidInLastWord); i++) {
407
85.7k
    tempWord = tempWord & ~(1 << i); /* clear bit i in tempWord */
408
85.7k
  }
409
410
  /* calculate last word */
411
4.62k
  r = bitfieldWord << THIRTYTWO_LOG_DIV_TWO_LOG;
412
66.7k
  for (i = 0; i < *pNumBitValidInLastWord; i++) {
413
62.1k
    if (pRemainingBitsInSegment[r + i] == 0) {
414
6.98k
      tempWord = tempWord & ~(1 << (NUMBER_OF_BIT_IN_WORD - 1 -
415
6.98k
                                    i)); /* set a zero at bit number
416
                                            (NUMBER_OF_BIT_IN_WORD-1-i) in
417
                                            tempWord */
418
55.1k
    } else {
419
55.1k
      numValidSegment += 1; /* count segments which are not empty */
420
55.1k
    }
421
62.1k
  }
422
4.62k
  pSegmentBitfield[bitfieldWord] = tempWord; /* store result */
423
424
4.62k
  return numValidSegment;
425
4.62k
}
426
427
/*---------------------------------------------------------------------------------------------
428
  description:  This function sets up sideinfo for the non-PCW decoder (for the
429
current set).
430
---------------------------------------------------------------------------------------------*/
431
19.8k
static void InitNonPCWSideInformationForCurrentSet(H_HCR_INFO pHcr) {
432
19.8k
  USHORT i, k;
433
19.8k
  UCHAR codebookDim;
434
19.8k
  UINT startNode;
435
436
19.8k
  UCHAR *pCodebook = pHcr->nonPcwSideinfo.pCodebook;
437
19.8k
  UINT *iNode = pHcr->nonPcwSideinfo.iNode;
438
19.8k
  UCHAR *pCntSign = pHcr->nonPcwSideinfo.pCntSign;
439
19.8k
  USHORT *iResultPointer = pHcr->nonPcwSideinfo.iResultPointer;
440
19.8k
  UINT *pEscapeSequenceInfo = pHcr->nonPcwSideinfo.pEscapeSequenceInfo;
441
19.8k
  SCHAR *pSta = pHcr->nonPcwSideinfo.pSta;
442
19.8k
  USHORT *pNumExtendedSortedCodewordInSection =
443
19.8k
      pHcr->sectionInfo.pNumExtendedSortedCodewordInSection;
444
19.8k
  int numExtendedSortedCodewordInSectionIdx =
445
19.8k
      pHcr->sectionInfo.numExtendedSortedCodewordInSectionIdx;
446
19.8k
  UCHAR *pExtendedSortedCodebook = pHcr->sectionInfo.pExtendedSortedCodebook;
447
19.8k
  int extendedSortedCodebookIdx = pHcr->sectionInfo.extendedSortedCodebookIdx;
448
19.8k
  USHORT *pNumExtendedSortedSectionsInSets =
449
19.8k
      pHcr->sectionInfo.pNumExtendedSortedSectionsInSets;
450
19.8k
  int numExtendedSortedSectionsInSetsIdx =
451
19.8k
      pHcr->sectionInfo.numExtendedSortedSectionsInSetsIdx;
452
19.8k
  int quantizedSpectralCoefficientsIdx =
453
19.8k
      pHcr->decInOut.quantizedSpectralCoefficientsIdx;
454
19.8k
  const UCHAR *pCbDimension = aDimCb;
455
19.8k
  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
19.8k
  for (i = pNumExtendedSortedSectionsInSets[numExtendedSortedSectionsInSetsIdx];
461
51.7k
       i != 0; i--) {
462
32.0k
    codebookDim =
463
32.0k
        pCbDimension[pExtendedSortedCodebook[extendedSortedCodebookIdx]];
464
32.0k
    startNode = *aHuffTable[pExtendedSortedCodebook[extendedSortedCodebookIdx]];
465
466
32.0k
    for (k = pNumExtendedSortedCodewordInSection
467
32.0k
             [numExtendedSortedCodewordInSectionIdx];
468
277k
         k != 0; k--) {
469
245k
      iterationCounter++;
470
245k
      if (iterationCounter > (1024 >> 2)) {
471
4
        return;
472
4
      }
473
245k
      *pSta++ = aCodebook2StartInt
474
245k
          [pExtendedSortedCodebook[extendedSortedCodebookIdx]];
475
245k
      *pCodebook++ = pExtendedSortedCodebook[extendedSortedCodebookIdx];
476
245k
      *iNode++ = startNode;
477
245k
      *pCntSign++ = 0;
478
245k
      *iResultPointer++ = quantizedSpectralCoefficientsIdx;
479
245k
      *pEscapeSequenceInfo++ = 0;
480
245k
      quantizedSpectralCoefficientsIdx +=
481
245k
          codebookDim; /* update pointer by codebookDim --> point to next
482
                          starting value for writing out */
483
245k
      if (quantizedSpectralCoefficientsIdx >= 1024) {
484
165
        return;
485
165
      }
486
245k
    }
487
31.8k
    numExtendedSortedCodewordInSectionIdx++; /* inc ptr for next ext sort sec in
488
                                                current set */
489
31.8k
    extendedSortedCodebookIdx++; /* inc ptr for next ext sort sec in current set
490
                                  */
491
31.8k
    if (numExtendedSortedCodewordInSectionIdx >= (MAX_SFB_HCR + MAX_HCR_SETS) ||
492
31.8k
        extendedSortedCodebookIdx >= (MAX_SFB_HCR + MAX_HCR_SETS)) {
493
0
      return;
494
0
    }
495
31.8k
  }
496
19.6k
  numExtendedSortedSectionsInSetsIdx++; /* inc ptr for next set of non-PCWs */
497
19.6k
  if (numExtendedSortedCodewordInSectionIdx >= (MAX_SFB_HCR + MAX_HCR_SETS)) {
498
0
    return;
499
0
  }
500
501
  /* Write back indexes */
502
19.6k
  pHcr->sectionInfo.numExtendedSortedCodewordInSectionIdx =
503
19.6k
      numExtendedSortedCodewordInSectionIdx;
504
19.6k
  pHcr->sectionInfo.extendedSortedCodebookIdx = extendedSortedCodebookIdx;
505
19.6k
  pHcr->sectionInfo.numExtendedSortedSectionsInSetsIdx =
506
19.6k
      numExtendedSortedSectionsInSetsIdx;
507
19.6k
  pHcr->sectionInfo.numExtendedSortedCodewordInSectionIdx =
508
19.6k
      numExtendedSortedCodewordInSectionIdx;
509
19.6k
  pHcr->decInOut.quantizedSpectralCoefficientsIdx =
510
19.6k
      quantizedSpectralCoefficientsIdx;
511
19.6k
}
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
2.23M
static INT ModuloValue(INT input, INT bufferlength) {
522
2.23M
  if (input > (bufferlength - 1)) {
523
320k
    return (input - bufferlength);
524
320k
  }
525
1.91M
  if (input < 0) {
526
19.5k
    return (input + bufferlength);
527
19.5k
  }
528
1.89M
  return input;
529
1.91M
}
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
185k
                                 UINT *pBitfield) {
543
185k
  UINT numBitfieldWord;
544
185k
  UINT numBitfieldBit;
545
546
  /* get both values needed for clearing the bit */
547
185k
  numBitfieldWord = offset >> THIRTYTWO_LOG_DIV_TWO_LOG; /* int   = wordNr */
548
185k
  numBitfieldBit = offset - (numBitfieldWord
549
185k
                             << THIRTYTWO_LOG_DIV_TWO_LOG); /* fract = bitNr  */
550
551
  /* clear a bit in bitfield */
552
185k
  pBitfield[numBitfieldWord] =
553
185k
      pBitfield[numBitfieldWord] &
554
185k
      ~(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
185k
  *ptrState = NULL;
559
185k
}
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
58.4k
UINT Hcr_State_BODY_ONLY(HANDLE_FDK_BITSTREAM bs, void *ptr) {
578
58.4k
  H_HCR_INFO pHcr = (H_HCR_INFO)ptr;
579
58.4k
  UINT *pSegmentBitfield;
580
58.4k
  UINT *pCodewordBitfield;
581
58.4k
  UINT segmentOffset;
582
58.4k
  FIXP_DBL *pResultBase;
583
58.4k
  UINT *iNode;
584
58.4k
  USHORT *iResultPointer;
585
58.4k
  UINT codewordOffset;
586
58.4k
  UINT branchNode;
587
58.4k
  UINT branchValue;
588
58.4k
  UINT iQSC;
589
58.4k
  UINT treeNode;
590
58.4k
  UCHAR carryBit;
591
58.4k
  INT *pLeftStartOfSegment;
592
58.4k
  INT *pRightStartOfSegment;
593
58.4k
  SCHAR *pRemainingBitsInSegment;
594
58.4k
  UCHAR readDirection;
595
58.4k
  UCHAR *pCodebook;
596
58.4k
  UCHAR dimCntr;
597
58.4k
  const UINT *pCurrentTree;
598
58.4k
  const UCHAR *pCbDimension;
599
58.4k
  const SCHAR *pQuantVal;
600
58.4k
  const SCHAR *pQuantValBase;
601
602
58.4k
  pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment;
603
58.4k
  pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment;
604
58.4k
  pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment;
605
58.4k
  readDirection = pHcr->segmentInfo.readDirection;
606
58.4k
  pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield;
607
58.4k
  pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield;
608
58.4k
  segmentOffset = pHcr->segmentInfo.segmentOffset;
609
610
58.4k
  pCodebook = pHcr->nonPcwSideinfo.pCodebook;
611
58.4k
  iNode = pHcr->nonPcwSideinfo.iNode;
612
58.4k
  pResultBase = pHcr->nonPcwSideinfo.pResultBase;
613
58.4k
  iResultPointer = pHcr->nonPcwSideinfo.iResultPointer;
614
58.4k
  codewordOffset = pHcr->nonPcwSideinfo.codewordOffset;
615
616
58.4k
  pCbDimension = aDimCb;
617
618
58.4k
  treeNode = iNode[codewordOffset];
619
58.4k
  pCurrentTree = aHuffTable[pCodebook[codewordOffset]];
620
621
124k
  for (; pRemainingBitsInSegment[segmentOffset] > 0;
622
120k
       pRemainingBitsInSegment[segmentOffset] -= 1) {
623
120k
    carryBit = HcrGetABitFromBitstream(
624
120k
        bs, pHcr->decInOut.bitstreamAnchor, &pLeftStartOfSegment[segmentOffset],
625
120k
        &pRightStartOfSegment[segmentOffset], readDirection);
626
627
120k
    CarryBitToBranchValue(carryBit, /* make a step in decoding tree */
628
120k
                          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
120k
    if ((branchNode & TEST_BIT_10) ==
633
120k
        TEST_BIT_10) { /* test bit 10 ; ==> body is complete */
634
54.0k
      pQuantValBase = aQuantTable[pCodebook[codewordOffset]]; /* get base
635
                                                                 address of
636
                                                                 quantized
637
                                                                 values
638
                                                                 belonging to
639
                                                                 current
640
                                                                 codebook */
641
54.0k
      pQuantVal = pQuantValBase + branchValue; /* set pointer to first valid
642
                                                  line [of 2 or 4 quantized
643
                                                  values] */
644
645
54.0k
      iQSC = iResultPointer[codewordOffset]; /* get position of first line for
646
                                                writing out result */
647
648
164k
      for (dimCntr = pCbDimension[pCodebook[codewordOffset]]; dimCntr != 0;
649
110k
           dimCntr--) {
650
110k
        pResultBase[iQSC++] =
651
110k
            (FIXP_DBL)*pQuantVal++; /* write out 2 or 4 lines into
652
                                       spectrum; no Sign bits
653
                                       available in this state */
654
110k
      }
655
656
54.0k
      ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset,
657
54.0k
                           pCodewordBitfield); /* clear a bit in bitfield and
658
                                                  switch off statemachine */
659
54.0k
      pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of
660
                                                      for loop counter (see
661
                                                      above) is done here */
662
54.0k
      break; /* end of branch in tree reached  i.e. a whole nonPCW-Body is
663
                decoded */
664
66.0k
    } else { /* body is not decoded completely: */
665
66.0k
      treeNode = *(
666
66.0k
          pCurrentTree +
667
66.0k
          branchValue); /* update treeNode for further step in decoding tree */
668
66.0k
    }
669
120k
  }
670
58.4k
  iNode[codewordOffset] = treeNode; /* store updated treeNode because maybe
671
                                       decoding of codeword body not finished
672
                                       yet */
673
674
58.4k
  if (pRemainingBitsInSegment[segmentOffset] <= 0) {
675
6.69k
    ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset,
676
6.69k
                         pSegmentBitfield); /* clear a bit in bitfield and
677
                                               switch off statemachine */
678
679
6.69k
    if (pRemainingBitsInSegment[segmentOffset] < 0) {
680
1
      pHcr->decInOut.errorLog |= STATE_ERROR_BODY_ONLY;
681
1
      return BODY_ONLY;
682
1
    }
683
6.69k
  }
684
685
58.4k
  return STOP_THIS_STATE;
686
58.4k
}
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
70.2k
UINT Hcr_State_BODY_SIGN__BODY(HANDLE_FDK_BITSTREAM bs, void *ptr) {
705
70.2k
  H_HCR_INFO pHcr = (H_HCR_INFO)ptr;
706
70.2k
  SCHAR *pRemainingBitsInSegment;
707
70.2k
  INT *pLeftStartOfSegment;
708
70.2k
  INT *pRightStartOfSegment;
709
70.2k
  UCHAR readDirection;
710
70.2k
  UINT *pSegmentBitfield;
711
70.2k
  UINT *pCodewordBitfield;
712
70.2k
  UINT segmentOffset;
713
714
70.2k
  UCHAR *pCodebook;
715
70.2k
  UINT *iNode;
716
70.2k
  UCHAR *pCntSign;
717
70.2k
  FIXP_DBL *pResultBase;
718
70.2k
  USHORT *iResultPointer;
719
70.2k
  UINT codewordOffset;
720
721
70.2k
  UINT iQSC;
722
70.2k
  UINT cntSign;
723
70.2k
  UCHAR dimCntr;
724
70.2k
  UCHAR carryBit;
725
70.2k
  SCHAR *pSta;
726
70.2k
  UINT treeNode;
727
70.2k
  UINT branchValue;
728
70.2k
  UINT branchNode;
729
70.2k
  const UCHAR *pCbDimension;
730
70.2k
  const UINT *pCurrentTree;
731
70.2k
  const SCHAR *pQuantValBase;
732
70.2k
  const SCHAR *pQuantVal;
733
734
70.2k
  pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment;
735
70.2k
  pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment;
736
70.2k
  pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment;
737
70.2k
  readDirection = pHcr->segmentInfo.readDirection;
738
70.2k
  pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield;
739
70.2k
  pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield;
740
70.2k
  segmentOffset = pHcr->segmentInfo.segmentOffset;
741
742
70.2k
  pCodebook = pHcr->nonPcwSideinfo.pCodebook;
743
70.2k
  iNode = pHcr->nonPcwSideinfo.iNode;
744
70.2k
  pCntSign = pHcr->nonPcwSideinfo.pCntSign;
745
70.2k
  pResultBase = pHcr->nonPcwSideinfo.pResultBase;
746
70.2k
  iResultPointer = pHcr->nonPcwSideinfo.iResultPointer;
747
70.2k
  codewordOffset = pHcr->nonPcwSideinfo.codewordOffset;
748
70.2k
  pSta = pHcr->nonPcwSideinfo.pSta;
749
750
70.2k
  pCbDimension = aDimCb;
751
752
70.2k
  treeNode = iNode[codewordOffset];
753
70.2k
  pCurrentTree = aHuffTable[pCodebook[codewordOffset]];
754
755
270k
  for (; pRemainingBitsInSegment[segmentOffset] > 0;
756
257k
       pRemainingBitsInSegment[segmentOffset] -= 1) {
757
257k
    carryBit = HcrGetABitFromBitstream(
758
257k
        bs, pHcr->decInOut.bitstreamAnchor, &pLeftStartOfSegment[segmentOffset],
759
257k
        &pRightStartOfSegment[segmentOffset], readDirection);
760
761
257k
    CarryBitToBranchValue(carryBit, /* make a step in decoding tree */
762
257k
                          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
257k
    if ((branchNode & TEST_BIT_10) ==
767
257k
        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
57.8k
      pQuantValBase = aQuantTable[pCodebook[codewordOffset]]; /* get base
771
                                                                 address of
772
                                                                 quantized
773
                                                                 values
774
                                                                 belonging to
775
                                                                 current
776
                                                                 codebook */
777
57.8k
      pQuantVal = pQuantValBase + branchValue; /* set pointer to first valid
778
                                                  line [of 2 or 4 quantized
779
                                                  values] */
780
781
57.8k
      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
57.8k
      cntSign = 0;
789
175k
      for (dimCntr = pCbDimension[pCodebook[codewordOffset]]; dimCntr != 0;
790
117k
           dimCntr--) {
791
117k
        pResultBase[iQSC++] =
792
117k
            (FIXP_DBL)*pQuantVal; /* write quant. spec. coef. into spectrum */
793
117k
        if (*pQuantVal++ != 0) {
794
100k
          cntSign += 1;
795
100k
        }
796
117k
      }
797
798
57.8k
      if (cntSign == 0) {
799
3.80k
        ClearBitFromBitfield(
800
3.80k
            &(pHcr->nonPcwSideinfo.pState), segmentOffset,
801
3.80k
            pCodewordBitfield); /* clear a bit in bitfield and switch off
802
                                   statemachine */
803
54.0k
      } else {
804
54.0k
        pCntSign[codewordOffset] = cntSign;     /* write sign count result into
805
                                                   codewordsideinfo of current
806
                                                   codeword */
807
54.0k
        pSta[codewordOffset] = BODY_SIGN__SIGN; /* change state */
808
54.0k
        pHcr->nonPcwSideinfo.pState =
809
54.0k
            aStateConstant2State[pSta[codewordOffset]]; /* get state from
810
                                                           separate array of
811
                                                           cw-sideinfo */
812
54.0k
      }
813
57.8k
      pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of
814
                                                      for loop counter (see
815
                                                      above) is done here */
816
57.8k
      break; /* end of branch in tree reached  i.e. a whole nonPCW-Body is
817
                decoded */
818
199k
    } else { /* body is not decoded completely: */
819
199k
      treeNode = *(
820
199k
          pCurrentTree +
821
199k
          branchValue); /* update treeNode for further step in decoding tree */
822
199k
    }
823
257k
  }
824
70.2k
  iNode[codewordOffset] = treeNode; /* store updated treeNode because maybe
825
                                       decoding of codeword body not finished
826
                                       yet */
827
828
70.2k
  if (pRemainingBitsInSegment[segmentOffset] <= 0) {
829
19.1k
    ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset,
830
19.1k
                         pSegmentBitfield); /* clear a bit in bitfield and
831
                                               switch off statemachine */
832
833
19.1k
    if (pRemainingBitsInSegment[segmentOffset] < 0) {
834
143
      pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN__BODY;
835
143
      return BODY_SIGN__BODY;
836
143
    }
837
19.1k
  }
838
839
70.1k
  return STOP_THIS_STATE;
840
70.2k
}
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
61.8k
UINT Hcr_State_BODY_SIGN__SIGN(HANDLE_FDK_BITSTREAM bs, void *ptr) {
854
61.8k
  H_HCR_INFO pHcr = (H_HCR_INFO)ptr;
855
61.8k
  SCHAR *pRemainingBitsInSegment;
856
61.8k
  INT *pLeftStartOfSegment;
857
61.8k
  INT *pRightStartOfSegment;
858
61.8k
  UCHAR readDirection;
859
61.8k
  UINT *pSegmentBitfield;
860
61.8k
  UINT *pCodewordBitfield;
861
61.8k
  UINT segmentOffset;
862
863
61.8k
  UCHAR *pCntSign;
864
61.8k
  FIXP_DBL *pResultBase;
865
61.8k
  USHORT *iResultPointer;
866
61.8k
  UINT codewordOffset;
867
868
61.8k
  UCHAR carryBit;
869
61.8k
  UINT iQSC;
870
61.8k
  UCHAR cntSign;
871
872
61.8k
  pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment;
873
61.8k
  pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment;
874
61.8k
  pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment;
875
61.8k
  readDirection = pHcr->segmentInfo.readDirection;
876
61.8k
  pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield;
877
61.8k
  pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield;
878
61.8k
  segmentOffset = pHcr->segmentInfo.segmentOffset;
879
880
  /*pCodebook               = */
881
61.8k
  pCntSign = pHcr->nonPcwSideinfo.pCntSign;
882
61.8k
  pResultBase = pHcr->nonPcwSideinfo.pResultBase;
883
61.8k
  iResultPointer = pHcr->nonPcwSideinfo.iResultPointer;
884
61.8k
  codewordOffset = pHcr->nonPcwSideinfo.codewordOffset;
885
886
61.8k
  iQSC = iResultPointer[codewordOffset];
887
61.8k
  cntSign = pCntSign[codewordOffset];
888
889
  /* loop for sign bit decoding */
890
108k
  for (; pRemainingBitsInSegment[segmentOffset] > 0;
891
99.3k
       pRemainingBitsInSegment[segmentOffset] -= 1) {
892
99.3k
    carryBit = HcrGetABitFromBitstream(
893
99.3k
        bs, pHcr->decInOut.bitstreamAnchor, &pLeftStartOfSegment[segmentOffset],
894
99.3k
        &pRightStartOfSegment[segmentOffset], readDirection);
895
99.3k
    cntSign -=
896
99.3k
        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
106k
    while (pResultBase[iQSC] == (FIXP_DBL)0) {
901
6.63k
      if (++iQSC >= 1024) { /* points to current value different from zero */
902
0
        return BODY_SIGN__SIGN;
903
0
      }
904
6.63k
    }
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
99.3k
    if (carryBit != 0) {
909
20.2k
      pResultBase[iQSC] = -pResultBase[iQSC]; /* carryBit = 1 --> minus */
910
20.2k
    }
911
912
99.3k
    iQSC++; /* update pointer to next (maybe valid) value */
913
914
99.3k
    if (cntSign == 0) { /* if (cntSign==0)  ==>  set state CODEWORD_DECODED */
915
53.2k
      ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset,
916
53.2k
                           pCodewordBitfield); /* clear a bit in bitfield and
917
                                                  switch off statemachine */
918
53.2k
      pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of
919
                                                      for loop counter (see
920
                                                      above) is done here */
921
53.2k
      break; /* whole nonPCW-Body and according sign bits are decoded */
922
53.2k
    }
923
99.3k
  }
924
61.8k
  pCntSign[codewordOffset] = cntSign;
925
61.8k
  iResultPointer[codewordOffset] = iQSC; /* store updated pResultPointer */
926
927
61.8k
  if (pRemainingBitsInSegment[segmentOffset] <= 0) {
928
20.1k
    ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset,
929
20.1k
                         pSegmentBitfield); /* clear a bit in bitfield and
930
                                               switch off statemachine */
931
932
20.1k
    if (pRemainingBitsInSegment[segmentOffset] < 0) {
933
4
      pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN__SIGN;
934
4
      return BODY_SIGN__SIGN;
935
4
    }
936
20.1k
  }
937
938
61.8k
  return STOP_THIS_STATE;
939
61.8k
}
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
24.7k
UINT Hcr_State_BODY_SIGN_ESC__BODY(HANDLE_FDK_BITSTREAM bs, void *ptr) {
958
24.7k
  H_HCR_INFO pHcr = (H_HCR_INFO)ptr;
959
24.7k
  SCHAR *pRemainingBitsInSegment;
960
24.7k
  INT *pLeftStartOfSegment;
961
24.7k
  INT *pRightStartOfSegment;
962
24.7k
  UCHAR readDirection;
963
24.7k
  UINT *pSegmentBitfield;
964
24.7k
  UINT *pCodewordBitfield;
965
24.7k
  UINT segmentOffset;
966
967
24.7k
  UINT *iNode;
968
24.7k
  UCHAR *pCntSign;
969
24.7k
  FIXP_DBL *pResultBase;
970
24.7k
  USHORT *iResultPointer;
971
24.7k
  UINT codewordOffset;
972
973
24.7k
  UCHAR carryBit;
974
24.7k
  UINT iQSC;
975
24.7k
  UINT cntSign;
976
24.7k
  UINT dimCntr;
977
24.7k
  UINT treeNode;
978
24.7k
  SCHAR *pSta;
979
24.7k
  UINT branchNode;
980
24.7k
  UINT branchValue;
981
24.7k
  const UINT *pCurrentTree;
982
24.7k
  const SCHAR *pQuantValBase;
983
24.7k
  const SCHAR *pQuantVal;
984
985
24.7k
  pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment;
986
24.7k
  pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment;
987
24.7k
  pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment;
988
24.7k
  readDirection = pHcr->segmentInfo.readDirection;
989
24.7k
  pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield;
990
24.7k
  pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield;
991
24.7k
  segmentOffset = pHcr->segmentInfo.segmentOffset;
992
993
24.7k
  iNode = pHcr->nonPcwSideinfo.iNode;
994
24.7k
  pCntSign = pHcr->nonPcwSideinfo.pCntSign;
995
24.7k
  pResultBase = pHcr->nonPcwSideinfo.pResultBase;
996
24.7k
  iResultPointer = pHcr->nonPcwSideinfo.iResultPointer;
997
24.7k
  codewordOffset = pHcr->nonPcwSideinfo.codewordOffset;
998
24.7k
  pSta = pHcr->nonPcwSideinfo.pSta;
999
1000
24.7k
  treeNode = iNode[codewordOffset];
1001
24.7k
  pCurrentTree = aHuffTable[ESCAPE_CODEBOOK];
1002
1003
116k
  for (; pRemainingBitsInSegment[segmentOffset] > 0;
1004
114k
       pRemainingBitsInSegment[segmentOffset] -= 1) {
1005
114k
    carryBit = HcrGetABitFromBitstream(
1006
114k
        bs, pHcr->decInOut.bitstreamAnchor, &pLeftStartOfSegment[segmentOffset],
1007
114k
        &pRightStartOfSegment[segmentOffset], readDirection);
1008
1009
    /* make a step in tree */
1010
114k
    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
114k
    if ((branchNode & TEST_BIT_10) ==
1015
114k
        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
22.3k
      pQuantValBase = aQuantTable[ESCAPE_CODEBOOK]; /* get base address of
1021
                                                       quantized values
1022
                                                       belonging to current
1023
                                                       codebook */
1024
22.3k
      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
22.3k
      iNode[codewordOffset] = iResultPointer[codewordOffset];
1031
1032
      /* get position of first line for writing result */
1033
22.3k
      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
22.3k
      cntSign = 0;
1040
1041
67.1k
      for (dimCntr = DIMENSION_OF_ESCAPE_CODEBOOK; dimCntr != 0; dimCntr--) {
1042
44.7k
        pResultBase[iQSC++] =
1043
44.7k
            (FIXP_DBL)*pQuantVal; /* write quant. spec. coef. into spectrum */
1044
44.7k
        if (*pQuantVal++ != 0) {
1045
16.3k
          cntSign += 1;
1046
16.3k
        }
1047
44.7k
      }
1048
1049
22.3k
      if (cntSign == 0) {
1050
13.6k
        ClearBitFromBitfield(
1051
13.6k
            &(pHcr->nonPcwSideinfo.pState), segmentOffset,
1052
13.6k
            pCodewordBitfield); /* clear a bit in bitfield and switch off
1053
                                   statemachine */
1054
        /* codeword decoded */
1055
13.6k
      } else {
1056
        /* write sign count result into codewordsideinfo of current codeword */
1057
8.73k
        pCntSign[codewordOffset] = cntSign;
1058
8.73k
        pSta[codewordOffset] = BODY_SIGN_ESC__SIGN; /* change state */
1059
8.73k
        pHcr->nonPcwSideinfo.pState =
1060
8.73k
            aStateConstant2State[pSta[codewordOffset]]; /* get state from
1061
                                                           separate array of
1062
                                                           cw-sideinfo */
1063
8.73k
      }
1064
22.3k
      pRemainingBitsInSegment[segmentOffset] -= 1; /* the last reinitialzation
1065
                                                      of for loop counter (see
1066
                                                      above) is done here */
1067
22.3k
      break; /* end of branch in tree reached  i.e. a whole nonPCW-Body is
1068
                decoded */
1069
91.9k
    } 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
91.9k
      treeNode = *(pCurrentTree + branchValue);
1073
91.9k
      iNode[codewordOffset] = treeNode;
1074
91.9k
    }
1075
114k
  }
1076
1077
24.7k
  if (pRemainingBitsInSegment[segmentOffset] <= 0) {
1078
3.22k
    ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset,
1079
3.22k
                         pSegmentBitfield); /* clear a bit in bitfield and
1080
                                               switch off statemachine */
1081
1082
3.22k
    if (pRemainingBitsInSegment[segmentOffset] < 0) {
1083
214
      pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN_ESC__BODY;
1084
214
      return BODY_SIGN_ESC__BODY;
1085
214
    }
1086
3.22k
  }
1087
1088
24.4k
  return STOP_THIS_STATE;
1089
24.7k
}
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
8.98k
UINT Hcr_State_BODY_SIGN_ESC__SIGN(HANDLE_FDK_BITSTREAM bs, void *ptr) {
1121
8.98k
  H_HCR_INFO pHcr = (H_HCR_INFO)ptr;
1122
8.98k
  SCHAR *pRemainingBitsInSegment;
1123
8.98k
  INT *pLeftStartOfSegment;
1124
8.98k
  INT *pRightStartOfSegment;
1125
8.98k
  UCHAR readDirection;
1126
8.98k
  UINT *pSegmentBitfield;
1127
8.98k
  UINT *pCodewordBitfield;
1128
8.98k
  UINT segmentOffset;
1129
1130
8.98k
  UINT *iNode;
1131
8.98k
  UCHAR *pCntSign;
1132
8.98k
  FIXP_DBL *pResultBase;
1133
8.98k
  USHORT *iResultPointer;
1134
8.98k
  UINT *pEscapeSequenceInfo;
1135
8.98k
  UINT codewordOffset;
1136
1137
8.98k
  UINT iQSC;
1138
8.98k
  UCHAR cntSign;
1139
8.98k
  UINT flagA;
1140
8.98k
  UINT flagB;
1141
8.98k
  UINT flags;
1142
8.98k
  UCHAR carryBit;
1143
8.98k
  SCHAR *pSta;
1144
1145
8.98k
  pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment;
1146
8.98k
  pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment;
1147
8.98k
  pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment;
1148
8.98k
  readDirection = pHcr->segmentInfo.readDirection;
1149
8.98k
  pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield;
1150
8.98k
  pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield;
1151
8.98k
  segmentOffset = pHcr->segmentInfo.segmentOffset;
1152
1153
8.98k
  iNode = pHcr->nonPcwSideinfo.iNode;
1154
8.98k
  pCntSign = pHcr->nonPcwSideinfo.pCntSign;
1155
8.98k
  pResultBase = pHcr->nonPcwSideinfo.pResultBase;
1156
8.98k
  iResultPointer = pHcr->nonPcwSideinfo.iResultPointer;
1157
8.98k
  pEscapeSequenceInfo = pHcr->nonPcwSideinfo.pEscapeSequenceInfo;
1158
8.98k
  codewordOffset = pHcr->nonPcwSideinfo.codewordOffset;
1159
8.98k
  pSta = pHcr->nonPcwSideinfo.pSta;
1160
1161
8.98k
  iQSC = iResultPointer[codewordOffset];
1162
8.98k
  cntSign = pCntSign[codewordOffset];
1163
1164
  /* loop for sign bit decoding */
1165
16.5k
  for (; pRemainingBitsInSegment[segmentOffset] > 0;
1166
16.2k
       pRemainingBitsInSegment[segmentOffset] -= 1) {
1167
16.2k
    carryBit = HcrGetABitFromBitstream(
1168
16.2k
        bs, pHcr->decInOut.bitstreamAnchor, &pLeftStartOfSegment[segmentOffset],
1169
16.2k
        &pRightStartOfSegment[segmentOffset], readDirection);
1170
1171
    /* decrement sign counter because one sign bit has been read */
1172
16.2k
    cntSign -= 1;
1173
16.2k
    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
16.7k
    while (pResultBase[iQSC] == (FIXP_DBL)0) {
1178
451
      if (++iQSC >= 1024) {
1179
0
        return BODY_SIGN_ESC__SIGN;
1180
0
      }
1181
451
    }
1182
16.2k
    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
16.2k
    if (carryBit != 0) {
1188
4.91k
      pResultBase[iQSC] = -pResultBase[iQSC]; /* carryBit = 1 --> minus */
1189
4.91k
    }
1190
16.2k
    iQSC++; /* update index to next (maybe valid) value */
1191
16.2k
    iResultPointer[codewordOffset] = iQSC;
1192
1193
16.2k
    if (cntSign == 0) {
1194
      /* all sign bits are decoded now */
1195
8.68k
      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
8.68k
      iQSC = iNode[codewordOffset];
1207
1208
      /* step 1 */
1209
      /* test first value if escape sequence follows */
1210
8.68k
      flagA = 0; /* for first possible escape sequence */
1211
8.68k
      if (fixp_abs(pResultBase[iQSC++]) == (FIXP_DBL)ESCAPE_VALUE) {
1212
1.65k
        flagA = 1;
1213
1.65k
      }
1214
1215
      /* step 2 */
1216
      /* test second value if escape sequence follows */
1217
8.68k
      flagB = 0; /* for second possible escape sequence */
1218
8.68k
      if (fixp_abs(pResultBase[iQSC]) == (FIXP_DBL)ESCAPE_VALUE) {
1219
2.35k
        flagB = 1;
1220
2.35k
      }
1221
1222
      /* step 3 */
1223
      /* evaluate flag result and go on if necessary */
1224
8.68k
      if (!flagA && !flagB) {
1225
5.96k
        ClearBitFromBitfield(
1226
5.96k
            &(pHcr->nonPcwSideinfo.pState), segmentOffset,
1227
5.96k
            pCodewordBitfield); /* clear a bit in bitfield and switch off
1228
                                   statemachine */
1229
5.96k
      } 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
2.72k
        flags = flagA << POSITION_OF_FLAG_A;
1234
2.72k
        flags |= (flagB << POSITION_OF_FLAG_B);
1235
2.72k
        pEscapeSequenceInfo[codewordOffset] = flags;
1236
1237
        /* set next state */
1238
2.72k
        pSta[codewordOffset] = BODY_SIGN_ESC__ESC_PREFIX;
1239
2.72k
        pHcr->nonPcwSideinfo.pState =
1240
2.72k
            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
2.72k
        iResultPointer[codewordOffset] = iNode[codewordOffset];
1246
1247
2.72k
        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
1.07k
          iQSC = iResultPointer[codewordOffset];
1252
1.07k
          iQSC++;
1253
1.07k
          iResultPointer[codewordOffset] = iQSC;
1254
1.07k
        }
1255
1256
2.72k
      }      /* at least one of two lines is 16 */
1257
8.68k
      break; /* nonPCW-Body at cb 11 and according sign bits are decoded */
1258
1259
8.68k
    } /* if ( cntSign == 0 ) */
1260
16.2k
  }   /* loop over remaining Bits in segment */
1261
1262
8.98k
  if (pRemainingBitsInSegment[segmentOffset] <= 0) {
1263
641
    ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset,
1264
641
                         pSegmentBitfield); /* clear a bit in bitfield and
1265
                                               switch off statemachine */
1266
1267
641
    if (pRemainingBitsInSegment[segmentOffset] < 0) {
1268
1
      pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN_ESC__SIGN;
1269
1
      return BODY_SIGN_ESC__SIGN;
1270
1
    }
1271
641
  }
1272
8.98k
  return STOP_THIS_STATE;
1273
8.98k
}
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
4.26k
UINT Hcr_State_BODY_SIGN_ESC__ESC_PREFIX(HANDLE_FDK_BITSTREAM bs, void *ptr) {
1292
4.26k
  H_HCR_INFO pHcr = (H_HCR_INFO)ptr;
1293
4.26k
  SCHAR *pRemainingBitsInSegment;
1294
4.26k
  INT *pLeftStartOfSegment;
1295
4.26k
  INT *pRightStartOfSegment;
1296
4.26k
  UCHAR readDirection;
1297
4.26k
  UINT *pSegmentBitfield;
1298
4.26k
  UINT segmentOffset;
1299
4.26k
  UINT *pEscapeSequenceInfo;
1300
4.26k
  UINT codewordOffset;
1301
4.26k
  UCHAR carryBit;
1302
4.26k
  UINT escapePrefixUp;
1303
4.26k
  SCHAR *pSta;
1304
1305
4.26k
  pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment;
1306
4.26k
  pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment;
1307
4.26k
  pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment;
1308
4.26k
  readDirection = pHcr->segmentInfo.readDirection;
1309
4.26k
  pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield;
1310
4.26k
  segmentOffset = pHcr->segmentInfo.segmentOffset;
1311
4.26k
  pEscapeSequenceInfo = pHcr->nonPcwSideinfo.pEscapeSequenceInfo;
1312
4.26k
  codewordOffset = pHcr->nonPcwSideinfo.codewordOffset;
1313
4.26k
  pSta = pHcr->nonPcwSideinfo.pSta;
1314
1315
4.26k
  escapePrefixUp =
1316
4.26k
      (pEscapeSequenceInfo[codewordOffset] & MASK_ESCAPE_PREFIX_UP) >>
1317
4.26k
      LSB_ESCAPE_PREFIX_UP;
1318
1319
  /* decode escape prefix */
1320
5.96k
  for (; pRemainingBitsInSegment[segmentOffset] > 0;
1321
5.69k
       pRemainingBitsInSegment[segmentOffset] -= 1) {
1322
5.69k
    carryBit = HcrGetABitFromBitstream(
1323
5.69k
        bs, pHcr->decInOut.bitstreamAnchor, &pLeftStartOfSegment[segmentOffset],
1324
5.69k
        &pRightStartOfSegment[segmentOffset], readDirection);
1325
1326
    /* count ones and store sum in escapePrefixUp */
1327
5.69k
    if (carryBit == 1) {
1328
1.71k
      escapePrefixUp += 1; /* update conter for ones */
1329
1.71k
      if (escapePrefixUp > 8) {
1330
9
        pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN_ESC__ESC_PREFIX;
1331
9
        return BODY_SIGN_ESC__ESC_PREFIX;
1332
9
      }
1333
1334
      /* store updated counter in sideinfo of current codeword */
1335
1.70k
      pEscapeSequenceInfo[codewordOffset] &=
1336
1.70k
          ~MASK_ESCAPE_PREFIX_UP;              /* delete old escapePrefixUp */
1337
1.70k
      escapePrefixUp <<= LSB_ESCAPE_PREFIX_UP; /* shift to correct position */
1338
1.70k
      pEscapeSequenceInfo[codewordOffset] |=
1339
1.70k
          escapePrefixUp;                      /* insert new escapePrefixUp */
1340
1.70k
      escapePrefixUp >>= LSB_ESCAPE_PREFIX_UP; /* shift back down */
1341
3.98k
    } else {                                   /* separator [zero] reached */
1342
3.98k
      pRemainingBitsInSegment[segmentOffset] -= 1; /* last reinitialzation of
1343
                                                      for loop counter (see
1344
                                                      above) is done here */
1345
3.98k
      escapePrefixUp +=
1346
3.98k
          4; /* if escape_separator '0' appears, add 4 and ==> break */
1347
1348
      /* store escapePrefixUp in pEscapeSequenceInfo[codewordOffset] at bit
1349
       * position escapePrefixUp */
1350
3.98k
      pEscapeSequenceInfo[codewordOffset] &=
1351
3.98k
          ~MASK_ESCAPE_PREFIX_UP;              /* delete old escapePrefixUp */
1352
3.98k
      escapePrefixUp <<= LSB_ESCAPE_PREFIX_UP; /* shift to correct position */
1353
3.98k
      pEscapeSequenceInfo[codewordOffset] |=
1354
3.98k
          escapePrefixUp;                      /* insert new escapePrefixUp */
1355
3.98k
      escapePrefixUp >>= LSB_ESCAPE_PREFIX_UP; /* shift back down */
1356
1357
      /* store escapePrefixUp in pEscapeSequenceInfo[codewordOffset] at bit
1358
       * position escapePrefixDown */
1359
3.98k
      pEscapeSequenceInfo[codewordOffset] &=
1360
3.98k
          ~MASK_ESCAPE_PREFIX_DOWN; /* delete old escapePrefixDown */
1361
3.98k
      escapePrefixUp <<= LSB_ESCAPE_PREFIX_DOWN; /* shift to correct position */
1362
3.98k
      pEscapeSequenceInfo[codewordOffset] |=
1363
3.98k
          escapePrefixUp; /* insert new escapePrefixDown */
1364
1365
3.98k
      pSta[codewordOffset] = BODY_SIGN_ESC__ESC_WORD; /* set next state */
1366
3.98k
      pHcr->nonPcwSideinfo.pState =
1367
3.98k
          aStateConstant2State[pSta[codewordOffset]]; /* get state from separate
1368
                                                         array of cw-sideinfo */
1369
3.98k
      break;
1370
3.98k
    }
1371
5.69k
  }
1372
1373
4.25k
  if (pRemainingBitsInSegment[segmentOffset] <= 0) {
1374
515
    ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset,
1375
515
                         pSegmentBitfield); /* clear a bit in bitfield and
1376
                                               switch off statemachine */
1377
1378
515
    if (pRemainingBitsInSegment[segmentOffset] < 0) {
1379
2
      pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN_ESC__ESC_PREFIX;
1380
2
      return BODY_SIGN_ESC__ESC_PREFIX;
1381
2
    }
1382
515
  }
1383
1384
4.25k
  return STOP_THIS_STATE;
1385
4.25k
}
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
5.37k
UINT Hcr_State_BODY_SIGN_ESC__ESC_WORD(HANDLE_FDK_BITSTREAM bs, void *ptr) {
1401
5.37k
  H_HCR_INFO pHcr = (H_HCR_INFO)ptr;
1402
5.37k
  SCHAR *pRemainingBitsInSegment;
1403
5.37k
  INT *pLeftStartOfSegment;
1404
5.37k
  INT *pRightStartOfSegment;
1405
5.37k
  UCHAR readDirection;
1406
5.37k
  UINT *pSegmentBitfield;
1407
5.37k
  UINT *pCodewordBitfield;
1408
5.37k
  UINT segmentOffset;
1409
1410
5.37k
  FIXP_DBL *pResultBase;
1411
5.37k
  USHORT *iResultPointer;
1412
5.37k
  UINT *pEscapeSequenceInfo;
1413
5.37k
  UINT codewordOffset;
1414
1415
5.37k
  UINT escapeWord;
1416
5.37k
  UINT escapePrefixDown;
1417
5.37k
  UINT escapePrefixUp;
1418
5.37k
  UCHAR carryBit;
1419
5.37k
  UINT iQSC;
1420
5.37k
  INT sign;
1421
5.37k
  UINT flagA;
1422
5.37k
  UINT flagB;
1423
5.37k
  SCHAR *pSta;
1424
1425
5.37k
  pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment;
1426
5.37k
  pLeftStartOfSegment = pHcr->segmentInfo.pLeftStartOfSegment;
1427
5.37k
  pRightStartOfSegment = pHcr->segmentInfo.pRightStartOfSegment;
1428
5.37k
  readDirection = pHcr->segmentInfo.readDirection;
1429
5.37k
  pSegmentBitfield = pHcr->segmentInfo.pSegmentBitfield;
1430
5.37k
  pCodewordBitfield = pHcr->segmentInfo.pCodewordBitfield;
1431
5.37k
  segmentOffset = pHcr->segmentInfo.segmentOffset;
1432
1433
5.37k
  pResultBase = pHcr->nonPcwSideinfo.pResultBase;
1434
5.37k
  iResultPointer = pHcr->nonPcwSideinfo.iResultPointer;
1435
5.37k
  pEscapeSequenceInfo = pHcr->nonPcwSideinfo.pEscapeSequenceInfo;
1436
5.37k
  codewordOffset = pHcr->nonPcwSideinfo.codewordOffset;
1437
5.37k
  pSta = pHcr->nonPcwSideinfo.pSta;
1438
1439
5.37k
  escapeWord = pEscapeSequenceInfo[codewordOffset] & MASK_ESCAPE_WORD;
1440
5.37k
  escapePrefixDown =
1441
5.37k
      (pEscapeSequenceInfo[codewordOffset] & MASK_ESCAPE_PREFIX_DOWN) >>
1442
5.37k
      LSB_ESCAPE_PREFIX_DOWN;
1443
1444
  /* decode escape word */
1445
18.8k
  for (; pRemainingBitsInSegment[segmentOffset] > 0;
1446
17.4k
       pRemainingBitsInSegment[segmentOffset] -= 1) {
1447
17.4k
    carryBit = HcrGetABitFromBitstream(
1448
17.4k
        bs, pHcr->decInOut.bitstreamAnchor, &pLeftStartOfSegment[segmentOffset],
1449
17.4k
        &pRightStartOfSegment[segmentOffset], readDirection);
1450
1451
    /* build escape word */
1452
17.4k
    escapeWord <<=
1453
17.4k
        1; /* left shift previous decoded part of escapeWord by on bit */
1454
17.4k
    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
17.4k
    escapePrefixDown -= 1;
1459
1460
    /* store updated escapePrefixDown */
1461
17.4k
    pEscapeSequenceInfo[codewordOffset] &=
1462
17.4k
        ~MASK_ESCAPE_PREFIX_DOWN; /* delete old escapePrefixDown */
1463
17.4k
    escapePrefixDown <<= LSB_ESCAPE_PREFIX_DOWN; /* shift to correct position */
1464
17.4k
    pEscapeSequenceInfo[codewordOffset] |=
1465
17.4k
        escapePrefixDown; /* insert new escapePrefixDown */
1466
17.4k
    escapePrefixDown >>= LSB_ESCAPE_PREFIX_DOWN; /* shift back */
1467
1468
    /* store updated escapeWord */
1469
17.4k
    pEscapeSequenceInfo[codewordOffset] &=
1470
17.4k
        ~MASK_ESCAPE_WORD; /* delete old escapeWord */
1471
17.4k
    pEscapeSequenceInfo[codewordOffset] |=
1472
17.4k
        escapeWord; /* insert new escapeWord */
1473
1474
17.4k
    if (escapePrefixDown == 0) {
1475
3.96k
      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
3.96k
      iQSC = iResultPointer[codewordOffset];
1485
3.96k
      sign = (pResultBase[iQSC] >= (FIXP_DBL)0)
1486
3.96k
                 ? 1
1487
3.96k
                 : -1; /* get sign of escape value 16 */
1488
1489
      /* step 1 */
1490
      /* get escapePrefixUp */
1491
3.96k
      escapePrefixUp =
1492
3.96k
          (pEscapeSequenceInfo[codewordOffset] & MASK_ESCAPE_PREFIX_UP) >>
1493
3.96k
          LSB_ESCAPE_PREFIX_UP;
1494
1495
      /* step 2 */
1496
      /* calculate escape value */
1497
3.96k
      pResultBase[iQSC] =
1498
3.96k
          (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
3.96k
      flagA = pEscapeSequenceInfo[codewordOffset] & MASK_FLAG_A;
1503
3.96k
      flagB = pEscapeSequenceInfo[codewordOffset] & MASK_FLAG_B;
1504
1505
      /* step 3 */
1506
      /* clear the whole escape sideinfo word */
1507
3.96k
      pEscapeSequenceInfo[codewordOffset] = 0;
1508
1509
      /* change state in dependence of flag flagB */
1510
3.96k
      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
1.63k
        pEscapeSequenceInfo[codewordOffset] &= ~MASK_FLAG_A;
1517
1518
1.63k
        if (flagB == 0) {
1519
354
          ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset,
1520
354
                               pCodewordBitfield); /* clear a bit in bitfield
1521
                                                      and switch off
1522
                                                      statemachine */
1523
1.28k
        } else {
1524
          /* updated pointer to next and last 16 */
1525
1.28k
          iQSC++;
1526
1.28k
          iResultPointer[codewordOffset] = iQSC;
1527
1528
          /* change state */
1529
1.28k
          pSta[codewordOffset] = BODY_SIGN_ESC__ESC_PREFIX;
1530
1.28k
          pHcr->nonPcwSideinfo.pState =
1531
1.28k
              aStateConstant2State[pSta[codewordOffset]]; /* get state from
1532
                                                             separate array of
1533
                                                             cw-sideinfo */
1534
1.28k
        }
1535
2.33k
      } else {
1536
2.33k
        ClearBitFromBitfield(
1537
2.33k
            &(pHcr->nonPcwSideinfo.pState), segmentOffset,
1538
2.33k
            pCodewordBitfield); /* clear a bit in bitfield and switch off
1539
                                   statemachine */
1540
2.33k
      }
1541
3.96k
      break;
1542
3.96k
    }
1543
17.4k
  }
1544
1545
5.37k
  if (pRemainingBitsInSegment[segmentOffset] <= 0) {
1546
1.53k
    ClearBitFromBitfield(&(pHcr->nonPcwSideinfo.pState), segmentOffset,
1547
1.53k
                         pSegmentBitfield); /* clear a bit in bitfield and
1548
                                               switch off statemachine */
1549
1550
1.53k
    if (pRemainingBitsInSegment[segmentOffset] < 0) {
1551
2
      pHcr->decInOut.errorLog |= STATE_ERROR_BODY_SIGN_ESC__ESC_WORD;
1552
2
      return BODY_SIGN_ESC__ESC_WORD;
1553
2
    }
1554
1.53k
  }
1555
1556
5.36k
  return STOP_THIS_STATE;
1557
5.37k
}