• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3 
4 © Copyright  1995 - 2021 Fraunhofer-Gesellschaft zur Förderung der angewandten
5 Forschung e.V. All rights reserved.
6 
7  1.    INTRODUCTION
8 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9 that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10 scheme for digital audio. This FDK AAC Codec software is intended to be used on
11 a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14 general perceptual audio codecs. AAC-ELD is considered the best-performing
15 full-bandwidth communications codec by independent studies and is widely
16 deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17 specifications.
18 
19 Patent licenses for necessary patent claims for the FDK AAC Codec (including
20 those of Fraunhofer) may be obtained through Via Licensing
21 (www.vialicensing.com) or through the respective patent owners individually for
22 the purpose of encoding or decoding bit streams in products that are compliant
23 with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24 Android devices already license these patent claims through Via Licensing or
25 directly from the patent owners, and therefore FDK AAC Codec software may
26 already be covered under those patent licenses when it is used for those
27 licensed purposes only.
28 
29 Commercially-licensed AAC software libraries, including floating-point versions
30 with enhanced sound quality, are also available from Fraunhofer. Users are
31 encouraged to check the Fraunhofer website for additional applications
32 information and documentation.
33 
34 2.    COPYRIGHT LICENSE
35 
36 Redistribution and use in source and binary forms, with or without modification,
37 are permitted without payment of copyright license fees provided that you
38 satisfy the following conditions:
39 
40 You must retain the complete text of this software license in redistributions of
41 the FDK AAC Codec or your modifications thereto in source code form.
42 
43 You must retain the complete text of this software license in the documentation
44 and/or other materials provided with redistributions of the FDK AAC Codec or
45 your modifications thereto in binary form. You must make available free of
46 charge copies of the complete source code of the FDK AAC Codec and your
47 modifications thereto to recipients of copies in binary form.
48 
49 The name of Fraunhofer may not be used to endorse or promote products derived
50 from this library without prior written permission.
51 
52 You may not charge copyright license fees for anyone to use, copy or distribute
53 the FDK AAC Codec software or your modifications thereto.
54 
55 Your modified versions of the FDK AAC Codec must carry prominent notices stating
56 that you changed the software and the date of any change. For modified versions
57 of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58 must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59 AAC Codec Library for Android."
60 
61 3.    NO PATENT LICENSE
62 
63 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64 limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65 Fraunhofer provides no warranty of patent non-infringement with respect to this
66 software.
67 
68 You may use this FDK AAC Codec software or modifications thereto only for
69 purposes that are authorized by appropriate patent licenses.
70 
71 4.    DISCLAIMER
72 
73 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74 holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75 including but not limited to the implied warranties of merchantability and
76 fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78 or consequential damages, including but not limited to procurement of substitute
79 goods or services; loss of use, data, or profits, or business interruption,
80 however caused and on any theory of liability, whether in contract, strict
81 liability, or tort (including negligence), arising in any way out of the use of
82 this software, even if advised of the possibility of such damage.
83 
84 5.    CONTACT INFORMATION
85 
86 Fraunhofer Institute for Integrated Circuits IIS
87 Attention: Audio and Multimedia Departments - FDK AAC LL
88 Am Wolfsmantel 33
89 91058 Erlangen, Germany
90 
91 www.iis.fraunhofer.de/amm
92 amm-info@iis.fraunhofer.de
93 ----------------------------------------------------------------------------- */
94 
95 /**************************** AAC decoder library ******************************
96 
97    Author(s):
98 
99    Description:
100 
101 *******************************************************************************/
102 
103 /*!
104   \file
105   \brief  RVLC Decoder
106   \author Robert Weidner
107 */
108 
109 #include "rvlc.h"
110 
111 #include "block.h"
112 
113 #include "aac_rom.h"
114 #include "rvlcbit.h"
115 #include "rvlcconceal.h"
116 #include "aacdec_hcr.h"
117 
118 /*---------------------------------------------------------------------------------------------
119      function:     rvlcInit
120 
121      description:  init RVLC by data from channelinfo, which was decoded
122 previously and set up pointers
123 -----------------------------------------------------------------------------------------------
124         input:     - pointer rvlc structure
125                    - pointer channel info structure
126                    - pointer bitstream structure
127 -----------------------------------------------------------------------------------------------
128         return:    -
129 --------------------------------------------------------------------------------------------
130 */
131 
rvlcInit(CErRvlcInfo * pRvlc,CAacDecoderChannelInfo * pAacDecoderChannelInfo,HANDLE_FDK_BITSTREAM bs)132 static void rvlcInit(CErRvlcInfo *pRvlc,
133                      CAacDecoderChannelInfo *pAacDecoderChannelInfo,
134                      HANDLE_FDK_BITSTREAM bs) {
135   /* RVLC common initialization part 2 of 2 */
136   SHORT *pScfEsc = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfEsc;
137   SHORT *pScfFwd = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd;
138   SHORT *pScfBwd = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd;
139   SHORT *pScaleFactor = pAacDecoderChannelInfo->pDynData->aScaleFactor;
140   int bnds;
141 
142   pAacDecoderChannelInfo->pDynData->specificTo.aac.rvlcIntensityUsed = 0;
143 
144   pRvlc->numDecodedEscapeWordsEsc = 0;
145   pRvlc->numDecodedEscapeWordsFwd = 0;
146   pRvlc->numDecodedEscapeWordsBwd = 0;
147 
148   pRvlc->intensity_used = 0;
149   pRvlc->errorLogRvlc = 0;
150 
151   pRvlc->conceal_max = CONCEAL_MAX_INIT;
152   pRvlc->conceal_min = CONCEAL_MIN_INIT;
153 
154   pRvlc->conceal_max_esc = CONCEAL_MAX_INIT;
155   pRvlc->conceal_min_esc = CONCEAL_MIN_INIT;
156 
157   pRvlc->pHuffTreeRvlcEscape = aHuffTreeRvlcEscape;
158   pRvlc->pHuffTreeRvlCodewds = aHuffTreeRvlCodewds;
159 
160   /* init scf arrays (for savety (in case of there are only zero codebooks)) */
161   for (bnds = 0; bnds < RVLC_MAX_SFB; bnds++) {
162     pScfFwd[bnds] = 0;
163     pScfBwd[bnds] = 0;
164     pScfEsc[bnds] = 0;
165     pScaleFactor[bnds] = 0;
166   }
167 
168   /* set base bitstream ptr to the RVL-coded part (start of RVLC data (ESC 2))
169    */
170   FDKsyncCache(bs);
171   pRvlc->bsAnchor = (INT)FDKgetValidBits(bs);
172 
173   pRvlc->bitstreamIndexRvlFwd =
174       0; /* first bit within RVL coded block as start address for  forward
175             decoding */
176   pRvlc->bitstreamIndexRvlBwd =
177       pRvlc->length_of_rvlc_sf - 1; /* last bit within RVL coded block as start
178                                        address for backward decoding */
179 
180   /* skip RVLC-bitstream-part -- pointing now to escapes (if present) or to TNS
181    * data (if present) */
182   FDKpushFor(bs, pRvlc->length_of_rvlc_sf);
183 
184   if (pRvlc->sf_escapes_present != 0) {
185     /* locate internal bitstream ptr at escapes (which is the second part) */
186     FDKsyncCache(bs);
187     pRvlc->bitstreamIndexEsc = pRvlc->bsAnchor - (INT)FDKgetValidBits(bs);
188 
189     /* skip escapeRVLC-bitstream-part -- pointing to TNS data (if present)   to
190      * make decoder continue */
191     /* decoding of RVLC should work despite this second pushFor during
192      * initialization because        */
193     /* bitstream initialization is valid for both ESC2 data parts (RVL-coded
194      * values and ESC-coded values) */
195     FDKpushFor(bs, pRvlc->length_of_rvlc_escapes);
196   }
197 }
198 
199 /*---------------------------------------------------------------------------------------------
200      function:     rvlcCheckIntensityCb
201 
202      description:  Check if a intensity codebook is used in the current channel.
203 -----------------------------------------------------------------------------------------------
204         input:     - pointer rvlc structure
205                    - pointer channel info structure
206 -----------------------------------------------------------------------------------------------
207         output:    - intensity_used: 0 no intensity codebook is used
208                                      1 intensity codebook is used
209 -----------------------------------------------------------------------------------------------
210         return:    -
211 --------------------------------------------------------------------------------------------
212 */
213 
rvlcCheckIntensityCb(CErRvlcInfo * pRvlc,CAacDecoderChannelInfo * pAacDecoderChannelInfo)214 static void rvlcCheckIntensityCb(
215     CErRvlcInfo *pRvlc, CAacDecoderChannelInfo *pAacDecoderChannelInfo) {
216   int group, band, bnds;
217 
218   pRvlc->intensity_used = 0;
219 
220   for (group = 0; group < pRvlc->numWindowGroups; group++) {
221     for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
222       bnds = 16 * group + band;
223       if ((pAacDecoderChannelInfo->pDynData->aCodeBook[bnds] ==
224            INTENSITY_HCB) ||
225           (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds] ==
226            INTENSITY_HCB2)) {
227         pRvlc->intensity_used = 1;
228         break;
229       }
230     }
231   }
232 }
233 
234 /*---------------------------------------------------------------------------------------------
235      function:     rvlcDecodeEscapeWord
236 
237      description:  Decode a huffman coded RVLC Escape-word. This value is part
238 of a DPCM coded scalefactor.
239 -----------------------------------------------------------------------------------------------
240         input:     - pointer rvlc structure
241 -----------------------------------------------------------------------------------------------
242         return:    - a single RVLC-Escape value which had to be applied to a
243 DPCM value (which has a absolute value of 7)
244 --------------------------------------------------------------------------------------------
245 */
246 
rvlcDecodeEscapeWord(CErRvlcInfo * pRvlc,HANDLE_FDK_BITSTREAM bs)247 static SCHAR rvlcDecodeEscapeWord(CErRvlcInfo *pRvlc, HANDLE_FDK_BITSTREAM bs) {
248   int i;
249   SCHAR value;
250   UCHAR carryBit;
251   UINT treeNode;
252   UINT branchValue;
253   UINT branchNode;
254 
255   INT *pBitstreamIndexEsc;
256   const UINT *pEscTree;
257 
258   pEscTree = pRvlc->pHuffTreeRvlcEscape;
259   pBitstreamIndexEsc = &(pRvlc->bitstreamIndexEsc);
260   treeNode = *pEscTree; /* init at starting node */
261 
262   for (i = MAX_LEN_RVLC_ESCAPE_WORD - 1; i >= 0; i--) {
263     carryBit =
264         rvlcReadBitFromBitstream(bs, /* get next bit */
265                                  pRvlc->bsAnchor, pBitstreamIndexEsc, FWD);
266 
267     CarryBitToBranchValue(carryBit, /* huffman decoding, do a single step in
268                                        huffman decoding tree */
269                           treeNode, &branchValue, &branchNode);
270 
271     if ((branchNode & TEST_BIT_10) ==
272         TEST_BIT_10) { /* test bit 10 ; if set --> a RVLC-escape-word is
273                           completely decoded */
274       value = (SCHAR)branchNode & CLR_BIT_10;
275       pRvlc->length_of_rvlc_escapes -= (MAX_LEN_RVLC_ESCAPE_WORD - i);
276 
277       if (pRvlc->length_of_rvlc_escapes < 0) {
278         pRvlc->errorLogRvlc |= RVLC_ERROR_ALL_ESCAPE_WORDS_INVALID;
279         value = -1;
280       }
281 
282       return value;
283     } else {
284       treeNode = *(
285           pEscTree +
286           branchValue); /* update treeNode for further step in decoding tree */
287     }
288   }
289 
290   pRvlc->errorLogRvlc |= RVLC_ERROR_ALL_ESCAPE_WORDS_INVALID;
291 
292   return -1; /* should not be reached */
293 }
294 
295 /*---------------------------------------------------------------------------------------------
296      function:     rvlcDecodeEscapes
297 
298      description:  Decodes all huffman coded RVLC Escape Words.
299                    Here a difference to the pseudo-code-implementation from
300 standard can be found. A while loop (and not two nested for loops) is used for
301 two reasons:
302 
303                    1. The plain huffman encoded escapes are decoded before the
304 RVL-coded scalefactors. Therefore the escapes are present in the second step
305                       when decoding the RVL-coded-scalefactor values in forward
306 and backward direction.
307 
308                       When the RVL-coded scalefactors are decoded and there a
309 escape is needed, then it is just taken out of the array in ascending order.
310 
311                    2. It's faster.
312 -----------------------------------------------------------------------------------------------
313         input:     - pointer rvlc structure
314                    - handle to FDK bitstream
315 -----------------------------------------------------------------------------------------------
316         return:    - 0 ok     the decoded escapes seem to be valid
317                    - 1 error  there was a error detected during decoding escapes
318                               --> all escapes are invalid
319 --------------------------------------------------------------------------------------------
320 */
321 
rvlcDecodeEscapes(CErRvlcInfo * pRvlc,SHORT * pEsc,HANDLE_FDK_BITSTREAM bs)322 static void rvlcDecodeEscapes(CErRvlcInfo *pRvlc, SHORT *pEsc,
323                               HANDLE_FDK_BITSTREAM bs) {
324   SCHAR escWord;
325   SCHAR escCnt = 0;
326   SHORT *pEscBitCntSum;
327 
328   pEscBitCntSum = &(pRvlc->length_of_rvlc_escapes);
329 
330   /* Decode all RVLC-Escape words with a plain Huffman-Decoder */
331   while (*pEscBitCntSum > 0) {
332     escWord = rvlcDecodeEscapeWord(pRvlc, bs);
333 
334     if (escWord >= 0) {
335       pEsc[escCnt] = escWord;
336       escCnt++;
337     } else {
338       pRvlc->errorLogRvlc |= RVLC_ERROR_ALL_ESCAPE_WORDS_INVALID;
339       pRvlc->numDecodedEscapeWordsEsc = escCnt;
340 
341       return;
342     }
343   } /* all RVLC escapes decoded */
344 
345   pRvlc->numDecodedEscapeWordsEsc = escCnt;
346 }
347 
348 /*---------------------------------------------------------------------------------------------
349      function:     decodeRVLCodeword
350 
351      description:  Decodes a RVL-coded dpcm-word (-part).
352 -----------------------------------------------------------------------------------------------
353         input:     - FDK bitstream handle
354                    - pointer rvlc structure
355 -----------------------------------------------------------------------------------------------
356         return:    - a dpcm value which is within range [0,1,..,14] in case of
357 no errors. The offset of 7 must be subtracted to get a valid dpcm scalefactor
358 value. In case of errors a forbidden codeword is detected --> returning -1
359 --------------------------------------------------------------------------------------------
360 */
361 
decodeRVLCodeword(HANDLE_FDK_BITSTREAM bs,CErRvlcInfo * pRvlc)362 SCHAR decodeRVLCodeword(HANDLE_FDK_BITSTREAM bs, CErRvlcInfo *pRvlc) {
363   int i;
364   SCHAR value;
365   UCHAR carryBit;
366   UINT branchValue;
367   UINT branchNode;
368 
369   const UINT *pRvlCodeTree = pRvlc->pHuffTreeRvlCodewds;
370   UCHAR direction = pRvlc->direction;
371   INT *pBitstrIndxRvl = pRvlc->pBitstrIndxRvl_RVL;
372   UINT treeNode = *pRvlCodeTree;
373 
374   for (i = MAX_LEN_RVLC_CODE_WORD - 1; i >= 0; i--) {
375     carryBit =
376         rvlcReadBitFromBitstream(bs, /* get next bit */
377                                  pRvlc->bsAnchor, pBitstrIndxRvl, direction);
378 
379     CarryBitToBranchValue(carryBit, /* huffman decoding, do a single step in
380                                        huffman decoding tree */
381                           treeNode, &branchValue, &branchNode);
382 
383     if ((branchNode & TEST_BIT_10) ==
384         TEST_BIT_10) { /* test bit 10 ; if set --> a
385                           RVLC-codeword is completely decoded
386                         */
387       value = (SCHAR)(branchNode & CLR_BIT_10);
388       *pRvlc->pRvlBitCnt_RVL -= (MAX_LEN_RVLC_CODE_WORD - i);
389 
390       /* check available bits for decoding */
391       if (*pRvlc->pRvlBitCnt_RVL < 0) {
392         if (direction == FWD) {
393           pRvlc->errorLogRvlc |= RVLC_ERROR_RVL_SUM_BIT_COUNTER_BELOW_ZERO_FWD;
394         } else {
395           pRvlc->errorLogRvlc |= RVLC_ERROR_RVL_SUM_BIT_COUNTER_BELOW_ZERO_BWD;
396         }
397         value = -1; /* signalize an error in return value, because too many bits
398                        was decoded */
399       }
400 
401       /* check max value of dpcm value */
402       if (value > MAX_ALLOWED_DPCM_INDEX) {
403         if (direction == FWD) {
404           pRvlc->errorLogRvlc |= RVLC_ERROR_FORBIDDEN_CW_DETECTED_FWD;
405         } else {
406           pRvlc->errorLogRvlc |= RVLC_ERROR_FORBIDDEN_CW_DETECTED_BWD;
407         }
408         value = -1; /* signalize an error in return value, because a forbidden
409                        cw was detected*/
410       }
411 
412       return value; /* return a dpcm value with offset +7 or an error status */
413     } else {
414       treeNode = *(
415           pRvlCodeTree +
416           branchValue); /* update treeNode for further step in decoding tree */
417     }
418   }
419 
420   return -1;
421 }
422 
423 /*---------------------------------------------------------------------------------------------
424      function:     rvlcDecodeForward
425 
426      description:  Decode RVL-coded codewords in forward direction.
427 -----------------------------------------------------------------------------------------------
428         input:     - pointer rvlc structure
429                    - pointer channel info structure
430                    - handle to FDK bitstream
431 -----------------------------------------------------------------------------------------------
432         return:    -
433 --------------------------------------------------------------------------------------------
434 */
435 
rvlcDecodeForward(CErRvlcInfo * pRvlc,CAacDecoderChannelInfo * pAacDecoderChannelInfo,HANDLE_FDK_BITSTREAM bs)436 static void rvlcDecodeForward(CErRvlcInfo *pRvlc,
437                               CAacDecoderChannelInfo *pAacDecoderChannelInfo,
438                               HANDLE_FDK_BITSTREAM bs) {
439   int band = 0;
440   int group = 0;
441   int bnds = 0;
442 
443   SHORT dpcm;
444 
445   SHORT factor =
446       pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain - SF_OFFSET;
447   SHORT position = -SF_OFFSET;
448   SHORT noisenrg = pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain -
449                    SF_OFFSET - 90 - 256;
450 
451   SHORT *pScfFwd = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd;
452   SHORT *pScfEsc = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfEsc;
453   UCHAR *pEscFwdCnt = &(pRvlc->numDecodedEscapeWordsFwd);
454 
455   pRvlc->pRvlBitCnt_RVL = &(pRvlc->length_of_rvlc_sf_fwd);
456   pRvlc->pBitstrIndxRvl_RVL = &(pRvlc->bitstreamIndexRvlFwd);
457 
458   *pEscFwdCnt = 0;
459   pRvlc->direction = FWD;
460   pRvlc->noise_used = 0;
461   pRvlc->sf_used = 0;
462   pRvlc->lastScf = 0;
463   pRvlc->lastNrg = 0;
464   pRvlc->lastIs = 0;
465 
466   rvlcCheckIntensityCb(pRvlc, pAacDecoderChannelInfo);
467 
468   /* main loop fwd long */
469   for (group = 0; group < pRvlc->numWindowGroups; group++) {
470     for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
471       bnds = 16 * group + band;
472 
473       switch (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds]) {
474         case ZERO_HCB:
475           pScfFwd[bnds] = 0;
476           break;
477 
478         case INTENSITY_HCB2:
479         case INTENSITY_HCB:
480           /* store dpcm_is_position */
481           dpcm = decodeRVLCodeword(bs, pRvlc);
482           if (dpcm < 0) {
483             pRvlc->conceal_max = bnds;
484             return;
485           }
486           dpcm -= TABLE_OFFSET;
487           if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
488             if (pRvlc->length_of_rvlc_escapes) {
489               pRvlc->conceal_max = bnds;
490               return;
491             } else {
492               if (dpcm == MIN_RVL) {
493                 dpcm -= *pScfEsc++;
494               } else {
495                 dpcm += *pScfEsc++;
496               }
497               (*pEscFwdCnt)++;
498               if (pRvlc->conceal_max_esc == CONCEAL_MAX_INIT) {
499                 pRvlc->conceal_max_esc = bnds;
500               }
501             }
502           }
503           position += dpcm;
504           pScfFwd[bnds] = position;
505           pRvlc->lastIs = position;
506           break;
507 
508         case NOISE_HCB:
509           if (pRvlc->noise_used == 0) {
510             pRvlc->noise_used = 1;
511             pRvlc->first_noise_band = bnds;
512             noisenrg += pRvlc->dpcm_noise_nrg;
513             pScfFwd[bnds] = 100 + noisenrg;
514             pRvlc->lastNrg = noisenrg;
515           } else {
516             dpcm = decodeRVLCodeword(bs, pRvlc);
517             if (dpcm < 0) {
518               pRvlc->conceal_max = bnds;
519               return;
520             }
521             dpcm -= TABLE_OFFSET;
522             if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
523               if (pRvlc->length_of_rvlc_escapes) {
524                 pRvlc->conceal_max = bnds;
525                 return;
526               } else {
527                 if (dpcm == MIN_RVL) {
528                   dpcm -= *pScfEsc++;
529                 } else {
530                   dpcm += *pScfEsc++;
531                 }
532                 (*pEscFwdCnt)++;
533                 if (pRvlc->conceal_max_esc == CONCEAL_MAX_INIT) {
534                   pRvlc->conceal_max_esc = bnds;
535                 }
536               }
537             }
538             noisenrg += dpcm;
539             pScfFwd[bnds] = 100 + noisenrg;
540             pRvlc->lastNrg = noisenrg;
541           }
542           pAacDecoderChannelInfo->data.aac.PnsData.pnsUsed[bnds] = 1;
543           break;
544 
545         default:
546           pRvlc->sf_used = 1;
547           dpcm = decodeRVLCodeword(bs, pRvlc);
548           if (dpcm < 0) {
549             pRvlc->conceal_max = bnds;
550             return;
551           }
552           dpcm -= TABLE_OFFSET;
553           if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
554             if (pRvlc->length_of_rvlc_escapes) {
555               pRvlc->conceal_max = bnds;
556               return;
557             } else {
558               if (dpcm == MIN_RVL) {
559                 dpcm -= *pScfEsc++;
560               } else {
561                 dpcm += *pScfEsc++;
562               }
563               (*pEscFwdCnt)++;
564               if (pRvlc->conceal_max_esc == CONCEAL_MAX_INIT) {
565                 pRvlc->conceal_max_esc = bnds;
566               }
567             }
568           }
569           factor += dpcm;
570           pScfFwd[bnds] = factor;
571           pRvlc->lastScf = factor;
572           break;
573       }
574     }
575   }
576 
577   /* postfetch fwd long */
578   if (pRvlc->intensity_used) {
579     dpcm = decodeRVLCodeword(bs, pRvlc); /* dpcm_is_last_position */
580     if (dpcm < 0) {
581       pRvlc->conceal_max = bnds;
582       return;
583     }
584     dpcm -= TABLE_OFFSET;
585     if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
586       if (pRvlc->length_of_rvlc_escapes) {
587         pRvlc->conceal_max = bnds;
588         return;
589       } else {
590         if (dpcm == MIN_RVL) {
591           dpcm -= *pScfEsc++;
592         } else {
593           dpcm += *pScfEsc++;
594         }
595         (*pEscFwdCnt)++;
596         if (pRvlc->conceal_max_esc == CONCEAL_MAX_INIT) {
597           pRvlc->conceal_max_esc = bnds;
598         }
599       }
600     }
601     pRvlc->dpcm_is_last_position = dpcm;
602   }
603 }
604 
605 /*---------------------------------------------------------------------------------------------
606      function:     rvlcDecodeBackward
607 
608      description:  Decode RVL-coded codewords in backward direction.
609 -----------------------------------------------------------------------------------------------
610         input:     - pointer rvlc structure
611                    - pointer channel info structure
612                    - handle FDK bitstream
613 -----------------------------------------------------------------------------------------------
614         return:    -
615 --------------------------------------------------------------------------------------------
616 */
617 
rvlcDecodeBackward(CErRvlcInfo * pRvlc,CAacDecoderChannelInfo * pAacDecoderChannelInfo,HANDLE_FDK_BITSTREAM bs)618 static void rvlcDecodeBackward(CErRvlcInfo *pRvlc,
619                                CAacDecoderChannelInfo *pAacDecoderChannelInfo,
620                                HANDLE_FDK_BITSTREAM bs) {
621   SHORT band, group, dpcm, offset;
622   SHORT bnds = pRvlc->maxSfbTransmitted - 1;
623 
624   SHORT factor = pRvlc->rev_global_gain - SF_OFFSET;
625   SHORT position = pRvlc->dpcm_is_last_position - SF_OFFSET;
626   SHORT noisenrg = pRvlc->rev_global_gain + pRvlc->dpcm_noise_last_position -
627                    SF_OFFSET - 90 - 256;
628 
629   SHORT *pScfBwd = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd;
630   SHORT *pScfEsc = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfEsc;
631   UCHAR escEscCnt = pRvlc->numDecodedEscapeWordsEsc;
632   UCHAR *pEscBwdCnt = &(pRvlc->numDecodedEscapeWordsBwd);
633 
634   pRvlc->pRvlBitCnt_RVL = &(pRvlc->length_of_rvlc_sf_bwd);
635   pRvlc->pBitstrIndxRvl_RVL = &(pRvlc->bitstreamIndexRvlBwd);
636 
637   *pEscBwdCnt = 0;
638   pRvlc->direction = BWD;
639   pScfEsc += escEscCnt - 1; /* set pScfEsc to last entry */
640   pRvlc->firstScf = 0;
641   pRvlc->firstNrg = 0;
642   pRvlc->firstIs = 0;
643 
644   /* prefetch long BWD */
645   if (pRvlc->intensity_used) {
646     dpcm = decodeRVLCodeword(bs, pRvlc); /* dpcm_is_last_position */
647     if (dpcm < 0) {
648       pRvlc->dpcm_is_last_position = 0;
649       pRvlc->conceal_min = bnds;
650       return;
651     }
652     dpcm -= TABLE_OFFSET;
653     if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
654       if ((pRvlc->length_of_rvlc_escapes) || (*pEscBwdCnt >= escEscCnt)) {
655         pRvlc->conceal_min = bnds;
656         return;
657       } else {
658         if (dpcm == MIN_RVL) {
659           dpcm -= *pScfEsc--;
660         } else {
661           dpcm += *pScfEsc--;
662         }
663         (*pEscBwdCnt)++;
664         if (pRvlc->conceal_min_esc == CONCEAL_MIN_INIT) {
665           pRvlc->conceal_min_esc = bnds;
666         }
667       }
668     }
669     pRvlc->dpcm_is_last_position = dpcm;
670   }
671 
672   /* main loop long BWD */
673   for (group = pRvlc->numWindowGroups - 1; group >= 0; group--) {
674     for (band = pRvlc->maxSfbTransmitted - 1; band >= 0; band--) {
675       bnds = 16 * group + band;
676       if ((band == 0) && (pRvlc->numWindowGroups != 1))
677         offset = 16 - pRvlc->maxSfbTransmitted + 1;
678       else
679         offset = 1;
680 
681       switch (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds]) {
682         case ZERO_HCB:
683           pScfBwd[bnds] = 0;
684           break;
685 
686         case INTENSITY_HCB2:
687         case INTENSITY_HCB:
688           /* store dpcm_is_position */
689           dpcm = decodeRVLCodeword(bs, pRvlc);
690           if (dpcm < 0) {
691             pScfBwd[bnds] = position;
692             pRvlc->conceal_min = fMax(0, bnds - offset);
693             return;
694           }
695           dpcm -= TABLE_OFFSET;
696           if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
697             if ((pRvlc->length_of_rvlc_escapes) || (*pEscBwdCnt >= escEscCnt)) {
698               pScfBwd[bnds] = position;
699               pRvlc->conceal_min = fMax(0, bnds - offset);
700               return;
701             } else {
702               if (dpcm == MIN_RVL) {
703                 dpcm -= *pScfEsc--;
704               } else {
705                 dpcm += *pScfEsc--;
706               }
707               (*pEscBwdCnt)++;
708               if (pRvlc->conceal_min_esc == CONCEAL_MIN_INIT) {
709                 pRvlc->conceal_min_esc = fMax(0, bnds - offset);
710               }
711             }
712           }
713           pScfBwd[bnds] = position;
714           position -= dpcm;
715           pRvlc->firstIs = position;
716           break;
717 
718         case NOISE_HCB:
719           if (bnds == pRvlc->first_noise_band) {
720             pScfBwd[bnds] =
721                 pRvlc->dpcm_noise_nrg +
722                 pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain -
723                 SF_OFFSET - 90 - 256;
724             pRvlc->firstNrg = pScfBwd[bnds];
725           } else {
726             dpcm = decodeRVLCodeword(bs, pRvlc);
727             if (dpcm < 0) {
728               pScfBwd[bnds] = noisenrg;
729               pRvlc->conceal_min = fMax(0, bnds - offset);
730               return;
731             }
732             dpcm -= TABLE_OFFSET;
733             if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
734               if ((pRvlc->length_of_rvlc_escapes) ||
735                   (*pEscBwdCnt >= escEscCnt)) {
736                 pScfBwd[bnds] = noisenrg;
737                 pRvlc->conceal_min = fMax(0, bnds - offset);
738                 return;
739               } else {
740                 if (dpcm == MIN_RVL) {
741                   dpcm -= *pScfEsc--;
742                 } else {
743                   dpcm += *pScfEsc--;
744                 }
745                 (*pEscBwdCnt)++;
746                 if (pRvlc->conceal_min_esc == CONCEAL_MIN_INIT) {
747                   pRvlc->conceal_min_esc = fMax(0, bnds - offset);
748                 }
749               }
750             }
751             pScfBwd[bnds] = noisenrg;
752             noisenrg -= dpcm;
753             pRvlc->firstNrg = noisenrg;
754           }
755           break;
756 
757         default:
758           dpcm = decodeRVLCodeword(bs, pRvlc);
759           if (dpcm < 0) {
760             pScfBwd[bnds] = factor;
761             pRvlc->conceal_min = fMax(0, bnds - offset);
762             return;
763           }
764           dpcm -= TABLE_OFFSET;
765           if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
766             if ((pRvlc->length_of_rvlc_escapes) || (*pEscBwdCnt >= escEscCnt)) {
767               pScfBwd[bnds] = factor;
768               pRvlc->conceal_min = fMax(0, bnds - offset);
769               return;
770             } else {
771               if (dpcm == MIN_RVL) {
772                 dpcm -= *pScfEsc--;
773               } else {
774                 dpcm += *pScfEsc--;
775               }
776               (*pEscBwdCnt)++;
777               if (pRvlc->conceal_min_esc == CONCEAL_MIN_INIT) {
778                 pRvlc->conceal_min_esc = fMax(0, bnds - offset);
779               }
780             }
781           }
782           pScfBwd[bnds] = factor;
783           factor -= dpcm;
784           pRvlc->firstScf = factor;
785           break;
786       }
787     }
788   }
789 }
790 
791 /*---------------------------------------------------------------------------------------------
792      function:     rvlcFinalErrorDetection
793 
794      description:  Call RVLC concealment if error was detected in decoding
795 process
796 -----------------------------------------------------------------------------------------------
797         input:     - pointer rvlc structure
798                    - pointer channel info structure
799 -----------------------------------------------------------------------------------------------
800         return:    -
801 --------------------------------------------------------------------------------------------
802 */
803 
rvlcFinalErrorDetection(CAacDecoderChannelInfo * pAacDecoderChannelInfo,CAacDecoderStaticChannelInfo * pAacDecoderStaticChannelInfo)804 static void rvlcFinalErrorDetection(
805     CAacDecoderChannelInfo *pAacDecoderChannelInfo,
806     CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo) {
807   CErRvlcInfo *pRvlc =
808       &pAacDecoderChannelInfo->pComData->overlay.aac.erRvlcInfo;
809   UCHAR ErrorStatusComplete = 0;
810   UCHAR ErrorStatusLengthFwd = 0;
811   UCHAR ErrorStatusLengthBwd = 0;
812   UCHAR ErrorStatusLengthEscapes = 0;
813   UCHAR ErrorStatusFirstScf = 0;
814   UCHAR ErrorStatusLastScf = 0;
815   UCHAR ErrorStatusFirstNrg = 0;
816   UCHAR ErrorStatusLastNrg = 0;
817   UCHAR ErrorStatusFirstIs = 0;
818   UCHAR ErrorStatusLastIs = 0;
819   UCHAR ErrorStatusForbiddenCwFwd = 0;
820   UCHAR ErrorStatusForbiddenCwBwd = 0;
821   UCHAR ErrorStatusNumEscapesFwd = 0;
822   UCHAR ErrorStatusNumEscapesBwd = 0;
823   UCHAR ConcealStatus = 1;
824   UCHAR currentBlockType; /* short: 0, not short: 1*/
825 
826   pAacDecoderChannelInfo->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK = 1;
827 
828   /* invalid escape words, bit counter unequal zero, forbidden codeword detected
829    */
830   if (pRvlc->errorLogRvlc & RVLC_ERROR_FORBIDDEN_CW_DETECTED_FWD)
831     ErrorStatusForbiddenCwFwd = 1;
832 
833   if (pRvlc->errorLogRvlc & RVLC_ERROR_FORBIDDEN_CW_DETECTED_BWD)
834     ErrorStatusForbiddenCwBwd = 1;
835 
836   /* bit counter forward unequal zero */
837   if (pRvlc->length_of_rvlc_sf_fwd) ErrorStatusLengthFwd = 1;
838 
839   /* bit counter backward unequal zero */
840   if (pRvlc->length_of_rvlc_sf_bwd) ErrorStatusLengthBwd = 1;
841 
842   /* bit counter escape sequences unequal zero */
843   if (pRvlc->sf_escapes_present)
844     if (pRvlc->length_of_rvlc_escapes) ErrorStatusLengthEscapes = 1;
845 
846   if (pRvlc->sf_used) {
847     /* first decoded scf does not match to global gain in backward direction */
848     if (pRvlc->firstScf !=
849         (pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain - SF_OFFSET))
850       ErrorStatusFirstScf = 1;
851 
852     /* last decoded scf does not match to rev global gain in forward direction
853      */
854     if (pRvlc->lastScf != (pRvlc->rev_global_gain - SF_OFFSET))
855       ErrorStatusLastScf = 1;
856   }
857 
858   if (pRvlc->noise_used) {
859     /* first decoded nrg does not match to dpcm_noise_nrg in backward direction
860      */
861     if (pRvlc->firstNrg !=
862         (pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain +
863          pRvlc->dpcm_noise_nrg - SF_OFFSET - 90 - 256))
864       ErrorStatusFirstNrg = 1;
865 
866     /* last decoded nrg does not match to dpcm_noise_last_position in forward
867      * direction */
868     if (pRvlc->lastNrg !=
869         (pRvlc->rev_global_gain + pRvlc->dpcm_noise_last_position - SF_OFFSET -
870          90 - 256))
871       ErrorStatusLastNrg = 1;
872   }
873 
874   if (pRvlc->intensity_used) {
875     /* first decoded is position does not match in backward direction */
876     if (pRvlc->firstIs != (-SF_OFFSET)) ErrorStatusFirstIs = 1;
877 
878     /* last decoded is position does not match in forward direction */
879     if (pRvlc->lastIs != (pRvlc->dpcm_is_last_position - SF_OFFSET))
880       ErrorStatusLastIs = 1;
881   }
882 
883   /* decoded escapes and used escapes in forward direction do not fit */
884   if ((pRvlc->numDecodedEscapeWordsFwd != pRvlc->numDecodedEscapeWordsEsc) &&
885       (pRvlc->conceal_max == CONCEAL_MAX_INIT)) {
886     ErrorStatusNumEscapesFwd = 1;
887   }
888 
889   /* decoded escapes and used escapes in backward direction do not fit */
890   if ((pRvlc->numDecodedEscapeWordsBwd != pRvlc->numDecodedEscapeWordsEsc) &&
891       (pRvlc->conceal_min == CONCEAL_MIN_INIT)) {
892     ErrorStatusNumEscapesBwd = 1;
893   }
894 
895   if (ErrorStatusLengthEscapes ||
896       (((pRvlc->conceal_max == CONCEAL_MAX_INIT) &&
897         (pRvlc->numDecodedEscapeWordsFwd != pRvlc->numDecodedEscapeWordsEsc) &&
898         (ErrorStatusLastScf || ErrorStatusLastNrg || ErrorStatusLastIs))
899 
900        &&
901 
902        ((pRvlc->conceal_min == CONCEAL_MIN_INIT) &&
903         (pRvlc->numDecodedEscapeWordsBwd != pRvlc->numDecodedEscapeWordsEsc) &&
904         (ErrorStatusFirstScf || ErrorStatusFirstNrg || ErrorStatusFirstIs))) ||
905       ((pRvlc->conceal_max == CONCEAL_MAX_INIT) &&
906        ((pRvlc->rev_global_gain - SF_OFFSET - pRvlc->lastScf) < -15)) ||
907       ((pRvlc->conceal_min == CONCEAL_MIN_INIT) &&
908        ((pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain - SF_OFFSET -
909          pRvlc->firstScf) < -15))) {
910     if ((pRvlc->conceal_max == CONCEAL_MAX_INIT) ||
911         (pRvlc->conceal_min == CONCEAL_MIN_INIT)) {
912       pRvlc->conceal_max = 0;
913       pRvlc->conceal_min = fMax(
914           0, (pRvlc->numWindowGroups - 1) * 16 + pRvlc->maxSfbTransmitted - 1);
915     } else {
916       pRvlc->conceal_max = fMin(pRvlc->conceal_max, pRvlc->conceal_max_esc);
917       pRvlc->conceal_min = fMax(pRvlc->conceal_min, pRvlc->conceal_min_esc);
918     }
919   }
920 
921   ErrorStatusComplete = ErrorStatusLastScf || ErrorStatusFirstScf ||
922                         ErrorStatusLastNrg || ErrorStatusFirstNrg ||
923                         ErrorStatusLastIs || ErrorStatusFirstIs ||
924                         ErrorStatusForbiddenCwFwd ||
925                         ErrorStatusForbiddenCwBwd || ErrorStatusLengthFwd ||
926                         ErrorStatusLengthBwd || ErrorStatusLengthEscapes ||
927                         ErrorStatusNumEscapesFwd || ErrorStatusNumEscapesBwd;
928 
929   currentBlockType =
930       (GetWindowSequence(&pAacDecoderChannelInfo->icsInfo) == BLOCK_SHORT) ? 0
931                                                                            : 1;
932 
933   if (!ErrorStatusComplete) {
934     int band;
935     int group;
936     int bnds;
937     int lastSfbIndex;
938 
939     lastSfbIndex = (pRvlc->numWindowGroups > 1) ? 16 : 64;
940 
941     for (group = 0; group < pRvlc->numWindowGroups; group++) {
942       for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
943         bnds = 16 * group + band;
944         pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
945             pAacDecoderStaticChannelInfo->concealmentInfo
946                 .aRvlcPreviousScaleFactor[bnds] =
947                 pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds];
948       }
949     }
950 
951     for (group = 0; group < pRvlc->numWindowGroups; group++) {
952       for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
953         bnds = 16 * group + band;
954         pAacDecoderStaticChannelInfo->concealmentInfo
955             .aRvlcPreviousCodebook[bnds] =
956             pAacDecoderChannelInfo->pDynData->aCodeBook[bnds];
957       }
958       for (; band < lastSfbIndex; band++) {
959         bnds = 16 * group + band;
960         FDK_ASSERT(bnds >= 0 && bnds < RVLC_MAX_SFB);
961         pAacDecoderStaticChannelInfo->concealmentInfo
962             .aRvlcPreviousCodebook[bnds] = ZERO_HCB;
963       }
964     }
965   } else {
966     int band;
967     int group;
968 
969     /* A single bit error was detected in decoding of dpcm values. It also could
970        be an error with more bits in decoding of escapes and dpcm values whereby
971        an illegal codeword followed not directly after the corrupted bits but
972        just after decoding some more (wrong) scalefactors. Use the smaller
973        scalefactor from forward decoding, backward decoding and previous frame.
974      */
975     if (((pRvlc->conceal_min != CONCEAL_MIN_INIT) ||
976          (pRvlc->conceal_max != CONCEAL_MAX_INIT)) &&
977         (pRvlc->conceal_min <= pRvlc->conceal_max) &&
978         (pAacDecoderStaticChannelInfo->concealmentInfo.rvlcPreviousBlockType ==
979          currentBlockType) &&
980         pAacDecoderStaticChannelInfo->concealmentInfo
981             .rvlcPreviousScaleFactorOK &&
982         pRvlc->sf_concealment && ConcealStatus) {
983       BidirectionalEstimation_UseScfOfPrevFrameAsReference(
984           pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo);
985       ConcealStatus = 0;
986     }
987 
988     /* A single bit error was detected in decoding of dpcm values. It also could
989        be an error with more bits in decoding of escapes and dpcm values whereby
990        an illegal codeword followed not directly after the corrupted bits but
991        just after decoding some more (wrong) scalefactors. Use the smaller
992        scalefactor from forward and backward decoding. */
993     if ((pRvlc->conceal_min <= pRvlc->conceal_max) &&
994         ((pRvlc->conceal_min != CONCEAL_MIN_INIT) ||
995          (pRvlc->conceal_max != CONCEAL_MAX_INIT)) &&
996         !(pAacDecoderStaticChannelInfo->concealmentInfo
997               .rvlcPreviousScaleFactorOK &&
998           pRvlc->sf_concealment &&
999           (pAacDecoderStaticChannelInfo->concealmentInfo
1000                .rvlcPreviousBlockType == currentBlockType)) &&
1001         ConcealStatus) {
1002       BidirectionalEstimation_UseLowerScfOfCurrentFrame(pAacDecoderChannelInfo);
1003       ConcealStatus = 0;
1004     }
1005 
1006     /* No errors were detected in decoding of escapes and dpcm values however
1007        the first and last value of a group (is,nrg,sf) is incorrect */
1008     if ((pRvlc->conceal_min <= pRvlc->conceal_max) &&
1009         ((ErrorStatusLastScf && ErrorStatusFirstScf) ||
1010          (ErrorStatusLastNrg && ErrorStatusFirstNrg) ||
1011          (ErrorStatusLastIs && ErrorStatusFirstIs)) &&
1012         !(ErrorStatusForbiddenCwFwd || ErrorStatusForbiddenCwBwd ||
1013           ErrorStatusLengthEscapes) &&
1014         ConcealStatus) {
1015       StatisticalEstimation(pAacDecoderChannelInfo);
1016       ConcealStatus = 0;
1017     }
1018 
1019     /* A error with more bits in decoding of escapes and dpcm values was
1020        detected. Use the smaller scalefactor from forward decoding, backward
1021        decoding and previous frame. */
1022     if ((pRvlc->conceal_min <= pRvlc->conceal_max) &&
1023         pAacDecoderStaticChannelInfo->concealmentInfo
1024             .rvlcPreviousScaleFactorOK &&
1025         pRvlc->sf_concealment &&
1026         (pAacDecoderStaticChannelInfo->concealmentInfo.rvlcPreviousBlockType ==
1027          currentBlockType) &&
1028         ConcealStatus) {
1029       PredictiveInterpolation(pAacDecoderChannelInfo,
1030                               pAacDecoderStaticChannelInfo);
1031       ConcealStatus = 0;
1032     }
1033 
1034     /* Call frame concealment, because no better strategy was found. Setting the
1035        scalefactors to zero is done for debugging purposes */
1036     if (ConcealStatus) {
1037       for (group = 0; group < pRvlc->numWindowGroups; group++) {
1038         for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
1039           pAacDecoderChannelInfo->pDynData->aScaleFactor[16 * group + band] = 0;
1040         }
1041       }
1042       pAacDecoderChannelInfo->pDynData->specificTo.aac
1043           .rvlcCurrentScaleFactorOK = 0;
1044     }
1045   }
1046 }
1047 
1048 /*---------------------------------------------------------------------------------------------
1049      function:     CRvlc_Read
1050 
1051      description:  Read RVLC ESC1 data (side info) from bitstream.
1052 -----------------------------------------------------------------------------------------------
1053         input:     - pointer rvlc structure
1054                    - pointer channel info structure
1055                    - pointer bitstream structure
1056 -----------------------------------------------------------------------------------------------
1057         return:    -
1058 --------------------------------------------------------------------------------------------
1059 */
1060 
CRvlc_Read(CAacDecoderChannelInfo * pAacDecoderChannelInfo,HANDLE_FDK_BITSTREAM bs)1061 void CRvlc_Read(CAacDecoderChannelInfo *pAacDecoderChannelInfo,
1062                 HANDLE_FDK_BITSTREAM bs) {
1063   CErRvlcInfo *pRvlc =
1064       &pAacDecoderChannelInfo->pComData->overlay.aac.erRvlcInfo;
1065 
1066   int group, band;
1067 
1068   /* RVLC long specific initialization  Init part 1 of 2 */
1069   pRvlc->numWindowGroups = GetWindowGroups(&pAacDecoderChannelInfo->icsInfo);
1070   pRvlc->maxSfbTransmitted =
1071       GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
1072   pRvlc->noise_used = 0;               /* noise detection */
1073   pRvlc->dpcm_noise_nrg = 0;           /* only for debugging */
1074   pRvlc->dpcm_noise_last_position = 0; /* only for debugging */
1075   pRvlc->length_of_rvlc_escapes =
1076       -1; /* default value is used for error detection and concealment */
1077 
1078   /* read only error sensitivity class 1 data (ESC 1 - data) */
1079   pRvlc->sf_concealment = FDKreadBits(bs, 1);  /* #1 */
1080   pRvlc->rev_global_gain = FDKreadBits(bs, 8); /* #2 */
1081 
1082   if (GetWindowSequence(&pAacDecoderChannelInfo->icsInfo) == BLOCK_SHORT) {
1083     pRvlc->length_of_rvlc_sf = FDKreadBits(bs, 11); /* #3 */
1084   } else {
1085     pRvlc->length_of_rvlc_sf = FDKreadBits(bs, 9); /* #3 */
1086   }
1087 
1088   /* check if noise codebook is used */
1089   for (group = 0; group < pRvlc->numWindowGroups; group++) {
1090     for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
1091       if (pAacDecoderChannelInfo->pDynData->aCodeBook[16 * group + band] ==
1092           NOISE_HCB) {
1093         pRvlc->noise_used = 1;
1094         break;
1095       }
1096     }
1097   }
1098 
1099   if (pRvlc->noise_used)
1100     pRvlc->dpcm_noise_nrg = FDKreadBits(bs, 9); /* #4  PNS */
1101 
1102   pRvlc->sf_escapes_present = FDKreadBits(bs, 1); /* #5      */
1103 
1104   if (pRvlc->sf_escapes_present) {
1105     pRvlc->length_of_rvlc_escapes = FDKreadBits(bs, 8); /* #6      */
1106   }
1107 
1108   if (pRvlc->noise_used) {
1109     pRvlc->dpcm_noise_last_position = FDKreadBits(bs, 9); /* #7  PNS */
1110     pRvlc->length_of_rvlc_sf -= 9;
1111   }
1112 
1113   pRvlc->length_of_rvlc_sf_fwd = pRvlc->length_of_rvlc_sf;
1114   pRvlc->length_of_rvlc_sf_bwd = pRvlc->length_of_rvlc_sf;
1115 }
1116 
1117 /*---------------------------------------------------------------------------------------------
1118      function:     CRvlc_Decode
1119 
1120      description:  Decode rvlc data
1121                    The function reads both the escape sequences and the
1122 scalefactors in forward and backward direction. If an error occured during
1123 decoding process which can not be concealed with the rvlc concealment frame
1124 concealment will be initiated. Then the element "rvlcCurrentScaleFactorOK" in
1125 the decoder channel info is set to 0 otherwise it is set to 1.
1126 -----------------------------------------------------------------------------------------------
1127         input:     - pointer rvlc structure
1128                    - pointer channel info structure
1129                    - pointer to persistent channel info structure
1130                    - pointer bitstream structure
1131 -----------------------------------------------------------------------------------------------
1132         return:    ErrorStatus = AAC_DEC_OK
1133 --------------------------------------------------------------------------------------------
1134 */
1135 
CRvlc_Decode(CAacDecoderChannelInfo * pAacDecoderChannelInfo,CAacDecoderStaticChannelInfo * pAacDecoderStaticChannelInfo,HANDLE_FDK_BITSTREAM bs)1136 void CRvlc_Decode(CAacDecoderChannelInfo *pAacDecoderChannelInfo,
1137                   CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
1138                   HANDLE_FDK_BITSTREAM bs) {
1139   CErRvlcInfo *pRvlc =
1140       &pAacDecoderChannelInfo->pComData->overlay.aac.erRvlcInfo;
1141   INT bitCntOffst;
1142   INT saveBitCnt;
1143 
1144   rvlcInit(pRvlc, pAacDecoderChannelInfo, bs);
1145 
1146   /* save bitstream position */
1147   saveBitCnt = (INT)FDKgetValidBits(bs);
1148 
1149   if (pRvlc->sf_escapes_present)
1150     rvlcDecodeEscapes(
1151         pRvlc, pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfEsc, bs);
1152 
1153   rvlcDecodeForward(pRvlc, pAacDecoderChannelInfo, bs);
1154   rvlcDecodeBackward(pRvlc, pAacDecoderChannelInfo, bs);
1155   rvlcFinalErrorDetection(pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo);
1156 
1157   pAacDecoderChannelInfo->pDynData->specificTo.aac.rvlcIntensityUsed =
1158       pRvlc->intensity_used;
1159   pAacDecoderChannelInfo->data.aac.PnsData.PnsActive = pRvlc->noise_used;
1160 
1161   /* restore bitstream position */
1162   bitCntOffst = (INT)FDKgetValidBits(bs) - saveBitCnt;
1163   if (bitCntOffst) {
1164     FDKpushBiDirectional(bs, bitCntOffst);
1165   }
1166 }
1167 
CRvlc_ElementCheck(CAacDecoderChannelInfo * pAacDecoderChannelInfo[],CAacDecoderStaticChannelInfo * pAacDecoderStaticChannelInfo[],const UINT flags,const INT elChannels)1168 void CRvlc_ElementCheck(
1169     CAacDecoderChannelInfo *pAacDecoderChannelInfo[],
1170     CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo[],
1171     const UINT flags, const INT elChannels) {
1172   int ch;
1173 
1174   /* Required for MPS residuals. */
1175   if (pAacDecoderStaticChannelInfo == NULL) {
1176     return;
1177   }
1178 
1179   /* RVLC specific sanity checks */
1180   if ((flags & AC_ER_RVLC) && (elChannels == 2)) { /* to be reviewed */
1181     if (((pAacDecoderChannelInfo[0]
1182               ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK == 0) ||
1183          (pAacDecoderChannelInfo[1]
1184               ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK == 0)) &&
1185         pAacDecoderChannelInfo[0]->pComData->jointStereoData.MsMaskPresent) {
1186       pAacDecoderChannelInfo[0]
1187           ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK = 0;
1188       pAacDecoderChannelInfo[1]
1189           ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK = 0;
1190     }
1191 
1192     if ((pAacDecoderChannelInfo[0]
1193              ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK == 0) &&
1194         (pAacDecoderChannelInfo[1]
1195              ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK == 1) &&
1196         (pAacDecoderChannelInfo[1]
1197              ->pDynData->specificTo.aac.rvlcIntensityUsed == 1)) {
1198       pAacDecoderChannelInfo[1]
1199           ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK = 0;
1200     }
1201   }
1202 
1203   for (ch = 0; ch < elChannels; ch++) {
1204     pAacDecoderStaticChannelInfo[ch]->concealmentInfo.rvlcPreviousBlockType =
1205         (GetWindowSequence(&pAacDecoderChannelInfo[ch]->icsInfo) == BLOCK_SHORT)
1206             ? 0
1207             : 1;
1208     if (flags & AC_ER_RVLC) {
1209       pAacDecoderStaticChannelInfo[ch]
1210           ->concealmentInfo.rvlcPreviousScaleFactorOK =
1211           pAacDecoderChannelInfo[ch]
1212               ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK;
1213     } else {
1214       pAacDecoderStaticChannelInfo[ch]
1215           ->concealmentInfo.rvlcPreviousScaleFactorOK = 0;
1216     }
1217   }
1218 }
1219