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