Coverage Report

Created: 2024-06-17 06:33

/src/aac/libSACdec/src/sac_dec.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 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
100
101
*******************************************************************************/
102
103
#include "sac_dec_errorcodes.h"
104
#include "sac_dec.h"
105
106
#include "sac_process.h"
107
#include "sac_bitdec.h"
108
#include "sac_smoothing.h"
109
#include "sac_calcM1andM2.h"
110
#include "sac_reshapeBBEnv.h"
111
#include "sac_stp.h"
112
#include "sac_rom.h"
113
114
#include "FDK_decorrelate.h"
115
116
#include "FDK_trigFcts.h"
117
#include "FDK_matrixCalloc.h"
118
119
/* static int pbStrideTable[] = {1, 2, 5, 28}; see sac_rom.cpp */
120
121
enum {
122
  APPLY_M2_NONE = 0,    /* init value */
123
  APPLY_M2 = 1,         /* apply m2 fallback implementation */
124
  APPLY_M2_MODE212 = 2, /* apply m2 for 212 mode */
125
  APPLY_M2_MODE212_Res_PhaseCoding =
126
      3 /* apply m2 for 212 mode with residuals and phase coding */
127
};
128
129
/******************************************************************************************/
130
/* function: FDK_SpatialDecInitDefaultSpatialSpecificConfig */
131
/* output:   struct of type SPATIAL_SPECIFIC_CONFIG */
132
/* input:    core coder audio object type */
133
/* input:    nr of core channels */
134
/* input:    sampling rate */
135
/* input:    nr of time slots */
136
/* input:    decoder level */
137
/* input:    flag indicating upmix type blind */
138
/*                                                                                        */
139
/* returns:  error code */
140
/******************************************************************************************/
141
int FDK_SpatialDecInitDefaultSpatialSpecificConfig(
142
    SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig,
143
    AUDIO_OBJECT_TYPE coreCodec, int coreChannels, int samplingFreq,
144
1.47k
    int nTimeSlots, int decoderLevel, int isBlind) {
145
1.47k
  return SpatialDecDefaultSpecificConfig(pSpatialSpecificConfig, coreCodec,
146
1.47k
                                         samplingFreq, nTimeSlots, decoderLevel,
147
1.47k
                                         isBlind, coreChannels);
148
1.47k
}
149
150
/******************************************************************************************/
151
/* function: FDK_SpatialDecCompareSpatialSpecificConfigHeader */
152
/* input:    2 pointers to a ssc */
153
/*                                                                                        */
154
/* output:   - */
155
/* returns:  error code (0 = equal, <>0 unequal) */
156
/******************************************************************************************/
157
int FDK_SpatialDecCompareSpatialSpecificConfigHeader(
158
52.5k
    SPATIAL_SPECIFIC_CONFIG *pSsc1, SPATIAL_SPECIFIC_CONFIG *pSsc2) {
159
52.5k
  int result = MPS_OK;
160
161
  /* we assume: every bit must be equal */
162
52.5k
  if (FDKmemcmp(pSsc1, pSsc2, sizeof(SPATIAL_SPECIFIC_CONFIG)) != 0) {
163
41.1k
    result = MPS_UNEQUAL_SSC;
164
41.1k
  }
165
52.5k
  return result;
166
52.5k
}
167
168
/*******************************************************************************
169
 Functionname: SpatialDecClearFrameData
170
 *******************************************************************************
171
172
 Description: Clear/Fake frame data to avoid misconfiguration and allow proper
173
              error concealment.
174
 Arguments:
175
 Input:       self (frame data)
176
 Output:      No return value.
177
178
*******************************************************************************/
179
static void SpatialDecClearFrameData(
180
    spatialDec *self, /* Shall be removed */
181
53.7k
    SPATIAL_BS_FRAME *bsFrame, const SACDEC_CREATION_PARAMS *const setup) {
182
53.7k
  int i;
183
184
53.7k
  FDK_ASSERT(self != NULL);
185
53.7k
  FDK_ASSERT(bsFrame != NULL);
186
53.7k
  FDK_ASSERT(setup != NULL);
187
188
  /* do not apply shaping tools (GES or STP) */
189
161k
  for (i = 0; i < setup->maxNumOutputChannels;
190
107k
       i += 1) { /* MAX_OUTPUT_CHANNELS */
191
107k
    bsFrame->tempShapeEnableChannelSTP[i] = 0;
192
107k
    bsFrame->tempShapeEnableChannelGES[i] = 0;
193
107k
  }
194
195
53.7k
  bsFrame->TsdData->bsTsdEnable = 0;
196
197
  /* use only 1 parameter set at the end of the frame */
198
53.7k
  bsFrame->numParameterSets = 1;
199
53.7k
  bsFrame->paramSlot[0] = self->timeSlots - 1;
200
201
  /* parameter smoothing tool set to off */
202
53.7k
  bsFrame->bsSmoothMode[0] = 0;
203
53.7k
  initParameterSmoothing(self);
204
205
  /* reset residual data */
206
53.7k
  {
207
53.7k
    int resQmfBands, resTimeSlots = (1);
208
209
53.7k
    resQmfBands = setup->maxNumQmfBands;
210
211
107k
    for (i = 0; i < setup->bProcResidual
212
107k
                    ? fMin(setup->maxNumResChannels,
213
53.7k
                           setup->maxNumOttBoxes + setup->maxNumInputChannels)
214
107k
                    : 0;
215
53.7k
         i += 1) {
216
107k
      for (int j = 0; j < resTimeSlots; j += 1) {
217
3.49M
        for (int k = 0; k < resQmfBands; k += 1) {
218
3.44M
          self->qmfResidualReal__FDK[i][j][k] = FL2FXCONST_DBL(0.0f);
219
3.44M
          self->qmfResidualImag__FDK[i][j][k] = FL2FXCONST_DBL(0.0f);
220
3.44M
        }
221
53.7k
      }
222
53.7k
    }
223
53.7k
  }
224
225
53.7k
  return;
226
53.7k
}
227
228
/*******************************************************************************
229
 Functionname: FDK_SpatialDecOpen
230
 *******************************************************************************
231
232
 Description:
233
234
 Arguments:
235
236
 Return:
237
238
*******************************************************************************/
239
spatialDec *FDK_SpatialDecOpen(const SPATIAL_DEC_CONFIG *config,
240
27.1k
                               int stereoConfigIndex) {
241
27.1k
  int i;
242
27.1k
  int lfSize, hfSize;
243
27.1k
  spatialDec *self = NULL;
244
27.1k
  SACDEC_CREATION_PARAMS setup;
245
246
27.1k
  switch (config->decoderLevel) {
247
27.1k
    case DECODER_LEVEL_0: /* 212 maxNumOutputChannels== 2 */
248
27.1k
      setup.maxNumInputChannels = 1;
249
27.1k
      setup.maxNumOutputChannels = 2;
250
27.1k
      setup.maxNumQmfBands = 64;
251
27.1k
      setup.maxNumXChannels = 2;
252
27.1k
      setup.maxNumVChannels = 2;
253
27.1k
      setup.maxNumDecorChannels = 1;
254
27.1k
      setup.bProcResidual = 1;
255
27.1k
      setup.maxNumResidualChannels = 0;
256
27.1k
      setup.maxNumOttBoxes = 1;
257
27.1k
      setup.maxNumParams = setup.maxNumInputChannels + setup.maxNumOttBoxes;
258
27.1k
      break;
259
0
    default:
260
0
      return NULL;
261
27.1k
  }
262
263
27.1k
  setup.maxNumResChannels = 1;
264
265
27.1k
  {
266
27.1k
    switch (config->maxNumOutputChannels) {
267
0
      case OUTPUT_CHANNELS_2_0:
268
0
        setup.maxNumOutputChannels = fMin(setup.maxNumOutputChannels, 2);
269
0
        break;
270
27.1k
      case OUTPUT_CHANNELS_DEFAULT:
271
27.1k
      default:
272
27.1k
        break;
273
27.1k
    }
274
27.1k
  }
275
276
27.1k
  setup.maxNumHybridBands = SacGetHybridSubbands(setup.maxNumQmfBands);
277
278
27.1k
  switch (config->decoderMode) {
279
27.1k
    case EXT_HQ_ONLY:
280
27.1k
      setup.maxNumCmplxQmfBands = setup.maxNumQmfBands;
281
27.1k
      setup.maxNumCmplxHybBands = setup.maxNumHybridBands;
282
27.1k
      break;
283
0
    default:
284
0
      setup.maxNumCmplxQmfBands = fixMax(PC_NUM_BANDS, setup.maxNumQmfBands);
285
0
      setup.maxNumCmplxHybBands =
286
0
          fixMax(PC_NUM_HYB_BANDS, setup.maxNumHybridBands);
287
0
      break;
288
27.1k
  } /* switch config->decoderMode */
289
290
27.1k
  FDK_ALLOCATE_MEMORY_1D_INT(self, 1, spatialDec, SECT_DATA_L2)
291
292
27.1k
  self->createParams = setup;
293
294
27.1k
  FDK_ALLOCATE_MEMORY_1D(self->param2hyb, MAX_PARAMETER_BANDS + 1, int)
295
296
27.1k
  FDK_ALLOCATE_MEMORY_1D(self->numOttBands, setup.maxNumOttBoxes, int)
297
298
  /* allocate arrays */
299
300
27.1k
  FDK_ALLOCATE_MEMORY_1D(self->smgTime, MAX_PARAMETER_SETS, int)
301
27.1k
  FDK_ALLOCATE_MEMORY_2D(self->smgData, MAX_PARAMETER_SETS, MAX_PARAMETER_BANDS,
302
27.1k
                         UCHAR)
303
304
27.1k
  FDK_ALLOCATE_MEMORY_3D(self->ottCLD__FDK, setup.maxNumOttBoxes,
305
27.1k
                         MAX_PARAMETER_SETS, MAX_PARAMETER_BANDS, SCHAR)
306
27.1k
  FDK_ALLOCATE_MEMORY_3D(self->ottICC__FDK, setup.maxNumOttBoxes,
307
27.1k
                         MAX_PARAMETER_SETS, MAX_PARAMETER_BANDS, SCHAR)
308
27.1k
  FDK_ALLOCATE_MEMORY_3D(self->ottIPD__FDK, setup.maxNumOttBoxes,
309
27.1k
                         MAX_PARAMETER_SETS, MAX_PARAMETER_BANDS, SCHAR)
310
311
  /* Last parameters from prev frame */
312
27.1k
  FDK_ALLOCATE_MEMORY_2D(self->ottCLDidxPrev, setup.maxNumOttBoxes,
313
27.1k
                         MAX_PARAMETER_BANDS, SCHAR)
314
27.1k
  FDK_ALLOCATE_MEMORY_2D(self->ottICCidxPrev, setup.maxNumOttBoxes,
315
27.1k
                         MAX_PARAMETER_BANDS, SCHAR)
316
27.1k
  FDK_ALLOCATE_MEMORY_3D(self->ottICCdiffidx, setup.maxNumOttBoxes,
317
27.1k
                         MAX_PARAMETER_SETS, MAX_PARAMETER_BANDS, SCHAR)
318
27.1k
  FDK_ALLOCATE_MEMORY_2D(self->ottIPDidxPrev, setup.maxNumOttBoxes,
319
27.1k
                         MAX_PARAMETER_BANDS, SCHAR)
320
27.1k
  FDK_ALLOCATE_MEMORY_2D(self->arbdmxGainIdxPrev, setup.maxNumInputChannels,
321
27.1k
                         MAX_PARAMETER_BANDS, SCHAR)
322
27.1k
  FDK_ALLOCATE_MEMORY_2D(self->cmpOttCLDidxPrev, setup.maxNumOttBoxes,
323
27.1k
                         MAX_PARAMETER_BANDS, SCHAR)
324
27.1k
  FDK_ALLOCATE_MEMORY_2D(self->cmpOttICCidxPrev, setup.maxNumOttBoxes,
325
27.1k
                         MAX_PARAMETER_BANDS, SCHAR)
326
27.1k
  FDK_ALLOCATE_MEMORY_3D(self->outIdxData, setup.maxNumOttBoxes,
327
27.1k
                         MAX_PARAMETER_SETS, MAX_PARAMETER_BANDS, SCHAR)
328
329
27.1k
  FDK_ALLOCATE_MEMORY_3D(self->arbdmxGain__FDK, setup.maxNumInputChannels,
330
27.1k
                         MAX_PARAMETER_SETS, MAX_PARAMETER_BANDS, SCHAR)
331
27.1k
  FDK_ALLOCATE_MEMORY_1D(self->arbdmxAlpha__FDK, setup.maxNumInputChannels,
332
27.1k
                         FIXP_DBL)
333
27.1k
  FDK_ALLOCATE_MEMORY_1D(self->arbdmxAlphaPrev__FDK, setup.maxNumInputChannels,
334
27.1k
                         FIXP_DBL)
335
27.1k
  FDK_ALLOCATE_MEMORY_2D(self->cmpArbdmxGainIdxPrev, setup.maxNumInputChannels,
336
27.1k
                         MAX_PARAMETER_BANDS, SCHAR)
337
338
27.1k
  FDK_ALLOCATE_MEMORY_2D(self->cmpOttIPDidxPrev, setup.maxNumOttBoxes,
339
27.1k
                         MAX_PARAMETER_BANDS, SCHAR)
340
341
27.1k
  FDK_ALLOCATE_MEMORY_3D_INT(self->M2Real__FDK, setup.maxNumOutputChannels,
342
27.1k
                             setup.maxNumVChannels, MAX_PARAMETER_BANDS,
343
27.1k
                             FIXP_DBL, SECT_DATA_L2)
344
27.1k
  FDK_ALLOCATE_MEMORY_3D(self->M2Imag__FDK, setup.maxNumOutputChannels,
345
27.1k
                         setup.maxNumVChannels, MAX_PARAMETER_BANDS, FIXP_DBL)
346
347
27.1k
  FDK_ALLOCATE_MEMORY_3D_INT(self->M2RealPrev__FDK, setup.maxNumOutputChannels,
348
27.1k
                             setup.maxNumVChannels, MAX_PARAMETER_BANDS,
349
27.1k
                             FIXP_DBL, SECT_DATA_L2)
350
27.1k
  FDK_ALLOCATE_MEMORY_3D(self->M2ImagPrev__FDK, setup.maxNumOutputChannels,
351
27.1k
                         setup.maxNumVChannels, MAX_PARAMETER_BANDS, FIXP_DBL)
352
353
27.1k
  FDK_ALLOCATE_MEMORY_2D_INT_ALIGNED(
354
27.1k
      self->qmfInputReal__FDK, setup.maxNumInputChannels, setup.maxNumQmfBands,
355
27.1k
      FIXP_DBL, SECT_DATA_L2)
356
27.1k
  FDK_ALLOCATE_MEMORY_2D_INT_ALIGNED(
357
27.1k
      self->qmfInputImag__FDK, setup.maxNumInputChannels,
358
27.1k
      setup.maxNumCmplxQmfBands, FIXP_DBL, SECT_DATA_L2)
359
360
27.1k
  FDK_ALLOCATE_MEMORY_2D_INT(self->hybInputReal__FDK, setup.maxNumInputChannels,
361
27.1k
                             setup.maxNumHybridBands, FIXP_DBL, SECT_DATA_L2)
362
27.1k
  FDK_ALLOCATE_MEMORY_2D_INT(self->hybInputImag__FDK, setup.maxNumInputChannels,
363
27.1k
                             setup.maxNumCmplxHybBands, FIXP_DBL, SECT_DATA_L2)
364
365
27.1k
  if (setup.bProcResidual) {
366
27.1k
    FDK_ALLOCATE_MEMORY_1D(self->qmfResidualReal__FDK, setup.maxNumResChannels,
367
27.1k
                           FIXP_DBL **)
368
27.1k
    FDK_ALLOCATE_MEMORY_1D(self->qmfResidualImag__FDK, setup.maxNumResChannels,
369
27.1k
                           FIXP_DBL **)
370
371
27.1k
    FDK_ALLOCATE_MEMORY_1D(self->hybResidualReal__FDK, setup.maxNumResChannels,
372
27.1k
                           FIXP_DBL *)
373
27.1k
    FDK_ALLOCATE_MEMORY_1D(self->hybResidualImag__FDK, setup.maxNumResChannels,
374
27.1k
                           FIXP_DBL *)
375
376
54.3k
    for (i = 0; i < setup.maxNumResChannels; i++) {
377
27.1k
      int resQmfBands = (config->decoderMode == EXT_LP_ONLY)
378
27.1k
                            ? PC_NUM_BANDS
379
27.1k
                            : setup.maxNumQmfBands;
380
27.1k
      int resHybBands = (config->decoderMode == EXT_LP_ONLY)
381
27.1k
                            ? PC_NUM_HYB_BANDS
382
27.1k
                            : setup.maxNumHybridBands;
383
      /* Alignment is needed for USAC residuals because QMF analysis directly
384
       * writes to this buffer. */
385
27.1k
      FDK_ALLOCATE_MEMORY_2D_INT_ALIGNED(self->qmfResidualReal__FDK[i], (1),
386
27.1k
                                         resQmfBands, FIXP_DBL, SECT_DATA_L1)
387
27.1k
      FDK_ALLOCATE_MEMORY_2D_INT_ALIGNED(self->qmfResidualImag__FDK[i], (1),
388
27.1k
                                         resQmfBands, FIXP_DBL, SECT_DATA_L1)
389
390
27.1k
      FDK_ALLOCATE_MEMORY_1D(self->hybResidualReal__FDK[i],
391
27.1k
                             setup.maxNumHybridBands, FIXP_DBL)
392
27.1k
      FDK_ALLOCATE_MEMORY_1D(self->hybResidualImag__FDK[i], resHybBands,
393
27.1k
                             FIXP_DBL)
394
27.1k
    }
395
27.1k
  } /* if (setup.bProcResidual) */
396
397
27.1k
  FDK_ALLOCATE_MEMORY_2D_INT(self->wReal__FDK, setup.maxNumVChannels,
398
27.1k
                             setup.maxNumHybridBands, FIXP_DBL, SECT_DATA_L2)
399
27.1k
  FDK_ALLOCATE_MEMORY_2D_INT(self->wImag__FDK, setup.maxNumVChannels,
400
27.1k
                             setup.maxNumCmplxHybBands, FIXP_DBL, SECT_DATA_L2)
401
402
27.1k
  FDK_ALLOCATE_MEMORY_2D_INT(self->hybOutputRealDry__FDK,
403
27.1k
                             setup.maxNumOutputChannels,
404
27.1k
                             setup.maxNumHybridBands, FIXP_DBL, SECT_DATA_L2)
405
27.1k
  FDK_ALLOCATE_MEMORY_2D_INT(self->hybOutputImagDry__FDK,
406
27.1k
                             setup.maxNumOutputChannels,
407
27.1k
                             setup.maxNumCmplxHybBands, FIXP_DBL, SECT_DATA_L2)
408
409
27.1k
  FDK_ALLOCATE_MEMORY_2D_INT(self->hybOutputRealWet__FDK,
410
27.1k
                             setup.maxNumOutputChannels,
411
27.1k
                             setup.maxNumHybridBands, FIXP_DBL, SECT_DATA_L2)
412
27.1k
  FDK_ALLOCATE_MEMORY_2D_INT(self->hybOutputImagWet__FDK,
413
27.1k
                             setup.maxNumOutputChannels,
414
27.1k
                             setup.maxNumCmplxHybBands, FIXP_DBL, SECT_DATA_L2)
415
416
27.1k
  FDK_ALLOCATE_MEMORY_1D(self->hybridSynthesis, setup.maxNumOutputChannels,
417
27.1k
                         FDK_SYN_HYB_FILTER)
418
419
27.1k
  FDK_ALLOCATE_MEMORY_1D(
420
27.1k
      self->hybridAnalysis,
421
27.1k
      setup.bProcResidual ? setup.maxNumInputChannels + setup.maxNumResChannels
422
27.1k
                          : setup.maxNumInputChannels,
423
27.1k
      FDK_ANA_HYB_FILTER)
424
425
27.1k
  lfSize = 2 * BUFFER_LEN_LF * MAX_QMF_BANDS_TO_HYBRID;
426
27.1k
  {
427
27.1k
    hfSize =
428
27.1k
        BUFFER_LEN_HF * ((setup.maxNumQmfBands - MAX_QMF_BANDS_TO_HYBRID) +
429
27.1k
                         (setup.maxNumCmplxQmfBands - MAX_QMF_BANDS_TO_HYBRID));
430
27.1k
  }
431
432
27.1k
  FDK_ALLOCATE_MEMORY_2D_INT(self->pHybridAnaStatesLFdmx,
433
27.1k
                             setup.maxNumInputChannels, lfSize, FIXP_DBL,
434
27.1k
                             SECT_DATA_L2) {
435
27.1k
    FDK_ALLOCATE_MEMORY_2D(self->pHybridAnaStatesHFdmx,
436
27.1k
                           setup.maxNumInputChannels, hfSize, FIXP_DBL)
437
27.1k
  }
438
439
54.3k
  for (i = 0; i < setup.maxNumInputChannels; i++) {
440
27.1k
    FIXP_DBL *pHybridAnaStatesHFdmx;
441
442
27.1k
    pHybridAnaStatesHFdmx = self->pHybridAnaStatesHFdmx[i];
443
444
27.1k
    FDKhybridAnalysisOpen(&self->hybridAnalysis[i],
445
27.1k
                          self->pHybridAnaStatesLFdmx[i],
446
27.1k
                          lfSize * sizeof(FIXP_DBL), pHybridAnaStatesHFdmx,
447
27.1k
                          hfSize * sizeof(FIXP_DBL));
448
27.1k
  }
449
27.1k
  if (setup.bProcResidual) {
450
27.1k
    lfSize = 2 * BUFFER_LEN_LF * MAX_QMF_BANDS_TO_HYBRID;
451
27.1k
    hfSize = BUFFER_LEN_HF *
452
27.1k
             ((((config->decoderMode == EXT_LP_ONLY) ? PC_NUM_BANDS
453
27.1k
                                                     : setup.maxNumQmfBands) -
454
27.1k
               MAX_QMF_BANDS_TO_HYBRID) +
455
27.1k
              (setup.maxNumCmplxQmfBands - MAX_QMF_BANDS_TO_HYBRID));
456
457
27.1k
    FDK_ALLOCATE_MEMORY_2D_INT(self->pHybridAnaStatesLFres,
458
27.1k
                               setup.maxNumResChannels, lfSize, FIXP_DBL,
459
27.1k
                               SECT_DATA_L2)
460
27.1k
    FDK_ALLOCATE_MEMORY_2D(self->pHybridAnaStatesHFres, setup.maxNumResChannels,
461
27.1k
                           hfSize, FIXP_DBL)
462
463
27.1k
    for (i = setup.maxNumInputChannels;
464
54.3k
         i < (setup.maxNumInputChannels + setup.maxNumResChannels); i++) {
465
27.1k
      FDKhybridAnalysisOpen(
466
27.1k
          &self->hybridAnalysis[i],
467
27.1k
          self->pHybridAnaStatesLFres[i - setup.maxNumInputChannels],
468
27.1k
          lfSize * sizeof(FIXP_DBL),
469
27.1k
          self->pHybridAnaStatesHFres[i - setup.maxNumInputChannels],
470
27.1k
          hfSize * sizeof(FIXP_DBL));
471
27.1k
    }
472
27.1k
  }
473
474
27.1k
  FDK_ALLOCATE_MEMORY_1D(self->smoothState, 1, SMOOTHING_STATE)
475
27.1k
  FDK_ALLOCATE_MEMORY_1D(self->reshapeBBEnvState, 1, RESHAPE_BBENV_STATE)
476
477
27.1k
  FDK_ALLOCATE_MEMORY_1D(self->apDecor, setup.maxNumDecorChannels, DECORR_DEC)
478
27.1k
  FDK_ALLOCATE_MEMORY_2D_INT(self->pDecorBufferCplx, setup.maxNumDecorChannels,
479
27.1k
                             (2 * ((825) + (373))), FIXP_DBL, SECT_DATA_L2)
480
481
54.3k
  for (i = 0; i < setup.maxNumDecorChannels; i++) {
482
27.1k
    if (FDKdecorrelateOpen(&self->apDecor[i], self->pDecorBufferCplx[i],
483
27.1k
                           (2 * ((825) + (373))))) {
484
0
      goto bail;
485
0
    }
486
27.1k
  }
487
488
27.1k
  if (subbandTPCreate(&self->hStpDec) != MPS_OK) {
489
0
    goto bail;
490
0
  }
491
492
  /* save general decoder configuration */
493
27.1k
  self->decoderLevel = config->decoderLevel;
494
27.1k
  self->decoderMode = config->decoderMode;
495
27.1k
  self->binauralMode = config->binauralMode;
496
497
  /* preinitialize configuration */
498
27.1k
  self->partiallyComplex = (config->decoderMode != EXT_HQ_ONLY) ? 1 : 0;
499
500
  /* Set to default state */
501
27.1k
  SpatialDecConcealment_Init(&self->concealInfo, MPEGS_CONCEAL_RESET_ALL);
502
503
  /* Everything is fine so return the handle */
504
27.1k
  return self;
505
506
0
bail:
507
  /* Collector for all errors.
508
     Deallocate all memory and return a invalid handle. */
509
0
  FDK_SpatialDecClose(self);
510
511
0
  return NULL;
512
27.1k
}
513
514
/*******************************************************************************
515
 Functionname: isValidConfig
516
 *******************************************************************************
517
518
 Description: Validate if configuration is supported in present instance
519
520
 Arguments:
521
522
 Return: 1: all okay
523
         0: configuration not supported
524
*******************************************************************************/
525
static int isValidConfig(spatialDec const *const self,
526
                         const SPATIAL_DEC_UPMIX_TYPE upmixType,
527
                         SPATIALDEC_PARAM const *const pUserParams,
528
45.5k
                         const AUDIO_OBJECT_TYPE coreAot) {
529
45.5k
  UPMIXTYPE nUpmixType;
530
531
45.5k
  FDK_ASSERT(self != NULL);
532
45.5k
  FDK_ASSERT(pUserParams != NULL);
533
534
45.5k
  nUpmixType = (UPMIXTYPE)upmixType;
535
536
45.5k
  switch (nUpmixType) {
537
0
    case UPMIXTYPE_BYPASS: /* UPMIX_TYPE_BYPASS */
538
0
      break;
539
45.5k
    case UPMIXTYPE_NORMAL: /* UPMIX_TYPE_NORMAL */
540
45.5k
      break;
541
0
    default:
542
0
      return 0; /* unsupported upmixType */
543
45.5k
  }
544
545
45.5k
  return 1; /* upmixType supported */
546
45.5k
}
547
548
static SACDEC_ERROR CheckLevelTreeUpmixType(
549
    const SACDEC_CREATION_PARAMS *const pCreateParams,
550
    const SPATIAL_SPECIFIC_CONFIG *const pSsc, const int decoderLevel,
551
45.5k
    const UPMIXTYPE upmixType) {
552
45.5k
  SACDEC_ERROR err = MPS_OK;
553
45.5k
  int nOutputChannels, treeConfig;
554
555
45.5k
  FDK_ASSERT(pCreateParams != NULL);
556
45.5k
  FDK_ASSERT(pSsc != NULL);
557
558
45.5k
  treeConfig = pSsc->treeConfig;
559
560
45.5k
  switch (decoderLevel) {
561
45.5k
    case 0: {
562
45.5k
      if (treeConfig != SPATIALDEC_MODE_RSVD7) {
563
0
        err = MPS_INVALID_TREECONFIG;
564
0
        goto bail;
565
0
      }
566
45.5k
      break;
567
45.5k
    }
568
45.5k
    default:
569
0
      err = MPS_INVALID_PARAMETER /* MPS_UNIMPLEMENTED */;
570
0
      goto bail;
571
45.5k
  }
572
573
45.5k
  switch (upmixType) {
574
0
    case UPMIXTYPE_BYPASS:
575
0
      nOutputChannels = pSsc->nInputChannels;
576
0
      break;
577
45.5k
    default:
578
45.5k
      nOutputChannels = pSsc->nOutputChannels;
579
45.5k
      break;
580
45.5k
  }
581
582
  /* Is sufficient memory allocated. */
583
45.5k
  if ((pSsc->nInputChannels > pCreateParams->maxNumInputChannels) ||
584
45.5k
      (nOutputChannels > pCreateParams->maxNumOutputChannels) ||
585
45.5k
      (pSsc->nOttBoxes > pCreateParams->maxNumOttBoxes)) {
586
0
    err = MPS_INVALID_PARAMETER;
587
0
  }
588
589
45.5k
bail:
590
45.5k
  return err;
591
45.5k
}
592
593
69.6k
void SpatialDecInitParserContext(spatialDec *self) {
594
69.6k
  int i, j;
595
596
139k
  for (i = 0; i < self->createParams.maxNumOttBoxes; i += 1) {
597
2.02M
    for (j = 0; j < MAX_PARAMETER_BANDS; j++) {
598
1.95M
      self->ottCLDidxPrev[i][j] = 0;
599
1.95M
      self->ottICCidxPrev[i][j] = 0;
600
1.95M
      self->cmpOttCLDidxPrev[i][j] = 0;
601
1.95M
      self->cmpOttICCidxPrev[i][j] = 0;
602
1.95M
    }
603
69.6k
  }
604
139k
  for (i = 0; i < self->createParams.maxNumInputChannels; i++) {
605
2.02M
    for (j = 0; j < MAX_PARAMETER_BANDS; j++) {
606
1.95M
      self->arbdmxGainIdxPrev[i][j] = 0;
607
1.95M
      self->cmpArbdmxGainIdxPrev[i][j] = 0;
608
1.95M
    }
609
69.6k
  }
610
69.6k
}
611
612
/*******************************************************************************
613
 Functionname: FDK_SpatialDecInit
614
 *******************************************************************************
615
616
 Description:
617
618
 Arguments:
619
620
 Return:
621
622
*******************************************************************************/
623
624
SACDEC_ERROR FDK_SpatialDecInit(spatialDec *self, SPATIAL_BS_FRAME *frame,
625
                                SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig,
626
                                int nQmfBands,
627
                                SPATIAL_DEC_UPMIX_TYPE const upmixType,
628
45.5k
                                SPATIALDEC_PARAM *pUserParams, UINT initFlags) {
629
45.5k
  SACDEC_ERROR err = MPS_OK;
630
45.5k
  int nCh, i, j, k;
631
45.5k
  int maxQmfBands;
632
45.5k
  int bypassMode = 0;
633
634
45.5k
  self->useFDreverb = 0;
635
636
  /* check configuration parameter */
637
45.5k
  if (!isValidConfig(self, upmixType, pUserParams,
638
45.5k
                     pSpatialSpecificConfig->coreCodec)) {
639
0
    return MPS_INVALID_PARAMETER;
640
0
  }
641
642
  /* check tree configuration */
643
45.5k
  err = CheckLevelTreeUpmixType(&self->createParams, pSpatialSpecificConfig,
644
45.5k
                                self->decoderLevel, (UPMIXTYPE)upmixType);
645
45.5k
  if (err != MPS_OK) {
646
0
    goto bail;
647
0
  }
648
649
  /* Store and update instance after all checks passed successfully: */
650
45.5k
  self->upmixType = (UPMIXTYPE)upmixType;
651
652
45.5k
  if (initFlags & MPEGS_INIT_PARAMS_ERROR_CONCEALMENT) { /* At least one error
653
                                                            concealment
654
                                                            parameter changed */
655
10.6k
    err = SpatialDecConcealment_SetParam(
656
10.6k
        &self->concealInfo, SAC_DEC_CONCEAL_METHOD, pUserParams->concealMethod);
657
10.6k
    if (err != MPS_OK) {
658
0
      goto bail;
659
0
    }
660
10.6k
    err = SpatialDecConcealment_SetParam(&self->concealInfo,
661
10.6k
                                         SAC_DEC_CONCEAL_NUM_KEEP_FRAMES,
662
10.6k
                                         pUserParams->concealNumKeepFrames);
663
10.6k
    if (err != MPS_OK) {
664
0
      goto bail;
665
0
    }
666
10.6k
    err = SpatialDecConcealment_SetParam(
667
10.6k
        &self->concealInfo, SAC_DEC_CONCEAL_FADE_OUT_SLOPE_LENGTH,
668
10.6k
        pUserParams->concealFadeOutSlopeLength);
669
10.6k
    if (err != MPS_OK) {
670
0
      goto bail;
671
0
    }
672
10.6k
    err = SpatialDecConcealment_SetParam(&self->concealInfo,
673
10.6k
                                         SAC_DEC_CONCEAL_FADE_IN_SLOPE_LENGTH,
674
10.6k
                                         pUserParams->concealFadeInSlopeLength);
675
10.6k
    if (err != MPS_OK) {
676
0
      goto bail;
677
0
    }
678
10.6k
    err = SpatialDecConcealment_SetParam(&self->concealInfo,
679
10.6k
                                         SAC_DEC_CONCEAL_NUM_RELEASE_FRAMES,
680
10.6k
                                         pUserParams->concealNumReleaseFrames);
681
10.6k
    if (err != MPS_OK) {
682
0
      goto bail;
683
0
    }
684
10.6k
  }
685
686
45.5k
  if (initFlags &
687
45.5k
      MPEGS_INIT_STATES_ERROR_CONCEALMENT) { /* Set to default state */
688
10.6k
    SpatialDecConcealment_Init(&self->concealInfo, MPEGS_CONCEAL_RESET_STATE);
689
10.6k
  }
690
691
  /* determine bypass mode */
692
45.5k
  bypassMode |= pUserParams->bypassMode;
693
45.5k
  bypassMode |= ((self->upmixType == UPMIXTYPE_BYPASS) ? 1 : 0);
694
695
  /* static decoder scale depends on number of qmf bands */
696
45.5k
  switch (nQmfBands) {
697
2.60k
    case 16:
698
3.60k
    case 24:
699
28.0k
    case 32:
700
28.0k
      self->staticDecScale = 21;
701
28.0k
      break;
702
17.5k
    case 64:
703
17.5k
      self->staticDecScale = 22;
704
17.5k
      break;
705
0
    default:
706
0
      return MPS_INVALID_PARAMETER;
707
45.5k
  }
708
709
45.5k
  self->numParameterSetsPrev = 1;
710
711
45.5k
  self->qmfBands = nQmfBands;
712
  /* self->hybridBands will be updated in SpatialDecDecodeHeader() below. */
713
714
45.5k
  self->bShareDelayWithSBR = 0;
715
716
45.5k
  err = SpatialDecDecodeHeader(self, pSpatialSpecificConfig);
717
45.5k
  if (err != MPS_OK) {
718
1.48k
    goto bail;
719
1.48k
  }
720
721
44.0k
  self->stereoConfigIndex = pSpatialSpecificConfig->stereoConfigIndex;
722
723
44.0k
  if (initFlags & MPEGS_INIT_STATES_ANA_QMF_FILTER) {
724
9.15k
    self->qmfInputDelayBufPos = 0;
725
9.15k
    self->pc_filterdelay = 1; /* Division by 0 not possible */
726
9.15k
  }
727
728
44.0k
  maxQmfBands = self->qmfBands;
729
730
  /* init residual decoder */
731
732
  /* init tonality smoothing */
733
44.0k
  if (initFlags & MPEGS_INIT_STATES_PARAM) {
734
15.1k
    initParameterSmoothing(self);
735
15.1k
  }
736
737
  /* init GES */
738
44.0k
  initBBEnv(self, (initFlags & MPEGS_INIT_STATES_GES) ? 1 : 0);
739
740
  /* Clip protection is applied only for normal processing. */
741
44.0k
  if (!isTwoChMode(self->upmixType) && !bypassMode) {
742
44.0k
    self->staticDecScale += self->clipProtectGainSF__FDK;
743
44.0k
  }
744
745
44.0k
  {
746
44.0k
    UINT flags = 0;
747
44.0k
    INT initStatesFlag = (initFlags & MPEGS_INIT_STATES_ANA_QMF_FILTER) ? 1 : 0;
748
44.0k
    INT useLdFilter =
749
44.0k
        (self->pConfigCurrent->syntaxFlags & SACDEC_SYNTAX_LD) ? 1 : 0;
750
751
44.0k
    flags = self->pQmfDomain->globalConf.flags_requested;
752
44.0k
    flags &= (~(UINT)QMF_FLAG_LP);
753
754
44.0k
    if (initStatesFlag)
755
9.15k
      flags &= ~QMF_FLAG_KEEP_STATES;
756
34.9k
    else
757
34.9k
      flags |= QMF_FLAG_KEEP_STATES;
758
759
44.0k
    if (useLdFilter)
760
23.0k
      flags |= QMF_FLAG_MPSLDFB;
761
21.0k
    else
762
21.0k
      flags &= ~QMF_FLAG_MPSLDFB;
763
764
44.0k
    self->pQmfDomain->globalConf.flags_requested = flags;
765
44.0k
    FDK_QmfDomain_Configure(self->pQmfDomain);
766
767
    /* output scaling */
768
132k
    for (nCh = 0; nCh < self->numOutputChannelsAT; nCh++) {
769
88.1k
      int outputScale = 0, outputGain_e = 0, scale = -(8) + (1);
770
88.1k
      FIXP_DBL outputGain_m = getChGain(self, nCh, &outputGain_e);
771
772
88.1k
      if (!isTwoChMode(self->upmixType) && !bypassMode) {
773
88.1k
        outputScale +=
774
88.1k
            self->clipProtectGainSF__FDK; /* consider clip protection scaling at
775
                                             synthesis qmf */
776
88.1k
      }
777
778
88.1k
      scale += outputScale;
779
780
88.1k
      qmfChangeOutScalefactor(&self->pQmfDomain->QmfDomainOut[nCh].fb, scale);
781
88.1k
      qmfChangeOutGain(&self->pQmfDomain->QmfDomainOut[nCh].fb, outputGain_m,
782
88.1k
                       outputGain_e);
783
88.1k
    }
784
44.0k
  }
785
786
132k
  for (nCh = 0; nCh < self->numOutputChannelsAT; nCh++) {
787
88.1k
    FDKhybridSynthesisInit(&self->hybridSynthesis[nCh], THREE_TO_TEN,
788
88.1k
                           self->qmfBands, maxQmfBands);
789
88.1k
  }
790
791
  /* for input, residual channels and arbitrary down-mix residual channels */
792
88.1k
  for (nCh = 0; nCh < self->createParams.maxNumInputChannels; nCh++) {
793
44.0k
    FDKhybridAnalysisInit(
794
44.0k
        &self->hybridAnalysis[nCh], THREE_TO_TEN, self->qmfBands, maxQmfBands,
795
44.0k
        (initFlags & MPEGS_INIT_STATES_ANA_HYB_FILTER) ? 1 : 0);
796
44.0k
  }
797
88.1k
  for (; nCh < (self->createParams.bProcResidual
798
88.1k
                    ? (self->createParams.maxNumInputChannels +
799
88.1k
                       self->createParams.maxNumResChannels)
800
88.1k
                    : self->createParams.maxNumInputChannels);
801
44.0k
       nCh++) {
802
44.0k
    FDKhybridAnalysisInit(&self->hybridAnalysis[nCh], THREE_TO_TEN, maxQmfBands,
803
44.0k
                          maxQmfBands, 0);
804
44.0k
  }
805
806
44.0k
  {
807
88.1k
    for (k = 0; k < self->numDecorSignals; k++) {
808
44.0k
      int errCode, idec;
809
44.0k
      FDK_DECORR_TYPE decorrType = DECORR_PS;
810
44.0k
      decorrType = DECORR_LD;
811
44.0k
      if (self->pConfigCurrent->syntaxFlags &
812
44.0k
          (SACDEC_SYNTAX_USAC | SACDEC_SYNTAX_RSVD50)) {
813
21.0k
        decorrType =
814
21.0k
            ((self->treeConfig == TREE_212) && (self->decorrType == DECORR_PS))
815
21.0k
                ? DECORR_PS
816
21.0k
                : DECORR_USAC;
817
21.0k
      }
818
44.0k
      {
819
44.0k
        idec = k;
820
44.0k
        if (self->pConfigCurrent->syntaxFlags & SACDEC_SYNTAX_LD) {
821
23.0k
          if (self->treeConfig == TREE_212 && k == 0) {
822
23.0k
            idec = 2;
823
23.0k
          }
824
23.0k
        }
825
44.0k
      }
826
44.0k
      errCode = FDKdecorrelateInit(
827
44.0k
          &self->apDecor[k], self->hybridBands, decorrType, DUCKER_AUTOMATIC,
828
44.0k
          self->decorrConfig, idec, 0, /* self->partiallyComplex */
829
44.0k
          0, 0,                        /* isLegacyPS */
830
44.0k
          (initFlags & MPEGS_INIT_STATES_DECORRELATOR) ? 1 : 0);
831
44.0k
      if (errCode) return MPS_NOTOK;
832
44.0k
    }
833
44.0k
  } /* !self->partiallyComplex */
834
835
44.0k
  err = initM1andM2(self, (initFlags & MPEGS_INIT_STATES_M1M2) ? 1 : 0,
836
44.0k
                    (initFlags & MPEGS_INIT_CONFIG) ? 1 : 0);
837
44.0k
  if (err != MPS_OK) return err;
838
839
  /* Initialization of previous frame data */
840
44.0k
  if (initFlags & MPEGS_INIT_STATES_PARAM) {
841
30.2k
    for (i = 0; i < self->createParams.maxNumOttBoxes; i += 1) {
842
      /* reset icc diff data */
843
151k
      for (k = 0; k < MAX_PARAMETER_SETS; k += 1) {
844
3.95M
        for (j = 0; j < MAX_PARAMETER_BANDS; j += 1) {
845
3.81M
          self->ottICCdiffidx[i][k][j] = 0;
846
3.81M
        }
847
136k
      }
848
15.1k
    }
849
    /* Parameter Smoothing */
850
    /* robustness: init with one of the values of smgTimeTable[] = {64, 128,
851
       256, 512} to avoid division by zero in calcFilterCoeff__FDK() */
852
15.1k
    self->smoothState->prevSmgTime = smgTimeTable[2]; /* == 256 */
853
15.1k
    FDKmemclear(self->smoothState->prevSmgData,
854
15.1k
                MAX_PARAMETER_BANDS * sizeof(UCHAR));
855
15.1k
    FDKmemclear(self->smoothState->opdLeftState__FDK,
856
15.1k
                MAX_PARAMETER_BANDS * sizeof(FIXP_DBL));
857
15.1k
    FDKmemclear(self->smoothState->opdRightState__FDK,
858
15.1k
                MAX_PARAMETER_BANDS * sizeof(FIXP_DBL));
859
15.1k
  }
860
861
44.0k
  self->prevTimeSlot = -1;
862
44.0k
  self->curTimeSlot =
863
44.0k
      MAX_TIME_SLOTS + 1; /* Initialize with a invalid value to trigger
864
                             concealment if first frame has no valid data. */
865
44.0k
  self->curPs = 0;
866
867
44.0k
  subbandTPInit(self->hStpDec);
868
869
45.5k
bail:
870
45.5k
  return err;
871
44.0k
}
872
873
void SpatialDecChannelProperties(spatialDec *self,
874
                                 AUDIO_CHANNEL_TYPE channelType[],
875
                                 UCHAR channelIndices[],
876
158k
                                 const FDK_channelMapDescr *const mapDescr) {
877
158k
  if ((self == NULL) || (channelType == NULL) || (channelIndices == NULL) ||
878
158k
      (mapDescr == NULL)) {
879
0
    return; /* no extern buffer to be filled */
880
0
  }
881
882
158k
  if (self->numOutputChannelsAT !=
883
158k
      treePropertyTable[self->treeConfig].numOutputChannels) {
884
0
    int ch;
885
    /* Declare all channels to be front channels: */
886
0
    for (ch = 0; ch < self->numOutputChannelsAT; ch += 1) {
887
0
      channelType[ch] = ACT_FRONT;
888
0
      channelIndices[ch] = ch;
889
0
    }
890
158k
  } else {
891
    /* ISO/IEC FDIS 23003-1:2006(E), page 46, Table 40 bsTreeConfig */
892
158k
    switch (self->treeConfig) {
893
158k
      case TREE_212:
894
158k
        channelType[0] = ACT_FRONT;
895
158k
        channelIndices[0] = 0;
896
158k
        channelType[1] = ACT_FRONT;
897
158k
        channelIndices[1] = 1;
898
158k
        break;
899
0
      default:;
900
158k
    }
901
158k
  }
902
158k
}
903
904
/*******************************************************************************
905
 Functionname: FDK_SpatialDecClose
906
 *******************************************************************************
907
908
 Description:
909
910
 Arguments:
911
912
 Return:
913
914
*******************************************************************************/
915
916
124k
void FDK_SpatialDecClose(spatialDec *self) {
917
124k
  if (self) {
918
27.1k
    int k;
919
920
27.1k
    if (self->apDecor != NULL) {
921
54.3k
      for (k = 0; k < self->createParams.maxNumDecorChannels; k++) {
922
27.1k
        FDKdecorrelateClose(&(self->apDecor[k]));
923
27.1k
      }
924
27.1k
      FDK_FREE_MEMORY_1D(self->apDecor);
925
27.1k
    }
926
27.1k
    if (self->pDecorBufferCplx != NULL) {
927
27.1k
      FDK_FREE_MEMORY_2D(self->pDecorBufferCplx);
928
27.1k
    }
929
930
27.1k
    subbandTPDestroy(&self->hStpDec);
931
932
27.1k
    FDK_FREE_MEMORY_1D(self->reshapeBBEnvState);
933
27.1k
    FDK_FREE_MEMORY_1D(self->smoothState);
934
935
27.1k
    FDK_FREE_MEMORY_2D(self->pHybridAnaStatesLFdmx);
936
27.1k
    FDK_FREE_MEMORY_2D(self->pHybridAnaStatesHFdmx);
937
27.1k
    FDK_FREE_MEMORY_2D(self->pHybridAnaStatesLFres);
938
27.1k
    FDK_FREE_MEMORY_2D(self->pHybridAnaStatesHFres);
939
27.1k
    FDK_FREE_MEMORY_1D(self->hybridAnalysis);
940
941
27.1k
    FDK_FREE_MEMORY_1D(self->hybridSynthesis);
942
943
    /* The time buffer is passed to the decoder from outside to avoid copying
944
     * (zero copy). */
945
    /* FDK_FREE_MEMORY_2D(self->timeOut__FDK); */
946
947
27.1k
    FDK_FREE_MEMORY_2D(self->hybOutputImagWet__FDK);
948
27.1k
    FDK_FREE_MEMORY_2D(self->hybOutputRealWet__FDK);
949
950
27.1k
    FDK_FREE_MEMORY_2D(self->hybOutputImagDry__FDK);
951
27.1k
    FDK_FREE_MEMORY_2D(self->hybOutputRealDry__FDK);
952
953
27.1k
    FDK_FREE_MEMORY_2D(self->wImag__FDK);
954
27.1k
    FDK_FREE_MEMORY_2D(self->wReal__FDK);
955
956
27.1k
    if (self->createParams.bProcResidual) {
957
27.1k
      int i;
958
959
54.3k
      for (i = 0; i < self->createParams.maxNumResChannels; i++) {
960
27.1k
        if (self->hybResidualImag__FDK != NULL)
961
27.1k
          FDK_FREE_MEMORY_1D(self->hybResidualImag__FDK[i]);
962
27.1k
        if (self->hybResidualReal__FDK != NULL)
963
27.1k
          FDK_FREE_MEMORY_1D(self->hybResidualReal__FDK[i]);
964
27.1k
        if (self->qmfResidualImag__FDK != NULL)
965
27.1k
          FDK_FREE_MEMORY_2D_ALIGNED(self->qmfResidualImag__FDK[i]);
966
27.1k
        if (self->qmfResidualReal__FDK != NULL)
967
27.1k
          FDK_FREE_MEMORY_2D_ALIGNED(self->qmfResidualReal__FDK[i]);
968
27.1k
      }
969
970
27.1k
      FDK_FREE_MEMORY_1D(self->hybResidualImag__FDK);
971
27.1k
      FDK_FREE_MEMORY_1D(self->hybResidualReal__FDK);
972
973
27.1k
      FDK_FREE_MEMORY_1D(self->qmfResidualImag__FDK);
974
27.1k
      FDK_FREE_MEMORY_1D(self->qmfResidualReal__FDK);
975
976
27.1k
    } /* self->createParams.bProcResidual */
977
978
27.1k
    FDK_FREE_MEMORY_2D(self->hybInputImag__FDK);
979
27.1k
    FDK_FREE_MEMORY_2D(self->hybInputReal__FDK);
980
981
27.1k
    FDK_FREE_MEMORY_2D_ALIGNED(self->qmfInputImag__FDK);
982
27.1k
    FDK_FREE_MEMORY_2D_ALIGNED(self->qmfInputReal__FDK);
983
984
27.1k
    FDK_FREE_MEMORY_3D(self->M2ImagPrev__FDK);
985
986
27.1k
    FDK_FREE_MEMORY_3D(self->M2RealPrev__FDK);
987
988
27.1k
    FDK_FREE_MEMORY_3D(self->M2Imag__FDK);
989
990
27.1k
    FDK_FREE_MEMORY_3D(self->M2Real__FDK);
991
992
27.1k
    FDK_FREE_MEMORY_1D(self->arbdmxAlphaPrev__FDK);
993
27.1k
    FDK_FREE_MEMORY_1D(self->arbdmxAlpha__FDK);
994
995
27.1k
    FDK_FREE_MEMORY_3D(self->arbdmxGain__FDK);
996
997
27.1k
    FDK_FREE_MEMORY_3D(self->ottIPD__FDK);
998
27.1k
    FDK_FREE_MEMORY_3D(self->ottICC__FDK);
999
27.1k
    FDK_FREE_MEMORY_3D(self->ottCLD__FDK);
1000
1001
    /* Last parameters from prev frame */
1002
27.1k
    FDK_FREE_MEMORY_2D(self->ottCLDidxPrev);
1003
27.1k
    FDK_FREE_MEMORY_2D(self->ottICCidxPrev);
1004
27.1k
    FDK_FREE_MEMORY_3D(self->ottICCdiffidx);
1005
27.1k
    FDK_FREE_MEMORY_2D(self->ottIPDidxPrev);
1006
27.1k
    FDK_FREE_MEMORY_2D(self->arbdmxGainIdxPrev);
1007
1008
27.1k
    FDK_FREE_MEMORY_2D(self->cmpOttCLDidxPrev);
1009
27.1k
    FDK_FREE_MEMORY_2D(self->cmpOttICCidxPrev);
1010
27.1k
    FDK_FREE_MEMORY_3D(self->outIdxData);
1011
27.1k
    FDK_FREE_MEMORY_2D(self->cmpOttIPDidxPrev);
1012
27.1k
    FDK_FREE_MEMORY_2D(self->cmpArbdmxGainIdxPrev);
1013
1014
27.1k
    FDK_FREE_MEMORY_2D(self->smgData);
1015
27.1k
    FDK_FREE_MEMORY_1D(self->smgTime);
1016
1017
27.1k
    FDK_FREE_MEMORY_1D(self->numOttBands);
1018
1019
27.1k
    FDK_FREE_MEMORY_1D(self->param2hyb);
1020
1021
27.1k
    FDK_FREE_MEMORY_1D(self);
1022
27.1k
  }
1023
1024
124k
  return;
1025
124k
}
1026
1027
/**
1028
 * \brief Apply Surround bypass buffer copies
1029
 * \param self spatialDec handle
1030
 * \param hybInputReal
1031
 * \param hybInputImag
1032
 * \param hybOutputReal
1033
 * \param hybOutputImag
1034
 * \param numInputChannels amount if input channels available in hybInputReal
1035
 * and hybInputImag, which may differ from self->numInputChannels.
1036
 */
1037
static void SpatialDecApplyBypass(spatialDec *self, FIXP_DBL **hybInputReal,
1038
                                  FIXP_DBL **hybInputImag,
1039
                                  FIXP_DBL **hybOutputReal,
1040
                                  FIXP_DBL **hybOutputImag,
1041
0
                                  const int numInputChannels) {
1042
0
  int complexHybBands;
1043
1044
0
  complexHybBands = self->hybridBands;
1045
1046
0
  {
1047
0
    int ch;
1048
0
    int rf = -1, lf = -1, cf = -1; /* Right Front, Left Front, Center Front */
1049
1050
    /* Determine output channel indices according to tree config */
1051
0
    switch (self->treeConfig) {
1052
0
      case TREE_212: /* 212  */
1053
0
        lf = 0;
1054
0
        rf = 1;
1055
0
        break;
1056
0
      default:;
1057
0
    }
1058
1059
    /* Note: numInputChannels might not match the tree config ! */
1060
0
    switch (numInputChannels) {
1061
0
      case 1:
1062
0
        if (cf > 0) {
1063
0
          FDKmemcpy(hybOutputReal[cf], hybInputReal[0],
1064
0
                    self->hybridBands * sizeof(FIXP_DBL));
1065
0
          FDKmemcpy(hybOutputImag[cf], hybInputImag[0],
1066
0
                    complexHybBands * sizeof(FIXP_DBL));
1067
0
        } else {
1068
0
          FDKmemcpy(hybOutputReal[lf], hybInputReal[0],
1069
0
                    self->hybridBands * sizeof(FIXP_DBL));
1070
0
          FDKmemcpy(hybOutputReal[rf], hybInputReal[0],
1071
0
                    self->hybridBands * sizeof(FIXP_DBL));
1072
0
          FDKmemcpy(hybOutputImag[lf], hybInputImag[0],
1073
0
                    complexHybBands * sizeof(FIXP_DBL));
1074
0
          FDKmemcpy(hybOutputImag[rf], hybInputImag[0],
1075
0
                    complexHybBands * sizeof(FIXP_DBL));
1076
0
        }
1077
0
        break;
1078
0
      case 2:
1079
0
        FDK_ASSERT(lf != -1);
1080
0
        FDK_ASSERT(rf != -1);
1081
0
        FDKmemcpy(hybOutputReal[lf], hybInputReal[0],
1082
0
                  self->hybridBands * sizeof(FIXP_DBL));
1083
0
        FDKmemcpy(hybOutputReal[rf], hybInputReal[1],
1084
0
                  self->hybridBands * sizeof(FIXP_DBL));
1085
0
        FDKmemcpy(hybOutputImag[lf], hybInputImag[0],
1086
0
                  complexHybBands * sizeof(FIXP_DBL));
1087
0
        FDKmemcpy(hybOutputImag[rf], hybInputImag[1],
1088
0
                  complexHybBands * sizeof(FIXP_DBL));
1089
0
        break;
1090
0
    }
1091
0
    for (ch = 0; ch < self->numOutputChannelsAT; ch++) {
1092
0
      if (ch == lf || ch == rf || ch == cf) {
1093
0
        continue; /* Skip bypassed channels */
1094
0
      }
1095
0
      FDKmemclear(hybOutputReal[ch], self->hybridBands * sizeof(FIXP_DBL));
1096
0
      FDKmemclear(hybOutputImag[ch], complexHybBands * sizeof(FIXP_DBL));
1097
0
    }
1098
0
  }
1099
0
}
1100
1101
/**
1102
 * \brief Set internal error and reset error status
1103
 *
1104
 * \param self         spatialDec handle.
1105
 * \param bypassMode   pointer to bypassMode.
1106
 * \param err          error status.
1107
 *
1108
 * \return  error status.
1109
 */
1110
static SACDEC_ERROR SpatialDecSetInternalError(spatialDec *self,
1111
                                               int *bypassMode,
1112
0
                                               SACDEC_ERROR err) {
1113
0
  *bypassMode = 1;
1114
1115
0
  if (self->errInt == MPS_OK) {
1116
    /* store internal error before it gets overwritten */
1117
0
    self->errInt = err;
1118
0
  }
1119
1120
0
  return MPS_OK;
1121
0
}
1122
1123
/*******************************************************************************
1124
 Functionname: SpatialDecApplyParameterSets
1125
 *******************************************************************************
1126
1127
 Description:
1128
1129
 Arguments:
1130
1131
 Return:
1132
1133
*******************************************************************************/
1134
static SACDEC_ERROR SpatialDecApplyParameterSets(
1135
    spatialDec *self, const SPATIAL_BS_FRAME *frame, SPATIALDEC_INPUT_MODE mode,
1136
    PCM_MPS *inData,          /* Time domain input  */
1137
    FIXP_DBL **qmfInDataReal, /* QMF domain data l/r */
1138
    FIXP_DBL **qmfInDataImag, /* QMF domain data l/r */
1139
    UINT nSamples, UINT controlFlags, int numInputChannels,
1140
158k
    const FDK_channelMapDescr *const mapDescr) {
1141
158k
  SACDEC_ERROR err = MPS_OK;
1142
1143
158k
  FIXP_SGL alpha = FL2FXCONST_SGL(0.0);
1144
1145
158k
  int ts;
1146
158k
  int ch;
1147
158k
  int hyb;
1148
1149
158k
  int prevSlot = self->prevTimeSlot;
1150
158k
  int ps = self->curPs;
1151
158k
  int ts_io = 0; /* i/o dependent slot */
1152
158k
  int bypassMode = (controlFlags & MPEGS_BYPASSMODE) ? 1 : 0;
1153
1154
  /* Bypass can be triggered by the upmixType, too. */
1155
158k
  bypassMode |= ((self->upmixType == UPMIXTYPE_BYPASS) ? 1 : 0);
1156
1157
  /*
1158
   * Decode available slots
1159
   */
1160
158k
  for (ts = self->curTimeSlot;
1161
5.35M
       ts <= fixMin(self->curTimeSlot + (int)nSamples / self->qmfBands - 1,
1162
158k
                    self->timeSlots - 1);
1163
5.19M
       ts++, ts_io++) {
1164
5.19M
    int currSlot = frame->paramSlot[ps];
1165
1166
5.19M
    err = (currSlot < ts) ? MPS_WRONG_PARAMETERSETS : MPS_OK;
1167
5.19M
    if (err != MPS_OK) {
1168
0
      err = SpatialDecSetInternalError(self, &bypassMode, err);
1169
0
    }
1170
1171
    /*
1172
     * Get new parameter set
1173
     */
1174
5.19M
    if (ts == prevSlot + 1) {
1175
202k
      if (bypassMode == 0) {
1176
202k
        err = SpatialDecCalculateM1andM2(
1177
202k
            self, ps, frame); /* input: ottCLD, ottICC, ... */
1178
                              /* output: M1param(Real/Imag), M2(Real/Imag) */
1179
202k
        if (err != MPS_OK) {
1180
0
          err = SpatialDecSetInternalError(self, &bypassMode, err);
1181
0
        }
1182
202k
      }
1183
1184
202k
      if ((ps == 0) && (self->bOverwriteM1M2prev != 0)) {
1185
        /* copy matrix entries of M1/M2 of the first parameter set to the
1186
           previous matrices (of the last frame). This avoids the interpolation
1187
           of incompatible values. E.g. for residual bands the coefficients are
1188
           calculated differently compared to non-residual bands.
1189
         */
1190
5.97k
        SpatialDecBufferMatrices(self); /* input: M(1/2)param(Real/Imag) */
1191
                                        /* output: M(1/2)param(Real/Imag)Prev */
1192
5.97k
        self->bOverwriteM1M2prev = 0;
1193
5.97k
      }
1194
1195
202k
      if (bypassMode == 0) {
1196
202k
        SpatialDecSmoothM1andM2(
1197
202k
            self, frame,
1198
202k
            ps); /* input: M1param(Real/Imag)(Prev), M2(Real/Imag)(Prev) */
1199
202k
      }          /* output: M1param(Real/Imag), M2(Real/Imag) */
1200
202k
    }
1201
1202
5.19M
    if (bypassMode == 0) {
1203
5.19M
      alpha = FX_DBL2FX_SGL(fDivNorm(ts - prevSlot, currSlot - prevSlot));
1204
5.19M
    }
1205
1206
5.19M
    switch (mode) {
1207
3.46M
      case INPUTMODE_QMF_SBR:
1208
3.46M
        if (self->pConfigCurrent->syntaxFlags & SACDEC_SYNTAX_LD)
1209
14.3k
          self->bShareDelayWithSBR = 0; /* We got no hybrid delay */
1210
3.45M
        else
1211
3.45M
          self->bShareDelayWithSBR = 1;
1212
3.46M
        SpatialDecFeedQMF(
1213
3.46M
            self, qmfInDataReal, qmfInDataImag, ts_io, bypassMode,
1214
3.46M
            self->qmfInputReal__FDK, self->qmfInputImag__FDK,
1215
3.46M
            (bypassMode) ? numInputChannels : self->numInputChannels);
1216
3.46M
        break;
1217
1.72M
      case INPUTMODE_TIME:
1218
1.72M
        self->bShareDelayWithSBR = 0;
1219
1.72M
        SpatialDecQMFAnalysis(
1220
1.72M
            self, inData, ts_io, bypassMode, self->qmfInputReal__FDK,
1221
1.72M
            self->qmfInputImag__FDK,
1222
1.72M
            (bypassMode) ? numInputChannels : self->numInputChannels);
1223
1.72M
        break;
1224
0
      default:
1225
0
        break;
1226
5.19M
    }
1227
1228
5.19M
    if ((self->pConfigCurrent->syntaxFlags & SACDEC_SYNTAX_USAC) &&
1229
5.19M
        self->residualCoding) {
1230
1.33M
      int offset;
1231
1.33M
      ch = 1;
1232
1233
1.33M
      offset = self->pQmfDomain->globalConf.nBandsSynthesis *
1234
1.33M
               self->pQmfDomain->globalConf.nQmfTimeSlots;
1235
1236
1.33M
      {
1237
1.33M
        const PCM_MPS *inSamples =
1238
1.33M
            &inData[ts * self->pQmfDomain->globalConf.nBandsAnalysis];
1239
1240
1.33M
        CalculateSpaceAnalysisQmf(
1241
1.33M
            &self->pQmfDomain->QmfDomainIn[ch].fb, inSamples + (ch * offset),
1242
1.33M
            self->qmfResidualReal__FDK[0][0], self->qmfResidualImag__FDK[0][0]);
1243
1244
1.33M
        if (!isTwoChMode(self->upmixType) && !bypassMode) {
1245
1.33M
          int i;
1246
1.33M
          FIXP_DBL *RESTRICT self_qmfResidualReal__FDK_0_0 =
1247
1.33M
              &self->qmfResidualReal__FDK[0][0][0];
1248
1.33M
          FIXP_DBL *RESTRICT self_qmfResidualImag__FDK_0_0 =
1249
1.33M
              &self->qmfResidualImag__FDK[0][0][0];
1250
1251
1.33M
          if ((self->pQmfDomain->globalConf.nBandsAnalysis == 24) &&
1252
1.33M
              !(self->stereoConfigIndex == 3)) {
1253
2.17M
            for (i = 0; i < self->qmfBands; i++) {
1254
2.14M
              self_qmfResidualReal__FDK_0_0[i] =
1255
2.14M
                  fMult(scaleValueSaturate(self_qmfResidualReal__FDK_0_0[i],
1256
2.14M
                                           1 + self->sacInDataHeadroom - (1)),
1257
2.14M
                        self->clipProtectGain__FDK);
1258
2.14M
              self_qmfResidualImag__FDK_0_0[i] =
1259
2.14M
                  fMult(scaleValueSaturate(self_qmfResidualImag__FDK_0_0[i],
1260
2.14M
                                           1 + self->sacInDataHeadroom - (1)),
1261
2.14M
                        self->clipProtectGain__FDK);
1262
2.14M
            }
1263
1.30M
          } else {
1264
25.3M
            for (i = 0; i < self->qmfBands; i++) {
1265
24.0M
              self_qmfResidualReal__FDK_0_0[i] =
1266
24.0M
                  fMult(scaleValueSaturate(self_qmfResidualReal__FDK_0_0[i],
1267
24.0M
                                           self->sacInDataHeadroom - (1)),
1268
24.0M
                        self->clipProtectGain__FDK);
1269
24.0M
              self_qmfResidualImag__FDK_0_0[i] =
1270
24.0M
                  fMult(scaleValueSaturate(self_qmfResidualImag__FDK_0_0[i],
1271
24.0M
                                           self->sacInDataHeadroom - (1)),
1272
24.0M
                        self->clipProtectGain__FDK);
1273
24.0M
            }
1274
1.30M
          }
1275
1.33M
        }
1276
1.33M
      }
1277
1.33M
    }
1278
1279
5.19M
    SpatialDecHybridAnalysis(
1280
5.19M
        self, /* input: qmfInput(Real/Imag), qmfResidual(Real/Imag) */
1281
5.19M
        self->qmfInputReal__FDK, self->qmfInputImag__FDK,
1282
5.19M
        self->hybInputReal__FDK, self->hybInputImag__FDK, ts, numInputChannels);
1283
1284
5.19M
    if (bypassMode) {
1285
0
      SpatialDecApplyBypass(
1286
0
          self, self->hybInputReal__FDK, /* input: hybInput(Real/Imag) */
1287
0
          self->hybInputImag__FDK,
1288
0
          self->hybOutputRealDry__FDK, /* output: hybOutput(Real/Imag)Dry */
1289
0
          self->hybOutputImagDry__FDK, numInputChannels);
1290
0
    } else /* !bypassMode */
1291
5.19M
    {
1292
5.19M
      FIXP_DBL *pxReal[MAX_NUM_XCHANNELS] = {NULL};
1293
5.19M
      FIXP_DBL *pxImag[MAX_NUM_XCHANNELS] = {NULL};
1294
1295
5.19M
      SpatialDecCreateX(self,
1296
5.19M
                        self->hybInputReal__FDK, /* input: hybInput(Real/Imag),
1297
                                                    hybResidual(Real/Imag) */
1298
5.19M
                        self->hybInputImag__FDK, pxReal, pxImag);
1299
1300
5.19M
      {
1301
5.19M
        SpatialDecApplyM1_CreateW_Mode212(
1302
5.19M
            self, frame, pxReal, pxImag,
1303
5.19M
            self->wReal__FDK, /* output: w(Real/Imag) */
1304
5.19M
            self->wImag__FDK);
1305
5.19M
      }
1306
5.19M
      if (err != MPS_OK) goto bail;
1307
1308
5.19M
      int applyM2Config = APPLY_M2_NONE;
1309
1310
5.19M
      applyM2Config = APPLY_M2;
1311
5.19M
      if ((self->pConfigCurrent->syntaxFlags &
1312
5.19M
           (SACDEC_SYNTAX_USAC | SACDEC_SYNTAX_RSVD50)) &&
1313
5.19M
          (self->tempShapeConfig != 1) && (self->tempShapeConfig != 2)) {
1314
1.20M
        if (self->phaseCoding == 3)
1315
32.0k
          applyM2Config = APPLY_M2_MODE212_Res_PhaseCoding;
1316
1.16M
        else
1317
1.16M
          applyM2Config = APPLY_M2_MODE212;
1318
1.20M
      }
1319
1320
5.19M
      switch (applyM2Config) {
1321
1.16M
        case APPLY_M2_MODE212: {
1322
1.16M
          err = SpatialDecApplyM2_Mode212(
1323
1.16M
              self, ps, alpha, self->wReal__FDK, self->wImag__FDK,
1324
1.16M
              self->hybOutputRealDry__FDK, self->hybOutputImagDry__FDK);
1325
1.16M
        } break;
1326
32.0k
        case APPLY_M2_MODE212_Res_PhaseCoding:
1327
32.0k
          err = SpatialDecApplyM2_Mode212_ResidualsPlusPhaseCoding(
1328
32.0k
              self, ps, alpha, self->wReal__FDK, self->wImag__FDK,
1329
32.0k
              self->hybOutputRealDry__FDK, self->hybOutputImagDry__FDK);
1330
32.0k
          break;
1331
3.99M
        case APPLY_M2:
1332
3.99M
          err = SpatialDecApplyM2(
1333
3.99M
              self, ps, alpha, self->wReal__FDK, self->wImag__FDK,
1334
3.99M
              self->hybOutputRealDry__FDK, self->hybOutputImagDry__FDK,
1335
3.99M
              self->hybOutputRealWet__FDK, self->hybOutputImagWet__FDK);
1336
3.99M
          break;
1337
0
        default:
1338
0
          err = MPS_APPLY_M2_ERROR;
1339
0
          goto bail;
1340
5.19M
      }
1341
1342
5.19M
      if (err != MPS_OK) goto bail;
1343
1344
5.19M
      if ((self->tempShapeConfig == 2) && (!isTwoChMode(self->upmixType))) {
1345
3.33M
        SpatialDecReshapeBBEnv(self, frame,
1346
3.33M
                               ts); /* input: reshapeBBEnvState,
1347
                                       hybOutput(Real/Imag)(Dry/Wet),
1348
                                       hybInput(Real/Imag) */
1349
3.33M
      }                             /* output: hybOutput(Real/Imag)Dry */
1350
1351
      /* Merge parts of the dry and wet QMF buffers. */
1352
5.19M
      if ((self->tempShapeConfig == 1) && (!isTwoChMode(self->upmixType))) {
1353
1.55M
        for (ch = 0; ch < self->numOutputChannels; ch++) {
1354
13.4M
          for (hyb = 0; hyb < self->tp_hybBandBorder; hyb++) {
1355
12.4M
            self->hybOutputRealDry__FDK[ch][hyb] =
1356
12.4M
                fAddSaturate(self->hybOutputRealDry__FDK[ch][hyb],
1357
12.4M
                             self->hybOutputRealWet__FDK[ch][hyb]);
1358
12.4M
            self->hybOutputImagDry__FDK[ch][hyb] =
1359
12.4M
                fAddSaturate(self->hybOutputImagDry__FDK[ch][hyb],
1360
12.4M
                             self->hybOutputImagWet__FDK[ch][hyb]);
1361
12.4M
          } /* loop hyb */
1362
1.03M
        }   /* loop ch */
1363
518k
        err = subbandTPApply(
1364
518k
            self, frame); /* input: hStpDec, hybOutput(Real/Imag)Dry/Wet */
1365
                          /* output: hStpDec, hybOutput(Real/Imag)Dry */
1366
518k
        if (err != MPS_OK) goto bail;
1367
518k
      } /* (self->tempShapeConfig == 1) */
1368
4.67M
      else {
1369
        /* The wet signal is added to the dry signal in applyM2 if GES and STP
1370
         * are disabled */
1371
4.67M
        if ((self->tempShapeConfig == 1) || (self->tempShapeConfig == 2)) {
1372
3.33M
          int nHybBands;
1373
3.33M
          nHybBands = self->hybridBands;
1374
1375
10.0M
          for (ch = 0; ch < self->numOutputChannels; ch++) {
1376
6.67M
            FIXP_DBL *RESTRICT pRealDry = self->hybOutputRealDry__FDK[ch];
1377
6.67M
            FIXP_DBL *RESTRICT pImagDry = self->hybOutputImagDry__FDK[ch];
1378
6.67M
            FIXP_DBL *RESTRICT pRealWet = self->hybOutputRealWet__FDK[ch];
1379
6.67M
            FIXP_DBL *RESTRICT pImagWet = self->hybOutputImagWet__FDK[ch];
1380
473M
            for (hyb = 0; hyb < nHybBands; hyb++) {
1381
467M
              pRealDry[hyb] = fAddSaturate(pRealDry[hyb], pRealWet[hyb]);
1382
467M
              pImagDry[hyb] = fAddSaturate(pImagDry[hyb], pImagWet[hyb]);
1383
467M
            } /* loop hyb */
1384
6.67M
            for (; hyb < self->hybridBands; hyb++) {
1385
0
              pRealDry[hyb] = fAddSaturate(pRealDry[hyb], pRealWet[hyb]);
1386
0
            } /* loop hyb */
1387
6.67M
          }   /* loop ch */
1388
3.33M
        } /* ( self->tempShapeConfig == 1 ) || ( self->tempShapeConfig == 2 ) */
1389
4.67M
      }   /* !self->tempShapeConfig == 1 */
1390
5.19M
    }     /*  !bypassMode */
1391
1392
5.19M
    if ((self->phaseCoding == 1) && (bypassMode == 0)) {
1393
      /* only if bsPhaseCoding == 1 and bsResidualCoding == 0 */
1394
1395
291k
      SpatialDecApplyPhase(
1396
291k
          self, alpha, (ts == currSlot) /* signal the last slot of the set */
1397
291k
      );
1398
291k
    }
1399
1400
    /*
1401
     * Synthesis Filtering
1402
     */
1403
1404
5.19M
    err = SpatialDecSynthesis(
1405
5.19M
        self, ts_io,
1406
5.19M
        self->hybOutputRealDry__FDK, /* input: hybOutput(Real/Imag)Dry */
1407
5.19M
        self->hybOutputImagDry__FDK, self->timeOut__FDK, /* output: timeOut */
1408
5.19M
        numInputChannels, mapDescr);
1409
1410
5.19M
    if (err != MPS_OK) goto bail;
1411
1412
    /*
1413
     * Update parameter buffer
1414
     */
1415
5.19M
    if (ts == currSlot) {
1416
191k
      SpatialDecBufferMatrices(self); /* input: M(1/2)param(Real/Imag) */
1417
                                      /* output: M(1/2)param(Real/Imag)Prev */
1418
1419
191k
      prevSlot = currSlot;
1420
191k
      ps++;
1421
191k
    } /* if (ts==currSlot) */
1422
1423
5.19M
  } /* ts loop */
1424
1425
  /*
1426
   * Save parameter states
1427
   */
1428
158k
  self->prevTimeSlot = prevSlot;
1429
158k
  self->curTimeSlot = ts;
1430
158k
  self->curPs = ps;
1431
1432
158k
bail:
1433
1434
158k
  return err;
1435
158k
}
1436
1437
SACDEC_ERROR SpatialDecApplyFrame(
1438
    spatialDec *self,
1439
    SPATIAL_BS_FRAME *frame, /* parsed frame data to be applied */
1440
    SPATIALDEC_INPUT_MODE inputMode, PCM_MPS *inData, /* Time domain input  */
1441
    FIXP_DBL **qmfInDataReal,                         /* QMF domain data l/r */
1442
    FIXP_DBL **qmfInDataImag,                         /* QMF domain data l/r */
1443
    PCM_MPS *pcmOutBuf, /* MAX_OUTPUT_CHANNELS*MAX_TIME_SLOTS*NUM_QMF_BANDS] */
1444
    UINT nSamples, UINT *pControlFlags, int numInputChannels,
1445
158k
    const FDK_channelMapDescr *const mapDescr) {
1446
158k
  SACDEC_ERROR err = MPS_OK;
1447
1448
158k
  int fDecAndMapFrameData;
1449
158k
  int controlFlags;
1450
1451
158k
  FDK_ASSERT(self != NULL);
1452
158k
  FDK_ASSERT(pControlFlags != NULL);
1453
158k
  FDK_ASSERT(pcmOutBuf != NULL);
1454
158k
  FDK_ASSERT(self->sacInDataHeadroom >= (1));
1455
1456
158k
  self->errInt = err; /* Init internal error */
1457
1458
158k
  controlFlags = *pControlFlags;
1459
1460
158k
  if ((self->pConfigCurrent->syntaxFlags & SACDEC_SYNTAX_USAC) &&
1461
158k
      (self->stereoConfigIndex > 1)) {
1462
23.0k
    numInputChannels =
1463
23.0k
        1; /* Do not count residual channel as input channel. It is handled
1464
              seperately. */
1465
23.0k
  }
1466
1467
  /* Check if input amount of channels is consistent */
1468
158k
  if (numInputChannels != self->numInputChannels) {
1469
0
    controlFlags |= MPEGS_CONCEAL;
1470
0
    if (numInputChannels > self->createParams.maxNumInputChannels) {
1471
0
      return MPS_INVALID_PARAMETER;
1472
0
    }
1473
0
  }
1474
1475
158k
  self->timeOut__FDK = pcmOutBuf;
1476
1477
  /* Determine local function control flags */
1478
158k
  fDecAndMapFrameData = frame->newBsData;
1479
1480
158k
  if (((fDecAndMapFrameData ==
1481
158k
        0) /* assures that conceal flag will not be set for blind mode */
1482
158k
       && (self->curTimeSlot + (int)nSamples / self->qmfBands >
1483
51.0k
           self->timeSlots)) ||
1484
158k
      (frame->numParameterSets ==
1485
108k
       0)) { /* New input samples but missing side info */
1486
50.5k
    fDecAndMapFrameData = 1;
1487
50.5k
    controlFlags |= MPEGS_CONCEAL;
1488
50.5k
  }
1489
1490
158k
  if ((fDecAndMapFrameData == 0) &&
1491
158k
      (frame->paramSlot[fMax(0, frame->numParameterSets - 1)] !=
1492
489
           (self->timeSlots - 1) ||
1493
489
       self->curTimeSlot >
1494
489
           frame->paramSlot[self->curPs])) { /* Detected faulty parameter slot
1495
                                                data. */
1496
0
    fDecAndMapFrameData = 1;
1497
0
    controlFlags |= MPEGS_CONCEAL;
1498
0
  }
1499
1500
  /* Update concealment state machine */
1501
158k
  SpatialDecConcealment_UpdateState(
1502
158k
      &self->concealInfo,
1503
158k
      (controlFlags & MPEGS_CONCEAL)
1504
158k
          ? 0
1505
158k
          : 1); /* convert from conceal flag to frame ok flag */
1506
1507
158k
  if (fDecAndMapFrameData) {
1508
    /* Reset spatial framing control vars */
1509
158k
    frame->newBsData = 0;
1510
158k
    self->prevTimeSlot = -1;
1511
158k
    self->curTimeSlot = 0;
1512
158k
    self->curPs = 0;
1513
1514
158k
    if (controlFlags & MPEGS_CONCEAL) {
1515
      /* Reset frame data to avoid misconfiguration. */
1516
53.7k
      SpatialDecClearFrameData(self, frame, &self->createParams);
1517
53.7k
    }
1518
1519
158k
    {
1520
158k
      err = SpatialDecDecodeFrame(self, frame); /* input: ... */
1521
      /* output: decodeAndMapFrameDATA */
1522
158k
    }
1523
1524
158k
    if (err != MPS_OK) {
1525
      /* Rescue strategy is to apply bypass mode in order
1526
         to keep at least the downmix channels continuous. */
1527
0
      controlFlags |= MPEGS_CONCEAL;
1528
0
      if (self->errInt == MPS_OK) {
1529
        /* store internal error befor it gets overwritten */
1530
0
        self->errInt = err;
1531
0
      }
1532
0
    }
1533
158k
  }
1534
1535
158k
  err = SpatialDecApplyParameterSets(
1536
158k
      self, frame, inputMode, inData, qmfInDataReal, qmfInDataImag, nSamples,
1537
158k
      controlFlags | ((err == MPS_OK) ? 0 : MPEGS_BYPASSMODE), numInputChannels,
1538
158k
      mapDescr);
1539
158k
  if (err != MPS_OK) {
1540
0
    goto bail;
1541
0
  }
1542
1543
158k
bail:
1544
1545
158k
  *pControlFlags = controlFlags;
1546
1547
158k
  return err;
1548
158k
}