Coverage Report

Created: 2026-01-09 06:47

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