Coverage Report

Created: 2026-07-16 06:55

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
118k
                     HANDLE_FDK_BITSTREAM bs) {
135
  /* RVLC common initialization part 2 of 2 */
136
118k
  SHORT *pScfEsc = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfEsc;
137
118k
  SHORT *pScfFwd = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd;
138
118k
  SHORT *pScfBwd = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd;
139
118k
  SHORT *pScaleFactor = pAacDecoderChannelInfo->pDynData->aScaleFactor;
140
118k
  int bnds;
141
142
118k
  pAacDecoderChannelInfo->pDynData->specificTo.aac.rvlcIntensityUsed = 0;
143
144
118k
  pRvlc->numDecodedEscapeWordsEsc = 0;
145
118k
  pRvlc->numDecodedEscapeWordsFwd = 0;
146
118k
  pRvlc->numDecodedEscapeWordsBwd = 0;
147
148
118k
  pRvlc->intensity_used = 0;
149
118k
  pRvlc->errorLogRvlc = 0;
150
151
118k
  pRvlc->conceal_max = CONCEAL_MAX_INIT;
152
118k
  pRvlc->conceal_min = CONCEAL_MIN_INIT;
153
154
118k
  pRvlc->conceal_max_esc = CONCEAL_MAX_INIT;
155
118k
  pRvlc->conceal_min_esc = CONCEAL_MIN_INIT;
156
157
118k
  pRvlc->pHuffTreeRvlcEscape = aHuffTreeRvlcEscape;
158
118k
  pRvlc->pHuffTreeRvlCodewds = aHuffTreeRvlCodewds;
159
160
  /* init scf arrays (for savety (in case of there are only zero codebooks)) */
161
15.2M
  for (bnds = 0; bnds < RVLC_MAX_SFB; bnds++) {
162
15.1M
    pScfFwd[bnds] = 0;
163
15.1M
    pScfBwd[bnds] = 0;
164
15.1M
    pScfEsc[bnds] = 0;
165
15.1M
    pScaleFactor[bnds] = 0;
166
15.1M
  }
167
168
  /* set base bitstream ptr to the RVL-coded part (start of RVLC data (ESC 2))
169
   */
170
118k
  FDKsyncCache(bs);
171
118k
  pRvlc->bsAnchor = (INT)FDKgetValidBits(bs);
172
173
118k
  pRvlc->bitstreamIndexRvlFwd =
174
118k
      0; /* first bit within RVL coded block as start address for  forward
175
            decoding */
176
118k
  pRvlc->bitstreamIndexRvlBwd =
177
118k
      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
118k
  FDKpushFor(bs, pRvlc->length_of_rvlc_sf);
183
184
118k
  if (pRvlc->sf_escapes_present != 0) {
185
    /* locate internal bitstream ptr at escapes (which is the second part) */
186
27.2k
    FDKsyncCache(bs);
187
27.2k
    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
27.2k
    FDKpushFor(bs, pRvlc->length_of_rvlc_escapes);
196
27.2k
  }
197
118k
}
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
118k
    CErRvlcInfo *pRvlc, CAacDecoderChannelInfo *pAacDecoderChannelInfo) {
216
118k
  int group, band, bnds;
217
218
118k
  pRvlc->intensity_used = 0;
219
220
327k
  for (group = 0; group < pRvlc->numWindowGroups; group++) {
221
819k
    for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
222
633k
      bnds = 16 * group + band;
223
633k
      if ((pAacDecoderChannelInfo->pDynData->aCodeBook[bnds] ==
224
633k
           INTENSITY_HCB) ||
225
622k
          (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds] ==
226
622k
           INTENSITY_HCB2)) {
227
23.6k
        pRvlc->intensity_used = 1;
228
23.6k
        break;
229
23.6k
      }
230
633k
    }
231
209k
  }
232
118k
}
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.15M
static SCHAR rvlcDecodeEscapeWord(CErRvlcInfo *pRvlc, HANDLE_FDK_BITSTREAM bs) {
248
1.15M
  int i;
249
1.15M
  SCHAR value;
250
1.15M
  UCHAR carryBit;
251
1.15M
  UINT treeNode;
252
1.15M
  UINT branchValue;
253
1.15M
  UINT branchNode;
254
255
1.15M
  INT *pBitstreamIndexEsc;
256
1.15M
  const UINT *pEscTree;
257
258
1.15M
  pEscTree = pRvlc->pHuffTreeRvlcEscape;
259
1.15M
  pBitstreamIndexEsc = &(pRvlc->bitstreamIndexEsc);
260
1.15M
  treeNode = *pEscTree; /* init at starting node */
261
262
2.84M
  for (i = MAX_LEN_RVLC_ESCAPE_WORD - 1; i >= 0; i--) {
263
2.84M
    carryBit =
264
2.84M
        rvlcReadBitFromBitstream(bs, /* get next bit */
265
2.84M
                                 pRvlc->bsAnchor, pBitstreamIndexEsc, FWD);
266
267
2.84M
    CarryBitToBranchValue(carryBit, /* huffman decoding, do a single step in
268
                                       huffman decoding tree */
269
2.84M
                          treeNode, &branchValue, &branchNode);
270
271
2.84M
    if ((branchNode & TEST_BIT_10) ==
272
2.84M
        TEST_BIT_10) { /* test bit 10 ; if set --> a RVLC-escape-word is
273
                          completely decoded */
274
1.15M
      value = (SCHAR)branchNode & CLR_BIT_10;
275
1.15M
      pRvlc->length_of_rvlc_escapes -= (MAX_LEN_RVLC_ESCAPE_WORD - i);
276
277
1.15M
      if (pRvlc->length_of_rvlc_escapes < 0) {
278
11.5k
        pRvlc->errorLogRvlc |= RVLC_ERROR_ALL_ESCAPE_WORDS_INVALID;
279
11.5k
        value = -1;
280
11.5k
      }
281
282
1.15M
      return value;
283
1.68M
    } else {
284
1.68M
      treeNode = *(
285
1.68M
          pEscTree +
286
1.68M
          branchValue); /* update treeNode for further step in decoding tree */
287
1.68M
    }
288
2.84M
  }
289
290
0
  pRvlc->errorLogRvlc |= RVLC_ERROR_ALL_ESCAPE_WORDS_INVALID;
291
292
0
  return -1; /* should not be reached */
293
1.15M
}
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
27.2k
                              HANDLE_FDK_BITSTREAM bs) {
324
27.2k
  SCHAR escWord;
325
27.2k
  SCHAR escCnt = 0;
326
27.2k
  SHORT *pEscBitCntSum;
327
328
27.2k
  pEscBitCntSum = &(pRvlc->length_of_rvlc_escapes);
329
330
  /* Decode all RVLC-Escape words with a plain Huffman-Decoder */
331
1.17M
  while (*pEscBitCntSum > 0) {
332
1.15M
    escWord = rvlcDecodeEscapeWord(pRvlc, bs);
333
334
1.15M
    if (escWord >= 0) {
335
1.14M
      pEsc[escCnt] = escWord;
336
1.14M
      escCnt++;
337
1.14M
    } else {
338
11.5k
      pRvlc->errorLogRvlc |= RVLC_ERROR_ALL_ESCAPE_WORDS_INVALID;
339
11.5k
      pRvlc->numDecodedEscapeWordsEsc = escCnt;
340
341
11.5k
      return;
342
11.5k
    }
343
1.15M
  } /* all RVLC escapes decoded */
344
345
15.6k
  pRvlc->numDecodedEscapeWordsEsc = escCnt;
346
15.6k
}
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
578k
SCHAR decodeRVLCodeword(HANDLE_FDK_BITSTREAM bs, CErRvlcInfo *pRvlc) {
363
578k
  int i;
364
578k
  SCHAR value;
365
578k
  UCHAR carryBit;
366
578k
  UINT branchValue;
367
578k
  UINT branchNode;
368
369
578k
  const UINT *pRvlCodeTree = pRvlc->pHuffTreeRvlCodewds;
370
578k
  UCHAR direction = pRvlc->direction;
371
578k
  INT *pBitstrIndxRvl = pRvlc->pBitstrIndxRvl_RVL;
372
578k
  UINT treeNode = *pRvlCodeTree;
373
374
1.09M
  for (i = MAX_LEN_RVLC_CODE_WORD - 1; i >= 0; i--) {
375
1.09M
    carryBit =
376
1.09M
        rvlcReadBitFromBitstream(bs, /* get next bit */
377
1.09M
                                 pRvlc->bsAnchor, pBitstrIndxRvl, direction);
378
379
1.09M
    CarryBitToBranchValue(carryBit, /* huffman decoding, do a single step in
380
                                       huffman decoding tree */
381
1.09M
                          treeNode, &branchValue, &branchNode);
382
383
1.09M
    if ((branchNode & TEST_BIT_10) ==
384
1.09M
        TEST_BIT_10) { /* test bit 10 ; if set --> a
385
                          RVLC-codeword is completely decoded
386
                        */
387
578k
      value = (SCHAR)(branchNode & CLR_BIT_10);
388
578k
      *pRvlc->pRvlBitCnt_RVL -= (MAX_LEN_RVLC_CODE_WORD - i);
389
390
      /* check available bits for decoding */
391
578k
      if (*pRvlc->pRvlBitCnt_RVL < 0) {
392
32.2k
        if (direction == FWD) {
393
16.4k
          pRvlc->errorLogRvlc |= RVLC_ERROR_RVL_SUM_BIT_COUNTER_BELOW_ZERO_FWD;
394
16.4k
        } else {
395
15.7k
          pRvlc->errorLogRvlc |= RVLC_ERROR_RVL_SUM_BIT_COUNTER_BELOW_ZERO_BWD;
396
15.7k
        }
397
32.2k
        value = -1; /* signalize an error in return value, because too many bits
398
                       was decoded */
399
32.2k
      }
400
401
      /* check max value of dpcm value */
402
578k
      if (value > MAX_ALLOWED_DPCM_INDEX) {
403
23.3k
        if (direction == FWD) {
404
14.9k
          pRvlc->errorLogRvlc |= RVLC_ERROR_FORBIDDEN_CW_DETECTED_FWD;
405
14.9k
        } else {
406
8.41k
          pRvlc->errorLogRvlc |= RVLC_ERROR_FORBIDDEN_CW_DETECTED_BWD;
407
8.41k
        }
408
23.3k
        value = -1; /* signalize an error in return value, because a forbidden
409
                       cw was detected*/
410
23.3k
      }
411
412
578k
      return value; /* return a dpcm value with offset +7 or an error status */
413
578k
    } else {
414
520k
      treeNode = *(
415
520k
          pRvlCodeTree +
416
520k
          branchValue); /* update treeNode for further step in decoding tree */
417
520k
    }
418
1.09M
  }
419
420
0
  return -1;
421
578k
}
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
118k
                              HANDLE_FDK_BITSTREAM bs) {
439
118k
  int band = 0;
440
118k
  int group = 0;
441
118k
  int bnds = 0;
442
443
118k
  SHORT dpcm;
444
445
118k
  SHORT factor =
446
118k
      pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain - SF_OFFSET;
447
118k
  SHORT position = -SF_OFFSET;
448
118k
  SHORT noisenrg = pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain -
449
118k
                   SF_OFFSET - 90 - 256;
450
451
118k
  SHORT *pScfFwd = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd;
452
118k
  SHORT *pScfEsc = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfEsc;
453
118k
  UCHAR *pEscFwdCnt = &(pRvlc->numDecodedEscapeWordsFwd);
454
455
118k
  pRvlc->pRvlBitCnt_RVL = &(pRvlc->length_of_rvlc_sf_fwd);
456
118k
  pRvlc->pBitstrIndxRvl_RVL = &(pRvlc->bitstreamIndexRvlFwd);
457
458
118k
  *pEscFwdCnt = 0;
459
118k
  pRvlc->direction = FWD;
460
118k
  pRvlc->noise_used = 0;
461
118k
  pRvlc->sf_used = 0;
462
118k
  pRvlc->lastScf = 0;
463
118k
  pRvlc->lastNrg = 0;
464
118k
  pRvlc->lastIs = 0;
465
466
118k
  rvlcCheckIntensityCb(pRvlc, pAacDecoderChannelInfo);
467
468
  /* main loop fwd long */
469
259k
  for (group = 0; group < pRvlc->numWindowGroups; group++) {
470
627k
    for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
471
485k
      bnds = 16 * group + band;
472
473
485k
      switch (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds]) {
474
186k
        case ZERO_HCB:
475
186k
          pScfFwd[bnds] = 0;
476
186k
          break;
477
478
27.1k
        case INTENSITY_HCB2:
479
43.6k
        case INTENSITY_HCB:
480
          /* store dpcm_is_position */
481
43.6k
          dpcm = decodeRVLCodeword(bs, pRvlc);
482
43.6k
          if (dpcm < 0) {
483
7.20k
            pRvlc->conceal_max = bnds;
484
7.20k
            return;
485
7.20k
          }
486
36.4k
          dpcm -= TABLE_OFFSET;
487
36.4k
          if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
488
621
            if (pRvlc->length_of_rvlc_escapes) {
489
312
              pRvlc->conceal_max = bnds;
490
312
              return;
491
312
            } else {
492
309
              if (dpcm == MIN_RVL) {
493
126
                dpcm -= *pScfEsc++;
494
183
              } else {
495
183
                dpcm += *pScfEsc++;
496
183
              }
497
309
              (*pEscFwdCnt)++;
498
309
              if (pRvlc->conceal_max_esc == CONCEAL_MAX_INIT) {
499
87
                pRvlc->conceal_max_esc = bnds;
500
87
              }
501
309
            }
502
621
          }
503
36.1k
          position += dpcm;
504
36.1k
          pScfFwd[bnds] = position;
505
36.1k
          pRvlc->lastIs = position;
506
36.1k
          break;
507
508
45.7k
        case NOISE_HCB:
509
45.7k
          if (pRvlc->noise_used == 0) {
510
16.1k
            pRvlc->noise_used = 1;
511
16.1k
            pRvlc->first_noise_band = bnds;
512
16.1k
            noisenrg += pRvlc->dpcm_noise_nrg;
513
16.1k
            pScfFwd[bnds] = 100 + noisenrg;
514
16.1k
            pRvlc->lastNrg = noisenrg;
515
29.6k
          } else {
516
29.6k
            dpcm = decodeRVLCodeword(bs, pRvlc);
517
29.6k
            if (dpcm < 0) {
518
3.87k
              pRvlc->conceal_max = bnds;
519
3.87k
              return;
520
3.87k
            }
521
25.7k
            dpcm -= TABLE_OFFSET;
522
25.7k
            if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
523
620
              if (pRvlc->length_of_rvlc_escapes) {
524
270
                pRvlc->conceal_max = bnds;
525
270
                return;
526
350
              } else {
527
350
                if (dpcm == MIN_RVL) {
528
113
                  dpcm -= *pScfEsc++;
529
237
                } else {
530
237
                  dpcm += *pScfEsc++;
531
237
                }
532
350
                (*pEscFwdCnt)++;
533
350
                if (pRvlc->conceal_max_esc == CONCEAL_MAX_INIT) {
534
49
                  pRvlc->conceal_max_esc = bnds;
535
49
                }
536
350
              }
537
620
            }
538
25.5k
            noisenrg += dpcm;
539
25.5k
            pScfFwd[bnds] = 100 + noisenrg;
540
25.5k
            pRvlc->lastNrg = noisenrg;
541
25.5k
          }
542
41.6k
          pAacDecoderChannelInfo->data.aac.PnsData.pnsUsed[bnds] = 1;
543
41.6k
          break;
544
545
209k
        default:
546
209k
          pRvlc->sf_used = 1;
547
209k
          dpcm = decodeRVLCodeword(bs, pRvlc);
548
209k
          if (dpcm < 0) {
549
19.1k
            pRvlc->conceal_max = bnds;
550
19.1k
            return;
551
19.1k
          }
552
190k
          dpcm -= TABLE_OFFSET;
553
190k
          if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
554
3.78k
            if (pRvlc->length_of_rvlc_escapes) {
555
2.40k
              pRvlc->conceal_max = bnds;
556
2.40k
              return;
557
2.40k
            } else {
558
1.37k
              if (dpcm == MIN_RVL) {
559
218
                dpcm -= *pScfEsc++;
560
1.15k
              } else {
561
1.15k
                dpcm += *pScfEsc++;
562
1.15k
              }
563
1.37k
              (*pEscFwdCnt)++;
564
1.37k
              if (pRvlc->conceal_max_esc == CONCEAL_MAX_INIT) {
565
1.02k
                pRvlc->conceal_max_esc = bnds;
566
1.02k
              }
567
1.37k
            }
568
3.78k
          }
569
188k
          factor += dpcm;
570
188k
          pScfFwd[bnds] = factor;
571
188k
          pRvlc->lastScf = factor;
572
188k
          break;
573
485k
      }
574
485k
    }
575
174k
  }
576
577
  /* postfetch fwd long */
578
84.9k
  if (pRvlc->intensity_used) {
579
6.53k
    dpcm = decodeRVLCodeword(bs, pRvlc); /* dpcm_is_last_position */
580
6.53k
    if (dpcm < 0) {
581
1.23k
      pRvlc->conceal_max = bnds;
582
1.23k
      return;
583
1.23k
    }
584
5.30k
    dpcm -= TABLE_OFFSET;
585
5.30k
    if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
586
634
      if (pRvlc->length_of_rvlc_escapes) {
587
580
        pRvlc->conceal_max = bnds;
588
580
        return;
589
580
      } else {
590
54
        if (dpcm == MIN_RVL) {
591
28
          dpcm -= *pScfEsc++;
592
28
        } else {
593
26
          dpcm += *pScfEsc++;
594
26
        }
595
54
        (*pEscFwdCnt)++;
596
54
        if (pRvlc->conceal_max_esc == CONCEAL_MAX_INIT) {
597
34
          pRvlc->conceal_max_esc = bnds;
598
34
        }
599
54
      }
600
634
    }
601
4.72k
    pRvlc->dpcm_is_last_position = dpcm;
602
4.72k
  }
603
84.9k
}
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
118k
                               HANDLE_FDK_BITSTREAM bs) {
621
118k
  SHORT band, group, dpcm, offset;
622
118k
  SHORT bnds = pRvlc->maxSfbTransmitted - 1;
623
624
118k
  SHORT factor = pRvlc->rev_global_gain - SF_OFFSET;
625
118k
  SHORT position = pRvlc->dpcm_is_last_position - SF_OFFSET;
626
118k
  SHORT noisenrg = pRvlc->rev_global_gain + pRvlc->dpcm_noise_last_position -
627
118k
                   SF_OFFSET - 90 - 256;
628
629
118k
  SHORT *pScfBwd = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd;
630
118k
  SHORT *pScfEsc = pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfEsc;
631
118k
  UCHAR escEscCnt = pRvlc->numDecodedEscapeWordsEsc;
632
118k
  UCHAR *pEscBwdCnt = &(pRvlc->numDecodedEscapeWordsBwd);
633
634
118k
  pRvlc->pRvlBitCnt_RVL = &(pRvlc->length_of_rvlc_sf_bwd);
635
118k
  pRvlc->pBitstrIndxRvl_RVL = &(pRvlc->bitstreamIndexRvlBwd);
636
637
118k
  *pEscBwdCnt = 0;
638
118k
  pRvlc->direction = BWD;
639
118k
  pScfEsc += escEscCnt - 1; /* set pScfEsc to last entry */
640
118k
  pRvlc->firstScf = 0;
641
118k
  pRvlc->firstNrg = 0;
642
118k
  pRvlc->firstIs = 0;
643
644
  /* prefetch long BWD */
645
118k
  if (pRvlc->intensity_used) {
646
22.7k
    dpcm = decodeRVLCodeword(bs, pRvlc); /* dpcm_is_last_position */
647
22.7k
    if (dpcm < 0) {
648
5.47k
      pRvlc->dpcm_is_last_position = 0;
649
5.47k
      pRvlc->conceal_min = bnds;
650
5.47k
      return;
651
5.47k
    }
652
17.2k
    dpcm -= TABLE_OFFSET;
653
17.2k
    if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
654
1.29k
      if ((pRvlc->length_of_rvlc_escapes) || (*pEscBwdCnt >= escEscCnt)) {
655
1.25k
        pRvlc->conceal_min = bnds;
656
1.25k
        return;
657
1.25k
      } else {
658
40
        if (dpcm == MIN_RVL) {
659
24
          dpcm -= *pScfEsc--;
660
24
        } else {
661
16
          dpcm += *pScfEsc--;
662
16
        }
663
40
        (*pEscBwdCnt)++;
664
40
        if (pRvlc->conceal_min_esc == CONCEAL_MIN_INIT) {
665
40
          pRvlc->conceal_min_esc = bnds;
666
40
        }
667
40
      }
668
1.29k
    }
669
16.0k
    pRvlc->dpcm_is_last_position = dpcm;
670
16.0k
  }
671
672
  /* main loop long BWD */
673
234k
  for (group = pRvlc->numWindowGroups - 1; group >= 0; group--) {
674
534k
    for (band = pRvlc->maxSfbTransmitted - 1; band >= 0; band--) {
675
411k
      bnds = 16 * group + band;
676
411k
      if ((band == 0) && (pRvlc->numWindowGroups != 1))
677
42.4k
        offset = 16 - pRvlc->maxSfbTransmitted + 1;
678
368k
      else
679
368k
        offset = 1;
680
681
411k
      switch (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds]) {
682
141k
        case ZERO_HCB:
683
141k
          pScfBwd[bnds] = 0;
684
141k
          break;
685
686
26.1k
        case INTENSITY_HCB2:
687
52.1k
        case INTENSITY_HCB:
688
          /* store dpcm_is_position */
689
52.1k
          dpcm = decodeRVLCodeword(bs, pRvlc);
690
52.1k
          if (dpcm < 0) {
691
982
            pScfBwd[bnds] = position;
692
982
            pRvlc->conceal_min = fMax(0, bnds - offset);
693
982
            return;
694
982
          }
695
51.1k
          dpcm -= TABLE_OFFSET;
696
51.1k
          if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
697
1.16k
            if ((pRvlc->length_of_rvlc_escapes) || (*pEscBwdCnt >= escEscCnt)) {
698
878
              pScfBwd[bnds] = position;
699
878
              pRvlc->conceal_min = fMax(0, bnds - offset);
700
878
              return;
701
878
            } else {
702
285
              if (dpcm == MIN_RVL) {
703
79
                dpcm -= *pScfEsc--;
704
206
              } else {
705
206
                dpcm += *pScfEsc--;
706
206
              }
707
285
              (*pEscBwdCnt)++;
708
285
              if (pRvlc->conceal_min_esc == CONCEAL_MIN_INIT) {
709
50
                pRvlc->conceal_min_esc = fMax(0, bnds - offset);
710
50
              }
711
285
            }
712
1.16k
          }
713
50.2k
          pScfBwd[bnds] = position;
714
50.2k
          position -= dpcm;
715
50.2k
          pRvlc->firstIs = position;
716
50.2k
          break;
717
718
19.2k
        case NOISE_HCB:
719
19.2k
          if (bnds == pRvlc->first_noise_band) {
720
3.98k
            pScfBwd[bnds] =
721
3.98k
                pRvlc->dpcm_noise_nrg +
722
3.98k
                pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain -
723
3.98k
                SF_OFFSET - 90 - 256;
724
3.98k
            pRvlc->firstNrg = pScfBwd[bnds];
725
15.2k
          } else {
726
15.2k
            dpcm = decodeRVLCodeword(bs, pRvlc);
727
15.2k
            if (dpcm < 0) {
728
2.52k
              pScfBwd[bnds] = noisenrg;
729
2.52k
              pRvlc->conceal_min = fMax(0, bnds - offset);
730
2.52k
              return;
731
2.52k
            }
732
12.6k
            dpcm -= TABLE_OFFSET;
733
12.6k
            if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
734
509
              if ((pRvlc->length_of_rvlc_escapes) ||
735
336
                  (*pEscBwdCnt >= escEscCnt)) {
736
194
                pScfBwd[bnds] = noisenrg;
737
194
                pRvlc->conceal_min = fMax(0, bnds - offset);
738
194
                return;
739
315
              } else {
740
315
                if (dpcm == MIN_RVL) {
741
161
                  dpcm -= *pScfEsc--;
742
161
                } else {
743
154
                  dpcm += *pScfEsc--;
744
154
                }
745
315
                (*pEscBwdCnt)++;
746
315
                if (pRvlc->conceal_min_esc == CONCEAL_MIN_INIT) {
747
21
                  pRvlc->conceal_min_esc = fMax(0, bnds - offset);
748
21
                }
749
315
              }
750
509
            }
751
12.4k
            pScfBwd[bnds] = noisenrg;
752
12.4k
            noisenrg -= dpcm;
753
12.4k
            pRvlc->firstNrg = noisenrg;
754
12.4k
          }
755
16.4k
          break;
756
757
198k
        default:
758
198k
          dpcm = decodeRVLCodeword(bs, pRvlc);
759
198k
          if (dpcm < 0) {
760
15.1k
            pScfBwd[bnds] = factor;
761
15.1k
            pRvlc->conceal_min = fMax(0, bnds - offset);
762
15.1k
            return;
763
15.1k
          }
764
183k
          dpcm -= TABLE_OFFSET;
765
183k
          if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
766
7.93k
            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
1.18k
              if (dpcm == MIN_RVL) {
772
136
                dpcm -= *pScfEsc--;
773
1.04k
              } else {
774
1.04k
                dpcm += *pScfEsc--;
775
1.04k
              }
776
1.18k
              (*pEscBwdCnt)++;
777
1.18k
              if (pRvlc->conceal_min_esc == CONCEAL_MIN_INIT) {
778
877
                pRvlc->conceal_min_esc = fMax(0, bnds - offset);
779
877
              }
780
1.18k
            }
781
7.93k
          }
782
176k
          pScfBwd[bnds] = factor;
783
176k
          factor -= dpcm;
784
176k
          pRvlc->firstScf = factor;
785
176k
          break;
786
411k
      }
787
411k
    }
788
150k
  }
789
111k
}
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
118k
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo) {
807
118k
  CErRvlcInfo *pRvlc =
808
118k
      &pAacDecoderChannelInfo->pComData->overlay.aac.erRvlcInfo;
809
118k
  UCHAR ErrorStatusComplete = 0;
810
118k
  UCHAR ErrorStatusLengthFwd = 0;
811
118k
  UCHAR ErrorStatusLengthBwd = 0;
812
118k
  UCHAR ErrorStatusLengthEscapes = 0;
813
118k
  UCHAR ErrorStatusFirstScf = 0;
814
118k
  UCHAR ErrorStatusLastScf = 0;
815
118k
  UCHAR ErrorStatusFirstNrg = 0;
816
118k
  UCHAR ErrorStatusLastNrg = 0;
817
118k
  UCHAR ErrorStatusFirstIs = 0;
818
118k
  UCHAR ErrorStatusLastIs = 0;
819
118k
  UCHAR ErrorStatusForbiddenCwFwd = 0;
820
118k
  UCHAR ErrorStatusForbiddenCwBwd = 0;
821
118k
  UCHAR ErrorStatusNumEscapesFwd = 0;
822
118k
  UCHAR ErrorStatusNumEscapesBwd = 0;
823
118k
  UCHAR ConcealStatus = 1;
824
118k
  UCHAR currentBlockType; /* short: 0, not short: 1*/
825
826
118k
  pAacDecoderChannelInfo->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK = 1;
827
828
  /* invalid escape words, bit counter unequal zero, forbidden codeword detected
829
   */
830
118k
  if (pRvlc->errorLogRvlc & RVLC_ERROR_FORBIDDEN_CW_DETECTED_FWD)
831
14.9k
    ErrorStatusForbiddenCwFwd = 1;
832
833
118k
  if (pRvlc->errorLogRvlc & RVLC_ERROR_FORBIDDEN_CW_DETECTED_BWD)
834
8.41k
    ErrorStatusForbiddenCwBwd = 1;
835
836
  /* bit counter forward unequal zero */
837
118k
  if (pRvlc->length_of_rvlc_sf_fwd) ErrorStatusLengthFwd = 1;
838
839
  /* bit counter backward unequal zero */
840
118k
  if (pRvlc->length_of_rvlc_sf_bwd) ErrorStatusLengthBwd = 1;
841
842
  /* bit counter escape sequences unequal zero */
843
118k
  if (pRvlc->sf_escapes_present)
844
27.2k
    if (pRvlc->length_of_rvlc_escapes) ErrorStatusLengthEscapes = 1;
845
846
118k
  if (pRvlc->sf_used) {
847
    /* first decoded scf does not match to global gain in backward direction */
848
48.8k
    if (pRvlc->firstScf !=
849
48.8k
        (pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain - SF_OFFSET))
850
43.6k
      ErrorStatusFirstScf = 1;
851
852
    /* last decoded scf does not match to rev global gain in forward direction
853
     */
854
48.8k
    if (pRvlc->lastScf != (pRvlc->rev_global_gain - SF_OFFSET))
855
45.4k
      ErrorStatusLastScf = 1;
856
48.8k
  }
857
858
118k
  if (pRvlc->noise_used) {
859
    /* first decoded nrg does not match to dpcm_noise_nrg in backward direction
860
     */
861
16.1k
    if (pRvlc->firstNrg !=
862
16.1k
        (pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain +
863
16.1k
         pRvlc->dpcm_noise_nrg - SF_OFFSET - 90 - 256))
864
12.1k
      ErrorStatusFirstNrg = 1;
865
866
    /* last decoded nrg does not match to dpcm_noise_last_position in forward
867
     * direction */
868
16.1k
    if (pRvlc->lastNrg !=
869
16.1k
        (pRvlc->rev_global_gain + pRvlc->dpcm_noise_last_position - SF_OFFSET -
870
16.1k
         90 - 256))
871
15.9k
      ErrorStatusLastNrg = 1;
872
16.1k
  }
873
874
118k
  if (pRvlc->intensity_used) {
875
    /* first decoded is position does not match in backward direction */
876
22.7k
    if (pRvlc->firstIs != (-SF_OFFSET)) ErrorStatusFirstIs = 1;
877
878
    /* last decoded is position does not match in forward direction */
879
22.7k
    if (pRvlc->lastIs != (pRvlc->dpcm_is_last_position - SF_OFFSET))
880
17.2k
      ErrorStatusLastIs = 1;
881
22.7k
  }
882
883
  /* decoded escapes and used escapes in forward direction do not fit */
884
118k
  if ((pRvlc->numDecodedEscapeWordsFwd != pRvlc->numDecodedEscapeWordsEsc) &&
885
23.3k
      (pRvlc->conceal_max == CONCEAL_MAX_INIT)) {
886
17.8k
    ErrorStatusNumEscapesFwd = 1;
887
17.8k
  }
888
889
  /* decoded escapes and used escapes in backward direction do not fit */
890
118k
  if ((pRvlc->numDecodedEscapeWordsBwd != pRvlc->numDecodedEscapeWordsEsc) &&
891
22.2k
      (pRvlc->conceal_min == CONCEAL_MIN_INIT)) {
892
17.4k
    ErrorStatusNumEscapesBwd = 1;
893
17.4k
  }
894
895
118k
  if (ErrorStatusLengthEscapes ||
896
104k
      (((pRvlc->conceal_max == CONCEAL_MAX_INIT) &&
897
74.0k
        (pRvlc->numDecodedEscapeWordsFwd != pRvlc->numDecodedEscapeWordsEsc) &&
898
9.94k
        (ErrorStatusLastScf || ErrorStatusLastNrg || ErrorStatusLastIs))
899
900
3.26k
       &&
901
902
3.26k
       ((pRvlc->conceal_min == CONCEAL_MIN_INIT) &&
903
2.11k
        (pRvlc->numDecodedEscapeWordsBwd != pRvlc->numDecodedEscapeWordsEsc) &&
904
1.18k
        (ErrorStatusFirstScf || ErrorStatusFirstNrg || ErrorStatusFirstIs))) ||
905
103k
      ((pRvlc->conceal_max == CONCEAL_MAX_INIT) &&
906
73.0k
       ((pRvlc->rev_global_gain - SF_OFFSET - pRvlc->lastScf) < -15)) ||
907
82.3k
      ((pRvlc->conceal_min == CONCEAL_MIN_INIT) &&
908
52.1k
       ((pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain - SF_OFFSET -
909
73.7k
         pRvlc->firstScf) < -15))) {
910
73.7k
    if ((pRvlc->conceal_max == CONCEAL_MAX_INIT) ||
911
71.1k
        (pRvlc->conceal_min == CONCEAL_MIN_INIT)) {
912
71.1k
      pRvlc->conceal_max = 0;
913
71.1k
      pRvlc->conceal_min = fMax(
914
71.1k
          0, (pRvlc->numWindowGroups - 1) * 16 + pRvlc->maxSfbTransmitted - 1);
915
71.1k
    } else {
916
2.58k
      pRvlc->conceal_max = fMin(pRvlc->conceal_max, pRvlc->conceal_max_esc);
917
2.58k
      pRvlc->conceal_min = fMax(pRvlc->conceal_min, pRvlc->conceal_min_esc);
918
2.58k
    }
919
73.7k
  }
920
921
118k
  ErrorStatusComplete = ErrorStatusLastScf || ErrorStatusFirstScf ||
922
71.7k
                        ErrorStatusLastNrg || ErrorStatusFirstNrg ||
923
66.9k
                        ErrorStatusLastIs || ErrorStatusFirstIs ||
924
64.4k
                        ErrorStatusForbiddenCwFwd ||
925
64.3k
                        ErrorStatusForbiddenCwBwd || ErrorStatusLengthFwd ||
926
25.1k
                        ErrorStatusLengthBwd || ErrorStatusLengthEscapes ||
927
23.9k
                        ErrorStatusNumEscapesFwd || ErrorStatusNumEscapesBwd;
928
929
118k
  currentBlockType =
930
118k
      (GetWindowSequence(&pAacDecoderChannelInfo->icsInfo) == BLOCK_SHORT) ? 0
931
118k
                                                                           : 1;
932
933
118k
  if (!ErrorStatusComplete) {
934
23.3k
    int band;
935
23.3k
    int group;
936
23.3k
    int bnds;
937
23.3k
    int lastSfbIndex;
938
939
23.3k
    lastSfbIndex = (pRvlc->numWindowGroups > 1) ? 16 : 64;
940
941
48.4k
    for (group = 0; group < pRvlc->numWindowGroups; group++) {
942
38.6k
      for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
943
13.6k
        bnds = 16 * group + band;
944
13.6k
        pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
945
13.6k
            pAacDecoderStaticChannelInfo->concealmentInfo
946
13.6k
                .aRvlcPreviousScaleFactor[bnds] =
947
13.6k
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds];
948
13.6k
      }
949
25.0k
    }
950
951
48.4k
    for (group = 0; group < pRvlc->numWindowGroups; group++) {
952
38.6k
      for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
953
13.6k
        bnds = 16 * group + band;
954
13.6k
        pAacDecoderStaticChannelInfo->concealmentInfo
955
13.6k
            .aRvlcPreviousCodebook[bnds] =
956
13.6k
            pAacDecoderChannelInfo->pDynData->aCodeBook[bnds];
957
13.6k
      }
958
1.52M
      for (; band < lastSfbIndex; band++) {
959
1.49M
        bnds = 16 * group + band;
960
1.49M
        FDK_ASSERT(bnds >= 0 && bnds < RVLC_MAX_SFB);
961
1.49M
        pAacDecoderStaticChannelInfo->concealmentInfo
962
1.49M
            .aRvlcPreviousCodebook[bnds] = ZERO_HCB;
963
1.49M
      }
964
25.0k
    }
965
94.7k
  } else {
966
94.7k
    int band;
967
94.7k
    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
94.7k
    if (((pRvlc->conceal_min != CONCEAL_MIN_INIT) ||
976
13.6k
         (pRvlc->conceal_max != CONCEAL_MAX_INIT)) &&
977
84.7k
        (pRvlc->conceal_min <= pRvlc->conceal_max) &&
978
63.0k
        (pAacDecoderStaticChannelInfo->concealmentInfo.rvlcPreviousBlockType ==
979
63.0k
         currentBlockType) &&
980
51.3k
        pAacDecoderStaticChannelInfo->concealmentInfo
981
51.3k
            .rvlcPreviousScaleFactorOK &&
982
42.0k
        pRvlc->sf_concealment && ConcealStatus) {
983
22.8k
      BidirectionalEstimation_UseScfOfPrevFrameAsReference(
984
22.8k
          pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo);
985
22.8k
      ConcealStatus = 0;
986
22.8k
    }
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
94.7k
    if ((pRvlc->conceal_min <= pRvlc->conceal_max) &&
994
73.0k
        ((pRvlc->conceal_min != CONCEAL_MIN_INIT) ||
995
11.1k
         (pRvlc->conceal_max != CONCEAL_MAX_INIT)) &&
996
63.0k
        !(pAacDecoderStaticChannelInfo->concealmentInfo
997
63.0k
              .rvlcPreviousScaleFactorOK &&
998
42.1k
          pRvlc->sf_concealment &&
999
22.9k
          (pAacDecoderStaticChannelInfo->concealmentInfo
1000
22.9k
               .rvlcPreviousBlockType == currentBlockType)) &&
1001
40.1k
        ConcealStatus) {
1002
40.1k
      BidirectionalEstimation_UseLowerScfOfCurrentFrame(pAacDecoderChannelInfo);
1003
40.1k
      ConcealStatus = 0;
1004
40.1k
    }
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
94.7k
    if ((pRvlc->conceal_min <= pRvlc->conceal_max) &&
1009
73.0k
        ((ErrorStatusLastScf && ErrorStatusFirstScf) ||
1010
47.2k
         (ErrorStatusLastNrg && ErrorStatusFirstNrg) ||
1011
44.2k
         (ErrorStatusLastIs && ErrorStatusFirstIs)) &&
1012
29.1k
        !(ErrorStatusForbiddenCwFwd || ErrorStatusForbiddenCwBwd ||
1013
16.6k
          ErrorStatusLengthEscapes) &&
1014
14.7k
        ConcealStatus) {
1015
3.20k
      StatisticalEstimation(pAacDecoderChannelInfo);
1016
3.20k
      ConcealStatus = 0;
1017
3.20k
    }
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
94.7k
    if ((pRvlc->conceal_min <= pRvlc->conceal_max) &&
1023
73.0k
        pAacDecoderStaticChannelInfo->concealmentInfo
1024
73.0k
            .rvlcPreviousScaleFactorOK &&
1025
48.7k
        pRvlc->sf_concealment &&
1026
28.0k
        (pAacDecoderStaticChannelInfo->concealmentInfo.rvlcPreviousBlockType ==
1027
28.0k
         currentBlockType) &&
1028
28.0k
        ConcealStatus) {
1029
5.06k
      PredictiveInterpolation(pAacDecoderChannelInfo,
1030
5.06k
                              pAacDecoderStaticChannelInfo);
1031
5.06k
      ConcealStatus = 0;
1032
5.06k
    }
1033
1034
    /* Call frame concealment, because no better strategy was found. Setting the
1035
       scalefactors to zero is done for debugging purposes */
1036
94.7k
    if (ConcealStatus) {
1037
66.7k
      for (group = 0; group < pRvlc->numWindowGroups; group++) {
1038
337k
        for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
1039
294k
          pAacDecoderChannelInfo->pDynData->aScaleFactor[16 * group + band] = 0;
1040
294k
        }
1041
43.3k
      }
1042
23.4k
      pAacDecoderChannelInfo->pDynData->specificTo.aac
1043
23.4k
          .rvlcCurrentScaleFactorOK = 0;
1044
23.4k
    }
1045
94.7k
  }
1046
118k
}
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
118k
                HANDLE_FDK_BITSTREAM bs) {
1063
118k
  CErRvlcInfo *pRvlc =
1064
118k
      &pAacDecoderChannelInfo->pComData->overlay.aac.erRvlcInfo;
1065
1066
118k
  int group, band;
1067
1068
  /* RVLC long specific initialization  Init part 1 of 2 */
1069
118k
  pRvlc->numWindowGroups = GetWindowGroups(&pAacDecoderChannelInfo->icsInfo);
1070
118k
  pRvlc->maxSfbTransmitted =
1071
118k
      GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
1072
118k
  pRvlc->noise_used = 0;               /* noise detection */
1073
118k
  pRvlc->dpcm_noise_nrg = 0;           /* only for debugging */
1074
118k
  pRvlc->dpcm_noise_last_position = 0; /* only for debugging */
1075
118k
  pRvlc->length_of_rvlc_escapes =
1076
118k
      -1; /* default value is used for error detection and concealment */
1077
1078
  /* read only error sensitivity class 1 data (ESC 1 - data) */
1079
118k
  pRvlc->sf_concealment = FDKreadBits(bs, 1);  /* #1 */
1080
118k
  pRvlc->rev_global_gain = FDKreadBits(bs, 8); /* #2 */
1081
1082
118k
  if (GetWindowSequence(&pAacDecoderChannelInfo->icsInfo) == BLOCK_SHORT) {
1083
18.9k
    pRvlc->length_of_rvlc_sf = FDKreadBits(bs, 11); /* #3 */
1084
99.2k
  } else {
1085
99.2k
    pRvlc->length_of_rvlc_sf = FDKreadBits(bs, 9); /* #3 */
1086
99.2k
  }
1087
1088
  /* check if noise codebook is used */
1089
325k
  for (group = 0; group < pRvlc->numWindowGroups; group++) {
1090
841k
    for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
1091
652k
      if (pAacDecoderChannelInfo->pDynData->aCodeBook[16 * group + band] ==
1092
652k
          NOISE_HCB) {
1093
19.0k
        pRvlc->noise_used = 1;
1094
19.0k
        break;
1095
19.0k
      }
1096
652k
    }
1097
207k
  }
1098
1099
118k
  if (pRvlc->noise_used)
1100
18.2k
    pRvlc->dpcm_noise_nrg = FDKreadBits(bs, 9); /* #4  PNS */
1101
1102
118k
  pRvlc->sf_escapes_present = FDKreadBits(bs, 1); /* #5      */
1103
1104
118k
  if (pRvlc->sf_escapes_present) {
1105
27.1k
    pRvlc->length_of_rvlc_escapes = FDKreadBits(bs, 8); /* #6      */
1106
27.1k
  }
1107
1108
118k
  if (pRvlc->noise_used) {
1109
18.2k
    pRvlc->dpcm_noise_last_position = FDKreadBits(bs, 9); /* #7  PNS */
1110
18.2k
    pRvlc->length_of_rvlc_sf -= 9;
1111
18.2k
  }
1112
1113
118k
  pRvlc->length_of_rvlc_sf_fwd = pRvlc->length_of_rvlc_sf;
1114
118k
  pRvlc->length_of_rvlc_sf_bwd = pRvlc->length_of_rvlc_sf;
1115
118k
}
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
118k
                  HANDLE_FDK_BITSTREAM bs) {
1139
118k
  CErRvlcInfo *pRvlc =
1140
118k
      &pAacDecoderChannelInfo->pComData->overlay.aac.erRvlcInfo;
1141
118k
  INT bitCntOffst;
1142
118k
  INT saveBitCnt;
1143
1144
118k
  rvlcInit(pRvlc, pAacDecoderChannelInfo, bs);
1145
1146
  /* save bitstream position */
1147
118k
  saveBitCnt = (INT)FDKgetValidBits(bs);
1148
1149
118k
  if (pRvlc->sf_escapes_present)
1150
27.2k
    rvlcDecodeEscapes(
1151
27.2k
        pRvlc, pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfEsc, bs);
1152
1153
118k
  rvlcDecodeForward(pRvlc, pAacDecoderChannelInfo, bs);
1154
118k
  rvlcDecodeBackward(pRvlc, pAacDecoderChannelInfo, bs);
1155
118k
  rvlcFinalErrorDetection(pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo);
1156
1157
118k
  pAacDecoderChannelInfo->pDynData->specificTo.aac.rvlcIntensityUsed =
1158
118k
      pRvlc->intensity_used;
1159
118k
  pAacDecoderChannelInfo->data.aac.PnsData.PnsActive = pRvlc->noise_used;
1160
1161
  /* restore bitstream position */
1162
118k
  bitCntOffst = (INT)FDKgetValidBits(bs) - saveBitCnt;
1163
118k
  if (bitCntOffst) {
1164
60.0k
    FDKpushBiDirectional(bs, bitCntOffst);
1165
60.0k
  }
1166
118k
}
1167
1168
void CRvlc_ElementCheck(
1169
    CAacDecoderChannelInfo *pAacDecoderChannelInfo[],
1170
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo[],
1171
468k
    const UINT flags, const INT elChannels) {
1172
468k
  int ch;
1173
1174
  /* Required for MPS residuals. */
1175
468k
  if (pAacDecoderStaticChannelInfo == NULL) {
1176
0
    return;
1177
0
  }
1178
1179
  /* RVLC specific sanity checks */
1180
468k
  if ((flags & AC_ER_RVLC) && (elChannels == 2)) { /* to be reviewed */
1181
46.0k
    if (((pAacDecoderChannelInfo[0]
1182
46.0k
              ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK == 0) ||
1183
37.5k
         (pAacDecoderChannelInfo[1]
1184
37.5k
              ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK == 0)) &&
1185
15.4k
        pAacDecoderChannelInfo[0]->pComData->jointStereoData.MsMaskPresent) {
1186
7.83k
      pAacDecoderChannelInfo[0]
1187
7.83k
          ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK = 0;
1188
7.83k
      pAacDecoderChannelInfo[1]
1189
7.83k
          ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK = 0;
1190
7.83k
    }
1191
1192
46.0k
    if ((pAacDecoderChannelInfo[0]
1193
46.0k
             ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK == 0) &&
1194
10.8k
        (pAacDecoderChannelInfo[1]
1195
10.8k
             ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK == 1) &&
1196
1.43k
        (pAacDecoderChannelInfo[1]
1197
1.43k
             ->pDynData->specificTo.aac.rvlcIntensityUsed == 1)) {
1198
1.07k
      pAacDecoderChannelInfo[1]
1199
1.07k
          ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK = 0;
1200
1.07k
    }
1201
46.0k
  }
1202
1203
1.11M
  for (ch = 0; ch < elChannels; ch++) {
1204
647k
    pAacDecoderStaticChannelInfo[ch]->concealmentInfo.rvlcPreviousBlockType =
1205
647k
        (GetWindowSequence(&pAacDecoderChannelInfo[ch]->icsInfo) == BLOCK_SHORT)
1206
647k
            ? 0
1207
647k
            : 1;
1208
647k
    if (flags & AC_ER_RVLC) {
1209
115k
      pAacDecoderStaticChannelInfo[ch]
1210
115k
          ->concealmentInfo.rvlcPreviousScaleFactorOK =
1211
115k
          pAacDecoderChannelInfo[ch]
1212
115k
              ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK;
1213
532k
    } else {
1214
532k
      pAacDecoderStaticChannelInfo[ch]
1215
532k
          ->concealmentInfo.rvlcPreviousScaleFactorOK = 0;
1216
532k
    }
1217
647k
  }
1218
468k
}