aboutsummaryrefslogtreecommitdiffstats
path: root/libAACdec/src/aacdec_hcr.cpp
blob: 5b8683375653958f1544b7656675d12141968a85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
/* -----------------------------------------------------------------------------------------------------------
Software License for The Fraunhofer FDK AAC Codec Library for Android

� Copyright  1995 - 2012 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
  All rights reserved.

 1.    INTRODUCTION
The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
This FDK AAC Codec software is intended to be used on a wide variety of Android devices.

AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
of the MPEG specifications.

Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
individually for the purpose of encoding or decoding bit streams in products that are compliant with
the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
software may already be covered under those patent licenses when it is used for those licensed purposes only.

Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
applications information and documentation.

2.    COPYRIGHT LICENSE

Redistribution and use in source and binary forms, with or without modification, are permitted without
payment of copyright license fees provided that you satisfy the following conditions:

You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
your modifications thereto in source code form.

You must retain the complete text of this software license in the documentation and/or other materials
provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
modifications thereto to recipients of copies in binary form.

The name of Fraunhofer may not be used to endorse or promote products derived from this library without
prior written permission.

You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
software or your modifications thereto.

Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
and the date of any change. For modified versions of the FDK AAC Codec, the term
"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."

3.    NO PATENT LICENSE

NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
respect to this software.

You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
by appropriate patent licenses.

4.    DISCLAIMER

This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
or business interruption, however caused and on any theory of liability, whether in contract, strict
liability, or tort (including negligence), arising in any way out of the use of this software, even if
advised of the possibility of such damage.

5.    CONTACT INFORMATION

Fraunhofer Institute for Integrated Circuits IIS
Attention: Audio and Multimedia Departments - FDK AAC LL
Am Wolfsmantel 33
91058 Erlangen, Germany

www.iis.fraunhofer.de/amm
amm-info@iis.fraunhofer.de
----------------------------------------------------------------------------------------------------------- */

/*****************************  MPEG-4 AAC Decoder  ***************************

   Author(s):   Robert Weidner (DSP Solutions)
   Description: HCR Decoder: HCR initialization, preprocess HCR sideinfo,
                decode priority codewords (PCWs)

*******************************************************************************/

#include "aacdec_hcr.h"



#include "aacdec_hcr_types.h"
#include "aacdec_hcr_bit.h"
#include "aacdec_hcrs.h"
#include "aac_ram.h"
#include "aac_rom.h"
#include "channel.h"
#include "block.h"

#include "aacdecoder.h"     /* for ID_CPE, ID_SCE ... */
#include "FDK_bitstream.h"

extern int mlFileChCurr;

static void errDetectorInHcrSideinfoShrt(SCHAR cb,
                                         SHORT numLine,
                                         UINT *errorWord);

static void errDetectorInHcrLengths(SCHAR  lengthOfLongestCodeword,
                                    SHORT  lengthOfReorderedSpectralData,
                                    UINT  *errorWord);

static void HcrCalcNumCodeword                    (H_HCR_INFO pHcr);
static void HcrSortCodebookAndNumCodewordInSection(H_HCR_INFO pHcr);
static void HcrPrepareSegmentationGrid            (H_HCR_INFO pHcr);
static void HcrExtendedSectionInfo                (H_HCR_INFO pHcr);

static void DeriveNumberOfExtendedSortedSectionsInSets(UINT   numSegment,
                                                       USHORT  *pNumExtendedSortedCodewordInSection,
                                                       int      numExtendedSortedCodewordInSectionIdx,
                                                       USHORT  *pNumExtendedSortedSectionsInSets,
                                                       int      numExtendedSortedSectionsInSetsIdx);

static INT  DecodeEscapeSequence(HANDLE_FDK_BITSTREAM  bs,
                                 INT                   quantSpecCoef,
                                 USHORT               *pLeftStartOfSegment,
                                 SCHAR                *pRemainingBitsInSegment,
                                 int                  *pNumDecodedBits
                                );

static int DecodePCW_Sign(HANDLE_FDK_BITSTREAM  bs,
                          UINT                  codebookDim,
                          const SCHAR          *pQuantVal,
                          FIXP_DBL             *pQuantSpecCoef,
                          int                  *quantSpecCoefIdx,
                          USHORT               *pLeftStartOfSegment,
                          SCHAR                *pRemainingBitsInSegment,
                          int                  *pNumDecodedBits
                         );

static const SCHAR *DecodePCW_Body(HANDLE_FDK_BITSTREAM  bs,
                                   const UINT           *pCurrentTree,
                                   const SCHAR          *pQuantValBase,
                                         USHORT         *pLeftStartOfSegment,
                                         SCHAR          *pRemainingBitsInSegment,
                                         int            *pNumDecodedBits
                                  );

static void DecodePCWs(HANDLE_FDK_BITSTREAM bs, H_HCR_INFO pHcr);

static void HcrReorderQuantizedSpectralCoefficients(
        H_HCR_INFO pHcr,
        CAacDecoderChannelInfo *pAacDecoderChannelInfo,
        const SamplingRateInfo *pSamplingRateInfo
        );


#if CHECK_SEGMENTATION_IMMEDIATELY
static UCHAR errDetectPcwSegmentation(SCHAR       remainingBitsInSegment,
                                      H_HCR_INFO  pHcr,
                                      PCW_TYPE    kind,
                                      FIXP_DBL   *qsc_base_of_cw,
                                      UCHAR       dimension);
#endif

#if CHECK_SEGMENTATION_FINAL
static void errDetectWithinSegmentationFinal(H_HCR_INFO pHcr);
#endif

/*---------------------------------------------------------------------------------------------
     description:   Check if codebook and numSect are within allowed range (short only)
-------------------------------------------------------------------------------------------- */
static void errDetectorInHcrSideinfoShrt(SCHAR cb, SHORT numLine,UINT* errorWord)
{



  if ( cb < ZERO_HCB || cb >= MAX_CB_CHECK || cb == BOOKSCL ) {
    *errorWord |= CB_OUT_OF_RANGE_SHORT_BLOCK;
  }
  if ( numLine < 0 || numLine > 1024 ) {
    *errorWord |= LINE_IN_SECT_OUT_OF_RANGE_SHORT_BLOCK;
  }
}

/*---------------------------------------------------------------------------------------------
     description:   Check both HCR lengths
-------------------------------------------------------------------------------------------- */
static void errDetectorInHcrLengths(SCHAR  lengthOfLongestCodeword,
                                    SHORT  lengthOfReorderedSpectralData,
                                    UINT  *errorWord)
{
  if ( lengthOfReorderedSpectralData < lengthOfLongestCodeword ) {
    *errorWord |= HCR_SI_LENGTHS_FAILURE;
  }
}

/*---------------------------------------------------------------------------------------------
     description:   Decode (and adapt if necessary) the two HCR sideinfo components:
                    'reordered_spectral_data_length' and 'longest_codeword_length'
-------------------------------------------------------------------------------------------- */

void CHcr_Read(HANDLE_FDK_BITSTREAM    bs,
               CAacDecoderChannelInfo *pAacDecoderChannelInfo)
{
  INT    globalHcrType = getHcrType(&pAacDecoderChannelInfo->pComData->overlay.aac.erHcrInfo);
  SHORT  lengOfReorderedSpectralData;
  SCHAR  lengOfLongestCodeword;

  pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfReorderedSpectralData = 0;
  pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfLongestCodeword       = 0;



    /* ------- SI-Value No 1 ------- */
    lengOfReorderedSpectralData = FDKreadBits(bs,14) + ERROR_LORSD;
    if ( globalHcrType == ID_CPE ) {
      if ((lengOfReorderedSpectralData >= 0) && (lengOfReorderedSpectralData <= CPE_TOP_LENGTH)) {
        pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfReorderedSpectralData = lengOfReorderedSpectralData; /* the decoded value is within range */
      }
      else {
        if (lengOfReorderedSpectralData > CPE_TOP_LENGTH) {
          pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfReorderedSpectralData = CPE_TOP_LENGTH;            /* use valid maximum */
        }
      }
    }
    else if (globalHcrType == ID_SCE || globalHcrType == ID_LFE || globalHcrType == ID_CCE ) {
      if ((lengOfReorderedSpectralData >= 0) && (lengOfReorderedSpectralData <= SCE_TOP_LENGTH)) {
        pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfReorderedSpectralData = lengOfReorderedSpectralData; /* the decoded value is within range */
      }
      else {
        if (lengOfReorderedSpectralData > SCE_TOP_LENGTH) {
          pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfReorderedSpectralData = SCE_TOP_LENGTH;            /* use valid maximum */
        }
      }
    }

    /* ------- SI-Value No 2 ------- */
    lengOfLongestCodeword = FDKreadBits(bs,6) + ERROR_LOLC;
    if ((lengOfLongestCodeword >= 0) && (lengOfLongestCodeword <= LEN_OF_LONGEST_CW_TOP_LENGTH)) {
      pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfLongestCodeword = lengOfLongestCodeword;                /* the decoded value is within range */
    }
    else {
      if (lengOfLongestCodeword > LEN_OF_LONGEST_CW_TOP_LENGTH) {
        pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfLongestCodeword = LEN_OF_LONGEST_CW_TOP_LENGTH;       /* use valid maximum */
      }
    }
}


/*---------------------------------------------------------------------------------------------
     description:   Sets up HCR ROM-Tables
-------------------------------------------------------------------------------------------- */

void HcrInitRom(H_HCR_INFO pHcr)
{
  pHcr->cbPairs.pMinOfCbPair     = aMinOfCbPair;
  pHcr->cbPairs.pMaxOfCbPair     = aMaxOfCbPair;

  pHcr->tableInfo.pMaxCwLength   = aMaxCwLen;
  pHcr->tableInfo.pCbDimension   = aDimCb;
  pHcr->tableInfo.pCbDimShift    = aDimCbShift;
  pHcr->tableInfo.pCbSign        = aSignCb;
  pHcr->tableInfo.pCbPriority    = aCbPriority;
  pHcr->tableInfo.pLargestAbsVal = aLargestAbsoluteValue;
}

/*---------------------------------------------------------------------------------------------
     description:   Set up HCR - must be called before every call to HcrDecoder().
                    For short block a sorting algorithm is applied to get the SI in the order
                    that HCR could assemble the qsc's as if it is a long block.
-----------------------------------------------------------------------------------------------
        return:     error log
-------------------------------------------------------------------------------------------- */

UINT HcrInit(H_HCR_INFO              pHcr,
             CAacDecoderChannelInfo *pAacDecoderChannelInfo,
             const SamplingRateInfo *pSamplingRateInfo,
             HANDLE_FDK_BITSTREAM    bs)
{
  CIcsInfo *pIcsInfo = &pAacDecoderChannelInfo->icsInfo;
  SHORT *pNumLinesInSec;
  UCHAR *pCodeBk;
  SHORT  numSection;
  SCHAR  cb;
  int  numLine;
  int  i;

  pHcr->decInOut.lengthOfReorderedSpectralData      = pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfReorderedSpectralData;
  pHcr->decInOut.lengthOfLongestCodeword            = pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfLongestCodeword;
  pHcr->decInOut.pQuantizedSpectralCoefficientsBase = pAacDecoderChannelInfo->pSpectralCoefficient;
  pHcr->decInOut.quantizedSpectralCoefficientsIdx   = 0;
  pHcr->decInOut.pCodebook                          = pAacDecoderChannelInfo->pDynData->specificTo.aac.aCodeBooks4Hcr;
  pHcr->decInOut.pNumLineInSect                     = pAacDecoderChannelInfo->pDynData->specificTo.aac.aNumLineInSec4Hcr;
  pHcr->decInOut.numSection                         = pAacDecoderChannelInfo->pDynData->specificTo.aac.numberSection;
  pHcr->decInOut.errorLog = 0;
  pHcr->nonPcwSideinfo.pResultBase                  = SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient);

  FDKsyncCache(bs);
  pHcr->decInOut.bitstreamIndex                     = FDKgetBitCnt(bs);

  if (!IsLongBlock(&pAacDecoderChannelInfo->icsInfo))   /* short block */
  {
    SHORT  band;
    SHORT  maxBand;
    SCHAR  group;
    SCHAR  winGroupLen;
    SCHAR  window;
    SCHAR  numUnitInBand;
    SCHAR  cntUnitInBand;
    SCHAR  groupWin;
    SCHAR  cb_prev;

    UCHAR *pCodeBook;
    const SHORT *BandOffsets;
    SCHAR  numOfGroups;


    pCodeBook      = pAacDecoderChannelInfo->pDynData->aCodeBook;             /* in */
    pNumLinesInSec = pHcr->decInOut.pNumLineInSect;                               /* out */
    pCodeBk        = pHcr->decInOut.pCodebook;                                    /* out */
    BandOffsets    = GetScaleFactorBandOffsets(pIcsInfo, pSamplingRateInfo); /* aux */
    numOfGroups    = GetWindowGroups(pIcsInfo);

    numLine = 0;
    numSection = 0;
    cb      = pCodeBook[0];
    cb_prev = pCodeBook[0];

    /* convert HCR-sideinfo into a unitwise manner: When the cb changes, a new section starts */

    *pCodeBk++ = cb_prev;

    maxBand = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
    for (band = 0; band < maxBand; band++) {  /* from low to high sfbs i.e. from low to high frequencies  */
      numUnitInBand = ((BandOffsets[band+1] - BandOffsets[band]) >> FOUR_LOG_DIV_TWO_LOG); /* get the number of units in current sfb */
      for (cntUnitInBand = numUnitInBand;  cntUnitInBand != 0; cntUnitInBand-- ) { /* for every unit in the band */
        for (window = 0, group = 0; group < numOfGroups; group++) {
          winGroupLen = GetWindowGroupLength(&pAacDecoderChannelInfo->icsInfo,group);
          for (groupWin = winGroupLen; groupWin != 0; groupWin--, window++) {
            cb = pCodeBook[group * 16 + band];
            if (cb != cb_prev) {
#if CHECK_VALID_HCR_INPUT /* short-block  1 of 2 */
              errDetectorInHcrSideinfoShrt(cb,numLine,&pHcr->decInOut.errorLog );
              if (pHcr->decInOut.errorLog != 0 ) {
                return ( pHcr->decInOut.errorLog );
              }
#endif
              *pCodeBk++ = cb;
              *pNumLinesInSec++ = numLine;
              numSection++;

              cb_prev = cb;
              numLine = LINES_PER_UNIT;
            }
            else {
              numLine += LINES_PER_UNIT;
            }
          }
        }
      }
    }

    numSection++;

#if CHECK_VALID_HCR_INPUT /* short-block  2 of 2 */
    errDetectorInHcrSideinfoShrt(cb,numLine,&pHcr->decInOut.errorLog );
    if ( numSection <= 0 || numSection > 1024/2 ) {
      pHcr->decInOut.errorLog |= NUM_SECT_OUT_OF_RANGE_SHORT_BLOCK;
    }
    errDetectorInHcrLengths(pHcr->decInOut.lengthOfLongestCodeword,
                            pHcr->decInOut.lengthOfReorderedSpectralData,
                           &pHcr->decInOut.errorLog);
    if (pHcr->decInOut.errorLog != 0 ) {
      return ( pHcr->decInOut.errorLog );
    }
#endif

    *pCodeBk = cb;
    *pNumLinesInSec = numLine;
    pHcr->decInOut.numSection = numSection;

  } else /* end short block prepare SI */
  {      /* long block */
#if CHECK_VALID_HCR_INPUT /* long-block  1 of 1 */
    errDetectorInHcrLengths(pHcr->decInOut.lengthOfLongestCodeword,
                            pHcr->decInOut.lengthOfReorderedSpectralData,
                           &pHcr->decInOut.errorLog);
    numSection     = pHcr->decInOut.numSection;
    pNumLinesInSec = pHcr->decInOut.pNumLineInSect;
    pCodeBk        = pHcr->decInOut.pCodebook;
    if ( numSection <= 0 || numSection > 64 ) {
      pHcr->decInOut.errorLog |= NUM_SECT_OUT_OF_RANGE_LONG_BLOCK;
      numSection = 0;
    }

    for ( i = numSection; i != 0; i-- )
    {
      cb = *pCodeBk++;

      if ( cb < ZERO_HCB || cb >= MAX_CB_CHECK || cb == BOOKSCL ) {
        pHcr->decInOut.errorLog |= CB_OUT_OF_RANGE_LONG_BLOCK;
      }

      numLine = *pNumLinesInSec++;
      /* FDK_ASSERT(numLine > 0); */

      if ( (numLine <= 0) || (numLine > 1024) ) {
        pHcr->decInOut.errorLog |= LINE_IN_SECT_OUT_OF_RANGE_LONG_BLOCK;
      }
    }
    if (pHcr->decInOut.errorLog != 0 ) {
      return ( pHcr->decInOut.errorLog );
    }
#endif  /* CHECK_VALID_HCR_INPUT */
  }

  pCodeBk = pHcr->decInOut.pCodebook;
  for ( i = 0; i < numSection; i++ ) {
    if (
         (*pCodeBk == NOISE_HCB)      ||
         (*pCodeBk == INTENSITY_HCB2) ||
         (*pCodeBk == INTENSITY_HCB))
    {
      *pCodeBk = 0;
    }
    pCodeBk++;
  }

  /* HCR-sideinfo-input is complete and seems to be valid */



  return ( pHcr->decInOut.errorLog );
}




#if USE_HCR_DUMMY

/*---------------------------------------------------------------------------------------------

     description:   This HCR - dummy  - function writes only a dirac-sequence in output buffer

-------------------------------------------------------------------------------------------- */
UINT HcrDecoder(H_HCR_INFO              pHcr,
                const CAacDecoderChannelInfo *pAacDecoderChannelInfo,
                HANDLE_FDK_BITSTREAM    bs)
{
  for (SHORT i=0; i < 1024; i++ ) {
    pHcr->decInOut.pQuantizedSpectralCoefficients->Long[i] = FL2FXCONST_DBL(0.0f);
    if ( i % 30 == 0) {
      pHcr->decInOut.pQuantizedSpectralCoefficients->Long[i] = (FIXP_DBL)HCR_DIRAC;
    }
  }
  return 0;
}

#else /* USE_HCR_DUMMY */

/*---------------------------------------------------------------------------------------------
     description:   This function decodes the codewords of the spectral coefficients from the
                    bitstream according to the HCR algorithm and stores the quantized spectral
                    coefficients in correct order in the output buffer.
-------------------------------------------------------------------------------------------- */

UINT HcrDecoder(H_HCR_INFO              pHcr,
                CAacDecoderChannelInfo *pAacDecoderChannelInfo,
                const SamplingRateInfo       *pSamplingRateInfo,
                HANDLE_FDK_BITSTREAM    bs)
{
  int pTmp1, pTmp2, pTmp3, pTmp4;
#if DETECT_TOO_LONG_CW_READS
  int        pTmp5;
#endif

  INT        bitCntOffst;
  UINT       saveBitCnt = FDKgetBitCnt(bs);   /* save bitstream position */

  HcrCalcNumCodeword(pHcr);

  HcrSortCodebookAndNumCodewordInSection(pHcr);

  HcrPrepareSegmentationGrid(pHcr);

  HcrExtendedSectionInfo(pHcr);

  if (( pHcr->decInOut.errorLog & HCR_FATAL_PCW_ERROR_MASK ) != 0 ) {
    return ( pHcr->decInOut.errorLog );  /* sideinfo is massively corrupt, return from HCR without having decoded anything */
  }

  DeriveNumberOfExtendedSortedSectionsInSets(pHcr->segmentInfo.numSegment,
                                             pHcr->sectionInfo.pNumExtendedSortedCodewordInSection,
                                             pHcr->sectionInfo.numExtendedSortedCodewordInSectionIdx,
                                             pHcr->sectionInfo.pNumExtendedSortedSectionsInSets,
                                             pHcr->sectionInfo.numExtendedSortedSectionsInSetsIdx);

  /* store */
  pTmp1 = pHcr->sectionInfo.numExtendedSortedCodewordInSectionIdx;
  pTmp2 = pHcr->sectionInfo.extendedSortedCodebookIdx;
  pTmp3 = pHcr->sectionInfo.numExtendedSortedSectionsInSetsIdx;
  pTmp4 = pHcr->decInOut.quantizedSpectralCoefficientsIdx;
#if DETECT_TOO_LONG_CW_READS
  pTmp5 = pHcr->sectionInfo.maxLenOfCbInExtSrtSecIdx;
#endif

  /* ------- decode meaningful PCWs ------ */
  DecodePCWs(bs, pHcr);

  if (( pHcr->decInOut.errorLog & HCR_FATAL_PCW_ERROR_MASK ) == 0 ) {
    /* ------ decode the non-PCWs -------- */
    DecodeNonPCWs(bs, pHcr);
  }


#if CHECK_SEGMENTATION_FINAL
  errDetectWithinSegmentationFinal(pHcr);
#endif

  /* restore */
  pHcr->sectionInfo.numExtendedSortedCodewordInSectionIdx = pTmp1;
  pHcr->sectionInfo.extendedSortedCodebookIdx             = pTmp2;
  pHcr->sectionInfo.numExtendedSortedSectionsInSetsIdx    = pTmp3;
  pHcr->decInOut.quantizedSpectralCoefficientsIdx         = pTmp4;
#if DETECT_TOO_LONG_CW_READS
  pHcr->sectionInfo.maxLenOfCbInExtSrtSecIdx              = pTmp5;
#endif

  HcrReorderQuantizedSpectralCoefficients(pHcr, pAacDecoderChannelInfo, pSamplingRateInfo);

  /* restore bitstream position */
  bitCntOffst = saveBitCnt - FDKgetBitCnt(bs);
  if( bitCntOffst ) {
    FDKpushBiDirectional(bs, bitCntOffst);
  }

  return ( pHcr->decInOut.errorLog );
}


#endif  /* USE_HCR_DUMMY */




/*---------------------------------------------------------------------------------------------
     description:   This function reorders the quantized spectral coefficients sectionwise for
                    long- and short-blocks and compares to the LAV (Largest Absolute Value of
                    the current codebook) -- a counter is incremented if there is an error
                    detected.
                    Additional for short-blocks a unit-based-deinterleaving is applied.
                    Moreover (for short blocks) the scaling is derived (compare plain huffman
                    decoder).
-------------------------------------------------------------------------------------------- */

static void HcrReorderQuantizedSpectralCoefficients(
        H_HCR_INFO pHcr, CAacDecoderChannelInfo *pAacDecoderChannelInfo,
        const SamplingRateInfo *pSamplingRateInfo
        )
{
  INT       qsc;
  UINT      abs_qsc;
  UINT      i,j;
  USHORT    numSpectralValuesInSection;
  FIXP_DBL *pTeVa;
  USHORT    lavErrorCnt = 0;

  UINT      numSection                         = pHcr->decInOut.numSection;
  SPECTRAL_PTR  pQuantizedSpectralCoefficientsBase = pHcr->decInOut.pQuantizedSpectralCoefficientsBase;
  FIXP_DBL *pQuantizedSpectralCoefficients     = SPEC_LONG(pHcr->decInOut.pQuantizedSpectralCoefficientsBase);
  const UCHAR    *pCbDimShift                  = pHcr->tableInfo.pCbDimShift;
  const USHORT   *pLargestAbsVal               = pHcr->tableInfo.pLargestAbsVal;
  UCHAR    *pSortedCodebook                    = pHcr->sectionInfo.pSortedCodebook;
  USHORT   *pNumSortedCodewordInSection        = pHcr->sectionInfo.pNumSortedCodewordInSection;
  USHORT   *pReorderOffset                     = pHcr->sectionInfo.pReorderOffset;
  FIXP_DBL *pTempValues                        = pHcr->segmentInfo.pTempValues;
  FIXP_DBL *pBak                               = pHcr->segmentInfo.pTempValues;

  FDKmemclear(pTempValues,1024*sizeof(FIXP_DBL));

  /* long and short: check if decoded huffman-values (quantized spectral coefficients) are within range */
  for ( i=numSection; i != 0; i-- ) {
    numSpectralValuesInSection = *pNumSortedCodewordInSection++ << pCbDimShift[*pSortedCodebook];
    pTeVa = &pTempValues[*pReorderOffset++];
    for( j = numSpectralValuesInSection; j != 0; j-- ) {
      qsc = *pQuantizedSpectralCoefficients++;
      abs_qsc = FDKabs(qsc);
#if VALID_LAV_ERROR_TRIGGER
      if ( abs_qsc <= pLargestAbsVal[*pSortedCodebook] ) {
        *pTeVa++ = (FIXP_DBL)qsc;  /* the qsc value is within range */
      }
      else { /* line is too high .. */
        if ( abs_qsc == Q_VALUE_INVALID ) { /* .. because of previous marking --> dont set LAV flag (would be confusing), just copy out the already marked value */
          *pTeVa++ = (FIXP_DBL) qsc;
        }
        else {                              /* .. because a too high value was decoded for this cb --> set LAV flag */
          *pTeVa++ = (FIXP_DBL) Q_VALUE_INVALID;
          lavErrorCnt += 1;
        }
      }
#else
      if ( abs_qsc <= pLargestAbsVal[*pSortedCodebook] ) {
        *pTeVa++ = qsc;
      }
      else {
        *pTeVa++ = Q_VALUE_INVALID;
        lavErrorCnt += 1;
      }
#endif
    }
    pSortedCodebook++;
  }

  if (!IsLongBlock(&pAacDecoderChannelInfo->icsInfo))
  {
    FIXP_DBL *pOut;
    FIXP_DBL  locMax;
    FIXP_DBL  tmp;
    SCHAR     groupoffset;
    SCHAR     group;
    SCHAR     band;
    SCHAR     groupwin;
    SCHAR     window;
    SCHAR     numWinGroup;
    SHORT     interm;
    SCHAR     numSfbTransm;
    SCHAR     winGroupLen;
    SHORT     index;
    INT       msb;
    INT       lsb;

    SHORT   *pScaleFacHcr = pAacDecoderChannelInfo->pDynData->aScaleFactor;
    SHORT   *pSfbSclHcr   = pAacDecoderChannelInfo->pDynData->aSfbScale;
    const SHORT *BandOffsets = GetScaleFactorBandOffsets(&pAacDecoderChannelInfo->icsInfo, pSamplingRateInfo);

    pBak = pHcr->segmentInfo.pTempValues;
    /* deinterleave unitwise for short blocks */
    for ( window = 0; window < (8); window++ ) {
      pOut = SPEC(pQuantizedSpectralCoefficientsBase, window, pAacDecoderChannelInfo->granuleLength);
      for ( i=0; i < (LINES_PER_UNIT_GROUP); i++ ) {
        pTeVa = pBak + (window << FOUR_LOG_DIV_TWO_LOG) + i * 32;	/* distance of lines between unit groups has to be constant for every framelength (32)!  */
        for ( j=(LINES_PER_UNIT); j != 0; j-- ) {
          *pOut++ = *pTeVa++;
        }
      }
    }

    /* short blocks only */
    /* derive global scaling-value for every sfb and every window (as it is done in plain-huffman-decoder at short blocks) */
    groupoffset = 0;

    numWinGroup = GetWindowGroups(&pAacDecoderChannelInfo->icsInfo);
    numSfbTransm = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);

    for (group = 0; group < numWinGroup; group++) {
      winGroupLen = GetWindowGroupLength(&pAacDecoderChannelInfo->icsInfo,group);
      for (band = 0; band < numSfbTransm; band++) {
        interm = group * 16 + band;
        msb = pScaleFacHcr[interm] >> 2;
        lsb = pScaleFacHcr[interm] & 3;
        for (groupwin = 0; groupwin < winGroupLen; groupwin++) {
          window = groupoffset + groupwin;
          pBak = SPEC(pQuantizedSpectralCoefficientsBase, window, pAacDecoderChannelInfo->granuleLength);
          locMax = FL2FXCONST_DBL(0.0f);
          for (index = BandOffsets[band]; index < BandOffsets[band+1]; index += LINES_PER_UNIT) {
            pTeVa = &pBak[index];
            for ( i = LINES_PER_UNIT; i != 0; i --) {
              tmp = (*pTeVa < FL2FXCONST_DBL(0.0f))? -*pTeVa++ : *pTeVa++;
              locMax = fixMax(tmp,locMax);
            }
          }
          if ( fixp_abs(locMax) > (FIXP_DBL)MAX_QUANTIZED_VALUE ) {
            locMax = (FIXP_DBL)MAX_QUANTIZED_VALUE;
          }
          pSfbSclHcr[window*16+band] = msb - GetScaleFromValue(locMax, lsb);  /* save global scale maxima in this sfb */
        }
      }
      groupoffset += GetWindowGroupLength(&pAacDecoderChannelInfo->icsInfo,group);
    }
  } else
  {
    /* copy straight for long-blocks */
    pQuantizedSpectralCoefficients = SPEC_LONG(pQuantizedSpectralCoefficientsBase);
    for ( i = 1024; i != 0; i-- ) {
      *pQuantizedSpectralCoefficients++ = *pBak++;
    }
  }

  if ( lavErrorCnt != 0 ) {
    pHcr->decInOut.errorLog |= LAV_VIOLATION;
  }
}


/*---------------------------------------------------------------------------------------------
     description:   This function calculates the number of codewords
                    for each section (numCodewordInSection) and the number of codewords
                    for all sections (numCodeword).
                    For zero and intensity codebooks a entry is also done in the variable
                    numCodewordInSection. It is assumed that the codebook is a two tuples
                    codebook. This is needed later for the calculation of the base addresses
                    for the reordering of the quantize spectral coefficients at the end of the
                    hcr tool.
                    The variable numCodeword contain the number of codewords which are really
                    in the bitstream. Zero or intensity codebooks does not increase the
                    variable numCodewords.
-----------------------------------------------------------------------------------------------
        return:   -
-------------------------------------------------------------------------------------------- */

static void HcrCalcNumCodeword(H_HCR_INFO pHcr)
{
  int     hcrSection;
  UINT    numCodeword;

  UINT    numSection            =   pHcr->decInOut.numSection;
  UCHAR  *pCodebook             =   pHcr->decInOut.pCodebook;
  SHORT  *pNumLineInSection     =   pHcr->decInOut.pNumLineInSect;
  const UCHAR  *pCbDimShift     =   pHcr->tableInfo.pCbDimShift;
  USHORT *pNumCodewordInSection =   pHcr->sectionInfo.pNumCodewordInSection;

  numCodeword = 0;
  for ( hcrSection = numSection; hcrSection != 0; hcrSection-- ) {
    *pNumCodewordInSection = *pNumLineInSection++ >> pCbDimShift[*pCodebook];
    if ( *pCodebook != 0 ) {
      numCodeword += *pNumCodewordInSection;
    }
    pNumCodewordInSection++;
    pCodebook++;
  }
  pHcr->sectionInfo.numCodeword = numCodeword;
}


/*---------------------------------------------------------------------------------------------
     description:   This function calculates the number
                    of sorted codebooks and sorts the codebooks and the numCodewordInSection
                    according to the priority.
-------------------------------------------------------------------------------------------- */

static void HcrSortCodebookAndNumCodewordInSection(H_HCR_INFO pHcr)
{

  UINT    i,j,k;
  UCHAR   temp;
  UINT    counter;
  UINT    startOffset;
  UINT    numZeroSection;
  UCHAR  *pDest;
  UINT    numSectionDec;

  UINT    numSection                  =   pHcr->decInOut.numSection;
  UCHAR  *pCodebook                   =   pHcr->decInOut.pCodebook;
  UCHAR  *pSortedCodebook             =   pHcr->sectionInfo.pSortedCodebook;
  USHORT *pNumCodewordInSection       =   pHcr->sectionInfo.pNumCodewordInSection;
  USHORT *pNumSortedCodewordInSection =   pHcr->sectionInfo.pNumSortedCodewordInSection;
  UCHAR  *pCodebookSwitch             =   pHcr->sectionInfo.pCodebookSwitch;
  USHORT *pReorderOffset              =   pHcr->sectionInfo.pReorderOffset;
  const UCHAR   *pCbPriority          =   pHcr->tableInfo.pCbPriority;
  const UCHAR   *pMinOfCbPair         =   pHcr->cbPairs.pMinOfCbPair;
  const UCHAR   *pMaxOfCbPair         =   pHcr->cbPairs.pMaxOfCbPair;
  const UCHAR   *pCbDimShift          =   pHcr->tableInfo.pCbDimShift;

  UINT searchStart = 0;

  /* calculate *pNumSortedSection and store the priorities in array pSortedCdebook */
  pDest = pSortedCodebook;
  numZeroSection = 0;
  for ( i=numSection; i != 0; i-- ) {
    if ( pCbPriority[*pCodebook] == 0 ) {
      numZeroSection += 1;
    }
    *pDest++ = pCbPriority[*pCodebook++];
  }
  pHcr->sectionInfo.numSortedSection = numSection - numZeroSection;    /* numSortedSection contains no zero or intensity section */
  pCodebook = pHcr->decInOut.pCodebook;

  /* sort priorities of the codebooks in array pSortedCdebook[] */
  numSectionDec = numSection - 1;
  if ( numSectionDec > 0 ) {
    counter = numSectionDec;
    for ( j=numSectionDec; j != 0; j-- ) {
      for ( i=0; i < counter; i++ ) {
        /* swap priorities */
        if ( pSortedCodebook[i+1] > pSortedCodebook[i] ) {
          temp                 = pSortedCodebook[i];
          pSortedCodebook[i]   = pSortedCodebook[i+1];
          pSortedCodebook[i+1] = temp;
        }
      }
      counter -= 1;
    }
  }

  /* clear codebookSwitch array */
  for ( i = numSection; i != 0; i--) {
    *pCodebookSwitch++ = 0;
  }
  pCodebookSwitch = pHcr->sectionInfo.pCodebookSwitch;

  /* sort sectionCodebooks and numCodwordsInSection and calculate pReorderOffst[j] */
  for ( j = 0; j < numSection; j++ ) {
    for ( i = searchStart; i < numSection; i++ ) {
      if ( pCodebookSwitch[i] == 0 && ( pMinOfCbPair[pSortedCodebook[j]] == pCodebook[i]  ||  pMaxOfCbPair[pSortedCodebook[j]] == pCodebook[i] )) {
        pCodebookSwitch[i] = 1;
        pSortedCodebook[j] = pCodebook[i];                              /* sort codebook */
        pNumSortedCodewordInSection[j] = pNumCodewordInSection[i];      /* sort NumCodewordInSection */

        startOffset = 0;
        for ( k = 0; k < i; k++ ) {                                     /* make entry in pReorderOffst */
          startOffset += pNumCodewordInSection[k] << pCbDimShift[pCodebook[k]];
        }
        pReorderOffset[j] = startOffset;                                /* offset for reordering the codewords */

        if(i == searchStart) {
          UINT k = i;
          while(pCodebookSwitch[k++] == 1) searchStart++;
        }
        break;
      }
    }
  }
}


/*---------------------------------------------------------------------------------------------
     description:   This function calculates the segmentation, which includes numSegment,
                    leftStartOfSegment, rightStartOfSegment and remainingBitsInSegment.
                    The segmentation could be visualized a as kind of 'overlay-grid' for the
                    bitstream-block holding the HCR-encoded quantized-spectral-coefficients.
-------------------------------------------------------------------------------------------- */

static void HcrPrepareSegmentationGrid(H_HCR_INFO pHcr)
{
  USHORT   i,j;
  USHORT   numSegment = 0;
  USHORT   segmentStart = 0;
  UCHAR    segmentWidth;
  UCHAR    lastSegmentWidth;
  UCHAR    sortedCodebook;
  UCHAR    endFlag = 0;
  USHORT   intermediateResult;

  SCHAR    lengthOfLongestCodeword       =   pHcr->decInOut.lengthOfLongestCodeword;
  SHORT    lengthOfReorderedSpectralData =   pHcr->decInOut.lengthOfReorderedSpectralData;
  UINT     numSortedSection              =   pHcr->sectionInfo.numSortedSection;
  UCHAR   *pSortedCodebook               =   pHcr->sectionInfo.pSortedCodebook;
  USHORT  *pNumSortedCodewordInSection   =   pHcr->sectionInfo.pNumSortedCodewordInSection;
  USHORT  *pLeftStartOfSegment           =   pHcr->segmentInfo.pLeftStartOfSegment;
  USHORT  *pRightStartOfSegment          =   pHcr->segmentInfo.pRightStartOfSegment;
  SCHAR   *pRemainingBitsInSegment       =   pHcr->segmentInfo.pRemainingBitsInSegment;
  USHORT   bitstreamIndex                =   pHcr->decInOut.bitstreamIndex;
  const UCHAR *pMaxCwLength              =   pHcr->tableInfo.pMaxCwLength;

  for ( i=numSortedSection; i != 0; i-- ) {
    sortedCodebook = *pSortedCodebook++;
    segmentWidth = FDKmin(pMaxCwLength[sortedCodebook],lengthOfLongestCodeword);

    for ( j = *pNumSortedCodewordInSection; j != 0 ; j-- ) {
      /* width allows a new segment */
      intermediateResult = bitstreamIndex + segmentStart;
      if ( (segmentStart + segmentWidth) <= lengthOfReorderedSpectralData ) {
        /* store segment start, segment length and increment the number of segments */
        *pLeftStartOfSegment++ = intermediateResult;
        *pRightStartOfSegment++ = intermediateResult + segmentWidth - 1;
        *pRemainingBitsInSegment++ = segmentWidth;
        segmentStart += segmentWidth;
        numSegment += 1;
      }
      /* width does not allow a new segment */
      else {
        /* correct the last segment length */
        pLeftStartOfSegment--;
        pRightStartOfSegment--;
        pRemainingBitsInSegment--;
        segmentStart = *pLeftStartOfSegment - bitstreamIndex;

        lastSegmentWidth = lengthOfReorderedSpectralData - segmentStart;
        *pRemainingBitsInSegment = lastSegmentWidth;
        *pRightStartOfSegment = bitstreamIndex + segmentStart + lastSegmentWidth - 1;
        endFlag = 1;
        break;
      }
    }
    pNumSortedCodewordInSection++;
    if (endFlag != 0) {
      break;
    }
  }
  pHcr->segmentInfo.numSegment = numSegment;

}


/*---------------------------------------------------------------------------------------------
     description:   This function adapts the sorted section boundaries to the boundaries of
                    segmentation. If the section lengths does not fit completely into the
                    current segment, the section is spitted into two so called 'extended
                    sections'. The extended-section-info (pNumExtendedSortedCodewordInSectin
                    and pExtendedSortedCodebook) is updated in this case.

-------------------------------------------------------------------------------------------- */

static void HcrExtendedSectionInfo(H_HCR_INFO pHcr)
{
  UINT    srtSecCnt = 0;         /* counter for sorted sections */
  UINT    xSrtScCnt = 0;         /* counter for extended sorted sections */
  UINT    remainNumCwInSortSec;
  UINT    inSegmentRemainNumCW;

  UINT    numSortedSection            = pHcr->sectionInfo.numSortedSection;
  UCHAR  *pSortedCodebook             = pHcr->sectionInfo.pSortedCodebook;
  USHORT *pNumSortedCodewordInSection = pHcr->sectionInfo.pNumSortedCodewordInSection;
  UCHAR  *pExtendedSortedCoBo         = pHcr->sectionInfo.pExtendedSortedCodebook;
  USHORT *pNumExtSortCwInSect         = pHcr->sectionInfo.pNumExtendedSortedCodewordInSection;
  UINT    numSegment                  = pHcr->segmentInfo.numSegment;
#if DETECT_TOO_LONG_CW_READS
  UCHAR  *pMaxLenOfCbInExtSrtSec      = pHcr->sectionInfo.pMaxLenOfCbInExtSrtSec;
  SCHAR   lengthOfLongestCodeword     = pHcr->decInOut.lengthOfLongestCodeword;
  const UCHAR   *pMaxCwLength         = pHcr->tableInfo.pMaxCwLength;
#endif

  remainNumCwInSortSec = pNumSortedCodewordInSection[srtSecCnt];
  inSegmentRemainNumCW = numSegment;

  while (srtSecCnt < numSortedSection) {
    if (inSegmentRemainNumCW < remainNumCwInSortSec) {

      pNumExtSortCwInSect[xSrtScCnt] = inSegmentRemainNumCW;
      pExtendedSortedCoBo[xSrtScCnt] = pSortedCodebook[srtSecCnt];

      remainNumCwInSortSec -= inSegmentRemainNumCW;
      inSegmentRemainNumCW = numSegment;
      /* data of a sorted section was not integrated in extended sorted section */
    }
    else if (inSegmentRemainNumCW == remainNumCwInSortSec) {
      pNumExtSortCwInSect[xSrtScCnt] = inSegmentRemainNumCW;
      pExtendedSortedCoBo[xSrtScCnt] = pSortedCodebook[srtSecCnt];

      srtSecCnt++;
      remainNumCwInSortSec = pNumSortedCodewordInSection[srtSecCnt];
      inSegmentRemainNumCW = numSegment;
      /* data of a sorted section was integrated in extended sorted section */
    }
    else { /* inSegmentRemainNumCW > remainNumCwInSortSec */
      pNumExtSortCwInSect[xSrtScCnt] = remainNumCwInSortSec;
      pExtendedSortedCoBo[xSrtScCnt] = pSortedCodebook[srtSecCnt];


      inSegmentRemainNumCW -= remainNumCwInSortSec;
      srtSecCnt++;
      remainNumCwInSortSec = pNumSortedCodewordInSection[srtSecCnt];
      /* data of a sorted section was integrated in extended sorted section */
    }
#if DETECT_TOO_LONG_CW_READS
    pMaxLenOfCbInExtSrtSec[xSrtScCnt] = FDKmin(pMaxCwLength[pExtendedSortedCoBo[xSrtScCnt]],lengthOfLongestCodeword);
#endif



    xSrtScCnt += 1;

    if ( xSrtScCnt >= (MAX_SFB_HCR + MAX_HCR_SETS) ) {
      pHcr->decInOut.errorLog |= EXTENDED_SORTED_COUNTER_OVERFLOW;
      return;
    }

  }
  pNumExtSortCwInSect[xSrtScCnt] = 0;

}


/*---------------------------------------------------------------------------------------------
     description:   This function calculates the number of extended sorted sections which
                    belong to the sets. Each set from set 0 (one and only set for the PCWs)
                    till to the last set gets a entry in the array to which
                    'pNumExtendedSortedSectinsInSets' points to.

                    Calculation: The entrys in pNumExtendedSortedCodewordInSectin are added
                    untill the value numSegment is reached. Then the sum_variable is cleared
                    and the calculation starts from the beginning. As much extended sorted
                    Sections are summed up to reach the value numSegment, as much is the
                    current entry in *pNumExtendedSortedCodewordInSectin.
-------------------------------------------------------------------------------------------- */
static void DeriveNumberOfExtendedSortedSectionsInSets(UINT    numSegment,
                                                       USHORT *pNumExtendedSortedCodewordInSection,
                                                       int     numExtendedSortedCodewordInSectionIdx,
                                                       USHORT *pNumExtendedSortedSectionsInSets,
                                                       int     numExtendedSortedSectionsInSetsIdx)
{
  USHORT  counter = 0;
  UINT    cwSum   = 0;
  USHORT *pNumExSortCwInSec   = pNumExtendedSortedCodewordInSection;
  USHORT *pNumExSortSecInSets = pNumExtendedSortedSectionsInSets;

  while (pNumExSortCwInSec[numExtendedSortedCodewordInSectionIdx] != 0)
  {
    cwSum += pNumExSortCwInSec[numExtendedSortedCodewordInSectionIdx];
    numExtendedSortedCodewordInSectionIdx++;
    if (numExtendedSortedCodewordInSectionIdx >= (MAX_SFB_HCR+MAX_HCR_SETS)) {
      return;
    }
    if (cwSum > numSegment) {
      return;
    }
    counter++;
    if (counter > 1024/4) {
      return;
    }
    if ( cwSum == numSegment ) {
      pNumExSortSecInSets[numExtendedSortedSectionsInSetsIdx] = counter;
      numExtendedSortedSectionsInSetsIdx++;
      if (numExtendedSortedSectionsInSetsIdx >= MAX_HCR_SETS) {
        return;
      }
      counter = 0;
      cwSum = 0;
    }
  }
  pNumExSortSecInSets[numExtendedSortedSectionsInSetsIdx] = counter;   /* save last entry for the last - probably shorter - set */
}


/*---------------------------------------------------------------------------------------------
     description:   This function decodes all priority codewords (PCWs) in a spectrum (within
                    set 0). The calculation of the PCWs is managed in two loops. The
                    loopcounter of the outer loop is set to the first value pointer
                    pNumExtendedSortedSectionsInSets points to. This value represents the
                    number of extended sorted sections within set 0.
                    The loopcounter of the inner loop is set to the first value pointer
                    pNumExtendedSortedCodewordInSectin points to. The value represents the
                    number of extended sorted codewords in sections (the original sections have
                    been splitted to go along with the borders of the sets).
                    Each time the number of the extended sorted codewords in sections are de-
                    coded, the pointer 'pNumExtendedSortedCodewordInSectin' is incremented by
                    one.
-------------------------------------------------------------------------------------------- */
static void DecodePCWs(HANDLE_FDK_BITSTREAM bs, H_HCR_INFO pHcr)
{
  UINT         i;
  USHORT       extSortSec;
  USHORT       curExtSortCwInSec;
  UCHAR        codebook;
  UCHAR        dimension;
  const UINT  *pCurrentTree;
  const SCHAR *pQuantValBase;
  const SCHAR *pQuantVal;

  USHORT      *pNumExtendedSortedCodewordInSection = pHcr->sectionInfo.pNumExtendedSortedCodewordInSection;
  int          numExtendedSortedCodewordInSectionIdx = pHcr->sectionInfo.numExtendedSortedCodewordInSectionIdx;
  UCHAR       *pExtendedSortedCodebook             = pHcr->sectionInfo.pExtendedSortedCodebook;
  int          extendedSortedCodebookIdx           = pHcr->sectionInfo.extendedSortedCodebookIdx;
  USHORT      *pNumExtendedSortedSectionsInSets    = pHcr->sectionInfo.pNumExtendedSortedSectionsInSets;
  int          numExtendedSortedSectionsInSetsIdx  = pHcr->sectionInfo.numExtendedSortedSectionsInSetsIdx;
  FIXP_DBL    *pQuantizedSpectralCoefficients      = SPEC_LONG(pHcr->decInOut.pQuantizedSpectralCoefficientsBase);
  int          quantizedSpectralCoefficientsIdx    = pHcr->decInOut.quantizedSpectralCoefficientsIdx;
  USHORT      *pLeftStartOfSegment                 = pHcr->segmentInfo.pLeftStartOfSegment;
  SCHAR       *pRemainingBitsInSegment             = pHcr->segmentInfo.pRemainingBitsInSegment;
#if DETECT_TOO_LONG_CW_READS
  UCHAR       *pMaxLenOfCbInExtSrtSec              = pHcr->sectionInfo.pMaxLenOfCbInExtSrtSec;
  int          maxLenOfCbInExtSrtSecIdx            = pHcr->sectionInfo.maxLenOfCbInExtSrtSecIdx;
  UCHAR        maxAllowedCwLen;
  int          numDecodedBits;
#endif
  const UCHAR *pCbDimension =  pHcr->tableInfo.pCbDimension;
  const UCHAR *pCbSign      =  pHcr->tableInfo.pCbSign;

  /* clear result array */
  //pQSC = &pQuantizedSpectralCoefficients[quantizedSpectralCoefficientsIdx];
  //pQSC = *pQuantizedSpectralCoefficients;

  FDKmemclear(pQuantizedSpectralCoefficients+quantizedSpectralCoefficientsIdx,1024*sizeof(FIXP_DBL));

  /* decode all PCWs in the extended sorted section(s) belonging to set 0 */
  for ( extSortSec = pNumExtendedSortedSectionsInSets[numExtendedSortedSectionsInSetsIdx]; extSortSec != 0; extSortSec-- ) {

    codebook        =  pExtendedSortedCodebook[extendedSortedCodebookIdx];   /* get codebook for this extended sorted section and increment ptr to cb of next ext. sort sec */
    extendedSortedCodebookIdx++;
    if (extendedSortedCodebookIdx >= (MAX_SFB_HCR+MAX_HCR_SETS)) {
      return;
    }
    dimension       =  pCbDimension[codebook];         /* get dimension of codebook of this extended sort. sec. */
    pCurrentTree    =  aHuffTable  [codebook];         /* convert codebook to pointer to QSCs */
    pQuantValBase   =  aQuantTable [codebook];         /* convert codebook to index to table of QSCs */
#if DETECT_TOO_LONG_CW_READS
    maxAllowedCwLen = pMaxLenOfCbInExtSrtSec[maxLenOfCbInExtSrtSecIdx];
    maxLenOfCbInExtSrtSecIdx++;
    if (maxLenOfCbInExtSrtSecIdx >= (MAX_SFB_HCR+MAX_HCR_SETS)) {
      return;
    }
#endif

    /* switch for decoding with different codebooks: */
    if ( pCbSign[codebook] == 0 ) {                    /* no sign bits follow after the codeword-body */
      /* PCW_BodyONLY */
      /*==============*/

      for ( curExtSortCwInSec = pNumExtendedSortedCodewordInSection[numExtendedSortedCodewordInSectionIdx] ; curExtSortCwInSec != 0; curExtSortCwInSec--) {
        numDecodedBits = 0;

        /* decode PCW_BODY */
        pQuantVal = DecodePCW_Body(bs,
                                   pCurrentTree,
                                   pQuantValBase,
                                   pLeftStartOfSegment,
                                   pRemainingBitsInSegment,
                                  &numDecodedBits
                                  );

        /* result is written out here because NO sign bits follow the body */
        for( i=dimension; i != 0 ; i-- ) {
          pQuantizedSpectralCoefficients[quantizedSpectralCoefficientsIdx] = (FIXP_DBL) *pQuantVal++;                               /* write quant. spec. coef. into spectrum; sign is already valid */
          quantizedSpectralCoefficientsIdx++;
          if (quantizedSpectralCoefficientsIdx >= 1024) {
            return;
          }
        }

        /* one more PCW should be decoded */

#if DETECT_TOO_LONG_CW_READS
        if ( maxAllowedCwLen < (numDecodedBits + ERROR_PCW_BODY_ONLY_TOO_LONG) ) {
          pHcr->decInOut.errorLog |= TOO_MANY_PCW_BODY_BITS_DECODED;
        }
#endif

#if CHECK_SEGMENTATION_IMMEDIATELY
        if (1 == errDetectPcwSegmentation(*pRemainingBitsInSegment-ERROR_PCW_BODY,pHcr,PCW_BODY,pQuantizedSpectralCoefficients+quantizedSpectralCoefficientsIdx-dimension,dimension)) {
          return;
        }
#endif
        pLeftStartOfSegment++;                                             /* update pointer for decoding the next PCW */
        pRemainingBitsInSegment++;                                         /* update pointer for decoding the next PCW */
      }
    }
    else if (( pCbSign[codebook] == 1 ) && ( codebook < 11 )) {  /* possibly there follow 1,2,3 or 4 sign bits after the codeword-body */
      /* PCW_Body and PCW_Sign */
      /*=======================*/

      for ( curExtSortCwInSec = pNumExtendedSortedCodewordInSection[numExtendedSortedCodewordInSectionIdx] ; curExtSortCwInSec != 0; curExtSortCwInSec--)
      {
        int err;
        numDecodedBits  = 0;

        pQuantVal = DecodePCW_Body(bs,
                                   pCurrentTree,
                                   pQuantValBase,
                                   pLeftStartOfSegment,
                                   pRemainingBitsInSegment,
                                  &numDecodedBits
                                  );

        err = DecodePCW_Sign( bs,
                              dimension,
                              pQuantVal,
                              pQuantizedSpectralCoefficients,
                             &quantizedSpectralCoefficientsIdx,
                              pLeftStartOfSegment,
                              pRemainingBitsInSegment,
                             &numDecodedBits
                             );
        if (err != 0) {
          return;
        }
        /* one more PCW should be decoded */

#if DETECT_TOO_LONG_CW_READS
        if ( maxAllowedCwLen < (numDecodedBits + ERROR_PCW_BODY_SIGN_TOO_LONG) ) {
          pHcr->decInOut.errorLog |= TOO_MANY_PCW_BODY_SIGN_BITS_DECODED;
        }
#endif

#if CHECK_SEGMENTATION_IMMEDIATELY
        if (1 == errDetectPcwSegmentation(*pRemainingBitsInSegment-ERROR_PCW_BODY_SIGN,pHcr,PCW_BODY_SIGN, pQuantizedSpectralCoefficients+quantizedSpectralCoefficientsIdx-dimension,dimension)) {
          return;
        }
#endif
        pLeftStartOfSegment++;
        pRemainingBitsInSegment++;
      }
    }
    else if (( pCbSign[codebook] == 1 ) && ( codebook >= 11 )) { /* possibly there follow some sign bits and maybe one or two escape sequences after the cw-body */
      /* PCW_Body, PCW_Sign and maybe PCW_Escape */
      /*=========================================*/

      for ( curExtSortCwInSec = pNumExtendedSortedCodewordInSection[numExtendedSortedCodewordInSectionIdx] ; curExtSortCwInSec != 0; curExtSortCwInSec--)
      {
        int err;
        numDecodedBits  = 0;

        /* decode PCW_BODY */
        pQuantVal = DecodePCW_Body(bs,
                                   pCurrentTree,
                                   pQuantValBase,
                                   pLeftStartOfSegment,
                                   pRemainingBitsInSegment,
                                  &numDecodedBits
                                  );

        err = DecodePCW_Sign( bs,
                              dimension,
                              pQuantVal,
                              pQuantizedSpectralCoefficients,
                             &quantizedSpectralCoefficientsIdx,
                              pLeftStartOfSegment,
                              pRemainingBitsInSegment,
                             &numDecodedBits
                             );
        if (err != 0) {
          return;
        }

        /* decode PCW_ESCAPE if present */
        quantizedSpectralCoefficientsIdx -= DIMENSION_OF_ESCAPE_CODEBOOK;

        if ( fixp_abs(pQuantizedSpectralCoefficients[quantizedSpectralCoefficientsIdx]) == (FIXP_DBL)ESCAPE_VALUE ) {
          pQuantizedSpectralCoefficients[quantizedSpectralCoefficientsIdx] = (FIXP_DBL) DecodeEscapeSequence( bs,
                                                   pQuantizedSpectralCoefficients[quantizedSpectralCoefficientsIdx],
                                                   pLeftStartOfSegment,
                                                   pRemainingBitsInSegment,
                                                  &numDecodedBits
                                                 );
        }
        quantizedSpectralCoefficientsIdx++;
        if (quantizedSpectralCoefficientsIdx >= 1024) {
          return;
        }

        if ( fixp_abs(pQuantizedSpectralCoefficients[quantizedSpectralCoefficientsIdx]) == (FIXP_DBL)ESCAPE_VALUE ) {
          pQuantizedSpectralCoefficients[quantizedSpectralCoefficientsIdx] = (FIXP_DBL) DecodeEscapeSequence( bs,
                                                   pQuantizedSpectralCoefficients[quantizedSpectralCoefficientsIdx],
                                                   pLeftStartOfSegment,
                                                   pRemainingBitsInSegment,
                                                  &numDecodedBits
                                                 );
        }
        quantizedSpectralCoefficientsIdx++;
        if (quantizedSpectralCoefficientsIdx >= 1024) {
          return;
        }

        /* one more PCW should be decoded */

#if DETECT_TOO_LONG_CW_READS
        if ( maxAllowedCwLen < (numDecodedBits + ERROR_PCW_BODY_SIGN_ESC_TOO_LONG) ) {
          pHcr->decInOut.errorLog |= TOO_MANY_PCW_BODY_SIGN_ESC_BITS_DECODED;
        }
#endif

#if CHECK_SEGMENTATION_IMMEDIATELY
        if (1 == errDetectPcwSegmentation(*pRemainingBitsInSegment-ERROR_PCW_BODY_SIGN_ESC,pHcr,PCW_BODY_SIGN_ESC,pQuantizedSpectralCoefficients+quantizedSpectralCoefficientsIdx-DIMENSION_OF_ESCAPE_CODEBOOK,DIMENSION_OF_ESCAPE_CODEBOOK)) {
          return;
        }
#endif
        pLeftStartOfSegment++;
        pRemainingBitsInSegment++;
      }
    }

    /* all PCWs belonging to this extended section should be decoded */
    numExtendedSortedCodewordInSectionIdx++;
    if (numExtendedSortedCodewordInSectionIdx >= MAX_SFB_HCR+MAX_HCR_SETS) {
      return;
    }
  }
  /* all PCWs should be decoded */

  numExtendedSortedSectionsInSetsIdx++;
  if (numExtendedSortedSectionsInSetsIdx >= MAX_HCR_SETS) {
    return;
  }

  /* Write back indexes into structure */
  pHcr->sectionInfo.numExtendedSortedCodewordInSectionIdx = numExtendedSortedCodewordInSectionIdx;
  pHcr->sectionInfo.extendedSortedCodebookIdx = extendedSortedCodebookIdx;
  pHcr->sectionInfo.numExtendedSortedSectionsInSetsIdx = numExtendedSortedSectionsInSetsIdx;
  pHcr->decInOut.quantizedSpectralCoefficientsIdx = quantizedSpectralCoefficientsIdx;
  pHcr->sectionInfo.maxLenOfCbInExtSrtSecIdx = maxLenOfCbInExtSrtSecIdx;
}

#if CHECK_SEGMENTATION_IMMEDIATELY
/*---------------------------------------------------------------------------------------------
     description:   This function checks immediately after every decoded PCW, whether out of
                    the current segment too many bits have been read or not. If an error occurrs,
                    probably the sideinfo or the HCR-bitstream block holding the huffman
                    encoded quantized spectral coefficients is distorted. In this case the two
                    or four quantized spectral coefficients belonging to the current codeword
                    are marked (for being detected by concealment later).
-------------------------------------------------------------------------------------------- */
static UCHAR errDetectPcwSegmentation(SCHAR       remainingBitsInSegment,
                                      H_HCR_INFO  pHcr,
                                      PCW_TYPE    kind,
                                      FIXP_DBL   *qsc_base_of_cw,
                                      UCHAR       dimension)
{
  SCHAR i;
  if ( remainingBitsInSegment < 0 ) {
    /* log the error */
    switch (kind) {
    case PCW_BODY:
        pHcr->decInOut.errorLog |= SEGMENT_OVERRIDE_ERR_PCW_BODY;
        break;
    case PCW_BODY_SIGN:
        pHcr->decInOut.errorLog |= SEGMENT_OVERRIDE_ERR_PCW_BODY_SIGN;
        break;
    case PCW_BODY_SIGN_ESC:
        pHcr->decInOut.errorLog |= SEGMENT_OVERRIDE_ERR_PCW_BODY_SIGN_ESC;
        break;
    }
    /* mark the erred lines */
    for ( i = dimension; i != 0; i-- ) {
      *qsc_base_of_cw++ = (FIXP_DBL) Q_VALUE_INVALID;
    }
    return 1;
  }
  return 0;
}
#endif

#if CHECK_SEGMENTATION_FINAL
/*---------------------------------------------------------------------------------------------
     description:   This function checks if all segments are empty after decoding. There
                    are _no lines markded_ as invalid because it could not be traced back
                    where from the remaining bits are.
-------------------------------------------------------------------------------------------- */
static void errDetectWithinSegmentationFinal(H_HCR_INFO pHcr)
{
  UCHAR   segmentationErrorFlag   = 0;
  USHORT  i;
  SCHAR  *pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment;
  UINT    numSegment              = pHcr->segmentInfo.numSegment;

  for ( i=numSegment; i != 0 ; i--) {
    if (*pRemainingBitsInSegment++ != 0) {
      segmentationErrorFlag = 1;
    }
  }
  if (segmentationErrorFlag == 1) {
    pHcr->decInOut.errorLog |= BIT_IN_SEGMENTATION_ERROR;
  }
}
#endif

/*---------------------------------------------------------------------------------------------
     description:   This function walks one step within the decoding tree. Which branch is
                    taken depends on the decoded carryBit input parameter.
-------------------------------------------------------------------------------------------- */
void  CarryBitToBranchValue(UCHAR  carryBit,
                            UINT   treeNode,
                            UINT  *branchValue,
                            UINT  *branchNode)
{
  if (carryBit == 0) {
    *branchNode = (treeNode & MASK_LEFT) >> LEFT_OFFSET; /* MASK_LEFT:  00FFF000 */
  }
  else {
    *branchNode = treeNode & MASK_RIGHT;                 /* MASK_RIGHT: 00000FFF */
  }

  *branchValue = *branchNode & CLR_BIT_10;               /* clear bit 10 (if set) */
}


/*---------------------------------------------------------------------------------------------
     description:   Decodes the body of a priority codeword (PCW)
-----------------------------------------------------------------------------------------------
        return:   - return value is pointer to first of two or four quantized spectral
                    coefficients
-------------------------------------------------------------------------------------------- */
static const SCHAR *DecodePCW_Body(HANDLE_FDK_BITSTREAM  bs,
                                   const UINT           *pCurrentTree,
                                   const SCHAR          *pQuantValBase,
                                         USHORT         *pLeftStartOfSegment,
                                         SCHAR          *pRemainingBitsInSegment,
                                         int            *pNumDecodedBits
                                  )
{
  UCHAR carryBit;
  UINT  branchNode;
  UINT  treeNode;
  UINT  branchValue;
  const  SCHAR *pQuantVal;

  /* decode PCW_BODY */
  treeNode = *pCurrentTree;                                                 /* get first node of current tree belonging to current codebook */

  /* decode whole PCW-codeword-body */
  while (1) {

    carryBit = HcrGetABitFromBitstream(bs,
                                       pLeftStartOfSegment,
                                       pLeftStartOfSegment,                 /* dummy */
                                       FROM_LEFT_TO_RIGHT);
    *pRemainingBitsInSegment -= 1;
    *pNumDecodedBits += 1;

    CarryBitToBranchValue(carryBit,
                          treeNode,
                          &branchValue,
                          &branchNode);

    if ((branchNode & TEST_BIT_10) == TEST_BIT_10) {                        /* test bit 10 ; if set --> codeword-body is complete */
      break;                                                                /* end of branch in tree reached  i.e. a whole PCW-Body is decoded */
    }
    else {
      treeNode = *(pCurrentTree + branchValue);                             /* update treeNode for further step in decoding tree */
    }

  }

  pQuantVal = pQuantValBase + branchValue;                                  /* update pointer to valid first of 2 or 4 quantized values */

  return pQuantVal;
}


/*---------------------------------------------------------------------------------------------
     description:   This function decodes one escape sequence. In case of a escape codebook
                    and in case of the absolute value of the quantized spectral value == 16,
                    a escapeSequence is decoded in two steps:
                      1. escape prefix
                      2. escape word
-------------------------------------------------------------------------------------------- */

static INT  DecodeEscapeSequence(HANDLE_FDK_BITSTREAM  bs,
                                 INT                   quantSpecCoef,
                                 USHORT               *pLeftStartOfSegment,
                                 SCHAR                *pRemainingBitsInSegment,
                                 int                  *pNumDecodedBits
                                )
{
  UINT  i;
  INT   sign;
  UINT  escapeOnesCounter = 0;
  UINT  carryBit;
  INT   escape_word = 0;

  /* decode escape prefix */
  while (1) {
    carryBit = HcrGetABitFromBitstream(bs,
                                       pLeftStartOfSegment,
                                       pLeftStartOfSegment,                /* dummy */
                                       FROM_LEFT_TO_RIGHT);
    *pRemainingBitsInSegment -= 1;
    *pNumDecodedBits += 1;

    if (carryBit != 0) {
      escapeOnesCounter += 1;
    }
    else {
      escapeOnesCounter += 4;
      break;
    }
  }

  /* decode escape word */
  for( i=escapeOnesCounter; i != 0 ; i-- ) {
    carryBit = HcrGetABitFromBitstream(bs,
                                       pLeftStartOfSegment,
                                       pLeftStartOfSegment,                /* dummy */
                                       FROM_LEFT_TO_RIGHT);
    *pRemainingBitsInSegment -= 1;
    *pNumDecodedBits += 1;

    escape_word <<= 1;
    escape_word = escape_word | carryBit;
  }

  sign = (quantSpecCoef >= 0) ? 1 : -1;

  quantSpecCoef = sign * (((INT ) 1 << escapeOnesCounter) + escape_word);

  return quantSpecCoef;
}


/*---------------------------------------------------------------------------------------------
     description:   Decodes the Signbits of a priority codeword (PCW) and writes out the
                    resulting quantized spectral values into unsorted sections
-----------------------------------------------------------------------------------------------
        output:   - two or four lines at position in corresponding section (which are not
                    located at the desired position, i.e. they must be reordered in the last
                    of eight function of HCR)
-----------------------------------------------------------------------------------------------
        return:   - updated pQuantSpecCoef pointer (to next empty storage for a line)
-------------------------------------------------------------------------------------------- */
static int DecodePCW_Sign(HANDLE_FDK_BITSTREAM  bs,
                          UINT                  codebookDim,
                          const SCHAR          *pQuantVal,
                          FIXP_DBL             *pQuantSpecCoef,
                          int                  *quantSpecCoefIdx,
                          USHORT               *pLeftStartOfSegment,
                          SCHAR                *pRemainingBitsInSegment,
                          int                  *pNumDecodedBits
                         )
{
  UINT     i;
  UINT     carryBit;
  INT      quantSpecCoef;

  for( i=codebookDim; i != 0 ; i-- ) {
    quantSpecCoef = *pQuantVal++;
    if (quantSpecCoef != 0) {
      carryBit = HcrGetABitFromBitstream(bs,
                                         pLeftStartOfSegment,
                                         pLeftStartOfSegment,    /* dummy */
                                         FROM_LEFT_TO_RIGHT);
      *pRemainingBitsInSegment -= 1;
      *pNumDecodedBits += 1;
      if (*pRemainingBitsInSegment < 0 || *pNumDecodedBits >= (1024>>1)) {
        return -1;
      }

      /* adapt sign of values according to the decoded sign bit */
      if (carryBit != 0) {
        pQuantSpecCoef[*quantSpecCoefIdx] = -(FIXP_DBL)quantSpecCoef;
      }
      else {
        pQuantSpecCoef[*quantSpecCoefIdx] =  (FIXP_DBL)quantSpecCoef;
      }
    }
    else {
      pQuantSpecCoef[*quantSpecCoefIdx] = FL2FXCONST_DBL(0.0f);
    }
    *quantSpecCoefIdx += 1 ;
    if (*quantSpecCoefIdx >= 1024) {
      return -1;
    }
  }
  return 0;
}


/*---------------------------------------------------------------------------------------------
     description:   Mutes spectral lines which have been marked as erroneous (Q_VALUE_INVALID)
-------------------------------------------------------------------------------------------- */
void HcrMuteErroneousLines(H_HCR_INFO hHcr)
{
  int c;
  FIXP_DBL *RESTRICT pLong = SPEC_LONG(hHcr->decInOut.pQuantizedSpectralCoefficientsBase);

  /* if there is a line with value Q_VALUE_INVALID mute it */
  for (c = 0; c < 1024; c++) {
    if (pLong[c] == (FIXP_DBL)Q_VALUE_INVALID) {
#if HCR_LISTEN_TO_MUTED_LINES
      pLong[c] = (FIXP_DBL)HCR_DIRAC;   /* marking */
#else
      pLong[c] = FL2FXCONST_DBL(0.0f);  /* muting */
#endif
    }
  }
}


/*---------------------------------------------------------------------------------------------
     description:   Sets global HCR type
-------------------------------------------------------------------------------------------- */
void setHcrType(H_HCR_INFO hHcr, MP4_ELEMENT_ID type)
{
  switch (type) {
    case ID_SCE:
      hHcr->globalHcrType = 0;
      break;
    case ID_CPE:
      hHcr->globalHcrType = 1;
      break;
    default:
      break;
  }
}


/*---------------------------------------------------------------------------------------------
     description:   Gets HCR type from the HCR data structure
-----------------------------------------------------------------------------------------------
        return:   - global HCR type
-------------------------------------------------------------------------------------------- */
INT getHcrType(H_HCR_INFO hHcr)
{
  return hHcr->globalHcrType;
}