Coverage Report

Created: 2026-04-12 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aac/libSACdec/src/sac_bitdec.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 Dec bitstream decoder
100
101
*******************************************************************************/
102
103
#include "sac_bitdec.h"
104
105
#include "sac_dec_errorcodes.h"
106
#include "nlc_dec.h"
107
#include "sac_rom.h"
108
#include "FDK_matrixCalloc.h"
109
#include "sac_tsd.h"
110
111
enum {
112
  ottVsTotInactiv = 0,
113
  ottVsTotDb1Activ = 1,
114
  ottVsTotDb2Activ = 2,
115
  ottVsTotDb1Db2Activ = 3
116
};
117
118
static SACDEC_ERROR SpatialDecDecodeHelperInfo(
119
165k
    SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig, UPMIXTYPE upmixType) {
120
165k
  int i;
121
165k
  UINT syntaxFlags;
122
123
  /* Determine bit stream syntax */
124
165k
  syntaxFlags = 0;
125
165k
  switch (pSpatialSpecificConfig->coreCodec) {
126
64.9k
    case AOT_ER_AAC_ELD:
127
64.9k
    case AOT_ER_AAC_LD:
128
64.9k
      syntaxFlags |= SACDEC_SYNTAX_LD;
129
64.9k
      break;
130
100k
    case AOT_USAC:
131
100k
      syntaxFlags |= SACDEC_SYNTAX_USAC;
132
100k
      break;
133
0
    case AOT_NONE:
134
679
    default:
135
679
      return MPS_UNSUPPORTED_FORMAT;
136
165k
  }
137
138
165k
  pSpatialSpecificConfig->syntaxFlags = syntaxFlags;
139
140
165k
  switch (pSpatialSpecificConfig->treeConfig) {
141
165k
    case TREE_212: {
142
165k
      pSpatialSpecificConfig->ottCLDdefault[0] = 0;
143
165k
    } break;
144
0
    default:
145
0
      return MPS_INVALID_TREECONFIG;
146
165k
  }
147
148
165k
  if (syntaxFlags & SACDEC_SYNTAX_USAC) {
149
100k
    if (pSpatialSpecificConfig->bsOttBandsPhasePresent) {
150
20.1k
      pSpatialSpecificConfig->numOttBandsIPD =
151
20.1k
          pSpatialSpecificConfig->bsOttBandsPhase;
152
80.1k
    } else {
153
80.1k
      int numParameterBands;
154
155
80.1k
      numParameterBands = pSpatialSpecificConfig->freqRes;
156
80.1k
      switch (numParameterBands) {
157
2.56k
        case 4:
158
5.43k
        case 5:
159
5.43k
          pSpatialSpecificConfig->numOttBandsIPD = 2;
160
5.43k
          break;
161
33.0k
        case 7:
162
33.0k
          pSpatialSpecificConfig->numOttBandsIPD = 3;
163
33.0k
          break;
164
11.1k
        case 10:
165
11.1k
          pSpatialSpecificConfig->numOttBandsIPD = 5;
166
11.1k
          break;
167
10.0k
        case 14:
168
10.0k
          pSpatialSpecificConfig->numOttBandsIPD = 7;
169
10.0k
          break;
170
15.4k
        case 20:
171
20.4k
        case 28:
172
20.4k
          pSpatialSpecificConfig->numOttBandsIPD = 10;
173
20.4k
          break;
174
0
        default:
175
0
          return MPS_INVALID_PARAMETERBANDS;
176
80.1k
      }
177
80.1k
    }
178
100k
  } else {
179
64.9k
    pSpatialSpecificConfig->numOttBandsIPD = 0;
180
64.9k
  }
181
330k
  for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
182
165k
    {
183
165k
      pSpatialSpecificConfig->bitstreamOttBands[i] =
184
165k
          pSpatialSpecificConfig->freqRes;
185
165k
    }
186
165k
    {
187
165k
      pSpatialSpecificConfig->numOttBands[i] =
188
165k
          pSpatialSpecificConfig->bitstreamOttBands[i];
189
165k
      if (syntaxFlags & SACDEC_SYNTAX_USAC &&
190
100k
          !pSpatialSpecificConfig->bsOttBandsPhasePresent) {
191
80.1k
        if (pSpatialSpecificConfig->bResidualCoding &&
192
26.1k
            pSpatialSpecificConfig->ResidualConfig[i].bResidualPresent &&
193
26.1k
            (pSpatialSpecificConfig->numOttBandsIPD <
194
26.1k
             pSpatialSpecificConfig->ResidualConfig[i].nResidualBands)) {
195
6.18k
          pSpatialSpecificConfig->numOttBandsIPD =
196
6.18k
              pSpatialSpecificConfig->ResidualConfig[i].nResidualBands;
197
6.18k
        }
198
80.1k
      }
199
165k
    }
200
165k
  } /* i */
201
202
165k
  return MPS_OK;
203
165k
}
204
205
/*******************************************************************************
206
 Functionname: SpatialDecParseExtensionConfig
207
 *******************************************************************************
208
209
 Description:
210
211
 Arguments:
212
213
 Return:
214
215
*******************************************************************************/
216
217
static SACDEC_ERROR SpatialDecParseExtensionConfig(
218
    HANDLE_FDK_BITSTREAM bitstream, SPATIAL_SPECIFIC_CONFIG *config,
219
30.5k
    int numOttBoxes, int numTttBoxes, int numOutChan, int bitsAvailable) {
220
30.5k
  SACDEC_ERROR err = MPS_OK;
221
30.5k
  INT ba = bitsAvailable;
222
223
30.5k
  config->sacExtCnt = 0;
224
30.5k
  config->bResidualCoding = 0;
225
226
30.5k
  ba = fMin((int)FDKgetValidBits(bitstream), ba);
227
228
82.0k
  while ((ba >= 8) && (config->sacExtCnt < MAX_NUM_EXT_TYPES)) {
229
52.6k
    int bitsRead, nFillBits;
230
52.6k
    INT tmp;
231
52.6k
    UINT sacExtLen;
232
233
52.6k
    config->sacExtType[config->sacExtCnt] = FDKreadBits(bitstream, 4);
234
52.6k
    ba -= 4;
235
236
52.6k
    sacExtLen = FDKreadBits(bitstream, 4);
237
52.6k
    ba -= 4;
238
239
52.6k
    if (sacExtLen == 15) {
240
2.63k
      sacExtLen += FDKreadBits(bitstream, 8);
241
2.63k
      ba -= 8;
242
2.63k
      if (sacExtLen == 15 + 255) {
243
883
        sacExtLen += FDKreadBits(bitstream, 16);
244
883
        ba -= 16;
245
883
      }
246
2.63k
    }
247
248
52.6k
    tmp = (INT)FDKgetValidBits(
249
52.6k
        bitstream); /* Extension config payload start anchor. */
250
52.6k
    if ((tmp <= 0) || (tmp < (INT)sacExtLen * 8) || (ba < (INT)sacExtLen * 8)) {
251
1.14k
      err = MPS_PARSE_ERROR;
252
1.14k
      goto bail;
253
1.14k
    }
254
255
51.5k
    switch (config->sacExtType[config->sacExtCnt]) {
256
51.5k
      default:; /* unknown extension data => do nothing */
257
51.5k
    }
258
259
    /* skip remaining extension data */
260
51.5k
    bitsRead = tmp - FDKgetValidBits(bitstream);
261
51.5k
    nFillBits = 8 * sacExtLen - bitsRead;
262
263
51.5k
    if (nFillBits < 0) {
264
0
      err = MPS_PARSE_ERROR;
265
0
      goto bail;
266
51.5k
    } else {
267
      /* Skip fill bits or an unkown extension. */
268
51.5k
      FDKpushFor(bitstream, nFillBits);
269
51.5k
    }
270
271
51.5k
    ba -= 8 * sacExtLen;
272
51.5k
    config->sacExtCnt++;
273
51.5k
  }
274
275
30.5k
bail:
276
30.5k
  return err;
277
30.5k
}
278
279
SACDEC_ERROR SpatialDecParseSpecificConfigHeader(
280
    HANDLE_FDK_BITSTREAM bitstream,
281
    SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig,
282
6.20k
    AUDIO_OBJECT_TYPE coreCodec, SPATIAL_DEC_UPMIX_TYPE upmixType) {
283
6.20k
  SACDEC_ERROR err = MPS_OK;
284
6.20k
  INT numFillBits;
285
6.20k
  int sacHeaderLen = 0;
286
6.20k
  int sacTimeAlignFlag = 0;
287
288
6.20k
  sacTimeAlignFlag = FDKreadBits(bitstream, 1);
289
6.20k
  sacHeaderLen = FDKreadBits(bitstream, 7);
290
291
6.20k
  if (sacHeaderLen == 127) {
292
275
    sacHeaderLen += FDKreadBits(bitstream, 16);
293
275
  }
294
6.20k
  numFillBits = (INT)FDKgetValidBits(bitstream);
295
296
6.20k
  err = SpatialDecParseSpecificConfig(bitstream, pSpatialSpecificConfig,
297
6.20k
                                      sacHeaderLen, coreCodec);
298
299
6.20k
  numFillBits -=
300
6.20k
      (INT)FDKgetValidBits(bitstream); /* the number of read bits (tmpBits) */
301
6.20k
  numFillBits = (8 * sacHeaderLen) - numFillBits;
302
6.20k
  if (numFillBits < 0) {
303
    /* Parsing went wrong */
304
70
    err = MPS_PARSE_ERROR;
305
70
  }
306
  /* Move to the very end of the SSC */
307
6.20k
  FDKpushBiDirectional(bitstream, numFillBits);
308
309
6.20k
  if ((err == MPS_OK) && sacTimeAlignFlag) {
310
    /* not supported */
311
153
    FDKreadBits(bitstream, 16);
312
153
    err = MPS_UNSUPPORTED_CONFIG;
313
153
  }
314
315
  /* Derive additional helper variables */
316
6.20k
  SpatialDecDecodeHelperInfo(pSpatialSpecificConfig, (UPMIXTYPE)upmixType);
317
318
6.20k
  return err;
319
6.20k
}
320
321
SACDEC_ERROR SpatialDecParseMps212Config(
322
    HANDLE_FDK_BITSTREAM bitstream,
323
    SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig, int samplingRate,
324
    AUDIO_OBJECT_TYPE coreCodec, INT stereoConfigIndex,
325
76.2k
    INT coreSbrFrameLengthIndex) {
326
76.2k
  int i;
327
328
76.2k
  FDKmemclear(pSpatialSpecificConfig, sizeof(SPATIAL_SPECIFIC_CONFIG));
329
330
76.2k
  pSpatialSpecificConfig->stereoConfigIndex = stereoConfigIndex;
331
76.2k
  pSpatialSpecificConfig->coreSbrFrameLengthIndex = coreSbrFrameLengthIndex;
332
76.2k
  pSpatialSpecificConfig->freqRes =
333
76.2k
      (SPATIALDEC_FREQ_RES)freqResTable[FDKreadBits(bitstream, 3)];
334
76.2k
  if (pSpatialSpecificConfig->freqRes == 0) {
335
487
    return MPS_PARSE_ERROR; /* reserved value */
336
487
  }
337
338
75.8k
  switch (coreCodec) {
339
0
    case AOT_DRM_USAC:
340
0
      pSpatialSpecificConfig->bsFixedGainDMX =
341
0
          (SPATIALDEC_FIXED_GAINS)FDKreadBits(bitstream, 3);
342
      /* tempShapeConfig = (bsTempShapeConfigDrm == 1) ? 3 : 0 */
343
0
      pSpatialSpecificConfig->tempShapeConfig =
344
0
          (SPATIALDEC_TS_CONF)(FDKreadBits(bitstream, 1) * 3);
345
0
      pSpatialSpecificConfig->decorrConfig = (SPATIALDEC_DECORR_CONF)0;
346
0
      pSpatialSpecificConfig->bsDecorrType = 0;
347
0
      break;
348
75.8k
    case AOT_USAC:
349
75.8k
      pSpatialSpecificConfig->bsFixedGainDMX =
350
75.8k
          (SPATIALDEC_FIXED_GAINS)FDKreadBits(bitstream, 3);
351
75.8k
      pSpatialSpecificConfig->tempShapeConfig =
352
75.8k
          (SPATIALDEC_TS_CONF)FDKreadBits(bitstream, 2);
353
75.8k
      pSpatialSpecificConfig->decorrConfig =
354
75.8k
          (SPATIALDEC_DECORR_CONF)FDKreadBits(bitstream, 2);
355
75.8k
      if (pSpatialSpecificConfig->decorrConfig > 2) {
356
694
        return MPS_PARSE_ERROR; /* reserved value */
357
694
      }
358
75.1k
      pSpatialSpecificConfig->bsDecorrType = 0;
359
75.1k
      break;
360
0
    default:
361
0
      return MPS_UNSUPPORTED_FORMAT;
362
75.8k
  }
363
75.1k
  pSpatialSpecificConfig->nTimeSlots = (coreSbrFrameLengthIndex == 4) ? 64 : 32;
364
75.1k
  pSpatialSpecificConfig->bsHighRateMode = (UCHAR)FDKreadBits(bitstream, 1);
365
366
75.1k
  {
367
75.1k
    pSpatialSpecificConfig->bsPhaseCoding = (UCHAR)FDKreadBits(bitstream, 1);
368
75.1k
    pSpatialSpecificConfig->bsOttBandsPhasePresent =
369
75.1k
        (UCHAR)FDKreadBits(bitstream, 1);
370
75.1k
    if (pSpatialSpecificConfig->bsOttBandsPhasePresent) {
371
19.2k
      if (MAX_PARAMETER_BANDS < (pSpatialSpecificConfig->bsOttBandsPhase =
372
19.2k
                                     FDKreadBits(bitstream, 5))) {
373
213
        return MPS_PARSE_ERROR;
374
213
      }
375
55.8k
    } else {
376
55.8k
      pSpatialSpecificConfig->bsOttBandsPhase = 0;
377
55.8k
    }
378
75.1k
  }
379
380
74.8k
  if (stereoConfigIndex > 1) { /* do residual coding */
381
34.5k
    pSpatialSpecificConfig->bResidualCoding = 1;
382
34.5k
    pSpatialSpecificConfig->ResidualConfig->bResidualPresent = 1;
383
34.5k
    if (pSpatialSpecificConfig->freqRes <
384
34.5k
        (pSpatialSpecificConfig->ResidualConfig->nResidualBands =
385
34.5k
             FDKreadBits(bitstream, 5))) {
386
574
      return MPS_PARSE_ERROR;
387
574
    }
388
34.0k
    pSpatialSpecificConfig->bsOttBandsPhase =
389
34.0k
        fMax(pSpatialSpecificConfig->bsOttBandsPhase,
390
34.0k
             pSpatialSpecificConfig->ResidualConfig->nResidualBands);
391
34.0k
    pSpatialSpecificConfig->bsPseudoLr = (UCHAR)FDKreadBits(bitstream, 1);
392
393
34.0k
    if (pSpatialSpecificConfig->bsPhaseCoding) {
394
14.0k
      pSpatialSpecificConfig->bsPhaseCoding = 3;
395
14.0k
    }
396
40.3k
  } else {
397
40.3k
    pSpatialSpecificConfig->bResidualCoding = 0;
398
40.3k
    pSpatialSpecificConfig->ResidualConfig->bResidualPresent = 0;
399
40.3k
  }
400
401
74.3k
  if (pSpatialSpecificConfig->tempShapeConfig == 2) {
402
41.9k
    switch (coreCodec) {
403
41.9k
      case AOT_USAC:
404
41.9k
        pSpatialSpecificConfig->envQuantMode = FDKreadBits(bitstream, 1);
405
41.9k
        break;
406
0
      default: /* added to avoid compiler warning */
407
0
        break; /* added to avoid compiler warning */
408
41.9k
    }
409
41.9k
  }
410
411
  /* Static parameters */
412
413
74.3k
  pSpatialSpecificConfig->samplingFreq =
414
74.3k
      samplingRate; /* wrong for stereoConfigIndex == 3 but value is unused */
415
74.3k
  pSpatialSpecificConfig->treeConfig = SPATIALDEC_MODE_RSVD7;
416
74.3k
  pSpatialSpecificConfig->nOttBoxes =
417
74.3k
      treePropertyTable[pSpatialSpecificConfig->treeConfig].numOttBoxes;
418
74.3k
  pSpatialSpecificConfig->nInputChannels =
419
74.3k
      treePropertyTable[pSpatialSpecificConfig->treeConfig].numInputChannels;
420
74.3k
  pSpatialSpecificConfig->nOutputChannels =
421
74.3k
      treePropertyTable[pSpatialSpecificConfig->treeConfig].numOutputChannels;
422
423
74.3k
  pSpatialSpecificConfig->bArbitraryDownmix = 0;
424
425
148k
  for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
426
74.3k
    pSpatialSpecificConfig->OttConfig[i].nOttBands = 0;
427
74.3k
  }
428
429
74.3k
  if (coreCodec == AOT_DRM_USAC) {
430
    /* MPS payload is MPEG conform -> no need for pseudo DRM AOT */
431
0
    coreCodec = AOT_USAC;
432
0
  }
433
74.3k
  pSpatialSpecificConfig->coreCodec = coreCodec;
434
435
  /* Derive additional helper variables */
436
74.3k
  SpatialDecDecodeHelperInfo(pSpatialSpecificConfig, UPMIXTYPE_NORMAL);
437
438
74.3k
  return MPS_OK;
439
74.3k
}
440
441
SACDEC_ERROR SpatialDecParseSpecificConfig(
442
    HANDLE_FDK_BITSTREAM bitstream,
443
    SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig, int sacHeaderLen,
444
53.8k
    AUDIO_OBJECT_TYPE coreCodec) {
445
53.8k
  SACDEC_ERROR err = MPS_OK;
446
53.8k
  int i;
447
53.8k
  int bsSamplingFreqIndex;
448
53.8k
  int bsFreqRes, b3DaudioMode = 0;
449
53.8k
  int numHeaderBits;
450
53.8k
  int cfgStartPos, bitsAvailable;
451
452
53.8k
  FDKmemclear(pSpatialSpecificConfig, sizeof(SPATIAL_SPECIFIC_CONFIG));
453
454
53.8k
  cfgStartPos = FDKgetValidBits(bitstream);
455
  /* It might be that we do not know the SSC length beforehand. */
456
53.8k
  if (sacHeaderLen == 0) {
457
2.58k
    bitsAvailable = cfgStartPos;
458
51.2k
  } else {
459
51.2k
    bitsAvailable = 8 * sacHeaderLen;
460
51.2k
    if (bitsAvailable > cfgStartPos) {
461
555
      err = MPS_PARSE_ERROR;
462
555
      goto bail;
463
555
    }
464
51.2k
  }
465
466
53.2k
  bsSamplingFreqIndex = FDKreadBits(bitstream, 4);
467
468
53.2k
  if (bsSamplingFreqIndex == 15) {
469
5.69k
    pSpatialSpecificConfig->samplingFreq = FDKreadBits(bitstream, 24);
470
47.6k
  } else {
471
47.6k
    pSpatialSpecificConfig->samplingFreq =
472
47.6k
        samplingFreqTable[bsSamplingFreqIndex];
473
47.6k
    if (pSpatialSpecificConfig->samplingFreq == 0) {
474
586
      err = MPS_PARSE_ERROR;
475
586
      goto bail;
476
586
    }
477
47.6k
  }
478
479
52.7k
  pSpatialSpecificConfig->nTimeSlots = FDKreadBits(bitstream, 5) + 1;
480
52.7k
  if ((pSpatialSpecificConfig->nTimeSlots < 1) ||
481
52.7k
      (pSpatialSpecificConfig->nTimeSlots > MAX_TIME_SLOTS)) {
482
0
    err = MPS_PARSE_ERROR;
483
0
    goto bail;
484
0
  }
485
486
52.7k
  bsFreqRes = FDKreadBits(bitstream, 3);
487
488
52.7k
  pSpatialSpecificConfig->freqRes =
489
52.7k
      (SPATIALDEC_FREQ_RES)freqResTable_LD[bsFreqRes];
490
491
52.7k
  {
492
52.7k
    UINT treeConfig = FDKreadBits(bitstream, 4);
493
494
52.7k
    switch (treeConfig) {
495
31.8k
      case SPATIALDEC_MODE_RSVD7:
496
31.8k
        pSpatialSpecificConfig->treeConfig = (SPATIALDEC_TREE_CONFIG)treeConfig;
497
31.8k
        break;
498
20.8k
      default:
499
20.8k
        err = MPS_UNSUPPORTED_CONFIG;
500
20.8k
        goto bail;
501
52.7k
    }
502
52.7k
  }
503
504
31.8k
  {
505
31.8k
    pSpatialSpecificConfig->nOttBoxes =
506
31.8k
        treePropertyTable[pSpatialSpecificConfig->treeConfig].numOttBoxes;
507
31.8k
    pSpatialSpecificConfig->nTttBoxes =
508
31.8k
        treePropertyTable[pSpatialSpecificConfig->treeConfig].numTttBoxes;
509
31.8k
    pSpatialSpecificConfig->nInputChannels =
510
31.8k
        treePropertyTable[pSpatialSpecificConfig->treeConfig].numInputChannels;
511
31.8k
    pSpatialSpecificConfig->nOutputChannels =
512
31.8k
        treePropertyTable[pSpatialSpecificConfig->treeConfig].numOutputChannels;
513
31.8k
  }
514
515
31.8k
  pSpatialSpecificConfig->quantMode =
516
31.8k
      (SPATIALDEC_QUANT_MODE)FDKreadBits(bitstream, 2);
517
518
31.8k
  pSpatialSpecificConfig->bArbitraryDownmix = FDKreadBits(bitstream, 1);
519
520
31.8k
  pSpatialSpecificConfig->bsFixedGainDMX =
521
31.8k
      (SPATIALDEC_FIXED_GAINS)FDKreadBits(bitstream, 3);
522
523
31.8k
  pSpatialSpecificConfig->tempShapeConfig =
524
31.8k
      (SPATIALDEC_TS_CONF)FDKreadBits(bitstream, 2);
525
31.8k
  if (pSpatialSpecificConfig->tempShapeConfig > 2) {
526
705
    return MPS_PARSE_ERROR; /* reserved value */
527
705
  }
528
529
31.1k
  pSpatialSpecificConfig->decorrConfig =
530
31.1k
      (SPATIALDEC_DECORR_CONF)FDKreadBits(bitstream, 2);
531
31.1k
  if (pSpatialSpecificConfig->decorrConfig > 2) {
532
289
    return MPS_PARSE_ERROR; /* reserved value */
533
289
  }
534
535
61.7k
  for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
536
30.8k
    pSpatialSpecificConfig->OttConfig[i].nOttBands = 0;
537
30.8k
  }
538
539
30.8k
  for (i = 0; i < pSpatialSpecificConfig->nTttBoxes; i++) {
540
0
    int bTttDualMode = FDKreadBits(bitstream, 1);
541
0
    FDKreadBits(bitstream, 3); /* not supported */
542
543
0
    if (bTttDualMode) {
544
0
      FDKreadBits(bitstream, 8); /* not supported */
545
0
    }
546
0
  }
547
548
30.8k
  if (pSpatialSpecificConfig->tempShapeConfig == 2) {
549
10.0k
    pSpatialSpecificConfig->envQuantMode = FDKreadBits(bitstream, 1);
550
10.0k
  }
551
552
30.8k
  if (b3DaudioMode) {
553
0
    if (FDKreadBits(bitstream, 2) == 0) { /* b3DaudioHRTFset ? */
554
0
      int hc;
555
0
      int HRTFnumBand;
556
0
      int HRTFfreqRes = FDKreadBits(bitstream, 3);
557
0
      int HRTFnumChan = FDKreadBits(bitstream, 4);
558
0
      int HRTFasymmetric = FDKreadBits(bitstream, 1);
559
560
0
      HRTFnumBand = freqResTable_LD[HRTFfreqRes];
561
562
0
      for (hc = 0; hc < HRTFnumChan; hc++) {
563
0
        FDKpushFor(bitstream, HRTFnumBand * 6); /* HRTFlevelLeft[hc][hb] */
564
0
        if (HRTFasymmetric) {
565
0
          FDKpushFor(bitstream, HRTFnumBand * 6); /* HRTFlevelRight[hc][hb] */
566
0
        }
567
0
        if (FDKreadBits(bitstream, 1)) {          /* HRTFphase[hc] ? */
568
0
          FDKpushFor(bitstream, HRTFnumBand * 6); /* HRTFphaseLR[hc][hb] */
569
0
        }
570
0
        if (FDKreadBits(bitstream, 1)) {          /* HRTFicc[hc] ? */
571
0
          FDKpushFor(bitstream, HRTFnumBand * 6); /* HRTFiccLR[hc][hb] */
572
0
        }
573
0
      }
574
0
    }
575
0
  }
576
577
30.8k
  FDKbyteAlign(bitstream,
578
30.8k
               cfgStartPos); /* ISO/IEC FDIS 23003-1: 5.2. ... byte alignment
579
                                with respect to the beginning of the syntactic
580
                                element in which ByteAlign() occurs. */
581
582
30.8k
  numHeaderBits = cfgStartPos - (INT)FDKgetValidBits(bitstream);
583
30.8k
  bitsAvailable -= numHeaderBits;
584
30.8k
  if (bitsAvailable < 0) {
585
308
    err = MPS_PARSE_ERROR;
586
308
    goto bail;
587
308
  }
588
589
30.5k
  pSpatialSpecificConfig->sacExtCnt = 0;
590
30.5k
  pSpatialSpecificConfig->bResidualCoding = 0;
591
592
30.5k
  err = SpatialDecParseExtensionConfig(
593
30.5k
      bitstream, pSpatialSpecificConfig, pSpatialSpecificConfig->nOttBoxes,
594
30.5k
      pSpatialSpecificConfig->nTttBoxes,
595
30.5k
      pSpatialSpecificConfig->nOutputChannels, bitsAvailable);
596
597
30.5k
  FDKbyteAlign(
598
30.5k
      bitstream,
599
30.5k
      cfgStartPos); /* Same alignment anchor as above because
600
                       SpatialExtensionConfig() always reads full bytes */
601
602
30.5k
  pSpatialSpecificConfig->coreCodec = coreCodec;
603
604
30.5k
  SpatialDecDecodeHelperInfo(pSpatialSpecificConfig, UPMIXTYPE_NORMAL);
605
606
52.8k
bail:
607
52.8k
  if (sacHeaderLen > 0) {
608
    /* If the config is of known length then assure that the
609
       bitbuffer is exactly at its end when leaving the function. */
610
50.6k
    FDKpushBiDirectional(
611
50.6k
        bitstream,
612
50.6k
        (sacHeaderLen * 8) - (cfgStartPos - (INT)FDKgetValidBits(bitstream)));
613
50.6k
  }
614
615
52.8k
  return err;
616
30.5k
}
617
618
int SpatialDecDefaultSpecificConfig(
619
    SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig,
620
    AUDIO_OBJECT_TYPE coreCodec, int samplingFreq, int nTimeSlots,
621
    int sacDecoderLevel, int isBlind, int numCoreChannels)
622
623
766
{
624
766
  int err = MPS_OK;
625
766
  int i;
626
627
766
  FDK_ASSERT(coreCodec != AOT_NONE);
628
766
  FDK_ASSERT(nTimeSlots > 0);
629
766
  FDK_ASSERT(samplingFreq > 0);
630
631
766
  pSpatialSpecificConfig->coreCodec = coreCodec;
632
766
  pSpatialSpecificConfig->samplingFreq = samplingFreq;
633
766
  pSpatialSpecificConfig->nTimeSlots = nTimeSlots;
634
766
  if ((pSpatialSpecificConfig->coreCodec == AOT_ER_AAC_ELD) ||
635
0
      (pSpatialSpecificConfig->coreCodec == AOT_ER_AAC_LD))
636
766
    pSpatialSpecificConfig->freqRes = SPATIALDEC_FREQ_RES_23;
637
0
  else
638
0
    pSpatialSpecificConfig->freqRes = SPATIALDEC_FREQ_RES_28;
639
640
766
  {
641
766
    pSpatialSpecificConfig->treeConfig =
642
766
        SPATIALDEC_MODE_RSVD7; /* 212  configuration */
643
766
  }
644
645
766
  {
646
766
    pSpatialSpecificConfig->nOttBoxes =
647
766
        treePropertyTable[pSpatialSpecificConfig->treeConfig].numOttBoxes;
648
766
    pSpatialSpecificConfig->nInputChannels =
649
766
        treePropertyTable[pSpatialSpecificConfig->treeConfig].numInputChannels;
650
766
    pSpatialSpecificConfig->nOutputChannels =
651
766
        treePropertyTable[pSpatialSpecificConfig->treeConfig].numOutputChannels;
652
766
  }
653
654
766
  pSpatialSpecificConfig->quantMode = SPATIALDEC_QUANT_FINE_DEF;
655
766
  pSpatialSpecificConfig->bArbitraryDownmix = 0;
656
766
  pSpatialSpecificConfig->bResidualCoding = 0;
657
766
  if ((pSpatialSpecificConfig->coreCodec == AOT_ER_AAC_ELD) ||
658
0
      (pSpatialSpecificConfig->coreCodec == AOT_ER_AAC_LD))
659
766
    pSpatialSpecificConfig->bsFixedGainDMX = SPATIALDEC_GAIN_RSVD2;
660
0
  else
661
0
    pSpatialSpecificConfig->bsFixedGainDMX = SPATIALDEC_GAIN_MODE0;
662
663
766
  pSpatialSpecificConfig->tempShapeConfig = SPATIALDEC_TS_TPNOWHITE;
664
766
  pSpatialSpecificConfig->decorrConfig = SPATIALDEC_DECORR_MODE0;
665
666
1.53k
  for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
667
766
    pSpatialSpecificConfig->OttConfig[i].nOttBands = 0;
668
766
  }
669
670
766
  return err;
671
766
}
672
673
/*******************************************************************************
674
 Functionname: coarse2fine
675
 *******************************************************************************
676
677
 Description:
678
   Parameter index mapping from coarse to fine quantization
679
680
 Arguments:
681
682
Input:
683
684
Output:
685
686
*******************************************************************************/
687
static void coarse2fine(SCHAR *data, DATA_TYPE dataType, int startBand,
688
76.9k
                        int numBands) {
689
76.9k
  int i;
690
691
778k
  for (i = startBand; i < startBand + numBands; i++) {
692
701k
    data[i] <<= 1;
693
701k
  }
694
695
76.9k
  if (dataType == t_CLD) {
696
360k
    for (i = startBand; i < startBand + numBands; i++) {
697
327k
      if (data[i] == -14)
698
115k
        data[i] = -15;
699
212k
      else if (data[i] == 14)
700
12.9k
        data[i] = 15;
701
327k
    }
702
33.2k
  }
703
76.9k
}
704
705
/*******************************************************************************
706
 Functionname: fine2coarse
707
 *******************************************************************************
708
709
 Description:
710
   Parameter index mapping from fine to coarse quantization
711
712
 Arguments:
713
714
Input:
715
716
Output:
717
718
*******************************************************************************/
719
static void fine2coarse(SCHAR *data, DATA_TYPE dataType, int startBand,
720
17.6k
                        int numBands) {
721
17.6k
  int i;
722
723
184k
  for (i = startBand; i < startBand + numBands; i++) {
724
    /* Note: the if cases below actually make a difference (negative values) */
725
166k
    if (dataType == t_CLD)
726
77.7k
      data[i] /= 2;
727
89.0k
    else
728
89.0k
      data[i] >>= 1;
729
166k
  }
730
17.6k
}
731
732
/*******************************************************************************
733
 Functionname: getStrideMap
734
 *******************************************************************************
735
736
 Description:
737
   Index Mapping accroding to pbStrides
738
739
 Arguments:
740
741
Input:
742
743
Output:
744
745
*******************************************************************************/
746
static int getStrideMap(int freqResStride, int startBand, int stopBand,
747
53.5k
                        int *aStrides) {
748
53.5k
  int i, pb, pbStride, dataBands, strOffset;
749
750
53.5k
  pbStride = pbStrideTable[freqResStride];
751
53.5k
  dataBands = (stopBand - startBand - 1) / pbStride + 1;
752
753
53.5k
  aStrides[0] = startBand;
754
260k
  for (pb = 1; pb <= dataBands; pb++) {
755
207k
    aStrides[pb] = aStrides[pb - 1] + pbStride;
756
207k
  }
757
53.5k
  strOffset = 0;
758
480k
  while (aStrides[dataBands] > stopBand) {
759
427k
    if (strOffset < dataBands) strOffset++;
760
860k
    for (i = strOffset; i <= dataBands; i++) {
761
432k
      aStrides[i]--;
762
432k
    }
763
427k
  }
764
765
53.5k
  return dataBands;
766
53.5k
}
767
768
/*******************************************************************************
769
 Functionname: ecDataDec
770
 *******************************************************************************
771
772
 Description:
773
   Do delta decoding and dequantization
774
775
 Arguments:
776
777
Input:
778
779
Output:
780
781
782
*******************************************************************************/
783
784
static SACDEC_ERROR ecDataDec(
785
    const SPATIAL_BS_FRAME *frame, UINT syntaxFlags,
786
    HANDLE_FDK_BITSTREAM bitstream, LOSSLESSDATA *const llData,
787
    SCHAR (*data)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS], SCHAR **lastdata,
788
200k
    int datatype, int boxIdx, int startBand, int stopBand, SCHAR defaultValue) {
789
200k
  SACDEC_ERROR err = MPS_OK;
790
200k
  int i, j, pb, dataSets, setIdx, bsDataPair, dataBands, oldQuantCoarseXXX;
791
200k
  INT aStrides[MAX_PARAMETER_BANDS + 1] = {0};
792
793
200k
  dataSets = 0;
794
523k
  for (i = 0; i < frame->numParameterSets; i++) {
795
325k
    llData->bsXXXDataMode[i] = (SCHAR)FDKreadBits(bitstream, 2);
796
797
325k
    if ((frame->bsIndependencyFlag == 1) && (i == 0) &&
798
164k
        (llData->bsXXXDataMode[i] == 1 ||
799
164k
         llData->bsXXXDataMode[i] == 2)) { /* This check catches bitstreams
800
                                              generated by older encoder that
801
                                              cause trouble */
802
145
      return MPS_PARSE_ERROR;
803
145
    }
804
325k
    if ((i >= frame->numParameterSets - 1) &&
805
200k
        (llData->bsXXXDataMode[i] ==
806
200k
         2)) { /* The interpolation mode must not be active for the last
807
                  parameter set */
808
2.40k
      return MPS_PARSE_ERROR;
809
2.40k
    }
810
811
323k
    if (llData->bsXXXDataMode[i] == 3) {
812
78.4k
      dataSets++;
813
78.4k
    }
814
323k
  }
815
816
198k
  setIdx = 0;
817
198k
  bsDataPair = 0;
818
198k
  oldQuantCoarseXXX = llData->state->bsQuantCoarseXXXprevParse;
819
820
517k
  for (i = 0; i < frame->numParameterSets; i++) {
821
321k
    if (llData->bsXXXDataMode[i] == 0) {
822
2.10M
      for (pb = startBand; pb < stopBand; pb++) {
823
1.90M
        lastdata[boxIdx][pb] = defaultValue;
824
1.90M
      }
825
826
201k
      oldQuantCoarseXXX = 0;
827
201k
    }
828
829
321k
    if (llData->bsXXXDataMode[i] == 3) {
830
78.0k
      if (bsDataPair) {
831
24.5k
        bsDataPair = 0;
832
53.5k
      } else {
833
53.5k
        bsDataPair = FDKreadBits(bitstream, 1);
834
53.5k
        llData->bsQuantCoarseXXX[setIdx] = (UCHAR)FDKreadBits(bitstream, 1);
835
53.5k
        llData->bsFreqResStrideXXX[setIdx] = (UCHAR)FDKreadBits(bitstream, 2);
836
837
53.5k
        if (llData->bsQuantCoarseXXX[setIdx] != oldQuantCoarseXXX) {
838
24.2k
          if (oldQuantCoarseXXX) {
839
6.61k
            coarse2fine(lastdata[boxIdx], (DATA_TYPE)datatype, startBand,
840
6.61k
                        stopBand - startBand);
841
17.6k
          } else {
842
17.6k
            fine2coarse(lastdata[boxIdx], (DATA_TYPE)datatype, startBand,
843
17.6k
                        stopBand - startBand);
844
17.6k
          }
845
24.2k
        }
846
847
53.5k
        dataBands = getStrideMap(llData->bsFreqResStrideXXX[setIdx], startBand,
848
53.5k
                                 stopBand, aStrides);
849
850
260k
        for (pb = 0; pb < dataBands; pb++) {
851
207k
          lastdata[boxIdx][startBand + pb] = lastdata[boxIdx][aStrides[pb]];
852
207k
        }
853
854
53.5k
        if (boxIdx > MAX_NUM_OTT) return MPS_INVALID_BOXIDX;
855
53.5k
        if ((setIdx + bsDataPair) > MAX_PARAMETER_SETS)
856
0
          return MPS_INVALID_SETIDX;
857
858
        /* DECODER_TYPE defined in FDK_tools */
859
53.5k
        DECODER_TYPE this_decoder_type = SAC_DECODER;
860
53.5k
        if (syntaxFlags & (SACDEC_SYNTAX_USAC | SACDEC_SYNTAX_RSVD50)) {
861
47.4k
          this_decoder_type = USAC_DECODER;
862
47.4k
        } else if (syntaxFlags & SACDEC_SYNTAX_LD) {
863
6.06k
          this_decoder_type = SAOC_DECODER;
864
6.06k
        }
865
866
53.5k
        err = (SACDEC_ERROR)EcDataPairDec(
867
53.5k
            this_decoder_type, bitstream, data[boxIdx][setIdx + 0],
868
53.5k
            data[boxIdx][setIdx + 1], lastdata[boxIdx], (DATA_TYPE)datatype,
869
53.5k
            startBand, dataBands, bsDataPair, llData->bsQuantCoarseXXX[setIdx],
870
53.5k
            !(frame->bsIndependencyFlag && (i == 0)) || (setIdx > 0));
871
53.5k
        if (err != MPS_OK) goto bail;
872
873
51.7k
        if (datatype == t_IPD) {
874
9.16k
          const SCHAR mask = (llData->bsQuantCoarseXXX[setIdx]) ? 7 : 15;
875
50.2k
          for (pb = 0; pb < dataBands; pb++) {
876
115k
            for (j = aStrides[pb]; j < aStrides[pb + 1]; j++) {
877
73.9k
              lastdata[boxIdx][j] =
878
73.9k
                  data[boxIdx][setIdx + bsDataPair][startBand + pb] & mask;
879
73.9k
            }
880
41.1k
          }
881
42.6k
        } else {
882
201k
          for (pb = 0; pb < dataBands; pb++) {
883
571k
            for (j = aStrides[pb]; j < aStrides[pb + 1]; j++) {
884
412k
              lastdata[boxIdx][j] =
885
412k
                  data[boxIdx][setIdx + bsDataPair][startBand + pb];
886
412k
            }
887
158k
          }
888
42.6k
        }
889
890
51.7k
        oldQuantCoarseXXX = llData->bsQuantCoarseXXX[setIdx];
891
892
51.7k
        if (bsDataPair) {
893
32.3k
          llData->bsQuantCoarseXXX[setIdx + 1] =
894
32.3k
              llData->bsQuantCoarseXXX[setIdx];
895
32.3k
          llData->bsFreqResStrideXXX[setIdx + 1] =
896
32.3k
              llData->bsFreqResStrideXXX[setIdx];
897
32.3k
        }
898
51.7k
        setIdx += bsDataPair + 1;
899
51.7k
      } /* !bsDataPair */
900
78.0k
    }   /* llData->bsXXXDataMode[i] == 3 */
901
321k
  }
902
903
196k
  llData->state->bsQuantCoarseXXXprevParse = oldQuantCoarseXXX;
904
905
198k
bail:
906
198k
  return err;
907
196k
}
908
909
/*******************************************************************************
910
 Functionname: parseArbitraryDownmixData
911
 *******************************************************************************
912
913
 Description:
914
915
 Arguments:
916
917
 Return:
918
919
*******************************************************************************/
920
static SACDEC_ERROR parseArbitraryDownmixData(
921
    spatialDec *self, const SPATIAL_SPECIFIC_CONFIG *pSSC,
922
    const UINT syntaxFlags, const SPATIAL_BS_FRAME *frame,
923
4.25k
    HANDLE_FDK_BITSTREAM bitstream) {
924
4.25k
  SACDEC_ERROR err = MPS_OK;
925
4.25k
  int ch;
926
4.25k
  int offset = pSSC->nOttBoxes;
927
928
  /* CLD (arbitrary down-mix gains) */
929
6.53k
  for (ch = 0; ch < pSSC->nInputChannels; ch++) {
930
4.25k
    err = ecDataDec(frame, syntaxFlags, bitstream,
931
4.25k
                    &frame->CLDLosslessData[offset + ch],
932
4.25k
                    frame->cmpArbdmxGainIdx, self->cmpArbdmxGainIdxPrev, t_CLD,
933
4.25k
                    ch, 0, pSSC->freqRes, arbdmxGainDefault);
934
4.25k
    if (err != MPS_OK) return err;
935
4.25k
  }
936
937
2.27k
  return err;
938
939
4.25k
} /* parseArbitraryDownmixData */
940
941
/*******************************************************************************
942
 Functionname: SpatialDecParseFrame
943
 *******************************************************************************
944
945
 Description:
946
947
 Arguments:
948
949
 Input:
950
951
 Output:
952
953
*******************************************************************************/
954
955
4.67k
static int nBitsParamSlot(int i) {
956
4.67k
  int bitsParamSlot;
957
958
4.67k
  bitsParamSlot = fMax(0, DFRACT_BITS - 1 - fNormz((FIXP_DBL)i));
959
4.67k
  if ((1 << bitsParamSlot) < i) {
960
79
    bitsParamSlot++;
961
79
  }
962
4.67k
  FDK_ASSERT((bitsParamSlot >= 0) && (bitsParamSlot <= 32));
963
964
4.67k
  return bitsParamSlot;
965
4.67k
}
966
967
SACDEC_ERROR SpatialDecParseFrameData(
968
    spatialDec_struct *self, SPATIAL_BS_FRAME *frame,
969
    HANDLE_FDK_BITSTREAM bitstream,
970
    const SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig, UPMIXTYPE upmixType,
971
98.1k
    int fGlobalIndependencyFlag) {
972
98.1k
  SACDEC_ERROR err = MPS_OK;
973
98.1k
  int bsFramingType, dataBands, ps, pg, i;
974
98.1k
  int pb;
975
98.1k
  int numTempShapeChan = 0;
976
98.1k
  int bsNumOutputChannels =
977
98.1k
      treePropertyTable[pSpatialSpecificConfig->treeConfig]
978
98.1k
          .numOutputChannels; /* CAUTION: Maybe different to
979
                                 pSpatialSpecificConfig->treeConfig in some
980
                                 modes! */
981
98.1k
  int paramSetErr = 0;
982
98.1k
  UINT alignAnchor = FDKgetValidBits(
983
98.1k
      bitstream); /* Anchor for ByteAlign() function. See comment below. */
984
98.1k
  UINT syntaxFlags;
985
986
98.1k
  syntaxFlags = pSpatialSpecificConfig->syntaxFlags;
987
988
98.1k
  if ((syntaxFlags & (SACDEC_SYNTAX_USAC | SACDEC_SYNTAX_RSVD50)) &&
989
91.2k
      pSpatialSpecificConfig->bsHighRateMode == 0) {
990
11.2k
    bsFramingType = 0; /* fixed framing */
991
11.2k
    frame->numParameterSets = 1;
992
86.8k
  } else {
993
86.8k
    bsFramingType = FDKreadBits(bitstream, 1);
994
86.8k
    if (syntaxFlags & SACDEC_SYNTAX_LD)
995
6.91k
      frame->numParameterSets = FDKreadBits(bitstream, 1) + 1;
996
79.9k
    else
997
79.9k
      frame->numParameterSets = FDKreadBits(bitstream, 3) + 1;
998
86.8k
  }
999
1000
  /* Any error after this line shall trigger parameter invalidation at bail
1001
   * label. */
1002
98.1k
  paramSetErr = 1;
1003
1004
98.1k
  if (frame->numParameterSets >= MAX_PARAMETER_SETS) {
1005
0
    goto bail;
1006
0
  }
1007
1008
  /* Basic config check. */
1009
98.1k
  if (pSpatialSpecificConfig->nInputChannels <= 0 ||
1010
98.1k
      pSpatialSpecificConfig->nOutputChannels <= 0) {
1011
0
    err = MPS_UNSUPPORTED_CONFIG;
1012
0
    goto bail;
1013
0
  }
1014
1015
98.1k
  if (bsFramingType) {
1016
4.67k
    int prevParamSlot = -1;
1017
4.67k
    int bitsParamSlot;
1018
1019
4.67k
    {
1020
4.67k
      bitsParamSlot = nBitsParamSlot(pSpatialSpecificConfig->nTimeSlots);
1021
1022
10.9k
      for (i = 0; i < frame->numParameterSets; i++) {
1023
6.57k
        frame->paramSlot[i] = FDKreadBits(bitstream, bitsParamSlot);
1024
        /* Sanity check */
1025
6.57k
        if ((frame->paramSlot[i] <= prevParamSlot) ||
1026
6.35k
            (frame->paramSlot[i] >= pSpatialSpecificConfig->nTimeSlots)) {
1027
297
          err = MPS_PARSE_ERROR;
1028
297
          goto bail;
1029
297
        }
1030
6.28k
        prevParamSlot = frame->paramSlot[i];
1031
6.28k
      }
1032
4.67k
    }
1033
93.4k
  } else {
1034
238k
    for (i = 0; i < frame->numParameterSets; i++) {
1035
144k
      frame->paramSlot[i] = ((pSpatialSpecificConfig->nTimeSlots * (i + 1)) /
1036
144k
                             frame->numParameterSets) -
1037
144k
                            1;
1038
144k
    }
1039
93.4k
  }
1040
1041
97.8k
  if ((syntaxFlags & (SACDEC_SYNTAX_USAC | SACDEC_SYNTAX_RSVD50)) &&
1042
91.2k
      fGlobalIndependencyFlag) {
1043
77.3k
    frame->bsIndependencyFlag = 1;
1044
77.3k
  } else {
1045
20.4k
    frame->bsIndependencyFlag = (UCHAR)FDKreadBits(bitstream, 1);
1046
20.4k
  }
1047
1048
  /*
1049
   * OttData()
1050
   */
1051
193k
  for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
1052
97.8k
    err = ecDataDec(frame, syntaxFlags, bitstream, &frame->CLDLosslessData[i],
1053
97.8k
                    frame->cmpOttCLDidx, self->cmpOttCLDidxPrev, t_CLD, i, 0,
1054
97.8k
                    pSpatialSpecificConfig->bitstreamOttBands[i],
1055
97.8k
                    pSpatialSpecificConfig->ottCLDdefault[i]);
1056
97.8k
    if (err != MPS_OK) {
1057
1.77k
      goto bail;
1058
1.77k
    }
1059
97.8k
  } /* i < numOttBoxes */
1060
1061
96.0k
  {
1062
191k
    for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
1063
96.0k
      err = ecDataDec(frame, syntaxFlags, bitstream, &frame->ICCLosslessData[i],
1064
96.0k
                      frame->cmpOttICCidx, self->cmpOttICCidxPrev, t_ICC, i, 0,
1065
96.0k
                      pSpatialSpecificConfig->bitstreamOttBands[i], ICCdefault);
1066
96.0k
      if (err != MPS_OK) {
1067
459
        goto bail;
1068
459
      }
1069
96.0k
    } /* i < numOttBoxes */
1070
96.0k
  }   /* !oneICC */
1071
1072
95.6k
  if ((pSpatialSpecificConfig->treeConfig == SPATIALDEC_MODE_RSVD7) &&
1073
95.6k
      (pSpatialSpecificConfig->bsPhaseCoding)) {
1074
9.04k
    frame->phaseMode = FDKreadBits(bitstream, 1);
1075
1076
9.04k
    if (frame->phaseMode == 0) {
1077
40.5k
      for (pb = 0; pb < pSpatialSpecificConfig->numOttBandsIPD; pb++) {
1078
34.2k
        self->cmpOttIPDidxPrev[0][pb] = 0;
1079
214k
        for (i = 0; i < frame->numParameterSets; i++) {
1080
180k
          frame->cmpOttIPDidx[0][i][pb] = 0;
1081
          // frame->ottIPDidx[0][i][pb] = 0;
1082
180k
        }
1083
        /* self->ottIPDidxPrev[0][pb] = 0; */
1084
34.2k
      }
1085
6.28k
      frame->OpdSmoothingMode = 0;
1086
6.28k
    } else {
1087
2.75k
      frame->OpdSmoothingMode = FDKreadBits(bitstream, 1);
1088
2.75k
      err = ecDataDec(frame, syntaxFlags, bitstream, &frame->IPDLosslessData[0],
1089
2.75k
                      frame->cmpOttIPDidx, self->cmpOttIPDidxPrev, t_IPD, 0, 0,
1090
2.75k
                      pSpatialSpecificConfig->numOttBandsIPD, IPDdefault);
1091
2.75k
      if (err != MPS_OK) {
1092
71
        goto bail;
1093
71
      }
1094
2.75k
    }
1095
9.04k
  }
1096
1097
  /*
1098
   * SmgData()
1099
   */
1100
1101
95.5k
  {
1102
95.5k
    if (!pSpatialSpecificConfig->bsHighRateMode &&
1103
15.7k
        (syntaxFlags & SACDEC_SYNTAX_USAC)) {
1104
22.4k
      for (ps = 0; ps < frame->numParameterSets; ps++) {
1105
11.2k
        frame->bsSmoothMode[ps] = 0;
1106
11.2k
      }
1107
84.2k
    } else {
1108
220k
      for (ps = 0; ps < frame->numParameterSets; ps++) {
1109
136k
        frame->bsSmoothMode[ps] = (UCHAR)FDKreadBits(bitstream, 2);
1110
136k
        if (frame->bsSmoothMode[ps] >= 2) {
1111
17.8k
          frame->bsSmoothTime[ps] = (UCHAR)FDKreadBits(bitstream, 2);
1112
17.8k
        }
1113
136k
        if (frame->bsSmoothMode[ps] == 3) {
1114
11.4k
          frame->bsFreqResStrideSmg[ps] = (UCHAR)FDKreadBits(bitstream, 2);
1115
11.4k
          dataBands = (pSpatialSpecificConfig->freqRes - 1) /
1116
11.4k
                          pbStrideTable[frame->bsFreqResStrideSmg[ps]] +
1117
11.4k
                      1;
1118
50.6k
          for (pg = 0; pg < dataBands; pg++) {
1119
39.1k
            frame->bsSmgData[ps][pg] = (UCHAR)FDKreadBits(bitstream, 1);
1120
39.1k
          }
1121
11.4k
        }
1122
136k
      } /* ps < numParameterSets */
1123
84.2k
    }
1124
95.5k
  }
1125
1126
  /*
1127
   * TempShapeData()
1128
   */
1129
95.5k
  if ((pSpatialSpecificConfig->tempShapeConfig == 3) &&
1130
7.35k
      (syntaxFlags & SACDEC_SYNTAX_USAC)) {
1131
7.35k
    int TsdErr;
1132
7.35k
    TsdErr = TsdRead(bitstream, pSpatialSpecificConfig->nTimeSlots,
1133
7.35k
                     &frame->TsdData[0]);
1134
7.35k
    if (TsdErr) {
1135
0
      err = MPS_PARSE_ERROR;
1136
0
      goto bail;
1137
0
    }
1138
88.1k
  } else {
1139
88.1k
    frame->TsdData[0].bsTsdEnable = 0;
1140
88.1k
  }
1141
1142
286k
  for (i = 0; i < bsNumOutputChannels; i++) {
1143
191k
    frame->tempShapeEnableChannelSTP[i] = 0;
1144
191k
    frame->tempShapeEnableChannelGES[i] = 0;
1145
191k
  }
1146
1147
95.5k
  if ((pSpatialSpecificConfig->tempShapeConfig == 1) ||
1148
89.8k
      (pSpatialSpecificConfig->tempShapeConfig == 2)) {
1149
82.7k
    int bsTempShapeEnable = FDKreadBits(bitstream, 1);
1150
82.7k
    if (bsTempShapeEnable) {
1151
4.72k
      numTempShapeChan =
1152
4.72k
          tempShapeChanTable[pSpatialSpecificConfig->tempShapeConfig - 1]
1153
4.72k
                            [pSpatialSpecificConfig->treeConfig];
1154
4.72k
      switch (pSpatialSpecificConfig->tempShapeConfig) {
1155
2.14k
        case 1: /* STP */
1156
6.44k
          for (i = 0; i < numTempShapeChan; i++) {
1157
4.29k
            int stpEnable = FDKreadBits(bitstream, 1);
1158
4.29k
            frame->tempShapeEnableChannelSTP[i] = stpEnable;
1159
4.29k
          }
1160
2.14k
          break;
1161
2.58k
        case 2: /* GES */
1162
2.58k
        {
1163
2.58k
          UCHAR gesChannelEnable[MAX_OUTPUT_CHANNELS];
1164
1165
7.74k
          for (i = 0; i < numTempShapeChan; i++) {
1166
5.16k
            gesChannelEnable[i] = (UCHAR)FDKreadBits(bitstream, 1);
1167
5.16k
            frame->tempShapeEnableChannelGES[i] = gesChannelEnable[i];
1168
5.16k
          }
1169
7.57k
          for (i = 0; i < numTempShapeChan; i++) {
1170
5.11k
            if (gesChannelEnable[i]) {
1171
2.62k
              int envShapeData_tmp[MAX_TIME_SLOTS];
1172
2.62k
              if (huff_dec_reshape(bitstream, envShapeData_tmp,
1173
2.62k
                                   pSpatialSpecificConfig->nTimeSlots) != 0) {
1174
121
                err = MPS_PARSE_ERROR;
1175
121
                goto bail;
1176
121
              }
1177
80.2k
              for (int ts = 0; ts < pSpatialSpecificConfig->nTimeSlots; ts++) {
1178
77.7k
                if (!(envShapeData_tmp[ts] >= 0) &&
1179
0
                    (envShapeData_tmp[ts] <= 4)) {
1180
0
                  err = MPS_PARSE_ERROR;
1181
0
                  goto bail;
1182
0
                }
1183
77.7k
                frame->bsEnvShapeData[i][ts] = (UCHAR)envShapeData_tmp[ts];
1184
77.7k
              }
1185
2.50k
            }
1186
5.11k
          }
1187
2.58k
        } break;
1188
2.46k
        default:
1189
0
          err = MPS_INVALID_TEMPSHAPE;
1190
0
          goto bail;
1191
4.72k
      }
1192
4.72k
    } /* bsTempShapeEnable */
1193
82.7k
  }   /* pSpatialSpecificConfig->tempShapeConfig != 0 */
1194
1195
95.4k
  if (pSpatialSpecificConfig->bArbitraryDownmix != 0) {
1196
4.25k
    err = parseArbitraryDownmixData(self, pSpatialSpecificConfig, syntaxFlags,
1197
4.25k
                                    frame, bitstream);
1198
4.25k
    if (err != MPS_OK) goto bail;
1199
4.25k
  }
1200
1201
93.4k
  if (1 && (!(syntaxFlags & (SACDEC_SYNTAX_USAC)))) {
1202
2.40k
    FDKbyteAlign(bitstream,
1203
2.40k
                 alignAnchor); /* ISO/IEC FDIS 23003-1: 5.2. ... byte alignment
1204
                                  with respect to the beginning of the syntactic
1205
                                  element in which ByteAlign() occurs. */
1206
2.40k
  }
1207
1208
98.1k
bail:
1209
98.1k
  if (err != MPS_OK && paramSetErr != 0) {
1210
    /* Since the parameter set data has already been written to the instance we
1211
     * need to ... */
1212
4.70k
    frame->numParameterSets = 0; /* ... signal that it is corrupt ... */
1213
4.70k
  }
1214
1215
98.1k
  return err;
1216
1217
93.4k
} /* SpatialDecParseFrame */
1218
1219
/*******************************************************************************
1220
 Functionname: createMapping
1221
 *******************************************************************************
1222
1223
 Description:
1224
1225
 Arguments:
1226
1227
 Return:
1228
1229
*******************************************************************************/
1230
static void createMapping(int aMap[MAX_PARAMETER_BANDS + 1], int startBand,
1231
85.9k
                          int stopBand, int stride) {
1232
85.9k
  int inBands, outBands, bandsAchived, bandsDiff, incr, k, i;
1233
85.9k
  int vDk[MAX_PARAMETER_BANDS + 1];
1234
85.9k
  inBands = stopBand - startBand;
1235
85.9k
  outBands = (inBands - 1) / stride + 1;
1236
1237
85.9k
  if (outBands < 1) {
1238
1.92k
    outBands = 1;
1239
1.92k
  }
1240
1241
85.9k
  bandsAchived = outBands * stride;
1242
85.9k
  bandsDiff = inBands - bandsAchived;
1243
345k
  for (i = 0; i < outBands; i++) {
1244
259k
    vDk[i] = stride;
1245
259k
  }
1246
1247
85.9k
  if (bandsDiff > 0) {
1248
0
    incr = -1;
1249
0
    k = outBands - 1;
1250
85.9k
  } else {
1251
85.9k
    incr = 1;
1252
85.9k
    k = 0;
1253
85.9k
  }
1254
1255
890k
  while (bandsDiff != 0) {
1256
804k
    vDk[k] = vDk[k] - incr;
1257
804k
    k = k + incr;
1258
804k
    bandsDiff = bandsDiff + incr;
1259
804k
    if (k >= outBands) {
1260
801k
      if (bandsDiff > 0) {
1261
0
        k = outBands - 1;
1262
801k
      } else if (bandsDiff < 0) {
1263
758k
        k = 0;
1264
758k
      }
1265
801k
    }
1266
804k
  }
1267
85.9k
  aMap[0] = startBand;
1268
345k
  for (i = 0; i < outBands; i++) {
1269
259k
    aMap[i + 1] = aMap[i] + vDk[i];
1270
259k
  }
1271
85.9k
} /* createMapping */
1272
1273
/*******************************************************************************
1274
 Functionname: mapFrequency
1275
 *******************************************************************************
1276
1277
 Description:
1278
1279
 Arguments:
1280
1281
 Return:
1282
1283
*******************************************************************************/
1284
static void mapFrequency(const SCHAR *pInput, /* Input */
1285
                         SCHAR *pOutput,      /* Output */
1286
                         int *pMap,           /* Mapping function */
1287
                         int dataBands)       /* Number of data Bands */
1288
75.9k
{
1289
75.9k
  int i, j;
1290
75.9k
  int startBand0 = pMap[0];
1291
1292
301k
  for (i = 0; i < dataBands; i++) {
1293
225k
    int startBand, stopBand, value;
1294
1295
225k
    value = pInput[i + startBand0];
1296
1297
225k
    startBand = pMap[i];
1298
225k
    stopBand = pMap[i + 1];
1299
913k
    for (j = startBand; j < stopBand; j++) {
1300
688k
      pOutput[j] = value;
1301
688k
    }
1302
225k
  }
1303
75.9k
}
1304
1305
/*******************************************************************************
1306
 Functionname: deq
1307
 *******************************************************************************
1308
1309
 Description:
1310
1311
 Arguments:
1312
1313
 Return:
1314
1315
*******************************************************************************/
1316
4.33M
static int deqIdx(int value, int paramType) {
1317
4.33M
  int idx = -1;
1318
1319
4.33M
  switch (paramType) {
1320
2.09M
    case t_CLD:
1321
2.09M
      if (((value + 15) >= 0) && ((value + 15) < 31)) {
1322
2.04M
        idx = (value + 15);
1323
2.04M
      }
1324
2.09M
      break;
1325
1326
1.85M
    case t_ICC:
1327
1.85M
      if ((value >= 0) && (value < 8)) {
1328
1.78M
        idx = value;
1329
1.78M
      }
1330
1.85M
      break;
1331
1332
389k
    case t_IPD:
1333
      /* (+/-)15 * MAX_PARAMETER_BANDS for differential coding in frequency
1334
       * domain (according to rbl) */
1335
389k
      if ((value >= -420) && (value <= 420)) {
1336
389k
        idx = (value & 0xf);
1337
389k
      }
1338
389k
      break;
1339
1340
0
    default:
1341
0
      FDK_ASSERT(0);
1342
4.33M
  }
1343
1344
4.33M
  return idx;
1345
4.33M
}
1346
1347
  /*******************************************************************************
1348
   Functionname: factorFunct
1349
   *******************************************************************************
1350
1351
   Description:
1352
1353
   Arguments:
1354
1355
   Return:
1356
1357
  *******************************************************************************/
1358
1359
0
#define SF_IDX (7)
1360
0
#define SF_FACTOR (3)
1361
#define SCALE_FACTOR (1 << SF_FACTOR)
1362
#define SCALE_CLD_C1C2 (1 << SF_CLD_C1C2)
1363
1364
0
static FIXP_DBL factorFunct(FIXP_DBL ottVsTotDb, INT quantMode) {
1365
0
  FIXP_DBL factor;
1366
1367
0
  if (ottVsTotDb > FL2FXCONST_DBL(0.0)) {
1368
0
    ottVsTotDb = FL2FXCONST_DBL(0.0);
1369
0
  }
1370
1371
0
  ottVsTotDb = -ottVsTotDb;
1372
1373
0
  switch (quantMode) {
1374
0
    case 0:
1375
0
      factor = FL2FXCONST_DBL(1.0f / SCALE_FACTOR);
1376
0
      break;
1377
0
    case 1:
1378
0
      if (ottVsTotDb >= FL2FXCONST_DBL(21.0f / SCALE_CLD_C1C2))
1379
0
        factor = FL2FXCONST_DBL(5.0f / SCALE_FACTOR);
1380
0
      else if (ottVsTotDb <= FL2FXCONST_DBL(1.0f / SCALE_CLD_C1C2))
1381
0
        factor = FL2FXCONST_DBL(1.0f / SCALE_FACTOR);
1382
0
      else
1383
0
        factor = (fMult(FL2FXCONST_DBL(0.2f), ottVsTotDb) +
1384
0
                  FL2FXCONST_DBL(0.8f / SCALE_CLD_C1C2))
1385
0
                 << (SF_CLD_C1C2 - SF_FACTOR);
1386
0
      break;
1387
0
    case 2:
1388
0
      if (ottVsTotDb >= FL2FXCONST_DBL(25.0f / SCALE_CLD_C1C2)) {
1389
0
        FDK_ASSERT(SF_FACTOR == 3);
1390
0
        factor = (FIXP_DBL)
1391
0
            MAXVAL_DBL; /* avoid warning: FL2FXCONST_DBL(8.0f/SCALE_FACTOR) */
1392
0
      } else if (ottVsTotDb <= FL2FXCONST_DBL(1.0f / SCALE_CLD_C1C2))
1393
0
        factor = FL2FXCONST_DBL(1.0f / SCALE_FACTOR);
1394
0
      else
1395
0
        factor = (fMult(FL2FXCONST_DBL(7.0f / 24.0f), ottVsTotDb) +
1396
0
                  FL2FXCONST_DBL((17.0f / 24.0f) / SCALE_CLD_C1C2))
1397
0
                 << (SF_CLD_C1C2 - SF_FACTOR);
1398
0
      break;
1399
0
    default:
1400
0
      factor = FL2FXCONST_DBL(0.0f);
1401
0
  }
1402
1403
0
  return (factor);
1404
0
}
1405
1406
/*******************************************************************************
1407
 Functionname: factorCLD
1408
 *******************************************************************************
1409
1410
 Description:
1411
1412
 Arguments:
1413
1414
 Return:
1415
1416
*******************************************************************************/
1417
static void factorCLD(SCHAR *idx, FIXP_DBL ottVsTotDb, FIXP_DBL *ottVsTotDb1,
1418
                      FIXP_DBL *ottVsTotDb2, SCHAR ottVsTotDbMode,
1419
0
                      INT quantMode) {
1420
0
  FIXP_DBL factor;
1421
0
  FIXP_DBL cldIdxFract;
1422
0
  INT cldIdx;
1423
1424
0
  factor = factorFunct(ottVsTotDb, quantMode);
1425
1426
0
  cldIdxFract =
1427
0
      fMult((FIXP_DBL)((*idx) << ((DFRACT_BITS - 1) - SF_IDX)), factor);
1428
0
  cldIdxFract += FL2FXCONST_DBL(15.5f / (1 << (SF_FACTOR + SF_IDX)));
1429
0
  cldIdx = fixp_truncateToInt(cldIdxFract, SF_FACTOR + SF_IDX);
1430
1431
0
  cldIdx = fMin(cldIdx, 30);
1432
0
  cldIdx = fMax(cldIdx, 0);
1433
1434
0
  *idx = cldIdx - 15;
1435
1436
0
  if (ottVsTotDbMode & ottVsTotDb1Activ)
1437
0
    (*ottVsTotDb1) = ottVsTotDb + dequantCLD_c1[cldIdx];
1438
1439
0
  if (ottVsTotDbMode & ottVsTotDb2Activ)
1440
0
    (*ottVsTotDb2) = ottVsTotDb + dequantCLD_c1[30 - cldIdx];
1441
0
}
1442
1443
/*******************************************************************************
1444
 Functionname: mapIndexData
1445
 *******************************************************************************
1446
1447
 Description:
1448
1449
 Arguments:
1450
1451
 Return:
1452
1453
*******************************************************************************/
1454
static SACDEC_ERROR mapIndexData(
1455
    LOSSLESSDATA *llData, SCHAR ***outputDataIdx, SCHAR ***outputIdxData,
1456
    const SCHAR (*cmpIdxData)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS],
1457
    SCHAR ***diffIdxData, SCHAR xttIdx, SCHAR **idxPrev, int paramIdx,
1458
    int paramType, int startBand, int stopBand, SCHAR defaultValue,
1459
    int numParameterSets, const int *paramSlot, int extendFrame, int quantMode,
1460
    SpatialDecConcealmentInfo *concealmentInfo, SCHAR ottVsTotDbMode,
1461
    FIXP_DBL (*pOttVsTotDbIn)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS],
1462
    FIXP_DBL (*pOttVsTotDb1)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS],
1463
331k
    FIXP_DBL (*pOttVsTotDb2)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS]) {
1464
331k
  int aParamSlots[MAX_PARAMETER_SETS];
1465
331k
  int aInterpolate[MAX_PARAMETER_SETS] = {0};
1466
1467
331k
  int dataSets;
1468
331k
  int aMap[MAX_PARAMETER_BANDS + 1];
1469
1470
331k
  int setIdx, i, band, parmSlot;
1471
331k
  int dataBands;
1472
331k
  int ps, pb;
1473
331k
  int i1;
1474
1475
331k
  if (numParameterSets > MAX_PARAMETER_SETS) return MPS_WRONG_PARAMETERSETS;
1476
1477
331k
  dataSets = 0;
1478
797k
  for (i = 0; i < numParameterSets; i++) {
1479
465k
    if (llData->bsXXXDataMode[i] == 3) {
1480
82.6k
      aParamSlots[dataSets] = i;
1481
82.6k
      dataSets++;
1482
82.6k
    }
1483
465k
  }
1484
1485
331k
  setIdx = 0;
1486
1487
  /* Main concealment stage is here: */
1488
331k
  SpatialDecConcealment_Apply(
1489
331k
      concealmentInfo, cmpIdxData[xttIdx],
1490
331k
      (diffIdxData != NULL) ? diffIdxData[xttIdx] : NULL, idxPrev[xttIdx],
1491
331k
      llData->bsXXXDataMode, startBand, stopBand, defaultValue, paramType,
1492
331k
      numParameterSets);
1493
1494
  /* Prepare data */
1495
797k
  for (i = 0; i < numParameterSets; i++) {
1496
465k
    if (llData->bsXXXDataMode[i] == 0) {
1497
299k
      llData->nocmpQuantCoarseXXX[i] = 0;
1498
2.91M
      for (band = startBand; band < stopBand; band++) {
1499
2.61M
        outputIdxData[xttIdx][i][band] = defaultValue;
1500
2.61M
      }
1501
2.91M
      for (band = startBand; band < stopBand; band++) {
1502
2.61M
        idxPrev[xttIdx][band] = outputIdxData[xttIdx][i][band];
1503
2.61M
      }
1504
      /* Because the idxPrev are also set to the defaultValue -> signalize fine
1505
       */
1506
299k
      llData->state->bsQuantCoarseXXXprev = 0;
1507
299k
    }
1508
1509
465k
    if (llData->bsXXXDataMode[i] == 1) {
1510
862k
      for (band = startBand; band < stopBand; band++) {
1511
796k
        outputIdxData[xttIdx][i][band] = idxPrev[xttIdx][band];
1512
796k
      }
1513
66.9k
      llData->nocmpQuantCoarseXXX[i] = llData->state->bsQuantCoarseXXXprev;
1514
66.9k
    }
1515
1516
465k
    if (llData->bsXXXDataMode[i] == 2) {
1517
259k
      for (band = startBand; band < stopBand; band++) {
1518
236k
        outputIdxData[xttIdx][i][band] = idxPrev[xttIdx][band];
1519
236k
      }
1520
23.4k
      llData->nocmpQuantCoarseXXX[i] = llData->state->bsQuantCoarseXXXprev;
1521
23.4k
      aInterpolate[i] = 1;
1522
442k
    } else {
1523
442k
      aInterpolate[i] = 0;
1524
442k
    }
1525
1526
465k
    if (llData->bsXXXDataMode[i] == 3) {
1527
75.9k
      int stride;
1528
1529
75.9k
      parmSlot = aParamSlots[setIdx];
1530
75.9k
      stride = pbStrideTable[llData->bsFreqResStrideXXX[setIdx]];
1531
75.9k
      dataBands = (stopBand - startBand - 1) / stride + 1;
1532
75.9k
      createMapping(aMap, startBand, stopBand, stride);
1533
75.9k
      mapFrequency(&cmpIdxData[xttIdx][setIdx][0],
1534
75.9k
                   &outputIdxData[xttIdx][parmSlot][0], aMap, dataBands);
1535
764k
      for (band = startBand; band < stopBand; band++) {
1536
688k
        idxPrev[xttIdx][band] = outputIdxData[xttIdx][parmSlot][band];
1537
688k
      }
1538
75.9k
      llData->state->bsQuantCoarseXXXprev = llData->bsQuantCoarseXXX[setIdx];
1539
75.9k
      llData->nocmpQuantCoarseXXX[i] = llData->bsQuantCoarseXXX[setIdx];
1540
1541
75.9k
      setIdx++;
1542
75.9k
    }
1543
465k
    if (diffIdxData != NULL) {
1544
2.04M
      for (band = startBand; band < stopBand; band++) {
1545
1.85M
        outputIdxData[xttIdx][i][band] += diffIdxData[xttIdx][i][band];
1546
1.85M
      }
1547
189k
    }
1548
465k
  } /* for( i = 0 ; i < numParameterSets; i++ ) */
1549
1550
  /* Map all coarse data to fine */
1551
797k
  for (i = 0; i < numParameterSets; i++) {
1552
465k
    if (llData->nocmpQuantCoarseXXX[i] == 1) {
1553
70.2k
      coarse2fine(outputIdxData[xttIdx][i], (DATA_TYPE)paramType, startBand,
1554
70.2k
                  stopBand - startBand);
1555
70.2k
      llData->nocmpQuantCoarseXXX[i] = 0;
1556
70.2k
    }
1557
465k
  }
1558
1559
  /* Interpolate */
1560
331k
  i1 = 0;
1561
797k
  for (i = 0; i < numParameterSets; i++) {
1562
465k
    if (aInterpolate[i] != 1) {
1563
442k
      i1 = i;
1564
442k
    } else {
1565
23.4k
      int xi, i2, x1, x2;
1566
1567
57.1k
      for (i2 = i; i2 < numParameterSets; i2++) {
1568
57.0k
        if (aInterpolate[i2] != 1) break;
1569
57.0k
      }
1570
23.4k
      if (i2 >= numParameterSets) return MPS_WRONG_PARAMETERSETS;
1571
1572
23.3k
      x1 = paramSlot[i1];
1573
23.3k
      xi = paramSlot[i];
1574
23.3k
      x2 = paramSlot[i2];
1575
1576
259k
      for (band = startBand; band < stopBand; band++) {
1577
235k
        int yi, y1, y2;
1578
235k
        y1 = outputIdxData[xttIdx][i1][band];
1579
235k
        y2 = outputIdxData[xttIdx][i2][band];
1580
235k
        if (x1 != x2) {
1581
235k
          yi = y1 + (xi - x1) * (y2 - y1) / (x2 - x1);
1582
235k
        } else {
1583
0
          yi = y1 /*+ (xi-x1)*(y2-y1)/1e-12*/;
1584
0
        }
1585
235k
        outputIdxData[xttIdx][i][band] = yi;
1586
235k
      }
1587
23.3k
    }
1588
465k
  } /* for( i = 0 ; i < numParameterSets; i++ ) */
1589
1590
  /* Dequantize data and apply factorCLD if necessary */
1591
796k
  for (ps = 0; ps < numParameterSets; ps++) {
1592
465k
    if (quantMode && (paramType == t_CLD)) {
1593
0
      if (pOttVsTotDbIn == 0) return MPS_WRONG_OTT;
1594
0
      if ((pOttVsTotDb1 == 0) && (ottVsTotDbMode & ottVsTotDb1Activ))
1595
0
        return MPS_WRONG_OTT;
1596
0
      if ((pOttVsTotDb2 == 0) && (ottVsTotDbMode & ottVsTotDb2Activ))
1597
0
        return MPS_WRONG_OTT;
1598
1599
0
      for (pb = startBand; pb < stopBand; pb++) {
1600
0
        factorCLD(&(outputIdxData[xttIdx][ps][pb]), (*pOttVsTotDbIn)[ps][pb],
1601
0
                  (pOttVsTotDb1 != NULL) ? &((*pOttVsTotDb1)[ps][pb]) : NULL,
1602
0
                  (pOttVsTotDb2 != NULL) ? &((*pOttVsTotDb2)[ps][pb]) : NULL,
1603
0
                  ottVsTotDbMode, quantMode);
1604
0
      }
1605
0
    }
1606
1607
    /* Dequantize data */
1608
4.80M
    for (band = startBand; band < stopBand; band++) {
1609
4.33M
      outputDataIdx[xttIdx][ps][band] =
1610
4.33M
          deqIdx(outputIdxData[xttIdx][ps][band], paramType);
1611
4.33M
      if (outputDataIdx[xttIdx][ps][band] == -1) {
1612
119k
        outputDataIdx[xttIdx][ps][band] = defaultValue;
1613
119k
      }
1614
4.33M
    }
1615
465k
  } /* for( i = 0 ; i < numParameterSets; i++ ) */
1616
1617
331k
  if (extendFrame) {
1618
5.97k
    if (paramType == t_IPD) {
1619
971
      llData->bsQuantCoarseXXX[numParameterSets] =
1620
971
          llData->bsQuantCoarseXXX[numParameterSets - 1];
1621
971
    }
1622
50.1k
    for (band = startBand; band < stopBand; band++) {
1623
44.1k
      outputDataIdx[xttIdx][numParameterSets][band] =
1624
44.1k
          outputDataIdx[xttIdx][numParameterSets - 1][band];
1625
44.1k
    }
1626
5.97k
  }
1627
1628
331k
  return MPS_OK;
1629
331k
}
1630
1631
/*******************************************************************************
1632
 Functionname: decodeAndMapFrameOtt
1633
 *******************************************************************************
1634
1635
 Description:
1636
   Do delta decoding and dequantization
1637
1638
 Arguments:
1639
1640
Input:
1641
1642
Output:
1643
1644
*******************************************************************************/
1645
static SACDEC_ERROR decodeAndMapFrameOtt(HANDLE_SPATIAL_DEC self,
1646
157k
                                         SPATIAL_BS_FRAME *pCurBs) {
1647
157k
  int i, ottIdx;
1648
157k
  int numOttBoxes;
1649
1650
157k
  SACDEC_ERROR err = MPS_OK;
1651
1652
157k
  numOttBoxes = self->numOttBoxes;
1653
1654
157k
  switch (self->treeConfig) {
1655
157k
    default: {
1656
157k
      if (self->quantMode != 0) {
1657
17.1k
        goto bail;
1658
17.1k
      }
1659
157k
    }
1660
280k
      for (i = 0; i < numOttBoxes; i++) {
1661
140k
        err = mapIndexData(
1662
140k
            &pCurBs->CLDLosslessData[i], /* LOSSLESSDATA *llData,*/
1663
140k
            self->ottCLD__FDK, self->outIdxData,
1664
140k
            pCurBs
1665
140k
                ->cmpOttCLDidx, /* int
1666
                                   cmpIdxData[MAX_NUM_OTT][MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS],
1667
                                 */
1668
140k
            NULL,               /* no differential data */
1669
140k
            i, /*  int   xttIdx,  Which ott/ttt index to use for input and
1670
                  output buffers */
1671
140k
            self->ottCLDidxPrev,                        /* int
1672
                                                           idxPrev[MAX_NUM_OTT][MAX_PARAMETER_BANDS],
1673
                                                         */
1674
140k
            i, t_CLD, 0,                                /* int   startBand, */
1675
140k
            self->pConfigCurrent->bitstreamOttBands[i], /*  int   stopBand, */
1676
140k
            self->pConfigCurrent->ottCLDdefault[i], /* int   defaultValue, */
1677
140k
            pCurBs->numParameterSets, /* int   numParameterSets) */
1678
140k
            pCurBs->paramSlot, self->extendFrame, self->quantMode,
1679
140k
            &(self->concealInfo), ottVsTotInactiv, NULL, NULL, NULL);
1680
140k
        if (err != MPS_OK) goto bail;
1681
1682
140k
      } /* for(i = 0; i < numOttBoxes ; i++ ) */
1683
140k
      break;
1684
157k
  } /* case */
1685
1686
280k
  for (ottIdx = 0; ottIdx < numOttBoxes; ottIdx++) {
1687
    /* Read ICC */
1688
140k
    err = mapIndexData(
1689
140k
        &pCurBs->ICCLosslessData[ottIdx], /* LOSSLESSDATA *llData,*/
1690
140k
        self->ottICC__FDK, self->outIdxData,
1691
140k
        pCurBs
1692
140k
            ->cmpOttICCidx,  /* int
1693
                                cmpIdxData[MAX_NUM_OTT][MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS],
1694
                              */
1695
140k
        self->ottICCdiffidx, /* differential data */
1696
140k
        ottIdx, /* int   xttIdx,  Which ott/ttt index to use for input and
1697
                   output buffers */
1698
140k
        self->ottICCidxPrev, /* int   idxPrev[MAX_NUM_OTT][MAX_PARAMETER_BANDS],
1699
                              */
1700
140k
        ottIdx, t_ICC, 0,    /* int   startBand, */
1701
140k
        self->pConfigCurrent->bitstreamOttBands[ottIdx], /* int   stopBand, */
1702
140k
        ICCdefault,               /* int   defaultValue, */
1703
140k
        pCurBs->numParameterSets, /* int   numParameterSets) */
1704
140k
        pCurBs->paramSlot, self->extendFrame, self->quantMode,
1705
140k
        &(self->concealInfo), ottVsTotInactiv, NULL, NULL, NULL);
1706
140k
    if (err != MPS_OK) goto bail;
1707
140k
  } /* ottIdx */
1708
1709
140k
  if ((self->treeConfig == TREE_212) && (self->phaseCoding)) {
1710
16.0k
    if (pCurBs->phaseMode == 0) {
1711
100k
      for (int pb = 0; pb < self->pConfigCurrent->numOttBandsIPD; pb++) {
1712
88.1k
        self->ottIPDidxPrev[0][pb] = 0;
1713
88.1k
      }
1714
12.7k
    }
1715
32.1k
    for (ottIdx = 0; ottIdx < numOttBoxes; ottIdx++) {
1716
16.0k
      err = mapIndexData(
1717
16.0k
          &pCurBs->IPDLosslessData[ottIdx], self->ottIPD__FDK, self->outIdxData,
1718
16.0k
          pCurBs->cmpOttIPDidx, NULL, ottIdx, self->ottIPDidxPrev, ottIdx,
1719
16.0k
          t_IPD, 0, self->numOttBandsIPD, IPDdefault, pCurBs->numParameterSets,
1720
16.0k
          pCurBs->paramSlot, self->extendFrame, self->quantMode,
1721
16.0k
          &(self->concealInfo), ottVsTotInactiv, NULL, NULL, NULL);
1722
16.0k
    }
1723
16.0k
  }
1724
1725
157k
bail:
1726
1727
157k
  return MPS_OK;
1728
1729
140k
} /* decodeAndMapFrameOtt */
1730
1731
/*******************************************************************************
1732
 Functionname: decodeAndMapFrameSmg
1733
 *******************************************************************************
1734
1735
 Description:
1736
   Decode smoothing flags
1737
1738
 Arguments:
1739
1740
Input:
1741
1742
Output:
1743
1744
1745
*******************************************************************************/
1746
static SACDEC_ERROR decodeAndMapFrameSmg(HANDLE_SPATIAL_DEC self,
1747
157k
                                         const SPATIAL_BS_FRAME *frame) {
1748
157k
  int ps, pb, pg, pbStride, dataBands, pbStart, pbStop,
1749
157k
      aGroupToBand[MAX_PARAMETER_BANDS + 1];
1750
1751
157k
  if (frame->numParameterSets > MAX_PARAMETER_SETS)
1752
0
    return MPS_WRONG_PARAMETERSETS;
1753
157k
  if (self->bitstreamParameterBands > MAX_PARAMETER_BANDS)
1754
0
    return MPS_WRONG_PARAMETERBANDS;
1755
1756
363k
  for (ps = 0; ps < frame->numParameterSets; ps++) {
1757
206k
    switch (frame->bsSmoothMode[ps]) {
1758
179k
      case 0:
1759
179k
        self->smgTime[ps] = 256;
1760
179k
        FDKmemclear(self->smgData[ps],
1761
179k
                    self->bitstreamParameterBands * sizeof(UCHAR));
1762
179k
        break;
1763
1764
11.1k
      case 1:
1765
11.1k
        if (ps > 0) {
1766
9.77k
          self->smgTime[ps] = self->smgTime[ps - 1];
1767
9.77k
          FDKmemcpy(self->smgData[ps], self->smgData[ps - 1],
1768
9.77k
                    self->bitstreamParameterBands * sizeof(UCHAR));
1769
9.77k
        } else {
1770
1.35k
          self->smgTime[ps] = self->smoothState->prevSmgTime;
1771
1.35k
          FDKmemcpy(self->smgData[ps], self->smoothState->prevSmgData,
1772
1.35k
                    self->bitstreamParameterBands * sizeof(UCHAR));
1773
1.35k
        }
1774
11.1k
        break;
1775
1776
5.43k
      case 2:
1777
5.43k
        self->smgTime[ps] = smgTimeTable[frame->bsSmoothTime[ps]];
1778
59.0k
        for (pb = 0; pb < self->bitstreamParameterBands; pb++) {
1779
53.6k
          self->smgData[ps][pb] = 1;
1780
53.6k
        }
1781
5.43k
        break;
1782
1783
10.0k
      case 3:
1784
10.0k
        self->smgTime[ps] = smgTimeTable[frame->bsSmoothTime[ps]];
1785
10.0k
        pbStride = pbStrideTable[frame->bsFreqResStrideSmg[ps]];
1786
10.0k
        dataBands = (self->bitstreamParameterBands - 1) / pbStride + 1;
1787
10.0k
        createMapping(aGroupToBand, 0, self->bitstreamParameterBands, pbStride);
1788
42.3k
        for (pg = 0; pg < dataBands; pg++) {
1789
32.3k
          pbStart = aGroupToBand[pg];
1790
32.3k
          pbStop = aGroupToBand[pg + 1];
1791
122k
          for (pb = pbStart; pb < pbStop; pb++) {
1792
89.8k
            self->smgData[ps][pb] = frame->bsSmgData[ps][pg];
1793
89.8k
          }
1794
32.3k
        }
1795
10.0k
        break;
1796
206k
    }
1797
206k
  }
1798
1799
157k
  self->smoothState->prevSmgTime = self->smgTime[frame->numParameterSets - 1];
1800
157k
  FDKmemcpy(self->smoothState->prevSmgData,
1801
157k
            self->smgData[frame->numParameterSets - 1],
1802
157k
            self->bitstreamParameterBands * sizeof(UCHAR));
1803
1804
157k
  if (self->extendFrame) {
1805
2.48k
    self->smgTime[frame->numParameterSets] =
1806
2.48k
        self->smgTime[frame->numParameterSets - 1];
1807
2.48k
    FDKmemcpy(self->smgData[frame->numParameterSets],
1808
2.48k
              self->smgData[frame->numParameterSets - 1],
1809
2.48k
              self->bitstreamParameterBands * sizeof(UCHAR));
1810
2.48k
  }
1811
1812
157k
  return MPS_OK;
1813
157k
}
1814
1815
/*******************************************************************************
1816
 Functionname: decodeAndMapFrameArbdmx
1817
 *******************************************************************************
1818
1819
 Description:
1820
   Do delta decoding and dequantization
1821
1822
 Arguments:
1823
1824
Input:
1825
1826
Output:
1827
1828
*******************************************************************************/
1829
static SACDEC_ERROR decodeAndMapFrameArbdmx(HANDLE_SPATIAL_DEC self,
1830
35.4k
                                            const SPATIAL_BS_FRAME *frame) {
1831
35.4k
  SACDEC_ERROR err = MPS_OK;
1832
35.4k
  int ch;
1833
35.4k
  int offset = self->numOttBoxes;
1834
1835
70.8k
  for (ch = 0; ch < self->numInputChannels; ch++) {
1836
35.4k
    err = mapIndexData(&frame->CLDLosslessData[offset + ch],
1837
35.4k
                       self->arbdmxGain__FDK, self->outIdxData,
1838
35.4k
                       frame->cmpArbdmxGainIdx, NULL, /* no differential data */
1839
35.4k
                       ch, self->arbdmxGainIdxPrev, offset + ch, t_CLD, 0,
1840
35.4k
                       self->bitstreamParameterBands,
1841
35.4k
                       0 /*self->arbdmxGainDefault*/, frame->numParameterSets,
1842
35.4k
                       frame->paramSlot, self->extendFrame, 0,
1843
35.4k
                       &(self->concealInfo), ottVsTotInactiv, NULL, NULL, NULL);
1844
35.4k
    if (err != MPS_OK) goto bail;
1845
35.4k
  }
1846
1847
35.4k
bail:
1848
35.4k
  return err;
1849
35.4k
} /* decodeAndMapFrameArbdmx */
1850
1851
/*******************************************************************************
1852
 Functionname: SpatialDecDecodeFrame
1853
 *******************************************************************************
1854
1855
 Description:
1856
1857
 Arguments:
1858
1859
 Return:
1860
1861
*******************************************************************************/
1862
157k
SACDEC_ERROR SpatialDecDecodeFrame(spatialDec *self, SPATIAL_BS_FRAME *frame) {
1863
157k
  SACDEC_ERROR err = MPS_OK;
1864
1865
157k
  self->extendFrame = 0;
1866
157k
  if (frame->paramSlot[frame->numParameterSets - 1] != self->timeSlots - 1) {
1867
2.48k
    self->extendFrame = 1;
1868
2.48k
  }
1869
1870
157k
  self->TsdTs = 0;
1871
1872
  /****** DTDF and MAP DATA ********/
1873
157k
  if ((err = decodeAndMapFrameOtt(self, frame)) != MPS_OK) goto bail;
1874
1875
157k
  if ((err = decodeAndMapFrameSmg(self, frame)) != MPS_OK) goto bail;
1876
1877
157k
  if (self->arbitraryDownmix != 0) {
1878
35.4k
    if ((err = decodeAndMapFrameArbdmx(self, frame)) != MPS_OK) goto bail;
1879
35.4k
  }
1880
1881
157k
  if (self->extendFrame) {
1882
2.48k
    frame->numParameterSets =
1883
2.48k
        fixMin(MAX_PARAMETER_SETS, frame->numParameterSets + 1);
1884
2.48k
    frame->paramSlot[frame->numParameterSets - 1] = self->timeSlots - 1;
1885
1886
8.98k
    for (int p = 0; p < frame->numParameterSets; p++) {
1887
6.50k
      if (frame->paramSlot[p] > self->timeSlots - 1) {
1888
0
        frame->paramSlot[p] = self->timeSlots - 1;
1889
0
        err = MPS_PARSE_ERROR;
1890
0
      }
1891
6.50k
    }
1892
2.48k
    if (err != MPS_OK) {
1893
0
      goto bail;
1894
0
    }
1895
2.48k
  }
1896
1897
157k
bail:
1898
157k
  return err;
1899
157k
} /* SpatialDecDecodeFrame() */
1900
1901
/*******************************************************************************
1902
 Functionname: SpatialDecodeHeader
1903
 *******************************************************************************
1904
1905
 Description:
1906
1907
 Arguments:
1908
1909
 Return:
1910
1911
*******************************************************************************/
1912
1913
SACDEC_ERROR SpatialDecDecodeHeader(
1914
55.6k
    spatialDec *self, SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig) {
1915
55.6k
  SACDEC_ERROR err = MPS_OK;
1916
55.6k
  int i;
1917
1918
55.6k
  self->samplingFreq = pSpatialSpecificConfig->samplingFreq;
1919
55.6k
  self->timeSlots = pSpatialSpecificConfig->nTimeSlots;
1920
55.6k
  self->frameLength = self->timeSlots * self->qmfBands;
1921
55.6k
  self->bitstreamParameterBands = pSpatialSpecificConfig->freqRes;
1922
1923
55.6k
  if (self->pConfigCurrent->syntaxFlags & SACDEC_SYNTAX_LD)
1924
29.6k
    self->hybridBands = self->qmfBands;
1925
25.9k
  else
1926
25.9k
    self->hybridBands = SacGetHybridSubbands(self->qmfBands);
1927
55.6k
  self->tp_hybBandBorder = 12;
1928
1929
55.6k
  self->numParameterBands = self->bitstreamParameterBands;
1930
1931
55.6k
  if (self->pConfigCurrent->syntaxFlags & SACDEC_SYNTAX_LD) {
1932
29.6k
    switch (self->numParameterBands) {
1933
629
      case 4:
1934
629
        self->kernels = kernels_4_to_64;
1935
629
        break;
1936
11.4k
      case 5:
1937
11.4k
        self->kernels = kernels_5_to_64;
1938
11.4k
        break;
1939
375
      case 7:
1940
375
        self->kernels = kernels_7_to_64;
1941
375
        break;
1942
12.2k
      case 9:
1943
12.2k
        self->kernels = kernels_9_to_64;
1944
12.2k
        break;
1945
354
      case 12:
1946
354
        self->kernels = kernels_12_to_64;
1947
354
        break;
1948
1.82k
      case 15:
1949
1.82k
        self->kernels = kernels_15_to_64;
1950
1.82k
        break;
1951
1.92k
      case 23:
1952
1.92k
        self->kernels = kernels_23_to_64;
1953
1.92k
        break;
1954
776
      default:
1955
776
        return MPS_INVALID_PARAMETERBANDS; /* unsupported numParameterBands */
1956
29.6k
    }
1957
29.6k
  } else {
1958
25.9k
    switch (self->numParameterBands) {
1959
1.07k
      case 4:
1960
1.07k
        self->kernels = kernels_4_to_71;
1961
1.07k
        break;
1962
1.15k
      case 5:
1963
1.15k
        self->kernels = kernels_5_to_71;
1964
1.15k
        break;
1965
8.63k
      case 7:
1966
8.63k
        self->kernels = kernels_7_to_71;
1967
8.63k
        break;
1968
3.84k
      case 10:
1969
3.84k
        self->kernels = kernels_10_to_71;
1970
3.84k
        break;
1971
2.43k
      case 14:
1972
2.43k
        self->kernels = kernels_14_to_71;
1973
2.43k
        break;
1974
6.55k
      case 20:
1975
6.55k
        self->kernels = kernels_20_to_71;
1976
6.55k
        break;
1977
2.28k
      case 28:
1978
2.28k
        self->kernels = kernels_28_to_71;
1979
2.28k
        break;
1980
0
      default:
1981
0
        return MPS_INVALID_PARAMETERBANDS; /* unsupported numParameterBands */
1982
25.9k
    }
1983
25.9k
  }
1984
1985
  /* create param to hyb band table */
1986
54.8k
  FDKmemclear(self->param2hyb, (MAX_PARAMETER_BANDS + 1) * sizeof(int));
1987
2.65M
  for (i = 0; i < self->hybridBands; i++) {
1988
2.60M
    self->param2hyb[self->kernels[i] + 1] = i + 1;
1989
2.60M
  }
1990
54.8k
  {
1991
54.8k
    int pb = self->kernels[i - 1] + 2;
1992
1.01M
    for (; pb < (MAX_PARAMETER_BANDS + 1); pb++) {
1993
960k
      self->param2hyb[pb] = i;
1994
960k
    }
1995
1.58M
    for (pb = 0; pb < MAX_PARAMETER_BANDS; pb += 1) {
1996
1.53M
      self->kernels_width[pb] = self->param2hyb[pb + 1] - self->param2hyb[pb];
1997
1.53M
    }
1998
54.8k
  }
1999
2000
54.8k
  self->treeConfig = pSpatialSpecificConfig->treeConfig;
2001
2002
54.8k
  self->numOttBoxes = pSpatialSpecificConfig->nOttBoxes;
2003
2004
54.8k
  self->numInputChannels = pSpatialSpecificConfig->nInputChannels;
2005
2006
54.8k
  self->numOutputChannels = pSpatialSpecificConfig->nOutputChannels;
2007
2008
54.8k
  self->quantMode = pSpatialSpecificConfig->quantMode;
2009
2010
54.8k
  self->arbitraryDownmix = pSpatialSpecificConfig->bArbitraryDownmix;
2011
2012
54.8k
  self->numM2rows = self->numOutputChannels;
2013
2014
54.8k
  {
2015
54.8k
    self->residualCoding = 0;
2016
54.8k
    if (self->arbitraryDownmix == 2)
2017
0
      self->arbitraryDownmix = 1; /* no arbitrary downmix residuals */
2018
54.8k
  }
2019
54.8k
  if ((self->pConfigCurrent->syntaxFlags & SACDEC_SYNTAX_USAC)) {
2020
25.9k
    self->residualCoding = pSpatialSpecificConfig->bResidualCoding;
2021
25.9k
  }
2022
2023
54.8k
  self->clipProtectGain__FDK =
2024
54.8k
      FX_CFG2FX_DBL(clipGainTable__FDK[pSpatialSpecificConfig->bsFixedGainDMX]);
2025
54.8k
  self->clipProtectGainSF__FDK =
2026
54.8k
      clipGainSFTable__FDK[pSpatialSpecificConfig->bsFixedGainDMX];
2027
2028
54.8k
  self->tempShapeConfig = pSpatialSpecificConfig->tempShapeConfig;
2029
2030
54.8k
  self->decorrConfig = pSpatialSpecificConfig->decorrConfig;
2031
2032
54.8k
  if (self->upmixType == UPMIXTYPE_BYPASS) {
2033
0
    self->numOutputChannels = self->numInputChannels;
2034
0
  }
2035
2036
54.8k
  self->numOutputChannelsAT = self->numOutputChannels;
2037
2038
54.8k
  self->numOttBandsIPD = pSpatialSpecificConfig->numOttBandsIPD;
2039
54.8k
  self->phaseCoding = pSpatialSpecificConfig->bsPhaseCoding;
2040
109k
  for (i = 0; i < self->numOttBoxes; i++) {
2041
54.8k
    {
2042
54.8k
      self->pConfigCurrent->bitstreamOttBands[i] =
2043
54.8k
          self->bitstreamParameterBands;
2044
54.8k
    }
2045
54.8k
    self->numOttBands[i] = self->pConfigCurrent->bitstreamOttBands[i];
2046
54.8k
  } /* i */
2047
2048
54.8k
  if (self->residualCoding) {
2049
6.27k
    int numBoxes = self->numOttBoxes;
2050
12.5k
    for (i = 0; i < numBoxes; i++) {
2051
6.27k
      self->residualPresent[i] =
2052
6.27k
          pSpatialSpecificConfig->ResidualConfig[i].bResidualPresent;
2053
2054
6.27k
      if (self->residualPresent[i]) {
2055
6.27k
        self->residualBands[i] =
2056
6.27k
            pSpatialSpecificConfig->ResidualConfig[i].nResidualBands;
2057
        /* conversion from hybrid bands to qmf bands */
2058
6.27k
        self->residualQMFBands[i] =
2059
6.27k
            fMax(self->param2hyb[self->residualBands[i]] + 3 - 10,
2060
6.27k
                 3); /* simplification for the lowest 10 hybrid bands */
2061
6.27k
      } else {
2062
0
        self->residualBands[i] = 0;
2063
0
        self->residualQMFBands[i] = 0;
2064
0
      }
2065
6.27k
    }
2066
6.27k
  } /* self->residualCoding */
2067
48.5k
  else {
2068
48.5k
    int boxes = self->numOttBoxes;
2069
97.0k
    for (i = 0; i < boxes; i += 1) {
2070
48.5k
      self->residualPresent[i] = 0;
2071
48.5k
      self->residualBands[i] = 0;
2072
48.5k
    }
2073
48.5k
  }
2074
2075
54.8k
  switch (self->treeConfig) {
2076
54.8k
    case TREE_212:
2077
54.8k
      self->numDirektSignals = 1;
2078
54.8k
      self->numDecorSignals = 1;
2079
54.8k
      self->numXChannels = 1;
2080
54.8k
      if (self->arbitraryDownmix == 2) {
2081
0
        self->numXChannels += 1;
2082
0
      }
2083
54.8k
      self->numVChannels = self->numDirektSignals + self->numDecorSignals;
2084
54.8k
      break;
2085
0
    default:
2086
0
      return MPS_INVALID_TREECONFIG;
2087
54.8k
  }
2088
2089
54.8k
  self->highRateMode = pSpatialSpecificConfig->bsHighRateMode;
2090
54.8k
  self->decorrType = pSpatialSpecificConfig->bsDecorrType;
2091
2092
54.8k
  SpatialDecDecodeHelperInfo(pSpatialSpecificConfig, UPMIXTYPE_NORMAL);
2093
2094
54.8k
  return err;
2095
54.8k
}
2096
2097
/*******************************************************************************
2098
 Functionname: SpatialDecCreateBsFrame
2099
 *******************************************************************************
2100
2101
 Description: Create spatial bitstream structure
2102
2103
 Arguments:   spatialDec* self
2104
              const SPATIAL_BS_FRAME **bsFrame
2105
2106
 Return:      -
2107
2108
*******************************************************************************/
2109
SACDEC_ERROR SpatialDecCreateBsFrame(SPATIAL_BS_FRAME *bsFrame,
2110
23.8k
                                     BS_LL_STATE *llState) {
2111
23.8k
  SPATIAL_BS_FRAME *pBs = bsFrame;
2112
2113
23.8k
  const int maxNumOtt = MAX_NUM_OTT;
2114
23.8k
  const int maxNumInputChannels = MAX_INPUT_CHANNELS;
2115
2116
23.8k
  FDK_ALLOCATE_MEMORY_1D_P(
2117
23.8k
      pBs->cmpOttIPDidx, maxNumOtt * MAX_PARAMETER_SETS * MAX_PARAMETER_BANDS,
2118
23.8k
      SCHAR, SCHAR(*)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS])
2119
2120
  /* Arbitrary Downmix */
2121
23.8k
  FDK_ALLOCATE_MEMORY_1D_P(
2122
23.8k
      pBs->cmpArbdmxGainIdx,
2123
23.8k
      maxNumInputChannels * MAX_PARAMETER_SETS * MAX_PARAMETER_BANDS, SCHAR,
2124
23.8k
      SCHAR(*)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS])
2125
2126
  /* Lossless control */
2127
23.8k
  FDK_ALLOCATE_MEMORY_1D(pBs->CLDLosslessData, MAX_NUM_PARAMETERS, LOSSLESSDATA)
2128
23.8k
  FDK_ALLOCATE_MEMORY_1D(pBs->ICCLosslessData, MAX_NUM_PARAMETERS, LOSSLESSDATA)
2129
2130
23.8k
  FDK_ALLOCATE_MEMORY_1D(pBs->IPDLosslessData, MAX_NUM_PARAMETERS, LOSSLESSDATA)
2131
2132
23.8k
  pBs->newBsData = 0;
2133
23.8k
  pBs->numParameterSets = 1;
2134
2135
  /* Link lossless states */
2136
167k
  for (int x = 0; x < MAX_NUM_PARAMETERS; x++) {
2137
143k
    pBs->CLDLosslessData[x].state = &llState->CLDLosslessState[x];
2138
143k
    pBs->ICCLosslessData[x].state = &llState->ICCLosslessState[x];
2139
2140
143k
    pBs->IPDLosslessData[x].state = &llState->IPDLosslessState[x];
2141
143k
  }
2142
2143
23.8k
  return MPS_OK;
2144
2145
0
bail:
2146
0
  return MPS_OUTOFMEMORY;
2147
23.8k
}
2148
2149
/*******************************************************************************
2150
 Functionname: SpatialDecCloseBsFrame
2151
 *******************************************************************************
2152
2153
 Description: Close spatial bitstream structure
2154
2155
 Arguments:   spatialDec* self
2156
2157
 Return:      -
2158
2159
*******************************************************************************/
2160
23.8k
void SpatialDecCloseBsFrame(SPATIAL_BS_FRAME *pBs) {
2161
23.8k
  if (pBs != NULL) {
2162
    /* These arrays contain the compact indices, only one value per pbstride,
2163
     * only paramsets actually containing data. */
2164
2165
23.8k
    FDK_FREE_MEMORY_1D(pBs->cmpOttIPDidx);
2166
2167
    /* Arbitrary Downmix */
2168
23.8k
    FDK_FREE_MEMORY_1D(pBs->cmpArbdmxGainIdx);
2169
2170
    /* Lossless control */
2171
23.8k
    FDK_FREE_MEMORY_1D(pBs->IPDLosslessData);
2172
23.8k
    FDK_FREE_MEMORY_1D(pBs->CLDLosslessData);
2173
    FDK_FREE_MEMORY_1D(pBs->ICCLosslessData);
2174
23.8k
  }
2175
23.8k
}