Coverage Report

Created: 2025-11-16 07:20

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