1
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5 � Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
6 All rights reserved.
7
8 1. INTRODUCTION
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
17
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
24
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
28
29 2. COPYRIGHT LICENSE
30
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
33
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
36
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
41
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
47
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and the date of any change. For modified versions of the FDK AAC Codec, the term
50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
52
53 3. NO PATENT LICENSE
54
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
58
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61
62 4. DISCLAIMER
63
64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
72
73 5. CONTACT INFORMATION
74
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
83
84 /***************************** MPEG-4 AAC Decoder ***************************
85
86 Author(s): Robert Weidner (DSP Solutions)
87 Description: HCR Decoder: HCR initialization, preprocess HCR sideinfo,
88 decode priority codewords (PCWs)
89
90 *******************************************************************************/
91
92 #include "aacdec_hcr.h"
93
94
95
96 #include "aacdec_hcr_types.h"
97 #include "aacdec_hcr_bit.h"
98 #include "aacdec_hcrs.h"
99 #include "aac_ram.h"
100 #include "aac_rom.h"
101 #include "channel.h"
102 #include "block.h"
103
104 #include "aacdecoder.h" /* for ID_CPE, ID_SCE ... */
105 #include "FDK_bitstream.h"
106
107 extern int mlFileChCurr;
108
109 static void errDetectorInHcrSideinfoShrt(SCHAR cb,
110 SHORT numLine,
111 UINT *errorWord);
112
113 static void errDetectorInHcrLengths(SCHAR lengthOfLongestCodeword,
114 SHORT lengthOfReorderedSpectralData,
115 UINT *errorWord);
116
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);
121
122 static void DeriveNumberOfExtendedSortedSectionsInSets(UINT numSegment,
123 USHORT *pNumExtendedSortedCodewordInSection,
124 int numExtendedSortedCodewordInSectionIdx,
125 USHORT *pNumExtendedSortedSectionsInSets,
126 int numExtendedSortedSectionsInSetsIdx);
127
128 static INT DecodeEscapeSequence(HANDLE_FDK_BITSTREAM bs,
129 INT quantSpecCoef,
130 USHORT *pLeftStartOfSegment,
131 SCHAR *pRemainingBitsInSegment,
132 int *pNumDecodedBits
133 );
134
135 static int DecodePCW_Sign(HANDLE_FDK_BITSTREAM bs,
136 UINT codebookDim,
137 const SCHAR *pQuantVal,
138 FIXP_DBL *pQuantSpecCoef,
139 int *quantSpecCoefIdx,
140 USHORT *pLeftStartOfSegment,
141 SCHAR *pRemainingBitsInSegment,
142 int *pNumDecodedBits
143 );
144
145 static const SCHAR *DecodePCW_Body(HANDLE_FDK_BITSTREAM bs,
146 const UINT *pCurrentTree,
147 const SCHAR *pQuantValBase,
148 USHORT *pLeftStartOfSegment,
149 SCHAR *pRemainingBitsInSegment,
150 int *pNumDecodedBits
151 );
152
153 static void DecodePCWs(HANDLE_FDK_BITSTREAM bs, H_HCR_INFO pHcr);
154
155 static void HcrReorderQuantizedSpectralCoefficients(
156 H_HCR_INFO pHcr,
157 CAacDecoderChannelInfo *pAacDecoderChannelInfo,
158 const SamplingRateInfo *pSamplingRateInfo
159 );
160
161
162 #if CHECK_SEGMENTATION_IMMEDIATELY
163 static UCHAR errDetectPcwSegmentation(SCHAR remainingBitsInSegment,
164 H_HCR_INFO pHcr,
165 PCW_TYPE kind,
166 FIXP_DBL *qsc_base_of_cw,
167 UCHAR dimension);
168 #endif
169
170 #if CHECK_SEGMENTATION_FINAL
171 static void errDetectWithinSegmentationFinal(H_HCR_INFO pHcr);
172 #endif
173
174 /*---------------------------------------------------------------------------------------------
175 description: Check if codebook and numSect are within allowed range (short only)
176 -------------------------------------------------------------------------------------------- */
errDetectorInHcrSideinfoShrt(SCHAR cb,SHORT numLine,UINT * errorWord)177 static void errDetectorInHcrSideinfoShrt(SCHAR cb, SHORT numLine,UINT* errorWord)
178 {
179
180
181
182 if ( cb < ZERO_HCB || cb >= MAX_CB_CHECK || cb == BOOKSCL ) {
183 *errorWord |= CB_OUT_OF_RANGE_SHORT_BLOCK;
184 }
185 if ( numLine < 0 || numLine > 1024 ) {
186 *errorWord |= LINE_IN_SECT_OUT_OF_RANGE_SHORT_BLOCK;
187 }
188 }
189
190 /*---------------------------------------------------------------------------------------------
191 description: Check both HCR lengths
192 -------------------------------------------------------------------------------------------- */
errDetectorInHcrLengths(SCHAR lengthOfLongestCodeword,SHORT lengthOfReorderedSpectralData,UINT * errorWord)193 static void errDetectorInHcrLengths(SCHAR lengthOfLongestCodeword,
194 SHORT lengthOfReorderedSpectralData,
195 UINT *errorWord)
196 {
197 if ( lengthOfReorderedSpectralData < lengthOfLongestCodeword ) {
198 *errorWord |= HCR_SI_LENGTHS_FAILURE;
199 }
200 }
201
202 /*---------------------------------------------------------------------------------------------
203 description: Decode (and adapt if necessary) the two HCR sideinfo components:
204 'reordered_spectral_data_length' and 'longest_codeword_length'
205 -------------------------------------------------------------------------------------------- */
206
CHcr_Read(HANDLE_FDK_BITSTREAM bs,CAacDecoderChannelInfo * pAacDecoderChannelInfo)207 void CHcr_Read(HANDLE_FDK_BITSTREAM bs,
208 CAacDecoderChannelInfo *pAacDecoderChannelInfo)
209 {
210 INT globalHcrType = getHcrType(&pAacDecoderChannelInfo->pComData->overlay.aac.erHcrInfo);
211 SHORT lengOfReorderedSpectralData;
212 SCHAR lengOfLongestCodeword;
213
214 pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfReorderedSpectralData = 0;
215 pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfLongestCodeword = 0;
216
217
218
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 */
224 }
225 else {
226 if (lengOfReorderedSpectralData > CPE_TOP_LENGTH) {
227 pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfReorderedSpectralData = CPE_TOP_LENGTH; /* use valid maximum */
228 }
229 }
230 }
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 */
234 }
235 else {
236 if (lengOfReorderedSpectralData > SCE_TOP_LENGTH) {
237 pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfReorderedSpectralData = SCE_TOP_LENGTH; /* use valid maximum */
238 }
239 }
240 }
241
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 */
246 }
247 else {
248 if (lengOfLongestCodeword > LEN_OF_LONGEST_CW_TOP_LENGTH) {
249 pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfLongestCodeword = LEN_OF_LONGEST_CW_TOP_LENGTH; /* use valid maximum */
250 }
251 }
252 }
253
254
255 /*---------------------------------------------------------------------------------------------
256 description: Sets up HCR ROM-Tables
257 -------------------------------------------------------------------------------------------- */
258
HcrInitRom(H_HCR_INFO pHcr)259 void HcrInitRom(H_HCR_INFO pHcr)
260 {
261 pHcr->cbPairs.pMinOfCbPair = aMinOfCbPair;
262 pHcr->cbPairs.pMaxOfCbPair = aMaxOfCbPair;
263
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;
270 }
271
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 -----------------------------------------------------------------------------------------------
277 return: error log
278 -------------------------------------------------------------------------------------------- */
279
HcrInit(H_HCR_INFO pHcr,CAacDecoderChannelInfo * pAacDecoderChannelInfo,const SamplingRateInfo * pSamplingRateInfo,HANDLE_FDK_BITSTREAM bs)280 UINT HcrInit(H_HCR_INFO pHcr,
281 CAacDecoderChannelInfo *pAacDecoderChannelInfo,
282 const SamplingRateInfo *pSamplingRateInfo,
283 HANDLE_FDK_BITSTREAM bs)
284 {
285 CIcsInfo *pIcsInfo = &pAacDecoderChannelInfo->icsInfo;
286 SHORT *pNumLinesInSec;
287 UCHAR *pCodeBk;
288 SHORT numSection;
289 SCHAR cb;
290 int numLine;
291 int i;
292
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);
302
303 FDKsyncCache(bs);
304 pHcr->decInOut.bitstreamIndex = FDKgetBitCnt(bs);
305
306 if (!IsLongBlock(&pAacDecoderChannelInfo->icsInfo)) /* short block */
307 {
308 SHORT band;
309 SHORT maxBand;
310 SCHAR group;
311 SCHAR winGroupLen;
312 SCHAR window;
313 SCHAR numUnitInBand;
314 SCHAR cntUnitInBand;
315 SCHAR groupWin;
316 SCHAR cb_prev;
317
318 UCHAR *pCodeBook;
319 const SHORT *BandOffsets;
320 SCHAR numOfGroups;
321
322
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);
328
329 numLine = 0;
330 numSection = 0;
331 cb = pCodeBook[0];
332 cb_prev = pCodeBook[0];
333
334 /* convert HCR-sideinfo into a unitwise manner: When the cb changes, a new section starts */
335
336 *pCodeBk++ = cb_prev;
337
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];
346 if (cb != cb_prev) {
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 );
351 }
352 #endif
353 *pCodeBk++ = cb;
354 *pNumLinesInSec++ = numLine;
355 numSection++;
356
357 cb_prev = cb;
358 numLine = LINES_PER_UNIT;
359 }
360 else {
361 numLine += LINES_PER_UNIT;
362 }
363 }
364 }
365 }
366 }
367
368 numSection++;
369
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;
374 }
375 errDetectorInHcrLengths(pHcr->decInOut.lengthOfLongestCodeword,
376 pHcr->decInOut.lengthOfReorderedSpectralData,
377 &pHcr->decInOut.errorLog);
378 if (pHcr->decInOut.errorLog != 0 ) {
379 return ( pHcr->decInOut.errorLog );
380 }
381 #endif
382
383 *pCodeBk = cb;
384 *pNumLinesInSec = numLine;
385 pHcr->decInOut.numSection = numSection;
386
387 } else /* end short block prepare SI */
388 { /* long block */
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;
398 numSection = 0;
399 }
400
401 for ( i = numSection; i != 0; i-- )
402 {
403 cb = *pCodeBk++;
404
405 if ( cb < ZERO_HCB || cb >= MAX_CB_CHECK || cb == BOOKSCL ) {
406 pHcr->decInOut.errorLog |= CB_OUT_OF_RANGE_LONG_BLOCK;
407 }
408
409 numLine = *pNumLinesInSec++;
410 /* FDK_ASSERT(numLine > 0); */
411
412 if ( (numLine <= 0) || (numLine > 1024) ) {
413 pHcr->decInOut.errorLog |= LINE_IN_SECT_OUT_OF_RANGE_LONG_BLOCK;
414 }
415 }
416 if (pHcr->decInOut.errorLog != 0 ) {
417 return ( pHcr->decInOut.errorLog );
418 }
419 #endif /* CHECK_VALID_HCR_INPUT */
420 }
421
422 pCodeBk = pHcr->decInOut.pCodebook;
423 for ( i = 0; i < numSection; i++ ) {
424 if (
425 (*pCodeBk == NOISE_HCB) ||
426 (*pCodeBk == INTENSITY_HCB2) ||
427 (*pCodeBk == INTENSITY_HCB))
428 {
429 *pCodeBk = 0;
430 }
431 pCodeBk++;
432 }
433
434 /* HCR-sideinfo-input is complete and seems to be valid */
435
436
437
438 return ( pHcr->decInOut.errorLog );
439 }
440
441
442
443
444 #if USE_HCR_DUMMY
445
446 /*---------------------------------------------------------------------------------------------
447
448 description: This HCR - dummy - function writes only a dirac-sequence in output buffer
449
450 -------------------------------------------------------------------------------------------- */
HcrDecoder(H_HCR_INFO pHcr,const CAacDecoderChannelInfo * pAacDecoderChannelInfo,HANDLE_FDK_BITSTREAM bs)451 UINT HcrDecoder(H_HCR_INFO pHcr,
452 const CAacDecoderChannelInfo *pAacDecoderChannelInfo,
453 HANDLE_FDK_BITSTREAM bs)
454 {
455 for (SHORT i=0; i < 1024; i++ ) {
456 pHcr->decInOut.pQuantizedSpectralCoefficients->Long[i] = FL2FXCONST_DBL(0.0f);
457 if ( i % 30 == 0) {
458 pHcr->decInOut.pQuantizedSpectralCoefficients->Long[i] = (FIXP_DBL)HCR_DIRAC;
459 }
460 }
461 return 0;
462 }
463
464 #else /* USE_HCR_DUMMY */
465
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 -------------------------------------------------------------------------------------------- */
471
HcrDecoder(H_HCR_INFO pHcr,CAacDecoderChannelInfo * pAacDecoderChannelInfo,const SamplingRateInfo * pSamplingRateInfo,HANDLE_FDK_BITSTREAM bs)472 UINT HcrDecoder(H_HCR_INFO pHcr,
473 CAacDecoderChannelInfo *pAacDecoderChannelInfo,
474 const SamplingRateInfo *pSamplingRateInfo,
475 HANDLE_FDK_BITSTREAM bs)
476 {
477 int pTmp1, pTmp2, pTmp3, pTmp4;
478 #if DETECT_TOO_LONG_CW_READS
479 int pTmp5;
480 #endif
481
482 INT bitCntOffst;
483 UINT saveBitCnt = FDKgetBitCnt(bs); /* save bitstream position */
484
485 HcrCalcNumCodeword(pHcr);
486
487 HcrSortCodebookAndNumCodewordInSection(pHcr);
488
489 HcrPrepareSegmentationGrid(pHcr);
490
491 HcrExtendedSectionInfo(pHcr);
492
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 */
495 }
496
497 DeriveNumberOfExtendedSortedSectionsInSets(pHcr->segmentInfo.numSegment,
498 pHcr->sectionInfo.pNumExtendedSortedCodewordInSection,
499 pHcr->sectionInfo.numExtendedSortedCodewordInSectionIdx,
500 pHcr->sectionInfo.pNumExtendedSortedSectionsInSets,
501 pHcr->sectionInfo.numExtendedSortedSectionsInSetsIdx);
502
503 /* store */
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;
510 #endif
511
512 /* ------- decode meaningful PCWs ------ */
513 DecodePCWs(bs, pHcr);
514
515 if (( pHcr->decInOut.errorLog & HCR_FATAL_PCW_ERROR_MASK ) == 0 ) {
516 /* ------ decode the non-PCWs -------- */
517 DecodeNonPCWs(bs, pHcr);
518 }
519
520
521 #if CHECK_SEGMENTATION_FINAL
522 errDetectWithinSegmentationFinal(pHcr);
523 #endif
524
525 /* restore */
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;
532 #endif
533
534 HcrReorderQuantizedSpectralCoefficients(pHcr, pAacDecoderChannelInfo, pSamplingRateInfo);
535
536 /* restore bitstream position */
537 bitCntOffst = saveBitCnt - FDKgetBitCnt(bs);
538 if( bitCntOffst ) {
539 FDKpushBiDirectional(bs, bitCntOffst);
540 }
541
542 return ( pHcr->decInOut.errorLog );
543 }
544
545
546 #endif /* USE_HCR_DUMMY */
547
548
549
550
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
555 detected.
556 Additional for short-blocks a unit-based-deinterleaving is applied.
557 Moreover (for short blocks) the scaling is derived (compare plain huffman
558 decoder).
559 -------------------------------------------------------------------------------------------- */
560
HcrReorderQuantizedSpectralCoefficients(H_HCR_INFO pHcr,CAacDecoderChannelInfo * pAacDecoderChannelInfo,const SamplingRateInfo * pSamplingRateInfo)561 static void HcrReorderQuantizedSpectralCoefficients(
562 H_HCR_INFO pHcr, CAacDecoderChannelInfo *pAacDecoderChannelInfo,
563 const SamplingRateInfo *pSamplingRateInfo
564 )
565 {
566 INT qsc;
567 UINT abs_qsc;
568 UINT i,j;
569 USHORT numSpectralValuesInSection;
570 FIXP_DBL *pTeVa;
571 USHORT lavErrorCnt = 0;
572
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;
583
584 FDKmemclear(pTempValues,1024*sizeof(FIXP_DBL));
585
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 */
596 }
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;
600 }
601 else { /* .. because a too high value was decoded for this cb --> set LAV flag */
602 *pTeVa++ = (FIXP_DBL) Q_VALUE_INVALID;
603 lavErrorCnt += 1;
604 }
605 }
606 #else
607 if ( abs_qsc <= pLargestAbsVal[*pSortedCodebook] ) {
608 *pTeVa++ = qsc;
609 }
610 else {
611 *pTeVa++ = Q_VALUE_INVALID;
612 lavErrorCnt += 1;
613 }
614 #endif
615 }
616 pSortedCodebook++;
617 }
618
619 if (!IsLongBlock(&pAacDecoderChannelInfo->icsInfo))
620 {
621 FIXP_DBL *pOut;
622 FIXP_DBL locMax;
623 FIXP_DBL tmp;
624 SCHAR groupoffset;
625 SCHAR group;
626 SCHAR band;
627 SCHAR groupwin;
628 SCHAR window;
629 SCHAR numWinGroup;
630 SHORT interm;
631 SCHAR numSfbTransm;
632 SCHAR winGroupLen;
633 SHORT index;
634 INT msb;
635 INT lsb;
636
637 SHORT *pScaleFacHcr = pAacDecoderChannelInfo->pDynData->aScaleFactor;
638 SHORT *pSfbSclHcr = pAacDecoderChannelInfo->pDynData->aSfbScale;
639 const SHORT *BandOffsets = GetScaleFactorBandOffsets(&pAacDecoderChannelInfo->icsInfo, pSamplingRateInfo);
640
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-- ) {
648 *pOut++ = *pTeVa++;
649 }
650 }
651 }
652
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) */
655 groupoffset = 0;
656
657 numWinGroup = GetWindowGroups(&pAacDecoderChannelInfo->icsInfo);
658 numSfbTransm = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
659
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);
675 }
676 }
677 if ( fixp_abs(locMax) > (FIXP_DBL)MAX_QUANTIZED_VALUE ) {
678 locMax = (FIXP_DBL)MAX_QUANTIZED_VALUE;
679 }
680 pSfbSclHcr[window*16+band] = msb - GetScaleFromValue(locMax, lsb); /* save global scale maxima in this sfb */
681 }
682 }
683 groupoffset += GetWindowGroupLength(&pAacDecoderChannelInfo->icsInfo,group);
684 }
685 } else
686 {
687 /* copy straight for long-blocks */
688 pQuantizedSpectralCoefficients = SPEC_LONG(pQuantizedSpectralCoefficientsBase);
689 for ( i = 1024; i != 0; i-- ) {
690 *pQuantizedSpectralCoefficients++ = *pBak++;
691 }
692 }
693
694 if ( lavErrorCnt != 0 ) {
695 pHcr->decInOut.errorLog |= LAV_VIOLATION;
696 }
697 }
698
699
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
708 hcr tool.
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 -----------------------------------------------------------------------------------------------
713 return: -
714 -------------------------------------------------------------------------------------------- */
715
HcrCalcNumCodeword(H_HCR_INFO pHcr)716 static void HcrCalcNumCodeword(H_HCR_INFO pHcr)
717 {
718 int hcrSection;
719 UINT numCodeword;
720
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;
726
727 numCodeword = 0;
728 for ( hcrSection = numSection; hcrSection != 0; hcrSection-- ) {
729 *pNumCodewordInSection = *pNumLineInSection++ >> pCbDimShift[*pCodebook];
730 if ( *pCodebook != 0 ) {
731 numCodeword += *pNumCodewordInSection;
732 }
733 pNumCodewordInSection++;
734 pCodebook++;
735 }
736 pHcr->sectionInfo.numCodeword = numCodeword;
737 }
738
739
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 -------------------------------------------------------------------------------------------- */
745
HcrSortCodebookAndNumCodewordInSection(H_HCR_INFO pHcr)746 static void HcrSortCodebookAndNumCodewordInSection(H_HCR_INFO pHcr)
747 {
748
749 UINT i,j,k;
750 UCHAR temp;
751 UINT counter;
752 UINT startOffset;
753 UINT numZeroSection;
754 UCHAR *pDest;
755 UINT numSectionDec;
756
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;
768
769 UINT searchStart = 0;
770
771 /* calculate *pNumSortedSection and store the priorities in array pSortedCdebook */
772 pDest = pSortedCodebook;
773 numZeroSection = 0;
774 for ( i=numSection; i != 0; i-- ) {
775 if ( pCbPriority[*pCodebook] == 0 ) {
776 numZeroSection += 1;
777 }
778 *pDest++ = pCbPriority[*pCodebook++];
779 }
780 pHcr->sectionInfo.numSortedSection = numSection - numZeroSection; /* numSortedSection contains no zero or intensity section */
781 pCodebook = pHcr->decInOut.pCodebook;
782
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;
794 }
795 }
796 counter -= 1;
797 }
798 }
799
800 /* clear codebookSwitch array */
801 for ( i = numSection; i != 0; i--) {
802 *pCodebookSwitch++ = 0;
803 }
804 pCodebookSwitch = pHcr->sectionInfo.pCodebookSwitch;
805
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 */
813
814 startOffset = 0;
815 for ( k = 0; k < i; k++ ) { /* make entry in pReorderOffst */
816 startOffset += pNumCodewordInSection[k] << pCbDimShift[pCodebook[k]];
817 }
818 pReorderOffset[j] = startOffset; /* offset for reordering the codewords */
819
820 if(i == searchStart) {
821 UINT k = i;
822 while(pCodebookSwitch[k++] == 1) searchStart++;
823 }
824 break;
825 }
826 }
827 }
828 }
829
830
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 -------------------------------------------------------------------------------------------- */
837
HcrPrepareSegmentationGrid(H_HCR_INFO pHcr)838 static void HcrPrepareSegmentationGrid(H_HCR_INFO pHcr)
839 {
840 USHORT i,j;
841 USHORT numSegment = 0;
842 USHORT segmentStart = 0;
843 UCHAR segmentWidth;
844 UCHAR lastSegmentWidth;
845 UCHAR sortedCodebook;
846 UCHAR endFlag = 0;
847 USHORT intermediateResult;
848
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;
859
860 for ( i=numSortedSection; i != 0; i-- ) {
861 sortedCodebook = *pSortedCodebook++;
862 segmentWidth = FDKmin(pMaxCwLength[sortedCodebook],lengthOfLongestCodeword);
863
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;
873 numSegment += 1;
874 }
875 /* width does not allow a new segment */
876 else {
877 /* correct the last segment length */
878 pLeftStartOfSegment--;
879 pRightStartOfSegment--;
880 pRemainingBitsInSegment--;
881 segmentStart = *pLeftStartOfSegment - bitstreamIndex;
882
883 lastSegmentWidth = lengthOfReorderedSpectralData - segmentStart;
884 *pRemainingBitsInSegment = lastSegmentWidth;
885 *pRightStartOfSegment = bitstreamIndex + segmentStart + lastSegmentWidth - 1;
886 endFlag = 1;
887 break;
888 }
889 }
890 pNumSortedCodewordInSection++;
891 if (endFlag != 0) {
892 break;
893 }
894 }
895 pHcr->segmentInfo.numSegment = numSegment;
896
897 }
898
899
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.
906
907 -------------------------------------------------------------------------------------------- */
908
HcrExtendedSectionInfo(H_HCR_INFO pHcr)909 static void HcrExtendedSectionInfo(H_HCR_INFO pHcr)
910 {
911 UINT srtSecCnt = 0; /* counter for sorted sections */
912 UINT xSrtScCnt = 0; /* counter for extended sorted sections */
913 UINT remainNumCwInSortSec;
914 UINT inSegmentRemainNumCW;
915
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;
926 #endif
927
928 remainNumCwInSortSec = pNumSortedCodewordInSection[srtSecCnt];
929 inSegmentRemainNumCW = numSegment;
930
931 while (srtSecCnt < numSortedSection) {
932 if (inSegmentRemainNumCW < remainNumCwInSortSec) {
933
934 pNumExtSortCwInSect[xSrtScCnt] = inSegmentRemainNumCW;
935 pExtendedSortedCoBo[xSrtScCnt] = pSortedCodebook[srtSecCnt];
936
937 remainNumCwInSortSec -= inSegmentRemainNumCW;
938 inSegmentRemainNumCW = numSegment;
939 /* data of a sorted section was not integrated in extended sorted section */
940 }
941 else if (inSegmentRemainNumCW == remainNumCwInSortSec) {
942 pNumExtSortCwInSect[xSrtScCnt] = inSegmentRemainNumCW;
943 pExtendedSortedCoBo[xSrtScCnt] = pSortedCodebook[srtSecCnt];
944
945 srtSecCnt++;
946 remainNumCwInSortSec = pNumSortedCodewordInSection[srtSecCnt];
947 inSegmentRemainNumCW = numSegment;
948 /* data of a sorted section was integrated in extended sorted section */
949 }
950 else { /* inSegmentRemainNumCW > remainNumCwInSortSec */
951 pNumExtSortCwInSect[xSrtScCnt] = remainNumCwInSortSec;
952 pExtendedSortedCoBo[xSrtScCnt] = pSortedCodebook[srtSecCnt];
953
954
955 inSegmentRemainNumCW -= remainNumCwInSortSec;
956 srtSecCnt++;
957 remainNumCwInSortSec = pNumSortedCodewordInSection[srtSecCnt];
958 /* data of a sorted section was integrated in extended sorted section */
959 }
960 #if DETECT_TOO_LONG_CW_READS
961 pMaxLenOfCbInExtSrtSec[xSrtScCnt] = FDKmin(pMaxCwLength[pExtendedSortedCoBo[xSrtScCnt]],lengthOfLongestCodeword);
962 #endif
963
964
965
966 xSrtScCnt += 1;
967
968 if ( xSrtScCnt >= (MAX_SFB_HCR + MAX_HCR_SETS) ) {
969 pHcr->decInOut.errorLog |= EXTENDED_SORTED_COUNTER_OVERFLOW;
970 return;
971 }
972
973 }
974 pNumExtSortCwInSect[xSrtScCnt] = 0;
975
976 }
977
978
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.
984
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 -------------------------------------------------------------------------------------------- */
DeriveNumberOfExtendedSortedSectionsInSets(UINT numSegment,USHORT * pNumExtendedSortedCodewordInSection,int numExtendedSortedCodewordInSectionIdx,USHORT * pNumExtendedSortedSectionsInSets,int numExtendedSortedSectionsInSetsIdx)991 static void DeriveNumberOfExtendedSortedSectionsInSets(UINT numSegment,
992 USHORT *pNumExtendedSortedCodewordInSection,
993 int numExtendedSortedCodewordInSectionIdx,
994 USHORT *pNumExtendedSortedSectionsInSets,
995 int numExtendedSortedSectionsInSetsIdx)
996 {
997 USHORT counter = 0;
998 UINT cwSum = 0;
999 USHORT *pNumExSortCwInSec = pNumExtendedSortedCodewordInSection;
1000 USHORT *pNumExSortSecInSets = pNumExtendedSortedSectionsInSets;
1001
1002 while (pNumExSortCwInSec[numExtendedSortedCodewordInSectionIdx] != 0)
1003 {
1004 cwSum += pNumExSortCwInSec[numExtendedSortedCodewordInSectionIdx];
1005 numExtendedSortedCodewordInSectionIdx++;
1006 if (numExtendedSortedCodewordInSectionIdx >= (MAX_SFB_HCR+MAX_HCR_SETS)) {
1007 return;
1008 }
1009 if (cwSum > numSegment) {
1010 return;
1011 }
1012 counter++;
1013 if (counter > 1024/4) {
1014 return;
1015 }
1016 if ( cwSum == numSegment ) {
1017 pNumExSortSecInSets[numExtendedSortedSectionsInSetsIdx] = counter;
1018 numExtendedSortedSectionsInSetsIdx++;
1019 if (numExtendedSortedSectionsInSetsIdx >= MAX_HCR_SETS) {
1020 return;
1021 }
1022 counter = 0;
1023 cwSum = 0;
1024 }
1025 }
1026 pNumExSortSecInSets[numExtendedSortedSectionsInSetsIdx] = counter; /* save last entry for the last - probably shorter - set */
1027 }
1028
1029
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
1042 one.
1043 -------------------------------------------------------------------------------------------- */
DecodePCWs(HANDLE_FDK_BITSTREAM bs,H_HCR_INFO pHcr)1044 static void DecodePCWs(HANDLE_FDK_BITSTREAM bs, H_HCR_INFO pHcr)
1045 {
1046 UINT i;
1047 USHORT extSortSec;
1048 USHORT curExtSortCwInSec;
1049 UCHAR codebook;
1050 UCHAR dimension;
1051 const UINT *pCurrentTree;
1052 const SCHAR *pQuantValBase;
1053 const SCHAR *pQuantVal;
1054
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;
1069 int numDecodedBits;
1070 #endif
1071 const UCHAR *pCbDimension = pHcr->tableInfo.pCbDimension;
1072 const UCHAR *pCbSign = pHcr->tableInfo.pCbSign;
1073
1074 /* clear result array */
1075 //pQSC = &pQuantizedSpectralCoefficients[quantizedSpectralCoefficientsIdx];
1076 //pQSC = *pQuantizedSpectralCoefficients;
1077
1078 FDKmemclear(pQuantizedSpectralCoefficients+quantizedSpectralCoefficientsIdx,1024*sizeof(FIXP_DBL));
1079
1080 /* decode all PCWs in the extended sorted section(s) belonging to set 0 */
1081 for ( extSortSec = pNumExtendedSortedSectionsInSets[numExtendedSortedSectionsInSetsIdx]; extSortSec != 0; extSortSec-- ) {
1082
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)) {
1086 return;
1087 }
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)) {
1095 return;
1096 }
1097 #endif
1098
1099 /* switch for decoding with different codebooks: */
1100 if ( pCbSign[codebook] == 0 ) { /* no sign bits follow after the codeword-body */
1101 /* PCW_BodyONLY */
1102 /*==============*/
1103
1104 for ( curExtSortCwInSec = pNumExtendedSortedCodewordInSection[numExtendedSortedCodewordInSectionIdx] ; curExtSortCwInSec != 0; curExtSortCwInSec--) {
1105 numDecodedBits = 0;
1106
1107 /* decode PCW_BODY */
1108 pQuantVal = DecodePCW_Body(bs,
1109 pCurrentTree,
1110 pQuantValBase,
1111 pLeftStartOfSegment,
1112 pRemainingBitsInSegment,
1113 &numDecodedBits
1114 );
1115
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) {
1121 return;
1122 }
1123 }
1124
1125 /* one more PCW should be decoded */
1126
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;
1130 }
1131 #endif
1132
1133 #if CHECK_SEGMENTATION_IMMEDIATELY
1134 if (1 == errDetectPcwSegmentation(*pRemainingBitsInSegment-ERROR_PCW_BODY,pHcr,PCW_BODY,pQuantizedSpectralCoefficients+quantizedSpectralCoefficientsIdx-dimension,dimension)) {
1135 return;
1136 }
1137 #endif
1138 pLeftStartOfSegment++; /* update pointer for decoding the next PCW */
1139 pRemainingBitsInSegment++; /* update pointer for decoding the next PCW */
1140 }
1141 }
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 /*=======================*/
1145
1146 for ( curExtSortCwInSec = pNumExtendedSortedCodewordInSection[numExtendedSortedCodewordInSectionIdx] ; curExtSortCwInSec != 0; curExtSortCwInSec--)
1147 {
1148 int err;
1149 numDecodedBits = 0;
1150
1151 pQuantVal = DecodePCW_Body(bs,
1152 pCurrentTree,
1153 pQuantValBase,
1154 pLeftStartOfSegment,
1155 pRemainingBitsInSegment,
1156 &numDecodedBits
1157 );
1158
1159 err = DecodePCW_Sign( bs,
1160 dimension,
1161 pQuantVal,
1162 pQuantizedSpectralCoefficients,
1163 &quantizedSpectralCoefficientsIdx,
1164 pLeftStartOfSegment,
1165 pRemainingBitsInSegment,
1166 &numDecodedBits
1167 );
1168 if (err != 0) {
1169 return;
1170 }
1171 /* one more PCW should be decoded */
1172
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;
1176 }
1177 #endif
1178
1179 #if CHECK_SEGMENTATION_IMMEDIATELY
1180 if (1 == errDetectPcwSegmentation(*pRemainingBitsInSegment-ERROR_PCW_BODY_SIGN,pHcr,PCW_BODY_SIGN, pQuantizedSpectralCoefficients+quantizedSpectralCoefficientsIdx-dimension,dimension)) {
1181 return;
1182 }
1183 #endif
1184 pLeftStartOfSegment++;
1185 pRemainingBitsInSegment++;
1186 }
1187 }
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 /*=========================================*/
1191
1192 for ( curExtSortCwInSec = pNumExtendedSortedCodewordInSection[numExtendedSortedCodewordInSectionIdx] ; curExtSortCwInSec != 0; curExtSortCwInSec--)
1193 {
1194 int err;
1195 numDecodedBits = 0;
1196
1197 /* decode PCW_BODY */
1198 pQuantVal = DecodePCW_Body(bs,
1199 pCurrentTree,
1200 pQuantValBase,
1201 pLeftStartOfSegment,
1202 pRemainingBitsInSegment,
1203 &numDecodedBits
1204 );
1205
1206 err = DecodePCW_Sign( bs,
1207 dimension,
1208 pQuantVal,
1209 pQuantizedSpectralCoefficients,
1210 &quantizedSpectralCoefficientsIdx,
1211 pLeftStartOfSegment,
1212 pRemainingBitsInSegment,
1213 &numDecodedBits
1214 );
1215 if (err != 0) {
1216 return;
1217 }
1218
1219 /* decode PCW_ESCAPE if present */
1220 quantizedSpectralCoefficientsIdx -= DIMENSION_OF_ESCAPE_CODEBOOK;
1221
1222 if ( fixp_abs(pQuantizedSpectralCoefficients[quantizedSpectralCoefficientsIdx]) == (FIXP_DBL)ESCAPE_VALUE ) {
1223 pQuantizedSpectralCoefficients[quantizedSpectralCoefficientsIdx] = (FIXP_DBL) DecodeEscapeSequence( bs,
1224 pQuantizedSpectralCoefficients[quantizedSpectralCoefficientsIdx],
1225 pLeftStartOfSegment,
1226 pRemainingBitsInSegment,
1227 &numDecodedBits
1228 );
1229 }
1230 quantizedSpectralCoefficientsIdx++;
1231 if (quantizedSpectralCoefficientsIdx >= 1024) {
1232 return;
1233 }
1234
1235 if ( fixp_abs(pQuantizedSpectralCoefficients[quantizedSpectralCoefficientsIdx]) == (FIXP_DBL)ESCAPE_VALUE ) {
1236 pQuantizedSpectralCoefficients[quantizedSpectralCoefficientsIdx] = (FIXP_DBL) DecodeEscapeSequence( bs,
1237 pQuantizedSpectralCoefficients[quantizedSpectralCoefficientsIdx],
1238 pLeftStartOfSegment,
1239 pRemainingBitsInSegment,
1240 &numDecodedBits
1241 );
1242 }
1243 quantizedSpectralCoefficientsIdx++;
1244 if (quantizedSpectralCoefficientsIdx >= 1024) {
1245 return;
1246 }
1247
1248 /* one more PCW should be decoded */
1249
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;
1253 }
1254 #endif
1255
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)) {
1258 return;
1259 }
1260 #endif
1261 pLeftStartOfSegment++;
1262 pRemainingBitsInSegment++;
1263 }
1264 }
1265
1266 /* all PCWs belonging to this extended section should be decoded */
1267 numExtendedSortedCodewordInSectionIdx++;
1268 if (numExtendedSortedCodewordInSectionIdx >= MAX_SFB_HCR+MAX_HCR_SETS) {
1269 return;
1270 }
1271 }
1272 /* all PCWs should be decoded */
1273
1274 numExtendedSortedSectionsInSetsIdx++;
1275 if (numExtendedSortedSectionsInSetsIdx >= MAX_HCR_SETS) {
1276 return;
1277 }
1278
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;
1285 }
1286
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 -------------------------------------------------------------------------------------------- */
errDetectPcwSegmentation(SCHAR remainingBitsInSegment,H_HCR_INFO pHcr,PCW_TYPE kind,FIXP_DBL * qsc_base_of_cw,UCHAR dimension)1296 static UCHAR errDetectPcwSegmentation(SCHAR remainingBitsInSegment,
1297 H_HCR_INFO pHcr,
1298 PCW_TYPE kind,
1299 FIXP_DBL *qsc_base_of_cw,
1300 UCHAR dimension)
1301 {
1302 SCHAR i;
1303 if ( remainingBitsInSegment < 0 ) {
1304 /* log the error */
1305 switch (kind) {
1306 case PCW_BODY:
1307 pHcr->decInOut.errorLog |= SEGMENT_OVERRIDE_ERR_PCW_BODY;
1308 break;
1309 case PCW_BODY_SIGN:
1310 pHcr->decInOut.errorLog |= SEGMENT_OVERRIDE_ERR_PCW_BODY_SIGN;
1311 break;
1312 case PCW_BODY_SIGN_ESC:
1313 pHcr->decInOut.errorLog |= SEGMENT_OVERRIDE_ERR_PCW_BODY_SIGN_ESC;
1314 break;
1315 }
1316 /* mark the erred lines */
1317 for ( i = dimension; i != 0; i-- ) {
1318 *qsc_base_of_cw++ = (FIXP_DBL) Q_VALUE_INVALID;
1319 }
1320 return 1;
1321 }
1322 return 0;
1323 }
1324 #endif
1325
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 -------------------------------------------------------------------------------------------- */
errDetectWithinSegmentationFinal(H_HCR_INFO pHcr)1332 static void errDetectWithinSegmentationFinal(H_HCR_INFO pHcr)
1333 {
1334 UCHAR segmentationErrorFlag = 0;
1335 USHORT i;
1336 SCHAR *pRemainingBitsInSegment = pHcr->segmentInfo.pRemainingBitsInSegment;
1337 UINT numSegment = pHcr->segmentInfo.numSegment;
1338
1339 for ( i=numSegment; i != 0 ; i--) {
1340 if (*pRemainingBitsInSegment++ != 0) {
1341 segmentationErrorFlag = 1;
1342 }
1343 }
1344 if (segmentationErrorFlag == 1) {
1345 pHcr->decInOut.errorLog |= BIT_IN_SEGMENTATION_ERROR;
1346 }
1347 }
1348 #endif
1349
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 -------------------------------------------------------------------------------------------- */
CarryBitToBranchValue(UCHAR carryBit,UINT treeNode,UINT * branchValue,UINT * branchNode)1354 void CarryBitToBranchValue(UCHAR carryBit,
1355 UINT treeNode,
1356 UINT *branchValue,
1357 UINT *branchNode)
1358 {
1359 if (carryBit == 0) {
1360 *branchNode = (treeNode & MASK_LEFT) >> LEFT_OFFSET; /* MASK_LEFT: 00FFF000 */
1361 }
1362 else {
1363 *branchNode = treeNode & MASK_RIGHT; /* MASK_RIGHT: 00000FFF */
1364 }
1365
1366 *branchValue = *branchNode & CLR_BIT_10; /* clear bit 10 (if set) */
1367 }
1368
1369
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
1374 coefficients
1375 -------------------------------------------------------------------------------------------- */
DecodePCW_Body(HANDLE_FDK_BITSTREAM bs,const UINT * pCurrentTree,const SCHAR * pQuantValBase,USHORT * pLeftStartOfSegment,SCHAR * pRemainingBitsInSegment,int * pNumDecodedBits)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
1382 )
1383 {
1384 UCHAR carryBit;
1385 UINT branchNode;
1386 UINT treeNode;
1387 UINT branchValue;
1388 const SCHAR *pQuantVal;
1389
1390 /* decode PCW_BODY */
1391 treeNode = *pCurrentTree; /* get first node of current tree belonging to current codebook */
1392
1393 /* decode whole PCW-codeword-body */
1394 while (1) {
1395
1396 carryBit = HcrGetABitFromBitstream(bs,
1397 pLeftStartOfSegment,
1398 pLeftStartOfSegment, /* dummy */
1399 FROM_LEFT_TO_RIGHT);
1400 *pRemainingBitsInSegment -= 1;
1401 *pNumDecodedBits += 1;
1402
1403 CarryBitToBranchValue(carryBit,
1404 treeNode,
1405 &branchValue,
1406 &branchNode);
1407
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 */
1410 }
1411 else {
1412 treeNode = *(pCurrentTree + branchValue); /* update treeNode for further step in decoding tree */
1413 }
1414
1415 }
1416
1417 pQuantVal = pQuantValBase + branchValue; /* update pointer to valid first of 2 or 4 quantized values */
1418
1419 return pQuantVal;
1420 }
1421
1422
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:
1427 1. escape prefix
1428 2. escape word
1429 -------------------------------------------------------------------------------------------- */
1430
DecodeEscapeSequence(HANDLE_FDK_BITSTREAM bs,INT quantSpecCoef,USHORT * pLeftStartOfSegment,SCHAR * pRemainingBitsInSegment,int * pNumDecodedBits)1431 static INT DecodeEscapeSequence(HANDLE_FDK_BITSTREAM bs,
1432 INT quantSpecCoef,
1433 USHORT *pLeftStartOfSegment,
1434 SCHAR *pRemainingBitsInSegment,
1435 int *pNumDecodedBits
1436 )
1437 {
1438 UINT i;
1439 INT sign;
1440 UINT escapeOnesCounter = 0;
1441 UINT carryBit;
1442 INT escape_word = 0;
1443
1444 /* decode escape prefix */
1445 while (1) {
1446 carryBit = HcrGetABitFromBitstream(bs,
1447 pLeftStartOfSegment,
1448 pLeftStartOfSegment, /* dummy */
1449 FROM_LEFT_TO_RIGHT);
1450 *pRemainingBitsInSegment -= 1;
1451 *pNumDecodedBits += 1;
1452
1453 if (carryBit != 0) {
1454 escapeOnesCounter += 1;
1455 }
1456 else {
1457 escapeOnesCounter += 4;
1458 break;
1459 }
1460 }
1461
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;
1470
1471 escape_word <<= 1;
1472 escape_word = escape_word | carryBit;
1473 }
1474
1475 sign = (quantSpecCoef >= 0) ? 1 : -1;
1476
1477 quantSpecCoef = sign * (((INT ) 1 << escapeOnesCounter) + escape_word);
1478
1479 return quantSpecCoef;
1480 }
1481
1482
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 -------------------------------------------------------------------------------------------- */
DecodePCW_Sign(HANDLE_FDK_BITSTREAM bs,UINT codebookDim,const SCHAR * pQuantVal,FIXP_DBL * pQuantSpecCoef,int * quantSpecCoefIdx,USHORT * pLeftStartOfSegment,SCHAR * pRemainingBitsInSegment,int * pNumDecodedBits)1493 static int DecodePCW_Sign(HANDLE_FDK_BITSTREAM bs,
1494 UINT codebookDim,
1495 const SCHAR *pQuantVal,
1496 FIXP_DBL *pQuantSpecCoef,
1497 int *quantSpecCoefIdx,
1498 USHORT *pLeftStartOfSegment,
1499 SCHAR *pRemainingBitsInSegment,
1500 int *pNumDecodedBits
1501 )
1502 {
1503 UINT i;
1504 UINT carryBit;
1505 INT quantSpecCoef;
1506
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)) {
1517 return -1;
1518 }
1519
1520 /* adapt sign of values according to the decoded sign bit */
1521 if (carryBit != 0) {
1522 pQuantSpecCoef[*quantSpecCoefIdx] = -(FIXP_DBL)quantSpecCoef;
1523 }
1524 else {
1525 pQuantSpecCoef[*quantSpecCoefIdx] = (FIXP_DBL)quantSpecCoef;
1526 }
1527 }
1528 else {
1529 pQuantSpecCoef[*quantSpecCoefIdx] = FL2FXCONST_DBL(0.0f);
1530 }
1531 *quantSpecCoefIdx += 1 ;
1532 if (*quantSpecCoefIdx >= 1024) {
1533 return -1;
1534 }
1535 }
1536 return 0;
1537 }
1538
1539
1540 /*---------------------------------------------------------------------------------------------
1541 description: Mutes spectral lines which have been marked as erroneous (Q_VALUE_INVALID)
1542 -------------------------------------------------------------------------------------------- */
HcrMuteErroneousLines(H_HCR_INFO hHcr)1543 void HcrMuteErroneousLines(H_HCR_INFO hHcr)
1544 {
1545 int c;
1546 FIXP_DBL *RESTRICT pLong = SPEC_LONG(hHcr->decInOut.pQuantizedSpectralCoefficientsBase);
1547
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 */
1553 #else
1554 pLong[c] = FL2FXCONST_DBL(0.0f); /* muting */
1555 #endif
1556 }
1557 }
1558 }
1559
1560
1561 /*---------------------------------------------------------------------------------------------
1562 description: Sets global HCR type
1563 -------------------------------------------------------------------------------------------- */
setHcrType(H_HCR_INFO hHcr,MP4_ELEMENT_ID type)1564 void setHcrType(H_HCR_INFO hHcr, MP4_ELEMENT_ID type)
1565 {
1566 switch (type) {
1567 case ID_SCE:
1568 hHcr->globalHcrType = 0;
1569 break;
1570 case ID_CPE:
1571 hHcr->globalHcrType = 1;
1572 break;
1573 default:
1574 break;
1575 }
1576 }
1577
1578
1579 /*---------------------------------------------------------------------------------------------
1580 description: Gets HCR type from the HCR data structure
1581 -----------------------------------------------------------------------------------------------
1582 return: - global HCR type
1583 -------------------------------------------------------------------------------------------- */
getHcrType(H_HCR_INFO hHcr)1584 INT getHcrType(H_HCR_INFO hHcr)
1585 {
1586 return hHcr->globalHcrType;
1587 }
1588
1589
1590
1591
1592