Coverage Report

Created: 2026-02-14 06:49

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