Coverage Report

Created: 2025-12-31 07:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fdk-aac/libAACdec/src/conceal.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2019 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):   Josef Hoepfl
98
99
   Description: independent channel concealment
100
101
*******************************************************************************/
102
103
/*!
104
  \page concealment AAC core concealment
105
106
  This AAC core implementation includes a concealment function, which can be
107
  enabled using the several defines during compilation.
108
109
  There are various tests inside the core, starting with simple CRC tests and
110
  ending in a variety of plausibility checks. If such a check indicates an
111
  invalid bitstream, then concealment is applied.
112
113
  Concealment is also applied when the calling main program indicates a
114
  distorted or missing data frame using the frameOK flag. This is used for error
115
  detection on the transport layer. (See below)
116
117
  There are three concealment-modes:
118
119
  1) Muting: The spectral data is simply set to zero in case of an detected
120
  error.
121
122
  2) Noise substitution: In case of an detected error, concealment copies the
123
  last frame and adds attenuates the spectral data. For this mode you have to
124
  set the #CONCEAL_NOISE define. Noise substitution adds no additional delay.
125
126
  3) Interpolation: The interpolation routine swaps the spectral data from the
127
  previous and the current frame just before the final frequency to time
128
  conversion. In case a single frame is corrupted, concealmant interpolates
129
  between the last good and the first good frame to create the spectral data for
130
  the missing frame. If multiple frames are corrupted, concealment implements
131
  first a fade out based on slightly modified spectral values from the last good
132
     frame. As soon as good frames are available, concealmant fades in the new
133
  spectral data. For this mode you have to set the #CONCEAL_INTER define. Note
134
  that in this case, you also need to set #SBR_BS_DELAY_ENABLE, which basically
135
  adds approriate delay in the SBR decoder. Note that the
136
  Interpolating-Concealment increases the delay of your decoder by one frame and
137
  that it does require additional resources such as memory and computational
138
  complexity.
139
140
  <h2>How concealment can be used with errors on the transport layer</h2>
141
142
  Many errors can or have to be detected on the transport layer. For example in
143
  IP based systems packet loss can occur. The transport protocol used should
144
  indicate such packet loss by inserting an empty frame with frameOK=0.
145
*/
146
147
#include "conceal.h"
148
149
#include "aac_rom.h"
150
#include "genericStds.h"
151
152
/* PNS (of block) */
153
#include "aacdec_pns.h"
154
#include "block.h"
155
156
13.0k
#define CONCEAL_DFLT_COMF_NOISE_LEVEL (0x100000)
157
158
4.78M
#define CONCEAL_NOT_DEFINED ((UCHAR)-1)
159
160
/* default settings */
161
13.0k
#define CONCEAL_DFLT_FADEOUT_FRAMES (6)
162
13.0k
#define CONCEAL_DFLT_FADEIN_FRAMES (5)
163
13.0k
#define CONCEAL_DFLT_MUTE_RELEASE_FRAMES (0)
164
165
#define CONCEAL_DFLT_FADE_FACTOR (0.707106781186548f) /* 1/sqrt(2) */
166
167
/* some often used constants: */
168
#define FIXP_ZERO FL2FXCONST_DBL(0.0f)
169
#define FIXP_ONE FL2FXCONST_DBL(1.0f)
170
#define FIXP_FL_CORRECTION FL2FXCONST_DBL(0.53333333333333333f)
171
172
/* For parameter conversion */
173
0
#define CONCEAL_PARAMETER_BITS (8)
174
0
#define CONCEAL_MAX_QUANT_FACTOR ((1 << CONCEAL_PARAMETER_BITS) - 1)
175
/*#define CONCEAL_MIN_ATTENUATION_FACTOR_025  ( FL2FXCONST_DBL(0.971627951577106174) )*/ /* -0.25 dB */
176
#define CONCEAL_MIN_ATTENUATION_FACTOR_025_LD \
177
  FL2FXCONST_DBL(-0.041524101186092029596853445212299)
178
/*#define CONCEAL_MIN_ATTENUATION_FACTOR_050  ( FL2FXCONST_DBL(0.944060876285923380) )*/ /* -0.50 dB */
179
#define CONCEAL_MIN_ATTENUATION_FACTOR_050_LD \
180
  FL2FXCONST_DBL(-0.083048202372184059253597008145293)
181
182
typedef enum {
183
  CConcealment_NoExpand,
184
  CConcealment_Expand,
185
  CConcealment_Compress
186
} CConcealmentExpandType;
187
188
static const FIXP_SGL facMod4Table[4] = {
189
    FL2FXCONST_SGL(0.500000000f), /* FIXP_SGL(0x4000),  2^-(1-0,00) */
190
    FL2FXCONST_SGL(0.594603558f), /* FIXP_SGL(0x4c1b),  2^-(1-0,25) */
191
    FL2FXCONST_SGL(0.707106781f), /* FIXP_SGL(0x5a82),  2^-(1-0,50) */
192
    FL2FXCONST_SGL(0.840896415f)  /* FIXP_SGL(0x6ba2)   2^-(1-0,75) */
193
};
194
195
static void CConcealment_CalcBandEnergy(
196
    FIXP_DBL *spectrum, const SamplingRateInfo *pSamplingRateInfo,
197
    const int blockType, CConcealmentExpandType ex, int *sfbEnergy);
198
199
static void CConcealment_InterpolateBuffer(FIXP_DBL *spectrum,
200
                                           SHORT *pSpecScalePrev,
201
                                           SHORT *pSpecScaleAct,
202
                                           SHORT *pSpecScaleOut, int *enPrv,
203
                                           int *enAct, int sfbCnt,
204
                                           const SHORT *pSfbOffset);
205
206
static int CConcealment_ApplyInter(
207
    CConcealmentInfo *pConcealmentInfo,
208
    CAacDecoderChannelInfo *pAacDecoderChannelInfo,
209
    const SamplingRateInfo *pSamplingRateInfo, const int samplesPerFrame,
210
    const int improveTonal, const int frameOk, const int mute_release_active);
211
212
static int CConcealment_ApplyNoise(
213
    CConcealmentInfo *pConcealmentInfo,
214
    CAacDecoderChannelInfo *pAacDecoderChannelInfo,
215
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
216
    const SamplingRateInfo *pSamplingRateInfo, const int samplesPerFrame,
217
    const UINT flags);
218
219
static void CConcealment_UpdateState(
220
    CConcealmentInfo *pConcealmentInfo, int frameOk,
221
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
222
    const int samplesPerFrame, CAacDecoderChannelInfo *pAacDecoderChannelInfo);
223
224
static void CConcealment_ApplyRandomSign(int iRandomPhase, FIXP_DBL *spec,
225
                                         int samplesPerFrame);
226
227
/* TimeDomainFading */
228
static void CConcealment_TDFadePcmAtt(int start, int len, FIXP_DBL fadeStart,
229
                                      FIXP_DBL fadeStop, PCM_DEC *pcmdata);
230
static void CConcealment_TDFadeFillFadingStations(FIXP_DBL *fadingStations,
231
                                                  int *fadingSteps,
232
                                                  FIXP_DBL fadeStop,
233
                                                  FIXP_DBL fadeStart,
234
                                                  TDfadingType fadingType);
235
static void CConcealment_TDFading_doLinearFadingSteps(int *fadingSteps);
236
237
/* Streamline the state machine */
238
static int CConcealment_ApplyFadeOut(
239
    int mode, CConcealmentInfo *pConcealmentInfo,
240
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
241
    const int samplesPerFrame, CAacDecoderChannelInfo *pAacDecoderChannelInfo);
242
243
static int CConcealment_TDNoise_Random(ULONG *seed);
244
static void CConcealment_TDNoise_Apply(CConcealmentInfo *const pConcealmentInfo,
245
                                       const int len,
246
                                       const INT aacOutDataHeadroom,
247
                                       PCM_DEC *const pcmdata);
248
249
2.75M
static BLOCK_TYPE CConcealment_GetWinSeq(int prevWinSeq) {
250
2.75M
  BLOCK_TYPE newWinSeq = BLOCK_LONG;
251
252
  /* Try to have only long blocks */
253
2.75M
  if (prevWinSeq == BLOCK_START || prevWinSeq == BLOCK_SHORT) {
254
18.4k
    newWinSeq = BLOCK_STOP;
255
18.4k
  }
256
257
2.75M
  return (newWinSeq);
258
2.75M
}
259
260
/*!
261
  \brief Init common concealment information data
262
263
  \param pConcealCommonData Pointer to the concealment common data structure.
264
*/
265
13.0k
void CConcealment_InitCommonData(CConcealParams *pConcealCommonData) {
266
13.0k
  if (pConcealCommonData != NULL) {
267
13.0k
    int i;
268
269
    /* Set default error concealment technique */
270
13.0k
    pConcealCommonData->method = ConcealMethodInter;
271
272
13.0k
    pConcealCommonData->numFadeOutFrames = CONCEAL_DFLT_FADEOUT_FRAMES;
273
13.0k
    pConcealCommonData->numFadeInFrames = CONCEAL_DFLT_FADEIN_FRAMES;
274
13.0k
    pConcealCommonData->numMuteReleaseFrames = CONCEAL_DFLT_MUTE_RELEASE_FRAMES;
275
276
13.0k
    pConcealCommonData->comfortNoiseLevel =
277
13.0k
        (FIXP_DBL)CONCEAL_DFLT_COMF_NOISE_LEVEL;
278
279
    /* Init fade factors (symetric) */
280
13.0k
    pConcealCommonData->fadeOutFactor[0] =
281
13.0k
        FL2FXCONST_SGL(CONCEAL_DFLT_FADE_FACTOR);
282
13.0k
    pConcealCommonData->fadeInFactor[0] = pConcealCommonData->fadeOutFactor[0];
283
284
417k
    for (i = 1; i < CONCEAL_MAX_NUM_FADE_FACTORS; i++) {
285
404k
      pConcealCommonData->fadeOutFactor[i] =
286
404k
          FX_DBL2FX_SGL(fMult(pConcealCommonData->fadeOutFactor[i - 1],
287
404k
                              FL2FXCONST_SGL(CONCEAL_DFLT_FADE_FACTOR)));
288
404k
      pConcealCommonData->fadeInFactor[i] =
289
404k
          pConcealCommonData->fadeOutFactor[i];
290
404k
    }
291
13.0k
  }
292
13.0k
}
293
294
/*!
295
  \brief Get current concealment method.
296
297
  \param pConcealCommonData Pointer to common concealment data (for all
298
  channels)
299
*/
300
44.0k
CConcealmentMethod CConcealment_GetMethod(CConcealParams *pConcealCommonData) {
301
44.0k
  CConcealmentMethod method = ConcealMethodNone;
302
303
44.0k
  if (pConcealCommonData != NULL) {
304
44.0k
    method = pConcealCommonData->method;
305
44.0k
  }
306
307
44.0k
  return (method);
308
44.0k
}
309
310
/*!
311
  \brief Init concealment information for each channel
312
313
  \param pConcealChannelInfo Pointer to the channel related concealment info
314
  structure to be initialized. \param pConcealCommonData  Pointer to common
315
  concealment data (for all channels) \param initRenderMode      Initial render
316
  mode to be set for the current channel. \param samplesPerFrame     The number
317
  of samples per frame.
318
*/
319
void CConcealment_InitChannelData(CConcealmentInfo *pConcealChannelInfo,
320
                                  CConcealParams *pConcealCommonData,
321
                                  AACDEC_RENDER_MODE initRenderMode,
322
700k
                                  int samplesPerFrame) {
323
700k
  int i;
324
700k
  pConcealChannelInfo->TDNoiseSeed = 0;
325
700k
  FDKmemclear(pConcealChannelInfo->TDNoiseStates,
326
700k
              sizeof(pConcealChannelInfo->TDNoiseStates));
327
700k
  pConcealChannelInfo->TDNoiseCoef[0] = FL2FXCONST_SGL(0.05f);
328
700k
  pConcealChannelInfo->TDNoiseCoef[1] = FL2FXCONST_SGL(0.5f);
329
700k
  pConcealChannelInfo->TDNoiseCoef[2] = FL2FXCONST_SGL(0.45f);
330
331
700k
  pConcealChannelInfo->pConcealParams = pConcealCommonData;
332
333
700k
  pConcealChannelInfo->lastRenderMode = initRenderMode;
334
335
700k
  pConcealChannelInfo->windowShape = CONCEAL_NOT_DEFINED;
336
700k
  pConcealChannelInfo->windowSequence = BLOCK_LONG; /* default type */
337
700k
  pConcealChannelInfo->lastWinGrpLen = 1;
338
339
700k
  pConcealChannelInfo->concealState = ConcealState_Ok;
340
341
700k
  FDKmemclear(pConcealChannelInfo->spectralCoefficient,
342
700k
              1024 * sizeof(FIXP_CNCL));
343
344
6.30M
  for (i = 0; i < 8; i++) {
345
5.60M
    pConcealChannelInfo->specScale[i] = 0;
346
5.60M
  }
347
348
700k
  pConcealChannelInfo->iRandomPhase = 0;
349
350
700k
  pConcealChannelInfo->prevFrameOk[0] = 1;
351
700k
  pConcealChannelInfo->prevFrameOk[1] = 1;
352
353
700k
  pConcealChannelInfo->cntFadeFrames = 0;
354
700k
  pConcealChannelInfo->cntValidFrames = 0;
355
700k
  pConcealChannelInfo->fade_old = (FIXP_DBL)MAXVAL_DBL;
356
700k
  pConcealChannelInfo->winGrpOffset[0] = 0;
357
700k
  pConcealChannelInfo->winGrpOffset[1] = 0;
358
700k
  pConcealChannelInfo->attGrpOffset[0] = 0;
359
700k
  pConcealChannelInfo->attGrpOffset[1] = 0;
360
700k
}
361
362
/*!
363
  \brief Set error concealment parameters
364
365
  \param concealParams
366
  \param method
367
  \param fadeOutSlope
368
  \param fadeInSlope
369
  \param muteRelease
370
  \param comfNoiseLevel
371
*/
372
AAC_DECODER_ERROR
373
CConcealment_SetParams(CConcealParams *concealParams, int method,
374
                       int fadeOutSlope, int fadeInSlope, int muteRelease,
375
31.0k
                       FIXP_DBL comfNoiseLevel) {
376
  /* set concealment technique */
377
31.0k
  if (method != AACDEC_CONCEAL_PARAM_NOT_SPECIFIED) {
378
31.0k
    switch ((CConcealmentMethod)method) {
379
0
      case ConcealMethodMute:
380
17.9k
      case ConcealMethodNoise:
381
31.0k
      case ConcealMethodInter:
382
        /* Be sure to enable delay adjustment of SBR decoder! */
383
31.0k
        if (concealParams == NULL) {
384
0
          return AAC_DEC_INVALID_HANDLE;
385
31.0k
        } else {
386
          /* set param */
387
31.0k
          concealParams->method = (CConcealmentMethod)method;
388
31.0k
        }
389
31.0k
        break;
390
391
31.0k
      default:
392
0
        return AAC_DEC_SET_PARAM_FAIL;
393
31.0k
    }
394
31.0k
  }
395
396
  /* set number of frames for fade-out slope */
397
31.0k
  if (fadeOutSlope != AACDEC_CONCEAL_PARAM_NOT_SPECIFIED) {
398
0
    if ((fadeOutSlope < CONCEAL_MAX_NUM_FADE_FACTORS) && (fadeOutSlope >= 0)) {
399
0
      if (concealParams == NULL) {
400
0
        return AAC_DEC_INVALID_HANDLE;
401
0
      } else {
402
        /* set param */
403
0
        concealParams->numFadeOutFrames = fadeOutSlope;
404
0
      }
405
0
    } else {
406
0
      return AAC_DEC_SET_PARAM_FAIL;
407
0
    }
408
0
  }
409
410
  /* set number of frames for fade-in slope */
411
31.0k
  if (fadeInSlope != AACDEC_CONCEAL_PARAM_NOT_SPECIFIED) {
412
0
    if ((fadeInSlope < CONCEAL_MAX_NUM_FADE_FACTORS) && (fadeInSlope >= 0)) {
413
0
      if (concealParams == NULL) {
414
0
        return AAC_DEC_INVALID_HANDLE;
415
0
      } else {
416
        /* set param */
417
0
        concealParams->numFadeInFrames = fadeInSlope;
418
0
      }
419
0
    } else {
420
0
      return AAC_DEC_SET_PARAM_FAIL;
421
0
    }
422
0
  }
423
424
  /* set number of error-free frames after which the muting will be released */
425
31.0k
  if (muteRelease != AACDEC_CONCEAL_PARAM_NOT_SPECIFIED) {
426
0
    if ((muteRelease < (CONCEAL_MAX_NUM_FADE_FACTORS << 1)) &&
427
0
        (muteRelease >= 0)) {
428
0
      if (concealParams == NULL) {
429
0
        return AAC_DEC_INVALID_HANDLE;
430
0
      } else {
431
        /* set param */
432
0
        concealParams->numMuteReleaseFrames = muteRelease;
433
0
      }
434
0
    } else {
435
0
      return AAC_DEC_SET_PARAM_FAIL;
436
0
    }
437
0
  }
438
439
  /* set confort noise level which will be inserted while in state 'muting' */
440
31.0k
  if (comfNoiseLevel != (FIXP_DBL)AACDEC_CONCEAL_PARAM_NOT_SPECIFIED) {
441
0
    if ((comfNoiseLevel < (FIXP_DBL)0) ||
442
0
        (comfNoiseLevel > (FIXP_DBL)MAXVAL_DBL)) {
443
0
      return AAC_DEC_SET_PARAM_FAIL;
444
0
    }
445
0
    if (concealParams == NULL) {
446
0
      return AAC_DEC_INVALID_HANDLE;
447
0
    } else {
448
0
      concealParams->comfortNoiseLevel = (FIXP_DBL)comfNoiseLevel;
449
0
    }
450
0
  }
451
452
31.0k
  return (AAC_DEC_OK);
453
31.0k
}
454
455
/*!
456
  \brief Set fade-out/in attenuation factor vectors
457
458
  \param concealParams
459
  \param fadeOutAttenuationVector
460
  \param fadeInAttenuationVector
461
462
  \return 0 if OK all other values indicate errors
463
*/
464
AAC_DECODER_ERROR
465
CConcealment_SetAttenuation(CConcealParams *concealParams,
466
                            const SHORT *fadeOutAttenuationVector,
467
0
                            const SHORT *fadeInAttenuationVector) {
468
0
  if ((fadeOutAttenuationVector == NULL) && (fadeInAttenuationVector == NULL)) {
469
0
    return AAC_DEC_SET_PARAM_FAIL;
470
0
  }
471
472
  /* Fade-out factors */
473
0
  if (fadeOutAttenuationVector != NULL) {
474
0
    int i;
475
476
    /* check quantized factors first */
477
0
    for (i = 0; i < CONCEAL_MAX_NUM_FADE_FACTORS; i++) {
478
0
      if ((fadeOutAttenuationVector[i] < 0) ||
479
0
          (fadeOutAttenuationVector[i] > CONCEAL_MAX_QUANT_FACTOR)) {
480
0
        return AAC_DEC_SET_PARAM_FAIL;
481
0
      }
482
0
    }
483
0
    if (concealParams == NULL) {
484
0
      return AAC_DEC_INVALID_HANDLE;
485
0
    }
486
487
    /* now dequantize factors */
488
0
    for (i = 0; i < CONCEAL_MAX_NUM_FADE_FACTORS; i++) {
489
0
      concealParams->fadeOutFactor[i] =
490
0
          FX_DBL2FX_SGL(fLdPow(CONCEAL_MIN_ATTENUATION_FACTOR_025_LD, 0,
491
0
                               (FIXP_DBL)((INT)(FL2FXCONST_DBL(1.0 / 2.0) >>
492
0
                                                (CONCEAL_PARAMETER_BITS - 1)) *
493
0
                                          (INT)fadeOutAttenuationVector[i]),
494
0
                               CONCEAL_PARAMETER_BITS));
495
0
    }
496
0
  }
497
498
  /* Fade-in factors */
499
0
  if (fadeInAttenuationVector != NULL) {
500
0
    int i;
501
502
    /* check quantized factors first */
503
0
    for (i = 0; i < CONCEAL_MAX_NUM_FADE_FACTORS; i++) {
504
0
      if ((fadeInAttenuationVector[i] < 0) ||
505
0
          (fadeInAttenuationVector[i] > CONCEAL_MAX_QUANT_FACTOR)) {
506
0
        return AAC_DEC_SET_PARAM_FAIL;
507
0
      }
508
0
    }
509
0
    if (concealParams == NULL) {
510
0
      return AAC_DEC_INVALID_HANDLE;
511
0
    }
512
513
    /* now dequantize factors */
514
0
    for (i = 0; i < CONCEAL_MAX_NUM_FADE_FACTORS; i++) {
515
0
      concealParams->fadeInFactor[i] = FX_DBL2FX_SGL(
516
0
          fLdPow(CONCEAL_MIN_ATTENUATION_FACTOR_025_LD, 0,
517
0
                 (FIXP_DBL)((INT)(FIXP_ONE >> CONCEAL_PARAMETER_BITS) *
518
0
                            (INT)fadeInAttenuationVector[i]),
519
0
                 CONCEAL_PARAMETER_BITS));
520
0
    }
521
0
  }
522
523
0
  return (AAC_DEC_OK);
524
0
}
525
526
/*!
527
  \brief Get state of concealment module.
528
529
  \param pConcealChannelInfo
530
531
  \return Concealment state.
532
*/
533
0
CConcealmentState CConcealment_GetState(CConcealmentInfo *pConcealChannelInfo) {
534
0
  CConcealmentState state = ConcealState_Ok;
535
536
0
  if (pConcealChannelInfo != NULL) {
537
0
    state = pConcealChannelInfo->concealState;
538
0
  }
539
540
0
  return (state);
541
0
}
542
543
/*!
544
  \brief Store data for concealment techniques applied later
545
546
  Interface function to store data for different concealment strategies
547
 */
548
void CConcealment_Store(
549
    CConcealmentInfo *hConcealmentInfo,
550
    CAacDecoderChannelInfo *pAacDecoderChannelInfo,
551
468k
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo) {
552
468k
  UCHAR nbDiv = NB_DIV;
553
554
468k
  if (!(pAacDecoderChannelInfo->renderMode == AACDEC_RENDER_LPD &&
555
70.4k
        pAacDecoderChannelInfo->data.usac.mod[nbDiv - 1] == 0))
556
557
434k
  {
558
434k
    FIXP_DBL *pSpectralCoefficient =
559
434k
        SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient);
560
434k
    SHORT *pSpecScale = pAacDecoderChannelInfo->specScale;
561
434k
    CIcsInfo *pIcsInfo = &pAacDecoderChannelInfo->icsInfo;
562
563
434k
    SHORT tSpecScale[8];
564
434k
    UCHAR tWindowShape;
565
434k
    BLOCK_TYPE tWindowSequence;
566
567
    /* store old window infos for swapping */
568
434k
    tWindowSequence = hConcealmentInfo->windowSequence;
569
434k
    tWindowShape = hConcealmentInfo->windowShape;
570
571
    /* store old scale factors for swapping */
572
434k
    FDKmemcpy(tSpecScale, hConcealmentInfo->specScale, 8 * sizeof(SHORT));
573
574
    /* store new window infos */
575
434k
    hConcealmentInfo->windowSequence = GetWindowSequence(pIcsInfo);
576
434k
    hConcealmentInfo->windowShape = GetWindowShape(pIcsInfo);
577
434k
    hConcealmentInfo->lastWinGrpLen =
578
434k
        *(GetWindowGroupLengthTable(pIcsInfo) + GetWindowGroups(pIcsInfo) - 1);
579
580
    /* store new scale factors */
581
434k
    FDKmemcpy(hConcealmentInfo->specScale, pSpecScale, 8 * sizeof(SHORT));
582
583
434k
    if (hConcealmentInfo->pConcealParams->method < ConcealMethodInter) {
584
    /* store new spectral bins */
585
434k
#if (CNCL_FRACT_BITS == DFRACT_BITS)
586
434k
      FDKmemcpy(hConcealmentInfo->spectralCoefficient, pSpectralCoefficient,
587
434k
                1024 * sizeof(FIXP_CNCL));
588
#else
589
      FIXP_CNCL *RESTRICT pCncl =
590
          &hConcealmentInfo->spectralCoefficient[1024 - 1];
591
      FIXP_DBL *RESTRICT pSpec = &pSpectralCoefficient[1024 - 1];
592
      int i;
593
      for (i = 1024; i != 0; i--) {
594
        *pCncl-- = FX_DBL2FX_CNCL(*pSpec--);
595
      }
596
#endif
597
434k
    } else {
598
    /* swap spectral data */
599
0
#if (FIXP_CNCL == FIXP_DBL)
600
0
      C_ALLOC_SCRATCH_START(pSpecTmp, FIXP_DBL, 1024);
601
0
      FDKmemcpy(pSpecTmp, pSpectralCoefficient, 1024 * sizeof(FIXP_DBL));
602
0
      FDKmemcpy(pSpectralCoefficient, hConcealmentInfo->spectralCoefficient,
603
0
                1024 * sizeof(FIXP_DBL));
604
0
      FDKmemcpy(hConcealmentInfo->spectralCoefficient, pSpecTmp,
605
0
                1024 * sizeof(FIXP_DBL));
606
0
      C_ALLOC_SCRATCH_END(pSpecTmp, FIXP_DBL, 1024);
607
#else
608
      FIXP_CNCL *RESTRICT pCncl =
609
          &hConcealmentInfo->spectralCoefficient[1024 - 1];
610
      FIXP_DBL *RESTRICT pSpec = &pSpectralCoefficient[1024 - 1];
611
      FIXP_DBL tSpec;
612
613
      for (int i = 1024; i != 0; i--) {
614
        tSpec = *pSpec;
615
        *pSpec-- = FX_CNCL2FX_DBL(*pCncl);
616
        *pCncl-- = FX_DBL2FX_CNCL(tSpec);
617
      }
618
#endif
619
620
      /* complete swapping of window infos */
621
0
      pIcsInfo->WindowSequence = tWindowSequence;
622
0
      pIcsInfo->WindowShape = tWindowShape;
623
624
      /* complete swapping of scale factors */
625
0
      FDKmemcpy(pSpecScale, tSpecScale, 8 * sizeof(SHORT));
626
0
    }
627
434k
  }
628
629
468k
  if (pAacDecoderChannelInfo->renderMode == AACDEC_RENDER_LPD) {
630
    /* Store LSF4 */
631
70.4k
    FDKmemcpy(hConcealmentInfo->lsf4, pAacDecoderStaticChannelInfo->lpc4_lsf,
632
70.4k
              sizeof(hConcealmentInfo->lsf4));
633
    /* Store TCX gain */
634
70.4k
    hConcealmentInfo->last_tcx_gain =
635
70.4k
        pAacDecoderStaticChannelInfo->last_tcx_gain;
636
70.4k
    hConcealmentInfo->last_tcx_gain_e =
637
70.4k
        pAacDecoderStaticChannelInfo->last_tcx_gain_e;
638
70.4k
  }
639
468k
}
640
641
/*!
642
  \brief Apply concealment
643
644
  Interface function to different concealment strategies
645
 */
646
int CConcealment_Apply(
647
    CConcealmentInfo *hConcealmentInfo,
648
    CAacDecoderChannelInfo *pAacDecoderChannelInfo,
649
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
650
    const SamplingRateInfo *pSamplingRateInfo, const int samplesPerFrame,
651
4.08M
    const UCHAR lastLpdMode, const int frameOk, const UINT flags) {
652
4.08M
  int appliedProcessing = 0;
653
4.08M
  const int mute_release_active =
654
4.08M
      frameOk && (hConcealmentInfo->concealState >= ConcealState_Mute) &&
655
302k
      (hConcealmentInfo->cntValidFrames + 1 <=
656
302k
       hConcealmentInfo->pConcealParams->numMuteReleaseFrames);
657
658
4.08M
  if (hConcealmentInfo->windowShape == CONCEAL_NOT_DEFINED) {
659
    /* Initialize window_shape with same value as in the current (parsed) frame.
660
       Because section 4.6.11.3.2 (Windowing and block switching) of ISO/IEC
661
       14496-3:2009 says: For the first raw_data_block() to be decoded the
662
       window_shape of the left and right half of the window are identical. */
663
78.7k
    hConcealmentInfo->windowShape = pAacDecoderChannelInfo->icsInfo.WindowShape;
664
78.7k
  }
665
666
4.08M
  if (frameOk && !mute_release_active) {
667
    /* Update render mode if frameOk except for ongoing mute release state. */
668
468k
    hConcealmentInfo->lastRenderMode =
669
468k
        (SCHAR)pAacDecoderChannelInfo->renderMode;
670
671
    /* Rescue current data for concealment in future frames */
672
468k
    CConcealment_Store(hConcealmentInfo, pAacDecoderChannelInfo,
673
468k
                       pAacDecoderStaticChannelInfo);
674
    /* Reset index to random sign vector to make sign calculation frame agnostic
675
       (only depends on number of subsequently concealed spectral blocks) */
676
468k
    hConcealmentInfo->iRandomPhase = 0;
677
3.61M
  } else {
678
3.61M
    if (hConcealmentInfo->lastRenderMode == AACDEC_RENDER_INVALID) {
679
0
      hConcealmentInfo->lastRenderMode = AACDEC_RENDER_IMDCT;
680
0
    }
681
3.61M
    pAacDecoderChannelInfo->renderMode =
682
3.61M
        (AACDEC_RENDER_MODE)hConcealmentInfo->lastRenderMode;
683
3.61M
  }
684
685
  /* hand current frame status to the state machine */
686
4.08M
  CConcealment_UpdateState(hConcealmentInfo, frameOk,
687
4.08M
                           pAacDecoderStaticChannelInfo, samplesPerFrame,
688
4.08M
                           pAacDecoderChannelInfo);
689
690
4.08M
  {
691
4.08M
    if (!frameOk && pAacDecoderChannelInfo->renderMode == AACDEC_RENDER_IMDCT) {
692
      /* LPC extrapolation */
693
1.51M
      CLpc_Conceal(pAacDecoderChannelInfo->data.usac.lsp_coeff,
694
1.51M
                   pAacDecoderStaticChannelInfo->lpc4_lsf,
695
1.51M
                   pAacDecoderStaticChannelInfo->lsf_adaptive_mean,
696
1.51M
                   hConcealmentInfo->lastRenderMode == AACDEC_RENDER_IMDCT);
697
1.51M
      FDKmemcpy(hConcealmentInfo->lsf4, pAacDecoderStaticChannelInfo->lpc4_lsf,
698
1.51M
                sizeof(pAacDecoderStaticChannelInfo->lpc4_lsf));
699
1.51M
    }
700
701
    /* Create data for signal rendering according to the selected concealment
702
     * method and decoder operating mode. */
703
704
4.08M
    if ((!frameOk || mute_release_active) &&
705
3.61M
        (pAacDecoderChannelInfo->renderMode == AACDEC_RENDER_LPD)) {
706
      /* Restore old LSF4 */
707
502k
      FDKmemcpy(pAacDecoderStaticChannelInfo->lpc4_lsf, hConcealmentInfo->lsf4,
708
502k
                sizeof(pAacDecoderStaticChannelInfo->lpc4_lsf));
709
      /* Restore old TCX gain */
710
502k
      pAacDecoderStaticChannelInfo->last_tcx_gain =
711
502k
          hConcealmentInfo->last_tcx_gain;
712
502k
      pAacDecoderStaticChannelInfo->last_tcx_gain_e =
713
502k
          hConcealmentInfo->last_tcx_gain_e;
714
502k
    }
715
716
4.08M
    if (!(pAacDecoderChannelInfo->renderMode == AACDEC_RENDER_LPD &&
717
3.77M
          pAacDecoderStaticChannelInfo->last_lpd_mode == 0)) {
718
3.77M
      switch (hConcealmentInfo->pConcealParams->method) {
719
0
        default:
720
0
        case ConcealMethodMute:
721
0
          if (!frameOk) {
722
            /* Mute spectral data in case of errors */
723
0
            FDKmemclear(pAacDecoderChannelInfo->pSpectralCoefficient,
724
0
                        samplesPerFrame * sizeof(FIXP_DBL));
725
            /* Set last window shape */
726
0
            pAacDecoderChannelInfo->icsInfo.WindowShape =
727
0
                hConcealmentInfo->windowShape;
728
0
            appliedProcessing = 1;
729
0
          }
730
0
          break;
731
732
3.77M
        case ConcealMethodNoise:
733
          /* Noise substitution error concealment technique */
734
3.77M
          appliedProcessing = CConcealment_ApplyNoise(
735
3.77M
              hConcealmentInfo, pAacDecoderChannelInfo,
736
3.77M
              pAacDecoderStaticChannelInfo, pSamplingRateInfo, samplesPerFrame,
737
3.77M
              flags);
738
3.77M
          break;
739
740
0
        case ConcealMethodInter:
741
          /* Energy interpolation concealment based on 3GPP */
742
0
          appliedProcessing = CConcealment_ApplyInter(
743
0
              hConcealmentInfo, pAacDecoderChannelInfo, pSamplingRateInfo,
744
0
              samplesPerFrame, 0, /* don't use tonal improvement */
745
0
              frameOk, mute_release_active);
746
0
          break;
747
3.77M
      }
748
3.77M
    } else if (!frameOk || mute_release_active) {
749
      /* simply restore the buffer */
750
287k
      FIXP_DBL *pSpectralCoefficient =
751
287k
          SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient);
752
287k
      SHORT *pSpecScale = pAacDecoderChannelInfo->specScale;
753
287k
      CIcsInfo *pIcsInfo = &pAacDecoderChannelInfo->icsInfo;
754
#if (CNCL_FRACT_BITS != DFRACT_BITS)
755
      FIXP_CNCL *RESTRICT pCncl =
756
          &hConcealmentInfo->spectralCoefficient[1024 - 1];
757
      FIXP_DBL *RESTRICT pSpec = &pSpectralCoefficient[1024 - 1];
758
      int i;
759
#endif
760
761
      /* restore window infos (gri) do we need that? */
762
287k
      pIcsInfo->WindowSequence = hConcealmentInfo->windowSequence;
763
287k
      pIcsInfo->WindowShape = hConcealmentInfo->windowShape;
764
765
287k
      if (hConcealmentInfo->concealState != ConcealState_Mute) {
766
        /* restore scale factors */
767
58.4k
        FDKmemcpy(pSpecScale, hConcealmentInfo->specScale, 8 * sizeof(SHORT));
768
769
        /* restore spectral bins */
770
58.4k
#if (CNCL_FRACT_BITS == DFRACT_BITS)
771
58.4k
        FDKmemcpy(pSpectralCoefficient, hConcealmentInfo->spectralCoefficient,
772
58.4k
                  1024 * sizeof(FIXP_DBL));
773
#else
774
        for (i = 1024; i != 0; i--) {
775
          *pSpec-- = FX_CNCL2FX_DBL(*pCncl--);
776
        }
777
#endif
778
228k
      } else {
779
        /* clear scale factors */
780
228k
        FDKmemclear(pSpecScale, 8 * sizeof(SHORT));
781
782
        /* clear buffer */
783
228k
        FDKmemclear(pSpectralCoefficient, 1024 * sizeof(FIXP_CNCL));
784
228k
      }
785
287k
    }
786
4.08M
  }
787
  /* update history */
788
4.08M
  hConcealmentInfo->prevFrameOk[0] = hConcealmentInfo->prevFrameOk[1];
789
4.08M
  hConcealmentInfo->prevFrameOk[1] = frameOk;
790
791
4.08M
  return mute_release_active ? -1 : appliedProcessing;
792
4.08M
}
793
794
/*!
795
\brief Apply concealment noise substitution
796
797
  In case of frame lost this function produces a noisy frame with respect to the
798
  energies values of past frame.
799
 */
800
static int CConcealment_ApplyNoise(
801
    CConcealmentInfo *pConcealmentInfo,
802
    CAacDecoderChannelInfo *pAacDecoderChannelInfo,
803
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
804
    const SamplingRateInfo *pSamplingRateInfo, const int samplesPerFrame,
805
3.77M
    const UINT flags) {
806
3.77M
  FIXP_DBL *pSpectralCoefficient =
807
3.77M
      SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient);
808
3.77M
  CIcsInfo *pIcsInfo = &pAacDecoderChannelInfo->icsInfo;
809
810
3.77M
  int appliedProcessing = 0;
811
812
3.77M
  FDK_ASSERT(pConcealmentInfo != NULL);
813
3.77M
  FDK_ASSERT((samplesPerFrame >= 120) && (samplesPerFrame <= 1024));
814
815
3.77M
  switch (pConcealmentInfo->concealState) {
816
132k
    case ConcealState_Ok:
817
      /* Nothing to do here! */
818
132k
      break;
819
820
79.4k
    case ConcealState_Single:
821
574k
    case ConcealState_FadeOut:
822
574k
      appliedProcessing = CConcealment_ApplyFadeOut(
823
574k
          /*mode =*/1, pConcealmentInfo, pAacDecoderStaticChannelInfo,
824
574k
          samplesPerFrame, pAacDecoderChannelInfo);
825
574k
      break;
826
827
2.75M
    case ConcealState_Mute: {
828
      /* set dummy window parameters */
829
2.75M
      pIcsInfo->Valid = 0; /* Trigger the generation of a consitent IcsInfo */
830
2.75M
      pIcsInfo->WindowShape =
831
2.75M
          pConcealmentInfo->windowShape; /* Prevent an invalid WindowShape
832
                                            (required for F/T transform) */
833
2.75M
      pIcsInfo->WindowSequence =
834
2.75M
          CConcealment_GetWinSeq(pConcealmentInfo->windowSequence);
835
2.75M
      pConcealmentInfo->windowSequence =
836
2.75M
          pIcsInfo->WindowSequence; /* Store for next frame
837
                                       (spectrum in concealment
838
                                       buffer can't be used at
839
                                       all) */
840
841
      /* mute spectral data */
842
2.75M
      FDKmemclear(pSpectralCoefficient, samplesPerFrame * sizeof(FIXP_DBL));
843
2.75M
      FDKmemclear(pConcealmentInfo->spectralCoefficient,
844
2.75M
                  samplesPerFrame * sizeof(FIXP_DBL));
845
846
2.75M
      appliedProcessing = 1;
847
2.75M
    } break;
848
849
317k
    case ConcealState_FadeIn: {
850
      /* TimeDomainFading:                                        */
851
      /* Attenuation of signal is done in CConcealment_TDFading() */
852
853
317k
      appliedProcessing = 1;
854
317k
    } break;
855
856
0
    default:
857
      /* we shouldn't come here anyway */
858
0
      FDK_ASSERT(0);
859
0
      break;
860
3.77M
  }
861
862
3.77M
  return appliedProcessing;
863
3.77M
}
864
865
/*!
866
  \brief Apply concealment interpolation
867
868
  The function swaps the data from the current and the previous frame. If an
869
  error has occured, frame interpolation is performed to restore the missing
870
  frame. In case of multiple faulty frames, fade-in and fade-out is applied.
871
*/
872
static int CConcealment_ApplyInter(
873
    CConcealmentInfo *pConcealmentInfo,
874
    CAacDecoderChannelInfo *pAacDecoderChannelInfo,
875
    const SamplingRateInfo *pSamplingRateInfo, const int samplesPerFrame,
876
0
    const int improveTonal, const int frameOk, const int mute_release_active) {
877
0
#if defined(FDK_ASSERT_ENABLE)
878
0
  CConcealParams *pConcealCommonData = pConcealmentInfo->pConcealParams;
879
0
#endif
880
881
0
  FIXP_DBL *pSpectralCoefficient =
882
0
      SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient);
883
0
  CIcsInfo *pIcsInfo = &pAacDecoderChannelInfo->icsInfo;
884
0
  SHORT *pSpecScale = pAacDecoderChannelInfo->specScale;
885
886
0
  int sfbEnergyPrev[64];
887
0
  int sfbEnergyAct[64];
888
889
0
  int i, appliedProcessing = 0;
890
891
  /* clear/init */
892
0
  FDKmemclear(sfbEnergyPrev, 64 * sizeof(int));
893
0
  FDKmemclear(sfbEnergyAct, 64 * sizeof(int));
894
895
0
  if (!frameOk || mute_release_active) {
896
    /* Restore last frame from concealment buffer */
897
0
    pIcsInfo->WindowShape = pConcealmentInfo->windowShape;
898
0
    pIcsInfo->WindowSequence = pConcealmentInfo->windowSequence;
899
900
    /* Restore spectral data */
901
0
    for (i = 0; i < samplesPerFrame; i++) {
902
0
      pSpectralCoefficient[i] =
903
0
          FX_CNCL2FX_DBL(pConcealmentInfo->spectralCoefficient[i]);
904
0
    }
905
906
    /* Restore scale factors */
907
0
    FDKmemcpy(pSpecScale, pConcealmentInfo->specScale, 8 * sizeof(SHORT));
908
0
  }
909
910
  /* if previous frame was not ok */
911
0
  if (!pConcealmentInfo->prevFrameOk[1] || mute_release_active) {
912
    /* if current frame (f_n) is ok and the last but one frame (f_(n-2))
913
       was ok, too, then interpolate both frames in order to generate
914
       the current output frame (f_(n-1)). Otherwise, use the last stored
915
       frame (f_(n-2) or f_(n-3) or ...). */
916
0
    if (frameOk && pConcealmentInfo->prevFrameOk[0] && !mute_release_active) {
917
0
      appliedProcessing = 1;
918
919
      /* Interpolate both frames in order to generate the current output frame
920
       * (f_(n-1)). */
921
0
      if (pIcsInfo->WindowSequence == BLOCK_SHORT) {
922
        /* f_(n-2) == BLOCK_SHORT */
923
        /* short--??????--short, short--??????--long interpolation */
924
        /* short--short---short, short---long---long interpolation */
925
926
0
        int wnd;
927
928
0
        if (pConcealmentInfo->windowSequence ==
929
0
            BLOCK_SHORT) { /* f_n == BLOCK_SHORT */
930
          /* short--short---short interpolation */
931
932
0
          int scaleFactorBandsTotal =
933
0
              pSamplingRateInfo->NumberOfScaleFactorBands_Short;
934
0
          const SHORT *pSfbOffset = pSamplingRateInfo->ScaleFactorBands_Short;
935
0
          pIcsInfo->WindowShape = (samplesPerFrame <= 512) ? 2 : 1;
936
0
          pIcsInfo->WindowSequence = BLOCK_SHORT;
937
938
0
          for (wnd = 0; wnd < 8; wnd++) {
939
0
            CConcealment_CalcBandEnergy(
940
0
                &pSpectralCoefficient[wnd *
941
0
                                      (samplesPerFrame / 8)], /* spec_(n-2) */
942
0
                pSamplingRateInfo, BLOCK_SHORT, CConcealment_NoExpand,
943
0
                sfbEnergyPrev);
944
945
0
            CConcealment_CalcBandEnergy(
946
0
                &pConcealmentInfo->spectralCoefficient[wnd * (samplesPerFrame /
947
0
                                                              8)], /* spec_n */
948
0
                pSamplingRateInfo, BLOCK_SHORT, CConcealment_NoExpand,
949
0
                sfbEnergyAct);
950
951
0
            CConcealment_InterpolateBuffer(
952
0
                &pSpectralCoefficient[wnd *
953
0
                                      (samplesPerFrame / 8)], /* spec_(n-1) */
954
0
                &pSpecScale[wnd], &pConcealmentInfo->specScale[wnd],
955
0
                &pSpecScale[wnd], sfbEnergyPrev, sfbEnergyAct,
956
0
                scaleFactorBandsTotal, pSfbOffset);
957
0
          }
958
0
        } else { /* f_n != BLOCK_SHORT */
959
          /* short---long---long interpolation */
960
961
0
          int scaleFactorBandsTotal =
962
0
              pSamplingRateInfo->NumberOfScaleFactorBands_Long;
963
0
          const SHORT *pSfbOffset = pSamplingRateInfo->ScaleFactorBands_Long;
964
0
          SHORT specScaleOut;
965
966
0
          CConcealment_CalcBandEnergy(
967
0
              &pSpectralCoefficient[samplesPerFrame -
968
0
                                    (samplesPerFrame /
969
0
                                     8)], /* [wnd] spec_(n-2) */
970
0
              pSamplingRateInfo, BLOCK_SHORT, CConcealment_Expand,
971
0
              sfbEnergyAct);
972
973
0
          CConcealment_CalcBandEnergy(
974
0
              pConcealmentInfo->spectralCoefficient, /* spec_n */
975
0
              pSamplingRateInfo, BLOCK_LONG, CConcealment_NoExpand,
976
0
              sfbEnergyPrev);
977
978
0
          pIcsInfo->WindowShape = 0;
979
0
          pIcsInfo->WindowSequence = BLOCK_STOP;
980
981
0
          for (i = 0; i < samplesPerFrame; i++) {
982
0
            pSpectralCoefficient[i] =
983
0
                pConcealmentInfo->spectralCoefficient[i]; /* spec_n */
984
0
          }
985
986
0
          for (i = 0; i < 8; i++) { /* search for max(specScale) */
987
0
            if (pSpecScale[i] > pSpecScale[0]) {
988
0
              pSpecScale[0] = pSpecScale[i];
989
0
            }
990
0
          }
991
992
0
          CConcealment_InterpolateBuffer(
993
0
              pSpectralCoefficient, /* spec_(n-1) */
994
0
              &pConcealmentInfo->specScale[0], &pSpecScale[0], &specScaleOut,
995
0
              sfbEnergyPrev, sfbEnergyAct, scaleFactorBandsTotal, pSfbOffset);
996
997
0
          pSpecScale[0] = specScaleOut;
998
0
        }
999
0
      } else {
1000
        /* long--??????--short, long--??????--long interpolation */
1001
        /* long---long---short, long---long---long interpolation */
1002
1003
0
        int scaleFactorBandsTotal =
1004
0
            pSamplingRateInfo->NumberOfScaleFactorBands_Long;
1005
0
        const SHORT *pSfbOffset = pSamplingRateInfo->ScaleFactorBands_Long;
1006
0
        SHORT specScaleAct = pConcealmentInfo->specScale[0];
1007
1008
0
        CConcealment_CalcBandEnergy(pSpectralCoefficient, /* spec_(n-2) */
1009
0
                                    pSamplingRateInfo, BLOCK_LONG,
1010
0
                                    CConcealment_NoExpand, sfbEnergyPrev);
1011
1012
0
        if (pConcealmentInfo->windowSequence ==
1013
0
            BLOCK_SHORT) { /* f_n == BLOCK_SHORT */
1014
          /* long---long---short interpolation */
1015
1016
0
          pIcsInfo->WindowShape = (samplesPerFrame <= 512) ? 2 : 1;
1017
0
          pIcsInfo->WindowSequence = BLOCK_START;
1018
1019
0
          for (i = 1; i < 8; i++) { /* search for max(specScale) */
1020
0
            if (pConcealmentInfo->specScale[i] > specScaleAct) {
1021
0
              specScaleAct = pConcealmentInfo->specScale[i];
1022
0
            }
1023
0
          }
1024
1025
          /* Expand first short spectrum */
1026
0
          CConcealment_CalcBandEnergy(
1027
0
              pConcealmentInfo->spectralCoefficient,               /* spec_n */
1028
0
              pSamplingRateInfo, BLOCK_SHORT, CConcealment_Expand, /* !!! */
1029
0
              sfbEnergyAct);
1030
0
        } else {
1031
          /* long---long---long interpolation */
1032
1033
0
          pIcsInfo->WindowShape = 0;
1034
0
          pIcsInfo->WindowSequence = BLOCK_LONG;
1035
1036
0
          CConcealment_CalcBandEnergy(
1037
0
              pConcealmentInfo->spectralCoefficient, /* spec_n */
1038
0
              pSamplingRateInfo, BLOCK_LONG, CConcealment_NoExpand,
1039
0
              sfbEnergyAct);
1040
0
        }
1041
1042
0
        CConcealment_InterpolateBuffer(
1043
0
            pSpectralCoefficient, /* spec_(n-1) */
1044
0
            &pSpecScale[0], &specScaleAct, &pSpecScale[0], sfbEnergyPrev,
1045
0
            sfbEnergyAct, scaleFactorBandsTotal, pSfbOffset);
1046
0
      }
1047
0
    }
1048
1049
    /* Noise substitution of sign of the output spectral coefficients */
1050
0
    CConcealment_ApplyRandomSign(pConcealmentInfo->iRandomPhase,
1051
0
                                 pSpectralCoefficient, samplesPerFrame);
1052
    /* Increment random phase index to avoid repetition artifacts. */
1053
0
    pConcealmentInfo->iRandomPhase =
1054
0
        (pConcealmentInfo->iRandomPhase + 1) & (AAC_NF_NO_RANDOM_VAL - 1);
1055
0
  }
1056
1057
  /* scale spectrum according to concealment state */
1058
0
  switch (pConcealmentInfo->concealState) {
1059
0
    case ConcealState_Single:
1060
0
      appliedProcessing = 1;
1061
0
      break;
1062
1063
0
    case ConcealState_FadeOut: {
1064
0
      FDK_ASSERT(pConcealmentInfo->cntFadeFrames >= 0);
1065
0
      FDK_ASSERT(pConcealmentInfo->cntFadeFrames <
1066
0
                 CONCEAL_MAX_NUM_FADE_FACTORS);
1067
0
      FDK_ASSERT(pConcealmentInfo->cntFadeFrames <
1068
0
                 pConcealCommonData->numFadeOutFrames);
1069
1070
      /* TimeDomainFading:                                        */
1071
      /* Attenuation of signal is done in CConcealment_TDFading() */
1072
1073
0
      appliedProcessing = 1;
1074
0
    } break;
1075
1076
0
    case ConcealState_FadeIn: {
1077
0
      FDK_ASSERT(pConcealmentInfo->cntFadeFrames >= 0);
1078
0
      FDK_ASSERT(pConcealmentInfo->cntFadeFrames <
1079
0
                 CONCEAL_MAX_NUM_FADE_FACTORS);
1080
0
      FDK_ASSERT(pConcealmentInfo->cntFadeFrames <
1081
0
                 pConcealCommonData->numFadeInFrames);
1082
1083
      /* TimeDomainFading:                                        */
1084
      /* Attenuation of signal is done in CConcealment_TDFading() */
1085
1086
0
      appliedProcessing = 1;
1087
0
    } break;
1088
1089
0
    case ConcealState_Mute: {
1090
      /* set dummy window parameters */
1091
0
      pIcsInfo->Valid = 0; /* Trigger the generation of a consitent IcsInfo */
1092
0
      pIcsInfo->WindowShape =
1093
0
          pConcealmentInfo->windowShape; /* Prevent an invalid WindowShape
1094
                                            (required for F/T transform) */
1095
0
      pIcsInfo->WindowSequence =
1096
0
          CConcealment_GetWinSeq(pConcealmentInfo->windowSequence);
1097
0
      pConcealmentInfo->windowSequence =
1098
0
          pIcsInfo->WindowSequence; /* Store for next frame
1099
                                       (spectrum in concealment
1100
                                       buffer can't be used at
1101
                                       all) */
1102
1103
      /* mute spectral data */
1104
0
      FDKmemclear(pSpectralCoefficient, samplesPerFrame * sizeof(FIXP_DBL));
1105
1106
0
      appliedProcessing = 1;
1107
0
    } break;
1108
1109
0
    default:
1110
      /* nothing to do here */
1111
0
      break;
1112
0
  }
1113
1114
0
  return appliedProcessing;
1115
0
}
1116
1117
/*!
1118
  \brief Calculate the spectral energy
1119
1120
  The function calculates band-wise the spectral energy. This is used for
1121
  frame interpolation.
1122
*/
1123
static void CConcealment_CalcBandEnergy(
1124
    FIXP_DBL *spectrum, const SamplingRateInfo *pSamplingRateInfo,
1125
0
    const int blockType, CConcealmentExpandType expandType, int *sfbEnergy) {
1126
0
  const SHORT *pSfbOffset;
1127
0
  int line, sfb, scaleFactorBandsTotal = 0;
1128
1129
  /* In the following calculations, enAccu is initialized with LSB-value in
1130
   * order to avoid zero energy-level */
1131
1132
0
  line = 0;
1133
1134
0
  switch (blockType) {
1135
0
    case BLOCK_LONG:
1136
0
    case BLOCK_START:
1137
0
    case BLOCK_STOP:
1138
1139
0
      if (expandType == CConcealment_NoExpand) {
1140
        /* standard long calculation */
1141
0
        scaleFactorBandsTotal =
1142
0
            pSamplingRateInfo->NumberOfScaleFactorBands_Long;
1143
0
        pSfbOffset = pSamplingRateInfo->ScaleFactorBands_Long;
1144
1145
0
        for (sfb = 0; sfb < scaleFactorBandsTotal; sfb++) {
1146
0
          FIXP_DBL enAccu = (FIXP_DBL)(LONG)1;
1147
0
          int sfbScale =
1148
0
              (sizeof(LONG) << 3) -
1149
0
              CntLeadingZeros(pSfbOffset[sfb + 1] - pSfbOffset[sfb]) - 1;
1150
          /* scaling depends on sfb width. */
1151
0
          for (; line < pSfbOffset[sfb + 1]; line++) {
1152
0
            enAccu += fPow2Div2(*(spectrum + line)) >> sfbScale;
1153
0
          }
1154
0
          *(sfbEnergy + sfb) = CntLeadingZeros(enAccu) - 1;
1155
0
        }
1156
0
      } else {
1157
        /* compress long to short */
1158
0
        scaleFactorBandsTotal =
1159
0
            pSamplingRateInfo->NumberOfScaleFactorBands_Short;
1160
0
        pSfbOffset = pSamplingRateInfo->ScaleFactorBands_Short;
1161
1162
0
        for (sfb = 0; sfb < scaleFactorBandsTotal; sfb++) {
1163
0
          FIXP_DBL enAccu = (FIXP_DBL)(LONG)1;
1164
0
          int sfbScale =
1165
0
              (sizeof(LONG) << 3) -
1166
0
              CntLeadingZeros(pSfbOffset[sfb + 1] - pSfbOffset[sfb]) - 1;
1167
          /* scaling depends on sfb width. */
1168
0
          for (; line < pSfbOffset[sfb + 1] << 3; line++) {
1169
0
            enAccu +=
1170
0
                (enAccu + (fPow2Div2(*(spectrum + line)) >> sfbScale)) >> 3;
1171
0
          }
1172
0
          *(sfbEnergy + sfb) = CntLeadingZeros(enAccu) - 1;
1173
0
        }
1174
0
      }
1175
0
      break;
1176
1177
0
    case BLOCK_SHORT:
1178
1179
0
      if (expandType == CConcealment_NoExpand) {
1180
        /*   standard short calculation */
1181
0
        scaleFactorBandsTotal =
1182
0
            pSamplingRateInfo->NumberOfScaleFactorBands_Short;
1183
0
        pSfbOffset = pSamplingRateInfo->ScaleFactorBands_Short;
1184
1185
0
        for (sfb = 0; sfb < scaleFactorBandsTotal; sfb++) {
1186
0
          FIXP_DBL enAccu = (FIXP_DBL)(LONG)1;
1187
0
          int sfbScale =
1188
0
              (sizeof(LONG) << 3) -
1189
0
              CntLeadingZeros(pSfbOffset[sfb + 1] - pSfbOffset[sfb]) - 1;
1190
          /* scaling depends on sfb width. */
1191
0
          for (; line < pSfbOffset[sfb + 1]; line++) {
1192
0
            enAccu += fPow2Div2(*(spectrum + line)) >> sfbScale;
1193
0
          }
1194
0
          *(sfbEnergy + sfb) = CntLeadingZeros(enAccu) - 1;
1195
0
        }
1196
0
      } else {
1197
        /*  expand short to long spectrum */
1198
0
        scaleFactorBandsTotal =
1199
0
            pSamplingRateInfo->NumberOfScaleFactorBands_Long;
1200
0
        pSfbOffset = pSamplingRateInfo->ScaleFactorBands_Long;
1201
1202
0
        for (sfb = 0; sfb < scaleFactorBandsTotal; sfb++) {
1203
0
          FIXP_DBL enAccu = (FIXP_DBL)(LONG)1;
1204
0
          int sfbScale =
1205
0
              (sizeof(LONG) << 3) -
1206
0
              CntLeadingZeros(pSfbOffset[sfb + 1] - pSfbOffset[sfb]) - 1;
1207
          /* scaling depends on sfb width. */
1208
0
          for (; line < pSfbOffset[sfb + 1]; line++) {
1209
0
            enAccu += fPow2Div2(*(spectrum + (line >> 3))) >> sfbScale;
1210
0
          }
1211
0
          *(sfbEnergy + sfb) = CntLeadingZeros(enAccu) - 1;
1212
0
        }
1213
0
      }
1214
0
      break;
1215
0
  }
1216
0
}
1217
1218
/*!
1219
  \brief Interpolate buffer
1220
1221
  The function creates the interpolated spectral data according to the
1222
  energy of the last good frame and the current (good) frame.
1223
*/
1224
static void CConcealment_InterpolateBuffer(FIXP_DBL *spectrum,
1225
                                           SHORT *pSpecScalePrv,
1226
                                           SHORT *pSpecScaleAct,
1227
                                           SHORT *pSpecScaleOut, int *enPrv,
1228
                                           int *enAct, int sfbCnt,
1229
0
                                           const SHORT *pSfbOffset) {
1230
0
  int sfb, line = 0;
1231
0
  int fac_shift;
1232
0
  int fac_mod;
1233
1234
0
  for (sfb = 0; sfb < sfbCnt; sfb++) {
1235
0
    fac_shift =
1236
0
        enPrv[sfb] - enAct[sfb] + ((*pSpecScaleAct - *pSpecScalePrv) << 1);
1237
0
    fac_mod = fac_shift & 3;
1238
0
    fac_shift = (fac_shift >> 2) + 1;
1239
0
    fac_shift += *pSpecScalePrv - fixMax(*pSpecScalePrv, *pSpecScaleAct);
1240
0
    fac_shift = fMax(fMin(fac_shift, DFRACT_BITS - 1), -(DFRACT_BITS - 1));
1241
1242
0
    for (; line < pSfbOffset[sfb + 1]; line++) {
1243
0
      FIXP_DBL accu = fMult(*(spectrum + line), facMod4Table[fac_mod]);
1244
0
      *(spectrum + line) = scaleValue(accu, fac_shift);
1245
0
    }
1246
0
  }
1247
0
  *pSpecScaleOut = fixMax(*pSpecScalePrv, *pSpecScaleAct);
1248
0
}
1249
1250
/*!
1251
  \brief Find next fading frame in case of changing fading direction
1252
1253
  \param pConcealCommonData Pointer to the concealment common data structure.
1254
  \param actFadeIndex Last index used for fading
1255
  \param direction Direction of change: 0 : change from FADE-OUT to FADE-IN,  1
1256
  : change from FADE-IN to FADE-OUT
1257
1258
  This function determines the next fading index to be used for the fading
1259
  direction to be changed to.
1260
*/
1261
1262
static INT findEquiFadeFrame(CConcealParams *pConcealCommonData,
1263
438k
                             INT actFadeIndex, int direction) {
1264
438k
  FIXP_SGL *pFactor;
1265
438k
  FIXP_SGL referenceVal;
1266
438k
  FIXP_SGL minDiff = (FIXP_SGL)MAXVAL_SGL;
1267
1268
438k
  INT nextFadeIndex = 0;
1269
1270
438k
  int i;
1271
1272
  /* init depending on direction */
1273
438k
  if (direction == 0) { /* FADE-OUT => FADE-IN */
1274
145k
    if (actFadeIndex < 0) {
1275
0
      referenceVal = (FIXP_SGL)MAXVAL_SGL;
1276
145k
    } else {
1277
145k
      referenceVal = pConcealCommonData->fadeOutFactor[actFadeIndex] >> 1;
1278
145k
    }
1279
145k
    pFactor = pConcealCommonData->fadeInFactor;
1280
292k
  } else { /* FADE-IN => FADE-OUT */
1281
292k
    if (actFadeIndex < 0) {
1282
0
      referenceVal = (FIXP_SGL)MAXVAL_SGL;
1283
292k
    } else {
1284
292k
      referenceVal = pConcealCommonData->fadeInFactor[actFadeIndex] >> 1;
1285
292k
    }
1286
292k
    pFactor = pConcealCommonData->fadeOutFactor;
1287
292k
  }
1288
1289
  /* search for minimum difference */
1290
14.4M
  for (i = 0; i < CONCEAL_MAX_NUM_FADE_FACTORS; i++) {
1291
14.0M
    FIXP_SGL diff = fixp_abs((pFactor[i] >> 1) - referenceVal);
1292
14.0M
    if (diff < minDiff) {
1293
2.23M
      minDiff = diff;
1294
2.23M
      nextFadeIndex = i;
1295
2.23M
    }
1296
14.0M
  }
1297
1298
  /* check and adjust depending on direction */
1299
438k
  if (direction == 0) { /* FADE-OUT => FADE-IN */
1300
145k
    if (nextFadeIndex > pConcealCommonData->numFadeInFrames) {
1301
17.2k
      nextFadeIndex = fMax(pConcealCommonData->numFadeInFrames - 1, 0);
1302
17.2k
    }
1303
145k
    if (((pFactor[nextFadeIndex] >> 1) <= referenceVal) &&
1304
128k
        (nextFadeIndex > 0)) {
1305
128k
      nextFadeIndex -= 1;
1306
128k
    }
1307
292k
  } else { /* FADE-IN => FADE-OUT */
1308
292k
    if (((pFactor[nextFadeIndex] >> 1) >= referenceVal) &&
1309
292k
        (nextFadeIndex < CONCEAL_MAX_NUM_FADE_FACTORS - 1)) {
1310
292k
      nextFadeIndex += 1;
1311
292k
    }
1312
292k
  }
1313
1314
438k
  return (nextFadeIndex);
1315
438k
}
1316
1317
/*!
1318
  \brief Update the concealment state
1319
1320
  The function updates the state of the concealment state-machine. The
1321
  states are: mute, fade-in, fade-out, interpolate and frame-ok.
1322
*/
1323
static void CConcealment_UpdateState(
1324
    CConcealmentInfo *pConcealmentInfo, int frameOk,
1325
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
1326
4.08M
    const int samplesPerFrame, CAacDecoderChannelInfo *pAacDecoderChannelInfo) {
1327
4.08M
  CConcealParams *pConcealCommonData = pConcealmentInfo->pConcealParams;
1328
1329
4.08M
  switch (pConcealCommonData->method) {
1330
4.08M
    case ConcealMethodNoise: {
1331
4.08M
      if (pConcealmentInfo->concealState != ConcealState_Ok) {
1332
        /* count the valid frames during concealment process */
1333
3.89M
        if (frameOk) {
1334
357k
          pConcealmentInfo->cntValidFrames += 1;
1335
3.53M
        } else {
1336
3.53M
          pConcealmentInfo->cntValidFrames = 0;
1337
3.53M
        }
1338
3.89M
      }
1339
1340
      /* -- STATE MACHINE for Noise Substitution -- */
1341
4.08M
      switch (pConcealmentInfo->concealState) {
1342
191k
        case ConcealState_Ok:
1343
191k
          if (!frameOk) {
1344
80.2k
            pConcealmentInfo->cntFadeFrames = 0;
1345
80.2k
            pConcealmentInfo->cntValidFrames = 0;
1346
80.2k
            pConcealmentInfo->attGrpOffset[0] = 0;
1347
80.2k
            pConcealmentInfo->attGrpOffset[1] = 0;
1348
80.2k
            pConcealmentInfo->winGrpOffset[0] = 0;
1349
80.2k
            pConcealmentInfo->winGrpOffset[1] = 0;
1350
80.2k
            if (pConcealCommonData->numFadeOutFrames > 0) {
1351
              /* change to state SINGLE-FRAME-LOSS */
1352
80.2k
              pConcealmentInfo->concealState = ConcealState_Single;
1353
              /* mode 0 just updates the Fading counter */
1354
80.2k
              CConcealment_ApplyFadeOut(
1355
80.2k
                  /*mode =*/0, pConcealmentInfo, pAacDecoderStaticChannelInfo,
1356
80.2k
                  samplesPerFrame, pAacDecoderChannelInfo);
1357
1358
80.2k
            } else {
1359
              /* change to state MUTE */
1360
0
              pConcealmentInfo->concealState = ConcealState_Mute;
1361
0
            }
1362
80.2k
          }
1363
191k
          break;
1364
1365
54.2k
        case ConcealState_Single: /* Just a pre-stage before fade-out begins.
1366
                                     Stay here only one frame! */
1367
54.2k
          if (frameOk) {
1368
            /* change to state OK */
1369
19.3k
            pConcealmentInfo->concealState = ConcealState_Ok;
1370
34.9k
          } else {
1371
34.9k
            if (pConcealmentInfo->cntFadeFrames >=
1372
34.9k
                pConcealCommonData->numFadeOutFrames) {
1373
              /* change to state MUTE */
1374
0
              pConcealmentInfo->concealState = ConcealState_Mute;
1375
34.9k
            } else {
1376
              /* change to state FADE-OUT */
1377
34.9k
              pConcealmentInfo->concealState = ConcealState_FadeOut;
1378
              /* mode 0 just updates the Fading counter */
1379
34.9k
              CConcealment_ApplyFadeOut(
1380
34.9k
                  /*mode =*/0, pConcealmentInfo, pAacDecoderStaticChannelInfo,
1381
34.9k
                  samplesPerFrame, pAacDecoderChannelInfo);
1382
34.9k
            }
1383
34.9k
          }
1384
54.2k
          break;
1385
1386
531k
        case ConcealState_FadeOut:
1387
531k
          if (pConcealmentInfo->cntValidFrames >
1388
531k
              pConcealCommonData->numMuteReleaseFrames) {
1389
145k
            if (pConcealCommonData->numFadeInFrames > 0) {
1390
              /* change to state FADE-IN */
1391
145k
              pConcealmentInfo->concealState = ConcealState_FadeIn;
1392
145k
              pConcealmentInfo->cntFadeFrames = findEquiFadeFrame(
1393
145k
                  pConcealCommonData, pConcealmentInfo->cntFadeFrames,
1394
145k
                  0 /* FadeOut -> FadeIn */);
1395
145k
            } else {
1396
              /* change to state OK */
1397
0
              pConcealmentInfo->concealState = ConcealState_Ok;
1398
0
            }
1399
385k
          } else {
1400
385k
            if (frameOk) {
1401
              /* we have good frame information but stay fully in concealment -
1402
               * reset winGrpOffset/attGrpOffset */
1403
0
              pConcealmentInfo->winGrpOffset[0] = 0;
1404
0
              pConcealmentInfo->winGrpOffset[1] = 0;
1405
0
              pConcealmentInfo->attGrpOffset[0] = 0;
1406
0
              pConcealmentInfo->attGrpOffset[1] = 0;
1407
0
            }
1408
385k
            if (pConcealmentInfo->cntFadeFrames >=
1409
385k
                pConcealCommonData->numFadeOutFrames) {
1410
              /* change to state MUTE */
1411
160k
              pConcealmentInfo->concealState = ConcealState_Mute;
1412
160k
            } else /* Stay in FADE-OUT */
1413
224k
            {
1414
              /* mode 0 just updates the Fading counter */
1415
224k
              CConcealment_ApplyFadeOut(
1416
224k
                  /*mode =*/0, pConcealmentInfo, pAacDecoderStaticChannelInfo,
1417
224k
                  samplesPerFrame, pAacDecoderChannelInfo);
1418
224k
            }
1419
385k
          }
1420
531k
          break;
1421
1422
2.97M
        case ConcealState_Mute:
1423
2.97M
          if (pConcealmentInfo->cntValidFrames >
1424
2.97M
              pConcealCommonData->numMuteReleaseFrames) {
1425
156k
            if (pConcealCommonData->numFadeInFrames > 0) {
1426
              /* change to state FADE-IN */
1427
156k
              pConcealmentInfo->concealState = ConcealState_FadeIn;
1428
156k
              pConcealmentInfo->cntFadeFrames =
1429
156k
                  pConcealCommonData->numFadeInFrames - 1;
1430
156k
            } else {
1431
              /* change to state OK */
1432
0
              pConcealmentInfo->concealState = ConcealState_Ok;
1433
0
            }
1434
2.82M
          } else {
1435
2.82M
            if (frameOk) {
1436
              /* we have good frame information but stay fully in concealment -
1437
               * reset winGrpOffset/attGrpOffset */
1438
0
              pConcealmentInfo->winGrpOffset[0] = 0;
1439
0
              pConcealmentInfo->winGrpOffset[1] = 0;
1440
0
              pConcealmentInfo->attGrpOffset[0] = 0;
1441
0
              pConcealmentInfo->attGrpOffset[1] = 0;
1442
0
            }
1443
2.82M
          }
1444
2.97M
          break;
1445
1446
328k
        case ConcealState_FadeIn:
1447
328k
          pConcealmentInfo->cntFadeFrames -= 1;
1448
328k
          if (frameOk) {
1449
35.8k
            if (pConcealmentInfo->cntFadeFrames < 0) {
1450
              /* change to state OK */
1451
1.94k
              pConcealmentInfo->concealState = ConcealState_Ok;
1452
1.94k
            }
1453
292k
          } else {
1454
292k
            if (pConcealCommonData->numFadeOutFrames > 0) {
1455
              /* change to state FADE-OUT */
1456
292k
              pConcealmentInfo->concealState = ConcealState_FadeOut;
1457
292k
              pConcealmentInfo->cntFadeFrames = findEquiFadeFrame(
1458
292k
                  pConcealCommonData, pConcealmentInfo->cntFadeFrames + 1,
1459
292k
                  1 /* FadeIn -> FadeOut */);
1460
292k
              pConcealmentInfo->winGrpOffset[0] = 0;
1461
292k
              pConcealmentInfo->winGrpOffset[1] = 0;
1462
292k
              pConcealmentInfo->attGrpOffset[0] = 0;
1463
292k
              pConcealmentInfo->attGrpOffset[1] = 0;
1464
1465
292k
              pConcealmentInfo
1466
292k
                  ->cntFadeFrames--; /* decrease because
1467
                                        CConcealment_ApplyFadeOut() will
1468
                                        increase, accordingly */
1469
              /* mode 0 just updates the Fading counter */
1470
292k
              CConcealment_ApplyFadeOut(
1471
292k
                  /*mode =*/0, pConcealmentInfo, pAacDecoderStaticChannelInfo,
1472
292k
                  samplesPerFrame, pAacDecoderChannelInfo);
1473
292k
            } else {
1474
              /* change to state MUTE */
1475
0
              pConcealmentInfo->concealState = ConcealState_Mute;
1476
0
            }
1477
292k
          }
1478
328k
          break;
1479
1480
0
        default:
1481
0
          FDK_ASSERT(0);
1482
0
          break;
1483
4.08M
      }
1484
4.08M
    } break;
1485
1486
4.08M
    case ConcealMethodInter:
1487
0
    case ConcealMethodTonal: {
1488
0
      if (pConcealmentInfo->concealState != ConcealState_Ok) {
1489
        /* count the valid frames during concealment process */
1490
0
        if (pConcealmentInfo->prevFrameOk[1] ||
1491
0
            (pConcealmentInfo->prevFrameOk[0] &&
1492
0
             !pConcealmentInfo->prevFrameOk[1] && frameOk)) {
1493
          /* The frame is OK even if it can be estimated by the energy
1494
           * interpolation algorithm */
1495
0
          pConcealmentInfo->cntValidFrames += 1;
1496
0
        } else {
1497
0
          pConcealmentInfo->cntValidFrames = 0;
1498
0
        }
1499
0
      }
1500
1501
      /* -- STATE MACHINE for energy interpolation -- */
1502
0
      switch (pConcealmentInfo->concealState) {
1503
0
        case ConcealState_Ok:
1504
0
          if (!(pConcealmentInfo->prevFrameOk[1] ||
1505
0
                (pConcealmentInfo->prevFrameOk[0] &&
1506
0
                 !pConcealmentInfo->prevFrameOk[1] && frameOk))) {
1507
0
            if (pConcealCommonData->numFadeOutFrames > 0) {
1508
              /* Fade out only if the energy interpolation algorithm can not be
1509
               * applied! */
1510
0
              pConcealmentInfo->concealState = ConcealState_FadeOut;
1511
0
            } else {
1512
              /* change to state MUTE */
1513
0
              pConcealmentInfo->concealState = ConcealState_Mute;
1514
0
            }
1515
0
            pConcealmentInfo->cntFadeFrames = 0;
1516
0
            pConcealmentInfo->cntValidFrames = 0;
1517
0
          }
1518
0
          break;
1519
1520
0
        case ConcealState_Single:
1521
0
          pConcealmentInfo->concealState = ConcealState_Ok;
1522
0
          break;
1523
1524
0
        case ConcealState_FadeOut:
1525
0
          pConcealmentInfo->cntFadeFrames += 1;
1526
1527
0
          if (pConcealmentInfo->cntValidFrames >
1528
0
              pConcealCommonData->numMuteReleaseFrames) {
1529
0
            if (pConcealCommonData->numFadeInFrames > 0) {
1530
              /* change to state FADE-IN */
1531
0
              pConcealmentInfo->concealState = ConcealState_FadeIn;
1532
0
              pConcealmentInfo->cntFadeFrames = findEquiFadeFrame(
1533
0
                  pConcealCommonData, pConcealmentInfo->cntFadeFrames - 1,
1534
0
                  0 /* FadeOut -> FadeIn */);
1535
0
            } else {
1536
              /* change to state OK */
1537
0
              pConcealmentInfo->concealState = ConcealState_Ok;
1538
0
            }
1539
0
          } else {
1540
0
            if (pConcealmentInfo->cntFadeFrames >=
1541
0
                pConcealCommonData->numFadeOutFrames) {
1542
              /* change to state MUTE */
1543
0
              pConcealmentInfo->concealState = ConcealState_Mute;
1544
0
            }
1545
0
          }
1546
0
          break;
1547
1548
0
        case ConcealState_Mute:
1549
0
          if (pConcealmentInfo->cntValidFrames >
1550
0
              pConcealCommonData->numMuteReleaseFrames) {
1551
0
            if (pConcealCommonData->numFadeInFrames > 0) {
1552
              /* change to state FADE-IN */
1553
0
              pConcealmentInfo->concealState = ConcealState_FadeIn;
1554
0
              pConcealmentInfo->cntFadeFrames =
1555
0
                  pConcealCommonData->numFadeInFrames - 1;
1556
0
            } else {
1557
              /* change to state OK */
1558
0
              pConcealmentInfo->concealState = ConcealState_Ok;
1559
0
            }
1560
0
          }
1561
0
          break;
1562
1563
0
        case ConcealState_FadeIn:
1564
0
          pConcealmentInfo->cntFadeFrames -=
1565
0
              1; /* used to address the fade-in factors */
1566
1567
0
          if (frameOk || pConcealmentInfo->prevFrameOk[1]) {
1568
0
            if (pConcealmentInfo->cntFadeFrames < 0) {
1569
              /* change to state OK */
1570
0
              pConcealmentInfo->concealState = ConcealState_Ok;
1571
0
            }
1572
0
          } else {
1573
0
            if (pConcealCommonData->numFadeOutFrames > 0) {
1574
              /* change to state FADE-OUT */
1575
0
              pConcealmentInfo->concealState = ConcealState_FadeOut;
1576
0
              pConcealmentInfo->cntFadeFrames = findEquiFadeFrame(
1577
0
                  pConcealCommonData, pConcealmentInfo->cntFadeFrames + 1,
1578
0
                  1 /* FadeIn -> FadeOut */);
1579
0
            } else {
1580
              /* change to state MUTE */
1581
0
              pConcealmentInfo->concealState = ConcealState_Mute;
1582
0
            }
1583
0
          }
1584
0
          break;
1585
0
      } /* End switch(pConcealmentInfo->concealState) */
1586
0
    } break;
1587
1588
0
    default:
1589
      /* Don't need a state machine for other concealment methods. */
1590
0
      break;
1591
4.08M
  }
1592
4.08M
}
1593
1594
/*!
1595
\brief Randomizes the sign of the spectral data
1596
1597
  The function toggles the sign of the spectral data randomly. This is
1598
  useful to ensure the quality of the concealed frames.
1599
 */
1600
static void CConcealment_ApplyRandomSign(int randomPhase, FIXP_DBL *spec,
1601
1.01M
                                         int samplesPerFrame) {
1602
1.01M
  int i;
1603
1.01M
  USHORT packedSign = 0;
1604
1605
  /* random table 512x16bit has been reduced to 512 packed sign bits = 32x16 bit
1606
   */
1607
1608
  /* read current packed sign word */
1609
1.01M
  packedSign = AacDec_randomSign[randomPhase >> 4];
1610
1.01M
  packedSign >>= (randomPhase & 0xf);
1611
1612
440M
  for (i = 0; i < samplesPerFrame; i++) {
1613
439M
    if ((randomPhase & 0xf) == 0) {
1614
27.4M
      packedSign = AacDec_randomSign[randomPhase >> 4];
1615
27.4M
    }
1616
1617
439M
    if (packedSign & 0x1) {
1618
225M
      spec[i] = -fMax(spec[i], (FIXP_DBL)(MINVAL_DBL + 1));
1619
225M
    }
1620
439M
    packedSign >>= 1;
1621
1622
439M
    randomPhase = (randomPhase + 1) & (AAC_NF_NO_RANDOM_VAL - 1);
1623
439M
  }
1624
1.01M
}
1625
1626
/*!
1627
  \brief Get fadeing factor for current concealment state.
1628
1629
  The function returns the state (ok or not) of the previous frame.
1630
  If called before the function CConcealment_Apply() set the fBeforeApply
1631
  flag to get the correct value.
1632
1633
  \return Frame OK flag of previous frame.
1634
 */
1635
int CConcealment_GetLastFrameOk(CConcealmentInfo *hConcealmentInfo,
1636
3.55M
                                const int fBeforeApply) {
1637
3.55M
  int prevFrameOk = 1;
1638
1639
3.55M
  if (hConcealmentInfo != NULL) {
1640
3.55M
    prevFrameOk = hConcealmentInfo->prevFrameOk[fBeforeApply & 0x1];
1641
3.55M
  }
1642
1643
3.55M
  return prevFrameOk;
1644
3.55M
}
1645
1646
/*!
1647
  \brief Get the number of delay frames introduced by concealment technique.
1648
1649
  \return Number of delay frames.
1650
 */
1651
2.39M
UINT CConcealment_GetDelay(CConcealParams *pConcealCommonData) {
1652
2.39M
  UINT frameDelay = 0;
1653
1654
2.39M
  if (pConcealCommonData != NULL) {
1655
2.39M
    switch (pConcealCommonData->method) {
1656
0
      case ConcealMethodTonal:
1657
57.1k
      case ConcealMethodInter:
1658
57.1k
        frameDelay = 1;
1659
57.1k
        break;
1660
2.33M
      default:
1661
2.33M
        break;
1662
2.39M
    }
1663
2.39M
  }
1664
1665
2.39M
  return frameDelay;
1666
2.39M
}
1667
1668
static int CConcealment_ApplyFadeOut(
1669
    int mode, CConcealmentInfo *pConcealmentInfo,
1670
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
1671
1.20M
    const int samplesPerFrame, CAacDecoderChannelInfo *pAacDecoderChannelInfo) {
1672
  /* mode 1 = apply RandomSign and mute spectral coefficients if necessary,  *
1673
   * mode 0 = Update cntFadeFrames                                            */
1674
1675
  /* restore frequency coefficients from buffer with a specific muting */
1676
1.20M
  int srcWin, dstWin, numWindows = 1;
1677
1.20M
  int windowLen = samplesPerFrame;
1678
1.20M
  int srcGrpStart = 0;
1679
1.20M
  int winIdxStride = 1;
1680
1.20M
  int numWinGrpPerFac, attIdx, attIdxStride;
1681
1.20M
  int i;
1682
1.20M
  int appliedProcessing = 0;
1683
1684
1.20M
  CIcsInfo *pIcsInfo = &pAacDecoderChannelInfo->icsInfo;
1685
1.20M
  FIXP_DBL *pSpectralCoefficient =
1686
1.20M
      SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient);
1687
1.20M
  SHORT *pSpecScale = pAacDecoderChannelInfo->specScale;
1688
1689
  /* set old window parameters */
1690
1.20M
  if (pConcealmentInfo->lastRenderMode == AACDEC_RENDER_LPD) {
1691
131k
    switch (pAacDecoderStaticChannelInfo->last_lpd_mode) {
1692
29.8k
      case 1:
1693
29.8k
        numWindows = 4;
1694
29.8k
        srcGrpStart = 3;
1695
29.8k
        windowLen = samplesPerFrame >> 2;
1696
29.8k
        break;
1697
13.0k
      case 2:
1698
13.0k
        numWindows = 2;
1699
13.0k
        srcGrpStart = 1;
1700
13.0k
        windowLen = samplesPerFrame >> 1;
1701
13.0k
        winIdxStride = 2;
1702
13.0k
        break;
1703
8.02k
      case 3:
1704
8.02k
        numWindows = 1;
1705
8.02k
        srcGrpStart = 0;
1706
8.02k
        windowLen = samplesPerFrame;
1707
8.02k
        winIdxStride = 4;
1708
8.02k
        break;
1709
131k
    }
1710
131k
    pConcealmentInfo->lastWinGrpLen = 1;
1711
1.07M
  } else {
1712
1.07M
    pIcsInfo->WindowShape = pConcealmentInfo->windowShape;
1713
1.07M
    pIcsInfo->WindowSequence = pConcealmentInfo->windowSequence;
1714
1715
1.07M
    if (pConcealmentInfo->windowSequence == BLOCK_SHORT) {
1716
      /* short block handling */
1717
110k
      numWindows = 8;
1718
110k
      windowLen = samplesPerFrame >> 3;
1719
110k
      srcGrpStart = numWindows - pConcealmentInfo->lastWinGrpLen;
1720
110k
    }
1721
1.07M
  }
1722
1723
1.20M
  attIdxStride =
1724
1.20M
      fMax(1, (int)(numWindows / (pConcealmentInfo->lastWinGrpLen + 1)));
1725
1726
  /* load last state */
1727
1.20M
  attIdx = pConcealmentInfo->cntFadeFrames;
1728
1.20M
  numWinGrpPerFac = pConcealmentInfo->attGrpOffset[mode];
1729
1.20M
  srcWin = srcGrpStart + pConcealmentInfo->winGrpOffset[mode];
1730
1731
1.20M
  FDK_ASSERT((srcGrpStart * windowLen + windowLen) <= samplesPerFrame);
1732
1.20M
  FDK_ASSERT((srcWin * windowLen + windowLen) <= 1024);
1733
1734
3.29M
  for (dstWin = 0; dstWin < numWindows; dstWin += 1) {
1735
2.08M
    FIXP_CNCL *pCncl =
1736
2.08M
        pConcealmentInfo->spectralCoefficient + (srcWin * windowLen);
1737
2.08M
    FIXP_DBL *pOut = pSpectralCoefficient + (dstWin * windowLen);
1738
1739
2.08M
    if (mode == 1) {
1740
      /* mute if attIdx gets large enaugh */
1741
1.01M
      if (attIdx > pConcealmentInfo->pConcealParams->numFadeOutFrames) {
1742
75.9k
        FDKmemclear(pCncl, sizeof(FIXP_DBL) * windowLen);
1743
75.9k
      }
1744
1745
      /* restore frequency coefficients from buffer - attenuation is done later
1746
       */
1747
440M
      for (i = 0; i < windowLen; i++) {
1748
439M
        pOut[i] = pCncl[i];
1749
439M
      }
1750
1751
      /* apply random change of sign for spectral coefficients */
1752
1.01M
      CConcealment_ApplyRandomSign(pConcealmentInfo->iRandomPhase, pOut,
1753
1.01M
                                   windowLen);
1754
1755
      /* Increment random phase index to avoid repetition artifacts. */
1756
1.01M
      pConcealmentInfo->iRandomPhase =
1757
1.01M
          (pConcealmentInfo->iRandomPhase + 1) & (AAC_NF_NO_RANDOM_VAL - 1);
1758
1759
      /* set old scale factors */
1760
1.01M
      pSpecScale[dstWin * winIdxStride] =
1761
1.01M
          pConcealmentInfo->specScale[srcWin * winIdxStride];
1762
1.01M
    }
1763
1764
2.08M
    srcWin += 1;
1765
1766
2.08M
    if (srcWin >= numWindows) {
1767
      /* end of sequence -> rewind to first window of group */
1768
1.77M
      srcWin = srcGrpStart;
1769
1.77M
      numWinGrpPerFac += 1;
1770
1.77M
      if (numWinGrpPerFac >= attIdxStride) {
1771
1.33M
        numWinGrpPerFac = 0;
1772
1.33M
        attIdx += 1;
1773
1.33M
      }
1774
1.77M
    }
1775
2.08M
  }
1776
1777
  /* store current state */
1778
1779
1.20M
  pConcealmentInfo->winGrpOffset[mode] = srcWin - srcGrpStart;
1780
1.20M
  FDK_ASSERT((pConcealmentInfo->winGrpOffset[mode] >= 0) &&
1781
1.20M
             (pConcealmentInfo->winGrpOffset[mode] < 8));
1782
1.20M
  pConcealmentInfo->attGrpOffset[mode] = numWinGrpPerFac;
1783
1.20M
  FDK_ASSERT((pConcealmentInfo->attGrpOffset[mode] >= 0) &&
1784
1.20M
             (pConcealmentInfo->attGrpOffset[mode] < attIdxStride));
1785
1786
1.20M
  if (mode == 0) {
1787
632k
    pConcealmentInfo->cntFadeFrames = attIdx;
1788
632k
  }
1789
1790
1.20M
  appliedProcessing = 1;
1791
1792
1.20M
  return appliedProcessing;
1793
1.20M
}
1794
1795
/*!
1796
  \brief Do Time domain fading (TDFading) in concealment case
1797
1798
  In case of concealment, this function takes care of the fading, after time
1799
domain signal has been rendered by the respective signal rendering functions.
1800
  The fading out in case of ACELP decoding is not done by this function but by
1801
the ACELP decoder for the first concealed frame if CONCEAL_CORE_IGNORANT_FADE is
1802
not set.
1803
1804
  TimeDomain fading never creates jumps in energy / discontinuities, it always
1805
does a continuous fading. To achieve this, fading is always done from a starting
1806
point to a target point, while the starting point is always determined to be the
1807
last target point. By varying the target point of a fading, the fading slope can
1808
be controlled.
1809
1810
  This principle is applied to the fading within a frame and the fading from
1811
frame to frame.
1812
1813
  One frame is divided into 8 subframes to obtain 8 parts of fading slopes
1814
within a frame, each maybe with its own gradient.
1815
1816
  Workflow:
1817
  1.) Determine Fading behavior and end-of-frame target fading level, based on
1818
concealmentState (determined by CConcealment_UpdateState()) and the core mode.
1819
        - By _DEFAULT_,
1820
          The target fading level is determined by fadeOutFactor[cntFadeFrames]
1821
in case of fadeOut, or fadeInFactor[cntFadeFrames] in case of fadeIn.
1822
          --> fading type is FADE_TIMEDOMAIN in this case. Target fading level
1823
is determined by fading index cntFadeFrames.
1824
1825
        - If concealmentState is signalling a _MUTED SIGNAL_,
1826
          TDFading decays to 0 within 1/8th of a frame if numFadeOutFrames == 0.
1827
          --> fading type is FADE_TIMEDOMAIN_TOSPECTRALMUTE in this case.
1828
1829
        - If concealmentState is signalling the _END OF MUTING_,
1830
          TDFading fades to target fading level within 1/8th of a frame if
1831
numFadeInFrames == 0.
1832
          --> fading type is FADE_TIMEDOMAIN_FROMSPECTRALMUTE in this case.
1833
Target fading level is determined by fading index cntFadeFrames.
1834
1835
#ifndef CONCEAL_CORE_IGNORANT_FADE
1836
        - In case of an _ACELP FADEOUT_,
1837
          TDFading leaves fading control to ACELP decoder for 1/2 frame.
1838
          --> fading type is FADE_ACELPDOMAIN in this case.
1839
#endif
1840
1841
  2.) Render fading levels within current frame and do the final fading:
1842
      Map Fading slopes to fading levels and apply to time domain signal.
1843
1844
1845
*/
1846
1847
INT CConcealment_TDFading(
1848
    int len, CAacDecoderStaticChannelInfo **ppAacDecoderStaticChannelInfo,
1849
4.08M
    const INT aacOutDataHeadroom, PCM_DEC *pcmdata, PCM_DEC *pcmdata_1) {
1850
  /*
1851
  Do the fading in Time domain based on concealment states and core mode
1852
  */
1853
4.08M
  FIXP_DBL fadeStop, attMute = (FIXP_DBL)0;
1854
4.08M
  int idx = 0, ii;
1855
4.08M
  CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo =
1856
4.08M
      *ppAacDecoderStaticChannelInfo;
1857
4.08M
  CConcealmentInfo *pConcealmentInfo =
1858
4.08M
      &pAacDecoderStaticChannelInfo->concealmentInfo;
1859
4.08M
  CConcealParams *pConcealParams = pConcealmentInfo->pConcealParams;
1860
4.08M
  const CConcealmentState concealState = pConcealmentInfo->concealState;
1861
4.08M
  TDfadingType fadingType;
1862
4.08M
  FIXP_DBL fadingStations[9] = {0};
1863
4.08M
  int fadingSteps[8] = {0};
1864
4.08M
  const FIXP_DBL fadeStart =
1865
4.08M
      pConcealmentInfo
1866
4.08M
          ->fade_old; /* start fading at last end-of-frame attenuation */
1867
4.08M
  FIXP_SGL *fadeFactor = pConcealParams->fadeOutFactor;
1868
4.08M
  const INT cntFadeFrames = pConcealmentInfo->cntFadeFrames;
1869
4.08M
  int TDFadeOutStopBeforeMute = 1;
1870
4.08M
  int TDFadeInStopBeforeFullLevel = 1;
1871
1872
  /*
1873
  determine Fading behaviour (end-of-frame attenuation and fading type) (1.)
1874
  */
1875
1876
4.08M
  switch (concealState) {
1877
80.4k
    case ConcealState_Single:
1878
3.06M
    case ConcealState_Mute:
1879
3.61M
    case ConcealState_FadeOut:
1880
3.61M
      idx = (pConcealParams->method == ConcealMethodNoise) ? cntFadeFrames - 1
1881
3.61M
                                                           : cntFadeFrames;
1882
3.61M
      fadingType = FADE_TIMEDOMAIN;
1883
1884
3.61M
      if (concealState == ConcealState_Mute ||
1885
633k
          (cntFadeFrames + TDFadeOutStopBeforeMute) >
1886
3.16M
              pConcealmentInfo->pConcealParams->numFadeOutFrames) {
1887
3.16M
        fadingType = FADE_TIMEDOMAIN_TOSPECTRALMUTE;
1888
3.16M
      }
1889
1890
3.61M
      break;
1891
336k
    case ConcealState_FadeIn:
1892
336k
      idx = cntFadeFrames;
1893
336k
      idx -= TDFadeInStopBeforeFullLevel;
1894
336k
      FDK_FALLTHROUGH;
1895
469k
    case ConcealState_Ok:
1896
469k
      fadeFactor = pConcealParams->fadeInFactor;
1897
469k
      idx = (concealState == ConcealState_Ok) ? -1 : idx;
1898
469k
      fadingType = (pConcealmentInfo->concealState_old == ConcealState_Mute)
1899
469k
                       ? FADE_TIMEDOMAIN_FROMSPECTRALMUTE
1900
469k
                       : FADE_TIMEDOMAIN;
1901
469k
      break;
1902
0
    default:
1903
0
      FDK_ASSERT(0);
1904
0
      fadingType = FADE_TIMEDOMAIN_TOSPECTRALMUTE;
1905
0
      break;
1906
4.08M
  }
1907
1908
  /* determine Target end-of-frame fading level and fading slope */
1909
4.08M
  switch (fadingType) {
1910
156k
    case FADE_TIMEDOMAIN_FROMSPECTRALMUTE:
1911
156k
      fadeStop =
1912
156k
          (idx < 0) ? (FIXP_DBL)MAXVAL_DBL : FX_SGL2FX_DBL(fadeFactor[idx]);
1913
156k
      if (pConcealmentInfo->pConcealParams->numFadeInFrames == 0) {
1914
        /* do step as fast as possible */
1915
0
        fadingSteps[0] = 1;
1916
0
        break;
1917
0
      }
1918
156k
      CConcealment_TDFading_doLinearFadingSteps(&fadingSteps[0]);
1919
156k
      break;
1920
765k
    case FADE_TIMEDOMAIN:
1921
765k
      fadeStop =
1922
765k
          (idx < 0) ? (FIXP_DBL)MAXVAL_DBL : FX_SGL2FX_DBL(fadeFactor[idx]);
1923
765k
      CConcealment_TDFading_doLinearFadingSteps(&fadingSteps[0]);
1924
765k
      break;
1925
3.16M
    case FADE_TIMEDOMAIN_TOSPECTRALMUTE:
1926
3.16M
      fadeStop = attMute;
1927
3.16M
      if (pConcealmentInfo->pConcealParams->numFadeOutFrames == 0) {
1928
        /* do step as fast as possible */
1929
0
        fadingSteps[0] = 1;
1930
0
        break;
1931
0
      }
1932
3.16M
      CConcealment_TDFading_doLinearFadingSteps(&fadingSteps[0]);
1933
3.16M
      break;
1934
4.08M
  }
1935
1936
  /*
1937
  Render fading levels within current frame and do the final fading (2.)
1938
  */
1939
1940
4.08M
  len >>= 3;
1941
4.08M
  CConcealment_TDFadeFillFadingStations(fadingStations, fadingSteps, fadeStop,
1942
4.08M
                                        fadeStart, fadingType);
1943
1944
4.08M
  if ((fadingStations[8] != (FIXP_DBL)MAXVAL_DBL) ||
1945
140k
      (fadingStations[7] != (FIXP_DBL)MAXVAL_DBL) ||
1946
113k
      (fadingStations[6] != (FIXP_DBL)MAXVAL_DBL) ||
1947
113k
      (fadingStations[5] != (FIXP_DBL)MAXVAL_DBL) ||
1948
113k
      (fadingStations[4] != (FIXP_DBL)MAXVAL_DBL) ||
1949
113k
      (fadingStations[3] != (FIXP_DBL)MAXVAL_DBL) ||
1950
113k
      (fadingStations[2] != (FIXP_DBL)MAXVAL_DBL) ||
1951
113k
      (fadingStations[1] != (FIXP_DBL)MAXVAL_DBL) ||
1952
113k
      (fadingStations[0] !=
1953
113k
       (FIXP_DBL)MAXVAL_DBL)) /* if there's something to fade */
1954
3.97M
  {
1955
3.97M
    int start = 0;
1956
35.7M
    for (ii = 0; ii < 8; ii++) {
1957
31.7M
      CConcealment_TDFadePcmAtt(start, len, fadingStations[ii],
1958
31.7M
                                fadingStations[ii + 1], pcmdata);
1959
31.7M
      start += len;
1960
31.7M
    }
1961
3.97M
  }
1962
4.08M
  CConcealment_TDNoise_Apply(pConcealmentInfo, len, aacOutDataHeadroom,
1963
4.08M
                             pcmdata);
1964
1965
  /* Save end-of-frame attenuation and fading type */
1966
4.08M
  pConcealmentInfo->lastFadingType = fadingType;
1967
4.08M
  pConcealmentInfo->fade_old = fadeStop;
1968
4.08M
  pConcealmentInfo->concealState_old = concealState;
1969
1970
4.08M
  return 1;
1971
4.08M
}
1972
1973
/* attenuate pcmdata in Time Domain Fading process */
1974
static void CConcealment_TDFadePcmAtt(int start, int len, FIXP_DBL fadeStart,
1975
31.7M
                                      FIXP_DBL fadeStop, PCM_DEC *pcmdata) {
1976
31.7M
  int i;
1977
31.7M
  FIXP_DBL dStep;
1978
31.7M
  FIXP_DBL dGain;
1979
31.7M
  FIXP_DBL dGain_apply;
1980
1981
  /* set start energy */
1982
31.7M
  dGain = fadeStart;
1983
  /* determine energy steps from sample to sample */
1984
31.7M
  dStep = (FIXP_DBL)((int)((fadeStart >> 1) - (fadeStop >> 1)) / len) << 1;
1985
1986
2.82G
  for (i = start; i < (start + len); i++) {
1987
2.78G
    dGain -= dStep;
1988
    /* prevent gain from getting negative due to possible fixpoint inaccuracies
1989
     */
1990
2.78G
    dGain_apply = fMax((FIXP_DBL)0, dGain);
1991
    /* finally, attenuate samples */
1992
2.78G
    pcmdata[i] = FIXP_DBL2PCM_DEC(fMult(pcmdata[i], dGain_apply));
1993
2.78G
  }
1994
31.7M
}
1995
1996
/*
1997
\brief Fill FadingStations
1998
1999
The fadingstations are the attenuation factors, being applied to its dedicated
2000
portions of pcm data. They are calculated using the fadingsteps. One fadingstep
2001
is the weighted contribution to the fading slope within its dedicated portion of
2002
pcm data.
2003
2004
*Fadingsteps  :      0  0  0  1  0  1  2  0
2005
2006
                  |<-  1 Frame pcm data ->|
2007
      fadeStart-->|__________             |
2008
                  ^  ^  ^  ^ \____        |
2009
 Attenuation  :   |  |  |  |  ^  ^\__     |
2010
                  |  |  |  |  |  |  ^\    |
2011
                  |  |  |  |  |  |  | \___|<-- fadeStop
2012
                  |  |  |  |  |  |  |  ^  ^
2013
                  |  |  |  |  |  |  |  |  |
2014
Fadingstations:  [0][1][2][3][4][5][6][7][8]
2015
2016
(Fadingstations "[0]" is "[8] from previous frame", therefore its not meaningful
2017
to be edited)
2018
2019
*/
2020
static void CConcealment_TDFadeFillFadingStations(FIXP_DBL *fadingStations,
2021
                                                  int *fadingSteps,
2022
                                                  FIXP_DBL fadeStop,
2023
                                                  FIXP_DBL fadeStart,
2024
4.08M
                                                  TDfadingType fadingType) {
2025
4.08M
  int i;
2026
4.08M
  INT fadingSteps_sum = 0;
2027
4.08M
  INT fadeDiff;
2028
2029
4.08M
  fadingSteps_sum = fadingSteps[0] + fadingSteps[1] + fadingSteps[2] +
2030
4.08M
                    fadingSteps[3] + fadingSteps[4] + fadingSteps[5] +
2031
4.08M
                    fadingSteps[6] + fadingSteps[7];
2032
4.08M
  fadeDiff = ((INT)(fadeStop - fadeStart) / fMax(fadingSteps_sum, (INT)1));
2033
4.08M
  fadingStations[0] = fadeStart;
2034
32.6M
  for (i = 1; i < 8; i++) {
2035
28.5M
    fadingStations[i] =
2036
28.5M
        fadingStations[i - 1] + (FIXP_DBL)(fadeDiff * fadingSteps[i - 1]);
2037
28.5M
  }
2038
4.08M
  fadingStations[8] = fadeStop;
2039
4.08M
}
2040
2041
4.08M
static void CConcealment_TDFading_doLinearFadingSteps(int *fadingSteps) {
2042
4.08M
  fadingSteps[0] = fadingSteps[1] = fadingSteps[2] = fadingSteps[3] =
2043
4.08M
      fadingSteps[4] = fadingSteps[5] = fadingSteps[6] = fadingSteps[7] = 1;
2044
4.08M
}
2045
2046
/* end of TimeDomainFading functions */
2047
2048
/* derived from int UsacRandomSign() */
2049
2.79G
static int CConcealment_TDNoise_Random(ULONG *seed) {
2050
2.79G
  *seed = (ULONG)(((UINT64)(*seed) * 69069) + 5);
2051
2.79G
  return (int)(*seed);
2052
2.79G
}
2053
2054
static void CConcealment_TDNoise_Apply(CConcealmentInfo *const pConcealmentInfo,
2055
                                       const int len,
2056
                                       const INT aacOutDataHeadroom,
2057
4.08M
                                       PCM_DEC *const pcmdata) {
2058
4.08M
  PCM_DEC *states = pConcealmentInfo->TDNoiseStates;
2059
4.08M
  PCM_DEC noiseVal;
2060
4.08M
  FIXP_DBL noiseValLong;
2061
4.08M
  FIXP_SGL *coef = pConcealmentInfo->TDNoiseCoef;
2062
4.08M
  FIXP_DBL TDNoiseAtt;
2063
4.08M
  ULONG seed = pConcealmentInfo->TDNoiseSeed =
2064
4.08M
      (ULONG)CConcealment_TDNoise_Random(&pConcealmentInfo->TDNoiseSeed) + 1;
2065
2066
4.08M
  TDNoiseAtt = pConcealmentInfo->pConcealParams->comfortNoiseLevel;
2067
2068
4.08M
  int ii;
2069
2070
4.08M
  if ((pConcealmentInfo->concealState != ConcealState_Ok ||
2071
133k
       pConcealmentInfo->concealState_old != ConcealState_Ok) &&
2072
3.97M
      TDNoiseAtt != (FIXP_DBL)0) {
2073
2.79G
    for (ii = 0; ii < (len << 3); ii++) {
2074
      /* create filtered noise */
2075
2.79G
      states[2] = states[1];
2076
2.79G
      states[1] = states[0];
2077
2.79G
      states[0] =
2078
2.79G
          FIXP_DBL2PCM_DEC((FIXP_DBL)CConcealment_TDNoise_Random(&seed));
2079
2.79G
      noiseValLong = fMult(states[0], coef[0]) + fMult(states[1], coef[1]) +
2080
2.79G
                     fMult(states[2], coef[2]);
2081
2.79G
      noiseVal = FIXP_DBL2PCM_DEC(fMult(noiseValLong, TDNoiseAtt) >>
2082
2.79G
                                  aacOutDataHeadroom);
2083
2084
      /* add filtered noise - check for clipping, before */
2085
2.79G
      if (noiseVal > (PCM_DEC)0 &&
2086
1.39G
          pcmdata[ii] > (PCM_DEC)MAXVAL_PCM_DEC - noiseVal) {
2087
0
        noiseVal = noiseVal * (PCM_DEC)-1;
2088
2.79G
      } else if (noiseVal < (PCM_DEC)0 &&
2089
1.39G
                 pcmdata[ii] < (PCM_DEC)MINVAL_PCM_DEC - noiseVal) {
2090
0
        noiseVal = noiseVal * (PCM_DEC)-1;
2091
0
      }
2092
2093
2.79G
      pcmdata[ii] += noiseVal;
2094
2.79G
    }
2095
3.97M
  }
2096
4.08M
}