Coverage Report

Created: 2025-12-31 06:47

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.8k
    FDKsyncCache(bs);
187
39.8k
    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.8k
    FDKpushFor(bs, pRvlc->length_of_rvlc_escapes);
196
39.8k
  }
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
409k
  for (group = 0; group < pRvlc->numWindowGroups; group++) {
221
948k
    for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
222
733k
      bnds = 16 * group + band;
223
733k
      if ((pAacDecoderChannelInfo->pDynData->aCodeBook[bnds] ==
224
733k
           INTENSITY_HCB) ||
225
720k
          (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds] ==
226
720k
           INTENSITY_HCB2)) {
227
29.4k
        pRvlc->intensity_used = 1;
228
29.4k
        break;
229
29.4k
      }
230
733k
    }
231
244k
  }
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.62M
static SCHAR rvlcDecodeEscapeWord(CErRvlcInfo *pRvlc, HANDLE_FDK_BITSTREAM bs) {
248
1.62M
  int i;
249
1.62M
  SCHAR value;
250
1.62M
  UCHAR carryBit;
251
1.62M
  UINT treeNode;
252
1.62M
  UINT branchValue;
253
1.62M
  UINT branchNode;
254
255
1.62M
  INT *pBitstreamIndexEsc;
256
1.62M
  const UINT *pEscTree;
257
258
1.62M
  pEscTree = pRvlc->pHuffTreeRvlcEscape;
259
1.62M
  pBitstreamIndexEsc = &(pRvlc->bitstreamIndexEsc);
260
1.62M
  treeNode = *pEscTree; /* init at starting node */
261
262
4.02M
  for (i = MAX_LEN_RVLC_ESCAPE_WORD - 1; i >= 0; i--) {
263
4.02M
    carryBit =
264
4.02M
        rvlcReadBitFromBitstream(bs, /* get next bit */
265
4.02M
                                 pRvlc->bsAnchor, pBitstreamIndexEsc, FWD);
266
267
4.02M
    CarryBitToBranchValue(carryBit, /* huffman decoding, do a single step in
268
                                       huffman decoding tree */
269
4.02M
                          treeNode, &branchValue, &branchNode);
270
271
4.02M
    if ((branchNode & TEST_BIT_10) ==
272
4.02M
        TEST_BIT_10) { /* test bit 10 ; if set --> a RVLC-escape-word is
273
                          completely decoded */
274
1.62M
      value = (SCHAR)branchNode & CLR_BIT_10;
275
1.62M
      pRvlc->length_of_rvlc_escapes -= (MAX_LEN_RVLC_ESCAPE_WORD - i);
276
277
1.62M
      if (pRvlc->length_of_rvlc_escapes < 0) {
278
15.7k
        pRvlc->errorLogRvlc |= RVLC_ERROR_ALL_ESCAPE_WORDS_INVALID;
279
15.7k
        value = -1;
280
15.7k
      }
281
282
1.62M
      return value;
283
2.39M
    } else {
284
2.39M
      treeNode = *(
285
2.39M
          pEscTree +
286
2.39M
          branchValue); /* update treeNode for further step in decoding tree */
287
2.39M
    }
288
4.02M
  }
289
290
0
  pRvlc->errorLogRvlc |= RVLC_ERROR_ALL_ESCAPE_WORDS_INVALID;
291
292
0
  return -1; /* should not be reached */
293
1.62M
}
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.8k
                              HANDLE_FDK_BITSTREAM bs) {
324
39.8k
  SCHAR escWord;
325
39.8k
  SCHAR escCnt = 0;
326
39.8k
  SHORT *pEscBitCntSum;
327
328
39.8k
  pEscBitCntSum = &(pRvlc->length_of_rvlc_escapes);
329
330
  /* Decode all RVLC-Escape words with a plain Huffman-Decoder */
331
1.65M
  while (*pEscBitCntSum > 0) {
332
1.62M
    escWord = rvlcDecodeEscapeWord(pRvlc, bs);
333
334
1.62M
    if (escWord >= 0) {
335
1.61M
      pEsc[escCnt] = escWord;
336
1.61M
      escCnt++;
337
1.61M
    } else {
338
15.7k
      pRvlc->errorLogRvlc |= RVLC_ERROR_ALL_ESCAPE_WORDS_INVALID;
339
15.7k
      pRvlc->numDecodedEscapeWordsEsc = escCnt;
340
341
15.7k
      return;
342
15.7k
    }
343
1.62M
  } /* all RVLC escapes decoded */
344
345
24.0k
  pRvlc->numDecodedEscapeWordsEsc = escCnt;
346
24.0k
}
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
710k
SCHAR decodeRVLCodeword(HANDLE_FDK_BITSTREAM bs, CErRvlcInfo *pRvlc) {
363
710k
  int i;
364
710k
  SCHAR value;
365
710k
  UCHAR carryBit;
366
710k
  UINT branchValue;
367
710k
  UINT branchNode;
368
369
710k
  const UINT *pRvlCodeTree = pRvlc->pHuffTreeRvlCodewds;
370
710k
  UCHAR direction = pRvlc->direction;
371
710k
  INT *pBitstrIndxRvl = pRvlc->pBitstrIndxRvl_RVL;
372
710k
  UINT treeNode = *pRvlCodeTree;
373
374
1.28M
  for (i = MAX_LEN_RVLC_CODE_WORD - 1; i >= 0; i--) {
375
1.28M
    carryBit =
376
1.28M
        rvlcReadBitFromBitstream(bs, /* get next bit */
377
1.28M
                                 pRvlc->bsAnchor, pBitstrIndxRvl, direction);
378
379
1.28M
    CarryBitToBranchValue(carryBit, /* huffman decoding, do a single step in
380
                                       huffman decoding tree */
381
1.28M
                          treeNode, &branchValue, &branchNode);
382
383
1.28M
    if ((branchNode & TEST_BIT_10) ==
384
1.28M
        TEST_BIT_10) { /* test bit 10 ; if set --> a
385
                          RVLC-codeword is completely decoded
386
                        */
387
710k
      value = (SCHAR)(branchNode & CLR_BIT_10);
388
710k
      *pRvlc->pRvlBitCnt_RVL -= (MAX_LEN_RVLC_CODE_WORD - i);
389
390
      /* check available bits for decoding */
391
710k
      if (*pRvlc->pRvlBitCnt_RVL < 0) {
392
38.0k
        if (direction == FWD) {
393
19.0k
          pRvlc->errorLogRvlc |= RVLC_ERROR_RVL_SUM_BIT_COUNTER_BELOW_ZERO_FWD;
394
19.0k
        } else {
395
18.9k
          pRvlc->errorLogRvlc |= RVLC_ERROR_RVL_SUM_BIT_COUNTER_BELOW_ZERO_BWD;
396
18.9k
        }
397
38.0k
        value = -1; /* signalize an error in return value, because too many bits
398
                       was decoded */
399
38.0k
      }
400
401
      /* check max value of dpcm value */
402
710k
      if (value > MAX_ALLOWED_DPCM_INDEX) {
403
28.5k
        if (direction == FWD) {
404
18.7k
          pRvlc->errorLogRvlc |= RVLC_ERROR_FORBIDDEN_CW_DETECTED_FWD;
405
18.7k
        } else {
406
9.83k
          pRvlc->errorLogRvlc |= RVLC_ERROR_FORBIDDEN_CW_DETECTED_BWD;
407
9.83k
        }
408
28.5k
        value = -1; /* signalize an error in return value, because a forbidden
409
                       cw was detected*/
410
28.5k
      }
411
412
710k
      return value; /* return a dpcm value with offset +7 or an error status */
413
710k
    } else {
414
576k
      treeNode = *(
415
576k
          pRvlCodeTree +
416
576k
          branchValue); /* update treeNode for further step in decoding tree */
417
576k
    }
418
1.28M
  }
419
420
0
  return -1;
421
710k
}
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
344k
  for (group = 0; group < pRvlc->numWindowGroups; group++) {
470
773k
    for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
471
594k
      bnds = 16 * group + band;
472
473
594k
      switch (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds]) {
474
225k
        case ZERO_HCB:
475
225k
          pScfFwd[bnds] = 0;
476
225k
          break;
477
478
48.7k
        case INTENSITY_HCB2:
479
69.6k
        case INTENSITY_HCB:
480
          /* store dpcm_is_position */
481
69.6k
          dpcm = decodeRVLCodeword(bs, pRvlc);
482
69.6k
          if (dpcm < 0) {
483
8.64k
            pRvlc->conceal_max = bnds;
484
8.64k
            return;
485
8.64k
          }
486
61.0k
          dpcm -= TABLE_OFFSET;
487
61.0k
          if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
488
682
            if (pRvlc->length_of_rvlc_escapes) {
489
331
              pRvlc->conceal_max = bnds;
490
331
              return;
491
351
            } else {
492
351
              if (dpcm == MIN_RVL) {
493
159
                dpcm -= *pScfEsc++;
494
192
              } else {
495
192
                dpcm += *pScfEsc++;
496
192
              }
497
351
              (*pEscFwdCnt)++;
498
351
              if (pRvlc->conceal_max_esc == CONCEAL_MAX_INIT) {
499
90
                pRvlc->conceal_max_esc = bnds;
500
90
              }
501
351
            }
502
682
          }
503
60.6k
          position += dpcm;
504
60.6k
          pScfFwd[bnds] = position;
505
60.6k
          pRvlc->lastIs = position;
506
60.6k
          break;
507
508
43.0k
        case NOISE_HCB:
509
43.0k
          if (pRvlc->noise_used == 0) {
510
15.1k
            pRvlc->noise_used = 1;
511
15.1k
            pRvlc->first_noise_band = bnds;
512
15.1k
            noisenrg += pRvlc->dpcm_noise_nrg;
513
15.1k
            pScfFwd[bnds] = 100 + noisenrg;
514
15.1k
            pRvlc->lastNrg = noisenrg;
515
27.8k
          } else {
516
27.8k
            dpcm = decodeRVLCodeword(bs, pRvlc);
517
27.8k
            if (dpcm < 0) {
518
2.62k
              pRvlc->conceal_max = bnds;
519
2.62k
              return;
520
2.62k
            }
521
25.2k
            dpcm -= TABLE_OFFSET;
522
25.2k
            if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
523
645
              if (pRvlc->length_of_rvlc_escapes) {
524
378
                pRvlc->conceal_max = bnds;
525
378
                return;
526
378
              } else {
527
267
                if (dpcm == MIN_RVL) {
528
164
                  dpcm -= *pScfEsc++;
529
164
                } else {
530
103
                  dpcm += *pScfEsc++;
531
103
                }
532
267
                (*pEscFwdCnt)++;
533
267
                if (pRvlc->conceal_max_esc == CONCEAL_MAX_INIT) {
534
89
                  pRvlc->conceal_max_esc = bnds;
535
89
                }
536
267
              }
537
645
            }
538
24.8k
            noisenrg += dpcm;
539
24.8k
            pScfFwd[bnds] = 100 + noisenrg;
540
24.8k
            pRvlc->lastNrg = noisenrg;
541
24.8k
          }
542
39.9k
          pAacDecoderChannelInfo->data.aac.PnsData.pnsUsed[bnds] = 1;
543
39.9k
          break;
544
545
256k
        default:
546
256k
          pRvlc->sf_used = 1;
547
256k
          dpcm = decodeRVLCodeword(bs, pRvlc);
548
256k
          if (dpcm < 0) {
549
23.8k
            pRvlc->conceal_max = bnds;
550
23.8k
            return;
551
23.8k
          }
552
232k
          dpcm -= TABLE_OFFSET;
553
232k
          if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
554
3.54k
            if (pRvlc->length_of_rvlc_escapes) {
555
2.13k
              pRvlc->conceal_max = bnds;
556
2.13k
              return;
557
2.13k
            } else {
558
1.40k
              if (dpcm == MIN_RVL) {
559
282
                dpcm -= *pScfEsc++;
560
1.12k
              } else {
561
1.12k
                dpcm += *pScfEsc++;
562
1.12k
              }
563
1.40k
              (*pEscFwdCnt)++;
564
1.40k
              if (pRvlc->conceal_max_esc == CONCEAL_MAX_INIT) {
565
972
                pRvlc->conceal_max_esc = bnds;
566
972
              }
567
1.40k
            }
568
3.54k
          }
569
230k
          factor += dpcm;
570
230k
          pScfFwd[bnds] = factor;
571
230k
          pRvlc->lastScf = factor;
572
230k
          break;
573
594k
      }
574
594k
    }
575
217k
  }
576
577
  /* postfetch fwd long */
578
127k
  if (pRvlc->intensity_used) {
579
11.1k
    dpcm = decodeRVLCodeword(bs, pRvlc); /* dpcm_is_last_position */
580
11.1k
    if (dpcm < 0) {
581
2.68k
      pRvlc->conceal_max = bnds;
582
2.68k
      return;
583
2.68k
    }
584
8.43k
    dpcm -= TABLE_OFFSET;
585
8.43k
    if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
586
403
      if (pRvlc->length_of_rvlc_escapes) {
587
202
        pRvlc->conceal_max = bnds;
588
202
        return;
589
202
      } else {
590
201
        if (dpcm == MIN_RVL) {
591
172
          dpcm -= *pScfEsc++;
592
172
        } else {
593
29
          dpcm += *pScfEsc++;
594
29
        }
595
201
        (*pEscFwdCnt)++;
596
201
        if (pRvlc->conceal_max_esc == CONCEAL_MAX_INIT) {
597
183
          pRvlc->conceal_max_esc = bnds;
598
183
        }
599
201
      }
600
403
    }
601
8.23k
    pRvlc->dpcm_is_last_position = dpcm;
602
8.23k
  }
603
127k
}
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
28.1k
    dpcm = decodeRVLCodeword(bs, pRvlc); /* dpcm_is_last_position */
647
28.1k
    if (dpcm < 0) {
648
6.57k
      pRvlc->dpcm_is_last_position = 0;
649
6.57k
      pRvlc->conceal_min = bnds;
650
6.57k
      return;
651
6.57k
    }
652
21.5k
    dpcm -= TABLE_OFFSET;
653
21.5k
    if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
654
1.87k
      if ((pRvlc->length_of_rvlc_escapes) || (*pEscBwdCnt >= escEscCnt)) {
655
1.82k
        pRvlc->conceal_min = bnds;
656
1.82k
        return;
657
1.82k
      } else {
658
52
        if (dpcm == MIN_RVL) {
659
22
          dpcm -= *pScfEsc--;
660
30
        } else {
661
30
          dpcm += *pScfEsc--;
662
30
        }
663
52
        (*pEscBwdCnt)++;
664
52
        if (pRvlc->conceal_min_esc == CONCEAL_MIN_INIT) {
665
52
          pRvlc->conceal_min_esc = bnds;
666
52
        }
667
52
      }
668
1.87k
    }
669
19.7k
    pRvlc->dpcm_is_last_position = dpcm;
670
19.7k
  }
671
672
  /* main loop long BWD */
673
319k
  for (group = pRvlc->numWindowGroups - 1; group >= 0; group--) {
674
671k
    for (band = pRvlc->maxSfbTransmitted - 1; band >= 0; band--) {
675
509k
      bnds = 16 * group + band;
676
509k
      if ((band == 0) && (pRvlc->numWindowGroups != 1))
677
34.9k
        offset = 16 - pRvlc->maxSfbTransmitted + 1;
678
474k
      else
679
474k
        offset = 1;
680
681
509k
      switch (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds]) {
682
187k
        case ZERO_HCB:
683
187k
          pScfBwd[bnds] = 0;
684
187k
          break;
685
686
43.0k
        case INTENSITY_HCB2:
687
69.4k
        case INTENSITY_HCB:
688
          /* store dpcm_is_position */
689
69.4k
          dpcm = decodeRVLCodeword(bs, pRvlc);
690
69.4k
          if (dpcm < 0) {
691
1.98k
            pScfBwd[bnds] = position;
692
1.98k
            pRvlc->conceal_min = fMax(0, bnds - offset);
693
1.98k
            return;
694
1.98k
          }
695
67.4k
          dpcm -= TABLE_OFFSET;
696
67.4k
          if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
697
1.05k
            if ((pRvlc->length_of_rvlc_escapes) || (*pEscBwdCnt >= escEscCnt)) {
698
844
              pScfBwd[bnds] = position;
699
844
              pRvlc->conceal_min = fMax(0, bnds - offset);
700
844
              return;
701
844
            } else {
702
214
              if (dpcm == MIN_RVL) {
703
98
                dpcm -= *pScfEsc--;
704
116
              } else {
705
116
                dpcm += *pScfEsc--;
706
116
              }
707
214
              (*pEscBwdCnt)++;
708
214
              if (pRvlc->conceal_min_esc == CONCEAL_MIN_INIT) {
709
42
                pRvlc->conceal_min_esc = fMax(0, bnds - offset);
710
42
              }
711
214
            }
712
1.05k
          }
713
66.6k
          pScfBwd[bnds] = position;
714
66.6k
          position -= dpcm;
715
66.6k
          pRvlc->firstIs = position;
716
66.6k
          break;
717
718
19.3k
        case NOISE_HCB:
719
19.3k
          if (bnds == pRvlc->first_noise_band) {
720
4.82k
            pScfBwd[bnds] =
721
4.82k
                pRvlc->dpcm_noise_nrg +
722
4.82k
                pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain -
723
4.82k
                SF_OFFSET - 90 - 256;
724
4.82k
            pRvlc->firstNrg = pScfBwd[bnds];
725
14.5k
          } else {
726
14.5k
            dpcm = decodeRVLCodeword(bs, pRvlc);
727
14.5k
            if (dpcm < 0) {
728
1.81k
              pScfBwd[bnds] = noisenrg;
729
1.81k
              pRvlc->conceal_min = fMax(0, bnds - offset);
730
1.81k
              return;
731
1.81k
            }
732
12.6k
            dpcm -= TABLE_OFFSET;
733
12.6k
            if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
734
432
              if ((pRvlc->length_of_rvlc_escapes) ||
735
261
                  (*pEscBwdCnt >= escEscCnt)) {
736
261
                pScfBwd[bnds] = noisenrg;
737
261
                pRvlc->conceal_min = fMax(0, bnds - offset);
738
261
                return;
739
261
              } else {
740
171
                if (dpcm == MIN_RVL) {
741
78
                  dpcm -= *pScfEsc--;
742
93
                } else {
743
93
                  dpcm += *pScfEsc--;
744
93
                }
745
171
                (*pEscBwdCnt)++;
746
171
                if (pRvlc->conceal_min_esc == CONCEAL_MIN_INIT) {
747
26
                  pRvlc->conceal_min_esc = fMax(0, bnds - offset);
748
26
                }
749
171
              }
750
432
            }
751
12.4k
            pScfBwd[bnds] = noisenrg;
752
12.4k
            noisenrg -= dpcm;
753
12.4k
            pRvlc->firstNrg = noisenrg;
754
12.4k
          }
755
17.2k
          break;
756
757
232k
        default:
758
232k
          dpcm = decodeRVLCodeword(bs, pRvlc);
759
232k
          if (dpcm < 0) {
760
18.4k
            pScfBwd[bnds] = factor;
761
18.4k
            pRvlc->conceal_min = fMax(0, bnds - offset);
762
18.4k
            return;
763
18.4k
          }
764
214k
          dpcm -= TABLE_OFFSET;
765
214k
          if ((dpcm == MIN_RVL) || (dpcm == MAX_RVL)) {
766
7.08k
            if ((pRvlc->length_of_rvlc_escapes) || (*pEscBwdCnt >= escEscCnt)) {
767
6.34k
              pScfBwd[bnds] = factor;
768
6.34k
              pRvlc->conceal_min = fMax(0, bnds - offset);
769
6.34k
              return;
770
6.34k
            } else {
771
740
              if (dpcm == MIN_RVL) {
772
151
                dpcm -= *pScfEsc--;
773
589
              } else {
774
589
                dpcm += *pScfEsc--;
775
589
              }
776
740
              (*pEscBwdCnt)++;
777
740
              if (pRvlc->conceal_min_esc == CONCEAL_MIN_INIT) {
778
474
                pRvlc->conceal_min_esc = fMax(0, bnds - offset);
779
474
              }
780
740
            }
781
7.08k
          }
782
208k
          pScfBwd[bnds] = factor;
783
208k
          factor -= dpcm;
784
208k
          pRvlc->firstScf = factor;
785
208k
          break;
786
509k
      }
787
509k
    }
788
192k
  }
789
156k
}
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
18.7k
    ErrorStatusForbiddenCwFwd = 1;
832
833
165k
  if (pRvlc->errorLogRvlc & RVLC_ERROR_FORBIDDEN_CW_DETECTED_BWD)
834
9.83k
    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.8k
    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
60.7k
    if (pRvlc->firstScf !=
849
60.7k
        (pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain - SF_OFFSET))
850
52.1k
      ErrorStatusFirstScf = 1;
851
852
    /* last decoded scf does not match to rev global gain in forward direction
853
     */
854
60.7k
    if (pRvlc->lastScf != (pRvlc->rev_global_gain - SF_OFFSET))
855
56.0k
      ErrorStatusLastScf = 1;
856
60.7k
  }
857
858
165k
  if (pRvlc->noise_used) {
859
    /* first decoded nrg does not match to dpcm_noise_nrg in backward direction
860
     */
861
15.1k
    if (pRvlc->firstNrg !=
862
15.1k
        (pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain +
863
15.1k
         pRvlc->dpcm_noise_nrg - SF_OFFSET - 90 - 256))
864
10.3k
      ErrorStatusFirstNrg = 1;
865
866
    /* last decoded nrg does not match to dpcm_noise_last_position in forward
867
     * direction */
868
15.1k
    if (pRvlc->lastNrg !=
869
15.1k
        (pRvlc->rev_global_gain + pRvlc->dpcm_noise_last_position - SF_OFFSET -
870
15.1k
         90 - 256))
871
14.8k
      ErrorStatusLastNrg = 1;
872
15.1k
  }
873
874
165k
  if (pRvlc->intensity_used) {
875
    /* first decoded is position does not match in backward direction */
876
28.1k
    if (pRvlc->firstIs != (-SF_OFFSET)) ErrorStatusFirstIs = 1;
877
878
    /* last decoded is position does not match in forward direction */
879
28.1k
    if (pRvlc->lastIs != (pRvlc->dpcm_is_last_position - SF_OFFSET))
880
18.1k
      ErrorStatusLastIs = 1;
881
28.1k
  }
882
883
  /* decoded escapes and used escapes in forward direction do not fit */
884
165k
  if ((pRvlc->numDecodedEscapeWordsFwd != pRvlc->numDecodedEscapeWordsEsc) &&
885
33.1k
      (pRvlc->conceal_max == CONCEAL_MAX_INIT)) {
886
27.3k
    ErrorStatusNumEscapesFwd = 1;
887
27.3k
  }
888
889
  /* decoded escapes and used escapes in backward direction do not fit */
890
165k
  if ((pRvlc->numDecodedEscapeWordsBwd != pRvlc->numDecodedEscapeWordsEsc) &&
891
32.2k
      (pRvlc->conceal_min == CONCEAL_MIN_INIT)) {
892
26.1k
    ErrorStatusNumEscapesBwd = 1;
893
26.1k
  }
894
895
165k
  if (ErrorStatusLengthEscapes ||
896
146k
      (((pRvlc->conceal_max == CONCEAL_MAX_INIT) &&
897
111k
        (pRvlc->numDecodedEscapeWordsFwd != pRvlc->numDecodedEscapeWordsEsc) &&
898
15.6k
        (ErrorStatusLastScf || ErrorStatusLastNrg || ErrorStatusLastIs))
899
900
3.94k
       &&
901
902
3.94k
       ((pRvlc->conceal_min == CONCEAL_MIN_INIT) &&
903
2.28k
        (pRvlc->numDecodedEscapeWordsBwd != pRvlc->numDecodedEscapeWordsEsc) &&
904
1.45k
        (ErrorStatusFirstScf || ErrorStatusFirstNrg || ErrorStatusFirstIs))) ||
905
145k
      ((pRvlc->conceal_max == CONCEAL_MAX_INIT) &&
906
109k
       ((pRvlc->rev_global_gain - SF_OFFSET - pRvlc->lastScf) < -15)) ||
907
120k
      ((pRvlc->conceal_min == CONCEAL_MIN_INIT) &&
908
86.2k
       ((pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain - SF_OFFSET -
909
111k
         pRvlc->firstScf) < -15))) {
910
111k
    if ((pRvlc->conceal_max == CONCEAL_MAX_INIT) ||
911
108k
        (pRvlc->conceal_min == CONCEAL_MIN_INIT)) {
912
108k
      pRvlc->conceal_max = 0;
913
108k
      pRvlc->conceal_min = fMax(
914
108k
          0, (pRvlc->numWindowGroups - 1) * 16 + pRvlc->maxSfbTransmitted - 1);
915
108k
    } else {
916
3.45k
      pRvlc->conceal_max = fMin(pRvlc->conceal_max, pRvlc->conceal_max_esc);
917
3.45k
      pRvlc->conceal_min = fMax(pRvlc->conceal_min, pRvlc->conceal_min_esc);
918
3.45k
    }
919
111k
  }
920
921
165k
  ErrorStatusComplete = ErrorStatusLastScf || ErrorStatusFirstScf ||
922
107k
                        ErrorStatusLastNrg || ErrorStatusFirstNrg ||
923
104k
                        ErrorStatusLastIs || ErrorStatusFirstIs ||
924
100k
                        ErrorStatusForbiddenCwFwd ||
925
100k
                        ErrorStatusForbiddenCwBwd || ErrorStatusLengthFwd ||
926
38.1k
                        ErrorStatusLengthBwd || ErrorStatusLengthEscapes ||
927
36.0k
                        ErrorStatusNumEscapesFwd || ErrorStatusNumEscapesBwd;
928
929
165k
  currentBlockType =
930
165k
      (GetWindowSequence(&pAacDecoderChannelInfo->icsInfo) == BLOCK_SHORT) ? 0
931
165k
                                                                           : 1;
932
933
165k
  if (!ErrorStatusComplete) {
934
35.6k
    int band;
935
35.6k
    int group;
936
35.6k
    int bnds;
937
35.6k
    int lastSfbIndex;
938
939
35.6k
    lastSfbIndex = (pRvlc->numWindowGroups > 1) ? 16 : 64;
940
941
74.2k
    for (group = 0; group < pRvlc->numWindowGroups; group++) {
942
52.8k
      for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
943
14.2k
        bnds = 16 * group + band;
944
14.2k
        pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
945
14.2k
            pAacDecoderStaticChannelInfo->concealmentInfo
946
14.2k
                .aRvlcPreviousScaleFactor[bnds] =
947
14.2k
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds];
948
14.2k
      }
949
38.6k
    }
950
951
74.2k
    for (group = 0; group < pRvlc->numWindowGroups; group++) {
952
52.8k
      for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
953
14.2k
        bnds = 16 * group + band;
954
14.2k
        pAacDecoderStaticChannelInfo->concealmentInfo
955
14.2k
            .aRvlcPreviousCodebook[bnds] =
956
14.2k
            pAacDecoderChannelInfo->pDynData->aCodeBook[bnds];
957
14.2k
      }
958
2.32M
      for (; band < lastSfbIndex; band++) {
959
2.28M
        bnds = 16 * group + band;
960
2.28M
        FDK_ASSERT(bnds >= 0 && bnds < RVLC_MAX_SFB);
961
2.28M
        pAacDecoderStaticChannelInfo->concealmentInfo
962
2.28M
            .aRvlcPreviousCodebook[bnds] = ZERO_HCB;
963
2.28M
      }
964
38.6k
    }
965
129k
  } else {
966
129k
    int band;
967
129k
    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
129k
    if (((pRvlc->conceal_min != CONCEAL_MIN_INIT) ||
976
18.7k
         (pRvlc->conceal_max != CONCEAL_MAX_INIT)) &&
977
115k
        (pRvlc->conceal_min <= pRvlc->conceal_max) &&
978
86.2k
        (pAacDecoderStaticChannelInfo->concealmentInfo.rvlcPreviousBlockType ==
979
86.2k
         currentBlockType) &&
980
74.0k
        pAacDecoderStaticChannelInfo->concealmentInfo
981
74.0k
            .rvlcPreviousScaleFactorOK &&
982
62.8k
        pRvlc->sf_concealment && ConcealStatus) {
983
37.9k
      BidirectionalEstimation_UseScfOfPrevFrameAsReference(
984
37.9k
          pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo);
985
37.9k
      ConcealStatus = 0;
986
37.9k
    }
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
129k
    if ((pRvlc->conceal_min <= pRvlc->conceal_max) &&
994
99.8k
        ((pRvlc->conceal_min != CONCEAL_MIN_INIT) ||
995
14.6k
         (pRvlc->conceal_max != CONCEAL_MAX_INIT)) &&
996
86.2k
        !(pAacDecoderStaticChannelInfo->concealmentInfo
997
86.2k
              .rvlcPreviousScaleFactorOK &&
998
62.9k
          pRvlc->sf_concealment &&
999
37.9k
          (pAacDecoderStaticChannelInfo->concealmentInfo
1000
37.9k
               .rvlcPreviousBlockType == currentBlockType)) &&
1001
48.3k
        ConcealStatus) {
1002
48.3k
      BidirectionalEstimation_UseLowerScfOfCurrentFrame(pAacDecoderChannelInfo);
1003
48.3k
      ConcealStatus = 0;
1004
48.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
129k
    if ((pRvlc->conceal_min <= pRvlc->conceal_max) &&
1009
99.8k
        ((ErrorStatusLastScf && ErrorStatusFirstScf) ||
1010
71.3k
         (ErrorStatusLastNrg && ErrorStatusFirstNrg) ||
1011
69.2k
         (ErrorStatusLastIs && ErrorStatusFirstIs)) &&
1012
31.5k
        !(ErrorStatusForbiddenCwFwd || ErrorStatusForbiddenCwBwd ||
1013
18.5k
          ErrorStatusLengthEscapes) &&
1014
16.4k
        ConcealStatus) {
1015
3.34k
      StatisticalEstimation(pAacDecoderChannelInfo);
1016
3.34k
      ConcealStatus = 0;
1017
3.34k
    }
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
129k
    if ((pRvlc->conceal_min <= pRvlc->conceal_max) &&
1023
99.8k
        pAacDecoderStaticChannelInfo->concealmentInfo
1024
99.8k
            .rvlcPreviousScaleFactorOK &&
1025
71.5k
        pRvlc->sf_concealment &&
1026
44.1k
        (pAacDecoderStaticChannelInfo->concealmentInfo.rvlcPreviousBlockType ==
1027
44.1k
         currentBlockType) &&
1028
44.0k
        ConcealStatus) {
1029
6.06k
      PredictiveInterpolation(pAacDecoderChannelInfo,
1030
6.06k
                              pAacDecoderStaticChannelInfo);
1031
6.06k
      ConcealStatus = 0;
1032
6.06k
    }
1033
1034
    /* Call frame concealment, because no better strategy was found. Setting the
1035
       scalefactors to zero is done for debugging purposes */
1036
129k
    if (ConcealStatus) {
1037
86.8k
      for (group = 0; group < pRvlc->numWindowGroups; group++) {
1038
453k
        for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
1039
400k
          pAacDecoderChannelInfo->pDynData->aScaleFactor[16 * group + band] = 0;
1040
400k
        }
1041
52.8k
      }
1042
33.9k
      pAacDecoderChannelInfo->pDynData->specificTo.aac
1043
33.9k
          .rvlcCurrentScaleFactorOK = 0;
1044
33.9k
    }
1045
129k
  }
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
16.6k
    pRvlc->length_of_rvlc_sf = FDKreadBits(bs, 11); /* #3 */
1084
148k
  } else {
1085
148k
    pRvlc->length_of_rvlc_sf = FDKreadBits(bs, 9); /* #3 */
1086
148k
  }
1087
1088
  /* check if noise codebook is used */
1089
410k
  for (group = 0; group < pRvlc->numWindowGroups; group++) {
1090
1.00M
    for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
1091
777k
      if (pAacDecoderChannelInfo->pDynData->aCodeBook[16 * group + band] ==
1092
777k
          NOISE_HCB) {
1093
17.7k
        pRvlc->noise_used = 1;
1094
17.7k
        break;
1095
17.7k
      }
1096
777k
    }
1097
245k
  }
1098
1099
165k
  if (pRvlc->noise_used)
1100
16.9k
    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
16.9k
    pRvlc->dpcm_noise_last_position = FDKreadBits(bs, 9); /* #7  PNS */
1110
16.9k
    pRvlc->length_of_rvlc_sf -= 9;
1111
16.9k
  }
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.8k
    rvlcDecodeEscapes(
1151
39.8k
        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
73.6k
    FDKpushBiDirectional(bs, bitCntOffst);
1165
73.6k
  }
1166
165k
}
1167
1168
void CRvlc_ElementCheck(
1169
    CAacDecoderChannelInfo *pAacDecoderChannelInfo[],
1170
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo[],
1171
584k
    const UINT flags, const INT elChannels) {
1172
584k
  int ch;
1173
1174
  /* Required for MPS residuals. */
1175
584k
  if (pAacDecoderStaticChannelInfo == NULL) {
1176
0
    return;
1177
0
  }
1178
1179
  /* RVLC specific sanity checks */
1180
584k
  if ((flags & AC_ER_RVLC) && (elChannels == 2)) { /* to be reviewed */
1181
63.0k
    if (((pAacDecoderChannelInfo[0]
1182
63.0k
              ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK == 0) ||
1183
50.7k
         (pAacDecoderChannelInfo[1]
1184
50.7k
              ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK == 0)) &&
1185
21.2k
        pAacDecoderChannelInfo[0]->pComData->jointStereoData.MsMaskPresent) {
1186
10.3k
      pAacDecoderChannelInfo[0]
1187
10.3k
          ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK = 0;
1188
10.3k
      pAacDecoderChannelInfo[1]
1189
10.3k
          ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK = 0;
1190
10.3k
    }
1191
1192
63.0k
    if ((pAacDecoderChannelInfo[0]
1193
63.0k
             ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK == 0) &&
1194
14.8k
        (pAacDecoderChannelInfo[1]
1195
14.8k
             ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK == 1) &&
1196
1.07k
        (pAacDecoderChannelInfo[1]
1197
1.07k
             ->pDynData->specificTo.aac.rvlcIntensityUsed == 1)) {
1198
770
      pAacDecoderChannelInfo[1]
1199
770
          ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK = 0;
1200
770
    }
1201
63.0k
  }
1202
1203
1.39M
  for (ch = 0; ch < elChannels; ch++) {
1204
814k
    pAacDecoderStaticChannelInfo[ch]->concealmentInfo.rvlcPreviousBlockType =
1205
814k
        (GetWindowSequence(&pAacDecoderChannelInfo[ch]->icsInfo) == BLOCK_SHORT)
1206
814k
            ? 0
1207
814k
            : 1;
1208
814k
    if (flags & AC_ER_RVLC) {
1209
161k
      pAacDecoderStaticChannelInfo[ch]
1210
161k
          ->concealmentInfo.rvlcPreviousScaleFactorOK =
1211
161k
          pAacDecoderChannelInfo[ch]
1212
161k
              ->pDynData->specificTo.aac.rvlcCurrentScaleFactorOK;
1213
652k
    } else {
1214
652k
      pAacDecoderStaticChannelInfo[ch]
1215
652k
          ->concealmentInfo.rvlcPreviousScaleFactorOK = 0;
1216
652k
    }
1217
814k
  }
1218
584k
}