Coverage Report

Created: 2026-10-28 06:57

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