Coverage Report

Created: 2026-08-31 06:46

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