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