2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
5 © Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and the date of any change. For modified versions of the FDK AAC Codec, the term
50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
73 5. CONTACT INFORMATION
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
78 91058 Erlangen, Germany
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
84 /***************************** MPEG-4 AAC Decoder ***************************
86 Author(s): Robert Weidner (DSP Solutions)
87 Description: HCR Decoder: HCR initialization, preprocess HCR sideinfo,
88 decode priority codewords (PCWs)
90 *******************************************************************************/
92 #include "aacdec_hcr.h"
96 #include "aacdec_hcr_types.h"
97 #include "aacdec_hcr_bit.h"
98 #include "aacdec_hcrs.h"
104 #include "aacdecoder.h" /* for ID_CPE, ID_SCE ... */
105 #include "FDK_bitstream.h"
107 extern int mlFileChCurr
;
109 static void errDetectorInHcrSideinfoShrt(SCHAR cb
,
113 static void errDetectorInHcrLengths(SCHAR lengthOfLongestCodeword
,
114 SHORT lengthOfReorderedSpectralData
,
117 static void HcrCalcNumCodeword (H_HCR_INFO pHcr
);
118 static void HcrSortCodebookAndNumCodewordInSection(H_HCR_INFO pHcr
);
119 static void HcrPrepareSegmentationGrid (H_HCR_INFO pHcr
);
120 static void HcrExtendedSectionInfo (H_HCR_INFO pHcr
);
122 static void DeriveNumberOfExtendedSortedSectionsInSets(UINT numSegment
,
123 USHORT
*pNumExtendedSortedCodewordInSection
,
124 int numExtendedSortedCodewordInSectionIdx
,
125 USHORT
*pNumExtendedSortedSectionsInSets
,
126 int numExtendedSortedSectionsInSetsIdx
);
128 static INT
DecodeEscapeSequence(HANDLE_FDK_BITSTREAM bs
,
130 USHORT
*pLeftStartOfSegment
,
131 SCHAR
*pRemainingBitsInSegment
,
135 static int DecodePCW_Sign(HANDLE_FDK_BITSTREAM bs
,
137 const SCHAR
*pQuantVal
,
138 FIXP_DBL
*pQuantSpecCoef
,
139 int *quantSpecCoefIdx
,
140 USHORT
*pLeftStartOfSegment
,
141 SCHAR
*pRemainingBitsInSegment
,
145 static const SCHAR
*DecodePCW_Body(HANDLE_FDK_BITSTREAM bs
,
146 const UINT
*pCurrentTree
,
147 const SCHAR
*pQuantValBase
,
148 USHORT
*pLeftStartOfSegment
,
149 SCHAR
*pRemainingBitsInSegment
,
153 static void DecodePCWs(HANDLE_FDK_BITSTREAM bs
, H_HCR_INFO pHcr
);
155 static void HcrReorderQuantizedSpectralCoefficients(
157 CAacDecoderChannelInfo
*pAacDecoderChannelInfo
,
158 const SamplingRateInfo
*pSamplingRateInfo
162 #if CHECK_SEGMENTATION_IMMEDIATELY
163 static UCHAR
errDetectPcwSegmentation(SCHAR remainingBitsInSegment
,
166 FIXP_DBL
*qsc_base_of_cw
,
170 #if CHECK_SEGMENTATION_FINAL
171 static void errDetectWithinSegmentationFinal(H_HCR_INFO pHcr
);
174 /*---------------------------------------------------------------------------------------------
175 description: Check if codebook and numSect are within allowed range (short only)
176 -------------------------------------------------------------------------------------------- */
177 static void errDetectorInHcrSideinfoShrt(SCHAR cb
, SHORT numLine
,UINT
* errorWord
)
182 if ( cb
< ZERO_HCB
|| cb
>= MAX_CB_CHECK
|| cb
== BOOKSCL
) {
183 *errorWord
|= CB_OUT_OF_RANGE_SHORT_BLOCK
;
185 if ( numLine
< 0 || numLine
> 1024 ) {
186 *errorWord
|= LINE_IN_SECT_OUT_OF_RANGE_SHORT_BLOCK
;
190 /*---------------------------------------------------------------------------------------------
191 description: Check both HCR lengths
192 -------------------------------------------------------------------------------------------- */
193 static void errDetectorInHcrLengths(SCHAR lengthOfLongestCodeword
,
194 SHORT lengthOfReorderedSpectralData
,
197 if ( lengthOfReorderedSpectralData
< lengthOfLongestCodeword
) {
198 *errorWord
|= HCR_SI_LENGTHS_FAILURE
;
202 /*---------------------------------------------------------------------------------------------
203 description: Decode (and adapt if necessary) the two HCR sideinfo components:
204 'reordered_spectral_data_length' and 'longest_codeword_length'
205 -------------------------------------------------------------------------------------------- */
207 void CHcr_Read(HANDLE_FDK_BITSTREAM bs
,
208 CAacDecoderChannelInfo
*pAacDecoderChannelInfo
)
210 INT globalHcrType
= getHcrType(&pAacDecoderChannelInfo
->pComData
->overlay
.aac
.erHcrInfo
);
211 SHORT lengOfReorderedSpectralData
;
212 SCHAR lengOfLongestCodeword
;
214 pAacDecoderChannelInfo
->pDynData
->specificTo
.aac
.lenOfReorderedSpectralData
= 0;
215 pAacDecoderChannelInfo
->pDynData
->specificTo
.aac
.lenOfLongestCodeword
= 0;
219 /* ------- SI-Value No 1 ------- */
220 lengOfReorderedSpectralData
= FDKreadBits(bs
,14) + ERROR_LORSD
;
221 if ( globalHcrType
== ID_CPE
) {
222 if ((lengOfReorderedSpectralData
>= 0) && (lengOfReorderedSpectralData
<= CPE_TOP_LENGTH
)) {
223 pAacDecoderChannelInfo
->pDynData
->specificTo
.aac
.lenOfReorderedSpectralData
= lengOfReorderedSpectralData
; /* the decoded value is within range */
226 if (lengOfReorderedSpectralData
> CPE_TOP_LENGTH
) {
227 pAacDecoderChannelInfo
->pDynData
->specificTo
.aac
.lenOfReorderedSpectralData
= CPE_TOP_LENGTH
; /* use valid maximum */
231 else if (globalHcrType
== ID_SCE
|| globalHcrType
== ID_LFE
|| globalHcrType
== ID_CCE
) {
232 if ((lengOfReorderedSpectralData
>= 0) && (lengOfReorderedSpectralData
<= SCE_TOP_LENGTH
)) {
233 pAacDecoderChannelInfo
->pDynData
->specificTo
.aac
.lenOfReorderedSpectralData
= lengOfReorderedSpectralData
; /* the decoded value is within range */
236 if (lengOfReorderedSpectralData
> SCE_TOP_LENGTH
) {
237 pAacDecoderChannelInfo
->pDynData
->specificTo
.aac
.lenOfReorderedSpectralData
= SCE_TOP_LENGTH
; /* use valid maximum */
242 /* ------- SI-Value No 2 ------- */
243 lengOfLongestCodeword
= FDKreadBits(bs
,6) + ERROR_LOLC
;
244 if ((lengOfLongestCodeword
>= 0) && (lengOfLongestCodeword
<= LEN_OF_LONGEST_CW_TOP_LENGTH
)) {
245 pAacDecoderChannelInfo
->pDynData
->specificTo
.aac
.lenOfLongestCodeword
= lengOfLongestCodeword
; /* the decoded value is within range */
248 if (lengOfLongestCodeword
> LEN_OF_LONGEST_CW_TOP_LENGTH
) {
249 pAacDecoderChannelInfo
->pDynData
->specificTo
.aac
.lenOfLongestCodeword
= LEN_OF_LONGEST_CW_TOP_LENGTH
; /* use valid maximum */
255 /*---------------------------------------------------------------------------------------------
256 description: Sets up HCR ROM-Tables
257 -------------------------------------------------------------------------------------------- */
259 void HcrInitRom(H_HCR_INFO pHcr
)
261 pHcr
->cbPairs
.pMinOfCbPair
= aMinOfCbPair
;
262 pHcr
->cbPairs
.pMaxOfCbPair
= aMaxOfCbPair
;
264 pHcr
->tableInfo
.pMaxCwLength
= aMaxCwLen
;
265 pHcr
->tableInfo
.pCbDimension
= aDimCb
;
266 pHcr
->tableInfo
.pCbDimShift
= aDimCbShift
;
267 pHcr
->tableInfo
.pCbSign
= aSignCb
;
268 pHcr
->tableInfo
.pCbPriority
= aCbPriority
;
269 pHcr
->tableInfo
.pLargestAbsVal
= aLargestAbsoluteValue
;
272 /*---------------------------------------------------------------------------------------------
273 description: Set up HCR - must be called before every call to HcrDecoder().
274 For short block a sorting algorithm is applied to get the SI in the order
275 that HCR could assemble the qsc's as if it is a long block.
276 -----------------------------------------------------------------------------------------------
278 -------------------------------------------------------------------------------------------- */
280 UINT
HcrInit(H_HCR_INFO pHcr
,
281 CAacDecoderChannelInfo
*pAacDecoderChannelInfo
,
282 const SamplingRateInfo
*pSamplingRateInfo
,
283 HANDLE_FDK_BITSTREAM bs
)
285 CIcsInfo
*pIcsInfo
= &pAacDecoderChannelInfo
->icsInfo
;
286 SHORT
*pNumLinesInSec
;
293 pHcr
->decInOut
.lengthOfReorderedSpectralData
= pAacDecoderChannelInfo
->pDynData
->specificTo
.aac
.lenOfReorderedSpectralData
;
294 pHcr
->decInOut
.lengthOfLongestCodeword
= pAacDecoderChannelInfo
->pDynData
->specificTo
.aac
.lenOfLongestCodeword
;
295 pHcr
->decInOut
.pQuantizedSpectralCoefficientsBase
= pAacDecoderChannelInfo
->pSpectralCoefficient
;
296 pHcr
->decInOut
.quantizedSpectralCoefficientsIdx
= 0;
297 pHcr
->decInOut
.pCodebook
= pAacDecoderChannelInfo
->pDynData
->specificTo
.aac
.aCodeBooks4Hcr
;
298 pHcr
->decInOut
.pNumLineInSect
= pAacDecoderChannelInfo
->pDynData
->specificTo
.aac
.aNumLineInSec4Hcr
;
299 pHcr
->decInOut
.numSection
= pAacDecoderChannelInfo
->pDynData
->specificTo
.aac
.numberSection
;
300 pHcr
->decInOut
.errorLog
= 0;
301 pHcr
->nonPcwSideinfo
.pResultBase
= SPEC_LONG(pAacDecoderChannelInfo
->pSpectralCoefficient
);
304 pHcr
->decInOut
.bitstreamIndex
= FDKgetBitCnt(bs
);
306 if (!IsLongBlock(&pAacDecoderChannelInfo
->icsInfo
)) /* short block */
319 const SHORT
*BandOffsets
;
323 pCodeBook
= pAacDecoderChannelInfo
->pDynData
->aCodeBook
; /* in */
324 pNumLinesInSec
= pHcr
->decInOut
.pNumLineInSect
; /* out */
325 pCodeBk
= pHcr
->decInOut
.pCodebook
; /* out */
326 BandOffsets
= GetScaleFactorBandOffsets(pIcsInfo
, pSamplingRateInfo
); /* aux */
327 numOfGroups
= GetWindowGroups(pIcsInfo
);
332 cb_prev
= pCodeBook
[0];
334 /* convert HCR-sideinfo into a unitwise manner: When the cb changes, a new section starts */
336 *pCodeBk
++ = cb_prev
;
338 maxBand
= GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo
->icsInfo
);
339 for (band
= 0; band
< maxBand
; band
++) { /* from low to high sfbs i.e. from low to high frequencies */
340 numUnitInBand
= ((BandOffsets
[band
+1] - BandOffsets
[band
]) >> FOUR_LOG_DIV_TWO_LOG
); /* get the number of units in current sfb */
341 for (cntUnitInBand
= numUnitInBand
; cntUnitInBand
!= 0; cntUnitInBand
-- ) { /* for every unit in the band */
342 for (window
= 0, group
= 0; group
< numOfGroups
; group
++) {
343 winGroupLen
= GetWindowGroupLength(&pAacDecoderChannelInfo
->icsInfo
,group
);
344 for (groupWin
= winGroupLen
; groupWin
!= 0; groupWin
--, window
++) {
345 cb
= pCodeBook
[group
* 16 + band
];
347 #if CHECK_VALID_HCR_INPUT /* short-block 1 of 2 */
348 errDetectorInHcrSideinfoShrt(cb
,numLine
,&pHcr
->decInOut
.errorLog
);
349 if (pHcr
->decInOut
.errorLog
!= 0 ) {
350 return ( pHcr
->decInOut
.errorLog
);
354 *pNumLinesInSec
++ = numLine
;
358 numLine
= LINES_PER_UNIT
;
361 numLine
+= LINES_PER_UNIT
;
370 #if CHECK_VALID_HCR_INPUT /* short-block 2 of 2 */
371 errDetectorInHcrSideinfoShrt(cb
,numLine
,&pHcr
->decInOut
.errorLog
);
372 if ( numSection
<= 0 || numSection
> 1024/2 ) {
373 pHcr
->decInOut
.errorLog
|= NUM_SECT_OUT_OF_RANGE_SHORT_BLOCK
;
375 errDetectorInHcrLengths(pHcr
->decInOut
.lengthOfLongestCodeword
,
376 pHcr
->decInOut
.lengthOfReorderedSpectralData
,
377 &pHcr
->decInOut
.errorLog
);
378 if (pHcr
->decInOut
.errorLog
!= 0 ) {
379 return ( pHcr
->decInOut
.errorLog
);
384 *pNumLinesInSec
= numLine
;
385 pHcr
->decInOut
.numSection
= numSection
;
387 } else /* end short block prepare SI */
389 #if CHECK_VALID_HCR_INPUT /* long-block 1 of 1 */
390 errDetectorInHcrLengths(pHcr
->decInOut
.lengthOfLongestCodeword
,
391 pHcr
->decInOut
.lengthOfReorderedSpectralData
,
392 &pHcr
->decInOut
.errorLog
);
393 numSection
= pHcr
->decInOut
.numSection
;
394 pNumLinesInSec
= pHcr
->decInOut
.pNumLineInSect
;
395 pCodeBk
= pHcr
->decInOut
.pCodebook
;
396 if ( numSection
<= 0 || numSection
> 64 ) {
397 pHcr
->decInOut
.errorLog
|= NUM_SECT_OUT_OF_RANGE_LONG_BLOCK
;
401 for ( i
= numSection
; i
!= 0; i
-- )
405 if ( cb
< ZERO_HCB
|| cb
>= MAX_CB_CHECK
|| cb
== BOOKSCL
) {
406 pHcr
->decInOut
.errorLog
|= CB_OUT_OF_RANGE_LONG_BLOCK
;
409 numLine
= *pNumLinesInSec
++;
410 /* FDK_ASSERT(numLine > 0); */
412 if ( (numLine
<= 0) || (numLine
> 1024) ) {
413 pHcr
->decInOut
.errorLog
|= LINE_IN_SECT_OUT_OF_RANGE_LONG_BLOCK
;
416 if (pHcr
->decInOut
.errorLog
!= 0 ) {
417 return ( pHcr
->decInOut
.errorLog
);
419 #endif /* CHECK_VALID_HCR_INPUT */
422 pCodeBk
= pHcr
->decInOut
.pCodebook
;
423 for ( i
= 0; i
< numSection
; i
++ ) {
425 (*pCodeBk
== NOISE_HCB
) ||
426 (*pCodeBk
== INTENSITY_HCB2
) ||
427 (*pCodeBk
== INTENSITY_HCB
))
434 /* HCR-sideinfo-input is complete and seems to be valid */
438 return ( pHcr
->decInOut
.errorLog
);
446 /*---------------------------------------------------------------------------------------------
448 description: This HCR - dummy - function writes only a dirac-sequence in output buffer
450 -------------------------------------------------------------------------------------------- */
451 UINT
HcrDecoder(H_HCR_INFO pHcr
,
452 const CAacDecoderChannelInfo
*pAacDecoderChannelInfo
,
453 HANDLE_FDK_BITSTREAM bs
)
455 for (SHORT i
=0; i
< 1024; i
++ ) {
456 pHcr
->decInOut
.pQuantizedSpectralCoefficients
->Long
[i
] = FL2FXCONST_DBL(0.0f
);
458 pHcr
->decInOut
.pQuantizedSpectralCoefficients
->Long
[i
] = (FIXP_DBL
)HCR_DIRAC
;
464 #else /* USE_HCR_DUMMY */
466 /*---------------------------------------------------------------------------------------------
467 description: This function decodes the codewords of the spectral coefficients from the
468 bitstream according to the HCR algorithm and stores the quantized spectral
469 coefficients in correct order in the output buffer.
470 -------------------------------------------------------------------------------------------- */
472 UINT
HcrDecoder(H_HCR_INFO pHcr
,
473 CAacDecoderChannelInfo
*pAacDecoderChannelInfo
,
474 const SamplingRateInfo
*pSamplingRateInfo
,
475 HANDLE_FDK_BITSTREAM bs
)
477 int pTmp1
, pTmp2
, pTmp3
, pTmp4
;
478 #if DETECT_TOO_LONG_CW_READS
483 UINT saveBitCnt
= FDKgetBitCnt(bs
); /* save bitstream position */
485 HcrCalcNumCodeword(pHcr
);
487 HcrSortCodebookAndNumCodewordInSection(pHcr
);
489 HcrPrepareSegmentationGrid(pHcr
);
491 HcrExtendedSectionInfo(pHcr
);
493 if (( pHcr
->decInOut
.errorLog
& HCR_FATAL_PCW_ERROR_MASK
) != 0 ) {
494 return ( pHcr
->decInOut
.errorLog
); /* sideinfo is massively corrupt, return from HCR without having decoded anything */
497 DeriveNumberOfExtendedSortedSectionsInSets(pHcr
->segmentInfo
.numSegment
,
498 pHcr
->sectionInfo
.pNumExtendedSortedCodewordInSection
,
499 pHcr
->sectionInfo
.numExtendedSortedCodewordInSectionIdx
,
500 pHcr
->sectionInfo
.pNumExtendedSortedSectionsInSets
,
501 pHcr
->sectionInfo
.numExtendedSortedSectionsInSetsIdx
);
504 pTmp1
= pHcr
->sectionInfo
.numExtendedSortedCodewordInSectionIdx
;
505 pTmp2
= pHcr
->sectionInfo
.extendedSortedCodebookIdx
;
506 pTmp3
= pHcr
->sectionInfo
.numExtendedSortedSectionsInSetsIdx
;
507 pTmp4
= pHcr
->decInOut
.quantizedSpectralCoefficientsIdx
;
508 #if DETECT_TOO_LONG_CW_READS
509 pTmp5
= pHcr
->sectionInfo
.maxLenOfCbInExtSrtSecIdx
;
512 /* ------- decode meaningful PCWs ------ */
513 DecodePCWs(bs
, pHcr
);
515 if (( pHcr
->decInOut
.errorLog
& HCR_FATAL_PCW_ERROR_MASK
) == 0 ) {
516 /* ------ decode the non-PCWs -------- */
517 DecodeNonPCWs(bs
, pHcr
);
521 #if CHECK_SEGMENTATION_FINAL
522 errDetectWithinSegmentationFinal(pHcr
);
526 pHcr
->sectionInfo
.numExtendedSortedCodewordInSectionIdx
= pTmp1
;
527 pHcr
->sectionInfo
.extendedSortedCodebookIdx
= pTmp2
;
528 pHcr
->sectionInfo
.numExtendedSortedSectionsInSetsIdx
= pTmp3
;
529 pHcr
->decInOut
.quantizedSpectralCoefficientsIdx
= pTmp4
;
530 #if DETECT_TOO_LONG_CW_READS
531 pHcr
->sectionInfo
.maxLenOfCbInExtSrtSecIdx
= pTmp5
;
534 HcrReorderQuantizedSpectralCoefficients(pHcr
, pAacDecoderChannelInfo
, pSamplingRateInfo
);
536 /* restore bitstream position */
537 bitCntOffst
= saveBitCnt
- FDKgetBitCnt(bs
);
539 FDKpushBiDirectional(bs
, bitCntOffst
);
542 return ( pHcr
->decInOut
.errorLog
);
546 #endif /* USE_HCR_DUMMY */
551 /*---------------------------------------------------------------------------------------------
552 description: This function reorders the quantized spectral coefficients sectionwise for
553 long- and short-blocks and compares to the LAV (Largest Absolute Value of
554 the current codebook) -- a counter is incremented if there is an error
556 Additional for short-blocks a unit-based-deinterleaving is applied.
557 Moreover (for short blocks) the scaling is derived (compare plain huffman
559 -------------------------------------------------------------------------------------------- */
561 static void HcrReorderQuantizedSpectralCoefficients(
562 H_HCR_INFO pHcr
, CAacDecoderChannelInfo
*pAacDecoderChannelInfo
,
563 const SamplingRateInfo
*pSamplingRateInfo
569 USHORT numSpectralValuesInSection
;
571 USHORT lavErrorCnt
= 0;
573 UINT numSection
= pHcr
->decInOut
.numSection
;
574 SPECTRAL_PTR pQuantizedSpectralCoefficientsBase
= pHcr
->decInOut
.pQuantizedSpectralCoefficientsBase
;
575 FIXP_DBL
*pQuantizedSpectralCoefficients
= SPEC_LONG(pHcr
->decInOut
.pQuantizedSpectralCoefficientsBase
);
576 const UCHAR
*pCbDimShift
= pHcr
->tableInfo
.pCbDimShift
;
577 const USHORT
*pLargestAbsVal
= pHcr
->tableInfo
.pLargestAbsVal
;
578 UCHAR
*pSortedCodebook
= pHcr
->sectionInfo
.pSortedCodebook
;
579 USHORT
*pNumSortedCodewordInSection
= pHcr
->sectionInfo
.pNumSortedCodewordInSection
;
580 USHORT
*pReorderOffset
= pHcr
->sectionInfo
.pReorderOffset
;
581 FIXP_DBL
*pTempValues
= pHcr
->segmentInfo
.pTempValues
;
582 FIXP_DBL
*pBak
= pHcr
->segmentInfo
.pTempValues
;
584 FDKmemclear(pTempValues
,1024*sizeof(FIXP_DBL
));
586 /* long and short: check if decoded huffman-values (quantized spectral coefficients) are within range */
587 for ( i
=numSection
; i
!= 0; i
-- ) {
588 numSpectralValuesInSection
= *pNumSortedCodewordInSection
++ << pCbDimShift
[*pSortedCodebook
];
589 pTeVa
= &pTempValues
[*pReorderOffset
++];
590 for( j
= numSpectralValuesInSection
; j
!= 0; j
-- ) {
591 qsc
= *pQuantizedSpectralCoefficients
++;
592 abs_qsc
= FDKabs(qsc
);
593 #if VALID_LAV_ERROR_TRIGGER
594 if ( abs_qsc
<= pLargestAbsVal
[*pSortedCodebook
] ) {
595 *pTeVa
++ = (FIXP_DBL
)qsc
; /* the qsc value is within range */
597 else { /* line is too high .. */
598 if ( abs_qsc
== Q_VALUE_INVALID
) { /* .. because of previous marking --> dont set LAV flag (would be confusing), just copy out the already marked value */
599 *pTeVa
++ = (FIXP_DBL
) qsc
;
601 else { /* .. because a too high value was decoded for this cb --> set LAV flag */
602 *pTeVa
++ = (FIXP_DBL
) Q_VALUE_INVALID
;
607 if ( abs_qsc
<= pLargestAbsVal
[*pSortedCodebook
] ) {
611 *pTeVa
++ = Q_VALUE_INVALID
;
619 if (!IsLongBlock(&pAacDecoderChannelInfo
->icsInfo
))
637 SHORT
*pScaleFacHcr
= pAacDecoderChannelInfo
->pDynData
->aScaleFactor
;
638 SHORT
*pSfbSclHcr
= pAacDecoderChannelInfo
->pDynData
->aSfbScale
;
639 const SHORT
*BandOffsets
= GetScaleFactorBandOffsets(&pAacDecoderChannelInfo
->icsInfo
, pSamplingRateInfo
);
641 pBak
= pHcr
->segmentInfo
.pTempValues
;
642 /* deinterleave unitwise for short blocks */
643 for ( window
= 0; window
< (8); window
++ ) {
644 pOut
= SPEC(pQuantizedSpectralCoefficientsBase
, window
, pAacDecoderChannelInfo
->granuleLength
);
645 for ( i
=0; i
< (LINES_PER_UNIT_GROUP
); i
++ ) {
646 pTeVa
= pBak
+ (window
<< FOUR_LOG_DIV_TWO_LOG
) + i
* 32; /* distance of lines between unit groups has to be constant for every framelength (32)! */
647 for ( j
=(LINES_PER_UNIT
); j
!= 0; j
-- ) {
653 /* short blocks only */
654 /* derive global scaling-value for every sfb and every window (as it is done in plain-huffman-decoder at short blocks) */
657 numWinGroup
= GetWindowGroups(&pAacDecoderChannelInfo
->icsInfo
);
658 numSfbTransm
= GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo
->icsInfo
);
660 for (group
= 0; group
< numWinGroup
; group
++) {
661 winGroupLen
= GetWindowGroupLength(&pAacDecoderChannelInfo
->icsInfo
,group
);
662 for (band
= 0; band
< numSfbTransm
; band
++) {
663 interm
= group
* 16 + band
;
664 msb
= pScaleFacHcr
[interm
] >> 2;
665 lsb
= pScaleFacHcr
[interm
] & 3;
666 for (groupwin
= 0; groupwin
< winGroupLen
; groupwin
++) {
667 window
= groupoffset
+ groupwin
;
668 pBak
= SPEC(pQuantizedSpectralCoefficientsBase
, window
, pAacDecoderChannelInfo
->granuleLength
);
669 locMax
= FL2FXCONST_DBL(0.0f
);
670 for (index
= BandOffsets
[band
]; index
< BandOffsets
[band
+1]; index
+= LINES_PER_UNIT
) {
671 pTeVa
= &pBak
[index
];
672 for ( i
= LINES_PER_UNIT
; i
!= 0; i
--) {
673 tmp
= (*pTeVa
< FL2FXCONST_DBL(0.0f
))? -*pTeVa
++ : *pTeVa
++;
674 locMax
= fixMax(tmp
,locMax
);
677 if ( fixp_abs(locMax
) > (FIXP_DBL
)MAX_QUANTIZED_VALUE
) {
678 locMax
= (FIXP_DBL
)MAX_QUANTIZED_VALUE
;
680 pSfbSclHcr
[window
*16+band
] = msb
- GetScaleFromValue(locMax
, lsb
); /* save global scale maxima in this sfb */
683 groupoffset
+= GetWindowGroupLength(&pAacDecoderChannelInfo
->icsInfo
,group
);
687 /* copy straight for long-blocks */
688 pQuantizedSpectralCoefficients
= SPEC_LONG(pQuantizedSpectralCoefficientsBase
);
689 for ( i
= 1024; i
!= 0; i
-- ) {
690 *pQuantizedSpectralCoefficients
++ = *pBak
++;
694 if ( lavErrorCnt
!= 0 ) {
695 pHcr
->decInOut
.errorLog
|= LAV_VIOLATION
;
700 /*---------------------------------------------------------------------------------------------
701 description: This function calculates the number of codewords
702 for each section (numCodewordInSection) and the number of codewords
703 for all sections (numCodeword).
704 For zero and intensity codebooks a entry is also done in the variable
705 numCodewordInSection. It is assumed that the codebook is a two tuples
706 codebook. This is needed later for the calculation of the base addresses
707 for the reordering of the quantize spectral coefficients at the end of the
709 The variable numCodeword contain the number of codewords which are really
710 in the bitstream. Zero or intensity codebooks does not increase the
711 variable numCodewords.
712 -----------------------------------------------------------------------------------------------
714 -------------------------------------------------------------------------------------------- */
716 static void HcrCalcNumCodeword(H_HCR_INFO pHcr
)
721 UINT numSection
= pHcr
->decInOut
.numSection
;
722 UCHAR
*pCodebook
= pHcr
->decInOut
.pCodebook
;
723 SHORT
*pNumLineInSection
= pHcr
->decInOut
.pNumLineInSect
;
724 const UCHAR
*pCbDimShift
= pHcr
->tableInfo
.pCbDimShift
;
725 USHORT
*pNumCodewordInSection
= pHcr
->sectionInfo
.pNumCodewordInSection
;
728 for ( hcrSection
= numSection
; hcrSection
!= 0; hcrSection
-- ) {
729 *pNumCodewordInSection
= *pNumLineInSection
++ >> pCbDimShift
[*pCodebook
];
730 if ( *pCodebook
!= 0 ) {
731 numCodeword
+= *pNumCodewordInSection
;
733 pNumCodewordInSection
++;
736 pHcr
->sectionInfo
.numCodeword
= numCodeword
;
740 /*---------------------------------------------------------------------------------------------
741 description: This function calculates the number
742 of sorted codebooks and sorts the codebooks and the numCodewordInSection
743 according to the priority.
744 -------------------------------------------------------------------------------------------- */
746 static void HcrSortCodebookAndNumCodewordInSection(H_HCR_INFO pHcr
)
757 UINT numSection
= pHcr
->decInOut
.numSection
;
758 UCHAR
*pCodebook
= pHcr
->decInOut
.pCodebook
;
759 UCHAR
*pSortedCodebook
= pHcr
->sectionInfo
.pSortedCodebook
;
760 USHORT
*pNumCodewordInSection
= pHcr
->sectionInfo
.pNumCodewordInSection
;
761 USHORT
*pNumSortedCodewordInSection
= pHcr
->sectionInfo
.pNumSortedCodewordInSection
;
762 UCHAR
*pCodebookSwitch
= pHcr
->sectionInfo
.pCodebookSwitch
;
763 USHORT
*pReorderOffset
= pHcr
->sectionInfo
.pReorderOffset
;
764 const UCHAR
*pCbPriority
= pHcr
->tableInfo
.pCbPriority
;
765 const UCHAR
*pMinOfCbPair
= pHcr
->cbPairs
.pMinOfCbPair
;
766 const UCHAR
*pMaxOfCbPair
= pHcr
->cbPairs
.pMaxOfCbPair
;
767 const UCHAR
*pCbDimShift
= pHcr
->tableInfo
.pCbDimShift
;
769 UINT searchStart
= 0;
771 /* calculate *pNumSortedSection and store the priorities in array pSortedCdebook */
772 pDest
= pSortedCodebook
;
774 for ( i
=numSection
; i
!= 0; i
-- ) {
775 if ( pCbPriority
[*pCodebook
] == 0 ) {
778 *pDest
++ = pCbPriority
[*pCodebook
++];
780 pHcr
->sectionInfo
.numSortedSection
= numSection
- numZeroSection
; /* numSortedSection contains no zero or intensity section */
781 pCodebook
= pHcr
->decInOut
.pCodebook
;
783 /* sort priorities of the codebooks in array pSortedCdebook[] */
784 numSectionDec
= numSection
- 1;
785 if ( numSectionDec
> 0 ) {
786 counter
= numSectionDec
;
787 for ( j
=numSectionDec
; j
!= 0; j
-- ) {
788 for ( i
=0; i
< counter
; i
++ ) {
789 /* swap priorities */
790 if ( pSortedCodebook
[i
+1] > pSortedCodebook
[i
] ) {
791 temp
= pSortedCodebook
[i
];
792 pSortedCodebook
[i
] = pSortedCodebook
[i
+1];
793 pSortedCodebook
[i
+1] = temp
;
800 /* clear codebookSwitch array */
801 for ( i
= numSection
; i
!= 0; i
--) {
802 *pCodebookSwitch
++ = 0;
804 pCodebookSwitch
= pHcr
->sectionInfo
.pCodebookSwitch
;
806 /* sort sectionCodebooks and numCodwordsInSection and calculate pReorderOffst[j] */
807 for ( j
= 0; j
< numSection
; j
++ ) {
808 for ( i
= searchStart
; i
< numSection
; i
++ ) {
809 if ( pCodebookSwitch
[i
] == 0 && ( pMinOfCbPair
[pSortedCodebook
[j
]] == pCodebook
[i
] || pMaxOfCbPair
[pSortedCodebook
[j
]] == pCodebook
[i
] )) {
810 pCodebookSwitch
[i
] = 1;
811 pSortedCodebook
[j
] = pCodebook
[i
]; /* sort codebook */
812 pNumSortedCodewordInSection
[j
] = pNumCodewordInSection
[i
]; /* sort NumCodewordInSection */
815 for ( k
= 0; k
< i
; k
++ ) { /* make entry in pReorderOffst */
816 startOffset
+= pNumCodewordInSection
[k
] << pCbDimShift
[pCodebook
[k
]];
818 pReorderOffset
[j
] = startOffset
; /* offset for reordering the codewords */
820 if(i
== searchStart
) {
822 while(pCodebookSwitch
[k
++] == 1) searchStart
++;
831 /*---------------------------------------------------------------------------------------------
832 description: This function calculates the segmentation, which includes numSegment,
833 leftStartOfSegment, rightStartOfSegment and remainingBitsInSegment.
834 The segmentation could be visualized a as kind of 'overlay-grid' for the
835 bitstream-block holding the HCR-encoded quantized-spectral-coefficients.
836 -------------------------------------------------------------------------------------------- */
838 static void HcrPrepareSegmentationGrid(H_HCR_INFO pHcr
)
841 USHORT numSegment
= 0;
842 USHORT segmentStart
= 0;
844 UCHAR lastSegmentWidth
;
845 UCHAR sortedCodebook
;
847 USHORT intermediateResult
;
849 SCHAR lengthOfLongestCodeword
= pHcr
->decInOut
.lengthOfLongestCodeword
;
850 SHORT lengthOfReorderedSpectralData
= pHcr
->decInOut
.lengthOfReorderedSpectralData
;
851 UINT numSortedSection
= pHcr
->sectionInfo
.numSortedSection
;
852 UCHAR
*pSortedCodebook
= pHcr
->sectionInfo
.pSortedCodebook
;
853 USHORT
*pNumSortedCodewordInSection
= pHcr
->sectionInfo
.pNumSortedCodewordInSection
;
854 USHORT
*pLeftStartOfSegment
= pHcr
->segmentInfo
.pLeftStartOfSegment
;
855 USHORT
*pRightStartOfSegment
= pHcr
->segmentInfo
.pRightStartOfSegment
;
856 SCHAR
*pRemainingBitsInSegment
= pHcr
->segmentInfo
.pRemainingBitsInSegment
;
857 USHORT bitstreamIndex
= pHcr
->decInOut
.bitstreamIndex
;
858 const UCHAR
*pMaxCwLength
= pHcr
->tableInfo
.pMaxCwLength
;
860 for ( i
=numSortedSection
; i
!= 0; i
-- ) {
861 sortedCodebook
= *pSortedCodebook
++;
862 segmentWidth
= FDKmin(pMaxCwLength
[sortedCodebook
],lengthOfLongestCodeword
);
864 for ( j
= *pNumSortedCodewordInSection
; j
!= 0 ; j
-- ) {
865 /* width allows a new segment */
866 intermediateResult
= bitstreamIndex
+ segmentStart
;
867 if ( (segmentStart
+ segmentWidth
) <= lengthOfReorderedSpectralData
) {
868 /* store segment start, segment length and increment the number of segments */
869 *pLeftStartOfSegment
++ = intermediateResult
;
870 *pRightStartOfSegment
++ = intermediateResult
+ segmentWidth
- 1;
871 *pRemainingBitsInSegment
++ = segmentWidth
;
872 segmentStart
+= segmentWidth
;
875 /* width does not allow a new segment */
877 /* correct the last segment length */
878 pLeftStartOfSegment
--;
879 pRightStartOfSegment
--;
880 pRemainingBitsInSegment
--;
881 segmentStart
= *pLeftStartOfSegment
- bitstreamIndex
;
883 lastSegmentWidth
= lengthOfReorderedSpectralData
- segmentStart
;
884 *pRemainingBitsInSegment
= lastSegmentWidth
;
885 *pRightStartOfSegment
= bitstreamIndex
+ segmentStart
+ lastSegmentWidth
- 1;
890 pNumSortedCodewordInSection
++;
895 pHcr
->segmentInfo
.numSegment
= numSegment
;
900 /*---------------------------------------------------------------------------------------------
901 description: This function adapts the sorted section boundaries to the boundaries of
902 segmentation. If the section lengths does not fit completely into the
903 current segment, the section is spitted into two so called 'extended
904 sections'. The extended-section-info (pNumExtendedSortedCodewordInSectin
905 and pExtendedSortedCodebook) is updated in this case.
907 -------------------------------------------------------------------------------------------- */
909 static void HcrExtendedSectionInfo(H_HCR_INFO pHcr
)
911 UINT srtSecCnt
= 0; /* counter for sorted sections */
912 UINT xSrtScCnt
= 0; /* counter for extended sorted sections */
913 UINT remainNumCwInSortSec
;
914 UINT inSegmentRemainNumCW
;
916 UINT numSortedSection
= pHcr
->sectionInfo
.numSortedSection
;
917 UCHAR
*pSortedCodebook
= pHcr
->sectionInfo
.pSortedCodebook
;
918 USHORT
*pNumSortedCodewordInSection
= pHcr
->sectionInfo
.pNumSortedCodewordInSection
;
919 UCHAR
*pExtendedSortedCoBo
= pHcr
->sectionInfo
.pExtendedSortedCodebook
;
920 USHORT
*pNumExtSortCwInSect
= pHcr
->sectionInfo
.pNumExtendedSortedCodewordInSection
;
921 UINT numSegment
= pHcr
->segmentInfo
.numSegment
;
922 #if DETECT_TOO_LONG_CW_READS
923 UCHAR
*pMaxLenOfCbInExtSrtSec
= pHcr
->sectionInfo
.pMaxLenOfCbInExtSrtSec
;
924 SCHAR lengthOfLongestCodeword
= pHcr
->decInOut
.lengthOfLongestCodeword
;
925 const UCHAR
*pMaxCwLength
= pHcr
->tableInfo
.pMaxCwLength
;
928 remainNumCwInSortSec
= pNumSortedCodewordInSection
[srtSecCnt
];
929 inSegmentRemainNumCW
= numSegment
;
931 while (srtSecCnt
< numSortedSection
) {
932 if (inSegmentRemainNumCW
< remainNumCwInSortSec
) {
934 pNumExtSortCwInSect
[xSrtScCnt
] = inSegmentRemainNumCW
;
935 pExtendedSortedCoBo
[xSrtScCnt
] = pSortedCodebook
[srtSecCnt
];
937 remainNumCwInSortSec
-= inSegmentRemainNumCW
;
938 inSegmentRemainNumCW
= numSegment
;
939 /* data of a sorted section was not integrated in extended sorted section */
941 else if (inSegmentRemainNumCW
== remainNumCwInSortSec
) {
942 pNumExtSortCwInSect
[xSrtScCnt
] = inSegmentRemainNumCW
;
943 pExtendedSortedCoBo
[xSrtScCnt
] = pSortedCodebook
[srtSecCnt
];
946 remainNumCwInSortSec
= pNumSortedCodewordInSection
[srtSecCnt
];
947 inSegmentRemainNumCW
= numSegment
;
948 /* data of a sorted section was integrated in extended sorted section */
950 else { /* inSegmentRemainNumCW > remainNumCwInSortSec */
951 pNumExtSortCwInSect
[xSrtScCnt
] = remainNumCwInSortSec
;
952 pExtendedSortedCoBo
[xSrtScCnt
] = pSortedCodebook
[srtSecCnt
];
955 inSegmentRemainNumCW
-= remainNumCwInSortSec
;
957 remainNumCwInSortSec
= pNumSortedCodewordInSection
[srtSecCnt
];
958 /* data of a sorted section was integrated in extended sorted section */
960 #if DETECT_TOO_LONG_CW_READS
961 pMaxLenOfCbInExtSrtSec
[xSrtScCnt
] = FDKmin(pMaxCwLength
[pExtendedSortedCoBo
[xSrtScCnt
]],lengthOfLongestCodeword
);
968 if ( xSrtScCnt
>= (MAX_SFB_HCR
+ MAX_HCR_SETS
) ) {
969 pHcr
->decInOut
.errorLog
|= EXTENDED_SORTED_COUNTER_OVERFLOW
;
974 pNumExtSortCwInSect
[xSrtScCnt
] = 0;
979 /*---------------------------------------------------------------------------------------------
980 description: This function calculates the number of extended sorted sections which
981 belong to the sets. Each set from set 0 (one and only set for the PCWs)
982 till to the last set gets a entry in the array to which
983 'pNumExtendedSortedSectinsInSets' points to.
985 Calculation: The entrys in pNumExtendedSortedCodewordInSectin are added
986 untill the value numSegment is reached. Then the sum_variable is cleared
987 and the calculation starts from the beginning. As much extended sorted
988 Sections are summed up to reach the value numSegment, as much is the
989 current entry in *pNumExtendedSortedCodewordInSectin.
990 -------------------------------------------------------------------------------------------- */
991 static void DeriveNumberOfExtendedSortedSectionsInSets(UINT numSegment
,
992 USHORT
*pNumExtendedSortedCodewordInSection
,
993 int numExtendedSortedCodewordInSectionIdx
,
994 USHORT
*pNumExtendedSortedSectionsInSets
,
995 int numExtendedSortedSectionsInSetsIdx
)
999 USHORT
*pNumExSortCwInSec
= pNumExtendedSortedCodewordInSection
;
1000 USHORT
*pNumExSortSecInSets
= pNumExtendedSortedSectionsInSets
;
1002 while (pNumExSortCwInSec
[numExtendedSortedCodewordInSectionIdx
] != 0)
1004 cwSum
+= pNumExSortCwInSec
[numExtendedSortedCodewordInSectionIdx
];
1005 numExtendedSortedCodewordInSectionIdx
++;
1006 if (numExtendedSortedCodewordInSectionIdx
>= (MAX_SFB_HCR
+MAX_HCR_SETS
)) {
1009 if (cwSum
> numSegment
) {
1013 if (counter
> 1024/4) {
1016 if ( cwSum
== numSegment
) {
1017 pNumExSortSecInSets
[numExtendedSortedSectionsInSetsIdx
] = counter
;
1018 numExtendedSortedSectionsInSetsIdx
++;
1019 if (numExtendedSortedSectionsInSetsIdx
>= MAX_HCR_SETS
) {
1026 pNumExSortSecInSets
[numExtendedSortedSectionsInSetsIdx
] = counter
; /* save last entry for the last - probably shorter - set */
1030 /*---------------------------------------------------------------------------------------------
1031 description: This function decodes all priority codewords (PCWs) in a spectrum (within
1032 set 0). The calculation of the PCWs is managed in two loops. The
1033 loopcounter of the outer loop is set to the first value pointer
1034 pNumExtendedSortedSectionsInSets points to. This value represents the
1035 number of extended sorted sections within set 0.
1036 The loopcounter of the inner loop is set to the first value pointer
1037 pNumExtendedSortedCodewordInSectin points to. The value represents the
1038 number of extended sorted codewords in sections (the original sections have
1039 been splitted to go along with the borders of the sets).
1040 Each time the number of the extended sorted codewords in sections are de-
1041 coded, the pointer 'pNumExtendedSortedCodewordInSectin' is incremented by
1043 -------------------------------------------------------------------------------------------- */
1044 static void DecodePCWs(HANDLE_FDK_BITSTREAM bs
, H_HCR_INFO pHcr
)
1048 USHORT curExtSortCwInSec
;
1051 const UINT
*pCurrentTree
;
1052 const SCHAR
*pQuantValBase
;
1053 const SCHAR
*pQuantVal
;
1055 USHORT
*pNumExtendedSortedCodewordInSection
= pHcr
->sectionInfo
.pNumExtendedSortedCodewordInSection
;
1056 int numExtendedSortedCodewordInSectionIdx
= pHcr
->sectionInfo
.numExtendedSortedCodewordInSectionIdx
;
1057 UCHAR
*pExtendedSortedCodebook
= pHcr
->sectionInfo
.pExtendedSortedCodebook
;
1058 int extendedSortedCodebookIdx
= pHcr
->sectionInfo
.extendedSortedCodebookIdx
;
1059 USHORT
*pNumExtendedSortedSectionsInSets
= pHcr
->sectionInfo
.pNumExtendedSortedSectionsInSets
;
1060 int numExtendedSortedSectionsInSetsIdx
= pHcr
->sectionInfo
.numExtendedSortedSectionsInSetsIdx
;
1061 FIXP_DBL
*pQuantizedSpectralCoefficients
= SPEC_LONG(pHcr
->decInOut
.pQuantizedSpectralCoefficientsBase
);
1062 int quantizedSpectralCoefficientsIdx
= pHcr
->decInOut
.quantizedSpectralCoefficientsIdx
;
1063 USHORT
*pLeftStartOfSegment
= pHcr
->segmentInfo
.pLeftStartOfSegment
;
1064 SCHAR
*pRemainingBitsInSegment
= pHcr
->segmentInfo
.pRemainingBitsInSegment
;
1065 #if DETECT_TOO_LONG_CW_READS
1066 UCHAR
*pMaxLenOfCbInExtSrtSec
= pHcr
->sectionInfo
.pMaxLenOfCbInExtSrtSec
;
1067 int maxLenOfCbInExtSrtSecIdx
= pHcr
->sectionInfo
.maxLenOfCbInExtSrtSecIdx
;
1068 UCHAR maxAllowedCwLen
;
1071 const UCHAR
*pCbDimension
= pHcr
->tableInfo
.pCbDimension
;
1072 const UCHAR
*pCbSign
= pHcr
->tableInfo
.pCbSign
;
1074 /* clear result array */
1075 //pQSC = &pQuantizedSpectralCoefficients[quantizedSpectralCoefficientsIdx];
1076 //pQSC = *pQuantizedSpectralCoefficients;
1078 FDKmemclear(pQuantizedSpectralCoefficients
+quantizedSpectralCoefficientsIdx
,1024*sizeof(FIXP_DBL
));
1080 /* decode all PCWs in the extended sorted section(s) belonging to set 0 */
1081 for ( extSortSec
= pNumExtendedSortedSectionsInSets
[numExtendedSortedSectionsInSetsIdx
]; extSortSec
!= 0; extSortSec
-- ) {
1083 codebook
= pExtendedSortedCodebook
[extendedSortedCodebookIdx
]; /* get codebook for this extended sorted section and increment ptr to cb of next ext. sort sec */
1084 extendedSortedCodebookIdx
++;
1085 if (extendedSortedCodebookIdx
>= (MAX_SFB_HCR
+MAX_HCR_SETS
)) {
1088 dimension
= pCbDimension
[codebook
]; /* get dimension of codebook of this extended sort. sec. */
1089 pCurrentTree
= aHuffTable
[codebook
]; /* convert codebook to pointer to QSCs */
1090 pQuantValBase
= aQuantTable
[codebook
]; /* convert codebook to index to table of QSCs */
1091 #if DETECT_TOO_LONG_CW_READS
1092 maxAllowedCwLen
= pMaxLenOfCbInExtSrtSec
[maxLenOfCbInExtSrtSecIdx
];
1093 maxLenOfCbInExtSrtSecIdx
++;
1094 if (maxLenOfCbInExtSrtSecIdx
>= (MAX_SFB_HCR
+MAX_HCR_SETS
)) {
1099 /* switch for decoding with different codebooks: */
1100 if ( pCbSign
[codebook
] == 0 ) { /* no sign bits follow after the codeword-body */
1104 for ( curExtSortCwInSec
= pNumExtendedSortedCodewordInSection
[numExtendedSortedCodewordInSectionIdx
] ; curExtSortCwInSec
!= 0; curExtSortCwInSec
--) {
1107 /* decode PCW_BODY */
1108 pQuantVal
= DecodePCW_Body(bs
,
1111 pLeftStartOfSegment
,
1112 pRemainingBitsInSegment
,
1116 /* result is written out here because NO sign bits follow the body */
1117 for( i
=dimension
; i
!= 0 ; i
-- ) {
1118 pQuantizedSpectralCoefficients
[quantizedSpectralCoefficientsIdx
] = (FIXP_DBL
) *pQuantVal
++; /* write quant. spec. coef. into spectrum; sign is already valid */
1119 quantizedSpectralCoefficientsIdx
++;
1120 if (quantizedSpectralCoefficientsIdx
>= 1024) {
1125 /* one more PCW should be decoded */
1127 #if DETECT_TOO_LONG_CW_READS
1128 if ( maxAllowedCwLen
< (numDecodedBits
+ ERROR_PCW_BODY_ONLY_TOO_LONG
) ) {
1129 pHcr
->decInOut
.errorLog
|= TOO_MANY_PCW_BODY_BITS_DECODED
;
1133 #if CHECK_SEGMENTATION_IMMEDIATELY
1134 if (1 == errDetectPcwSegmentation(*pRemainingBitsInSegment
-ERROR_PCW_BODY
,pHcr
,PCW_BODY
,pQuantizedSpectralCoefficients
+quantizedSpectralCoefficientsIdx
-dimension
,dimension
)) {
1138 pLeftStartOfSegment
++; /* update pointer for decoding the next PCW */
1139 pRemainingBitsInSegment
++; /* update pointer for decoding the next PCW */
1142 else if (( pCbSign
[codebook
] == 1 ) && ( codebook
< 11 )) { /* possibly there follow 1,2,3 or 4 sign bits after the codeword-body */
1143 /* PCW_Body and PCW_Sign */
1144 /*=======================*/
1146 for ( curExtSortCwInSec
= pNumExtendedSortedCodewordInSection
[numExtendedSortedCodewordInSectionIdx
] ; curExtSortCwInSec
!= 0; curExtSortCwInSec
--)
1151 pQuantVal
= DecodePCW_Body(bs
,
1154 pLeftStartOfSegment
,
1155 pRemainingBitsInSegment
,
1159 err
= DecodePCW_Sign( bs
,
1162 pQuantizedSpectralCoefficients
,
1163 &quantizedSpectralCoefficientsIdx
,
1164 pLeftStartOfSegment
,
1165 pRemainingBitsInSegment
,
1171 /* one more PCW should be decoded */
1173 #if DETECT_TOO_LONG_CW_READS
1174 if ( maxAllowedCwLen
< (numDecodedBits
+ ERROR_PCW_BODY_SIGN_TOO_LONG
) ) {
1175 pHcr
->decInOut
.errorLog
|= TOO_MANY_PCW_BODY_SIGN_BITS_DECODED
;
1179 #if CHECK_SEGMENTATION_IMMEDIATELY
1180 if (1 == errDetectPcwSegmentation(*pRemainingBitsInSegment
-ERROR_PCW_BODY_SIGN
,pHcr
,PCW_BODY_SIGN
, pQuantizedSpectralCoefficients
+quantizedSpectralCoefficientsIdx
-dimension
,dimension
)) {
1184 pLeftStartOfSegment
++;
1185 pRemainingBitsInSegment
++;
1188 else if (( pCbSign
[codebook
] == 1 ) && ( codebook
>= 11 )) { /* possibly there follow some sign bits and maybe one or two escape sequences after the cw-body */
1189 /* PCW_Body, PCW_Sign and maybe PCW_Escape */
1190 /*=========================================*/
1192 for ( curExtSortCwInSec
= pNumExtendedSortedCodewordInSection
[numExtendedSortedCodewordInSectionIdx
] ; curExtSortCwInSec
!= 0; curExtSortCwInSec
--)
1197 /* decode PCW_BODY */
1198 pQuantVal
= DecodePCW_Body(bs
,
1201 pLeftStartOfSegment
,
1202 pRemainingBitsInSegment
,
1206 err
= DecodePCW_Sign( bs
,
1209 pQuantizedSpectralCoefficients
,
1210 &quantizedSpectralCoefficientsIdx
,
1211 pLeftStartOfSegment
,
1212 pRemainingBitsInSegment
,
1219 /* decode PCW_ESCAPE if present */
1220 quantizedSpectralCoefficientsIdx
-= DIMENSION_OF_ESCAPE_CODEBOOK
;
1222 if ( fixp_abs(pQuantizedSpectralCoefficients
[quantizedSpectralCoefficientsIdx
]) == (FIXP_DBL
)ESCAPE_VALUE
) {
1223 pQuantizedSpectralCoefficients
[quantizedSpectralCoefficientsIdx
] = (FIXP_DBL
) DecodeEscapeSequence( bs
,
1224 pQuantizedSpectralCoefficients
[quantizedSpectralCoefficientsIdx
],
1225 pLeftStartOfSegment
,
1226 pRemainingBitsInSegment
,
1230 quantizedSpectralCoefficientsIdx
++;
1231 if (quantizedSpectralCoefficientsIdx
>= 1024) {
1235 if ( fixp_abs(pQuantizedSpectralCoefficients
[quantizedSpectralCoefficientsIdx
]) == (FIXP_DBL
)ESCAPE_VALUE
) {
1236 pQuantizedSpectralCoefficients
[quantizedSpectralCoefficientsIdx
] = (FIXP_DBL
) DecodeEscapeSequence( bs
,
1237 pQuantizedSpectralCoefficients
[quantizedSpectralCoefficientsIdx
],
1238 pLeftStartOfSegment
,
1239 pRemainingBitsInSegment
,
1243 quantizedSpectralCoefficientsIdx
++;
1244 if (quantizedSpectralCoefficientsIdx
>= 1024) {
1248 /* one more PCW should be decoded */
1250 #if DETECT_TOO_LONG_CW_READS
1251 if ( maxAllowedCwLen
< (numDecodedBits
+ ERROR_PCW_BODY_SIGN_ESC_TOO_LONG
) ) {
1252 pHcr
->decInOut
.errorLog
|= TOO_MANY_PCW_BODY_SIGN_ESC_BITS_DECODED
;
1256 #if CHECK_SEGMENTATION_IMMEDIATELY
1257 if (1 == errDetectPcwSegmentation(*pRemainingBitsInSegment
-ERROR_PCW_BODY_SIGN_ESC
,pHcr
,PCW_BODY_SIGN_ESC
,pQuantizedSpectralCoefficients
+quantizedSpectralCoefficientsIdx
-DIMENSION_OF_ESCAPE_CODEBOOK
,DIMENSION_OF_ESCAPE_CODEBOOK
)) {
1261 pLeftStartOfSegment
++;
1262 pRemainingBitsInSegment
++;
1266 /* all PCWs belonging to this extended section should be decoded */
1267 numExtendedSortedCodewordInSectionIdx
++;
1268 if (numExtendedSortedCodewordInSectionIdx
>= MAX_SFB_HCR
+MAX_HCR_SETS
) {
1272 /* all PCWs should be decoded */
1274 numExtendedSortedSectionsInSetsIdx
++;
1275 if (numExtendedSortedSectionsInSetsIdx
>= MAX_HCR_SETS
) {
1279 /* Write back indexes into structure */
1280 pHcr
->sectionInfo
.numExtendedSortedCodewordInSectionIdx
= numExtendedSortedCodewordInSectionIdx
;
1281 pHcr
->sectionInfo
.extendedSortedCodebookIdx
= extendedSortedCodebookIdx
;
1282 pHcr
->sectionInfo
.numExtendedSortedSectionsInSetsIdx
= numExtendedSortedSectionsInSetsIdx
;
1283 pHcr
->decInOut
.quantizedSpectralCoefficientsIdx
= quantizedSpectralCoefficientsIdx
;
1284 pHcr
->sectionInfo
.maxLenOfCbInExtSrtSecIdx
= maxLenOfCbInExtSrtSecIdx
;
1287 #if CHECK_SEGMENTATION_IMMEDIATELY
1288 /*---------------------------------------------------------------------------------------------
1289 description: This function checks immediately after every decoded PCW, whether out of
1290 the current segment too many bits have been read or not. If an error occurrs,
1291 probably the sideinfo or the HCR-bitstream block holding the huffman
1292 encoded quantized spectral coefficients is distorted. In this case the two
1293 or four quantized spectral coefficients belonging to the current codeword
1294 are marked (for being detected by concealment later).
1295 -------------------------------------------------------------------------------------------- */
1296 static UCHAR
errDetectPcwSegmentation(SCHAR remainingBitsInSegment
,
1299 FIXP_DBL
*qsc_base_of_cw
,
1303 if ( remainingBitsInSegment
< 0 ) {
1307 pHcr
->decInOut
.errorLog
|= SEGMENT_OVERRIDE_ERR_PCW_BODY
;
1310 pHcr
->decInOut
.errorLog
|= SEGMENT_OVERRIDE_ERR_PCW_BODY_SIGN
;
1312 case PCW_BODY_SIGN_ESC
:
1313 pHcr
->decInOut
.errorLog
|= SEGMENT_OVERRIDE_ERR_PCW_BODY_SIGN_ESC
;
1316 /* mark the erred lines */
1317 for ( i
= dimension
; i
!= 0; i
-- ) {
1318 *qsc_base_of_cw
++ = (FIXP_DBL
) Q_VALUE_INVALID
;
1326 #if CHECK_SEGMENTATION_FINAL
1327 /*---------------------------------------------------------------------------------------------
1328 description: This function checks if all segments are empty after decoding. There
1329 are _no lines markded_ as invalid because it could not be traced back
1330 where from the remaining bits are.
1331 -------------------------------------------------------------------------------------------- */
1332 static void errDetectWithinSegmentationFinal(H_HCR_INFO pHcr
)
1334 UCHAR segmentationErrorFlag
= 0;
1336 SCHAR
*pRemainingBitsInSegment
= pHcr
->segmentInfo
.pRemainingBitsInSegment
;
1337 UINT numSegment
= pHcr
->segmentInfo
.numSegment
;
1339 for ( i
=numSegment
; i
!= 0 ; i
--) {
1340 if (*pRemainingBitsInSegment
++ != 0) {
1341 segmentationErrorFlag
= 1;
1344 if (segmentationErrorFlag
== 1) {
1345 pHcr
->decInOut
.errorLog
|= BIT_IN_SEGMENTATION_ERROR
;
1350 /*---------------------------------------------------------------------------------------------
1351 description: This function walks one step within the decoding tree. Which branch is
1352 taken depends on the decoded carryBit input parameter.
1353 -------------------------------------------------------------------------------------------- */
1354 void CarryBitToBranchValue(UCHAR carryBit
,
1359 if (carryBit
== 0) {
1360 *branchNode
= (treeNode
& MASK_LEFT
) >> LEFT_OFFSET
; /* MASK_LEFT: 00FFF000 */
1363 *branchNode
= treeNode
& MASK_RIGHT
; /* MASK_RIGHT: 00000FFF */
1366 *branchValue
= *branchNode
& CLR_BIT_10
; /* clear bit 10 (if set) */
1370 /*---------------------------------------------------------------------------------------------
1371 description: Decodes the body of a priority codeword (PCW)
1372 -----------------------------------------------------------------------------------------------
1373 return: - return value is pointer to first of two or four quantized spectral
1375 -------------------------------------------------------------------------------------------- */
1376 static const SCHAR
*DecodePCW_Body(HANDLE_FDK_BITSTREAM bs
,
1377 const UINT
*pCurrentTree
,
1378 const SCHAR
*pQuantValBase
,
1379 USHORT
*pLeftStartOfSegment
,
1380 SCHAR
*pRemainingBitsInSegment
,
1381 int *pNumDecodedBits
1388 const SCHAR
*pQuantVal
;
1390 /* decode PCW_BODY */
1391 treeNode
= *pCurrentTree
; /* get first node of current tree belonging to current codebook */
1393 /* decode whole PCW-codeword-body */
1396 carryBit
= HcrGetABitFromBitstream(bs
,
1397 pLeftStartOfSegment
,
1398 pLeftStartOfSegment
, /* dummy */
1399 FROM_LEFT_TO_RIGHT
);
1400 *pRemainingBitsInSegment
-= 1;
1401 *pNumDecodedBits
+= 1;
1403 CarryBitToBranchValue(carryBit
,
1408 if ((branchNode
& TEST_BIT_10
) == TEST_BIT_10
) { /* test bit 10 ; if set --> codeword-body is complete */
1409 break; /* end of branch in tree reached i.e. a whole PCW-Body is decoded */
1412 treeNode
= *(pCurrentTree
+ branchValue
); /* update treeNode for further step in decoding tree */
1417 pQuantVal
= pQuantValBase
+ branchValue
; /* update pointer to valid first of 2 or 4 quantized values */
1423 /*---------------------------------------------------------------------------------------------
1424 description: This function decodes one escape sequence. In case of a escape codebook
1425 and in case of the absolute value of the quantized spectral value == 16,
1426 a escapeSequence is decoded in two steps:
1429 -------------------------------------------------------------------------------------------- */
1431 static INT
DecodeEscapeSequence(HANDLE_FDK_BITSTREAM bs
,
1433 USHORT
*pLeftStartOfSegment
,
1434 SCHAR
*pRemainingBitsInSegment
,
1435 int *pNumDecodedBits
1440 UINT escapeOnesCounter
= 0;
1442 INT escape_word
= 0;
1444 /* decode escape prefix */
1446 carryBit
= HcrGetABitFromBitstream(bs
,
1447 pLeftStartOfSegment
,
1448 pLeftStartOfSegment
, /* dummy */
1449 FROM_LEFT_TO_RIGHT
);
1450 *pRemainingBitsInSegment
-= 1;
1451 *pNumDecodedBits
+= 1;
1453 if (carryBit
!= 0) {
1454 escapeOnesCounter
+= 1;
1457 escapeOnesCounter
+= 4;
1462 /* decode escape word */
1463 for( i
=escapeOnesCounter
; i
!= 0 ; i
-- ) {
1464 carryBit
= HcrGetABitFromBitstream(bs
,
1465 pLeftStartOfSegment
,
1466 pLeftStartOfSegment
, /* dummy */
1467 FROM_LEFT_TO_RIGHT
);
1468 *pRemainingBitsInSegment
-= 1;
1469 *pNumDecodedBits
+= 1;
1472 escape_word
= escape_word
| carryBit
;
1475 sign
= (quantSpecCoef
>= 0) ? 1 : -1;
1477 quantSpecCoef
= sign
* (((INT
) 1 << escapeOnesCounter
) + escape_word
);
1479 return quantSpecCoef
;
1483 /*---------------------------------------------------------------------------------------------
1484 description: Decodes the Signbits of a priority codeword (PCW) and writes out the
1485 resulting quantized spectral values into unsorted sections
1486 -----------------------------------------------------------------------------------------------
1487 output: - two or four lines at position in corresponding section (which are not
1488 located at the desired position, i.e. they must be reordered in the last
1489 of eight function of HCR)
1490 -----------------------------------------------------------------------------------------------
1491 return: - updated pQuantSpecCoef pointer (to next empty storage for a line)
1492 -------------------------------------------------------------------------------------------- */
1493 static int DecodePCW_Sign(HANDLE_FDK_BITSTREAM bs
,
1495 const SCHAR
*pQuantVal
,
1496 FIXP_DBL
*pQuantSpecCoef
,
1497 int *quantSpecCoefIdx
,
1498 USHORT
*pLeftStartOfSegment
,
1499 SCHAR
*pRemainingBitsInSegment
,
1500 int *pNumDecodedBits
1507 for( i
=codebookDim
; i
!= 0 ; i
-- ) {
1508 quantSpecCoef
= *pQuantVal
++;
1509 if (quantSpecCoef
!= 0) {
1510 carryBit
= HcrGetABitFromBitstream(bs
,
1511 pLeftStartOfSegment
,
1512 pLeftStartOfSegment
, /* dummy */
1513 FROM_LEFT_TO_RIGHT
);
1514 *pRemainingBitsInSegment
-= 1;
1515 *pNumDecodedBits
+= 1;
1516 if (*pRemainingBitsInSegment
< 0 || *pNumDecodedBits
>= (1024>>1)) {
1520 /* adapt sign of values according to the decoded sign bit */
1521 if (carryBit
!= 0) {
1522 pQuantSpecCoef
[*quantSpecCoefIdx
] = -(FIXP_DBL
)quantSpecCoef
;
1525 pQuantSpecCoef
[*quantSpecCoefIdx
] = (FIXP_DBL
)quantSpecCoef
;
1529 pQuantSpecCoef
[*quantSpecCoefIdx
] = FL2FXCONST_DBL(0.0f
);
1531 *quantSpecCoefIdx
+= 1 ;
1532 if (*quantSpecCoefIdx
>= 1024) {
1540 /*---------------------------------------------------------------------------------------------
1541 description: Mutes spectral lines which have been marked as erroneous (Q_VALUE_INVALID)
1542 -------------------------------------------------------------------------------------------- */
1543 void HcrMuteErroneousLines(H_HCR_INFO hHcr
)
1546 FIXP_DBL
*RESTRICT pLong
= SPEC_LONG(hHcr
->decInOut
.pQuantizedSpectralCoefficientsBase
);
1548 /* if there is a line with value Q_VALUE_INVALID mute it */
1549 for (c
= 0; c
< 1024; c
++) {
1550 if (pLong
[c
] == (FIXP_DBL
)Q_VALUE_INVALID
) {
1551 #if HCR_LISTEN_TO_MUTED_LINES
1552 pLong
[c
] = (FIXP_DBL
)HCR_DIRAC
; /* marking */
1554 pLong
[c
] = FL2FXCONST_DBL(0.0f
); /* muting */
1561 /*---------------------------------------------------------------------------------------------
1562 description: Sets global HCR type
1563 -------------------------------------------------------------------------------------------- */
1564 void setHcrType(H_HCR_INFO hHcr
, MP4_ELEMENT_ID type
)
1568 hHcr
->globalHcrType
= 0;
1571 hHcr
->globalHcrType
= 1;
1579 /*---------------------------------------------------------------------------------------------
1580 description: Gets HCR type from the HCR data structure
1581 -----------------------------------------------------------------------------------------------
1582 return: - global HCR type
1583 -------------------------------------------------------------------------------------------- */
1584 INT
getHcrType(H_HCR_INFO hHcr
)
1586 return hHcr
->globalHcrType
;