Coverage Report

Created: 2025-07-23 06:43

/src/aac/libAACdec/src/rvlcconceal.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2018 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 concealment
106
  \author Josef Hoepfl
107
*/
108
109
#include "rvlcconceal.h"
110
111
#include "block.h"
112
#include "rvlc.h"
113
114
/*---------------------------------------------------------------------------------------------
115
  function:      calcRefValFwd
116
117
  description:   The function determines the scalefactor which is closed to the
118
scalefactorband conceal_min. The same is done for intensity data and noise
119
energies.
120
-----------------------------------------------------------------------------------------------
121
  output:        - reference value scf
122
                 - reference value internsity data
123
                 - reference value noise energy
124
-----------------------------------------------------------------------------------------------
125
  return:        -
126
--------------------------------------------------------------------------------------------
127
*/
128
129
static void calcRefValFwd(CErRvlcInfo *pRvlc,
130
                          CAacDecoderChannelInfo *pAacDecoderChannelInfo,
131
56.9k
                          int *refIsFwd, int *refNrgFwd, int *refScfFwd) {
132
56.9k
  int band, bnds, group, startBand;
133
56.9k
  int idIs, idNrg, idScf;
134
56.9k
  int conceal_min, conceal_group_min;
135
56.9k
  int MaximumScaleFactorBands;
136
137
56.9k
  if (GetWindowSequence(&pAacDecoderChannelInfo->icsInfo) == BLOCK_SHORT)
138
1.35k
    MaximumScaleFactorBands = 16;
139
55.5k
  else
140
55.5k
    MaximumScaleFactorBands = 64;
141
142
56.9k
  conceal_min = pRvlc->conceal_min % MaximumScaleFactorBands;
143
56.9k
  conceal_group_min = pRvlc->conceal_min / MaximumScaleFactorBands;
144
145
  /* calculate first reference value for approach in forward direction */
146
56.9k
  idIs = idNrg = idScf = 1;
147
148
  /* set reference values */
149
56.9k
  *refIsFwd = -SF_OFFSET;
150
56.9k
  *refNrgFwd = pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain -
151
56.9k
               SF_OFFSET - 90 - 256;
152
56.9k
  *refScfFwd =
153
56.9k
      pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain - SF_OFFSET;
154
155
56.9k
  startBand = conceal_min - 1;
156
115k
  for (group = conceal_group_min; group >= 0; group--) {
157
78.0k
    for (band = startBand; band >= 0; band--) {
158
19.8k
      bnds = 16 * group + band;
159
19.8k
      switch (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds]) {
160
3.30k
        case ZERO_HCB:
161
3.30k
          break;
162
971
        case INTENSITY_HCB:
163
1.68k
        case INTENSITY_HCB2:
164
1.68k
          if (idIs) {
165
554
            *refIsFwd =
166
554
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds];
167
554
            idIs = 0; /* reference value has been set */
168
554
          }
169
1.68k
          break;
170
2.06k
        case NOISE_HCB:
171
2.06k
          if (idNrg) {
172
917
            *refNrgFwd =
173
917
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds];
174
917
            idNrg = 0; /* reference value has been set */
175
917
          }
176
2.06k
          break;
177
12.8k
        default:
178
12.8k
          if (idScf) {
179
10.5k
            *refScfFwd =
180
10.5k
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds];
181
10.5k
            idScf = 0; /* reference value has been set */
182
10.5k
          }
183
12.8k
          break;
184
19.8k
      }
185
19.8k
    }
186
58.2k
    startBand = pRvlc->maxSfbTransmitted - 1;
187
58.2k
  }
188
56.9k
}
189
190
/*---------------------------------------------------------------------------------------------
191
  function:      calcRefValBwd
192
193
  description:   The function determines the scalefactor which is closed to the
194
scalefactorband conceal_max. The same is done for intensity data and noise
195
energies.
196
-----------------------------------------------------------------------------------------------
197
  output:        - reference value scf
198
                 - reference value internsity data
199
                 - reference value noise energy
200
-----------------------------------------------------------------------------------------------
201
  return:        -
202
--------------------------------------------------------------------------------------------
203
*/
204
205
static void calcRefValBwd(CErRvlcInfo *pRvlc,
206
                          CAacDecoderChannelInfo *pAacDecoderChannelInfo,
207
56.9k
                          int *refIsBwd, int *refNrgBwd, int *refScfBwd) {
208
56.9k
  int band, bnds, group, startBand;
209
56.9k
  int idIs, idNrg, idScf;
210
56.9k
  int conceal_max, conceal_group_max;
211
56.9k
  int MaximumScaleFactorBands;
212
213
56.9k
  if (GetWindowSequence(&pAacDecoderChannelInfo->icsInfo) == BLOCK_SHORT)
214
1.35k
    MaximumScaleFactorBands = 16;
215
55.5k
  else
216
55.5k
    MaximumScaleFactorBands = 64;
217
218
56.9k
  conceal_max = pRvlc->conceal_max % MaximumScaleFactorBands;
219
56.9k
  conceal_group_max = pRvlc->conceal_max / MaximumScaleFactorBands;
220
221
  /* calculate first reference value for approach in backward direction */
222
56.9k
  idIs = idNrg = idScf = 1;
223
224
  /* set reference values */
225
56.9k
  *refIsBwd = pRvlc->dpcm_is_last_position - SF_OFFSET;
226
56.9k
  *refNrgBwd = pRvlc->rev_global_gain + pRvlc->dpcm_noise_last_position -
227
56.9k
               SF_OFFSET - 90 - 256 + pRvlc->dpcm_noise_nrg;
228
56.9k
  *refScfBwd = pRvlc->rev_global_gain - SF_OFFSET;
229
230
56.9k
  startBand = conceal_max + 1;
231
232
  /* if needed, re-set reference values */
233
118k
  for (group = conceal_group_max; group < pRvlc->numWindowGroups; group++) {
234
106k
    for (band = startBand; band < pRvlc->maxSfbTransmitted; band++) {
235
45.2k
      bnds = 16 * group + band;
236
45.2k
      switch (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds]) {
237
10.3k
        case ZERO_HCB:
238
10.3k
          break;
239
787
        case INTENSITY_HCB:
240
3.69k
        case INTENSITY_HCB2:
241
3.69k
          if (idIs) {
242
1.04k
            *refIsBwd =
243
1.04k
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd[bnds];
244
1.04k
            idIs = 0; /* reference value has been set */
245
1.04k
          }
246
3.69k
          break;
247
863
        case NOISE_HCB:
248
863
          if (idNrg) {
249
193
            *refNrgBwd =
250
193
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd[bnds];
251
193
            idNrg = 0; /* reference value has been set */
252
193
          }
253
863
          break;
254
30.3k
        default:
255
30.3k
          if (idScf) {
256
12.3k
            *refScfBwd =
257
12.3k
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd[bnds];
258
12.3k
            idScf = 0; /* reference value has been set */
259
12.3k
          }
260
30.3k
          break;
261
45.2k
      }
262
45.2k
    }
263
61.2k
    startBand = 0;
264
61.2k
  }
265
56.9k
}
266
267
/*---------------------------------------------------------------------------------------------
268
  function:      BidirectionalEstimation_UseLowerScfOfCurrentFrame
269
270
  description:   This approach by means of bidirectional estimation is generally
271
performed when a single bit error has been detected, the bit error can be
272
isolated between 'conceal_min' and 'conceal_max' and the 'sf_concealment' flag
273
is not set. The sets of scalefactors decoded in forward and backward direction
274
are compared with each other. The smaller scalefactor will be considered as the
275
correct one respectively. The reconstruction of the scalefactors with this
276
approach archieve good results in audio quality. The strategy must be applied to
277
scalefactors, intensity data and noise energy seperately.
278
-----------------------------------------------------------------------------------------------
279
  output:        Concealed scalefactor, noise energy and intensity data between
280
conceal_min and conceal_max
281
-----------------------------------------------------------------------------------------------
282
  return:        -
283
--------------------------------------------------------------------------------------------
284
*/
285
286
void BidirectionalEstimation_UseLowerScfOfCurrentFrame(
287
65.7k
    CAacDecoderChannelInfo *pAacDecoderChannelInfo) {
288
65.7k
  CErRvlcInfo *pRvlc =
289
65.7k
      &pAacDecoderChannelInfo->pComData->overlay.aac.erRvlcInfo;
290
65.7k
  int band, bnds, startBand, endBand, group;
291
65.7k
  int conceal_min, conceal_max;
292
65.7k
  int conceal_group_min, conceal_group_max;
293
65.7k
  int MaximumScaleFactorBands;
294
295
65.7k
  if (GetWindowSequence(&pAacDecoderChannelInfo->icsInfo) == BLOCK_SHORT) {
296
6.14k
    MaximumScaleFactorBands = 16;
297
59.5k
  } else {
298
59.5k
    MaximumScaleFactorBands = 64;
299
59.5k
  }
300
301
  /* If an error was detected just in forward or backward direction, set the
302
     corresponding border for concealment to a appropriate scalefactor band. The
303
     border is set to first or last sfb respectively, because the error will
304
     possibly not follow directly after the corrupt bit but just after decoding
305
     some more (wrong) scalefactors. */
306
65.7k
  if (pRvlc->conceal_min == CONCEAL_MIN_INIT) pRvlc->conceal_min = 0;
307
308
65.7k
  if (pRvlc->conceal_max == CONCEAL_MAX_INIT)
309
4.46k
    pRvlc->conceal_max =
310
4.46k
        (pRvlc->numWindowGroups - 1) * 16 + pRvlc->maxSfbTransmitted - 1;
311
312
65.7k
  conceal_min = pRvlc->conceal_min % MaximumScaleFactorBands;
313
65.7k
  conceal_group_min = pRvlc->conceal_min / MaximumScaleFactorBands;
314
65.7k
  conceal_max = pRvlc->conceal_max % MaximumScaleFactorBands;
315
65.7k
  conceal_group_max = pRvlc->conceal_max / MaximumScaleFactorBands;
316
317
65.7k
  if (pRvlc->conceal_min == pRvlc->conceal_max) {
318
56.9k
    int refIsFwd, refNrgFwd, refScfFwd;
319
56.9k
    int refIsBwd, refNrgBwd, refScfBwd;
320
321
56.9k
    bnds = pRvlc->conceal_min;
322
56.9k
    calcRefValFwd(pRvlc, pAacDecoderChannelInfo, &refIsFwd, &refNrgFwd,
323
56.9k
                  &refScfFwd);
324
56.9k
    calcRefValBwd(pRvlc, pAacDecoderChannelInfo, &refIsBwd, &refNrgBwd,
325
56.9k
                  &refScfBwd);
326
327
56.9k
    switch (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds]) {
328
39.6k
      case ZERO_HCB:
329
39.6k
        break;
330
450
      case INTENSITY_HCB:
331
664
      case INTENSITY_HCB2:
332
664
        if (refIsFwd < refIsBwd)
333
144
          pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] = refIsFwd;
334
520
        else
335
520
          pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] = refIsBwd;
336
664
        break;
337
1.10k
      case NOISE_HCB:
338
1.10k
        if (refNrgFwd < refNrgBwd)
339
313
          pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] = refNrgFwd;
340
793
        else
341
793
          pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] = refNrgBwd;
342
1.10k
        break;
343
15.5k
      default:
344
15.5k
        if (refScfFwd < refScfBwd)
345
13.7k
          pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] = refScfFwd;
346
1.77k
        else
347
1.77k
          pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] = refScfBwd;
348
15.5k
        break;
349
56.9k
    }
350
56.9k
  } else {
351
8.76k
    pAacDecoderChannelInfo->pComData->overlay.aac
352
8.76k
        .aRvlcScfFwd[pRvlc->conceal_max] =
353
8.76k
        pAacDecoderChannelInfo->pComData->overlay.aac
354
8.76k
            .aRvlcScfBwd[pRvlc->conceal_max];
355
8.76k
    pAacDecoderChannelInfo->pComData->overlay.aac
356
8.76k
        .aRvlcScfBwd[pRvlc->conceal_min] =
357
8.76k
        pAacDecoderChannelInfo->pComData->overlay.aac
358
8.76k
            .aRvlcScfFwd[pRvlc->conceal_min];
359
360
    /* consider the smaller of the forward and backward decoded value as the
361
     * correct one */
362
8.76k
    startBand = conceal_min;
363
8.76k
    if (conceal_group_min == conceal_group_max)
364
5.68k
      endBand = conceal_max;
365
3.08k
    else
366
3.08k
      endBand = pRvlc->maxSfbTransmitted - 1;
367
368
22.7k
    for (group = conceal_group_min; group <= conceal_group_max; group++) {
369
59.8k
      for (band = startBand; band <= endBand; band++) {
370
45.8k
        bnds = 16 * group + band;
371
45.8k
        if (pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds] <
372
45.8k
            pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd[bnds])
373
18.7k
          pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
374
18.7k
              pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds];
375
27.0k
        else
376
27.0k
          pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
377
27.0k
              pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd[bnds];
378
45.8k
      }
379
13.9k
      startBand = 0;
380
13.9k
      if ((group + 1) == conceal_group_max) endBand = conceal_max;
381
13.9k
    }
382
8.76k
  }
383
384
  /* now copy all data to the output buffer which needs not to be concealed */
385
65.7k
  if (conceal_group_min == 0)
386
62.1k
    endBand = conceal_min;
387
3.52k
  else
388
3.52k
    endBand = pRvlc->maxSfbTransmitted;
389
143k
  for (group = 0; group <= conceal_group_min; group++) {
390
144k
    for (band = 0; band < endBand; band++) {
391
67.0k
      bnds = 16 * group + band;
392
67.0k
      pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
393
67.0k
          pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds];
394
67.0k
    }
395
77.4k
    if ((group + 1) == conceal_group_min) endBand = conceal_min;
396
77.4k
  }
397
398
65.7k
  startBand = conceal_max + 1;
399
143k
  for (group = conceal_group_max; group < pRvlc->numWindowGroups; group++) {
400
157k
    for (band = startBand; band < pRvlc->maxSfbTransmitted; band++) {
401
79.3k
      bnds = 16 * group + band;
402
79.3k
      pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
403
79.3k
          pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd[bnds];
404
79.3k
    }
405
78.1k
    startBand = 0;
406
78.1k
  }
407
65.7k
}
408
409
/*---------------------------------------------------------------------------------------------
410
  function:      BidirectionalEstimation_UseScfOfPrevFrameAsReference
411
412
  description:   This approach by means of bidirectional estimation is generally
413
performed when a single bit error has been detected, the bit error can be
414
isolated between 'conceal_min' and 'conceal_max', the 'sf_concealment' flag is
415
set and the previous frame has the same block type as the current frame. The
416
scalefactor decoded in forward and backward direction and the scalefactor of the
417
previous frame are compared with each other. The smaller scalefactor will be
418
considered as the correct one. At this the codebook of the previous and current
419
frame must be of the same set (scf, nrg, is) in each scalefactorband. Otherwise
420
the scalefactor of the previous frame is not considered in the minimum
421
calculation. The reconstruction of the scalefactors with this approach archieve
422
good results in audio quality. The strategy must be applied to scalefactors,
423
intensity data and noise energy seperately.
424
-----------------------------------------------------------------------------------------------
425
  output:        Concealed scalefactor, noise energy and intensity data between
426
conceal_min and conceal_max
427
-----------------------------------------------------------------------------------------------
428
  return:        -
429
--------------------------------------------------------------------------------------------
430
*/
431
432
void BidirectionalEstimation_UseScfOfPrevFrameAsReference(
433
    CAacDecoderChannelInfo *pAacDecoderChannelInfo,
434
43.8k
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo) {
435
43.8k
  CErRvlcInfo *pRvlc =
436
43.8k
      &pAacDecoderChannelInfo->pComData->overlay.aac.erRvlcInfo;
437
43.8k
  int band, bnds, startBand, endBand, group;
438
43.8k
  int conceal_min, conceal_max;
439
43.8k
  int conceal_group_min, conceal_group_max;
440
43.8k
  int MaximumScaleFactorBands;
441
43.8k
  SHORT commonMin;
442
443
43.8k
  if (GetWindowSequence(&pAacDecoderChannelInfo->icsInfo) == BLOCK_SHORT) {
444
5.16k
    MaximumScaleFactorBands = 16;
445
38.7k
  } else {
446
38.7k
    MaximumScaleFactorBands = 64;
447
38.7k
  }
448
449
  /* If an error was detected just in forward or backward direction, set the
450
     corresponding border for concealment to a appropriate scalefactor band. The
451
     border is set to first or last sfb respectively, because the error will
452
     possibly not follow directly after the corrupt bit but just after decoding
453
     some more (wrong) scalefactors. */
454
43.8k
  if (pRvlc->conceal_min == CONCEAL_MIN_INIT) pRvlc->conceal_min = 0;
455
456
43.8k
  if (pRvlc->conceal_max == CONCEAL_MAX_INIT)
457
4.99k
    pRvlc->conceal_max =
458
4.99k
        (pRvlc->numWindowGroups - 1) * 16 + pRvlc->maxSfbTransmitted - 1;
459
460
43.8k
  conceal_min = pRvlc->conceal_min % MaximumScaleFactorBands;
461
43.8k
  conceal_group_min = pRvlc->conceal_min / MaximumScaleFactorBands;
462
43.8k
  conceal_max = pRvlc->conceal_max % MaximumScaleFactorBands;
463
43.8k
  conceal_group_max = pRvlc->conceal_max / MaximumScaleFactorBands;
464
465
43.8k
  pAacDecoderChannelInfo->pComData->overlay.aac
466
43.8k
      .aRvlcScfFwd[pRvlc->conceal_max] =
467
43.8k
      pAacDecoderChannelInfo->pComData->overlay.aac
468
43.8k
          .aRvlcScfBwd[pRvlc->conceal_max];
469
43.8k
  pAacDecoderChannelInfo->pComData->overlay.aac
470
43.8k
      .aRvlcScfBwd[pRvlc->conceal_min] =
471
43.8k
      pAacDecoderChannelInfo->pComData->overlay.aac
472
43.8k
          .aRvlcScfFwd[pRvlc->conceal_min];
473
474
  /* consider the smaller of the forward and backward decoded value as the
475
   * correct one */
476
43.8k
  startBand = conceal_min;
477
43.8k
  if (conceal_group_min == conceal_group_max)
478
39.7k
    endBand = conceal_max;
479
4.10k
  else
480
4.10k
    endBand = pRvlc->maxSfbTransmitted - 1;
481
482
95.3k
  for (group = conceal_group_min; group <= conceal_group_max; group++) {
483
153k
    for (band = startBand; band <= endBand; band++) {
484
101k
      bnds = 16 * group + band;
485
101k
      switch (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds]) {
486
31.5k
        case ZERO_HCB:
487
31.5k
          pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] = 0;
488
31.5k
          break;
489
490
4.41k
        case INTENSITY_HCB:
491
25.8k
        case INTENSITY_HCB2:
492
25.8k
          if ((pAacDecoderStaticChannelInfo->concealmentInfo
493
25.8k
                   .aRvlcPreviousCodebook[bnds] == INTENSITY_HCB) ||
494
25.8k
              (pAacDecoderStaticChannelInfo->concealmentInfo
495
25.8k
                   .aRvlcPreviousCodebook[bnds] == INTENSITY_HCB2)) {
496
19.8k
            commonMin = fMin(
497
19.8k
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds],
498
19.8k
                pAacDecoderChannelInfo->pComData->overlay.aac
499
19.8k
                    .aRvlcScfBwd[bnds]);
500
19.8k
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
501
19.8k
                fMin(commonMin, pAacDecoderStaticChannelInfo->concealmentInfo
502
19.8k
                                    .aRvlcPreviousScaleFactor[bnds]);
503
19.8k
          } else {
504
6.04k
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] = fMin(
505
6.04k
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds],
506
6.04k
                pAacDecoderChannelInfo->pComData->overlay.aac
507
6.04k
                    .aRvlcScfBwd[bnds]);
508
6.04k
          }
509
25.8k
          break;
510
511
4.90k
        case NOISE_HCB:
512
4.90k
          if (pAacDecoderStaticChannelInfo->concealmentInfo
513
4.90k
                  .aRvlcPreviousCodebook[bnds] == NOISE_HCB) {
514
682
            commonMin = fMin(
515
682
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds],
516
682
                pAacDecoderChannelInfo->pComData->overlay.aac
517
682
                    .aRvlcScfBwd[bnds]);
518
682
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
519
682
                fMin(commonMin, pAacDecoderStaticChannelInfo->concealmentInfo
520
682
                                    .aRvlcPreviousScaleFactor[bnds]);
521
4.21k
          } else {
522
4.21k
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] = fMin(
523
4.21k
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds],
524
4.21k
                pAacDecoderChannelInfo->pComData->overlay.aac
525
4.21k
                    .aRvlcScfBwd[bnds]);
526
4.21k
          }
527
4.90k
          break;
528
529
39.5k
        default:
530
39.5k
          if ((pAacDecoderStaticChannelInfo->concealmentInfo
531
39.5k
                   .aRvlcPreviousCodebook[bnds] != ZERO_HCB) &&
532
39.5k
              (pAacDecoderStaticChannelInfo->concealmentInfo
533
12.5k
                   .aRvlcPreviousCodebook[bnds] != NOISE_HCB) &&
534
39.5k
              (pAacDecoderStaticChannelInfo->concealmentInfo
535
12.2k
                   .aRvlcPreviousCodebook[bnds] != INTENSITY_HCB) &&
536
39.5k
              (pAacDecoderStaticChannelInfo->concealmentInfo
537
12.1k
                   .aRvlcPreviousCodebook[bnds] != INTENSITY_HCB2)) {
538
12.0k
            commonMin = fMin(
539
12.0k
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds],
540
12.0k
                pAacDecoderChannelInfo->pComData->overlay.aac
541
12.0k
                    .aRvlcScfBwd[bnds]);
542
12.0k
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
543
12.0k
                fMin(commonMin, pAacDecoderStaticChannelInfo->concealmentInfo
544
12.0k
                                    .aRvlcPreviousScaleFactor[bnds]);
545
27.4k
          } else {
546
27.4k
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] = fMin(
547
27.4k
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds],
548
27.4k
                pAacDecoderChannelInfo->pComData->overlay.aac
549
27.4k
                    .aRvlcScfBwd[bnds]);
550
27.4k
          }
551
39.5k
          break;
552
101k
      }
553
101k
    }
554
51.4k
    startBand = 0;
555
51.4k
    if ((group + 1) == conceal_group_max) endBand = conceal_max;
556
51.4k
  }
557
558
  /* now copy all data to the output buffer which needs not to be concealed */
559
43.8k
  if (conceal_group_min == 0)
560
38.7k
    endBand = conceal_min;
561
5.12k
  else
562
5.12k
    endBand = pRvlc->maxSfbTransmitted;
563
101k
  for (group = 0; group <= conceal_group_min; group++) {
564
105k
    for (band = 0; band < endBand; band++) {
565
48.1k
      bnds = 16 * group + band;
566
48.1k
      pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
567
48.1k
          pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds];
568
48.1k
    }
569
57.2k
    if ((group + 1) == conceal_group_min) endBand = conceal_min;
570
57.2k
  }
571
572
43.8k
  startBand = conceal_max + 1;
573
92.3k
  for (group = conceal_group_max; group < pRvlc->numWindowGroups; group++) {
574
72.9k
    for (band = startBand; band < pRvlc->maxSfbTransmitted; band++) {
575
24.4k
      bnds = 16 * group + band;
576
24.4k
      pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
577
24.4k
          pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd[bnds];
578
24.4k
    }
579
48.5k
    startBand = 0;
580
48.5k
  }
581
43.8k
}
582
583
/*---------------------------------------------------------------------------------------------
584
  function:      StatisticalEstimation
585
586
  description:   This approach by means of statistical estimation is generally
587
performed when both the start value and the end value are different and no
588
further errors have been detected. Considering the forward and backward decoded
589
scalefactors, the set with the lower scalefactors in sum will be considered as
590
the correct one. The scalefactors are differentially encoded. Normally it would
591
reach to compare one pair of the forward and backward decoded scalefactors to
592
specify the lower set. But having detected no further errors does not
593
necessarily mean the absence of errors. Therefore all scalefactors decoded in
594
forward and backward direction are summed up seperately. The set with the lower
595
sum will be used. The strategy must be applied to scalefactors, intensity data
596
and noise energy seperately.
597
-----------------------------------------------------------------------------------------------
598
  output:        Concealed scalefactor, noise energy and intensity data
599
-----------------------------------------------------------------------------------------------
600
  return:        -
601
--------------------------------------------------------------------------------------------
602
*/
603
604
4.07k
void StatisticalEstimation(CAacDecoderChannelInfo *pAacDecoderChannelInfo) {
605
4.07k
  CErRvlcInfo *pRvlc =
606
4.07k
      &pAacDecoderChannelInfo->pComData->overlay.aac.erRvlcInfo;
607
4.07k
  int band, bnds, group;
608
4.07k
  int sumIsFwd, sumIsBwd;   /* sum of intensity data forward/backward */
609
4.07k
  int sumNrgFwd, sumNrgBwd; /* sum of noise energy data forward/backward */
610
4.07k
  int sumScfFwd, sumScfBwd; /* sum of scalefactor data forward/backward */
611
4.07k
  int useIsFwd, useNrgFwd, useScfFwd; /* the flags signals the elements which
612
                                         are used for the final result */
613
614
4.07k
  sumIsFwd = sumIsBwd = sumNrgFwd = sumNrgBwd = sumScfFwd = sumScfBwd = 0;
615
4.07k
  useIsFwd = useNrgFwd = useScfFwd = 0;
616
617
  /* calculate sum of each group (scf,nrg,is) of forward and backward direction
618
   */
619
8.71k
  for (group = 0; group < pRvlc->numWindowGroups; group++) {
620
29.3k
    for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
621
24.6k
      bnds = 16 * group + band;
622
24.6k
      switch (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds]) {
623
10.2k
        case ZERO_HCB:
624
10.2k
          break;
625
626
559
        case INTENSITY_HCB:
627
1.15k
        case INTENSITY_HCB2:
628
1.15k
          sumIsFwd +=
629
1.15k
              pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds];
630
1.15k
          sumIsBwd +=
631
1.15k
              pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd[bnds];
632
1.15k
          break;
633
634
1.08k
        case NOISE_HCB:
635
1.08k
          sumNrgFwd +=
636
1.08k
              pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds];
637
1.08k
          sumNrgBwd +=
638
1.08k
              pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd[bnds];
639
1.08k
          break;
640
641
12.1k
        default:
642
12.1k
          sumScfFwd +=
643
12.1k
              pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds];
644
12.1k
          sumScfBwd +=
645
12.1k
              pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd[bnds];
646
12.1k
          break;
647
24.6k
      }
648
24.6k
    }
649
4.63k
  }
650
651
  /* find for each group (scf,nrg,is) the correct direction */
652
4.07k
  if (sumIsFwd < sumIsBwd) useIsFwd = 1;
653
654
4.07k
  if (sumNrgFwd < sumNrgBwd) useNrgFwd = 1;
655
656
4.07k
  if (sumScfFwd < sumScfBwd) useScfFwd = 1;
657
658
  /* conceal each group (scf,nrg,is) */
659
8.71k
  for (group = 0; group < pRvlc->numWindowGroups; group++) {
660
29.3k
    for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
661
24.6k
      bnds = 16 * group + band;
662
24.6k
      switch (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds]) {
663
10.2k
        case ZERO_HCB:
664
10.2k
          break;
665
666
559
        case INTENSITY_HCB:
667
1.15k
        case INTENSITY_HCB2:
668
1.15k
          if (useIsFwd)
669
346
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
670
346
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds];
671
805
          else
672
805
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
673
805
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd[bnds];
674
1.15k
          break;
675
676
1.08k
        case NOISE_HCB:
677
1.08k
          if (useNrgFwd)
678
306
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
679
306
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds];
680
782
          else
681
782
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
682
782
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd[bnds];
683
1.08k
          break;
684
685
12.1k
        default:
686
12.1k
          if (useScfFwd)
687
5.09k
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
688
5.09k
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds];
689
7.05k
          else
690
7.05k
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
691
7.05k
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfBwd[bnds];
692
12.1k
          break;
693
24.6k
      }
694
24.6k
    }
695
4.63k
  }
696
4.07k
}
697
698
/*---------------------------------------------------------------------------------------------
699
  description:   Approach by means of predictive interpolation
700
                 This approach by means of predictive estimation is generally
701
performed when the error cannot be isolated between 'conceal_min' and
702
'conceal_max', the 'sf_concealment' flag is set and the previous frame has the
703
same block type as the current frame. Check for each scalefactorband if the same
704
type of data (scalefactor, internsity data, noise energies) is transmitted. If
705
so use the scalefactor (intensity data, noise energy) in the current frame.
706
Otherwise set the scalefactor (intensity data, noise energy) for this
707
scalefactorband to zero.
708
-----------------------------------------------------------------------------------------------
709
  output:        Concealed scalefactor, noise energy and intensity data
710
-----------------------------------------------------------------------------------------------
711
  return:        -
712
--------------------------------------------------------------------------------------------
713
*/
714
715
void PredictiveInterpolation(
716
    CAacDecoderChannelInfo *pAacDecoderChannelInfo,
717
8.48k
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo) {
718
8.48k
  CErRvlcInfo *pRvlc =
719
8.48k
      &pAacDecoderChannelInfo->pComData->overlay.aac.erRvlcInfo;
720
8.48k
  int band, bnds, group;
721
8.48k
  SHORT commonMin;
722
723
17.1k
  for (group = 0; group < pRvlc->numWindowGroups; group++) {
724
42.3k
    for (band = 0; band < pRvlc->maxSfbTransmitted; band++) {
725
33.6k
      bnds = 16 * group + band;
726
33.6k
      switch (pAacDecoderChannelInfo->pDynData->aCodeBook[bnds]) {
727
196
        case ZERO_HCB:
728
196
          pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] = 0;
729
196
          break;
730
731
139
        case INTENSITY_HCB:
732
18.2k
        case INTENSITY_HCB2:
733
18.2k
          if ((pAacDecoderStaticChannelInfo->concealmentInfo
734
18.2k
                   .aRvlcPreviousCodebook[bnds] == INTENSITY_HCB) ||
735
18.2k
              (pAacDecoderStaticChannelInfo->concealmentInfo
736
18.2k
                   .aRvlcPreviousCodebook[bnds] == INTENSITY_HCB2)) {
737
17.3k
            commonMin = fMin(
738
17.3k
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds],
739
17.3k
                pAacDecoderChannelInfo->pComData->overlay.aac
740
17.3k
                    .aRvlcScfBwd[bnds]);
741
17.3k
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
742
17.3k
                fMin(commonMin, pAacDecoderStaticChannelInfo->concealmentInfo
743
17.3k
                                    .aRvlcPreviousScaleFactor[bnds]);
744
17.3k
          } else {
745
922
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] = -110;
746
922
          }
747
18.2k
          break;
748
749
868
        case NOISE_HCB:
750
868
          if (pAacDecoderStaticChannelInfo->concealmentInfo
751
868
                  .aRvlcPreviousCodebook[bnds] == NOISE_HCB) {
752
459
            commonMin = fMin(
753
459
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds],
754
459
                pAacDecoderChannelInfo->pComData->overlay.aac
755
459
                    .aRvlcScfBwd[bnds]);
756
459
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
757
459
                fMin(commonMin, pAacDecoderStaticChannelInfo->concealmentInfo
758
459
                                    .aRvlcPreviousScaleFactor[bnds]);
759
459
          } else {
760
409
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] = -110;
761
409
          }
762
868
          break;
763
764
14.3k
        default:
765
14.3k
          if ((pAacDecoderStaticChannelInfo->concealmentInfo
766
14.3k
                   .aRvlcPreviousCodebook[bnds] != ZERO_HCB) &&
767
14.3k
              (pAacDecoderStaticChannelInfo->concealmentInfo
768
13.3k
                   .aRvlcPreviousCodebook[bnds] != NOISE_HCB) &&
769
14.3k
              (pAacDecoderStaticChannelInfo->concealmentInfo
770
13.1k
                   .aRvlcPreviousCodebook[bnds] != INTENSITY_HCB) &&
771
14.3k
              (pAacDecoderStaticChannelInfo->concealmentInfo
772
13.0k
                   .aRvlcPreviousCodebook[bnds] != INTENSITY_HCB2)) {
773
13.0k
            commonMin = fMin(
774
13.0k
                pAacDecoderChannelInfo->pComData->overlay.aac.aRvlcScfFwd[bnds],
775
13.0k
                pAacDecoderChannelInfo->pComData->overlay.aac
776
13.0k
                    .aRvlcScfBwd[bnds]);
777
13.0k
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] =
778
13.0k
                fMin(commonMin, pAacDecoderStaticChannelInfo->concealmentInfo
779
13.0k
                                    .aRvlcPreviousScaleFactor[bnds]);
780
13.0k
          } else {
781
1.29k
            pAacDecoderChannelInfo->pDynData->aScaleFactor[bnds] = 0;
782
1.29k
          }
783
14.3k
          break;
784
33.6k
      }
785
33.6k
    }
786
8.70k
  }
787
8.48k
}