Coverage Report

Created: 2026-04-01 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aac/libSACdec/src/sac_dec_lib.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2020 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
/*********************** MPEG surround decoder library *************************
96
97
   Author(s):
98
99
   Description: SAC Decoder Library Interface
100
101
*******************************************************************************/
102
103
#include "sac_dec_lib.h"
104
#include "sac_dec_interface.h"
105
#include "sac_dec.h"
106
#include "sac_bitdec.h"
107
#include "FDK_matrixCalloc.h"
108
109
9.42k
#define MPS_DATA_BUFFER_SIZE (2048)
110
111
/**
112
 * \brief MPEG Surround data indication.
113
 **/
114
typedef enum {
115
  MPEGS_ANCTYPE_FRAME = 0, /*!< MPEG Surround frame, see ISO/IEC 23003-1 */
116
  MPEGS_ANCTYPE_HEADER_AND_FRAME = 1, /*!< MPEG Surround header and MPEG
117
                                         Surround frame, see ISO/IEC 23003-1 */
118
  MPEGS_ANCTYPE_RESERVED_1 = 2,       /*!< reserved, see ISO/IEC 23003-1 */
119
  MPEGS_ANCTYPE_RESERVED_2 = 3        /*!< reserved, see ISO/IEC 23003-1*/
120
} MPEGS_ANCTYPE;
121
122
/**
123
 * \brief MPEG Surround data segment indication.
124
 **/
125
typedef enum {
126
  MPEGS_CONTINUE = 0, /*!< Indicates if data segment continues a data block. */
127
  MPEGS_STOP = 1,     /*!< Indicates if data segment ends a data block. */
128
  MPEGS_START = 2,    /*!< Indicates if data segment begins a data block. */
129
  MPEGS_START_STOP =
130
      3 /*!< Indicates if data segment begins and ends a data block. */
131
} MPEGS_ANCSTARTSTOP;
132
133
/**
134
 * \brief MPEG Surround synchronizaiton state.
135
 *
136
 *  CAUTION: Changing the enumeration values can break the sync mechanism
137
 *because it is based on comparing the state values.
138
 **/
139
typedef enum {
140
  MPEGS_SYNC_LOST =
141
      0, /*!< Indicates lost sync because of current discontinuity. */
142
  MPEGS_SYNC_FOUND = 1,   /*!< Parsed a valid header and (re)intialization was
143
                             successfully completed. */
144
  MPEGS_SYNC_COMPLETE = 2 /*!< In sync and continuous. Found an independent
145
                             frame in addition to MPEGS_SYNC_FOUND.
146
                               Precondition: MPEGS_SYNC_FOUND. */
147
} MPEGS_SYNCSTATE;
148
149
/**
150
 * \brief MPEG Surround operation mode.
151
 **/
152
typedef enum {
153
  MPEGS_OPMODE_EMM = 0,           /*!< Mode: Enhanced Matrix Mode (Blind) */
154
  MPEGS_OPMODE_MPS_PAYLOAD = 1,   /*!< Mode: Normal, Stereo or Binaural */
155
  MPEGS_OPMODE_NO_MPS_PAYLOAD = 2 /*!< Mode: no MPEG Surround payload */
156
} MPEGS_OPMODE;
157
158
/**
159
 * \brief MPEG Surround init flags.
160
 **/
161
typedef enum {
162
  MPEGS_INIT_OK = 0x00000000, /*!< indicate correct initialization */
163
  MPEGS_INIT_ENFORCE_REINIT =
164
      0x00000001, /*!< indicate complete initialization */
165
166
  MPEGS_INIT_CHANGE_OUTPUT_MODE =
167
      0x00000010, /*!< indicate change of the output mode */
168
  MPEGS_INIT_CHANGE_PARTIALLY_COMPLEX =
169
      0x00000020, /*!< indicate change of low power/high quality */
170
  MPEGS_INIT_CHANGE_TIME_FREQ_INTERFACE =
171
      0x00000040, /*!< indicate change of qmf/time interface */
172
  MPEGS_INIT_CHANGE_HEADER = 0x00000080, /*!< indicate change of header */
173
174
  MPEGS_INIT_ERROR_PAYLOAD =
175
      0x00000100, /*!< indicate payload/ancType/ancStartStop error */
176
177
  MPEGS_INIT_BS_INTERRUPTION =
178
      0x00001000, /*!< indicate bitstream interruption  */
179
  MPEGS_INIT_CLEAR_HISTORY =
180
      0x00002000, /*!< indicate that all states shall be cleared */
181
182
  /* Re-initialization of submodules */
183
184
  MPEGS_INIT_CHANGE_CONCEAL_PARAMS = 0x00100000, /*!< indicate a change of at
185
                                                    least one error concealment
186
                                                    param */
187
188
  /* No re-initialization needed, currently not used */
189
  MPEGS_INIT_CHANGE_BYPASS_MODE =
190
      0x01000000, /*!< indicate change of bypass mode */
191
192
  /* Re-initialization needed, currently not used */
193
  MPEGS_INIT_ERROR_ANC_TYPE = 0x10000000, /*!< indicate ancType error*/
194
  MPEGS_INIT_ERROR_ANC_STARTSTOP =
195
      0x20000000 /*!< indicate ancStartStop error */
196
} MPEGS_INIT_FLAGS;
197
198
struct MpegSurroundDecoder {
199
  HANDLE_FDK_QMF_DOMAIN pQmfDomain;
200
  UCHAR mpsData[MPS_DATA_BUFFER_SIZE]; /* Buffer for MPS payload accross more
201
                                          than one segment */
202
  INT mpsDataBits;                     /* Amount of bits in mpsData */
203
  /* MPEG Surround decoder */
204
  SPATIAL_SPECIFIC_CONFIG spatialSpecificConfig[1]; /* SSC delay line which is
205
                                                       used during decoding */
206
  spatialDec *pSpatialDec;
207
  SPATIAL_SPECIFIC_CONFIG
208
  spatialSpecificConfigBackup; /* SSC used while parsing */
209
210
  /* Creation parameter */
211
  UCHAR mpegSurroundDecoderLevel;
212
  /* Run-time parameter */
213
  UCHAR mpegSurroundSscIsGlobalCfg; /* Flag telling that the SSC
214
                                       (::spatialSpecificConfig) is a
215
                                       out-of-band configuration. */
216
  UCHAR mpegSurroundUseTimeInterface;
217
218
  SPATIAL_BS_FRAME
219
  bsFrames[1];         /* Bitstream Structs that contain data read from the
220
                          SpatialFrame() bitstream element */
221
  BS_LL_STATE llState; /* Bit stream parser state memory */
222
  UCHAR bsFrameParse;  /* Current parse frame context index */
223
  UCHAR bsFrameDecode; /* Current decode/apply frame context index */
224
  UCHAR bsFrameDelay;  /* Amount of frames delay between parsing and processing.
225
                          Required i.e. for interpolation error concealment. */
226
227
  /* User prameters */
228
  SPATIALDEC_PARAM mpegSurroundUserParams;
229
230
  /* Internal flags */
231
  SPATIAL_DEC_UPMIX_TYPE upmixType;
232
  int initFlags[1];
233
  MPEGS_ANCSTARTSTOP ancStartStopPrev;
234
  MPEGS_SYNCSTATE fOnSync[1];
235
236
  /* Inital decoder configuration */
237
  SPATIAL_DEC_CONFIG decConfig;
238
};
239
240
SACDEC_ERROR
241
static sscCheckOutOfBand(const SPATIAL_SPECIFIC_CONFIG *pSsc,
242
                         const INT coreCodec, const INT sampleRate,
243
                         const INT frameSize);
244
245
static SACDEC_ERROR sscParseCheck(const SPATIAL_SPECIFIC_CONFIG *pSsc);
246
247
/**
248
 * \brief Get the number of QMF bands from the sampling frequency (in Hz)
249
 **/
250
static int mpegSurroundDecoder_GetNrOfQmfBands(
251
86.2k
    const SPATIAL_SPECIFIC_CONFIG *pSsc, UINT sampleRate) {
252
86.2k
  UINT samplingFrequency = sampleRate;
253
86.2k
  int qmfBands = 64;
254
255
86.2k
  if (pSsc != NULL) {
256
84.2k
    switch (pSsc->coreCodec) {
257
24.3k
      case AOT_USAC:
258
24.3k
        if ((pSsc->stereoConfigIndex == 3)) {
259
4.69k
          static const UCHAR mapIdx2QmfBands[3] = {24, 32, 16};
260
4.69k
          FDK_ASSERT((pSsc->coreSbrFrameLengthIndex >= 2) &&
261
4.69k
                     (pSsc->coreSbrFrameLengthIndex <= 4));
262
4.69k
          qmfBands = mapIdx2QmfBands[pSsc->coreSbrFrameLengthIndex - 2];
263
4.69k
        }
264
24.3k
        return qmfBands;
265
59.9k
      default:
266
59.9k
        samplingFrequency = pSsc->samplingFreq;
267
59.9k
        break;
268
84.2k
    }
269
84.2k
  }
270
271
  /* number of QMF bands depend on sampling frequency, see FDIS 23003-1:2006
272
   * Chapter 6.3.3 */
273
61.8k
  if (samplingFrequency < 27713) {
274
44.0k
    qmfBands = 32;
275
44.0k
  }
276
61.8k
  if (samplingFrequency > 55426) {
277
652
    qmfBands = 128;
278
652
  }
279
280
61.8k
  return qmfBands;
281
86.2k
}
282
283
/**
284
 * \brief Analyse init flags
285
 **/
286
static int mpegSurroundDecoder_CalcInitFlags(SPATIAL_SPECIFIC_CONFIG *pSsc1,
287
                                             SPATIAL_SPECIFIC_CONFIG *pSsc2,
288
                                             int upmixTypeFlag,
289
                                             int binauralQualityFlag,
290
                                             int partiallyComplexFlag,
291
40.9k
                                             int *ctrlFlags) {
292
  /* Analyse core coder */
293
40.9k
  if (pSsc1->coreCodec != pSsc2->coreCodec) {
294
0
    *ctrlFlags |= MASK_MPEGS_INIT_ALL_STATES;
295
0
    *ctrlFlags |= MASK_MPEGS_INIT_ALL_PARAMS;
296
40.9k
  } else {
297
    /* Analyse elements for initialization of space analysis qmf filterbank */
298
40.9k
    if ((partiallyComplexFlag) || (pSsc1->treeConfig != pSsc2->treeConfig) ||
299
40.9k
        (pSsc1->samplingFreq != pSsc2->samplingFreq)) {
300
0
      *ctrlFlags |= MPEGS_INIT_STATES_ANA_QMF_FILTER;
301
0
      *ctrlFlags |= MPEGS_INIT_STATES_ANA_HYB_FILTER;
302
0
    }
303
304
    /* Analyse elements for initialization of space synthesis qmf filterbank */
305
40.9k
    if ((upmixTypeFlag) || (partiallyComplexFlag) ||
306
40.9k
        (pSsc1->treeConfig != pSsc2->treeConfig) ||
307
40.9k
        (pSsc1->samplingFreq != pSsc2->samplingFreq) ||
308
40.9k
        (pSsc1->bsFixedGainDMX != pSsc2->bsFixedGainDMX)) {
309
1.79k
      *ctrlFlags |= MPEGS_INIT_STATES_SYN_QMF_FILTER;
310
1.79k
    }
311
312
    /* Analyse elements for initialization of decorrelator */
313
40.9k
    if ((upmixTypeFlag) || (partiallyComplexFlag) ||
314
40.9k
        (pSsc1->treeConfig != pSsc2->treeConfig) ||
315
40.9k
        (pSsc1->samplingFreq != pSsc2->samplingFreq) ||
316
40.9k
        (pSsc1->decorrConfig != pSsc2->decorrConfig)) {
317
726
      *ctrlFlags |= MPEGS_INIT_STATES_DECORRELATOR;
318
726
    }
319
320
    /* Analyse elements for initialization of m1 and m2 calculation */
321
40.9k
    if ((upmixTypeFlag) || (binauralQualityFlag) ||
322
40.9k
        (pSsc1->treeConfig != pSsc2->treeConfig) ||
323
40.9k
        (pSsc1->samplingFreq != pSsc2->samplingFreq))
324
325
0
    {
326
0
      *ctrlFlags |= MPEGS_INIT_STATES_M1M2;
327
0
    }
328
329
    /* Analyse elements for initialization of GES */
330
40.9k
    if ((upmixTypeFlag) || (pSsc1->treeConfig != pSsc2->treeConfig) ||
331
40.9k
        (pSsc1->tempShapeConfig != pSsc2->tempShapeConfig)) {
332
995
      *ctrlFlags |= MPEGS_INIT_STATES_GES;
333
995
    }
334
335
    /* Analyse elements for initialization of FDreverb */
336
40.9k
    if ((upmixTypeFlag) || (binauralQualityFlag) || (partiallyComplexFlag) ||
337
40.9k
        (pSsc1->samplingFreq != pSsc2->samplingFreq) ||
338
40.9k
        (pSsc1->nTimeSlots != pSsc2->nTimeSlots)) {
339
696
      *ctrlFlags |= MPEGS_INIT_STATES_REVERB;
340
696
    }
341
342
    /* Reset previous frame data whenever the config changes */
343
40.9k
    if (*ctrlFlags & MPEGS_INIT_CONFIG) {
344
6.79k
      *ctrlFlags |= MPEGS_INIT_STATES_PARAM;
345
6.79k
    }
346
40.9k
  }
347
348
40.9k
  return MPS_OK;
349
40.9k
}
350
351
/**
352
 * \brief Reset MPEG Surround status info
353
 **/
354
static void updateMpegSurroundDecoderStatus(
355
    CMpegSurroundDecoder *pMpegSurroundDecoder, int initFlags,
356
81.7k
    MPEGS_SYNCSTATE fOnSync, MPEGS_ANCSTARTSTOP ancStartStopPrev) {
357
81.7k
  pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
358
81.7k
      initFlags;
359
81.7k
  if ((pMpegSurroundDecoder->mpegSurroundSscIsGlobalCfg != 0) &&
360
38.8k
      (pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] >=
361
38.8k
       MPEGS_SYNC_FOUND) &&
362
35.6k
      (fOnSync < MPEGS_SYNC_FOUND)) {
363
35.6k
    pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
364
35.6k
        MPEGS_SYNC_FOUND;
365
46.0k
  } else {
366
46.0k
    pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
367
46.0k
        fOnSync;
368
46.0k
  }
369
81.7k
  pMpegSurroundDecoder->ancStartStopPrev = ancStartStopPrev;
370
81.7k
}
371
372
static SACDEC_ERROR mpegSurroundDecoder_Create(
373
    CMpegSurroundDecoder **pMpegSurroundDecoder, int stereoConfigIndex,
374
    HANDLE_FDK_QMF_DOMAIN pQmfDomain);
375
376
SAC_INSTANCE_AVAIL
377
mpegSurroundDecoder_IsFullMpegSurroundDecoderInstanceAvailable(
378
2.71k
    CMpegSurroundDecoder *pMpegSurroundDecoder) {
379
2.71k
  SAC_INSTANCE_AVAIL instanceAvailable = SAC_INSTANCE_NOT_FULL_AVAILABLE;
380
381
2.71k
  if (pMpegSurroundDecoder->pSpatialDec != NULL) {
382
1.75k
    instanceAvailable = SAC_INSTANCE_FULL_AVAILABLE;
383
1.75k
  }
384
385
2.71k
  return instanceAvailable;
386
2.71k
}
387
388
SACDEC_ERROR mpegSurroundDecoder_Open(
389
    CMpegSurroundDecoder **pMpegSurroundDecoder, int stereoConfigIndex,
390
55.3k
    HANDLE_FDK_QMF_DOMAIN pQmfDomain) {
391
55.3k
  SACDEC_ERROR error;
392
393
55.3k
  error = mpegSurroundDecoder_Create(pMpegSurroundDecoder, stereoConfigIndex,
394
55.3k
                                     pQmfDomain);
395
396
55.3k
  return error;
397
55.3k
}
398
399
/**
400
 * \brief  Renamed function from getUpmixType to check_UParam_Build_DecConfig.
401
 *         This function checks if user params, decoder config and SSC are valid
402
 *         and if the decoder build can handle all this settings.
403
 *         The upmix type may be modified by this function.
404
 *         It is called in initMpegSurroundDecoder() after the ssc parse check,
405
 *         to have all checks in one place and to ensure these checks are always
406
 *         performed if config changes (inband and out-of-band).
407
 *
408
 * \param pUserParams  User data handle.
409
 * \param pDecConfig   decoder config handle.
410
 * \param pSsc         spatial specific config handle.
411
 * \param pUpmixType   upmix type which is set by this function
412
 *
413
 * \return  MPS_OK on sucess, and else on failure.
414
 */
415
static SACDEC_ERROR check_UParam_Build_DecConfig(
416
    SPATIALDEC_PARAM const *pUserParams, SPATIAL_DEC_CONFIG const *pDecConfig,
417
51.9k
    const SPATIAL_SPECIFIC_CONFIG *pSsc, SPATIAL_DEC_UPMIX_TYPE *pUpmixType) {
418
51.9k
  int dmxChannels, outChannels, maxNumOutChannels;
419
420
51.9k
  FDK_ASSERT(pUserParams != NULL);
421
51.9k
  FDK_ASSERT(pUpmixType != NULL);
422
423
  /* checks if implementation can handle the Ssc */
424
425
51.9k
  switch (pSsc->treeConfig) {
426
51.9k
    case SPATIALDEC_MODE_RSVD7: /* 212 */
427
51.9k
      dmxChannels = 1;
428
51.9k
      outChannels = 2;
429
51.9k
      break;
430
0
    default:
431
0
      return MPS_UNSUPPORTED_CONFIG;
432
51.9k
  }
433
434
  /* ------------------------------------------- */
435
436
  /* Analyse pDecConfig params */
437
51.9k
  switch (pDecConfig->binauralMode) {
438
51.9k
    case BINAURAL_NONE:
439
51.9k
      break;
440
0
    default:
441
0
      return MPS_UNSUPPORTED_CONFIG;
442
51.9k
  }
443
444
51.9k
  switch (pDecConfig->decoderMode) {
445
51.9k
    case EXT_HQ_ONLY:
446
51.9k
      break;
447
0
    default:
448
0
      return MPS_UNSUPPORTED_CONFIG;
449
51.9k
  }
450
451
51.9k
  switch (pDecConfig->maxNumOutputChannels) {
452
51.9k
    case OUTPUT_CHANNELS_DEFAULT:
453
      /* No special restrictions -> Get the level restriction: */
454
51.9k
      switch (pDecConfig->decoderLevel) {
455
51.9k
        case DECODER_LEVEL_0:
456
51.9k
          maxNumOutChannels = 2;
457
51.9k
          break;
458
0
        default:
459
0
          return MPS_UNSUPPORTED_CONFIG;
460
51.9k
      }
461
51.9k
      break;
462
51.9k
    case OUTPUT_CHANNELS_2_0:
463
0
      maxNumOutChannels = 2;
464
0
      break;
465
0
    default:
466
0
      return MPS_UNSUPPORTED_CONFIG;
467
51.9k
  }
468
  /* ------------------------- */
469
470
  /* check if we can handle user params */
471
51.9k
  if (pUserParams->blindEnable == 1) {
472
0
    return MPS_UNSUPPORTED_CONFIG;
473
0
  }
474
51.9k
  {
475
51.9k
    switch ((SAC_DEC_OUTPUT_MODE)pUserParams->outputMode) {
476
51.9k
      case SACDEC_OUT_MODE_NORMAL:
477
51.9k
        if (maxNumOutChannels >= outChannels) {
478
51.9k
          *pUpmixType = UPMIX_TYPE_NORMAL;
479
51.9k
        } else {
480
0
          { *pUpmixType = UPMIX_TYPE_BYPASS; }
481
0
        }
482
51.9k
        break;
483
0
      case SACDEC_OUT_MODE_STEREO:
484
0
        if (dmxChannels == 1) {
485
0
          if (outChannels == 2) {
486
0
            *pUpmixType = UPMIX_TYPE_NORMAL;
487
0
          }
488
0
        } else {
489
0
          *pUpmixType = UPMIX_TYPE_BYPASS;
490
0
        }
491
0
        break;
492
0
      case SACDEC_OUT_MODE_6CHANNEL:
493
0
        if (outChannels > 6) {
494
0
          { *pUpmixType = UPMIX_TYPE_BYPASS; }
495
0
        } else {
496
0
          *pUpmixType = UPMIX_TYPE_NORMAL;
497
0
        }
498
0
        break;
499
0
      default:
500
0
        return MPS_UNSUPPORTED_CONFIG;
501
51.9k
    }
502
51.9k
  }
503
504
51.9k
  return MPS_OK;
505
51.9k
}
506
507
/**
508
 * \brief Init MPEG Surround decoder.
509
 **/
510
static SACDEC_ERROR initMpegSurroundDecoder(
511
51.9k
    CMpegSurroundDecoder *pMpegSurroundDecoder) {
512
51.9k
  SACDEC_ERROR err;
513
51.9k
  int initFlags = MPEGS_INIT_NONE, initFlagsDec;
514
51.9k
  int upmixTypeCurr = pMpegSurroundDecoder->upmixType;
515
516
51.9k
  FDK_ASSERT(pMpegSurroundDecoder != NULL);
517
518
51.9k
  SPATIAL_SPECIFIC_CONFIG *const pSSCinput =
519
51.9k
      &pMpegSurroundDecoder->spatialSpecificConfigBackup;
520
51.9k
  SPATIAL_SPECIFIC_CONFIG *const pSSCtarget =
521
51.9k
      &pMpegSurroundDecoder
522
51.9k
           ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameDecode];
523
51.9k
  initFlagsDec =
524
51.9k
      pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode];
525
526
51.9k
  if (pSSCinput->coreCodec != AOT_USAC) {
527
    /* here we check if we have a valid Ssc */
528
27.5k
    err = sscParseCheck(pSSCinput);
529
27.5k
    if (err != MPS_OK) goto bail;
530
27.5k
  }
531
532
  /* here we check if Ssc matches build; also check UParams and DecConfig */
533
  /* if desired upmixType is changes                                      */
534
51.9k
  err = check_UParam_Build_DecConfig(
535
51.9k
      &pMpegSurroundDecoder->mpegSurroundUserParams,
536
51.9k
      &pMpegSurroundDecoder->decConfig, pSSCinput,
537
51.9k
      &pMpegSurroundDecoder->upmixType);
538
51.9k
  if (err != MPS_OK) goto bail;
539
540
  /* init config */
541
51.9k
  if (initFlagsDec & MPEGS_INIT_CHANGE_HEADER) {
542
16.7k
    initFlags |= MPEGS_INIT_CONFIG;
543
16.7k
  }
544
  /* init all states */
545
51.9k
  if (initFlagsDec & MPEGS_INIT_CLEAR_HISTORY) {
546
0
    initFlags |= MASK_MPEGS_INIT_ALL_STATES;
547
0
  }
548
51.9k
  if (initFlagsDec & MPEGS_INIT_CHANGE_CONCEAL_PARAMS) {
549
0
    initFlags |= MPEGS_INIT_PARAMS_ERROR_CONCEALMENT;
550
0
  }
551
552
51.9k
  if (initFlagsDec & MPEGS_INIT_ENFORCE_REINIT) {
553
    /* init all states */
554
10.9k
    initFlags |= MASK_MPEGS_INIT_ALL_STATES;
555
10.9k
    initFlags |= MASK_MPEGS_INIT_ALL_PARAMS;
556
40.9k
  } else {
557
    /* analyse states which have to be initialized */
558
40.9k
    mpegSurroundDecoder_CalcInitFlags(
559
40.9k
        pSSCtarget, pSSCinput,
560
40.9k
        (upmixTypeCurr !=
561
40.9k
         pMpegSurroundDecoder->upmixType), /* upmixType changed */
562
40.9k
        0, (initFlagsDec & MPEGS_INIT_CHANGE_PARTIALLY_COMPLEX) ? 1 : 0,
563
40.9k
        &initFlags);
564
40.9k
  }
565
566
51.9k
  {
567
51.9k
    int nrOfQmfBands;
568
51.9k
    FDKmemcpy(pSSCtarget, pSSCinput, sizeof(SPATIAL_SPECIFIC_CONFIG));
569
570
51.9k
    nrOfQmfBands = mpegSurroundDecoder_GetNrOfQmfBands(
571
51.9k
        pSSCtarget, pSSCtarget->samplingFreq);
572
51.9k
    err = FDK_SpatialDecInit(
573
51.9k
        pMpegSurroundDecoder->pSpatialDec,
574
51.9k
        &pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameDecode],
575
51.9k
        pSSCtarget, nrOfQmfBands, pMpegSurroundDecoder->upmixType,
576
51.9k
        &pMpegSurroundDecoder->mpegSurroundUserParams, initFlags);
577
578
51.9k
    if (err != MPS_OK) goto bail;
579
580
    /* Signal that we got a header and can go on decoding */
581
51.2k
    if (err == MPS_OK) {
582
51.2k
      initFlagsDec = MPEGS_INIT_OK;
583
51.2k
      {
584
51.2k
        pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
585
51.2k
            MPEGS_SYNC_FOUND;
586
51.2k
      }
587
51.2k
    }
588
51.2k
  }
589
590
51.9k
bail:
591
51.9k
  pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] =
592
51.9k
      initFlagsDec;
593
51.9k
  return err;
594
51.2k
}
595
596
/**
597
 * \brief Init MPEG Surround decoder.
598
 **/
599
SACDEC_ERROR mpegSurroundDecoder_Init(
600
0
    CMpegSurroundDecoder *pMpegSurroundDecoder) {
601
0
  SACDEC_ERROR err = MPS_OK;
602
603
0
  if (pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode]) {
604
0
    err = initMpegSurroundDecoder(pMpegSurroundDecoder);
605
0
  }
606
0
  return err;
607
0
}
608
609
/**
610
 * \brief Open MPEG Surround decoder.
611
 **/
612
static SACDEC_ERROR mpegSurroundDecoder_Create(
613
    CMpegSurroundDecoder **pMpegSurroundDecoder, int stereoConfigIndex,
614
55.3k
    HANDLE_FDK_QMF_DOMAIN pQmfDomain) {
615
55.3k
  SACDEC_ERROR err = MPS_OK;
616
55.3k
  CMpegSurroundDecoder *sacDec = NULL;
617
55.3k
  spatialDec *self = NULL;
618
619
  /* decoderLevel  decoderMode  maxNumOutputChannels  binauralMode */
620
55.3k
  static const SPATIAL_DEC_CONFIG decConfig = {
621
55.3k
      (CFG_LEVEL)(0), EXT_HQ_ONLY, OUTPUT_CHANNELS_DEFAULT, BINAURAL_NONE};
622
623
55.3k
  if (*pMpegSurroundDecoder == NULL) {
624
29.3k
    FDK_ALLOCATE_MEMORY_1D(*pMpegSurroundDecoder, 1, CMpegSurroundDecoder)
625
626
58.6k
    for (int i = 0; i < 1; i++) {
627
29.3k
      err = SpatialDecCreateBsFrame(&(*pMpegSurroundDecoder)->bsFrames[i],
628
29.3k
                                    &(*pMpegSurroundDecoder)->llState);
629
29.3k
      if (err != MPS_OK) {
630
0
        sacDec = *pMpegSurroundDecoder;
631
0
        goto bail;
632
0
      }
633
29.3k
    }
634
29.3k
    (*pMpegSurroundDecoder)->pQmfDomain = pQmfDomain;
635
636
29.3k
    (*pMpegSurroundDecoder)->bsFrameDelay = 1;
637
29.3k
    (*pMpegSurroundDecoder)->bsFrameParse = 0;
638
29.3k
    (*pMpegSurroundDecoder)->bsFrameDecode = 0;
639
640
29.3k
    return err;
641
29.3k
  } else {
642
26.0k
    sacDec = *pMpegSurroundDecoder;
643
26.0k
  }
644
645
26.0k
  if (sacDec->pSpatialDec == NULL) {
646
25.3k
    if ((self = FDK_SpatialDecOpen(&decConfig, stereoConfigIndex)) == NULL) {
647
0
      err = MPS_OUTOFMEMORY;
648
0
      goto bail;
649
0
    }
650
25.3k
  } else {
651
765
    self = sacDec->pSpatialDec;
652
765
  }
653
654
26.0k
  self->pQmfDomain = sacDec->pQmfDomain;
655
656
26.0k
  sacDec->pSpatialDec = self;
657
658
  /* default parameter set */
659
26.0k
  sacDec->mpegSurroundUserParams.outputMode = SACDEC_OUT_MODE_NORMAL;
660
26.0k
  sacDec->mpegSurroundUserParams.blindEnable = 0;
661
26.0k
  sacDec->mpegSurroundUserParams.bypassMode = 0;
662
26.0k
  sacDec->mpegSurroundUserParams.concealMethod = 1;
663
26.0k
  sacDec->mpegSurroundUserParams.concealNumKeepFrames = 10;
664
26.0k
  sacDec->mpegSurroundUserParams.concealFadeOutSlopeLength = 5;
665
26.0k
  sacDec->mpegSurroundUserParams.concealFadeInSlopeLength = 5;
666
26.0k
  sacDec->mpegSurroundUserParams.concealNumReleaseFrames = 3;
667
26.0k
  sacDec->mpegSurroundSscIsGlobalCfg = 0;
668
26.0k
  sacDec->mpegSurroundUseTimeInterface = 1;
669
26.0k
  sacDec->mpegSurroundDecoderLevel = decConfig.decoderLevel;
670
671
26.0k
  sacDec->upmixType = UPMIX_TYPE_NORMAL;
672
673
  /* signalize spatial decoder re-initalization */
674
26.0k
  updateMpegSurroundDecoderStatus(sacDec, MPEGS_INIT_ENFORCE_REINIT,
675
26.0k
                                  MPEGS_SYNC_LOST, MPEGS_STOP);
676
677
  /* return decoder instance */
678
26.0k
  *pMpegSurroundDecoder = sacDec;
679
26.0k
  sacDec->decConfig = decConfig;
680
681
26.0k
  SpatialDecInitParserContext(sacDec->pSpatialDec);
682
683
26.0k
  return err;
684
685
0
bail:
686
0
  if (sacDec != NULL) {
687
0
    mpegSurroundDecoder_Close(sacDec);
688
0
  }
689
0
  *pMpegSurroundDecoder = NULL;
690
0
  if (err == MPS_OK) {
691
0
    return MPS_OUTOFMEMORY;
692
0
  } else {
693
0
    return err;
694
0
  }
695
0
}
696
697
/**
698
 * \brief Config MPEG Surround decoder.
699
 **/
700
SACDEC_ERROR mpegSurroundDecoder_Config(
701
    CMpegSurroundDecoder *pMpegSurroundDecoder, HANDLE_FDK_BITSTREAM hBs,
702
    AUDIO_OBJECT_TYPE coreCodec, INT samplingRate, INT frameSize,
703
    INT numChannels, INT stereoConfigIndex, INT coreSbrFrameLengthIndex,
704
142k
    INT configBytes, const UCHAR configMode, UCHAR *configChanged) {
705
142k
  SACDEC_ERROR err = MPS_OK;
706
142k
  INT nInputChannels;
707
142k
  SPATIAL_SPECIFIC_CONFIG spatialSpecificConfig;
708
142k
  SPATIAL_SPECIFIC_CONFIG *pSsc =
709
142k
      &pMpegSurroundDecoder->spatialSpecificConfigBackup;
710
711
142k
  switch (coreCodec) {
712
0
    case AOT_DRM_USAC:
713
86.8k
    case AOT_USAC:
714
86.8k
      if (configMode == AC_CM_DET_CFG_CHANGE) {
715
        /* In config detection mode write spatial specific config parameters
716
         * into temporarily allocated structure */
717
53.4k
        err = SpatialDecParseMps212Config(
718
53.4k
            hBs, &spatialSpecificConfig, samplingRate, coreCodec,
719
53.4k
            stereoConfigIndex, coreSbrFrameLengthIndex);
720
53.4k
        nInputChannels = spatialSpecificConfig.nInputChannels;
721
53.4k
        pSsc = &spatialSpecificConfig;
722
53.4k
      } else {
723
33.4k
        err = SpatialDecParseMps212Config(
724
33.4k
            hBs, &pMpegSurroundDecoder->spatialSpecificConfigBackup,
725
33.4k
            samplingRate, coreCodec, stereoConfigIndex,
726
33.4k
            coreSbrFrameLengthIndex);
727
33.4k
        nInputChannels =
728
33.4k
            pMpegSurroundDecoder->spatialSpecificConfigBackup.nInputChannels;
729
33.4k
      }
730
86.8k
      if ((err == MPS_OK) && (numChannels != nInputChannels)) {
731
0
        err = MPS_PARSE_ERROR;
732
0
      }
733
86.8k
      break;
734
55.2k
    case AOT_ER_AAC_ELD:
735
55.2k
    case AOT_ER_AAC_LD:
736
55.2k
      if (configMode == AC_CM_DET_CFG_CHANGE) {
737
        /* In config detection mode write spatial specific config parameters
738
         * into temporarily allocated structure */
739
41.9k
        err = SpatialDecParseSpecificConfig(hBs, &spatialSpecificConfig,
740
41.9k
                                            configBytes, coreCodec);
741
41.9k
        nInputChannels = spatialSpecificConfig.nInputChannels;
742
41.9k
        pSsc = &spatialSpecificConfig;
743
41.9k
      } else {
744
13.3k
        err = SpatialDecParseSpecificConfig(
745
13.3k
            hBs, &pMpegSurroundDecoder->spatialSpecificConfigBackup,
746
13.3k
            configBytes, coreCodec);
747
13.3k
        nInputChannels =
748
13.3k
            pMpegSurroundDecoder->spatialSpecificConfigBackup.nInputChannels;
749
13.3k
      }
750
      /* check number of channels for channel_configuration > 0  */
751
55.2k
      if ((err == MPS_OK) && (numChannels > 0) &&
752
26.8k
          (numChannels != nInputChannels)) {
753
289
        err = MPS_PARSE_ERROR;
754
289
      }
755
55.2k
      break;
756
640
    default:
757
640
      err = MPS_UNSUPPORTED_FORMAT;
758
640
      break;
759
142k
  }
760
761
142k
  if (err != MPS_OK) {
762
31.6k
    goto bail;
763
31.6k
  }
764
765
111k
  err = sscCheckOutOfBand(pSsc, coreCodec, samplingRate, frameSize);
766
767
111k
  if (err != MPS_OK) {
768
4.92k
    goto bail;
769
4.92k
  }
770
771
106k
  if (configMode & AC_CM_DET_CFG_CHANGE) {
772
64.6k
    return err;
773
64.6k
  }
774
775
41.6k
  if (configMode & AC_CM_ALLOC_MEM) {
776
41.6k
    if (*configChanged) {
777
25.1k
      err = mpegSurroundDecoder_Open(&pMpegSurroundDecoder, stereoConfigIndex,
778
25.1k
                                     NULL);
779
25.1k
      if (err) {
780
0
        return err;
781
0
      }
782
25.1k
    }
783
41.6k
  }
784
785
41.6k
  {
786
41.6k
    SPATIAL_SPECIFIC_CONFIG *sscParse =
787
41.6k
        &pMpegSurroundDecoder
788
41.6k
             ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameParse];
789
790
41.6k
    if (FDK_SpatialDecCompareSpatialSpecificConfigHeader(
791
41.6k
            &pMpegSurroundDecoder->spatialSpecificConfigBackup, sscParse)) {
792
31.4k
      pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameParse] |=
793
31.4k
          MPEGS_INIT_CHANGE_HEADER;
794
      /* Error resilience code */
795
31.4k
      if (pMpegSurroundDecoder->pSpatialDec == NULL) {
796
322
        err = MPS_NOTOK;
797
322
        goto bail;
798
322
      }
799
31.1k
      SpatialDecInitParserContext(pMpegSurroundDecoder->pSpatialDec);
800
31.1k
      pMpegSurroundDecoder->pSpatialDec->pConfigCurrent =
801
31.1k
          &pMpegSurroundDecoder
802
31.1k
               ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameDecode];
803
31.1k
    }
804
41.6k
  }
805
806
41.3k
  if (err == MPS_OK) {
807
    /* We got a valid out-of-band configuration so label it accordingly. */
808
41.3k
    pMpegSurroundDecoder->mpegSurroundSscIsGlobalCfg = 1;
809
41.3k
  }
810
811
78.2k
bail:
812
78.2k
  return err;
813
41.3k
}
814
815
/**
816
 * \brief Determine MPEG Surround operation mode.
817
 **/
818
static MPEGS_OPMODE mpegSurroundOperationMode(
819
148k
    CMpegSurroundDecoder *pMpegSurroundDecoder, int mpsDataBits) {
820
148k
  MPEGS_OPMODE mode;
821
822
148k
  {
823
148k
    if ((mpsDataBits > 0) &&
824
148k
        (pMpegSurroundDecoder->mpegSurroundUserParams.blindEnable == 0)) {
825
148k
      mode = MPEGS_OPMODE_MPS_PAYLOAD; /* Mode: Normal, Stereo or Binaural */
826
148k
    } else {
827
0
      mode = MPEGS_OPMODE_NO_MPS_PAYLOAD; /* Mode: No MPEG Surround Payload */
828
0
      updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
829
0
                                      MPEGS_INIT_ERROR_PAYLOAD, MPEGS_SYNC_LOST,
830
0
                                      MPEGS_STOP);
831
0
    }
832
148k
  }
833
834
148k
  return (mode);
835
148k
}
836
837
/**
838
 * \brief  Check ssc for parse errors.
839
 *         This one is called in initMpegSurroundDecoder()
840
 *         to ensure checking of inband and out-of-band mps configs.
841
 *         Only parse errors checked here! Check for valid config is done
842
 *         in check_UParam_Build_DecConfig()!
843
 *
844
 * \param pSsc         spatial specific config handle.
845
 *
846
 * \return  MPS_OK on sucess, and else on parse error.
847
 */
848
143k
static SACDEC_ERROR sscParseCheck(const SPATIAL_SPECIFIC_CONFIG *pSsc) {
849
143k
  if (pSsc->samplingFreq > 96000) return MPS_PARSE_ERROR;
850
142k
  if (pSsc->samplingFreq < 8000) return MPS_PARSE_ERROR;
851
852
142k
  if ((pSsc->treeConfig < 0) || (pSsc->treeConfig > 7)) {
853
0
    return MPS_PARSE_ERROR;
854
0
  }
855
856
142k
  if ((pSsc->quantMode < 0) || (pSsc->quantMode > 2)) {
857
1.01k
    return MPS_PARSE_ERROR;
858
1.01k
  }
859
860
  /* now we are sure there were no parsing errors */
861
862
141k
  return MPS_OK;
863
142k
}
864
865
/**
866
 * \brief  Check number of time slots
867
 *
868
 * Basically the mps frame length must be a multiple of the core coder frame
869
 * length. The below table shows all valid configurations in detail. See ISO/IEC
870
 * 23003-1: "Table 4A - Allowed values for bsFrameLength in the Baseline MPEG
871
 * Surround Profile"
872
 *
873
 * Downmix Coder        Downmix Code      Allowed values for bsFrameLength
874
 * Allowed frame sizes for normal, downsampled and upsampled MPS Framelength
875
 *                      (QMF Samples)
876
 *
877
 * AAC 1024                  16           15, 31, 47, 63 1024  2048  3072  4096
878
 * downsampled MPS           32           31, 63 1024  2048 upsampled MPS
879
 * 8            7, 15, 23, 31, 39, 47, 55, 63, 71    1024  2048  3072  4096
880
 * 5120  6144  7168  8192  9216
881
 *
882
 * AAC 960                   15           14, 29, 44, 59 960  1920  2880  3840
883
 * downsampled MPS           30           29, 59 960  1920 upsampled MPS
884
 * 7,5           14, 29, 44, 59                        1920  3840  5760  7680
885
 *
886
 * HE-AAC 1024/2048          32           31, 63 2048  4096 downsampled MPS
887
 * 64           63                                    2048 upsampled MPS
888
 * 16           15, 31, 47, 63                        2048  4096  6144  8192
889
 *
890
 * HE-AAC 960/1920           30           29, 59 1920  3840 downsampled MPS
891
 * 60           59                                    1920 upsampled MPS
892
 * 15           14, 29, 44, 59                        1920  3840  5760  7680
893
 *
894
 * BSAC                      16           15, 31, 47, 63 1024  2048  3072  4096
895
 * downsampled MPS           32           31, 63 1024  2048 upsampled MPS
896
 * 8            7, 15, 23, 31, 39, 47, 55, 63, 71    1024  2048  3072  4096
897
 * 5120  6144  7168  8192  9216
898
 *
899
 * BSAC with SBR             32           31, 63 2048  4096 downsampled MPS
900
 * 64           63                                    2048 upsampled MPS
901
 * 16           15, 31, 47, 63                        2048  4096  6144  8192
902
 *
903
 * AAC LD 512                 8            7, 15, 23, 31, 39, 47, 55, 63, 71
904
 * 512  1024  1536  2048  2560  3072  3584  4096  4608 downsampled MPS
905
 * 16           15, 31, 47, 63                         512  1024  1536  2048
906
 *
907
 * AAC ELD 512                8            7, 15, 23, 31, 39, 47, 55, 63, 71
908
 * 512  1024  1536  2048  2560  3072  3584  4096  4608 downsampled MPS
909
 * 16           15, 31, 47, 63                         512  1024  1536  2048
910
 *
911
 * AAC ELD with SBR 512/1024 16           15, 31, 47, 63 1024  2048  3072  4096
912
 * downsampled MPS           32           31, 63 1024  2048 upsampled MPS
913
 * 8            7, 15, 23, 31, 39, 47, 55, 63, 71    1024  2048  3072  4096
914
 * 5120  6144  7168  8192  9216
915
 *
916
 * MPEG1/2 Layer II          18           17, 35, 53, 71 1152  2304  3456  4608
917
 * downsampled MPS           36           35, 71 1152  2304
918
 *
919
 * MPEG1/2 Layer III         18           17, 35, 53, 71 1152  2304  3456  4608
920
 * downsampled MPS           36           35, 71 1152  2304
921
 *
922
 * \param frameLength
923
 * \param qmfBands
924
 * \param timeSlots
925
 *
926
 * \return  error code
927
 */
928
4.40k
SACDEC_ERROR checkTimeSlots(int frameLength, int qmfBands, int timeSlots) {
929
4.40k
  int len;
930
4.40k
  int maxFrameLength;
931
932
4.40k
  if (qmfBands == 64) {
933
    /* normal MPEG Surround */
934
467
    switch (frameLength) {
935
0
      case 960:
936
0
      case 1920:
937
0
        maxFrameLength = 3840;
938
0
        break;
939
0
      case 1024:
940
0
      case 2048:
941
0
        maxFrameLength = 4096;
942
0
        break;
943
427
      case 512:
944
427
      case 1152:
945
427
        maxFrameLength = 4608;
946
427
        break;
947
40
      default:
948
40
        return MPS_PARSE_ERROR;
949
467
    }
950
3.93k
  } else if (qmfBands == 32) {
951
    /* downsampled MPEG Surround */
952
3.28k
    switch (frameLength) {
953
0
      case 960:
954
0
      case 1920:
955
0
        maxFrameLength = 1920;
956
0
        break;
957
3.26k
      case 512:
958
3.26k
      case 1024:
959
3.26k
      case 2048:
960
3.26k
        maxFrameLength = 2048;
961
3.26k
        break;
962
0
      case 1152:
963
0
        maxFrameLength = 2304;
964
0
        break;
965
22
      default:
966
22
        return MPS_PARSE_ERROR;
967
3.28k
    }
968
3.28k
  } else if (qmfBands == 128) {
969
    /* upsampled MPEG Surround */
970
652
    switch (frameLength) {
971
0
      case 1920:
972
0
        maxFrameLength = 7680;
973
0
        break;
974
0
      case 1024:
975
0
        maxFrameLength = 9216;
976
0
        break;
977
0
      case 2048:
978
0
        maxFrameLength = 8192;
979
0
        break;
980
626
      case 512:
981
626
      case 960:
982
626
      case 1152:
983
      /* no break, no support for upsampled MPEG Surround */
984
652
      default:
985
652
        return MPS_PARSE_ERROR;
986
652
    }
987
652
  } else {
988
0
    return MPS_PARSE_ERROR;
989
0
  }
990
991
3.69k
  len = frameLength;
992
993
11.4k
  while (len <= maxFrameLength) {
994
10.7k
    if (len == timeSlots * qmfBands) {
995
3.00k
      return MPS_OK;
996
3.00k
    }
997
7.79k
    len += frameLength;
998
7.79k
  }
999
689
  return MPS_PARSE_ERROR;
1000
3.69k
}
1001
1002
/**
1003
 * \brief  Check ssc for consistency (e.g. bit errors could cause trouble)
1004
 *         First of currently two ssc-checks.
1005
 *         This (old) one is called in mpegSurroundDecoder_Apply()
1006
 *         only if inband mps config is contained in stream.
1007
 *
1008
 *         New ssc check is split in two functions sscParseCheck() and
1009
 * check_UParam_Build_DecConfig(). sscParseCheck() checks only for correct
1010
 * parsing. check_UParam_Build_DecConfig() is used to check if we have a
1011
 * valid config. Both are called in initMpegSurroundDecoder() to ensure
1012
 * checking of inband and out-of-band mps configs.
1013
 *
1014
 *         If this function can be integrated into the new functions.
1015
 *         We can remove this one.
1016
 *
1017
 * \param pSsc         spatial specific config handle.
1018
 * \param frameLength
1019
 * \param sampleRate
1020
 *
1021
 * \return  MPS_OK on sucess, and else on failure.
1022
 */
1023
static SACDEC_ERROR sscCheckInBand(SPATIAL_SPECIFIC_CONFIG *pSsc,
1024
4.40k
                                   int frameLength, int sampleRate) {
1025
4.40k
  SACDEC_ERROR err = MPS_OK;
1026
4.40k
  int qmfBands;
1027
1028
4.40k
  FDK_ASSERT(pSsc != NULL);
1029
1030
  /* check ssc for parse errors */
1031
4.40k
  if (sscParseCheck(pSsc) != MPS_OK) {
1032
1.18k
    err = MPS_PARSE_ERROR;
1033
1.18k
  }
1034
1035
  /* core fs and mps fs must match */
1036
4.40k
  if (pSsc->samplingFreq != sampleRate) {
1037
1.22k
    err = MPS_PARSE_ERROR /* MPEGSDEC_SSC_PARSE_ERROR */;
1038
1.22k
  }
1039
1040
4.40k
  qmfBands = mpegSurroundDecoder_GetNrOfQmfBands(pSsc, pSsc->samplingFreq);
1041
1042
4.40k
  if (checkTimeSlots(frameLength, qmfBands, pSsc->nTimeSlots) != MPS_OK) {
1043
1.40k
    err = MPS_PARSE_ERROR;
1044
1.40k
  }
1045
1046
4.40k
  return err;
1047
4.40k
}
1048
1049
SACDEC_ERROR
1050
mpegSurroundDecoder_ConfigureQmfDomain(
1051
    CMpegSurroundDecoder *pMpegSurroundDecoder,
1052
    SAC_INPUT_CONFIG sac_dec_interface, UINT coreSamplingRate,
1053
5.37k
    AUDIO_OBJECT_TYPE coreCodec) {
1054
5.37k
  SACDEC_ERROR err = MPS_OK;
1055
5.37k
  FDK_QMF_DOMAIN_GC *pGC = NULL;
1056
1057
5.37k
  if (pMpegSurroundDecoder == NULL) {
1058
0
    return MPS_INVALID_HANDLE;
1059
0
  }
1060
1061
5.37k
  FDK_ASSERT(pMpegSurroundDecoder->pSpatialDec);
1062
1063
5.37k
  pGC = &pMpegSurroundDecoder->pQmfDomain->globalConf;
1064
5.37k
  if (pMpegSurroundDecoder->mpegSurroundSscIsGlobalCfg) {
1065
4.10k
    SPATIAL_SPECIFIC_CONFIG *pSSC =
1066
4.10k
        &pMpegSurroundDecoder->spatialSpecificConfigBackup;
1067
4.10k
    if (sac_dec_interface == SAC_INTERFACE_TIME) {
1068
      /* For SAC_INTERFACE_QMF these parameters are set by SBR. */
1069
3.29k
      pGC->nBandsAnalysis_requested = mpegSurroundDecoder_GetNrOfQmfBands(
1070
3.29k
          pSSC, coreSamplingRate); /* coreSamplingRate == outputSamplingRate for
1071
                                      SAC_INTERFACE_TIME */
1072
3.29k
      pGC->nBandsSynthesis_requested = pGC->nBandsAnalysis_requested;
1073
3.29k
      pGC->nInputChannels_requested =
1074
3.29k
          fMax((UINT)pSSC->nInputChannels, (UINT)pGC->nInputChannels_requested);
1075
3.29k
    }
1076
4.10k
    pGC->nOutputChannels_requested =
1077
4.10k
        fMax((UINT)pSSC->nOutputChannels, (UINT)pGC->nOutputChannels_requested);
1078
4.10k
  } else {
1079
1.27k
    if (sac_dec_interface == SAC_INTERFACE_TIME) {
1080
      /* For SAC_INTERFACE_QMF these parameters are set by SBR. */
1081
1.27k
      pGC->nBandsAnalysis_requested = mpegSurroundDecoder_GetNrOfQmfBands(
1082
1.27k
          NULL, coreSamplingRate); /* coreSamplingRate == outputSamplingRate for
1083
                                      SAC_INTERFACE_TIME */
1084
1.27k
      pGC->nBandsSynthesis_requested = pGC->nBandsAnalysis_requested;
1085
1.27k
      pGC->nInputChannels_requested =
1086
1.27k
          pMpegSurroundDecoder->pSpatialDec->createParams.maxNumInputChannels;
1087
1.27k
    }
1088
1.27k
    pGC->nOutputChannels_requested =
1089
1.27k
        pMpegSurroundDecoder->pSpatialDec->createParams.maxNumOutputChannels;
1090
1.27k
  }
1091
5.37k
  pGC->nQmfProcBands_requested = 64;
1092
5.37k
  pGC->nQmfProcChannels_requested =
1093
5.37k
      fMin((INT)pGC->nInputChannels_requested,
1094
5.37k
           pMpegSurroundDecoder->pSpatialDec->createParams.maxNumInputChannels);
1095
1096
5.37k
  if (coreCodec == AOT_ER_AAC_ELD) {
1097
5.37k
    pGC->flags_requested |= QMF_FLAG_MPSLDFB;
1098
5.37k
    pGC->flags_requested &= ~QMF_FLAG_CLDFB;
1099
5.37k
  }
1100
1101
5.37k
  return err;
1102
5.37k
}
1103
1104
/**
1105
 * \brief  Check out-of-band config
1106
 *
1107
 * \param pSsc         spatial specific config handle.
1108
 * \param coreCodec    core codec.
1109
 * \param sampleRate   sampling frequency.
1110
 *
1111
 * \return  errorStatus
1112
 */
1113
SACDEC_ERROR
1114
sscCheckOutOfBand(const SPATIAL_SPECIFIC_CONFIG *pSsc, const INT coreCodec,
1115
111k
                  const INT sampleRate, const INT frameSize) {
1116
111k
  FDK_ASSERT(pSsc != NULL);
1117
111k
  int qmfBands = 0;
1118
1119
  /* check ssc for parse errors */
1120
111k
  if (sscParseCheck(pSsc) != MPS_OK) {
1121
678
    return MPS_PARSE_ERROR;
1122
678
  }
1123
1124
110k
  switch (coreCodec) {
1125
84.2k
    case AOT_USAC:
1126
84.2k
    case AOT_DRM_USAC:
1127
      /* ISO/IEC 23003-1:2007(E), Chapter 6.3.3, Support for lower and higher
1128
       * sampling frequencies */
1129
84.2k
      if (pSsc->samplingFreq >= 55426) {
1130
317
        return MPS_PARSE_ERROR;
1131
317
      }
1132
83.9k
      break;
1133
83.9k
    case AOT_ER_AAC_LD:
1134
26.1k
    case AOT_ER_AAC_ELD:
1135
      /* core fs and mps fs must match */
1136
26.1k
      if (pSsc->samplingFreq != sampleRate) {
1137
1.36k
        return MPS_PARSE_ERROR;
1138
1.36k
      }
1139
1140
      /* ISO/IEC 14496-3:2009 FDAM 3: Chapter 1.5.2.3, Levels for the Low Delay
1141
       * AAC v2 profile */
1142
24.8k
      if (pSsc->samplingFreq > 48000) {
1143
199
        return MPS_PARSE_ERROR;
1144
199
      }
1145
1146
24.6k
      qmfBands = mpegSurroundDecoder_GetNrOfQmfBands(pSsc, pSsc->samplingFreq);
1147
24.6k
      switch (frameSize) {
1148
4.09k
        case 480:
1149
4.09k
          if (!((qmfBands == 32) && (pSsc->nTimeSlots == 15))) {
1150
450
            return MPS_PARSE_ERROR;
1151
450
          }
1152
3.64k
          break;
1153
3.64k
        case 960:
1154
1.53k
          if (!((qmfBands == 64) && (pSsc->nTimeSlots == 15))) {
1155
505
            return MPS_PARSE_ERROR;
1156
505
          }
1157
1.03k
          break;
1158
18.3k
        case 512:
1159
18.3k
          if (!(((qmfBands == 32) && (pSsc->nTimeSlots == 16)) ||
1160
11.6k
                ((qmfBands == 64) && (pSsc->nTimeSlots == 8)))) {
1161
1.14k
            return MPS_PARSE_ERROR;
1162
1.14k
          }
1163
17.1k
          break;
1164
17.1k
        case 1024:
1165
688
          if (!((qmfBands == 64) && (pSsc->nTimeSlots == 16))) {
1166
273
            return MPS_PARSE_ERROR;
1167
273
          }
1168
415
          break;
1169
415
        default:
1170
0
          return MPS_PARSE_ERROR;
1171
24.6k
      }
1172
22.2k
      break;
1173
22.2k
    default:
1174
0
      return MPS_PARSE_ERROR;
1175
0
      break;
1176
110k
  }
1177
1178
106k
  return MPS_OK;
1179
110k
}
1180
1181
/**
1182
 * \brief Decode MPEG Surround frame.
1183
 **/
1184
int mpegSurroundDecoder_ParseNoHeader(
1185
    CMpegSurroundDecoder *pMpegSurroundDecoder, HANDLE_FDK_BITSTREAM hBs,
1186
85.0k
    int *pMpsDataBits, int fGlobalIndependencyFlag) {
1187
85.0k
  SACDEC_ERROR err = MPS_OK;
1188
85.0k
  SPATIAL_SPECIFIC_CONFIG *sscParse;
1189
85.0k
  int bitsAvail, numSacBits;
1190
1191
85.0k
  if (pMpegSurroundDecoder == NULL || hBs == NULL) {
1192
0
    return MPS_INVALID_HANDLE;
1193
0
  }
1194
1195
85.0k
  sscParse = &pMpegSurroundDecoder
1196
85.0k
                  ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameParse];
1197
1198
85.0k
  bitsAvail = FDKgetValidBits(hBs);
1199
1200
  /* First spatial specific config is parsed into spatialSpecificConfigBackup,
1201
   * second spatialSpecificConfigBackup is copied into
1202
   * spatialSpecificConfig[bsFrameDecode] */
1203
85.0k
  if (pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameParse]) {
1204
13.4k
    FDKmemcpy(sscParse, &pMpegSurroundDecoder->spatialSpecificConfigBackup,
1205
13.4k
              sizeof(SPATIAL_SPECIFIC_CONFIG));
1206
13.4k
    pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameParse] =
1207
13.4k
        MPEGS_SYNC_FOUND;
1208
13.4k
  }
1209
1210
85.0k
  if (bitsAvail <= 0) {
1211
1
    err = MPS_PARSE_ERROR;
1212
85.0k
  } else {
1213
85.0k
    err = SpatialDecParseFrameData(
1214
85.0k
        pMpegSurroundDecoder->pSpatialDec,
1215
85.0k
        &pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameParse],
1216
85.0k
        hBs, sscParse, (UPMIXTYPE)pMpegSurroundDecoder->upmixType,
1217
85.0k
        fGlobalIndependencyFlag);
1218
85.0k
    if (err == MPS_OK) {
1219
84.8k
      pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameParse]
1220
84.8k
          .newBsData = 1;
1221
84.8k
    }
1222
85.0k
  }
1223
1224
85.0k
  numSacBits = bitsAvail - (INT)FDKgetValidBits(hBs);
1225
1226
85.0k
  if (numSacBits > bitsAvail) {
1227
244
    pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameParse]
1228
244
        .newBsData = 0;
1229
244
    err = MPS_PARSE_ERROR;
1230
244
  }
1231
1232
85.0k
  *pMpsDataBits -= numSacBits;
1233
1234
85.0k
  return err;
1235
85.0k
}
1236
1237
/**
1238
 * \brief Check, if ancType is valid.
1239
 **/
1240
static int isValidAncType(CMpegSurroundDecoder *pMpegSurroundDecoder,
1241
32.3k
                          int ancType) {
1242
32.3k
  int ret = 1;
1243
1244
32.3k
  if ((ancType != MPEGS_ANCTYPE_HEADER_AND_FRAME) &&
1245
23.4k
      (ancType != MPEGS_ANCTYPE_FRAME)) {
1246
9.42k
    ret = 0;
1247
9.42k
  }
1248
1249
32.3k
  if (ret == 0) {
1250
9.42k
    updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
1251
9.42k
                                    MPEGS_INIT_ERROR_PAYLOAD, MPEGS_SYNC_LOST,
1252
9.42k
                                    MPEGS_STOP);
1253
9.42k
  }
1254
1255
32.3k
  return (ret);
1256
32.3k
}
1257
1258
/**
1259
 * \brief Check, if ancStartStop is valid.
1260
 **/
1261
static int isValidAncStartStop(CMpegSurroundDecoder *pMpegSurroundDecoder,
1262
22.9k
                               int ancStartStop) {
1263
22.9k
  int ret = 1;
1264
1265
22.9k
  switch (ancStartStop) {
1266
4.56k
    case MPEGS_START:
1267
      /* Sequence start - start and continue - start not allowed */
1268
4.56k
      if ((pMpegSurroundDecoder->ancStartStopPrev == MPEGS_START) ||
1269
4.49k
          (pMpegSurroundDecoder->ancStartStopPrev == MPEGS_CONTINUE)) {
1270
592
        ret = 0;
1271
592
      }
1272
4.56k
      break;
1273
1274
5.80k
    case MPEGS_STOP:
1275
      /* MPS payload of the previous frame must be valid if current type is stop
1276
         Sequence startstop - stop and stop - stop not allowed
1277
         Sequence startstop - continue and stop - continue are allowed */
1278
5.80k
      if ((pMpegSurroundDecoder->ancStartStopPrev == MPEGS_STOP) ||
1279
3.10k
          (pMpegSurroundDecoder->ancStartStopPrev == MPEGS_START_STOP)) {
1280
2.73k
        ret = 0;
1281
2.73k
      }
1282
5.80k
      break;
1283
1284
2.37k
    case MPEGS_CONTINUE:
1285
12.5k
    case MPEGS_START_STOP:
1286
      /* No error detection possible for this states */
1287
12.5k
      break;
1288
22.9k
  }
1289
1290
22.9k
  if (ret == 0) {
1291
3.32k
    updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
1292
3.32k
                                    MPEGS_INIT_ERROR_PAYLOAD, MPEGS_SYNC_LOST,
1293
3.32k
                                    MPEGS_STOP);
1294
19.6k
  } else {
1295
19.6k
    pMpegSurroundDecoder->ancStartStopPrev = (MPEGS_ANCSTARTSTOP)ancStartStop;
1296
19.6k
  }
1297
1298
22.9k
  return (ret);
1299
22.9k
}
1300
1301
int mpegSurroundDecoder_Parse(CMpegSurroundDecoder *pMpegSurroundDecoder,
1302
                              HANDLE_FDK_BITSTREAM hBs, int *pMpsDataBits,
1303
                              AUDIO_OBJECT_TYPE coreCodec, int sampleRate,
1304
32.3k
                              int frameSize, int fGlobalIndependencyFlag) {
1305
32.3k
  SACDEC_ERROR err = MPS_OK;
1306
32.3k
  SPATIAL_SPECIFIC_CONFIG *sscParse;
1307
32.3k
  SPATIAL_BS_FRAME *bsFrame;
1308
32.3k
  HANDLE_FDK_BITSTREAM hMpsBsData = NULL;
1309
32.3k
  FDK_BITSTREAM mpsBsData;
1310
32.3k
  int mpsDataBits = *pMpsDataBits;
1311
32.3k
  int mpsBsBits;
1312
32.3k
  MPEGS_ANCTYPE ancType;
1313
32.3k
  MPEGS_ANCSTARTSTOP ancStartStop;
1314
1315
32.3k
  if (pMpegSurroundDecoder == NULL) {
1316
0
    return MPS_INVALID_HANDLE;
1317
0
  }
1318
1319
32.3k
  FDK_ASSERT(pMpegSurroundDecoder->pSpatialDec);
1320
1321
32.3k
  mpsBsBits = (INT)FDKgetValidBits(hBs);
1322
1323
32.3k
  sscParse = &pMpegSurroundDecoder
1324
32.3k
                  ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameParse];
1325
32.3k
  bsFrame = &pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameParse];
1326
1327
  /*
1328
     Find operation mode of mpeg surround decoder:
1329
     - MPEGS_OPMODE_EMM:            Mode: Enhanced Matrix Mode (Blind)
1330
     - MPEGS_OPMODE_MPS_PAYLOAD:    Mode: Normal, Stereo or Binaural
1331
     - MPEGS_OPMODE_NO_MPS_PAYLOAD: Mode: No MpegSurround Payload
1332
  */
1333
32.3k
  {
1334
    /* Parse ancType and ancStartStop */
1335
32.3k
    ancType = (MPEGS_ANCTYPE)FDKreadBits(hBs, 2);
1336
32.3k
    ancStartStop = (MPEGS_ANCSTARTSTOP)FDKreadBits(hBs, 2);
1337
32.3k
    mpsDataBits -= 4;
1338
1339
    /* Set valid anc type flag, if ancType signals a payload with either header
1340
     * and frame or frame */
1341
32.3k
    if (isValidAncType(pMpegSurroundDecoder, ancType)) {
1342
      /* Set valid anc startstop flag, if transmitted sequence is not illegal */
1343
22.9k
      if (isValidAncStartStop(pMpegSurroundDecoder, ancStartStop)) {
1344
19.6k
        switch (ancStartStop) {
1345
3.97k
          case MPEGS_START:
1346
            /* Assuming that core coder frame size (AAC) is smaller than MPS
1347
               coder frame size. Save audio data for next frame. */
1348
3.97k
            if (mpsDataBits > MPS_DATA_BUFFER_SIZE * 8) {
1349
1
              err = MPS_NOTOK;
1350
1
              goto bail;
1351
1
            }
1352
17.2k
            for (int i = 0; i < mpsDataBits / 8; i++) {
1353
13.3k
              pMpegSurroundDecoder->mpsData[i] = FDKreadBits(hBs, 8);
1354
13.3k
            }
1355
3.97k
            pMpegSurroundDecoder->mpsDataBits = mpsDataBits;
1356
3.97k
            break;
1357
1358
2.37k
          case MPEGS_CONTINUE:
1359
5.44k
          case MPEGS_STOP:
1360
            /* Assuming that core coder frame size (AAC) is smaller than MPS
1361
               coder frame size. Save audio data for next frame. */
1362
5.44k
            if ((pMpegSurroundDecoder->mpsDataBits + mpsDataBits) >
1363
5.44k
                MPS_DATA_BUFFER_SIZE * 8) {
1364
11
              err = MPS_NOTOK;
1365
11
              goto bail;
1366
11
            }
1367
17.1k
            for (int i = 0; i < mpsDataBits / 8; i++) {
1368
11.7k
              pMpegSurroundDecoder
1369
11.7k
                  ->mpsData[(pMpegSurroundDecoder->mpsDataBits / 8) + i] =
1370
11.7k
                  FDKreadBits(hBs, 8);
1371
11.7k
            }
1372
5.43k
            pMpegSurroundDecoder->mpsDataBits += mpsDataBits;
1373
5.43k
            FDKinitBitStream(&mpsBsData, pMpegSurroundDecoder->mpsData,
1374
5.43k
                             MAX_BUFSIZE_BYTES,
1375
5.43k
                             pMpegSurroundDecoder->mpsDataBits, BS_READER);
1376
5.43k
            hMpsBsData = &mpsBsData;
1377
5.43k
            break;
1378
1379
10.2k
          case MPEGS_START_STOP:
1380
10.2k
            pMpegSurroundDecoder->mpsDataBits = mpsDataBits;
1381
10.2k
            hMpsBsData = hBs;
1382
10.2k
            break;
1383
1384
0
          default:
1385
0
            FDK_ASSERT(0);
1386
19.6k
        }
1387
1388
19.6k
        if ((ancStartStop == MPEGS_STOP) ||
1389
16.5k
            (ancStartStop == MPEGS_START_STOP)) {
1390
13.2k
          switch (ancType) {
1391
5.60k
            case MPEGS_ANCTYPE_HEADER_AND_FRAME: {
1392
5.60k
              int parseResult, bitsRead;
1393
5.60k
              SPATIAL_SPECIFIC_CONFIG spatialSpecificConfigTmp =
1394
5.60k
                  pMpegSurroundDecoder->spatialSpecificConfigBackup;
1395
1396
              /* Parse spatial specific config */
1397
5.60k
              bitsRead = (INT)FDKgetValidBits(hMpsBsData);
1398
1399
5.60k
              err = SpatialDecParseSpecificConfigHeader(
1400
5.60k
                  hMpsBsData,
1401
5.60k
                  &pMpegSurroundDecoder->spatialSpecificConfigBackup, coreCodec,
1402
5.60k
                  pMpegSurroundDecoder->upmixType);
1403
1404
5.60k
              bitsRead = (bitsRead - (INT)FDKgetValidBits(hMpsBsData));
1405
5.60k
              parseResult = ((err == MPS_OK) ? bitsRead : -bitsRead);
1406
1407
5.60k
              if (parseResult < 0) {
1408
1.20k
                parseResult = -parseResult;
1409
1.20k
                err = MPS_PARSE_ERROR;
1410
4.40k
              } else if (err == MPS_OK) {
1411
                /* Check SSC for consistency (e.g. bit errors could cause
1412
                 * trouble) */
1413
4.40k
                err = sscCheckInBand(
1414
4.40k
                    &pMpegSurroundDecoder->spatialSpecificConfigBackup,
1415
4.40k
                    frameSize, sampleRate);
1416
4.40k
              }
1417
5.60k
              if (err != MPS_OK) {
1418
2.63k
                pMpegSurroundDecoder->spatialSpecificConfigBackup =
1419
2.63k
                    spatialSpecificConfigTmp;
1420
2.63k
                break;
1421
2.63k
              }
1422
1423
2.97k
              pMpegSurroundDecoder->mpsDataBits -= parseResult;
1424
1425
              /* Initiate re-initialization, if header has changed */
1426
2.97k
              if (FDK_SpatialDecCompareSpatialSpecificConfigHeader(
1427
2.97k
                      &pMpegSurroundDecoder->spatialSpecificConfigBackup,
1428
2.97k
                      sscParse) == MPS_UNEQUAL_SSC) {
1429
2.74k
                pMpegSurroundDecoder
1430
2.74k
                    ->initFlags[pMpegSurroundDecoder->bsFrameParse] |=
1431
2.74k
                    MPEGS_INIT_CHANGE_HEADER;
1432
2.74k
                SpatialDecInitParserContext(pMpegSurroundDecoder->pSpatialDec);
1433
                /* We found a valid in-band configuration. Therefore any
1434
                 * previous config is invalid now. */
1435
2.74k
                pMpegSurroundDecoder->mpegSurroundSscIsGlobalCfg = 0;
1436
2.74k
              }
1437
2.97k
            }
1438
2.97k
              FDK_FALLTHROUGH;
1439
10.6k
            case MPEGS_ANCTYPE_FRAME:
1440
1441
10.6k
              if (pMpegSurroundDecoder
1442
10.6k
                      ->initFlags[pMpegSurroundDecoder->bsFrameParse] &
1443
10.6k
                  MPEGS_INIT_ERROR_PAYLOAD) {
1444
3.97k
                err = MPS_PARSE_ERROR;
1445
3.97k
                break;
1446
3.97k
              }
1447
1448
              /* First spatial specific config is parsed into
1449
               * spatialSpecificConfigBackup, second spatialSpecificConfigBackup
1450
               * is copied into spatialSpecificConfig[bsFrameDecode] */
1451
6.67k
              if (pMpegSurroundDecoder
1452
6.67k
                      ->initFlags[pMpegSurroundDecoder->bsFrameParse]) {
1453
1.81k
                FDKmemcpy(sscParse,
1454
1.81k
                          &pMpegSurroundDecoder->spatialSpecificConfigBackup,
1455
1.81k
                          sizeof(SPATIAL_SPECIFIC_CONFIG));
1456
1.81k
                pMpegSurroundDecoder
1457
1.81k
                    ->fOnSync[pMpegSurroundDecoder->bsFrameParse] =
1458
1.81k
                    MPEGS_SYNC_FOUND;
1459
1.81k
              }
1460
1461
6.67k
              if (pMpegSurroundDecoder
1462
6.67k
                      ->fOnSync[pMpegSurroundDecoder->bsFrameParse] >=
1463
6.67k
                  MPEGS_SYNC_FOUND) {
1464
6.64k
                int nbits = 0, bitsAvail;
1465
1466
6.64k
                if (err != MPS_OK) {
1467
0
                  break;
1468
0
                }
1469
1470
6.64k
                bitsAvail = FDKgetValidBits(hMpsBsData);
1471
1472
6.64k
                if (bitsAvail <= 0) {
1473
66
                  err = MPS_PARSE_ERROR;
1474
6.57k
                } else {
1475
6.57k
                  err = SpatialDecParseFrameData(
1476
6.57k
                      pMpegSurroundDecoder->pSpatialDec, bsFrame, hMpsBsData,
1477
6.57k
                      sscParse, (UPMIXTYPE)pMpegSurroundDecoder->upmixType,
1478
6.57k
                      fGlobalIndependencyFlag);
1479
6.57k
                  if (err == MPS_OK) {
1480
2.38k
                    bsFrame->newBsData = 1;
1481
2.38k
                  }
1482
6.57k
                }
1483
1484
6.64k
                nbits = bitsAvail - (INT)FDKgetValidBits(hMpsBsData);
1485
1486
6.64k
                if ((nbits > bitsAvail) ||
1487
4.50k
                    (nbits > pMpegSurroundDecoder->mpsDataBits) ||
1488
1.71k
                    (pMpegSurroundDecoder->mpsDataBits > nbits + 7 &&
1489
4.92k
                     !IS_LOWDELAY(coreCodec))) {
1490
4.92k
                  bsFrame->newBsData = 0;
1491
4.92k
                  err = MPS_PARSE_ERROR;
1492
4.92k
                  break;
1493
4.92k
                }
1494
1.71k
                pMpegSurroundDecoder->mpsDataBits -= nbits;
1495
1.71k
              }
1496
1.75k
              break;
1497
1498
1.75k
            default: /* added to avoid compiler warning */
1499
0
              err = MPS_NOTOK;
1500
0
              break; /* added to avoid compiler warning */
1501
13.2k
          }          /* switch (ancType) */
1502
1503
13.2k
          if (err == MPS_OK) {
1504
606
            pMpegSurroundDecoder->ancStartStopPrev = ancStartStop;
1505
12.6k
          } else {
1506
12.6k
            updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
1507
12.6k
                                            MPEGS_INIT_ERROR_PAYLOAD,
1508
12.6k
                                            MPEGS_SYNC_LOST, MPEGS_STOP);
1509
12.6k
            pMpegSurroundDecoder->mpsDataBits = 0;
1510
12.6k
          }
1511
13.2k
        } /* (ancStartStop == MPEGS_STOP) || (ancStartStop == MPEGS_START_STOP)
1512
           */
1513
19.6k
      }   /* validAncStartStop */
1514
22.9k
    }     /* validAncType */
1515
32.3k
  }
1516
1517
32.3k
bail:
1518
1519
32.3k
  *pMpsDataBits -= (mpsBsBits - (INT)FDKgetValidBits(hBs));
1520
1521
32.3k
  return err;
1522
32.3k
}
1523
1524
int mpegSurroundDecoder_Apply(CMpegSurroundDecoder *pMpegSurroundDecoder,
1525
                              PCM_MPS *input, PCM_MPS *pTimeData,
1526
                              const int timeDataSize, int timeDataFrameSize,
1527
                              int *nChannels, int *frameSize, int sampleRate,
1528
                              AUDIO_OBJECT_TYPE coreCodec,
1529
                              AUDIO_CHANNEL_TYPE channelType[],
1530
                              UCHAR channelIndices[],
1531
                              const FDK_channelMapDescr *const mapDescr,
1532
148k
                              const INT inDataHeadroom, INT *outDataHeadroom) {
1533
148k
  SACDEC_ERROR err = MPS_OK;
1534
148k
  PCM_MPS *pTimeOut = pTimeData;
1535
148k
  PCM_MPS *TDinput = NULL;
1536
148k
  UINT initControlFlags = 0, controlFlags = 0;
1537
148k
  int timeDataRequiredSize = 0;
1538
148k
  int newData;
1539
1540
148k
  if (pMpegSurroundDecoder == NULL) {
1541
0
    return MPS_INVALID_HANDLE;
1542
0
  }
1543
1544
148k
  FDK_ASSERT(pMpegSurroundDecoder->pSpatialDec);
1545
1546
148k
  if (!FDK_chMapDescr_isValid(mapDescr)) {
1547
0
    return MPS_INVALID_HANDLE;
1548
0
  }
1549
1550
148k
  if ((*nChannels <= 0) || (*nChannels > 2)) {
1551
0
    return MPS_NOTOK;
1552
0
  }
1553
1554
148k
  pMpegSurroundDecoder->pSpatialDec->sacInDataHeadroom = inDataHeadroom;
1555
148k
  *outDataHeadroom = (INT)(8);
1556
1557
148k
  pMpegSurroundDecoder->pSpatialDec->pConfigCurrent =
1558
148k
      &pMpegSurroundDecoder
1559
148k
           ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameDecode];
1560
148k
  newData = pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameParse]
1561
148k
                .newBsData;
1562
1563
148k
  switch (mpegSurroundOperationMode(pMpegSurroundDecoder, 1000)) {
1564
148k
    case MPEGS_OPMODE_MPS_PAYLOAD:
1565
148k
      if (pMpegSurroundDecoder
1566
148k
              ->initFlags[pMpegSurroundDecoder->bsFrameDecode]) {
1567
51.2k
        err = initMpegSurroundDecoder(pMpegSurroundDecoder);
1568
51.2k
      }
1569
1570
148k
      if (err == MPS_OK) {
1571
147k
        if ((pMpegSurroundDecoder
1572
147k
                 ->fOnSync[pMpegSurroundDecoder->bsFrameDecode] !=
1573
147k
             MPEGS_SYNC_COMPLETE) &&
1574
64.5k
            (pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameDecode]
1575
64.5k
                 .bsIndependencyFlag == 1)) {
1576
          /* We got a valid header and independently decodeable frame data.
1577
              -> Go to the next sync level and start processing. */
1578
31.1k
          pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
1579
31.1k
              MPEGS_SYNC_COMPLETE;
1580
31.1k
        }
1581
147k
      } else {
1582
        /* We got a valid config header but found an error while parsing the
1583
           bitstream. Wait for the next independent frame and apply error
1584
           conealment in the meantime. */
1585
709
        pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
1586
709
            MPEGS_SYNC_FOUND;
1587
709
        controlFlags |= MPEGS_CONCEAL;
1588
709
        err = MPS_OK;
1589
709
      }
1590
      /*
1591
         Concealment:
1592
         - Bitstream is available, no sync found during bitstream processing
1593
         - Bitstream is available, sync lost due to corrupted bitstream
1594
         - Bitstream is available, sync found but no independent frame
1595
      */
1596
148k
      if (pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] !=
1597
148k
          MPEGS_SYNC_COMPLETE) {
1598
34.1k
        controlFlags |= MPEGS_CONCEAL;
1599
34.1k
      }
1600
148k
      break;
1601
1602
0
    case MPEGS_OPMODE_NO_MPS_PAYLOAD:
1603
      /* Concealment: No bitstream is available */
1604
0
      controlFlags |= MPEGS_CONCEAL;
1605
0
      break;
1606
1607
0
    default:
1608
0
      err = MPS_NOTOK;
1609
148k
  }
1610
1611
148k
  if (err != MPS_OK) {
1612
0
    goto bail;
1613
0
  }
1614
1615
  /*
1616
   * Force BypassMode if choosen by user
1617
   */
1618
148k
  if (pMpegSurroundDecoder->mpegSurroundUserParams.bypassMode) {
1619
0
    controlFlags |= MPEGS_BYPASSMODE;
1620
0
  }
1621
1622
148k
  if (pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode]) {
1623
709
    int startWithDfltCfg = 0;
1624
    /*
1625
     * Init with a default configuration if we came here and are still not
1626
     * initialized.
1627
     */
1628
709
    if (pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] &
1629
709
        MPEGS_INIT_ENFORCE_REINIT) {
1630
      /* Get default spatial specific config */
1631
706
      if (FDK_SpatialDecInitDefaultSpatialSpecificConfig(
1632
706
              &pMpegSurroundDecoder->spatialSpecificConfigBackup, coreCodec,
1633
706
              *nChannels, sampleRate,
1634
706
              *frameSize /
1635
706
                  mpegSurroundDecoder_GetNrOfQmfBands(NULL, sampleRate),
1636
706
              pMpegSurroundDecoder->mpegSurroundDecoderLevel,
1637
706
              pMpegSurroundDecoder->mpegSurroundUserParams.blindEnable)) {
1638
0
        err = MPS_NOTOK;
1639
0
        goto bail;
1640
0
      }
1641
1642
      /* Initiate re-initialization, if header has changed */
1643
706
      if (FDK_SpatialDecCompareSpatialSpecificConfigHeader(
1644
706
              &pMpegSurroundDecoder->spatialSpecificConfigBackup,
1645
706
              &pMpegSurroundDecoder->spatialSpecificConfig
1646
706
                   [pMpegSurroundDecoder->bsFrameDecode]) == MPS_UNEQUAL_SSC) {
1647
706
        pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
1648
706
            MPEGS_INIT_CHANGE_HEADER;
1649
706
        SpatialDecInitParserContext(pMpegSurroundDecoder->pSpatialDec);
1650
706
      }
1651
1652
706
      startWithDfltCfg = 1;
1653
706
    }
1654
1655
    /* First spatial specific config is parsed into spatialSpecificConfigBackup,
1656
     * second spatialSpecificConfigBackup is copied into spatialSpecificConfig
1657
     */
1658
709
    err = initMpegSurroundDecoder(pMpegSurroundDecoder);
1659
1660
709
    if (startWithDfltCfg) {
1661
      /* initialized with default config, but no sync found */
1662
      /* maybe use updateMpegSurroundDecoderStatus later on */
1663
706
      pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
1664
706
          MPEGS_SYNC_LOST;
1665
706
    }
1666
1667
    /* Since we do not have state MPEGS_SYNC_COMPLETE apply concealment */
1668
709
    controlFlags |= MPEGS_CONCEAL;
1669
1670
709
    if (err != MPS_OK) {
1671
3
      goto bail;
1672
3
    }
1673
709
  }
1674
1675
  /*
1676
   * Process MPEG Surround Audio
1677
   */
1678
148k
  initControlFlags = controlFlags;
1679
1680
  /* Check that provided output buffer is large enough. */
1681
148k
  if (pMpegSurroundDecoder->pQmfDomain->globalConf.nBandsAnalysis == 0) {
1682
0
    err = MPS_UNSUPPORTED_FORMAT;
1683
0
    goto bail;
1684
0
  }
1685
148k
  timeDataRequiredSize =
1686
148k
      (timeDataFrameSize *
1687
148k
       pMpegSurroundDecoder->pSpatialDec->numOutputChannelsAT *
1688
148k
       pMpegSurroundDecoder->pQmfDomain->globalConf.nBandsSynthesis) /
1689
148k
      pMpegSurroundDecoder->pQmfDomain->globalConf.nBandsAnalysis;
1690
148k
  if (timeDataSize < timeDataRequiredSize) {
1691
0
    err = MPS_OUTPUT_BUFFER_TOO_SMALL;
1692
0
    goto bail;
1693
0
  }
1694
1695
148k
  if ((pMpegSurroundDecoder->pSpatialDec->pConfigCurrent->syntaxFlags &
1696
148k
       SACDEC_SYNTAX_USAC) &&
1697
110k
      (pMpegSurroundDecoder->pSpatialDec->stereoConfigIndex > 1)) {
1698
16.3k
    FDK_ASSERT(timeDataRequiredSize >= timeDataFrameSize * *nChannels);
1699
    /* Place samples comprising QMF time slots spaced at QMF output Band raster
1700
     * to allow slot wise processing */
1701
16.3k
    int timeDataFrameSizeOut =
1702
16.3k
        (timeDataFrameSize *
1703
16.3k
         pMpegSurroundDecoder->pQmfDomain->globalConf.nBandsSynthesis) /
1704
16.3k
        pMpegSurroundDecoder->pQmfDomain->globalConf.nBandsAnalysis;
1705
16.3k
    TDinput = pTimeData + timeDataFrameSizeOut - timeDataFrameSize;
1706
48.9k
    for (int i = *nChannels - 1; i >= 0; i--) {
1707
32.6k
      FDKmemmove(pTimeData + (i + 1) * timeDataFrameSizeOut - timeDataFrameSize,
1708
32.6k
                 pTimeData + timeDataFrameSize * i,
1709
32.6k
                 sizeof(PCM_MPS) * timeDataFrameSize);
1710
32.6k
      FDKmemclear(pTimeData + i * timeDataFrameSizeOut,
1711
32.6k
                  sizeof(PCM_MPS) * (timeDataFrameSizeOut - timeDataFrameSize));
1712
32.6k
    }
1713
131k
  } else {
1714
131k
    if (pMpegSurroundDecoder->mpegSurroundUseTimeInterface) {
1715
37.0k
      FDKmemcpy(input, pTimeData,
1716
37.0k
                sizeof(PCM_MPS) * (*nChannels) * (*frameSize));
1717
37.0k
      TDinput = input;
1718
37.0k
    }
1719
131k
  }
1720
1721
  /*
1722
   * Process MPEG Surround Audio
1723
   */
1724
148k
  err = SpatialDecApplyFrame(
1725
148k
      pMpegSurroundDecoder->pSpatialDec,
1726
148k
      &pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameDecode],
1727
148k
      pMpegSurroundDecoder->mpegSurroundUseTimeInterface ? INPUTMODE_TIME
1728
148k
                                                         : INPUTMODE_QMF_SBR,
1729
148k
      TDinput, NULL, NULL, pTimeOut, *frameSize, &controlFlags, *nChannels,
1730
148k
      mapDescr);
1731
148k
  *nChannels = pMpegSurroundDecoder->pSpatialDec->numOutputChannelsAT;
1732
1733
148k
  if (err !=
1734
148k
      MPS_OK) { /* A fatal error occured. Go back to start and try again: */
1735
0
    updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
1736
0
                                    MPEGS_INIT_ENFORCE_REINIT, MPEGS_SYNC_LOST,
1737
0
                                    MPEGS_STOP);
1738
0
    *frameSize =
1739
0
        0; /* Declare that framework can not use the data in pTimeOut. */
1740
148k
  } else {
1741
148k
    if (((controlFlags & MPEGS_CONCEAL) &&
1742
64.3k
         !(initControlFlags & MPEGS_CONCEAL)) ||
1743
118k
        (pMpegSurroundDecoder->pSpatialDec->errInt !=
1744
118k
         MPS_OK)) { /* Account for errors that occured in
1745
                       SpatialDecApplyFrame(): */
1746
30.2k
      updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
1747
30.2k
                                      MPEGS_INIT_ERROR_PAYLOAD, MPEGS_SYNC_LOST,
1748
30.2k
                                      MPEGS_STOP);
1749
30.2k
    }
1750
148k
  }
1751
1752
148k
  if ((err == MPS_OK) && !(controlFlags & MPEGS_BYPASSMODE) &&
1753
148k
      !(pMpegSurroundDecoder->upmixType == UPMIX_TYPE_BYPASS)) {
1754
148k
    SpatialDecChannelProperties(pMpegSurroundDecoder->pSpatialDec, channelType,
1755
148k
                                channelIndices, mapDescr);
1756
148k
  }
1757
1758
148k
bail:
1759
1760
148k
  if (newData) {
1761
    /* numParameterSetsPrev shall only be read in the decode process, because of
1762
       that we can update this state variable here */
1763
85.2k
    pMpegSurroundDecoder->pSpatialDec->numParameterSetsPrev =
1764
85.2k
        pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameDecode]
1765
85.2k
            .numParameterSets;
1766
85.2k
  }
1767
1768
148k
  return (err);
1769
148k
}
1770
1771
/**
1772
 * \brief Free config dependent MPEG Surround memory.
1773
 **/
1774
SACDEC_ERROR mpegSurroundDecoder_FreeMem(
1775
155k
    CMpegSurroundDecoder *pMpegSurroundDecoder) {
1776
155k
  SACDEC_ERROR err = MPS_OK;
1777
1778
155k
  if (pMpegSurroundDecoder != NULL) {
1779
155k
    FDK_SpatialDecClose(pMpegSurroundDecoder->pSpatialDec);
1780
155k
    pMpegSurroundDecoder->pSpatialDec = NULL;
1781
155k
  }
1782
1783
155k
  return err;
1784
155k
}
1785
1786
/**
1787
 * \brief Close MPEG Surround decoder.
1788
 **/
1789
29.3k
void mpegSurroundDecoder_Close(CMpegSurroundDecoder *pMpegSurroundDecoder) {
1790
29.3k
  if (pMpegSurroundDecoder != NULL) {
1791
29.3k
    FDK_SpatialDecClose(pMpegSurroundDecoder->pSpatialDec);
1792
29.3k
    pMpegSurroundDecoder->pSpatialDec = NULL;
1793
1794
58.6k
    for (int i = 0; i < 1; i++) {
1795
29.3k
      SpatialDecCloseBsFrame(&pMpegSurroundDecoder->bsFrames[i]);
1796
29.3k
    }
1797
1798
29.3k
    FDK_FREE_MEMORY_1D(pMpegSurroundDecoder);
1799
29.3k
  }
1800
29.3k
}
1801
1802
#define SACDEC_VL0 2
1803
#define SACDEC_VL1 1
1804
#define SACDEC_VL2 0
1805
1806
149k
int mpegSurroundDecoder_GetLibInfo(LIB_INFO *info) {
1807
149k
  int i;
1808
1809
149k
  if (info == NULL) {
1810
0
    return -1;
1811
0
  }
1812
1813
  /* search for next free tab */
1814
149k
  for (i = 0; i < FDK_MODULE_LAST; i++) {
1815
149k
    if (info[i].module_id == FDK_NONE) break;
1816
149k
  }
1817
149k
  if (i == FDK_MODULE_LAST) return -1;
1818
1819
149k
  info += i;
1820
1821
149k
  info->module_id = FDK_MPSDEC;
1822
#ifdef SUPPRESS_BUILD_DATE_INFO
1823
  info->build_date = "";
1824
  info->build_time = "";
1825
#else
1826
149k
  info->build_date = __DATE__;
1827
149k
  info->build_time = __TIME__;
1828
149k
#endif
1829
149k
  info->title = "MPEG Surround Decoder";
1830
149k
  info->version = LIB_VERSION(SACDEC_VL0, SACDEC_VL1, SACDEC_VL2);
1831
149k
  LIB_VERSION_STRING(info);
1832
149k
  info->flags = 0 | CAPF_MPS_LD | CAPF_MPS_USAC | CAPF_MPS_HQ |
1833
149k
                CAPF_MPS_1CH_IN | CAPF_MPS_2CH_OUT; /* end flags */
1834
1835
149k
  return 0;
1836
149k
}
1837
1838
SACDEC_ERROR mpegSurroundDecoder_SetParam(
1839
    CMpegSurroundDecoder *pMpegSurroundDecoder, const SACDEC_PARAM param,
1840
297k
    const INT value) {
1841
297k
  SACDEC_ERROR err = MPS_OK;
1842
297k
  SPATIALDEC_PARAM *pUserParams = NULL;
1843
1844
  /* check decoder handle */
1845
297k
  if (pMpegSurroundDecoder != NULL) {
1846
    /* init local shortcuts */
1847
297k
    pUserParams = &pMpegSurroundDecoder->mpegSurroundUserParams;
1848
297k
  } else {
1849
0
    err = MPS_INVALID_HANDLE;
1850
    /* check the parameter values before exiting. */
1851
0
  }
1852
1853
  /* apply param value */
1854
297k
  switch (param) {
1855
0
    case SACDEC_OUTPUT_MODE:
1856
0
      switch ((SAC_DEC_OUTPUT_MODE)value) {
1857
0
        case SACDEC_OUT_MODE_NORMAL:
1858
0
        case SACDEC_OUT_MODE_STEREO:
1859
0
          break;
1860
0
        default:
1861
0
          err = MPS_INVALID_PARAMETER;
1862
0
      }
1863
0
      if (err == MPS_OK) {
1864
0
        if (0) {
1865
0
          err = MPS_INVALID_PARAMETER;
1866
0
        } else if (pUserParams->outputMode != (UCHAR)value) {
1867
0
          pUserParams->outputMode = (UCHAR)value;
1868
0
          pMpegSurroundDecoder
1869
0
              ->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
1870
0
              MPEGS_INIT_CHANGE_OUTPUT_MODE;
1871
0
        }
1872
0
      }
1873
0
      break;
1874
1875
148k
    case SACDEC_INTERFACE:
1876
148k
      if (value < 0 || value > 1) {
1877
0
        err = MPS_INVALID_PARAMETER;
1878
0
      }
1879
148k
      if (err != MPS_OK) {
1880
0
        goto bail;
1881
0
      }
1882
148k
      if (pMpegSurroundDecoder->mpegSurroundUseTimeInterface != (UCHAR)value) {
1883
4.69k
        pMpegSurroundDecoder->mpegSurroundUseTimeInterface = (UCHAR)value;
1884
4.69k
        pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
1885
4.69k
            MPEGS_INIT_CHANGE_TIME_FREQ_INTERFACE;
1886
4.69k
      }
1887
148k
      break;
1888
1889
0
    case SACDEC_BS_INTERRUPTION:
1890
0
      if ((err == MPS_OK) && (value != 0)) {
1891
0
        updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
1892
0
                                        MPEGS_INIT_BS_INTERRUPTION,
1893
0
                                        MPEGS_SYNC_LOST, MPEGS_STOP);
1894
0
      }
1895
0
      break;
1896
1897
0
    case SACDEC_CLEAR_HISTORY:
1898
0
      if ((err == MPS_OK) && (value != 0)) {
1899
        /* Just reset the states and go on. */
1900
0
        updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
1901
0
                                        MPEGS_INIT_CLEAR_HISTORY,
1902
0
                                        MPEGS_SYNC_LOST, MPEGS_STOP);
1903
0
      }
1904
0
      break;
1905
1906
0
    case SACDEC_CONCEAL_NUM_KEEP_FRAMES:
1907
0
      if (value < 0) { /* Check valid value range */
1908
0
        err = MPS_INVALID_PARAMETER;
1909
0
      }
1910
0
      if (err != MPS_OK) {
1911
0
        goto bail;
1912
0
      }
1913
0
      if (pUserParams->concealNumKeepFrames != (UINT)value) {
1914
0
        pUserParams->concealNumKeepFrames = (UINT)value;
1915
0
        pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
1916
0
            MPEGS_INIT_CHANGE_CONCEAL_PARAMS;
1917
0
      }
1918
0
      break;
1919
1920
0
    case SACDEC_CONCEAL_FADE_OUT_SLOPE_LENGTH:
1921
0
      if (value < 0) { /* Check valid value range */
1922
0
        err = MPS_INVALID_PARAMETER;
1923
0
      }
1924
0
      if (err != MPS_OK) {
1925
0
        goto bail;
1926
0
      }
1927
0
      if (pUserParams->concealFadeOutSlopeLength != (UINT)value) {
1928
0
        pUserParams->concealFadeOutSlopeLength = (UINT)value;
1929
0
        pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
1930
0
            MPEGS_INIT_CHANGE_CONCEAL_PARAMS;
1931
0
      }
1932
0
      break;
1933
1934
0
    case SACDEC_CONCEAL_FADE_IN_SLOPE_LENGTH:
1935
0
      if (value < 0) { /* Check valid value range */
1936
0
        err = MPS_INVALID_PARAMETER;
1937
0
      }
1938
0
      if (err != MPS_OK) {
1939
0
        goto bail;
1940
0
      }
1941
0
      if (pUserParams->concealFadeInSlopeLength != (UINT)value) {
1942
0
        pUserParams->concealFadeInSlopeLength = (UINT)value;
1943
0
        pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
1944
0
            MPEGS_INIT_CHANGE_CONCEAL_PARAMS;
1945
0
      }
1946
0
      break;
1947
1948
0
    case SACDEC_CONCEAL_NUM_RELEASE_FRAMES:
1949
0
      if (value < 0) { /* Check valid value range */
1950
0
        err = MPS_INVALID_PARAMETER;
1951
0
      }
1952
0
      if (err != MPS_OK) {
1953
0
        goto bail;
1954
0
      }
1955
0
      if (pUserParams->concealNumReleaseFrames != (UINT)value) {
1956
0
        pUserParams->concealNumReleaseFrames = (UINT)value;
1957
0
        pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
1958
0
            MPEGS_INIT_CHANGE_CONCEAL_PARAMS;
1959
0
      }
1960
0
      break;
1961
1962
149k
    default:
1963
149k
      err = MPS_INVALID_PARAMETER;
1964
149k
      break;
1965
297k
  } /* switch(param) */
1966
1967
297k
bail:
1968
297k
  return err;
1969
297k
}
1970
1971
SACDEC_ERROR mpegSurroundDecoder_IsPseudoLR(
1972
217k
    CMpegSurroundDecoder *pMpegSurroundDecoder, int *bsPseudoLr) {
1973
217k
  if (pMpegSurroundDecoder != NULL) {
1974
217k
    const SPATIAL_SPECIFIC_CONFIG *sscDecode =
1975
217k
        &pMpegSurroundDecoder
1976
217k
             ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameDecode];
1977
217k
    *bsPseudoLr = (int)sscDecode->bsPseudoLr;
1978
217k
    return MPS_OK;
1979
217k
  } else
1980
0
    return MPS_INVALID_HANDLE;
1981
217k
}
1982
1983
/**
1984
 * \brief Get the signal delay caused by the MPEG Surround decoder module.
1985
 **/
1986
148k
UINT mpegSurroundDecoder_GetDelay(const CMpegSurroundDecoder *self) {
1987
148k
  INT outputDelay = 0;
1988
1989
148k
  if (self != NULL) {
1990
148k
    const SPATIAL_SPECIFIC_CONFIG *sscDecode =
1991
148k
        &self->spatialSpecificConfig[self->bsFrameDecode];
1992
148k
    AUDIO_OBJECT_TYPE coreCodec = sscDecode->coreCodec;
1993
1994
    /* See chapter 4.5 (delay and synchronization) of ISO/IEC FDIS 23003-1 and
1995
       chapter 5.4.3 of ISO/IEC FDIS 23003-2 for details on the following
1996
       figures. */
1997
1998
148k
    if (coreCodec > AOT_NULL_OBJECT) {
1999
148k
      if (IS_LOWDELAY(coreCodec)) {
2000
        /* All low delay variants (ER-AAC-(E)LD): */
2001
37.9k
        outputDelay += 256;
2002
110k
      } else if (!IS_USAC(coreCodec)) {
2003
        /* By the method of elimination this is the GA (AAC-LC, HE-AAC, ...)
2004
         * branch: */
2005
0
        outputDelay += 320 + 257; /* cos to exp delay + QMF synthesis */
2006
0
        if (self->mpegSurroundUseTimeInterface) {
2007
0
          outputDelay += 320 + 384; /* QMF and hybrid analysis */
2008
0
        }
2009
0
      }
2010
148k
    }
2011
148k
  }
2012
2013
148k
  return (outputDelay);
2014
148k
}