Coverage Report

Created: 2025-11-09 07:00

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