Coverage Report

Created: 2026-07-10 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aac/libAACdec/src/aacdecoder_lib.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2023 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):   Manuel Jander
98
99
   Description:
100
101
*******************************************************************************/
102
103
#include "aacdecoder_lib.h"
104
105
#include "aac_ram.h"
106
#include "aacdecoder.h"
107
#include "tpdec_lib.h"
108
#include "FDK_core.h" /* FDK_tools version info */
109
110
#include "sbrdecoder.h"
111
112
#include "conceal.h"
113
114
#include "aacdec_drc.h"
115
116
#include "sac_dec_lib.h"
117
118
#include "pcm_utils.h"
119
120
/* Decoder library info */
121
#define AACDECODER_LIB_VL0 3
122
#define AACDECODER_LIB_VL1 2
123
#define AACDECODER_LIB_VL2 0
124
0
#define AACDECODER_LIB_TITLE "AAC Decoder Lib"
125
#ifdef SUPPRESS_BUILD_DATE_INFO
126
#define AACDECODER_LIB_BUILD_DATE ""
127
#define AACDECODER_LIB_BUILD_TIME ""
128
#else
129
0
#define AACDECODER_LIB_BUILD_DATE __DATE__
130
0
#define AACDECODER_LIB_BUILD_TIME __TIME__
131
#endif
132
133
static AAC_DECODER_ERROR setConcealMethod(const HANDLE_AACDECODER self,
134
                                          const INT method);
135
136
static void aacDecoder_setMetadataExpiry(const HANDLE_AACDECODER self,
137
712k
                                         const INT value) {
138
  /* check decoder handle */
139
712k
  if (self != NULL) {
140
712k
    INT mdExpFrame = 0; /* default: disable */
141
142
712k
    if ((value > 0) &&
143
0
        (self->streamInfo.aacSamplesPerFrame >
144
0
         0)) { /* Determine the corresponding number of frames: */
145
0
      FIXP_DBL frameTime = fDivNorm(self->streamInfo.aacSampleRate,
146
0
                                    self->streamInfo.aacSamplesPerFrame * 1000);
147
0
      mdExpFrame = fMultIceil(frameTime, value);
148
0
    }
149
150
    /* Configure DRC module */
151
712k
    aacDecoder_drcSetParam(self->hDrcInfo, DRC_DATA_EXPIRY_FRAME, mdExpFrame);
152
153
    /* Configure PCM downmix module */
154
712k
    pcmDmx_SetParam(self->hPcmUtils, DMX_BS_DATA_EXPIRY_FRAME, mdExpFrame);
155
712k
  }
156
712k
}
157
158
LINKSPEC_CPP AAC_DECODER_ERROR
159
0
aacDecoder_GetFreeBytes(const HANDLE_AACDECODER self, UINT *pFreeBytes) {
160
  /* reset free bytes */
161
0
  *pFreeBytes = 0;
162
163
  /* check handle */
164
0
  if (!self) return AAC_DEC_INVALID_HANDLE;
165
166
  /* return nr of free bytes */
167
0
  HANDLE_FDK_BITSTREAM hBs = transportDec_GetBitstream(self->hInput, 0);
168
0
  *pFreeBytes = FDKgetFreeBits(hBs) >> 3;
169
170
  /* success */
171
0
  return AAC_DEC_OK;
172
0
}
173
174
/**
175
 * Config Decoder using a CSAudioSpecificConfig struct.
176
 */
177
static LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_Config(
178
    HANDLE_AACDECODER self, const CSAudioSpecificConfig *pAscStruct,
179
727k
    UCHAR configMode, UCHAR *configChanged) {
180
727k
  AAC_DECODER_ERROR err;
181
182
  /* Initialize AAC core decoder, and update self->streaminfo */
183
727k
  err = CAacDecoder_Init(self, pAscStruct, configMode, configChanged);
184
185
727k
  if (!FDK_chMapDescr_isValid(&self->mapDescr)) {
186
0
    return AAC_DEC_UNSUPPORTED_CHANNELCONFIG;
187
0
  }
188
189
727k
  return err;
190
727k
}
191
192
LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_ConfigRaw(HANDLE_AACDECODER self,
193
                                                    UCHAR *conf[],
194
6.83k
                                                    const UINT length[]) {
195
6.83k
  AAC_DECODER_ERROR err = AAC_DEC_OK;
196
6.83k
  TRANSPORTDEC_ERROR errTp;
197
6.83k
  UINT layer, nrOfLayers = self->nrOfLayers;
198
199
9.52k
  for (layer = 0; layer < nrOfLayers; layer++) {
200
6.83k
    if (length[layer] > 0) {
201
6.83k
      errTp = transportDec_OutOfBandConfig(self->hInput, conf[layer],
202
6.83k
                                           length[layer], layer);
203
6.83k
      if (errTp != TRANSPORTDEC_OK) {
204
4.13k
        switch (errTp) {
205
0
          case TRANSPORTDEC_NEED_TO_RESTART:
206
0
            err = AAC_DEC_NEED_TO_RESTART;
207
0
            break;
208
138
          case TRANSPORTDEC_UNSUPPORTED_FORMAT:
209
138
            err = AAC_DEC_UNSUPPORTED_FORMAT;
210
138
            break;
211
3.99k
          default:
212
3.99k
            err = AAC_DEC_UNKNOWN;
213
3.99k
            break;
214
4.13k
        }
215
        /* if baselayer is OK we continue decoding */
216
4.13k
        if (layer >= 1) {
217
0
          self->nrOfLayers = layer;
218
0
          err = AAC_DEC_OK;
219
0
        }
220
4.13k
        break;
221
4.13k
      }
222
6.83k
    }
223
6.83k
  }
224
225
6.83k
  return err;
226
6.83k
}
227
228
LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_RawISOBMFFData(HANDLE_AACDECODER self,
229
                                                         UCHAR *buffer,
230
0
                                                         UINT length) {
231
0
  FDK_BITSTREAM bs;
232
0
  HANDLE_FDK_BITSTREAM hBs = &bs;
233
0
  AAC_DECODER_ERROR err = AAC_DEC_OK;
234
235
0
  if (length < 8) return AAC_DEC_UNKNOWN;
236
237
0
  while (length >= 8) {
238
0
    UINT size =
239
0
        (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
240
0
    DRC_DEC_ERROR uniDrcErr = DRC_DEC_OK;
241
242
0
    if (length < size) return AAC_DEC_UNKNOWN;
243
0
    if (size <= 8) return AAC_DEC_UNKNOWN;
244
245
0
    FDKinitBitStream(hBs, buffer + 8, 0x10000000, (size - 8) * 8);
246
247
0
    if ((buffer[4] == 'l') && (buffer[5] == 'u') && (buffer[6] == 'd') &&
248
0
        (buffer[7] == 't')) {
249
0
      uniDrcErr = FDK_drcDec_ReadLoudnessBox(self->hUniDrcDecoder, hBs);
250
0
    } else if ((buffer[4] == 'd') && (buffer[5] == 'm') && (buffer[6] == 'i') &&
251
0
               (buffer[7] == 'x')) {
252
0
      uniDrcErr =
253
0
          FDK_drcDec_ReadDownmixInstructions_Box(self->hUniDrcDecoder, hBs);
254
0
    } else if ((buffer[4] == 'u') && (buffer[5] == 'd') && (buffer[6] == 'i') &&
255
0
               (buffer[7] == '2')) {
256
0
      uniDrcErr =
257
0
          FDK_drcDec_ReadUniDrcInstructions_Box(self->hUniDrcDecoder, hBs);
258
0
    } else if ((buffer[4] == 'u') && (buffer[5] == 'd') && (buffer[6] == 'c') &&
259
0
               (buffer[7] == '2')) {
260
0
      uniDrcErr =
261
0
          FDK_drcDec_ReadUniDrcCoefficients_Box(self->hUniDrcDecoder, hBs);
262
0
    }
263
264
0
    if (uniDrcErr != DRC_DEC_OK) err = AAC_DEC_UNKNOWN;
265
266
0
    buffer += size;
267
0
    length -= size;
268
0
  }
269
270
0
  return err;
271
0
}
272
273
static INT aacDecoder_ConfigCallback(void *handle,
274
                                     const CSAudioSpecificConfig *pAscStruct,
275
727k
                                     UCHAR configMode, UCHAR *configChanged) {
276
727k
  HANDLE_AACDECODER self = (HANDLE_AACDECODER)handle;
277
727k
  AAC_DECODER_ERROR err = AAC_DEC_OK;
278
727k
  TRANSPORTDEC_ERROR errTp;
279
280
727k
  FDK_ASSERT(self != NULL);
281
727k
  {
282
727k
    { err = aacDecoder_Config(self, pAscStruct, configMode, configChanged); }
283
727k
  }
284
727k
  if (err == AAC_DEC_OK) {
285
    /*
286
    revert concealment method if either
287
       - Interpolation concealment might not be meaningful
288
       - Interpolation concealment is not implemented
289
    */
290
712k
    if ((self->flags[0] & (AC_LD | AC_ELD) &&
291
118k
         (self->concealMethodUser == ConcealMethodNone) &&
292
118k
         CConcealment_GetDelay(&self->concealCommonData) >
293
118k
             0) /* might not be meaningful but allow if user has set it
294
                   expicitly */
295
705k
        || (self->flags[0] & (AC_USAC | AC_RSVD50 | AC_RSV603DA) &&
296
153k
            CConcealment_GetDelay(&self->concealCommonData) >
297
153k
                0) /* not implemented */
298
712k
    ) {
299
      /* Revert to error concealment method Noise Substitution.
300
         Because interpolation is not implemented for USAC or
301
         the additional delay is unwanted for low delay codecs. */
302
21.1k
      setConcealMethod(self, 1);
303
21.1k
    }
304
712k
    aacDecoder_setMetadataExpiry(self, self->metadataExpiry);
305
712k
    errTp = TRANSPORTDEC_OK;
306
712k
  } else {
307
15.0k
    if (err == AAC_DEC_NEED_TO_RESTART) {
308
0
      errTp = TRANSPORTDEC_NEED_TO_RESTART;
309
15.0k
    } else if (IS_INIT_ERROR(err)) {
310
15.0k
      errTp = TRANSPORTDEC_UNSUPPORTED_FORMAT;
311
15.0k
    } /* Fatal errors */
312
0
    else {
313
0
      errTp = TRANSPORTDEC_UNKOWN_ERROR;
314
0
    }
315
15.0k
  }
316
317
727k
  return errTp;
318
727k
}
319
320
static INT aacDecoder_FreeMemCallback(void *handle,
321
252k
                                      const CSAudioSpecificConfig *pAscStruct) {
322
252k
  TRANSPORTDEC_ERROR errTp = TRANSPORTDEC_OK;
323
252k
  HANDLE_AACDECODER self = (HANDLE_AACDECODER)handle;
324
325
252k
  const int subStreamIndex = 0;
326
327
252k
  FDK_ASSERT(self != NULL);
328
329
252k
  if (CAacDecoder_FreeMem(self, subStreamIndex) != AAC_DEC_OK) {
330
0
    errTp = TRANSPORTDEC_UNKOWN_ERROR;
331
0
  }
332
333
  /* free Ram_SbrDecoder and Ram_SbrDecChannel */
334
252k
  if (self->hSbrDecoder != NULL) {
335
252k
    if (sbrDecoder_FreeMem(&self->hSbrDecoder) != SBRDEC_OK) {
336
0
      errTp = TRANSPORTDEC_UNKOWN_ERROR;
337
0
    }
338
252k
  }
339
340
  /* free pSpatialDec and mpsData */
341
252k
  if (self->pMpegSurroundDecoder != NULL) {
342
252k
    if (mpegSurroundDecoder_FreeMem(
343
252k
            (CMpegSurroundDecoder *)self->pMpegSurroundDecoder) != MPS_OK) {
344
0
      errTp = TRANSPORTDEC_UNKOWN_ERROR;
345
0
    }
346
252k
  }
347
348
  /* free persistent qmf domain buffer, QmfWorkBufferCore3, QmfWorkBufferCore4,
349
   * QmfWorkBufferCore5 and configuration variables */
350
252k
  FDK_QmfDomain_FreeMem(&self->qmfDomain);
351
352
252k
  return errTp;
353
252k
}
354
355
static INT aacDecoder_CtrlCFGChangeCallback(
356
124
    void *handle, const CCtrlCFGChange *pCtrlCFGChangeStruct) {
357
124
  TRANSPORTDEC_ERROR errTp = TRANSPORTDEC_OK;
358
124
  HANDLE_AACDECODER self = (HANDLE_AACDECODER)handle;
359
360
124
  if (self != NULL) {
361
124
    CAacDecoder_CtrlCFGChange(
362
124
        self, pCtrlCFGChangeStruct->flushStatus, pCtrlCFGChangeStruct->flushCnt,
363
124
        pCtrlCFGChangeStruct->buildUpStatus, pCtrlCFGChangeStruct->buildUpCnt);
364
124
  } else {
365
0
    errTp = TRANSPORTDEC_UNKOWN_ERROR;
366
0
  }
367
368
124
  return errTp;
369
124
}
370
371
static INT aacDecoder_SbrCallback(
372
    void *handle, HANDLE_FDK_BITSTREAM hBs, const INT sampleRateIn,
373
    const INT sampleRateOut, const INT samplesPerFrame,
374
    const AUDIO_OBJECT_TYPE coreCodec, const MP4_ELEMENT_ID elementID,
375
    const INT elementIndex, const UCHAR harmonicSBR,
376
    const UCHAR stereoConfigIndex, const UCHAR configMode, UCHAR *configChanged,
377
527k
    const INT downscaleFactor) {
378
527k
  HANDLE_SBRDECODER self = (HANDLE_SBRDECODER)handle;
379
380
527k
  INT errTp = sbrDecoder_Header(self, hBs, sampleRateIn, sampleRateOut,
381
527k
                                samplesPerFrame, coreCodec, elementID,
382
527k
                                elementIndex, harmonicSBR, stereoConfigIndex,
383
527k
                                configMode, configChanged, downscaleFactor);
384
385
527k
  return errTp;
386
527k
}
387
388
static INT aacDecoder_SscCallback(
389
    void *handle, HANDLE_FDK_BITSTREAM hBs, const AUDIO_OBJECT_TYPE coreCodec,
390
    const INT samplingRate, const INT frameSize, const INT numChannels,
391
    const INT stereoConfigIndex, const INT coreSbrFrameLengthIndex,
392
167k
    const INT configBytes, const UCHAR configMode, UCHAR *configChanged) {
393
167k
  SACDEC_ERROR err;
394
167k
  TRANSPORTDEC_ERROR errTp;
395
167k
  HANDLE_AACDECODER hAacDecoder = (HANDLE_AACDECODER)handle;
396
397
167k
  err = mpegSurroundDecoder_Config(
398
167k
      (CMpegSurroundDecoder *)hAacDecoder->pMpegSurroundDecoder, hBs, coreCodec,
399
167k
      samplingRate, frameSize, numChannels, stereoConfigIndex,
400
167k
      coreSbrFrameLengthIndex, configBytes, configMode, configChanged);
401
402
167k
  switch (err) {
403
27.9k
    case MPS_UNSUPPORTED_CONFIG:
404
      /* MPS found but invalid or not decodable by this instance            */
405
      /* We switch off MPS and keep going                                   */
406
27.9k
      hAacDecoder->mpsEnableCurr = 0;
407
27.9k
      hAacDecoder->mpsApplicable = 0;
408
27.9k
      errTp = TRANSPORTDEC_OK;
409
27.9k
      break;
410
15.5k
    case MPS_PARSE_ERROR:
411
      /* MPS found but invalid or not decodable by this instance            */
412
15.5k
      hAacDecoder->mpsEnableCurr = 0;
413
15.5k
      hAacDecoder->mpsApplicable = 0;
414
15.5k
      if ((coreCodec == AOT_USAC) || (coreCodec == AOT_DRM_USAC) ||
415
15.5k
          IS_LOWDELAY(coreCodec)) {
416
15.5k
        errTp = TRANSPORTDEC_PARSE_ERROR;
417
15.5k
      } else {
418
0
        errTp = TRANSPORTDEC_OK;
419
0
      }
420
15.5k
      break;
421
122k
    case MPS_OK:
422
122k
      hAacDecoder->mpsApplicable = 1;
423
122k
      errTp = TRANSPORTDEC_OK;
424
122k
      break;
425
1.84k
    default:
426
      /* especially Parsing error is critical for transport layer          */
427
1.84k
      hAacDecoder->mpsApplicable = 0;
428
1.84k
      errTp = TRANSPORTDEC_UNKOWN_ERROR;
429
167k
  }
430
431
167k
  return (INT)errTp;
432
167k
}
433
434
static INT aacDecoder_UniDrcCallback(void *handle, HANDLE_FDK_BITSTREAM hBs,
435
                                     const INT fullPayloadLength,
436
                                     const INT payloadType,
437
                                     const INT subStreamIndex,
438
                                     const INT payloadStart,
439
526k
                                     const AUDIO_OBJECT_TYPE aot) {
440
526k
  DRC_DEC_ERROR err = DRC_DEC_OK;
441
526k
  TRANSPORTDEC_ERROR errTp;
442
526k
  HANDLE_AACDECODER hAacDecoder = (HANDLE_AACDECODER)handle;
443
526k
  DRC_DEC_CODEC_MODE drcDecCodecMode = DRC_DEC_CODEC_MODE_UNDEFINED;
444
526k
  UCHAR dummyBuffer[4] = {0};
445
526k
  FDK_BITSTREAM dummyBs;
446
526k
  HANDLE_FDK_BITSTREAM hReadBs;
447
448
526k
  if (subStreamIndex != 0) {
449
0
    return TRANSPORTDEC_OK;
450
0
  }
451
452
526k
  if (hBs == NULL) {
453
    /* use dummy zero payload to clear memory */
454
396k
    hReadBs = &dummyBs;
455
396k
    FDKinitBitStream(hReadBs, dummyBuffer, 4, 24);
456
396k
  } else {
457
130k
    hReadBs = hBs;
458
130k
  }
459
460
526k
  if (aot == AOT_USAC) {
461
526k
    drcDecCodecMode = DRC_DEC_MPEG_D_USAC;
462
526k
  }
463
464
526k
  err = FDK_drcDec_SetCodecMode(hAacDecoder->hUniDrcDecoder, drcDecCodecMode);
465
526k
  if (err) return (INT)TRANSPORTDEC_UNKOWN_ERROR;
466
467
526k
  if (payloadType == 0) /* uniDrcConfig */
468
314k
  {
469
314k
    err = FDK_drcDec_ReadUniDrcConfig(hAacDecoder->hUniDrcDecoder, hReadBs);
470
314k
  } else /* loudnessInfoSet */
471
211k
  {
472
211k
    err = FDK_drcDec_ReadLoudnessInfoSet(hAacDecoder->hUniDrcDecoder, hReadBs);
473
211k
    hAacDecoder->loudnessInfoSetPosition[1] = payloadStart;
474
211k
    hAacDecoder->loudnessInfoSetPosition[2] = fullPayloadLength;
475
211k
  }
476
477
526k
  if (err == DRC_DEC_OK)
478
526k
    errTp = TRANSPORTDEC_OK;
479
0
  else
480
0
    errTp = TRANSPORTDEC_UNKOWN_ERROR;
481
482
526k
  return (INT)errTp;
483
526k
}
484
485
LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_AncDataInit(HANDLE_AACDECODER self,
486
0
                                                      UCHAR *buffer, int size) {
487
0
  CAncData *ancData = &self->ancData;
488
489
0
  return CAacDecoder_AncDataInit(ancData, buffer, size);
490
0
}
491
492
LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_AncDataGet(HANDLE_AACDECODER self,
493
                                                     int index, UCHAR **ptr,
494
0
                                                     int *size) {
495
0
  CAncData *ancData = &self->ancData;
496
497
0
  return CAacDecoder_AncDataGet(ancData, index, ptr, size);
498
0
}
499
500
/* If MPS is present in stream, but not supported by this instance, we'll
501
   have to switch off MPS and use QMF synthesis in the SBR module if required */
502
static int isSupportedMpsConfig(AUDIO_OBJECT_TYPE aot,
503
                                unsigned int numInChannels,
504
0
                                unsigned int fMpsPresent) {
505
0
  LIB_INFO libInfo[FDK_MODULE_LAST];
506
0
  UINT mpsCaps;
507
0
  int isSupportedCfg = 1;
508
509
0
  FDKinitLibInfo(libInfo);
510
511
0
  mpegSurroundDecoder_GetLibInfo(libInfo);
512
513
0
  mpsCaps = FDKlibInfo_getCapabilities(libInfo, FDK_MPSDEC);
514
515
0
  if (!(mpsCaps & CAPF_MPS_LD) && IS_LOWDELAY(aot)) {
516
    /* We got an LD AOT but MPS decoder does not support LD. */
517
0
    isSupportedCfg = 0;
518
0
  }
519
0
  if ((mpsCaps & CAPF_MPS_LD) && IS_LOWDELAY(aot) && !fMpsPresent) {
520
    /* We got an LD AOT and the MPS decoder supports it.
521
     * But LD-MPS is not explicitly signaled. */
522
0
    isSupportedCfg = 0;
523
0
  }
524
0
  if (!(mpsCaps & CAPF_MPS_USAC) && IS_USAC(aot)) {
525
    /* We got an USAC AOT but MPS decoder does not support USAC. */
526
0
    isSupportedCfg = 0;
527
0
  }
528
0
  if (!(mpsCaps & CAPF_MPS_STD) && !IS_LOWDELAY(aot) && !IS_USAC(aot)) {
529
    /* We got an GA AOT but MPS decoder does not support it. */
530
0
    isSupportedCfg = 0;
531
0
  }
532
  /* Check whether the MPS modul supports the given number of input channels: */
533
0
  switch (numInChannels) {
534
0
    case 1:
535
0
      if (!(mpsCaps & CAPF_MPS_1CH_IN)) {
536
        /* We got a one channel input to MPS decoder but it does not support it.
537
         */
538
0
        isSupportedCfg = 0;
539
0
      }
540
0
      break;
541
0
    case 2:
542
0
      if (!(mpsCaps & CAPF_MPS_2CH_IN)) {
543
        /* We got a two channel input to MPS decoder but it does not support it.
544
         */
545
0
        isSupportedCfg = 0;
546
0
      }
547
0
      break;
548
0
    case 5:
549
0
    case 6:
550
0
      if (!(mpsCaps & CAPF_MPS_6CH_IN)) {
551
        /* We got a six channel input to MPS decoder but it does not support it.
552
         */
553
0
        isSupportedCfg = 0;
554
0
      }
555
0
      break;
556
0
    default:
557
0
      isSupportedCfg = 0;
558
0
  }
559
560
0
  return (isSupportedCfg);
561
0
}
562
563
static AAC_DECODER_ERROR setConcealMethod(
564
    const HANDLE_AACDECODER self, /*!< Handle of the decoder instance */
565
55.9k
    const INT method) {
566
55.9k
  AAC_DECODER_ERROR errorStatus = AAC_DEC_OK;
567
55.9k
  CConcealParams *pConcealData = NULL;
568
55.9k
  int method_revert = 0;
569
55.9k
  HANDLE_SBRDECODER hSbrDec = NULL;
570
55.9k
  HANDLE_AAC_DRC hDrcInfo = NULL;
571
55.9k
  HANDLE_PCM_DOWNMIX hPcmDmx = NULL;
572
55.9k
  CConcealmentMethod backupMethod = ConcealMethodNone;
573
55.9k
  int backupDelay = 0;
574
55.9k
  int bsDelay = 0;
575
576
  /* check decoder handle */
577
55.9k
  if (self != NULL) {
578
55.9k
    pConcealData = &self->concealCommonData;
579
55.9k
    hSbrDec = self->hSbrDecoder;
580
55.9k
    hDrcInfo = self->hDrcInfo;
581
55.9k
    hPcmDmx = self->hPcmUtils;
582
55.9k
    if (self->flags[0] & (AC_USAC | AC_RSVD50 | AC_RSV603DA) && method >= 2) {
583
      /* Interpolation concealment is not implemented for USAC/RSVD50 */
584
      /* errorStatus = AAC_DEC_SET_PARAM_FAIL;
585
         goto bail; */
586
0
      method_revert = 1;
587
0
    }
588
55.9k
    if (self->flags[0] & (AC_USAC | AC_RSVD50 | AC_RSV603DA) && method >= 2) {
589
      /* Interpolation concealment is not implemented for USAC/RSVD50 */
590
0
      errorStatus = AAC_DEC_SET_PARAM_FAIL;
591
0
      goto bail;
592
0
    }
593
55.9k
  }
594
595
  /* Get current method/delay */
596
55.9k
  backupMethod = CConcealment_GetMethod(pConcealData);
597
55.9k
  backupDelay = CConcealment_GetDelay(pConcealData);
598
599
  /* Be sure to set AAC and SBR concealment method simultaneously! */
600
55.9k
  errorStatus = CConcealment_SetParams(
601
55.9k
      pConcealData,
602
55.9k
      (method_revert == 0) ? (int)method : (int)1,  // concealMethod
603
55.9k
      AACDEC_CONCEAL_PARAM_NOT_SPECIFIED,           // concealFadeOutSlope
604
55.9k
      AACDEC_CONCEAL_PARAM_NOT_SPECIFIED,           // concealFadeInSlope
605
55.9k
      AACDEC_CONCEAL_PARAM_NOT_SPECIFIED,           // concealMuteRelease
606
55.9k
      AACDEC_CONCEAL_PARAM_NOT_SPECIFIED            // concealComfNoiseLevel
607
55.9k
  );
608
55.9k
  if ((errorStatus != AAC_DEC_OK) && (errorStatus != AAC_DEC_INVALID_HANDLE)) {
609
0
    goto bail;
610
0
  }
611
612
  /* Get new delay */
613
55.9k
  bsDelay = CConcealment_GetDelay(pConcealData);
614
615
55.9k
  {
616
55.9k
    SBR_ERROR sbrErr = SBRDEC_OK;
617
618
    /* set SBR bitstream delay */
619
55.9k
    sbrErr = sbrDecoder_SetParam(hSbrDec, SBR_SYSTEM_BITSTREAM_DELAY, bsDelay);
620
621
55.9k
    switch (sbrErr) {
622
55.9k
      case SBRDEC_OK:
623
55.9k
      case SBRDEC_NOT_INITIALIZED:
624
55.9k
        if (self != NULL) {
625
          /* save the param value and set later
626
             (when SBR has been initialized) */
627
55.9k
          self->sbrParams.bsDelay = bsDelay;
628
55.9k
        }
629
55.9k
        break;
630
0
      default:
631
0
        errorStatus = AAC_DEC_SET_PARAM_FAIL;
632
0
        goto bail;
633
55.9k
    }
634
55.9k
  }
635
636
55.9k
  errorStatus = aacDecoder_drcSetParam(hDrcInfo, DRC_BS_DELAY, bsDelay);
637
55.9k
  if ((errorStatus != AAC_DEC_OK) && (errorStatus != AAC_DEC_INVALID_HANDLE)) {
638
0
    goto bail;
639
0
  }
640
641
55.9k
  if (errorStatus == AAC_DEC_OK) {
642
55.9k
    PCMDMX_ERROR err = pcmDmx_SetParam(hPcmDmx, DMX_BS_DATA_DELAY, bsDelay);
643
55.9k
    switch (err) {
644
0
      case PCMDMX_INVALID_HANDLE:
645
0
        errorStatus = AAC_DEC_INVALID_HANDLE;
646
0
        break;
647
55.9k
      case PCMDMX_OK:
648
55.9k
        break;
649
0
      default:
650
0
        errorStatus = AAC_DEC_SET_PARAM_FAIL;
651
0
        goto bail;
652
55.9k
    }
653
55.9k
  }
654
655
55.9k
bail:
656
55.9k
  if ((errorStatus != AAC_DEC_OK) && (errorStatus != AAC_DEC_INVALID_HANDLE)) {
657
    /* Revert to the initial state */
658
0
    CConcealment_SetParams(
659
0
        pConcealData, (int)backupMethod, AACDEC_CONCEAL_PARAM_NOT_SPECIFIED,
660
0
        AACDEC_CONCEAL_PARAM_NOT_SPECIFIED, AACDEC_CONCEAL_PARAM_NOT_SPECIFIED,
661
0
        AACDEC_CONCEAL_PARAM_NOT_SPECIFIED);
662
    /* Revert SBR bitstream delay */
663
0
    sbrDecoder_SetParam(hSbrDec, SBR_SYSTEM_BITSTREAM_DELAY, backupDelay);
664
    /* Revert DRC bitstream delay */
665
0
    aacDecoder_drcSetParam(hDrcInfo, DRC_BS_DELAY, backupDelay);
666
    /* Revert PCM mixdown bitstream delay */
667
0
    pcmDmx_SetParam(hPcmDmx, DMX_BS_DATA_DELAY, backupDelay);
668
0
  }
669
670
55.9k
  return errorStatus;
671
55.9k
}
672
673
LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_SetParam(
674
    const HANDLE_AACDECODER self, /*!< Handle of the decoder instance */
675
    const AACDEC_PARAM param,     /*!< Parameter to set               */
676
    const INT value)              /*!< Parameter valued               */
677
0
{
678
0
  AAC_DECODER_ERROR errorStatus = AAC_DEC_OK;
679
0
  HANDLE_TRANSPORTDEC hTpDec = NULL;
680
0
  TRANSPORTDEC_ERROR errTp = TRANSPORTDEC_OK;
681
0
  HANDLE_AAC_DRC hDrcInfo = NULL;
682
0
  HANDLE_PCM_DOWNMIX hPcmDmx = NULL;
683
0
  PCMDMX_ERROR dmxErr = PCMDMX_OK;
684
0
  TDLimiterPtr hPcmTdl = NULL;
685
0
  DRC_DEC_ERROR uniDrcErr = DRC_DEC_OK;
686
687
  /* check decoder handle */
688
0
  if (self != NULL) {
689
0
    hTpDec = self->hInput;
690
0
    hDrcInfo = self->hDrcInfo;
691
0
    hPcmDmx = self->hPcmUtils;
692
0
    hPcmTdl = self->hLimiter;
693
0
  } else {
694
0
    errorStatus = AAC_DEC_INVALID_HANDLE;
695
0
    goto bail;
696
0
  }
697
698
  /* configure the subsystems */
699
0
  switch (param) {
700
0
    case AAC_PCM_MIN_OUTPUT_CHANNELS:
701
0
      if (value < -1 || value > (8)) {
702
0
        return AAC_DEC_SET_PARAM_FAIL;
703
0
      }
704
0
      dmxErr = pcmDmx_SetParam(hPcmDmx, MIN_NUMBER_OF_OUTPUT_CHANNELS, value);
705
0
      break;
706
707
0
    case AAC_PCM_MAX_OUTPUT_CHANNELS:
708
0
      if (value < -1 || value > (8)) {
709
0
        return AAC_DEC_SET_PARAM_FAIL;
710
0
      }
711
0
      dmxErr = pcmDmx_SetParam(hPcmDmx, MAX_NUMBER_OF_OUTPUT_CHANNELS, value);
712
713
0
      if (dmxErr != PCMDMX_OK) {
714
0
        goto bail;
715
0
      }
716
0
      errorStatus =
717
0
          aacDecoder_drcSetParam(hDrcInfo, MAX_OUTPUT_CHANNELS, value);
718
0
      if (value > 0) {
719
0
        uniDrcErr = FDK_drcDec_SetParam(self->hUniDrcDecoder,
720
0
                                        DRC_DEC_TARGET_CHANNEL_COUNT_REQUESTED,
721
0
                                        (FIXP_DBL)value);
722
0
      }
723
0
      break;
724
725
0
    case AAC_PCM_DUAL_CHANNEL_OUTPUT_MODE:
726
0
      dmxErr = pcmDmx_SetParam(hPcmDmx, DMX_DUAL_CHANNEL_MODE, value);
727
0
      break;
728
729
0
    case AAC_PCM_LIMITER_ENABLE:
730
0
      if (value < -2 || value > 1) {
731
0
        return AAC_DEC_SET_PARAM_FAIL;
732
0
      }
733
0
      self->limiterEnableUser = value;
734
0
      break;
735
736
0
    case AAC_PCM_LIMITER_ATTACK_TIME:
737
0
      if (value <= 0) { /* module function converts value to unsigned */
738
0
        return AAC_DEC_SET_PARAM_FAIL;
739
0
      }
740
0
      switch (pcmLimiter_SetAttack(hPcmTdl, value)) {
741
0
        case TDLIMIT_OK:
742
0
          break;
743
0
        case TDLIMIT_INVALID_HANDLE:
744
0
          return AAC_DEC_INVALID_HANDLE;
745
0
        case TDLIMIT_INVALID_PARAMETER:
746
0
        default:
747
0
          return AAC_DEC_SET_PARAM_FAIL;
748
0
      }
749
0
      break;
750
751
0
    case AAC_PCM_LIMITER_RELEAS_TIME:
752
0
      if (value <= 0) { /* module function converts value to unsigned */
753
0
        return AAC_DEC_SET_PARAM_FAIL;
754
0
      }
755
0
      switch (pcmLimiter_SetRelease(hPcmTdl, value)) {
756
0
        case TDLIMIT_OK:
757
0
          break;
758
0
        case TDLIMIT_INVALID_HANDLE:
759
0
          return AAC_DEC_INVALID_HANDLE;
760
0
        case TDLIMIT_INVALID_PARAMETER:
761
0
        default:
762
0
          return AAC_DEC_SET_PARAM_FAIL;
763
0
      }
764
0
      break;
765
766
0
    case AAC_METADATA_PROFILE: {
767
0
      DMX_PROFILE_TYPE dmxProfile;
768
0
      INT mdExpiry = -1; /* in ms (-1: don't change) */
769
770
0
      switch ((AAC_MD_PROFILE)value) {
771
0
        case AAC_MD_PROFILE_MPEG_STANDARD:
772
0
          dmxProfile = DMX_PRFL_STANDARD;
773
0
          break;
774
0
        case AAC_MD_PROFILE_MPEG_LEGACY:
775
0
          dmxProfile = DMX_PRFL_MATRIX_MIX;
776
0
          break;
777
0
        case AAC_MD_PROFILE_MPEG_LEGACY_PRIO:
778
0
          dmxProfile = DMX_PRFL_FORCE_MATRIX_MIX;
779
0
          break;
780
0
        case AAC_MD_PROFILE_ARIB_JAPAN:
781
0
          dmxProfile = DMX_PRFL_ARIB_JAPAN;
782
0
          mdExpiry = 550; /* ms */
783
0
          break;
784
0
        default:
785
0
          return AAC_DEC_SET_PARAM_FAIL;
786
0
      }
787
0
      dmxErr = pcmDmx_SetParam(hPcmDmx, DMX_PROFILE_SETTING, (INT)dmxProfile);
788
0
      if (dmxErr != PCMDMX_OK) {
789
0
        goto bail;
790
0
      }
791
0
      if ((self != NULL) && (mdExpiry >= 0)) {
792
0
        self->metadataExpiry = mdExpiry;
793
        /* Determine the corresponding number of frames and configure all
794
         * related modules. */
795
0
        aacDecoder_setMetadataExpiry(self, mdExpiry);
796
0
      }
797
0
    } break;
798
799
0
    case AAC_METADATA_EXPIRY_TIME:
800
0
      if (value < 0) {
801
0
        return AAC_DEC_SET_PARAM_FAIL;
802
0
      }
803
0
      if (self != NULL) {
804
0
        self->metadataExpiry = value;
805
        /* Determine the corresponding number of frames and configure all
806
         * related modules. */
807
0
        aacDecoder_setMetadataExpiry(self, value);
808
0
      }
809
0
      break;
810
811
0
    case AAC_PCM_OUTPUT_CHANNEL_MAPPING:
812
0
      if (value < 0 || value > 1) {
813
0
        return AAC_DEC_SET_PARAM_FAIL;
814
0
      }
815
      /* CAUTION: The given value must be inverted to match the logic! */
816
0
      FDK_chMapDescr_setPassThrough(&self->mapDescr, !value);
817
0
      break;
818
819
0
    case AAC_QMF_LOWPOWER:
820
0
      if (value < -1 || value > 1) {
821
0
        return AAC_DEC_SET_PARAM_FAIL;
822
0
      }
823
824
      /**
825
       * Set QMF mode (might be overriden)
826
       *  0:HQ (complex)
827
       *  1:LP (partially complex)
828
       */
829
0
      self->qmfModeUser = (QMF_MODE)value;
830
0
      break;
831
832
0
    case AAC_DRC_ATTENUATION_FACTOR:
833
      /* DRC compression factor (where 0 is no and 127 is max compression) */
834
0
      if ((value < 0) || (value > 127)) {
835
0
        return AAC_DEC_SET_PARAM_FAIL;
836
0
      }
837
0
      errorStatus = aacDecoder_drcSetParam(hDrcInfo, DRC_CUT_SCALE, value);
838
0
      uniDrcErr = FDK_drcDec_SetParam(self->hUniDrcDecoder, DRC_DEC_COMPRESS,
839
0
                                      value * (FL2FXCONST_DBL(0.5f / 127.0f)));
840
0
      break;
841
842
0
    case AAC_DRC_BOOST_FACTOR:
843
      /* DRC boost factor (where 0 is no and 127 is max boost) */
844
0
      if ((value < 0) || (value > 127)) {
845
0
        return AAC_DEC_SET_PARAM_FAIL;
846
0
      }
847
0
      errorStatus = aacDecoder_drcSetParam(hDrcInfo, DRC_BOOST_SCALE, value);
848
0
      uniDrcErr = FDK_drcDec_SetParam(self->hUniDrcDecoder, DRC_DEC_BOOST,
849
0
                                      value * (FL2FXCONST_DBL(0.5f / 127.0f)));
850
0
      break;
851
852
0
    case AAC_DRC_REFERENCE_LEVEL:
853
0
      if ((value >= 0) &&
854
0
          ((value < 40) || (value > 127))) /* allowed range: -10 to -31.75 dB */
855
0
        return AAC_DEC_SET_PARAM_FAIL;
856
      /* DRC target reference level quantized in 0.25dB steps using values
857
         [40..127]. Negative values switch off loudness normalisation. Negative
858
         values also switch off MPEG-4 DRC, while MPEG-D DRC can be separately
859
         switched on/off with AAC_UNIDRC_SET_EFFECT */
860
0
      errorStatus = aacDecoder_drcSetParam(hDrcInfo, TARGET_REF_LEVEL, value);
861
0
      uniDrcErr = FDK_drcDec_SetParam(self->hUniDrcDecoder,
862
0
                                      DRC_DEC_LOUDNESS_NORMALIZATION_ON,
863
0
                                      (FIXP_DBL)(value >= 0));
864
      /* set target loudness also for MPEG-D DRC */
865
0
      self->defaultTargetLoudness = (SCHAR)value;
866
0
      break;
867
868
0
    case AAC_DRC_HEAVY_COMPRESSION:
869
      /* Don't need to overwrite cut/boost values */
870
0
      errorStatus =
871
0
          aacDecoder_drcSetParam(hDrcInfo, APPLY_HEAVY_COMPRESSION, value);
872
0
      break;
873
874
0
    case AAC_DRC_DEFAULT_PRESENTATION_MODE:
875
      /* DRC default presentation mode */
876
0
      errorStatus =
877
0
          aacDecoder_drcSetParam(hDrcInfo, DEFAULT_PRESENTATION_MODE, value);
878
0
      break;
879
880
0
    case AAC_DRC_ENC_TARGET_LEVEL:
881
      /* Encoder target level for light (i.e. not heavy) compression:
882
         Target reference level assumed at encoder for deriving limiting gains
883
       */
884
0
      errorStatus =
885
0
          aacDecoder_drcSetParam(hDrcInfo, ENCODER_TARGET_LEVEL, value);
886
0
      break;
887
888
0
    case AAC_UNIDRC_SET_EFFECT:
889
0
      if ((value < -1) || (value > 6)) return AAC_DEC_SET_PARAM_FAIL;
890
0
      uniDrcErr = FDK_drcDec_SetParam(self->hUniDrcDecoder, DRC_DEC_EFFECT_TYPE,
891
0
                                      (FIXP_DBL)value);
892
0
      break;
893
0
    case AAC_UNIDRC_ALBUM_MODE:
894
0
      uniDrcErr = FDK_drcDec_SetParam(self->hUniDrcDecoder, DRC_DEC_ALBUM_MODE,
895
0
                                      (FIXP_DBL)value);
896
0
      break;
897
898
0
    case AAC_TPDEC_CLEAR_BUFFER:
899
0
      errTp = transportDec_SetParam(hTpDec, TPDEC_PARAM_RESET, 1);
900
0
      self->streamInfo.numLostAccessUnits = 0;
901
0
      self->streamInfo.numBadBytes = 0;
902
0
      self->streamInfo.numTotalBytes = 0;
903
      /* aacDecoder_SignalInterruption(self); */
904
0
      break;
905
0
    case AAC_CONCEAL_METHOD:
906
      /* Changing the concealment method can introduce additional bitstream
907
         delay. And that in turn affects sub libraries and modules which makes
908
         the whole thing quite complex.  So the complete changing routine is
909
         packed into a helper function which keeps all modules and libs in a
910
         consistent state even in the case an error occures. */
911
0
      errorStatus = setConcealMethod(self, value);
912
0
      if (errorStatus == AAC_DEC_OK) {
913
0
        self->concealMethodUser = (CConcealmentMethod)value;
914
0
      }
915
0
      break;
916
917
0
    default:
918
0
      return AAC_DEC_SET_PARAM_FAIL;
919
0
  } /* switch(param) */
920
921
0
bail:
922
923
0
  if (errorStatus == AAC_DEC_OK) {
924
    /* Check error code returned by DMX module library: */
925
0
    switch (dmxErr) {
926
0
      case PCMDMX_OK:
927
0
        break;
928
0
      case PCMDMX_INVALID_HANDLE:
929
0
        errorStatus = AAC_DEC_INVALID_HANDLE;
930
0
        break;
931
0
      default:
932
0
        errorStatus = AAC_DEC_SET_PARAM_FAIL;
933
0
    }
934
0
  }
935
936
0
  if (errTp != TRANSPORTDEC_OK && errorStatus == AAC_DEC_OK) {
937
0
    errorStatus = AAC_DEC_SET_PARAM_FAIL;
938
0
  }
939
940
0
  if (errorStatus == AAC_DEC_OK) {
941
    /* Check error code returned by MPEG-D DRC decoder library: */
942
0
    switch (uniDrcErr) {
943
0
      case 0:
944
0
        break;
945
0
      case -9998:
946
0
        errorStatus = AAC_DEC_INVALID_HANDLE;
947
0
        break;
948
0
      default:
949
0
        errorStatus = AAC_DEC_SET_PARAM_FAIL;
950
0
        break;
951
0
    }
952
0
  }
953
954
0
  return (errorStatus);
955
0
}
956
LINKSPEC_CPP HANDLE_AACDECODER aacDecoder_Open(TRANSPORT_TYPE transportFmt,
957
34.7k
                                               UINT nrOfLayers) {
958
34.7k
  AAC_DECODER_INSTANCE *aacDec = NULL;
959
34.7k
  HANDLE_TRANSPORTDEC pIn;
960
34.7k
  int err = 0;
961
34.7k
  int stereoConfigIndex = -1;
962
963
34.7k
  UINT nrOfLayers_min = fMin(nrOfLayers, (UINT)1);
964
965
  /* Allocate transport layer struct. */
966
34.7k
  pIn = transportDec_Open(transportFmt, TP_FLAG_MPEG4, nrOfLayers_min);
967
34.7k
  if (pIn == NULL) {
968
33
    return NULL;
969
33
  }
970
971
  /* Allocate AAC decoder core struct. */
972
34.7k
  aacDec = CAacDecoder_Open(transportFmt);
973
974
34.7k
  if (aacDec == NULL) {
975
0
    transportDec_Close(&pIn);
976
0
    goto bail;
977
0
  }
978
34.7k
  aacDec->hInput = pIn;
979
980
34.7k
  aacDec->nrOfLayers = nrOfLayers_min;
981
982
  /* Setup channel mapping descriptor. */
983
34.7k
  FDK_chMapDescr_init(&aacDec->mapDescr, NULL, 0, 0);
984
985
  /* Register Config Update callback. */
986
34.7k
  transportDec_RegisterAscCallback(pIn, aacDecoder_ConfigCallback,
987
34.7k
                                   (void *)aacDec);
988
989
  /* Register Free Memory callback. */
990
34.7k
  transportDec_RegisterFreeMemCallback(pIn, aacDecoder_FreeMemCallback,
991
34.7k
                                       (void *)aacDec);
992
993
  /* Register config switch control callback. */
994
34.7k
  transportDec_RegisterCtrlCFGChangeCallback(
995
34.7k
      pIn, aacDecoder_CtrlCFGChangeCallback, (void *)aacDec);
996
997
34.7k
  FDKmemclear(&aacDec->qmfDomain, sizeof(FDK_QMF_DOMAIN));
998
  /* open SBR decoder */
999
34.7k
  if (SBRDEC_OK != sbrDecoder_Open(&aacDec->hSbrDecoder, &aacDec->qmfDomain)) {
1000
0
    err = -1;
1001
0
    goto bail;
1002
0
  }
1003
34.7k
  aacDec->qmfModeUser = NOT_DEFINED;
1004
34.7k
  transportDec_RegisterSbrCallback(aacDec->hInput, aacDecoder_SbrCallback,
1005
34.7k
                                   (void *)aacDec->hSbrDecoder);
1006
1007
34.7k
  if (mpegSurroundDecoder_Open(
1008
34.7k
          (CMpegSurroundDecoder **)&aacDec->pMpegSurroundDecoder,
1009
34.7k
          stereoConfigIndex, &aacDec->qmfDomain)) {
1010
0
    err = -1;
1011
0
    goto bail;
1012
0
  }
1013
  /* Set MPEG Surround defaults */
1014
34.7k
  aacDec->mpsEnableUser = 0;
1015
34.7k
  aacDec->mpsEnableCurr = 0;
1016
34.7k
  aacDec->mpsApplicable = 0;
1017
34.7k
  aacDec->mpsOutputMode = (SCHAR)SACDEC_OUT_MODE_NORMAL;
1018
34.7k
  transportDec_RegisterSscCallback(pIn, aacDecoder_SscCallback, (void *)aacDec);
1019
1020
34.7k
  {
1021
34.7k
    if (FDK_drcDec_Open(&(aacDec->hUniDrcDecoder), DRC_DEC_ALL) != 0) {
1022
0
      err = -1;
1023
0
      goto bail;
1024
0
    }
1025
34.7k
  }
1026
1027
34.7k
  transportDec_RegisterUniDrcConfigCallback(pIn, aacDecoder_UniDrcCallback,
1028
34.7k
                                            (void *)aacDec,
1029
34.7k
                                            aacDec->loudnessInfoSetPosition);
1030
34.7k
  aacDec->defaultTargetLoudness = (SCHAR)96;
1031
1032
34.7k
  pcmDmx_Open(&aacDec->hPcmUtils);
1033
34.7k
  if (aacDec->hPcmUtils == NULL) {
1034
0
    err = -1;
1035
0
    goto bail;
1036
0
  }
1037
1038
34.7k
  aacDec->hLimiter =
1039
34.7k
      pcmLimiter_Create(TDL_ATTACK_DEFAULT_MS, TDL_RELEASE_DEFAULT_MS,
1040
34.7k
                        (FIXP_DBL)MAXVAL_DBL, (8), 96000);
1041
34.7k
  if (NULL == aacDec->hLimiter) {
1042
0
    err = -1;
1043
0
    goto bail;
1044
0
  }
1045
34.7k
  aacDec->limiterEnableUser = (UCHAR)-1;
1046
34.7k
  aacDec->limiterEnableCurr = 0;
1047
1048
  /* Assure that all modules have same delay */
1049
34.7k
  if (setConcealMethod(aacDec,
1050
34.7k
                       CConcealment_GetMethod(&aacDec->concealCommonData))) {
1051
0
    err = -1;
1052
0
    goto bail;
1053
0
  }
1054
1055
34.7k
bail:
1056
34.7k
  if (err == -1) {
1057
0
    aacDecoder_Close(aacDec);
1058
0
    aacDec = NULL;
1059
0
  }
1060
34.7k
  return aacDec;
1061
34.7k
}
1062
1063
LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_Fill(HANDLE_AACDECODER self,
1064
                                               UCHAR *pBuffer[],
1065
                                               const UINT bufferSize[],
1066
28.7k
                                               UINT *pBytesValid) {
1067
28.7k
  TRANSPORTDEC_ERROR tpErr;
1068
  /* loop counter for layers; if not TT_MP4_RAWPACKETS used as index for only
1069
     available layer */
1070
28.7k
  INT layer = 0;
1071
28.7k
  INT nrOfLayers = self->nrOfLayers;
1072
1073
28.7k
  {
1074
57.4k
    for (layer = 0; layer < nrOfLayers; layer++) {
1075
28.7k
      {
1076
28.7k
        tpErr = transportDec_FillData(self->hInput, pBuffer[layer],
1077
28.7k
                                      bufferSize[layer], &pBytesValid[layer],
1078
28.7k
                                      layer);
1079
28.7k
        if (tpErr != TRANSPORTDEC_OK) {
1080
0
          return AAC_DEC_UNKNOWN; /* Must be an internal error */
1081
0
        }
1082
28.7k
      }
1083
28.7k
    }
1084
28.7k
  }
1085
1086
28.7k
  return AAC_DEC_OK;
1087
28.7k
}
1088
1089
8.28k
static void aacDecoder_SignalInterruption(HANDLE_AACDECODER self) {
1090
8.28k
  CAacDecoder_SignalInterruption(self);
1091
1092
8.28k
  if (self->hSbrDecoder != NULL) {
1093
8.28k
    sbrDecoder_SetParam(self->hSbrDecoder, SBR_BS_INTERRUPTION, 1);
1094
8.28k
  }
1095
8.28k
  if (self->mpsEnableUser) {
1096
0
    mpegSurroundDecoder_SetParam(
1097
0
        (CMpegSurroundDecoder *)self->pMpegSurroundDecoder,
1098
0
        SACDEC_BS_INTERRUPTION, 1);
1099
0
  }
1100
8.28k
}
1101
1102
static void aacDecoder_UpdateBitStreamCounters(CStreamInfo *pSi,
1103
                                               HANDLE_FDK_BITSTREAM hBs,
1104
                                               INT nBits,
1105
466k
                                               AAC_DECODER_ERROR ErrorStatus) {
1106
  /* calculate bit difference (amount of bits moved forward) */
1107
466k
  nBits = nBits - (INT)FDKgetValidBits(hBs);
1108
1109
  /* Note: The amount of bits consumed might become negative when parsing a
1110
     bit stream with several sub frames, and we find out at the last sub frame
1111
     that the total frame length does not match the sum of sub frame length.
1112
     If this happens, the transport decoder might want to rewind to the supposed
1113
     ending of the transport frame, and this position might be before the last
1114
     access unit beginning. */
1115
1116
  /* Calc bitrate. */
1117
466k
  if (pSi->frameSize > 0) {
1118
    /* bitRate = nBits * sampleRate / frameSize */
1119
454k
    int ratio_e = 0;
1120
454k
    FIXP_DBL ratio_m = fDivNorm(pSi->sampleRate, pSi->frameSize, &ratio_e);
1121
454k
    pSi->bitRate = (INT)fMultNorm(nBits, DFRACT_BITS - 1, ratio_m, ratio_e,
1122
454k
                                  DFRACT_BITS - 1);
1123
454k
  }
1124
1125
  /* bit/byte counters */
1126
466k
  {
1127
466k
    INT nBytes;
1128
1129
466k
    nBytes = nBits >> 3;
1130
466k
    pSi->numTotalBytes += nBytes;
1131
466k
    if (IS_OUTPUT_VALID(ErrorStatus)) {
1132
444k
      pSi->numTotalAccessUnits++;
1133
444k
    }
1134
466k
    if (IS_DECODE_ERROR(ErrorStatus)) {
1135
6.76k
      pSi->numBadBytes += nBytes;
1136
6.76k
      pSi->numBadAccessUnits++;
1137
6.76k
    }
1138
466k
  }
1139
466k
}
1140
1141
8.25k
static INT aacDecoder_EstimateNumberOfLostFrames(HANDLE_AACDECODER self) {
1142
8.25k
  INT n;
1143
1144
8.25k
  transportDec_GetMissingAccessUnitCount(&n, self->hInput);
1145
1146
8.25k
  return n;
1147
8.25k
}
1148
1149
LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_DecodeFrame(HANDLE_AACDECODER self,
1150
                                                      INT_PCM *pTimeData,
1151
                                                      const INT timeDataSize,
1152
466k
                                                      const UINT flags) {
1153
466k
  AAC_DECODER_ERROR ErrorStatus;
1154
466k
  INT layer;
1155
466k
  INT nBits;
1156
466k
  INT timeData2Size;
1157
466k
  INT timeData3Size;
1158
466k
  INT timeDataHeadroom;
1159
466k
  HANDLE_FDK_BITSTREAM hBs;
1160
466k
  int fTpInterruption = 0; /* Transport originated interruption detection. */
1161
466k
  int fTpConceal = 0;      /* Transport originated concealment. */
1162
466k
  UINT accessUnit = 0;
1163
466k
  UINT numAccessUnits = 1;
1164
466k
  UINT numPrerollAU = 0;
1165
466k
  int fEndAuNotAdjusted = 0; /* The end of the access unit was not adjusted */
1166
466k
  int applyCrossfade = 1;    /* flag indicates if flushing was possible */
1167
466k
  PCM_DEC *pTimeData2;
1168
466k
  PCM_AAC *pTimeData3;
1169
466k
  INT pcmLimiterScale = 0;
1170
466k
  INT interleaved = 0;
1171
1172
466k
  if (self == NULL) {
1173
0
    return AAC_DEC_INVALID_HANDLE;
1174
0
  }
1175
1176
466k
  if (flags & AACDEC_INTR) {
1177
0
    self->streamInfo.numLostAccessUnits = 0;
1178
0
  }
1179
466k
  hBs = transportDec_GetBitstream(self->hInput, 0);
1180
1181
  /* Get current bits position for bitrate calculation. */
1182
466k
  nBits = FDKgetValidBits(hBs);
1183
1184
466k
  if (flags & AACDEC_CLRHIST) {
1185
0
    if (self->flags[0] & AC_USAC) {
1186
      /* 1) store AudioSpecificConfig always in AudioSpecificConfig_Parse() */
1187
      /* 2) free memory of dynamic allocated data */
1188
0
      CSAudioSpecificConfig asc;
1189
0
      transportDec_GetAsc(self->hInput, 0, &asc);
1190
0
      aacDecoder_FreeMemCallback(self, &asc);
1191
0
      self->streamInfo.numChannels = 0;
1192
      /* 3) restore AudioSpecificConfig */
1193
0
      if (asc.configBits <= (TP_USAC_MAX_CONFIG_LEN << 3)) {
1194
0
        transportDec_OutOfBandConfig(self->hInput, asc.config,
1195
0
                                     (asc.configBits + 7) >> 3, 0);
1196
0
      }
1197
0
    }
1198
0
  }
1199
1200
466k
  if (!((flags & (AACDEC_CONCEAL | AACDEC_FLUSH)) ||
1201
466k
        (self->flushStatus == AACDEC_RSV60_DASH_IPF_ATSC_FLUSH_ON) ||
1202
466k
        (self->flushStatus == AACDEC_USAC_DASH_IPF_FLUSH_ON) ||
1203
466k
        (self->buildUpStatus == AACDEC_RSV60_BUILD_UP_IDLE_IN_BAND))) {
1204
466k
    TRANSPORTDEC_ERROR err;
1205
1206
923k
    for (layer = 0; layer < self->nrOfLayers; layer++) {
1207
466k
      err = transportDec_ReadAccessUnit(self->hInput, layer);
1208
466k
      if (err != TRANSPORTDEC_OK) {
1209
17.3k
        switch (err) {
1210
9.08k
          case TRANSPORTDEC_NOT_ENOUGH_BITS:
1211
9.08k
            ErrorStatus = AAC_DEC_NOT_ENOUGH_BITS;
1212
9.08k
            goto bail;
1213
8.25k
          case TRANSPORTDEC_SYNC_ERROR:
1214
8.25k
            self->streamInfo.numLostAccessUnits =
1215
8.25k
                aacDecoder_EstimateNumberOfLostFrames(self);
1216
8.25k
            fTpInterruption = 1;
1217
8.25k
            break;
1218
0
          case TRANSPORTDEC_NEED_TO_RESTART:
1219
0
            ErrorStatus = AAC_DEC_NEED_TO_RESTART;
1220
0
            goto bail;
1221
0
          case TRANSPORTDEC_CRC_ERROR:
1222
0
            fTpConceal = 1;
1223
0
            break;
1224
0
          case TRANSPORTDEC_UNSUPPORTED_FORMAT:
1225
0
            ErrorStatus = AAC_DEC_UNSUPPORTED_FORMAT;
1226
0
            goto bail;
1227
0
          default:
1228
0
            ErrorStatus = AAC_DEC_UNKNOWN;
1229
0
            goto bail;
1230
17.3k
        }
1231
17.3k
      }
1232
466k
    }
1233
466k
  } else {
1234
0
    if (self->streamInfo.numLostAccessUnits > 0) {
1235
0
      self->streamInfo.numLostAccessUnits--;
1236
0
    }
1237
0
  }
1238
1239
457k
  self->frameOK = 1;
1240
1241
457k
  UINT prerollAUOffset[AACDEC_MAX_NUM_PREROLL_AU];
1242
457k
  UINT prerollAULength[AACDEC_MAX_NUM_PREROLL_AU];
1243
2.28M
  for (int i = 0; i < AACDEC_MAX_NUM_PREROLL_AU + 1; i++)
1244
1.82M
    self->prerollAULength[i] = 0;
1245
1246
457k
  INT auStartAnchor;
1247
457k
  HANDLE_FDK_BITSTREAM hBsAu;
1248
1249
  /* Process preroll frames and current frame */
1250
459k
  do {
1251
459k
    if (!(flags & (AACDEC_CONCEAL | AACDEC_FLUSH)) &&
1252
459k
        (self->flushStatus != AACDEC_RSV60_CFG_CHANGE_ATSC_FLUSH_ON) &&
1253
459k
        (accessUnit == 0) &&
1254
457k
        (self->hasAudioPreRoll ||
1255
452k
         (self->buildUpStatus == AACDEC_RSV60_BUILD_UP_IDLE_IN_BAND)) &&
1256
5.14k
        !fTpInterruption &&
1257
4.52k
        !fTpConceal /* Bit stream pointer needs to be at the beginning of a
1258
                       (valid) AU. */
1259
459k
    ) {
1260
4.52k
      ErrorStatus = CAacDecoder_PreRollExtensionPayloadParse(
1261
4.52k
          self, &numPrerollAU, prerollAUOffset, prerollAULength);
1262
1263
4.52k
      if (ErrorStatus != AAC_DEC_OK) {
1264
77
        switch (ErrorStatus) {
1265
5
          case AAC_DEC_NOT_ENOUGH_BITS:
1266
5
            goto bail;
1267
72
          case AAC_DEC_PARSE_ERROR:
1268
72
            self->frameOK = 0;
1269
72
            break;
1270
0
          default:
1271
0
            break;
1272
77
        }
1273
77
      }
1274
1275
4.52k
      numAccessUnits += numPrerollAU;
1276
4.52k
    }
1277
1278
459k
    hBsAu = transportDec_GetBitstream(self->hInput, 0);
1279
459k
    auStartAnchor = (INT)FDKgetValidBits(hBsAu);
1280
1281
459k
    self->accessUnit = accessUnit;
1282
459k
    if (accessUnit < numPrerollAU) {
1283
2.47k
      FDKpushFor(hBsAu, prerollAUOffset[accessUnit]);
1284
2.47k
    }
1285
1286
    /* Signal bit stream interruption to other modules if required. */
1287
459k
    if (fTpInterruption || ((flags & AACDEC_INTR) && (accessUnit == 0))) {
1288
8.25k
      aacDecoder_SignalInterruption(self);
1289
8.25k
      if (!((flags & AACDEC_INTR) && (accessUnit == 0))) {
1290
8.25k
        ErrorStatus = AAC_DEC_TRANSPORT_SYNC_ERROR;
1291
8.25k
        goto bail;
1292
8.25k
      }
1293
8.25k
    }
1294
1295
    /* Clearing core data will be done in CAacDecoder_DecodeFrame() below.
1296
       Tell other modules to clear states if required. */
1297
451k
    if (flags & AACDEC_CLRHIST) {
1298
0
      if (!(self->flags[0] & AC_USAC)) {
1299
0
        sbrDecoder_SetParam(self->hSbrDecoder, SBR_CLEAR_HISTORY, 1);
1300
0
        mpegSurroundDecoder_SetParam(
1301
0
            (CMpegSurroundDecoder *)self->pMpegSurroundDecoder,
1302
0
            SACDEC_CLEAR_HISTORY, 1);
1303
0
        if (FDK_QmfDomain_ClearPersistentMemory(&self->qmfDomain) != 0) {
1304
0
          ErrorStatus = AAC_DEC_UNKNOWN;
1305
0
          goto bail;
1306
0
        }
1307
0
      }
1308
0
    }
1309
1310
    /* Empty bit buffer in case of flush request. */
1311
451k
    if (flags & AACDEC_FLUSH && !(flags & AACDEC_CONCEAL)) {
1312
0
      if (!self->flushStatus) {
1313
0
        transportDec_SetParam(self->hInput, TPDEC_PARAM_RESET, 1);
1314
0
        self->streamInfo.numLostAccessUnits = 0;
1315
0
        self->streamInfo.numBadBytes = 0;
1316
0
        self->streamInfo.numTotalBytes = 0;
1317
0
      }
1318
0
    }
1319
    /* Reset the output delay field. The modules will add their figures one
1320
     * after another. */
1321
451k
    self->streamInfo.outputDelay = 0;
1322
1323
451k
    if (self->limiterEnableUser == (UCHAR)-2) {
1324
      /* Enable limiter only for RSVD60. */
1325
0
      self->limiterEnableCurr = (self->flags[0] & AC_RSV603DA) ? 1 : 0;
1326
451k
    } else if (self->limiterEnableUser == (UCHAR)-1) {
1327
      /* Enable limiter for all non-lowdelay AOT's. */
1328
451k
      self->limiterEnableCurr = (self->flags[0] & (AC_LD | AC_ELD)) ? 0 : 1;
1329
451k
    } else {
1330
      /* Use limiter configuration as requested. */
1331
0
      self->limiterEnableCurr = self->limiterEnableUser;
1332
0
    }
1333
1334
    /* reset DRC level normalization gain on a per frame basis */
1335
451k
    self->extGain[0] = AACDEC_DRC_GAIN_INIT_VALUE;
1336
1337
451k
    pTimeData2 = self->pTimeData2;
1338
451k
    timeData2Size = self->timeData2Size / sizeof(PCM_DEC);
1339
451k
    pTimeData3 = (PCM_AAC *)self->pTimeData2;
1340
451k
    timeData3Size = self->timeData2Size / sizeof(PCM_AAC);
1341
1342
451k
    ErrorStatus = CAacDecoder_DecodeFrame(
1343
451k
        self,
1344
451k
        flags | (fTpConceal ? AACDEC_CONCEAL : 0) |
1345
451k
            ((self->flushStatus && !(flags & AACDEC_CONCEAL)) ? AACDEC_FLUSH
1346
451k
                                                              : 0),
1347
451k
        pTimeData2 + 0, timeData2Size, self->streamInfo.aacSamplesPerFrame + 0);
1348
1349
451k
    timeDataHeadroom = self->aacOutDataHeadroom;
1350
1351
    /* if flushing for USAC DASH IPF was not possible go on with decoding
1352
     * preroll */
1353
451k
    if ((self->flags[0] & AC_USAC) &&
1354
270k
        (self->flushStatus == AACDEC_USAC_DASH_IPF_FLUSH_ON) &&
1355
31
        !(flags & AACDEC_CONCEAL) && (ErrorStatus != AAC_DEC_OK)) {
1356
0
      applyCrossfade = 0;
1357
0
    } else /* USAC DASH IPF flushing possible begin */
1358
451k
    {
1359
451k
      if (!((flags & (AACDEC_CONCEAL | AACDEC_FLUSH)) || fTpConceal ||
1360
451k
            self->flushStatus) &&
1361
451k
          (!(IS_OUTPUT_VALID(ErrorStatus)) || !(accessUnit < numPrerollAU))) {
1362
449k
        TRANSPORTDEC_ERROR tpErr;
1363
449k
        tpErr = transportDec_EndAccessUnit(self->hInput);
1364
449k
        if (tpErr != TRANSPORTDEC_OK) {
1365
116k
          self->frameOK = 0;
1366
116k
        }
1367
449k
      } else { /* while preroll processing later possibly an error in the
1368
                  renderer part occurrs */
1369
2.50k
        if (IS_OUTPUT_VALID(ErrorStatus)) {
1370
2.50k
          fEndAuNotAdjusted = 1;
1371
2.50k
        }
1372
2.50k
      }
1373
1374
      /* If the current pTimeData2 does not contain a valid signal, there
1375
       * nothing else we can do, so bail. */
1376
451k
      if (!IS_OUTPUT_VALID(ErrorStatus)) {
1377
4.59k
        goto bail;
1378
4.59k
      }
1379
1380
447k
      {
1381
447k
        self->streamInfo.sampleRate = self->streamInfo.aacSampleRate;
1382
447k
        self->streamInfo.frameSize = self->streamInfo.aacSamplesPerFrame;
1383
447k
      }
1384
1385
447k
      self->streamInfo.numChannels = self->streamInfo.aacNumChannels;
1386
1387
447k
      {
1388
447k
        FDK_Delay_Apply(
1389
447k
            &self->usacResidualDelay,
1390
447k
            pTimeData2 + 1 * (self->streamInfo.aacSamplesPerFrame + 0) + 0,
1391
447k
            self->streamInfo.frameSize, 0);
1392
447k
      }
1393
1394
      /* Setting of internal MPS state; may be reset in CAacDecoder_SyncQmfMode
1395
         if decoder is unable to decode with user defined qmfMode */
1396
447k
      if (!(self->flags[0] & (AC_USAC | AC_RSVD50 | AC_RSV603DA | AC_ELD))) {
1397
59.1k
        self->mpsEnableCurr =
1398
59.1k
            (self->mpsEnableUser &&
1399
0
             isSupportedMpsConfig(self->streamInfo.aot,
1400
0
                                  self->streamInfo.numChannels,
1401
0
                                  (self->flags[0] & AC_MPS_PRESENT) ? 1 : 0));
1402
59.1k
      }
1403
1404
447k
      if (!self->qmfDomain.globalConf.qmfDomainExplicitConfig &&
1405
154k
          self->mpsEnableCurr) {
1406
        /* if not done yet, allocate full MPEG Surround decoder instance */
1407
1.88k
        if (mpegSurroundDecoder_IsFullMpegSurroundDecoderInstanceAvailable(
1408
1.88k
                (CMpegSurroundDecoder *)self->pMpegSurroundDecoder) ==
1409
1.88k
            SAC_INSTANCE_NOT_FULL_AVAILABLE) {
1410
613
          if (mpegSurroundDecoder_Open(
1411
613
                  (CMpegSurroundDecoder **)&self->pMpegSurroundDecoder, -1,
1412
613
                  &self->qmfDomain)) {
1413
0
            return AAC_DEC_OUT_OF_MEMORY;
1414
0
          }
1415
613
        }
1416
1.88k
      }
1417
1418
447k
      CAacDecoder_SyncQmfMode(self);
1419
1420
447k
      if (!self->qmfDomain.globalConf.qmfDomainExplicitConfig &&
1421
154k
          self->mpsEnableCurr) {
1422
1.88k
        SAC_INPUT_CONFIG sac_interface = (self->sbrEnabled && self->hSbrDecoder)
1423
1.88k
                                             ? SAC_INTERFACE_QMF
1424
1.88k
                                             : SAC_INTERFACE_TIME;
1425
        /* needs to be done before first SBR apply. */
1426
1.88k
        mpegSurroundDecoder_ConfigureQmfDomain(
1427
1.88k
            (CMpegSurroundDecoder *)self->pMpegSurroundDecoder, sac_interface,
1428
1.88k
            (UINT)self->streamInfo.aacSampleRate, self->streamInfo.aot);
1429
1.88k
        if (self->qmfDomain.globalConf.nBandsAnalysis_requested > 0) {
1430
1.88k
          self->qmfDomain.globalConf.nQmfTimeSlots_requested =
1431
1.88k
              self->streamInfo.aacSamplesPerFrame /
1432
1.88k
              self->qmfDomain.globalConf.nBandsAnalysis_requested;
1433
1.88k
        } else {
1434
0
          self->qmfDomain.globalConf.nQmfTimeSlots_requested = 0;
1435
0
        }
1436
1.88k
      }
1437
1438
447k
      switch (FDK_QmfDomain_Configure(&self->qmfDomain)) {
1439
0
        default:
1440
3
        case QMF_DOMAIN_INIT_ERROR:
1441
3
          ErrorStatus = AAC_DEC_UNKNOWN;
1442
3
          goto bail;
1443
0
        case QMF_DOMAIN_OUT_OF_MEMORY:
1444
0
          ErrorStatus = AAC_DEC_OUT_OF_MEMORY;
1445
0
          goto bail;
1446
446k
        case QMF_DOMAIN_OK:
1447
446k
          break;
1448
447k
      }
1449
1450
      /* sbr decoder */
1451
1452
446k
      if ((ErrorStatus != AAC_DEC_OK) || (flags & AACDEC_CONCEAL) ||
1453
439k
          self->pAacDecoderStaticChannelInfo[0]->concealmentInfo.concealState >
1454
439k
              ConcealState_FadeIn) {
1455
7.15k
        self->frameOK = 0; /* if an error has occured do concealment in the SBR
1456
                              decoder too */
1457
7.15k
      }
1458
1459
446k
      if (self->sbrEnabled && (!(self->flags[0] & AC_USAC_SCFGI3))) {
1460
287k
        SBR_ERROR sbrError = SBRDEC_OK;
1461
287k
        int chIdx, numCoreChannel = self->streamInfo.numChannels;
1462
1463
        /* set params */
1464
287k
        sbrDecoder_SetParam(self->hSbrDecoder, SBR_SYSTEM_BITSTREAM_DELAY,
1465
287k
                            self->sbrParams.bsDelay);
1466
287k
        sbrDecoder_SetParam(
1467
287k
            self->hSbrDecoder, SBR_FLUSH_DATA,
1468
287k
            (flags & AACDEC_FLUSH) |
1469
287k
                ((self->flushStatus && !(flags & AACDEC_CONCEAL)) ? AACDEC_FLUSH
1470
287k
                                                                  : 0));
1471
1472
287k
        if (self->streamInfo.aot == AOT_ER_AAC_ELD) {
1473
          /* Configure QMF */
1474
43.5k
          sbrDecoder_SetParam(self->hSbrDecoder, SBR_LD_QMF_TIME_ALIGN,
1475
43.5k
                              (self->flags[0] & AC_MPS_PRESENT) ? 1 : 0);
1476
43.5k
        }
1477
1478
287k
        {
1479
287k
          PCMDMX_ERROR dmxErr;
1480
287k
          INT maxOutCh = 0;
1481
1482
287k
          dmxErr = pcmDmx_GetParam(self->hPcmUtils,
1483
287k
                                   MAX_NUMBER_OF_OUTPUT_CHANNELS, &maxOutCh);
1484
287k
          if ((dmxErr == PCMDMX_OK) && (maxOutCh == 1)) {
1485
            /* Disable PS processing if we have to create a mono output signal.
1486
             */
1487
0
            self->psPossible = 0;
1488
0
          }
1489
287k
        }
1490
1491
287k
        sbrDecoder_SetParam(self->hSbrDecoder, SBR_SKIP_QMF,
1492
287k
                            (self->mpsEnableCurr) ? 2 : 0);
1493
1494
287k
        PCM_AAC *input;
1495
287k
        input = (PCM_AAC *)self->workBufferCore2;
1496
287k
        FDKmemcpy(input, pTimeData3,
1497
287k
                  sizeof(PCM_AAC) * (self->streamInfo.numChannels) *
1498
287k
                      (self->streamInfo.frameSize));
1499
1500
        /* apply SBR processing */
1501
287k
        sbrError = sbrDecoder_Apply(
1502
287k
            self->hSbrDecoder, input, pTimeData3, timeData3Size,
1503
287k
            &self->streamInfo.numChannels, &self->streamInfo.sampleRate,
1504
287k
            &self->mapDescr, self->chMapIndex, self->frameOK, &self->psPossible,
1505
287k
            self->aacOutDataHeadroom, &timeDataHeadroom);
1506
1507
287k
        if (sbrError == SBRDEC_OK) {
1508
          /* Update data in streaminfo structure. Assume that the SBR upsampling
1509
             factor is either 1, 2, 8/3 or 4. Maximum upsampling factor is 4
1510
             (CELP+SBR or USAC 4:1 SBR) */
1511
287k
          self->flags[0] |= AC_SBR_PRESENT;
1512
287k
          if (self->streamInfo.aacSampleRate != self->streamInfo.sampleRate) {
1513
263k
            if (self->streamInfo.aacSampleRate >> 2 ==
1514
263k
                self->streamInfo.sampleRate) {
1515
0
              self->streamInfo.frameSize =
1516
0
                  self->streamInfo.aacSamplesPerFrame >> 2;
1517
0
              self->streamInfo.outputDelay = self->streamInfo.outputDelay >> 2;
1518
263k
            } else if (self->streamInfo.aacSampleRate >> 1 ==
1519
263k
                       self->streamInfo.sampleRate) {
1520
0
              self->streamInfo.frameSize =
1521
0
                  self->streamInfo.aacSamplesPerFrame >> 1;
1522
0
              self->streamInfo.outputDelay = self->streamInfo.outputDelay >> 1;
1523
263k
            } else if (self->streamInfo.aacSampleRate << 1 ==
1524
263k
                       self->streamInfo.sampleRate) {
1525
171k
              self->streamInfo.frameSize = self->streamInfo.aacSamplesPerFrame
1526
171k
                                           << 1;
1527
171k
              self->streamInfo.outputDelay = self->streamInfo.outputDelay << 1;
1528
171k
            } else if (self->streamInfo.aacSampleRate << 2 ==
1529
92.6k
                       self->streamInfo.sampleRate) {
1530
18.0k
              self->streamInfo.frameSize = self->streamInfo.aacSamplesPerFrame
1531
18.0k
                                           << 2;
1532
18.0k
              self->streamInfo.outputDelay = self->streamInfo.outputDelay << 2;
1533
74.5k
            } else if (self->streamInfo.frameSize == 768) {
1534
74.5k
              self->streamInfo.frameSize =
1535
74.5k
                  (self->streamInfo.aacSamplesPerFrame << 3) / 3;
1536
74.5k
              self->streamInfo.outputDelay =
1537
74.5k
                  (self->streamInfo.outputDelay << 3) / 3;
1538
74.5k
            } else {
1539
11
              ErrorStatus = AAC_DEC_SET_PARAM_FAIL;
1540
11
              goto bail;
1541
11
            }
1542
263k
          } else {
1543
23.5k
            self->streamInfo.frameSize = self->streamInfo.aacSamplesPerFrame;
1544
23.5k
          }
1545
287k
          self->streamInfo.outputDelay +=
1546
287k
              sbrDecoder_GetDelay(self->hSbrDecoder);
1547
1548
287k
          if (self->psPossible) {
1549
4.24k
            self->flags[0] |= AC_PS_PRESENT;
1550
4.24k
          }
1551
294k
          for (chIdx = numCoreChannel; chIdx < self->streamInfo.numChannels;
1552
287k
               chIdx += 1) {
1553
6.95k
            self->channelType[chIdx] = ACT_FRONT;
1554
6.95k
            self->channelIndices[chIdx] = chIdx;
1555
6.95k
          }
1556
287k
        }
1557
287k
        if (sbrError == SBRDEC_OUTPUT_BUFFER_TOO_SMALL) {
1558
0
          ErrorStatus = AAC_DEC_OUTPUT_BUFFER_TOO_SMALL;
1559
0
          goto bail;
1560
0
        }
1561
287k
      }
1562
1563
446k
      if (self->mpsEnableCurr) {
1564
214k
        int err, sac_interface, nChannels, frameSize;
1565
1566
214k
        nChannels = self->streamInfo.numChannels;
1567
214k
        frameSize = self->streamInfo.frameSize;
1568
214k
        sac_interface = SAC_INTERFACE_TIME;
1569
1570
214k
        if (self->sbrEnabled && self->hSbrDecoder)
1571
152k
          sac_interface = SAC_INTERFACE_QMF;
1572
214k
        if (self->streamInfo.aot == AOT_USAC) {
1573
150k
          if (self->flags[0] & AC_USAC_SCFGI3) {
1574
17.0k
            sac_interface = SAC_INTERFACE_TIME;
1575
17.0k
          }
1576
150k
        }
1577
214k
        err = mpegSurroundDecoder_SetParam(
1578
214k
            (CMpegSurroundDecoder *)self->pMpegSurroundDecoder,
1579
214k
            SACDEC_INTERFACE, sac_interface);
1580
1581
214k
        if (err == 0) {
1582
214k
          err = mpegSurroundDecoder_Apply(
1583
214k
              (CMpegSurroundDecoder *)self->pMpegSurroundDecoder,
1584
214k
              (PCM_AAC *)self->workBufferCore2, pTimeData3, timeData3Size,
1585
214k
              self->streamInfo.aacSamplesPerFrame, &nChannels, &frameSize,
1586
214k
              self->streamInfo.sampleRate, self->streamInfo.aot,
1587
214k
              self->channelType, self->channelIndices, &self->mapDescr,
1588
214k
              self->aacOutDataHeadroom, &timeDataHeadroom);
1589
214k
        }
1590
1591
214k
        if (err == MPS_OUTPUT_BUFFER_TOO_SMALL) {
1592
0
          ErrorStatus = AAC_DEC_OUTPUT_BUFFER_TOO_SMALL;
1593
0
          goto bail;
1594
0
        }
1595
214k
        if (err == 0) {
1596
          /* Update output parameter */
1597
214k
          self->streamInfo.numChannels = nChannels;
1598
214k
          self->streamInfo.frameSize = frameSize;
1599
214k
          self->streamInfo.outputDelay += mpegSurroundDecoder_GetDelay(
1600
214k
              (CMpegSurroundDecoder *)self->pMpegSurroundDecoder);
1601
          /* Save current parameter for possible concealment of next frame */
1602
214k
          self->mpsOutChannelsLast = nChannels;
1603
214k
          self->mpsFrameSizeLast = frameSize;
1604
214k
        } else if ((self->mpsOutChannelsLast > 0) &&
1605
3
                   (self->mpsFrameSizeLast > 0)) {
1606
          /* Restore parameters of last frame ... */
1607
3
          self->streamInfo.numChannels = self->mpsOutChannelsLast;
1608
3
          self->streamInfo.frameSize = self->mpsFrameSizeLast;
1609
          /* ... and clear output buffer so that potentially corrupted data does
1610
           * not reach the framework. */
1611
3
          FDKmemclear(pTimeData3, self->mpsOutChannelsLast *
1612
3
                                      self->mpsFrameSizeLast * sizeof(PCM_AAC));
1613
          /* Additionally proclaim that this frame had errors during decoding.
1614
           */
1615
3
          ErrorStatus = AAC_DEC_DECODE_FRAME_ERROR;
1616
3
        } else {
1617
0
          ErrorStatus = AAC_DEC_UNKNOWN; /* no output */
1618
0
        }
1619
214k
      }
1620
1621
      /* SBR decoder for Unified Stereo Config (stereoConfigIndex == 3) */
1622
1623
446k
      if (self->sbrEnabled && (self->flags[0] & AC_USAC_SCFGI3)) {
1624
17.0k
        SBR_ERROR sbrError = SBRDEC_OK;
1625
1626
        /* set params */
1627
17.0k
        sbrDecoder_SetParam(self->hSbrDecoder, SBR_SYSTEM_BITSTREAM_DELAY,
1628
17.0k
                            self->sbrParams.bsDelay);
1629
17.0k
        sbrDecoder_SetParam(
1630
17.0k
            self->hSbrDecoder, SBR_FLUSH_DATA,
1631
17.0k
            (flags & AACDEC_FLUSH) |
1632
17.0k
                ((self->flushStatus && !(flags & AACDEC_CONCEAL)) ? AACDEC_FLUSH
1633
17.0k
                                                                  : 0));
1634
1635
17.0k
        sbrDecoder_SetParam(self->hSbrDecoder, SBR_SKIP_QMF, 1);
1636
1637
        /* apply SBR processing */
1638
17.0k
        sbrError = sbrDecoder_Apply(
1639
17.0k
            self->hSbrDecoder, pTimeData3, pTimeData3, timeData3Size,
1640
17.0k
            &self->streamInfo.numChannels, &self->streamInfo.sampleRate,
1641
17.0k
            &self->mapDescr, self->chMapIndex, self->frameOK, &self->psPossible,
1642
17.0k
            self->aacOutDataHeadroom, &timeDataHeadroom);
1643
1644
17.0k
        if (sbrError == SBRDEC_OK) {
1645
          /* Update data in streaminfo structure. Assume that the SBR upsampling
1646
           * factor is either 1,2 or 4 */
1647
17.0k
          self->flags[0] |= AC_SBR_PRESENT;
1648
17.0k
          if (self->streamInfo.aacSampleRate != self->streamInfo.sampleRate) {
1649
17.0k
            if (self->streamInfo.frameSize == 768) {
1650
2.88k
              self->streamInfo.frameSize =
1651
2.88k
                  (self->streamInfo.aacSamplesPerFrame * 8) / 3;
1652
14.1k
            } else if (self->streamInfo.aacSampleRate << 2 ==
1653
14.1k
                       self->streamInfo.sampleRate) {
1654
10.7k
              self->streamInfo.frameSize = self->streamInfo.aacSamplesPerFrame
1655
10.7k
                                           << 2;
1656
10.7k
            } else {
1657
3.41k
              self->streamInfo.frameSize = self->streamInfo.aacSamplesPerFrame
1658
3.41k
                                           << 1;
1659
3.41k
            }
1660
17.0k
          }
1661
1662
17.0k
          self->flags[0] &= ~AC_PS_PRESENT;
1663
17.0k
        }
1664
17.0k
        if (sbrError == SBRDEC_OUTPUT_BUFFER_TOO_SMALL) {
1665
0
          ErrorStatus = AAC_DEC_OUTPUT_BUFFER_TOO_SMALL;
1666
0
          goto bail;
1667
0
        }
1668
17.0k
      }
1669
1670
446k
      {
1671
446k
        if ((INT)PCM_OUT_HEADROOM != timeDataHeadroom) {
1672
80.2k
          for (int i = ((self->streamInfo.frameSize *
1673
80.2k
                         self->streamInfo.numChannels) -
1674
80.2k
                        1);
1675
127M
               i >= 0; i--) {
1676
127M
            pTimeData2[i] =
1677
127M
                (PCM_DEC)pTimeData3[i] >> (PCM_OUT_HEADROOM - timeDataHeadroom);
1678
127M
          }
1679
80.2k
        }
1680
446k
      }
1681
1682
446k
      {
1683
446k
        if ((FDK_drcDec_GetParam(self->hUniDrcDecoder, DRC_DEC_IS_ACTIVE)) &&
1684
26.3k
            (self->flags[0] & AC_USAC)) {
1685
          /* Apply DRC gains*/
1686
24.2k
          int ch, drcDelay = 0;
1687
24.2k
          int needsDeinterleaving = 0;
1688
24.2k
          FIXP_DBL *drcWorkBuffer = NULL;
1689
24.2k
          FIXP_DBL channelGain[(8)];
1690
24.2k
          int reverseInChannelMap[(8)];
1691
24.2k
          int reverseOutChannelMap[(8)];
1692
24.2k
          FDKmemclear(channelGain, sizeof(channelGain));
1693
217k
          for (ch = 0; ch < (8); ch++) {
1694
193k
            reverseInChannelMap[ch] = ch;
1695
193k
            reverseOutChannelMap[ch] = ch;
1696
193k
          }
1697
1698
          /* Update sampleRate and frameSize. This may be necessary in case of
1699
           * implicit SBR signaling */
1700
24.2k
          FDK_drcDec_SetParam(self->hUniDrcDecoder, DRC_DEC_SAMPLE_RATE,
1701
24.2k
                              self->streamInfo.sampleRate);
1702
24.2k
          FDK_drcDec_SetParam(self->hUniDrcDecoder, DRC_DEC_FRAME_SIZE,
1703
24.2k
                              self->streamInfo.frameSize);
1704
1705
          /* If SBR and/or MPS is active, the DRC gains are aligned to the QMF
1706
             domain signal before the QMF synthesis. Therefore the DRC gains
1707
             need to be delayed by the QMF synthesis delay. */
1708
24.2k
          if (self->sbrEnabled) drcDelay = 257;
1709
24.2k
          if (self->mpsEnableCurr) drcDelay = 257;
1710
          /* Take into account concealment delay */
1711
24.2k
          drcDelay += CConcealment_GetDelay(&self->concealCommonData) *
1712
24.2k
                      self->streamInfo.frameSize;
1713
1714
          /* The output of SBR and MPS is interleaved. Deinterleaving may be
1715
           * necessary for FDK_drcDec_ProcessTime, which accepts deinterleaved
1716
           * audio only. */
1717
24.2k
          if ((self->streamInfo.numChannels > 1) &&
1718
1.36k
              (0 || (self->sbrEnabled) || (self->mpsEnableCurr))) {
1719
            /* interleaving/deinterleaving is performed on upper part of
1720
             * pTimeData2. Check if this buffer is large enough. */
1721
1.29k
            if (timeData2Size < (INT)(2 * self->streamInfo.numChannels *
1722
1.29k
                                      self->streamInfo.frameSize)) {
1723
0
              ErrorStatus = AAC_DEC_UNKNOWN;
1724
0
              goto bail;
1725
0
            }
1726
1.29k
            needsDeinterleaving = 1;
1727
1.29k
            drcWorkBuffer =
1728
1.29k
                (FIXP_DBL *)pTimeData2 +
1729
1.29k
                self->streamInfo.numChannels * self->streamInfo.frameSize;
1730
1.29k
            FDK_deinterleave(
1731
1.29k
                pTimeData2, drcWorkBuffer, self->streamInfo.numChannels,
1732
1.29k
                self->streamInfo.frameSize, self->streamInfo.frameSize);
1733
22.9k
          } else {
1734
22.9k
            drcWorkBuffer = pTimeData2;
1735
22.9k
          }
1736
1737
          /* prepare Loudness Normalisation gain */
1738
24.2k
          FDK_drcDec_SetParam(self->hUniDrcDecoder, DRC_DEC_TARGET_LOUDNESS,
1739
24.2k
                              (INT)-self->defaultTargetLoudness *
1740
24.2k
                                  FL2FXCONST_DBL(1.0f / (float)(1 << 9)));
1741
24.2k
          FDK_drcDec_SetChannelGains(self->hUniDrcDecoder,
1742
24.2k
                                     self->streamInfo.numChannels,
1743
24.2k
                                     self->streamInfo.frameSize, channelGain,
1744
24.2k
                                     drcWorkBuffer, self->streamInfo.frameSize);
1745
24.2k
          FDK_drcDec_Preprocess(self->hUniDrcDecoder);
1746
1747
          /* apply DRC1 gain sequence */
1748
24.2k
          FDK_drcDec_ProcessTime(self->hUniDrcDecoder, drcDelay, DRC_DEC_DRC1,
1749
24.2k
                                 0, 0, self->streamInfo.numChannels,
1750
24.2k
                                 drcWorkBuffer, self->streamInfo.frameSize);
1751
          /* apply downmix */
1752
24.2k
          FDK_drcDec_ApplyDownmix(
1753
24.2k
              self->hUniDrcDecoder, reverseInChannelMap, reverseOutChannelMap,
1754
24.2k
              drcWorkBuffer,
1755
24.2k
              &self->streamInfo.numChannels); /* self->streamInfo.numChannels
1756
                                                 may change here */
1757
          /* apply DRC2/3 gain sequence */
1758
24.2k
          FDK_drcDec_ProcessTime(self->hUniDrcDecoder, drcDelay,
1759
24.2k
                                 DRC_DEC_DRC2_DRC3, 0, 0,
1760
24.2k
                                 self->streamInfo.numChannels, drcWorkBuffer,
1761
24.2k
                                 self->streamInfo.frameSize);
1762
1763
24.2k
          if (needsDeinterleaving) {
1764
1.29k
            FDK_interleave(
1765
1.29k
                drcWorkBuffer, pTimeData2, self->streamInfo.numChannels,
1766
1.29k
                self->streamInfo.frameSize, self->streamInfo.frameSize);
1767
1.29k
          }
1768
24.2k
        }
1769
446k
      }
1770
446k
      if (FDK_drcDec_GetParam(self->hUniDrcDecoder, DRC_DEC_IS_ACTIVE)) {
1771
        /* return output loudness information for MPEG-D DRC */
1772
26.3k
        LONG outputLoudness =
1773
26.3k
            FDK_drcDec_GetParam(self->hUniDrcDecoder, DRC_DEC_OUTPUT_LOUDNESS);
1774
26.3k
        if (outputLoudness == DRC_DEC_LOUDNESS_NOT_PRESENT) {
1775
          /* no valid MPEG-D DRC loudness value contained */
1776
24.9k
          self->streamInfo.outputLoudness = -1;
1777
24.9k
        } else {
1778
1.42k
          if (outputLoudness > 0) {
1779
            /* positive output loudness values (very unusual) are limited to 0
1780
             * dB */
1781
386
            self->streamInfo.outputLoudness = 0;
1782
1.03k
          } else {
1783
1.03k
            self->streamInfo.outputLoudness =
1784
1.03k
                -(INT)outputLoudness >>
1785
1.03k
                22; /* negate and scale from e = 7 to e = (31-2) */
1786
1.03k
          }
1787
1.42k
        }
1788
420k
      } else {
1789
        /* return output loudness information for MPEG-4 DRC */
1790
420k
        if (self->streamInfo.drcProgRefLev <
1791
420k
            0) { /* no MPEG-4 DRC loudness metadata contained */
1792
400k
          self->streamInfo.outputLoudness = -1;
1793
400k
        } else {
1794
19.8k
          if (self->defaultTargetLoudness <
1795
19.8k
              0) { /* loudness normalization is off */
1796
0
            self->streamInfo.outputLoudness = self->streamInfo.drcProgRefLev;
1797
19.8k
          } else {
1798
19.8k
            self->streamInfo.outputLoudness = self->defaultTargetLoudness;
1799
19.8k
          }
1800
19.8k
        }
1801
420k
      }
1802
1803
446k
      if (self->streamInfo.extAot != AOT_AAC_SLS) {
1804
446k
        interleaved = 0;
1805
446k
        interleaved |= (self->sbrEnabled) ? 1 : 0;
1806
446k
        interleaved |= (self->mpsEnableCurr) ? 1 : 0;
1807
446k
        PCMDMX_ERROR dmxErr = PCMDMX_OK;
1808
446k
        if ((flags & AACDEC_INTR) && (accessUnit == 0)) {
1809
          /* delete data from the past (e.g. mixdown coeficients) */
1810
0
          pcmDmx_Reset(self->hPcmUtils, PCMDMX_RESET_BS_DATA);
1811
0
        }
1812
446k
        if (flags & (AACDEC_CLRHIST)) {
1813
0
          if (!(self->flags[0] & AC_USAC)) {
1814
            /* delete data from the past (e.g. mixdown coeficients) */
1815
0
            pcmDmx_Reset(self->hPcmUtils, PCMDMX_RESET_BS_DATA);
1816
0
          }
1817
0
        }
1818
1819
        /* do PCM post processing */
1820
446k
        dmxErr = pcmDmx_ApplyFrame(self->hPcmUtils, pTimeData2, timeData2Size,
1821
446k
                                   self->streamInfo.frameSize,
1822
446k
                                   &self->streamInfo.numChannels, interleaved,
1823
446k
                                   self->channelType, self->channelIndices,
1824
446k
                                   &self->mapDescr, &pcmLimiterScale);
1825
446k
        if (dmxErr == PCMDMX_OUTPUT_BUFFER_TOO_SMALL) {
1826
0
          ErrorStatus = AAC_DEC_OUTPUT_BUFFER_TOO_SMALL;
1827
0
          goto bail;
1828
0
        }
1829
446k
        if ((ErrorStatus == AAC_DEC_OK) && (dmxErr == PCMDMX_INVALID_MODE)) {
1830
          /* Announce the framework that the current combination of channel
1831
           * configuration and downmix settings are not know to produce a
1832
           * predictable behavior and thus maybe produce strange output. */
1833
5
          ErrorStatus = AAC_DEC_DECODE_FRAME_ERROR;
1834
5
        }
1835
446k
      }
1836
1837
446k
      if (self->flags[0] & AC_USAC) {
1838
269k
        if (self->flushStatus == AACDEC_USAC_DASH_IPF_FLUSH_ON &&
1839
31
            !(flags & AACDEC_CONCEAL)) {
1840
31
          CAacDecoder_PrepareCrossFade(pTimeData2, self->pTimeDataFlush,
1841
31
                                       self->streamInfo.numChannels,
1842
31
                                       self->streamInfo.frameSize, interleaved);
1843
31
        }
1844
1845
        /* prepare crossfade buffer for fade in */
1846
269k
        if (!applyCrossfade &&
1847
0
            (self->applyCrossfade != AACDEC_CROSSFADE_BITMASK_OFF) &&
1848
0
            !(flags & AACDEC_CONCEAL)) {
1849
0
          for (int ch = 0; ch < self->streamInfo.numChannels; ch++) {
1850
0
            for (int i = 0; i < TIME_DATA_FLUSH_SIZE; i++) {
1851
0
              self->pTimeDataFlush[ch][i] = (PCM_DEC)0;
1852
0
            }
1853
0
          }
1854
0
          applyCrossfade = 1;
1855
0
        }
1856
1857
269k
        if (applyCrossfade &&
1858
269k
            (self->applyCrossfade != AACDEC_CROSSFADE_BITMASK_OFF) &&
1859
609
            !(accessUnit < numPrerollAU) &&
1860
201
            (self->buildUpStatus == AACDEC_USAC_BUILD_UP_ON)) {
1861
201
          CAacDecoder_ApplyCrossFade(pTimeData2, self->pTimeDataFlush,
1862
201
                                     self->streamInfo.numChannels,
1863
201
                                     self->streamInfo.frameSize, interleaved);
1864
201
          self->applyCrossfade =
1865
201
              AACDEC_CROSSFADE_BITMASK_OFF; /* disable cross-fade between frames
1866
                                               at nect config change */
1867
201
        }
1868
269k
      }
1869
1870
      /* Signal interruption to take effect in next frame. */
1871
446k
      if ((flags & AACDEC_FLUSH || self->flushStatus) &&
1872
31
          !(flags & AACDEC_CONCEAL)) {
1873
31
        aacDecoder_SignalInterruption(self);
1874
31
      }
1875
1876
      /* Update externally visible copy of flags */
1877
446k
      self->streamInfo.flags = self->flags[0];
1878
1879
446k
    } /* USAC DASH IPF flushing possible end */
1880
446k
    if (accessUnit < numPrerollAU) {
1881
2.47k
      FDKpushBack(hBsAu, auStartAnchor - (INT)FDKgetValidBits(hBsAu));
1882
444k
    } else {
1883
444k
      if ((self->buildUpStatus == AACDEC_RSV60_BUILD_UP_ON) ||
1884
444k
          (self->buildUpStatus == AACDEC_RSV60_BUILD_UP_ON_IN_BAND) ||
1885
444k
          (self->buildUpStatus == AACDEC_USAC_BUILD_UP_ON)) {
1886
1.22k
        self->buildUpCnt--;
1887
1888
1.22k
        if (self->buildUpCnt < 0) {
1889
1.22k
          self->buildUpStatus = 0;
1890
1.22k
        }
1891
1.22k
      }
1892
1893
444k
      if (self->flags[0] & AC_USAC) {
1894
266k
        if (self->flushStatus == AACDEC_USAC_DASH_IPF_FLUSH_ON &&
1895
31
            !(flags & AACDEC_CONCEAL)) {
1896
31
          self->streamInfo.frameSize = 0;
1897
31
        }
1898
266k
      }
1899
444k
    }
1900
1901
446k
    if (self->flushStatus != AACDEC_USAC_DASH_IPF_FLUSH_ON) {
1902
446k
      accessUnit++;
1903
446k
    }
1904
446k
  } while ((accessUnit < numAccessUnits) ||
1905
444k
           ((self->flushStatus == AACDEC_USAC_DASH_IPF_FLUSH_ON) &&
1906
0
            !(flags & AACDEC_CONCEAL)));
1907
1908
444k
  if (self->streamInfo.extAot != AOT_AAC_SLS) {
1909
444k
    pcmLimiterScale += PCM_OUT_HEADROOM;
1910
1911
444k
    if (flags & AACDEC_CLRHIST) {
1912
0
      if (!(self->flags[0] & AC_USAC)) {
1913
        /* Reset DRC data */
1914
0
        aacDecoder_drcReset(self->hDrcInfo);
1915
        /* Delete the delayed signal. */
1916
0
        pcmLimiter_Reset(self->hLimiter);
1917
0
      }
1918
0
    }
1919
1920
    /* Set applyExtGain if DRC processing is enabled and if progRefLevelPresent
1921
       is present for the first time. Consequences: The headroom of the output
1922
       signal can be set to AACDEC_DRC_GAIN_SCALING only for audio formats which
1923
       support legacy DRC Level Normalization. For all other audio formats the
1924
       headroom of the output signal is set to PCM_OUT_HEADROOM. */
1925
444k
    if (self->hDrcInfo->enable && (self->hDrcInfo->progRefLevelPresent == 1)) {
1926
19.9k
      self->hDrcInfo->applyExtGain |= 1;
1927
19.9k
    }
1928
1929
    /* Check whether time data buffer is large enough. */
1930
444k
    if (timeDataSize <
1931
444k
        (self->streamInfo.numChannels * self->streamInfo.frameSize)) {
1932
0
      ErrorStatus = AAC_DEC_OUTPUT_BUFFER_TOO_SMALL;
1933
0
      goto bail;
1934
0
    }
1935
1936
444k
    if (self->limiterEnableCurr) {
1937
      /* use workBufferCore2 buffer for interleaving */
1938
325k
      PCM_LIM *pInterleaveBuffer;
1939
325k
      int blockLength = self->streamInfo.frameSize;
1940
1941
      /* Set actual signal parameters */
1942
325k
      pcmLimiter_SetNChannels(self->hLimiter, self->streamInfo.numChannels);
1943
325k
      pcmLimiter_SetSampleRate(self->hLimiter, self->streamInfo.sampleRate);
1944
1945
325k
      if ((self->streamInfo.numChannels == 1) || (self->sbrEnabled) ||
1946
298k
          (self->mpsEnableCurr)) {
1947
298k
        pInterleaveBuffer = (PCM_LIM *)pTimeData2;
1948
298k
      } else {
1949
27.1k
        pInterleaveBuffer = (PCM_LIM *)self->workBufferCore2;
1950
1951
        /* applyLimiter requests for interleaved data */
1952
        /* Interleave ouput buffer */
1953
27.1k
        FDK_interleave(pTimeData2, pInterleaveBuffer,
1954
27.1k
                       self->streamInfo.numChannels, blockLength,
1955
27.1k
                       self->streamInfo.frameSize);
1956
27.1k
      }
1957
1958
325k
      FIXP_DBL *pGainPerSample = NULL;
1959
1960
325k
      if (self->hDrcInfo->enable && self->hDrcInfo->applyExtGain) {
1961
14.7k
        pGainPerSample = self->workBufferCore1;
1962
1963
14.7k
        if ((INT)GetRequiredMemWorkBufferCore1() <
1964
14.7k
            (INT)(self->streamInfo.frameSize * sizeof(FIXP_DBL))) {
1965
0
          ErrorStatus = AAC_DEC_UNKNOWN;
1966
0
          goto bail;
1967
0
        }
1968
1969
14.7k
        pcmLimiterScale = applyDrcLevelNormalization(
1970
14.7k
            self->hDrcInfo, (PCM_DEC *)pInterleaveBuffer, self->extGain,
1971
14.7k
            pGainPerSample, pcmLimiterScale, self->extGainDelay,
1972
14.7k
            self->streamInfo.frameSize, self->streamInfo.numChannels, 1, 1);
1973
14.7k
      }
1974
1975
325k
      pcmLimiter_Apply(self->hLimiter, pInterleaveBuffer, pTimeData,
1976
325k
                       pGainPerSample, pcmLimiterScale,
1977
325k
                       self->streamInfo.frameSize);
1978
1979
325k
      {
1980
        /* Announce the additional limiter output delay */
1981
325k
        self->streamInfo.outputDelay += pcmLimiter_GetDelay(self->hLimiter);
1982
325k
      }
1983
325k
    } else {
1984
118k
      if (self->hDrcInfo->enable && self->hDrcInfo->applyExtGain) {
1985
5.21k
        pcmLimiterScale = applyDrcLevelNormalization(
1986
5.21k
            self->hDrcInfo, pTimeData2, self->extGain, NULL, pcmLimiterScale,
1987
5.21k
            self->extGainDelay, self->streamInfo.frameSize,
1988
5.21k
            self->streamInfo.numChannels,
1989
5.21k
            (interleaved || (self->streamInfo.numChannels == 1))
1990
5.21k
                ? 1
1991
5.21k
                : self->streamInfo.frameSize,
1992
5.21k
            0);
1993
5.21k
      }
1994
1995
      /* If numChannels = 1 we do not need interleaving. The same applies if SBR
1996
      or MPS are used, since their output is interleaved already (resampled or
1997
      not) */
1998
118k
      if ((self->streamInfo.numChannels == 1) || (self->sbrEnabled) ||
1999
110k
          (self->mpsEnableCurr)) {
2000
110k
        scaleValuesSaturate(
2001
110k
            pTimeData, pTimeData2,
2002
110k
            self->streamInfo.frameSize * self->streamInfo.numChannels,
2003
110k
            pcmLimiterScale);
2004
2005
110k
      } else {
2006
8.07k
        scaleValuesSaturate(
2007
8.07k
            (INT_PCM *)self->workBufferCore2, pTimeData2,
2008
8.07k
            self->streamInfo.frameSize * self->streamInfo.numChannels,
2009
8.07k
            pcmLimiterScale);
2010
        /* Interleave ouput buffer */
2011
8.07k
        FDK_interleave((INT_PCM *)self->workBufferCore2, pTimeData,
2012
8.07k
                       self->streamInfo.numChannels, self->streamInfo.frameSize,
2013
8.07k
                       self->streamInfo.frameSize);
2014
8.07k
      }
2015
118k
    }
2016
444k
  } /* if (self->streamInfo.extAot != AOT_AAC_SLS)*/
2017
2018
466k
bail:
2019
2020
  /* error in renderer part occurred, ErrorStatus was set to invalid output */
2021
466k
  if (fEndAuNotAdjusted && !IS_OUTPUT_VALID(ErrorStatus) &&
2022
1
      (accessUnit < numPrerollAU)) {
2023
1
    transportDec_EndAccessUnit(self->hInput);
2024
1
  }
2025
2026
  /* Update Statistics */
2027
466k
  aacDecoder_UpdateBitStreamCounters(&self->streamInfo, hBs, nBits,
2028
466k
                                     ErrorStatus);
2029
466k
  if (((self->streamInfo.numChannels <= 0) ||
2030
454k
       (self->streamInfo.frameSize <= 0) ||
2031
454k
       (self->streamInfo.sampleRate <= 0)) &&
2032
12.5k
      IS_OUTPUT_VALID(ErrorStatus)) {
2033
    /* Ensure consistency of IS_OUTPUT_VALID() macro. */
2034
0
    ErrorStatus = AAC_DEC_UNKNOWN;
2035
0
  }
2036
2037
466k
  if (!IS_OUTPUT_VALID(ErrorStatus)) {
2038
21.9k
    FDKmemclear(pTimeData, timeDataSize * sizeof(*pTimeData));
2039
21.9k
  }
2040
2041
466k
  return ErrorStatus;
2042
444k
}
2043
2044
34.7k
LINKSPEC_CPP void aacDecoder_Close(HANDLE_AACDECODER self) {
2045
34.7k
  if (self == NULL) return;
2046
2047
34.7k
  if (self->hLimiter != NULL) {
2048
34.7k
    pcmLimiter_Destroy(self->hLimiter);
2049
34.7k
  }
2050
2051
34.7k
  if (self->hPcmUtils != NULL) {
2052
34.7k
    pcmDmx_Close(&self->hPcmUtils);
2053
34.7k
  }
2054
2055
34.7k
  FDK_drcDec_Close(&self->hUniDrcDecoder);
2056
2057
34.7k
  if (self->pMpegSurroundDecoder != NULL) {
2058
34.7k
    mpegSurroundDecoder_Close(
2059
34.7k
        (CMpegSurroundDecoder *)self->pMpegSurroundDecoder);
2060
34.7k
  }
2061
2062
34.7k
  if (self->hSbrDecoder != NULL) {
2063
34.7k
    sbrDecoder_Close(&self->hSbrDecoder);
2064
34.7k
  }
2065
2066
34.7k
  if (self->hInput != NULL) {
2067
34.7k
    transportDec_Close(&self->hInput);
2068
34.7k
  }
2069
2070
34.7k
  CAacDecoder_Close(self);
2071
34.7k
}
2072
2073
0
LINKSPEC_CPP CStreamInfo *aacDecoder_GetStreamInfo(HANDLE_AACDECODER self) {
2074
0
  return CAacDecoder_GetStreamInfo(self);
2075
0
}
2076
2077
0
LINKSPEC_CPP INT aacDecoder_GetLibInfo(LIB_INFO *info) {
2078
0
  int i;
2079
2080
0
  if (info == NULL) {
2081
0
    return -1;
2082
0
  }
2083
2084
0
  sbrDecoder_GetLibInfo(info);
2085
0
  mpegSurroundDecoder_GetLibInfo(info);
2086
0
  transportDec_GetLibInfo(info);
2087
0
  FDK_toolsGetLibInfo(info);
2088
0
  pcmDmx_GetLibInfo(info);
2089
0
  pcmLimiter_GetLibInfo(info);
2090
0
  FDK_drcDec_GetLibInfo(info);
2091
2092
  /* search for next free tab */
2093
0
  for (i = 0; i < FDK_MODULE_LAST; i++) {
2094
0
    if (info[i].module_id == FDK_NONE) break;
2095
0
  }
2096
0
  if (i == FDK_MODULE_LAST) {
2097
0
    return -1;
2098
0
  }
2099
0
  info += i;
2100
2101
0
  info->module_id = FDK_AACDEC;
2102
  /* build own library info */
2103
0
  info->version =
2104
0
      LIB_VERSION(AACDECODER_LIB_VL0, AACDECODER_LIB_VL1, AACDECODER_LIB_VL2);
2105
0
  LIB_VERSION_STRING(info);
2106
0
  info->build_date = AACDECODER_LIB_BUILD_DATE;
2107
0
  info->build_time = AACDECODER_LIB_BUILD_TIME;
2108
0
  info->title = AACDECODER_LIB_TITLE;
2109
2110
  /* Set flags */
2111
0
  info->flags = 0 | CAPF_AAC_LC | CAPF_ER_AAC_LC | CAPF_ER_AAC_SCAL |
2112
0
                CAPF_AAC_VCB11 | CAPF_AAC_HCR | CAPF_AAC_RVLC | CAPF_ER_AAC_LD |
2113
0
                CAPF_ER_AAC_ELD | CAPF_AAC_CONCEALMENT | CAPF_AAC_DRC |
2114
0
                CAPF_AAC_MPEG4 | CAPF_AAC_DRM_BSFORMAT | CAPF_AAC_1024 |
2115
0
                CAPF_AAC_960 | CAPF_AAC_512 | CAPF_AAC_480 |
2116
0
                CAPF_AAC_ELD_DOWNSCALE
2117
2118
0
                | CAPF_AAC_USAC | CAPF_ER_AAC_ELDV2 | CAPF_AAC_UNIDRC;
2119
  /* End of flags */
2120
2121
0
  return 0;
2122
0
}