Home | History | Annotate | Download | only in src
      1 /* -----------------------------------------------------------------------------
      2 Software License for The Fraunhofer FDK AAC Codec Library for Android
      3 
      4  Copyright  1995 - 2018 Fraunhofer-Gesellschaft zur Frderung 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 (at) 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 
    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 
    172   pRvlc->bitstreamIndexRvlFwd = FDKgetBitCnt(
    173       bs); /* first bit within RVL coded block as start address for  forward
    174               decoding */
    175   pRvlc->bitstreamIndexRvlBwd = FDKgetBitCnt(bs) + pRvlc->length_of_rvlc_sf -
    176                                 1; /* last bit within RVL coded block as start
    177                                       address for backward decoding */
    178 
    179   /* skip RVLC-bitstream-part -- pointing now to escapes (if present) or to TNS
    180    * data (if present) */
    181   FDKpushFor(bs, pRvlc->length_of_rvlc_sf);
    182 
    183   if (pRvlc->sf_escapes_present != 0) {
    184     /* locate internal bitstream ptr at escapes (which is the second part) */
    185     FDKsyncCache(bs);
    186     pRvlc->bitstreamIndexEsc = FDKgetBitCnt(bs);
    187 
    188     /* skip escapeRVLC-bitstream-part -- pointing to TNS data (if present)   to
    189      * make decoder continue */
    190     /* decoding of RVLC should work despite this second pushFor during
    191      * initialization because        */
    192     /* bitstream initialization is valid for both ESC2 data parts (RVL-coded
    193      * values and ESC-coded values) */
    194     FDKpushFor(bs, pRvlc->length_of_rvlc_escapes);
    195   }
    196 }
    197 
    198 /*---------------------------------------------------------------------------------------------
    199      function:     rvlcCheckIntensityCb
    200 
    201      description:  Check if a intensity codebook is used in the current channel.
    202 -----------------------------------------------------------------------------------------------
    203         input:     - pointer rvlc structure
    204                    - pointer channel info structure
    205 -----------------------------------------------------------------------------------------------
    206         output:    - intensity_used: 0 no intensity codebook is used
    207                                      1 intensity codebook is used
    208 -----------------------------------------------------------------------------------------------
    209         return:    -
    210 --------------------------------------------------------------------------------------------
    211 */
    212 
    213 static void rvlcCheckIntensityCb(
    214     CErRvlcInfo *pRvlc, CAacDecoderChannelInfo *pAacDecoderChannelInfo) {
    215   int group, band, bnds;
    216 
    217   pRvlc->intensity_used = 0;
    218 
    219   for (group = 0; group < pRvlc->numWindowGroups; group++) {
    220     for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
    221       bnds = 16 * group + band;
    222       if ((pAacDecoderChannelInfo->pDynData->aCodeBook[bnds] ==
    223            INTENSITY_HCB) ||
    224           (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds] ==
    225            INTENSITY_HCB2)) {
    226         pRvlc->intensity_used = 1;
    227         break;
    228       }
    229     }
    230   }
    231 }
    232 
    233 /*---------------------------------------------------------------------------------------------
    234      function:     rvlcDecodeEscapeWord
    235 
    236      description:  Decode a huffman coded RVLC Escape-word. This value is part
    237 of a DPCM coded scalefactor.
    238 -----------------------------------------------------------------------------------------------
    239         input:     - pointer rvlc structure
    240 -----------------------------------------------------------------------------------------------
    241         return:    - a single RVLC-Escape value which had to be applied to a
    242 DPCM value (which has a absolute value of 7)
    243 --------------------------------------------------------------------------------------------
    244 */
    245 
    246 static SCHAR rvlcDecodeEscapeWord(CErRvlcInfo *pRvlc, HANDLE_FDK_BITSTREAM bs) {
    247   int i;
    248   SCHAR value;
    249   UCHAR carryBit;
    250   UINT treeNode;
    251   UINT branchValue;
    252   UINT branchNode;
    253 
    254   INT *pBitstreamIndexEsc;
    255   const UINT *pEscTree;
    256 
    257   pEscTree = pRvlc->pHuffTreeRvlcEscape;
    258   pBitstreamIndexEsc = &(pRvlc->bitstreamIndexEsc);
    259   treeNode = *pEscTree; /* init at starting node */
    260 
    261   for (i = MAX_LEN_RVLC_ESCAPE_WORD - 1; i >= 0; i--) {
    262     carryBit = rvlcReadBitFromBitstream(bs, /* get next bit */
    263                                         pBitstreamIndexEsc, FWD);
    264 
    265     CarryBitToBranchValue(carryBit, /* huffman decoding, do a single step in
    266                                        huffman decoding tree */
    267                           treeNode, &branchValue, &branchNode);
    268 
    269     if ((branchNode & TEST_BIT_10) ==
    270         TEST_BIT_10) { /* test bit 10 ; if set --> a RVLC-escape-word is
    271                           completely decoded */
    272       value = (SCHAR)branchNode & CLR_BIT_10;
    273       pRvlc->length_of_rvlc_escapes -= (MAX_LEN_RVLC_ESCAPE_WORD - i);
    274 
    275       if (pRvlc->length_of_rvlc_escapes < 0) {
    276         pRvlc->errorLogRvlc |= RVLC_ERROR_ALL_ESCAPE_WORDS_INVALID;
    277         value = -1;
    278       }
    279 
    280       return value;
    281     } else {
    282       treeNode = *(
    283           pEscTree +
    284           branchValue); /* update treeNode for further step in decoding tree */
    285     }
    286   }
    287 
    288   pRvlc->errorLogRvlc |= RVLC_ERROR_ALL_ESCAPE_WORDS_INVALID;
    289 
    290   return -1; /* should not be reached */
    291 }
    292 
    293 /*---------------------------------------------------------------------------------------------
    294      function:     rvlcDecodeEscapes
    295 
    296      description:  Decodes all huffman coded RVLC Escape Words.
    297                    Here a difference to the pseudo-code-implementation from
    298 standard can be found. A while loop (and not two nested for loops) is used for
    299 two reasons:
    300 
    301                    1. The plain huffman encoded escapes are decoded before the
    302 RVL-coded scalefactors. Therefore the escapes are present in the second step
    303                       when decoding the RVL-coded-scalefactor values in forward
    304 and backward direction.
    305 
    306                       When the RVL-coded scalefactors are decoded and there a
    307 escape is needed, then it is just taken out of the array in ascending order.
    308 
    309                    2. It's faster.
    310 -----------------------------------------------------------------------------------------------
    311         input:     - pointer rvlc structure
    312                    - handle to FDK bitstream
    313 -----------------------------------------------------------------------------------------------
    314         return:    - 0 ok     the decoded escapes seem to be valid
    315                    - 1 error  there was a error detected during decoding escapes
    316                               --> all escapes are invalid
    317 --------------------------------------------------------------------------------------------
    318 */
    319 
    320 static void rvlcDecodeEscapes(CErRvlcInfo *pRvlc, SHORT *pEsc,
    321                               HANDLE_FDK_BITSTREAM bs) {
    322   SCHAR escWord;
    323   SCHAR escCnt = 0;
    324   SHORT *pEscBitCntSum;
    325 
    326   pEscBitCntSum = &(pRvlc->length_of_rvlc_escapes);
    327 
    328   /* Decode all RVLC-Escape words with a plain Huffman-Decoder */
    329   while (*pEscBitCntSum > 0) {
    330     escWord = rvlcDecodeEscapeWord(pRvlc, bs);
    331 
    332     if (escWord >= 0) {
    333       pEsc[escCnt] = escWord;
    334       escCnt++;
    335     } else {
    336       pRvlc->errorLogRvlc |= RVLC_ERROR_ALL_ESCAPE_WORDS_INVALID;
    337       pRvlc->numDecodedEscapeWordsEsc = escCnt;
    338 
    339       return;
    340     }
    341   } /* all RVLC escapes decoded */
    342 
    343   pRvlc->numDecodedEscapeWordsEsc = escCnt;
    344 }
    345 
    346 /*---------------------------------------------------------------------------------------------
    347      function:     decodeRVLCodeword
    348 
    349      description:  Decodes a RVL-coded dpcm-word (-part).
    350 -----------------------------------------------------------------------------------------------
    351         input:     - FDK bitstream handle
    352                    - pointer rvlc structure
    353 -----------------------------------------------------------------------------------------------
    354         return:    - a dpcm value which is within range [0,1,..,14] in case of
    355 no errors. The offset of 7 must be subtracted to get a valid dpcm scalefactor
    356 value. In case of errors a forbidden codeword is detected --> returning -1
    357 --------------------------------------------------------------------------------------------
    358 */
    359 
    360 SCHAR decodeRVLCodeword(HANDLE_FDK_BITSTREAM bs, CErRvlcInfo *pRvlc) {
    361   int i;
    362   SCHAR value;
    363   UCHAR carryBit;
    364   UINT branchValue;
    365   UINT branchNode;
    366 
    367   const UINT *pRvlCodeTree = pRvlc->pHuffTreeRvlCodewds;
    368   UCHAR direction = pRvlc->direction;
    369   INT *pBitstrIndxRvl = pRvlc->pBitstrIndxRvl_RVL;
    370   UINT treeNode = *pRvlCodeTree;
    371 
    372   for (i = MAX_LEN_RVLC_CODE_WORD - 1; i >= 0; i--) {
    373     carryBit = rvlcReadBitFromBitstream(bs, /* get next bit */
    374                                         pBitstrIndxRvl, direction);
    375 
    376     CarryBitToBranchValue(carryBit, /* huffman decoding, do a single step in
    377                                        huffman decoding tree */
    378                           treeNode, &branchValue, &branchNode);
    379 
    380     if ((branchNode & TEST_BIT_10) ==
    381         TEST_BIT_10) { /* test bit 10 ; if set --> a
    382                           RVLC-codeword is completely decoded
    383                         */
    384       value = (SCHAR)(branchNode & CLR_BIT_10);
    385       *pRvlc->pRvlBitCnt_RVL -= (MAX_LEN_RVLC_CODE_WORD - i);
    386 
    387       /* check available bits for decoding */
    388       if (*pRvlc->pRvlBitCnt_RVL < 0) {
    389         if (direction == FWD) {
    390           pRvlc->errorLogRvlc |= RVLC_ERROR_RVL_SUM_BIT_COUNTER_BELOW_ZERO_FWD;
    391         } else {
    392           pRvlc->errorLogRvlc |= RVLC_ERROR_RVL_SUM_BIT_COUNTER_BELOW_ZERO_BWD;
    393         }
    394         value = -1; /* signalize an error in return value, because too many bits
    395                        was decoded */
    396       }
    397 
    398       /* check max value of dpcm value */
    399       if (value > MAX_ALLOWED_DPCM_INDEX) {
    400         if (direction == FWD) {
    401           pRvlc->errorLogRvlc |= RVLC_ERROR_FORBIDDEN_CW_DETECTED_FWD;
    402         } else {
    403           pRvlc->errorLogRvlc |= RVLC_ERROR_FORBIDDEN_CW_DETECTED_BWD;
    404         }
    405         value = -1; /* signalize an error in return value, because a forbidden
    406                        cw was detected*/
    407       }
    408 
    409       return value; /* return a dpcm value with offset +7 or an error status */
    410     } else {
    411       treeNode = *(
    412           pRvlCodeTree +
    413           branchValue); /* update treeNode for further step in decoding tree */
    414     }
    415   }
    416 
    417   return -1;
    418 }
    419 
    420 /*---------------------------------------------------------------------------------------------
    421      function:     rvlcDecodeForward
    422 
    423      description:  Decode RVL-coded codewords in forward direction.
    424 -----------------------------------------------------------------------------------------------
    425         input:     - pointer rvlc structure
    426                    - pointer channel info structure
    427                    - handle to FDK bitstream
    428 -----------------------------------------------------------------------------------------------
    429         return:    -
    430 --------------------------------------------------------------------------------------------
    431 */
    432 
    433 static void rvlcDecodeForward(CErRvlcInfo *pRvlc,
    434                               CAacDecoderChannelInfo *pAacDecoderChannelInfo,
    435                               HANDLE_FDK_BITSTREAM bs) {
    436   int band = 0;
    437   int group = 0;
    438   int bnds = 0;
    439 
    440   SHORT dpcm;
    441 
    442   SHORT factor =
    443       pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain - SF_OFFSET;
    444   SHORT position = -SF_OFFSET;
    445   SHORT noisenrg = pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain -
    446                    SF_OFFSET - 90 - 256;
    447 
    448   SHORT *pScfFwd = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd;
    449   SHORT *pScfEsc = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfEsc;
    450   UCHAR *pEscFwdCnt = &(pRvlc->numDecodedEscapeWordsFwd);
    451 
    452   pRvlc->pRvlBitCnt_RVL = &(pRvlc->length_of_rvlc_sf_fwd);
    453   pRvlc->pBitstrIndxRvl_RVL = &(pRvlc->bitstreamIndexRvlFwd);
    454 
    455   *pEscFwdCnt = 0;
    456   pRvlc->direction = FWD;
    457   pRvlc->noise_used = 0;
    458   pRvlc->sf_used = 0;
    459   pRvlc->lastScf = 0;
    460   pRvlc->lastNrg = 0;
    461   pRvlc->lastIs = 0;
    462 
    463   rvlcCheckIntensityCb(pRvlc, pAacDecoderChannelInfo);
    464 
    465   /* main loop fwd long */
    466   for (group = 0; group < pRvlc->numWindowGroups; group++) {
    467     for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
    468       bnds = 16 * group + band;
    469 
    470       switch (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds]) {
    471         case ZERO_HCB:
    472           pScfFwd[bnds] = 0;
    473           break;
    474 
    475         case INTENSITY_HCB2:
    476         case INTENSITY_HCB:
    477           /* store dpcm_is_position */
    478           dpcm = decodeRVLCodeword(bs, pRvlc);
    479           if (dpcm < 0) {
    480             pRvlc->conceal_max = bnds;
    481             return;
    482           }
    483           dpcm -= TABLE_OFFSET;
    484           if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
    485             if (pRvlc->length_of_rvlc_escapes) {
    486               pRvlc->conceal_max = bnds;
    487               return;
    488             } else {
    489               if (dpcm == MIN_RVL) {
    490                 dpcm -= *pScfEsc++;
    491               } else {
    492                 dpcm += *pScfEsc++;
    493               }
    494               (*pEscFwdCnt)++;
    495               if (pRvlc->conceal_max_esc == CONCEAL_MAX_INIT) {
    496                 pRvlc->conceal_max_esc = bnds;
    497               }
    498             }
    499           }
    500           position += dpcm;
    501           pScfFwd[bnds] = position;
    502           pRvlc->lastIs = position;
    503           break;
    504 
    505         case NOISE_HCB:
    506           if (pRvlc->noise_used == 0) {
    507             pRvlc->noise_used = 1;
    508             pRvlc->first_noise_band = bnds;
    509             noisenrg += pRvlc->dpcm_noise_nrg;
    510             pScfFwd[bnds] = 100 + noisenrg;
    511             pRvlc->lastNrg = noisenrg;
    512           } else {
    513             dpcm = decodeRVLCodeword(bs, pRvlc);
    514             if (dpcm < 0) {
    515               pRvlc->conceal_max = bnds;
    516               return;
    517             }
    518             dpcm -= TABLE_OFFSET;
    519             if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
    520               if (pRvlc->length_of_rvlc_escapes) {
    521                 pRvlc->conceal_max = bnds;
    522                 return;
    523               } else {
    524                 if (dpcm == MIN_RVL) {
    525                   dpcm -= *pScfEsc++;
    526                 } else {
    527                   dpcm += *pScfEsc++;
    528                 }
    529                 (*pEscFwdCnt)++;
    530                 if (pRvlc->conceal_max_esc == CONCEAL_MAX_INIT) {
    531                   pRvlc->conceal_max_esc = bnds;
    532                 }
    533               }
    534             }
    535             noisenrg += dpcm;
    536             pScfFwd[bnds] = 100 + noisenrg;
    537             pRvlc->lastNrg = noisenrg;
    538           }
    539           pAacDecoderChannelInfo->data.aac.PnsData.pnsUsed[bnds] = 1;
    540           break;
    541 
    542         default:
    543           pRvlc->sf_used = 1;
    544           dpcm = decodeRVLCodeword(bs, pRvlc);
    545           if (dpcm < 0) {
    546             pRvlc->conceal_max = bnds;
    547             return;
    548           }
    549           dpcm -= TABLE_OFFSET;
    550           if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
    551             if (pRvlc->length_of_rvlc_escapes) {
    552               pRvlc->conceal_max = bnds;
    553               return;
    554             } else {
    555               if (dpcm == MIN_RVL) {
    556                 dpcm -= *pScfEsc++;
    557               } else {
    558                 dpcm += *pScfEsc++;
    559               }
    560               (*pEscFwdCnt)++;
    561               if (pRvlc->conceal_max_esc == CONCEAL_MAX_INIT) {
    562                 pRvlc->conceal_max_esc = bnds;
    563               }
    564             }
    565           }
    566           factor += dpcm;
    567           pScfFwd[bnds] = factor;
    568           pRvlc->lastScf = factor;
    569           break;
    570       }
    571     }
    572   }
    573 
    574   /* postfetch fwd long */
    575   if (pRvlc->intensity_used) {
    576     dpcm = decodeRVLCodeword(bs, pRvlc); /* dpcm_is_last_position */
    577     if (dpcm < 0) {
    578       pRvlc->conceal_max = bnds;
    579       return;
    580     }
    581     dpcm -= TABLE_OFFSET;
    582     if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
    583       if (pRvlc->length_of_rvlc_escapes) {
    584         pRvlc->conceal_max = bnds;
    585         return;
    586       } else {
    587         if (dpcm == MIN_RVL) {
    588           dpcm -= *pScfEsc++;
    589         } else {
    590           dpcm += *pScfEsc++;
    591         }
    592         (*pEscFwdCnt)++;
    593         if (pRvlc->conceal_max_esc == CONCEAL_MAX_INIT) {
    594           pRvlc->conceal_max_esc = bnds;
    595         }
    596       }
    597     }
    598     pRvlc->dpcm_is_last_position = dpcm;
    599   }
    600 }
    601 
    602 /*---------------------------------------------------------------------------------------------
    603      function:     rvlcDecodeBackward
    604 
    605      description:  Decode RVL-coded codewords in backward direction.
    606 -----------------------------------------------------------------------------------------------
    607         input:     - pointer rvlc structure
    608                    - pointer channel info structure
    609                    - handle FDK bitstream
    610 -----------------------------------------------------------------------------------------------
    611         return:    -
    612 --------------------------------------------------------------------------------------------
    613 */
    614 
    615 static void rvlcDecodeBackward(CErRvlcInfo *pRvlc,
    616                                CAacDecoderChannelInfo *pAacDecoderChannelInfo,
    617                                HANDLE_FDK_BITSTREAM bs) {
    618   SHORT band, group, dpcm, offset;
    619   SHORT bnds = pRvlc->maxSfbTransmitted - 1;
    620 
    621   SHORT factor = pRvlc->rev_global_gain - SF_OFFSET;
    622   SHORT position = pRvlc->dpcm_is_last_position - SF_OFFSET;
    623   SHORT noisenrg = pRvlc->rev_global_gain + pRvlc->dpcm_noise_last_position -
    624                    SF_OFFSET - 90 - 256;
    625 
    626   SHORT *pScfBwd = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd;
    627   SHORT *pScfEsc = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfEsc;
    628   UCHAR *pEscEscCnt = &(pRvlc->numDecodedEscapeWordsEsc);
    629   UCHAR *pEscBwdCnt = &(pRvlc->numDecodedEscapeWordsBwd);
    630 
    631   pRvlc->pRvlBitCnt_RVL = &(pRvlc->length_of_rvlc_sf_bwd);
    632   pRvlc->pBitstrIndxRvl_RVL = &(pRvlc->bitstreamIndexRvlBwd);
    633 
    634   *pEscBwdCnt = 0;
    635   pRvlc->direction = BWD;
    636   pScfEsc += *pEscEscCnt - 1; /* set pScfEsc to last entry */
    637   pRvlc->firstScf = 0;
    638   pRvlc->firstNrg = 0;
    639   pRvlc->firstIs = 0;
    640 
    641   /* prefetch long BWD */
    642   if (pRvlc->intensity_used) {
    643     dpcm = decodeRVLCodeword(bs, pRvlc); /* dpcm_is_last_position */
    644     if (dpcm < 0) {
    645       pRvlc->dpcm_is_last_position = 0;
    646       pRvlc->conceal_min = bnds;
    647       return;
    648     }
    649     dpcm -= TABLE_OFFSET;
    650     if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
    651       if (pRvlc->length_of_rvlc_escapes) {
    652         pRvlc->conceal_min = bnds;
    653         return;
    654       } else {
    655         if (dpcm == MIN_RVL) {
    656           dpcm -= *pScfEsc--;
    657         } else {
    658           dpcm += *pScfEsc--;
    659         }
    660         (*pEscBwdCnt)++;
    661         if (pRvlc->conceal_min_esc == CONCEAL_MIN_INIT) {
    662           pRvlc->conceal_min_esc = bnds;
    663         }
    664       }
    665     }
    666     pRvlc->dpcm_is_last_position = dpcm;
    667   }
    668 
    669   /* main loop long BWD */
    670   for (group = pRvlc->numWindowGroups - 1; group >= 0; group--) {
    671     for (band = pRvlc->maxSfbTransmitted - 1; band >= 0; band--) {
    672       bnds = 16 * group + band;
    673       if ((band == 0) && (pRvlc->numWindowGroups != 1))
    674         offset = 16 - pRvlc->maxSfbTransmitted + 1;
    675       else
    676         offset = 1;
    677 
    678       switch (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds]) {
    679         case ZERO_HCB:
    680           pScfBwd[bnds] = 0;
    681           break;
    682 
    683         case INTENSITY_HCB2:
    684         case INTENSITY_HCB:
    685           /* store dpcm_is_position */
    686           dpcm = decodeRVLCodeword(bs, pRvlc);
    687           if (dpcm < 0) {
    688             pScfBwd[bnds] = position;
    689             pRvlc->conceal_min = fMax(0, bnds - offset);
    690             return;
    691           }
    692           dpcm -= TABLE_OFFSET;
    693           if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
    694             if (pRvlc->length_of_rvlc_escapes) {
    695               pScfBwd[bnds] = position;
    696               pRvlc->conceal_min = fMax(0, bnds - offset);
    697               return;
    698             } else {
    699               if (dpcm == MIN_RVL) {
    700                 dpcm -= *pScfEsc--;
    701               } else {
    702                 dpcm += *pScfEsc--;
    703               }
    704               (*pEscBwdCnt)++;
    705               if (pRvlc->conceal_min_esc == CONCEAL_MIN_INIT) {
    706                 pRvlc->conceal_min_esc = fMax(0, bnds - offset);
    707               }
    708             }
    709           }
    710           pScfBwd[bnds] = position;
    711           position -= dpcm;
    712           pRvlc->firstIs = position;
    713           break;
    714 
    715         case NOISE_HCB:
    716           if (bnds == pRvlc->first_noise_band) {
    717             pScfBwd[bnds] =
    718                 pRvlc->dpcm_noise_nrg +
    719                 pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain -
    720                 SF_OFFSET - 90 - 256;
    721             pRvlc->firstNrg = pScfBwd[bnds];
    722           } else {
    723             dpcm = decodeRVLCodeword(bs, pRvlc);
    724             if (dpcm < 0) {
    725               pScfBwd[bnds] = noisenrg;
    726               pRvlc->conceal_min = fMax(0, bnds - offset);
    727               return;
    728             }
    729             dpcm -= TABLE_OFFSET;
    730             if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
    731               if (pRvlc->length_of_rvlc_escapes) {
    732                 pScfBwd[bnds] = noisenrg;
    733                 pRvlc->conceal_min = fMax(0, bnds - offset);
    734                 return;
    735               } else {
    736                 if (dpcm == MIN_RVL) {
    737                   dpcm -= *pScfEsc--;
    738                 } else {
    739                   dpcm += *pScfEsc--;
    740                 }
    741                 (*pEscBwdCnt)++;
    742                 if (pRvlc->conceal_min_esc == CONCEAL_MIN_INIT) {
    743                   pRvlc->conceal_min_esc = fMax(0, bnds - offset);
    744                 }
    745               }
    746             }
    747             pScfBwd[bnds] = noisenrg;
    748             noisenrg -= dpcm;
    749             pRvlc->firstNrg = noisenrg;
    750           }
    751           break;
    752 
    753         default:
    754           dpcm = decodeRVLCodeword(bs, pRvlc);
    755           if (dpcm < 0) {
    756             pScfBwd[bnds] = factor;
    757             pRvlc->conceal_min = fMax(0, bnds - offset);
    758             return;
    759           }
    760           dpcm -= TABLE_OFFSET;
    761           if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
    762             if (pRvlc->length_of_rvlc_escapes) {
    763               pScfBwd[bnds] = factor;
    764               pRvlc->conceal_min = fMax(0, bnds - offset);
    765               return;
    766             } else {
    767               if (dpcm == MIN_RVL) {
    768                 dpcm -= *pScfEsc--;
    769               } else {
    770                 dpcm += *pScfEsc--;
    771               }
    772               (*pEscBwdCnt)++;
    773               if (pRvlc->conceal_min_esc == CONCEAL_MIN_INIT) {
    774                 pRvlc->conceal_min_esc = fMax(0, bnds - offset);
    775               }
    776             }
    777           }
    778           pScfBwd[bnds] = factor;
    779           factor -= dpcm;
    780           pRvlc->firstScf = factor;
    781           break;
    782       }
    783     }
    784   }
    785 }
    786 
    787 /*---------------------------------------------------------------------------------------------
    788      function:     rvlcFinalErrorDetection
    789 
    790      description:  Call RVLC concealment if error was detected in decoding
    791 process
    792 -----------------------------------------------------------------------------------------------
    793         input:     - pointer rvlc structure
    794                    - pointer channel info structure
    795 -----------------------------------------------------------------------------------------------
    796         return:    -
    797 --------------------------------------------------------------------------------------------
    798 */
    799 
    800 static void rvlcFinalErrorDetection(
    801     CAacDecoderChannelInfo *pAacDecoderChannelInfo,
    802     CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo) {
    803   CErRvlcInfo *pRvlc =
    804       &pAacDecoderChannelInfo->pComData->overlay.aac.erRvlcInfo;
    805   UCHAR ErrorStatusComplete = 0;
    806   UCHAR ErrorStatusLengthFwd = 0;
    807   UCHAR ErrorStatusLengthBwd = 0;
    808   UCHAR ErrorStatusLengthEscapes = 0;
    809   UCHAR ErrorStatusFirstScf = 0;
    810   UCHAR ErrorStatusLastScf = 0;
    811   UCHAR ErrorStatusFirstNrg = 0;
    812   UCHAR ErrorStatusLastNrg = 0;
    813   UCHAR ErrorStatusFirstIs = 0;
    814   UCHAR ErrorStatusLastIs = 0;
    815   UCHAR ErrorStatusForbiddenCwFwd = 0;
    816   UCHAR ErrorStatusForbiddenCwBwd = 0;
    817   UCHAR ErrorStatusNumEscapesFwd = 0;
    818   UCHAR ErrorStatusNumEscapesBwd = 0;
    819   UCHAR ConcealStatus = 1;
    820   UCHAR currentBlockType; /* short: 0, not short: 1*/
    821 
    822   pAacDecoderChannelInfo->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK = 1;
    823 
    824   /* invalid escape words, bit counter unequal zero, forbidden codeword detected
    825    */
    826   if (pRvlc->errorLogRvlc & RVLC_ERROR_FORBIDDEN_CW_DETECTED_FWD)
    827     ErrorStatusForbiddenCwFwd = 1;
    828 
    829   if (pRvlc->errorLogRvlc & RVLC_ERROR_FORBIDDEN_CW_DETECTED_BWD)
    830     ErrorStatusForbiddenCwBwd = 1;
    831 
    832   /* bit counter forward unequal zero */
    833   if (pRvlc->length_of_rvlc_sf_fwd) ErrorStatusLengthFwd = 1;
    834 
    835   /* bit counter backward unequal zero */
    836   if (pRvlc->length_of_rvlc_sf_bwd) ErrorStatusLengthBwd = 1;
    837 
    838   /* bit counter escape sequences unequal zero */
    839   if (pRvlc->sf_escapes_present)
    840     if (pRvlc->length_of_rvlc_escapes) ErrorStatusLengthEscapes = 1;
    841 
    842   if (pRvlc->sf_used) {
    843     /* first decoded scf does not match to global gain in backward direction */
    844     if (pRvlc->firstScf !=
    845         (pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain - SF_OFFSET))
    846       ErrorStatusFirstScf = 1;
    847 
    848     /* last decoded scf does not match to rev global gain in forward direction
    849      */
    850     if (pRvlc->lastScf != (pRvlc->rev_global_gain - SF_OFFSET))
    851       ErrorStatusLastScf = 1;
    852   }
    853 
    854   if (pRvlc->noise_used) {
    855     /* first decoded nrg does not match to dpcm_noise_nrg in backward direction
    856      */
    857     if (pRvlc->firstNrg !=
    858         (pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain +
    859          pRvlc->dpcm_noise_nrg - SF_OFFSET - 90 - 256))
    860       ErrorStatusFirstNrg = 1;
    861 
    862     /* last decoded nrg does not match to dpcm_noise_last_position in forward
    863      * direction */
    864     if (pRvlc->lastNrg !=
    865         (pRvlc->rev_global_gain + pRvlc->dpcm_noise_last_position - SF_OFFSET -
    866          90 - 256))
    867       ErrorStatusLastNrg = 1;
    868   }
    869 
    870   if (pRvlc->intensity_used) {
    871     /* first decoded is position does not match in backward direction */
    872     if (pRvlc->firstIs != (-SF_OFFSET)) ErrorStatusFirstIs = 1;
    873 
    874     /* last decoded is position does not match in forward direction */
    875     if (pRvlc->lastIs != (pRvlc->dpcm_is_last_position - SF_OFFSET))
    876       ErrorStatusLastIs = 1;
    877   }
    878 
    879   /* decoded escapes and used escapes in forward direction do not fit */
    880   if ((pRvlc->numDecodedEscapeWordsFwd != pRvlc->numDecodedEscapeWordsEsc) &&
    881       (pRvlc->conceal_max == CONCEAL_MAX_INIT)) {
    882     ErrorStatusNumEscapesFwd = 1;
    883   }
    884 
    885   /* decoded escapes and used escapes in backward direction do not fit */
    886   if ((pRvlc->numDecodedEscapeWordsBwd != pRvlc->numDecodedEscapeWordsEsc) &&
    887       (pRvlc->conceal_min == CONCEAL_MIN_INIT)) {
    888     ErrorStatusNumEscapesBwd = 1;
    889   }
    890 
    891   if (ErrorStatusLengthEscapes ||
    892       (((pRvlc->conceal_max == CONCEAL_MAX_INIT) &&
    893         (pRvlc->numDecodedEscapeWordsFwd != pRvlc->numDecodedEscapeWordsEsc) &&
    894         (ErrorStatusLastScf || ErrorStatusLastNrg || ErrorStatusLastIs))
    895 
    896        &&
    897 
    898        ((pRvlc->conceal_min == CONCEAL_MIN_INIT) &&
    899         (pRvlc->numDecodedEscapeWordsBwd != pRvlc->numDecodedEscapeWordsEsc) &&
    900         (ErrorStatusFirstScf || ErrorStatusFirstNrg || ErrorStatusFirstIs))) ||
    901       ((pRvlc->conceal_max == CONCEAL_MAX_INIT) &&
    902        ((pRvlc->rev_global_gain - SF_OFFSET - pRvlc->lastScf) < -15)) ||
    903       ((pRvlc->conceal_min == CONCEAL_MIN_INIT) &&
    904        ((pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain - SF_OFFSET -
    905          pRvlc->firstScf) < -15))) {
    906     if ((pRvlc->conceal_max == CONCEAL_MAX_INIT) ||
    907         (pRvlc->conceal_min == CONCEAL_MIN_INIT)) {
    908       pRvlc->conceal_max = 0;
    909       pRvlc->conceal_min = fMax(
    910           0, (pRvlc->numWindowGroups - 1) * 16 + pRvlc->maxSfbTransmitted - 1);
    911     } else {
    912       pRvlc->conceal_max = fMin(pRvlc->conceal_max, pRvlc->conceal_max_esc);
    913       pRvlc->conceal_min = fMax(pRvlc->conceal_min, pRvlc->conceal_min_esc);
    914     }
    915   }
    916 
    917   ErrorStatusComplete = ErrorStatusLastScf || ErrorStatusFirstScf ||
    918                         ErrorStatusLastNrg || ErrorStatusFirstNrg ||
    919                         ErrorStatusLastIs || ErrorStatusFirstIs ||
    920                         ErrorStatusForbiddenCwFwd ||
    921                         ErrorStatusForbiddenCwBwd || ErrorStatusLengthFwd ||
    922                         ErrorStatusLengthBwd || ErrorStatusLengthEscapes ||
    923                         ErrorStatusNumEscapesFwd || ErrorStatusNumEscapesBwd;
    924 
    925   currentBlockType =
    926       (GetWindowSequence(&pAacDecoderChannelInfo->icsInfo) == BLOCK_SHORT) ? 0
    927                                                                            : 1;
    928 
    929   if (!ErrorStatusComplete) {
    930     int band;
    931     int group;
    932     int bnds;
    933     int lastSfbIndex;
    934 
    935     lastSfbIndex = (pRvlc->numWindowGroups > 1) ? 16 : 64;
    936 
    937     for (group = 0; group < pRvlc->numWindowGroups; group++) {
    938       for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
    939         bnds = 16 * group + band;
    940         pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
    941             pAacDecoderStaticChannelInfo->concealmentInfo
    942                 .aRvlcPreviousScaleFactor[bnds] =
    943                 pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds];
    944       }
    945     }
    946 
    947     for (group = 0; group < pRvlc->numWindowGroups; group++) {
    948       for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
    949         bnds = 16 * group + band;
    950         pAacDecoderStaticChannelInfo->concealmentInfo
    951             .aRvlcPreviousCodebook[bnds] =
    952             pAacDecoderChannelInfo->pDynData->aCodeBook[bnds];
    953       }
    954       for (; band < lastSfbIndex; band++) {
    955         bnds = 16 * group + band;
    956         FDK_ASSERT(bnds >= 0 && bnds < RVLC_MAX_SFB);
    957         pAacDecoderStaticChannelInfo->concealmentInfo
    958             .aRvlcPreviousCodebook[bnds] = ZERO_HCB;
    959       }
    960     }
    961   } else {
    962     int band;
    963     int group;
    964 
    965     /* A single bit error was detected in decoding of dpcm values. It also could
    966        be an error with more bits in decoding of escapes and dpcm values whereby
    967        an illegal codeword followed not directly after the corrupted bits but
    968        just after decoding some more (wrong) scalefactors. Use the smaller
    969        scalefactor from forward decoding, backward decoding and previous frame.
    970      */
    971     if (((pRvlc->conceal_min != CONCEAL_MIN_INIT) ||
    972          (pRvlc->conceal_max != CONCEAL_MAX_INIT)) &&
    973         (pRvlc->conceal_min <= pRvlc->conceal_max) &&
    974         (pAacDecoderStaticChannelInfo->concealmentInfo.rvlcPreviousBlockType ==
    975          currentBlockType) &&
    976         pAacDecoderStaticChannelInfo->concealmentInfo
    977             .rvlcPreviousScaleFactorOK &&
    978         pRvlc->sf_concealment && ConcealStatus) {
    979       BidirectionalEstimation_UseScfOfPrevFrameAsReference(
    980           pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo);
    981       ConcealStatus = 0;
    982     }
    983 
    984     /* A single bit error was detected in decoding of dpcm values. It also could
    985        be an error with more bits in decoding of escapes and dpcm values whereby
    986        an illegal codeword followed not directly after the corrupted bits but
    987        just after decoding some more (wrong) scalefactors. Use the smaller
    988        scalefactor from forward and backward decoding. */
    989     if ((pRvlc->conceal_min <= pRvlc->conceal_max) &&
    990         ((pRvlc->conceal_min != CONCEAL_MIN_INIT) ||
    991          (pRvlc->conceal_max != CONCEAL_MAX_INIT)) &&
    992         !(pAacDecoderStaticChannelInfo->concealmentInfo
    993               .rvlcPreviousScaleFactorOK &&
    994           pRvlc->sf_concealment &&
    995           (pAacDecoderStaticChannelInfo->concealmentInfo
    996                .rvlcPreviousBlockType == currentBlockType)) &&
    997         ConcealStatus) {
    998       BidirectionalEstimation_UseLowerScfOfCurrentFrame(pAacDecoderChannelInfo);
    999       ConcealStatus = 0;
   1000     }
   1001 
   1002     /* No errors were detected in decoding of escapes and dpcm values however
   1003        the first and last value of a group (is,nrg,sf) is incorrect */
   1004     if ((pRvlc->conceal_min <= pRvlc->conceal_max) &&
   1005         ((ErrorStatusLastScf && ErrorStatusFirstScf) ||
   1006          (ErrorStatusLastNrg && ErrorStatusFirstNrg) ||
   1007          (ErrorStatusLastIs && ErrorStatusFirstIs)) &&
   1008         !(ErrorStatusForbiddenCwFwd || ErrorStatusForbiddenCwBwd ||
   1009           ErrorStatusLengthEscapes) &&
   1010         ConcealStatus) {
   1011       StatisticalEstimation(pAacDecoderChannelInfo);
   1012       ConcealStatus = 0;
   1013     }
   1014 
   1015     /* A error with more bits in decoding of escapes and dpcm values was
   1016        detected. Use the smaller scalefactor from forward decoding, backward
   1017        decoding and previous frame. */
   1018     if ((pRvlc->conceal_min <= pRvlc->conceal_max) &&
   1019         pAacDecoderStaticChannelInfo->concealmentInfo
   1020             .rvlcPreviousScaleFactorOK &&
   1021         pRvlc->sf_concealment &&
   1022         (pAacDecoderStaticChannelInfo->concealmentInfo.rvlcPreviousBlockType ==
   1023          currentBlockType) &&
   1024         ConcealStatus) {
   1025       PredictiveInterpolation(pAacDecoderChannelInfo,
   1026                               pAacDecoderStaticChannelInfo);
   1027       ConcealStatus = 0;
   1028     }
   1029 
   1030     /* Call frame concealment, because no better strategy was found. Setting the
   1031        scalefactors to zero is done for debugging purposes */
   1032     if (ConcealStatus) {
   1033       for (group = 0; group < pRvlc->numWindowGroups; group++) {
   1034         for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
   1035           pAacDecoderChannelInfo->pDynData->aScaleFactor[16 * group + band] = 0;
   1036         }
   1037       }
   1038       pAacDecoderChannelInfo->pDynData->specificTo.aac
   1039           .rvlcCurrentScaleFactorOK = 0;
   1040     }
   1041   }
   1042 }
   1043 
   1044 /*---------------------------------------------------------------------------------------------
   1045      function:     CRvlc_Read
   1046 
   1047      description:  Read RVLC ESC1 data (side info) from bitstream.
   1048 -----------------------------------------------------------------------------------------------
   1049         input:     - pointer rvlc structure
   1050                    - pointer channel info structure
   1051                    - pointer bitstream structure
   1052 -----------------------------------------------------------------------------------------------
   1053         return:    -
   1054 --------------------------------------------------------------------------------------------
   1055 */
   1056 
   1057 void CRvlc_Read(CAacDecoderChannelInfo *pAacDecoderChannelInfo,
   1058                 HANDLE_FDK_BITSTREAM bs) {
   1059   CErRvlcInfo *pRvlc =
   1060       &pAacDecoderChannelInfo->pComData->overlay.aac.erRvlcInfo;
   1061 
   1062   int group, band;
   1063 
   1064   /* RVLC long specific initialization  Init part 1 of 2 */
   1065   pRvlc->numWindowGroups = GetWindowGroups(&pAacDecoderChannelInfo->icsInfo);
   1066   pRvlc->maxSfbTransmitted =
   1067       GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
   1068   pRvlc->noise_used = 0;               /* noise detection */
   1069   pRvlc->dpcm_noise_nrg = 0;           /* only for debugging */
   1070   pRvlc->dpcm_noise_last_position = 0; /* only for debugging */
   1071   pRvlc->length_of_rvlc_escapes =
   1072       -1; /* default value is used for error detection and concealment */
   1073 
   1074   /* read only error sensitivity class 1 data (ESC 1 - data) */
   1075   pRvlc->sf_concealment = FDKreadBits(bs, 1);  /* #1 */
   1076   pRvlc->rev_global_gain = FDKreadBits(bs, 8); /* #2 */
   1077 
   1078   if (GetWindowSequence(&pAacDecoderChannelInfo->icsInfo) == BLOCK_SHORT) {
   1079     pRvlc->length_of_rvlc_sf = FDKreadBits(bs, 11); /* #3 */
   1080   } else {
   1081     pRvlc->length_of_rvlc_sf = FDKreadBits(bs, 9); /* #3 */
   1082   }
   1083 
   1084   /* check if noise codebook is used */
   1085   for (group = 0; group < pRvlc->numWindowGroups; group++) {
   1086     for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
   1087       if (pAacDecoderChannelInfo->pDynData->aCodeBook[16 * group + band] ==
   1088           NOISE_HCB) {
   1089         pRvlc->noise_used = 1;
   1090         break;
   1091       }
   1092     }
   1093   }
   1094 
   1095   if (pRvlc->noise_used)
   1096     pRvlc->dpcm_noise_nrg = FDKreadBits(bs, 9); /* #4  PNS */
   1097 
   1098   pRvlc->sf_escapes_present = FDKreadBits(bs, 1); /* #5      */
   1099 
   1100   if (pRvlc->sf_escapes_present) {
   1101     pRvlc->length_of_rvlc_escapes = FDKreadBits(bs, 8); /* #6      */
   1102   }
   1103 
   1104   if (pRvlc->noise_used) {
   1105     pRvlc->dpcm_noise_last_position = FDKreadBits(bs, 9); /* #7  PNS */
   1106     pRvlc->length_of_rvlc_sf -= 9;
   1107   }
   1108 
   1109   pRvlc->length_of_rvlc_sf_fwd = pRvlc->length_of_rvlc_sf;
   1110   pRvlc->length_of_rvlc_sf_bwd = pRvlc->length_of_rvlc_sf;
   1111 }
   1112 
   1113 /*---------------------------------------------------------------------------------------------
   1114      function:     CRvlc_Decode
   1115 
   1116      description:  Decode rvlc data
   1117                    The function reads both the escape sequences and the
   1118 scalefactors in forward and backward direction. If an error occured during
   1119 decoding process which can not be concealed with the rvlc concealment frame
   1120 concealment will be initiated. Then the element "rvlcCurrentScaleFactorOK" in
   1121 the decoder channel info is set to 0 otherwise it is set to 1.
   1122 -----------------------------------------------------------------------------------------------
   1123         input:     - pointer rvlc structure
   1124                    - pointer channel info structure
   1125                    - pointer to persistent channel info structure
   1126                    - pointer bitstream structure
   1127 -----------------------------------------------------------------------------------------------
   1128         return:    ErrorStatus = AAC_DEC_OK
   1129 --------------------------------------------------------------------------------------------
   1130 */
   1131 
   1132 void CRvlc_Decode(CAacDecoderChannelInfo *pAacDecoderChannelInfo,
   1133                   CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
   1134                   HANDLE_FDK_BITSTREAM bs) {
   1135   CErRvlcInfo *pRvlc =
   1136       &pAacDecoderChannelInfo->pComData->overlay.aac.erRvlcInfo;
   1137   INT bitCntOffst;
   1138   INT saveBitCnt;
   1139 
   1140   rvlcInit(pRvlc, pAacDecoderChannelInfo, bs);
   1141 
   1142   /* save bitstream position */
   1143   saveBitCnt = FDKgetBitCnt(bs);
   1144 
   1145   if (pRvlc->sf_escapes_present)
   1146     rvlcDecodeEscapes(
   1147         pRvlc, pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfEsc, bs);
   1148 
   1149   rvlcDecodeForward(pRvlc, pAacDecoderChannelInfo, bs);
   1150   rvlcDecodeBackward(pRvlc, pAacDecoderChannelInfo, bs);
   1151   rvlcFinalErrorDetection(pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo);
   1152 
   1153   pAacDecoderChannelInfo->pDynData->specificTo.aac.rvlcIntensityUsed =
   1154       pRvlc->intensity_used;
   1155   pAacDecoderChannelInfo->data.aac.PnsData.PnsActive = pRvlc->noise_used;
   1156 
   1157   /* restore bitstream position */
   1158   bitCntOffst = saveBitCnt - FDKgetBitCnt(bs);
   1159   if (bitCntOffst) {
   1160     FDKpushBiDirectional(bs, bitCntOffst);
   1161   }
   1162 }
   1163 
   1164 void CRvlc_ElementCheck(
   1165     CAacDecoderChannelInfo *pAacDecoderChannelInfo[],
   1166     CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo[],
   1167     const UINT flags, const INT elChannels) {
   1168   int ch;
   1169 
   1170   /* Required for MPS residuals. */
   1171   if (pAacDecoderStaticChannelInfo == NULL) {
   1172     return;
   1173   }
   1174 
   1175   /* RVLC specific sanity checks */
   1176   if ((flags & AC_ER_RVLC) && (elChannels == 2)) { /* to be reviewed */
   1177     if (((pAacDecoderChannelInfo[0]
   1178               ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK == 0) ||
   1179          (pAacDecoderChannelInfo[1]
   1180               ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK == 0)) &&
   1181         pAacDecoderChannelInfo[0]->pComData->jointStereoData.MsMaskPresent) {
   1182       pAacDecoderChannelInfo[0]
   1183           ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK = 0;
   1184       pAacDecoderChannelInfo[1]
   1185           ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK = 0;
   1186     }
   1187 
   1188     if ((pAacDecoderChannelInfo[0]
   1189              ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK == 0) &&
   1190         (pAacDecoderChannelInfo[1]
   1191              ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK == 1) &&
   1192         (pAacDecoderChannelInfo[1]
   1193              ->pDynData->specificTo.aac.rvlcIntensityUsed == 1)) {
   1194       pAacDecoderChannelInfo[1]
   1195           ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK = 0;
   1196     }
   1197   }
   1198 
   1199   for (ch = 0; ch < elChannels; ch++) {
   1200     pAacDecoderStaticChannelInfo[ch]->concealmentInfo.rvlcPreviousBlockType =
   1201         (GetWindowSequence(&pAacDecoderChannelInfo[ch]->icsInfo) == BLOCK_SHORT)
   1202             ? 0
   1203             : 1;
   1204     if (flags & AC_ER_RVLC) {
   1205       pAacDecoderStaticChannelInfo[ch]
   1206           ->concealmentInfo.rvlcPreviousScaleFactorOK =
   1207           pAacDecoderChannelInfo[ch]
   1208               ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK;
   1209     } else {
   1210       pAacDecoderStaticChannelInfo[ch]
   1211           ->concealmentInfo.rvlcPreviousScaleFactorOK = 0;
   1212     }
   1213   }
   1214 }
   1215