Coverage Report

Created: 2024-06-17 06:33

/src/aac/libSACdec/src/sac_bitdec.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 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
182k
    SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig, UPMIXTYPE upmixType) {
120
182k
  int i;
121
182k
  UINT syntaxFlags;
122
123
  /* Determine bit stream syntax */
124
182k
  syntaxFlags = 0;
125
182k
  switch (pSpatialSpecificConfig->coreCodec) {
126
62.4k
    case AOT_ER_AAC_ELD:
127
62.4k
    case AOT_ER_AAC_LD:
128
62.4k
      syntaxFlags |= SACDEC_SYNTAX_LD;
129
62.4k
      break;
130
119k
    case AOT_USAC:
131
119k
      syntaxFlags |= SACDEC_SYNTAX_USAC;
132
119k
      break;
133
0
    case AOT_NONE:
134
359
    default:
135
359
      return MPS_UNSUPPORTED_FORMAT;
136
182k
  }
137
138
182k
  pSpatialSpecificConfig->syntaxFlags = syntaxFlags;
139
140
182k
  switch (pSpatialSpecificConfig->treeConfig) {
141
182k
    case TREE_212: {
142
182k
      pSpatialSpecificConfig->ottCLDdefault[0] = 0;
143
182k
    } break;
144
0
    default:
145
0
      return MPS_INVALID_TREECONFIG;
146
182k
  }
147
148
182k
  if (syntaxFlags & SACDEC_SYNTAX_USAC) {
149
119k
    if (pSpatialSpecificConfig->bsOttBandsPhasePresent) {
150
32.2k
      pSpatialSpecificConfig->numOttBandsIPD =
151
32.2k
          pSpatialSpecificConfig->bsOttBandsPhase;
152
87.4k
    } else {
153
87.4k
      int numParameterBands;
154
155
87.4k
      numParameterBands = pSpatialSpecificConfig->freqRes;
156
87.4k
      switch (numParameterBands) {
157
3.14k
        case 4:
158
6.95k
        case 5:
159
6.95k
          pSpatialSpecificConfig->numOttBandsIPD = 2;
160
6.95k
          break;
161
40.3k
        case 7:
162
40.3k
          pSpatialSpecificConfig->numOttBandsIPD = 3;
163
40.3k
          break;
164
16.5k
        case 10:
165
16.5k
          pSpatialSpecificConfig->numOttBandsIPD = 5;
166
16.5k
          break;
167
1.23k
        case 14:
168
1.23k
          pSpatialSpecificConfig->numOttBandsIPD = 7;
169
1.23k
          break;
170
4.65k
        case 20:
171
22.4k
        case 28:
172
22.4k
          pSpatialSpecificConfig->numOttBandsIPD = 10;
173
22.4k
          break;
174
0
        default:
175
0
          return MPS_INVALID_PARAMETERBANDS;
176
87.4k
      }
177
87.4k
    }
178
119k
  } else {
179
62.4k
    pSpatialSpecificConfig->numOttBandsIPD = 0;
180
62.4k
  }
181
364k
  for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
182
182k
    {
183
182k
      pSpatialSpecificConfig->bitstreamOttBands[i] =
184
182k
          pSpatialSpecificConfig->freqRes;
185
182k
    }
186
182k
    {
187
182k
      pSpatialSpecificConfig->numOttBands[i] =
188
182k
          pSpatialSpecificConfig->bitstreamOttBands[i];
189
182k
      if (syntaxFlags & SACDEC_SYNTAX_USAC &&
190
182k
          !pSpatialSpecificConfig->bsOttBandsPhasePresent) {
191
87.4k
        if (pSpatialSpecificConfig->bResidualCoding &&
192
87.4k
            pSpatialSpecificConfig->ResidualConfig[i].bResidualPresent &&
193
87.4k
            (pSpatialSpecificConfig->numOttBandsIPD <
194
34.1k
             pSpatialSpecificConfig->ResidualConfig[i].nResidualBands)) {
195
21.5k
          pSpatialSpecificConfig->numOttBandsIPD =
196
21.5k
              pSpatialSpecificConfig->ResidualConfig[i].nResidualBands;
197
21.5k
        }
198
87.4k
      }
199
182k
    }
200
182k
  } /* i */
201
202
182k
  return MPS_OK;
203
182k
}
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
35.4k
    int numOttBoxes, int numTttBoxes, int numOutChan, int bitsAvailable) {
220
35.4k
  SACDEC_ERROR err = MPS_OK;
221
35.4k
  INT ba = bitsAvailable;
222
223
35.4k
  config->sacExtCnt = 0;
224
35.4k
  config->bResidualCoding = 0;
225
226
35.4k
  ba = fMin((int)FDKgetValidBits(bitstream), ba);
227
228
71.9k
  while ((ba >= 8) && (config->sacExtCnt < MAX_NUM_EXT_TYPES)) {
229
37.5k
    int bitsRead, nFillBits;
230
37.5k
    INT tmp;
231
37.5k
    UINT sacExtLen;
232
233
37.5k
    config->sacExtType[config->sacExtCnt] = FDKreadBits(bitstream, 4);
234
37.5k
    ba -= 4;
235
236
37.5k
    sacExtLen = FDKreadBits(bitstream, 4);
237
37.5k
    ba -= 4;
238
239
37.5k
    if (sacExtLen == 15) {
240
1.79k
      sacExtLen += FDKreadBits(bitstream, 8);
241
1.79k
      ba -= 8;
242
1.79k
      if (sacExtLen == 15 + 255) {
243
590
        sacExtLen += FDKreadBits(bitstream, 16);
244
590
        ba -= 16;
245
590
      }
246
1.79k
    }
247
248
37.5k
    tmp = (INT)FDKgetValidBits(
249
37.5k
        bitstream); /* Extension config payload start anchor. */
250
37.5k
    if ((tmp <= 0) || (tmp < (INT)sacExtLen * 8) || (ba < (INT)sacExtLen * 8)) {
251
1.08k
      err = MPS_PARSE_ERROR;
252
1.08k
      goto bail;
253
1.08k
    }
254
255
36.5k
    switch (config->sacExtType[config->sacExtCnt]) {
256
36.5k
      default:; /* unknown extension data => do nothing */
257
36.5k
    }
258
259
    /* skip remaining extension data */
260
36.5k
    bitsRead = tmp - FDKgetValidBits(bitstream);
261
36.5k
    nFillBits = 8 * sacExtLen - bitsRead;
262
263
36.5k
    if (nFillBits < 0) {
264
0
      err = MPS_PARSE_ERROR;
265
0
      goto bail;
266
36.5k
    } else {
267
      /* Skip fill bits or an unkown extension. */
268
36.5k
      FDKpushFor(bitstream, nFillBits);
269
36.5k
    }
270
271
36.5k
    ba -= 8 * sacExtLen;
272
36.5k
    config->sacExtCnt++;
273
36.5k
  }
274
275
35.4k
bail:
276
35.4k
  return err;
277
35.4k
}
278
279
SACDEC_ERROR SpatialDecParseSpecificConfigHeader(
280
    HANDLE_FDK_BITSTREAM bitstream,
281
    SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig,
282
4.28k
    AUDIO_OBJECT_TYPE coreCodec, SPATIAL_DEC_UPMIX_TYPE upmixType) {
283
4.28k
  SACDEC_ERROR err = MPS_OK;
284
4.28k
  INT numFillBits;
285
4.28k
  int sacHeaderLen = 0;
286
4.28k
  int sacTimeAlignFlag = 0;
287
288
4.28k
  sacTimeAlignFlag = FDKreadBits(bitstream, 1);
289
4.28k
  sacHeaderLen = FDKreadBits(bitstream, 7);
290
291
4.28k
  if (sacHeaderLen == 127) {
292
27
    sacHeaderLen += FDKreadBits(bitstream, 16);
293
27
  }
294
4.28k
  numFillBits = (INT)FDKgetValidBits(bitstream);
295
296
4.28k
  err = SpatialDecParseSpecificConfig(bitstream, pSpatialSpecificConfig,
297
4.28k
                                      sacHeaderLen, coreCodec);
298
299
4.28k
  numFillBits -=
300
4.28k
      (INT)FDKgetValidBits(bitstream); /* the number of read bits (tmpBits) */
301
4.28k
  numFillBits = (8 * sacHeaderLen) - numFillBits;
302
4.28k
  if (numFillBits < 0) {
303
    /* Parsing went wrong */
304
61
    err = MPS_PARSE_ERROR;
305
61
  }
306
  /* Move to the very end of the SSC */
307
4.28k
  FDKpushBiDirectional(bitstream, numFillBits);
308
309
4.28k
  if ((err == MPS_OK) && sacTimeAlignFlag) {
310
    /* not supported */
311
126
    FDKreadBits(bitstream, 16);
312
126
    err = MPS_UNSUPPORTED_CONFIG;
313
126
  }
314
315
  /* Derive additional helper variables */
316
4.28k
  SpatialDecDecodeHelperInfo(pSpatialSpecificConfig, (UPMIXTYPE)upmixType);
317
318
4.28k
  return err;
319
4.28k
}
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
102k
    INT coreSbrFrameLengthIndex) {
326
102k
  int i;
327
328
102k
  FDKmemclear(pSpatialSpecificConfig, sizeof(SPATIAL_SPECIFIC_CONFIG));
329
330
102k
  pSpatialSpecificConfig->stereoConfigIndex = stereoConfigIndex;
331
102k
  pSpatialSpecificConfig->coreSbrFrameLengthIndex = coreSbrFrameLengthIndex;
332
102k
  pSpatialSpecificConfig->freqRes =
333
102k
      (SPATIALDEC_FREQ_RES)freqResTable[FDKreadBits(bitstream, 3)];
334
102k
  if (pSpatialSpecificConfig->freqRes == 0) {
335
2.73k
    return MPS_PARSE_ERROR; /* reserved value */
336
2.73k
  }
337
338
99.9k
  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
99.9k
    case AOT_USAC:
349
99.9k
      pSpatialSpecificConfig->bsFixedGainDMX =
350
99.9k
          (SPATIALDEC_FIXED_GAINS)FDKreadBits(bitstream, 3);
351
99.9k
      pSpatialSpecificConfig->tempShapeConfig =
352
99.9k
          (SPATIALDEC_TS_CONF)FDKreadBits(bitstream, 2);
353
99.9k
      pSpatialSpecificConfig->decorrConfig =
354
99.9k
          (SPATIALDEC_DECORR_CONF)FDKreadBits(bitstream, 2);
355
99.9k
      if (pSpatialSpecificConfig->decorrConfig > 2) {
356
480
        return MPS_PARSE_ERROR; /* reserved value */
357
480
      }
358
99.4k
      pSpatialSpecificConfig->bsDecorrType = 0;
359
99.4k
      break;
360
0
    default:
361
0
      return MPS_UNSUPPORTED_FORMAT;
362
99.9k
  }
363
99.4k
  pSpatialSpecificConfig->nTimeSlots = (coreSbrFrameLengthIndex == 4) ? 64 : 32;
364
99.4k
  pSpatialSpecificConfig->bsHighRateMode = (UCHAR)FDKreadBits(bitstream, 1);
365
366
99.4k
  {
367
99.4k
    pSpatialSpecificConfig->bsPhaseCoding = (UCHAR)FDKreadBits(bitstream, 1);
368
99.4k
    pSpatialSpecificConfig->bsOttBandsPhasePresent =
369
99.4k
        (UCHAR)FDKreadBits(bitstream, 1);
370
99.4k
    if (pSpatialSpecificConfig->bsOttBandsPhasePresent) {
371
31.3k
      if (MAX_PARAMETER_BANDS < (pSpatialSpecificConfig->bsOttBandsPhase =
372
31.3k
                                     FDKreadBits(bitstream, 5))) {
373
227
        return MPS_PARSE_ERROR;
374
227
      }
375
68.1k
    } else {
376
68.1k
      pSpatialSpecificConfig->bsOttBandsPhase = 0;
377
68.1k
    }
378
99.4k
  }
379
380
99.2k
  if (stereoConfigIndex > 1) { /* do residual coding */
381
57.0k
    pSpatialSpecificConfig->bResidualCoding = 1;
382
57.0k
    pSpatialSpecificConfig->ResidualConfig->bResidualPresent = 1;
383
57.0k
    if (pSpatialSpecificConfig->freqRes <
384
57.0k
        (pSpatialSpecificConfig->ResidualConfig->nResidualBands =
385
57.0k
             FDKreadBits(bitstream, 5))) {
386
511
      return MPS_PARSE_ERROR;
387
511
    }
388
56.5k
    pSpatialSpecificConfig->bsOttBandsPhase =
389
56.5k
        fMax(pSpatialSpecificConfig->bsOttBandsPhase,
390
56.5k
             pSpatialSpecificConfig->ResidualConfig->nResidualBands);
391
56.5k
    pSpatialSpecificConfig->bsPseudoLr = (UCHAR)FDKreadBits(bitstream, 1);
392
393
56.5k
    if (pSpatialSpecificConfig->bsPhaseCoding) {
394
19.7k
      pSpatialSpecificConfig->bsPhaseCoding = 3;
395
19.7k
    }
396
56.5k
  } else {
397
42.1k
    pSpatialSpecificConfig->bResidualCoding = 0;
398
42.1k
    pSpatialSpecificConfig->ResidualConfig->bResidualPresent = 0;
399
42.1k
  }
400
401
98.6k
  if (pSpatialSpecificConfig->tempShapeConfig == 2) {
402
58.1k
    switch (coreCodec) {
403
58.1k
      case AOT_USAC:
404
58.1k
        pSpatialSpecificConfig->envQuantMode = FDKreadBits(bitstream, 1);
405
58.1k
        break;
406
0
      default: /* added to avoid compiler warning */
407
0
        break; /* added to avoid compiler warning */
408
58.1k
    }
409
58.1k
  }
410
411
  /* Static parameters */
412
413
98.6k
  pSpatialSpecificConfig->samplingFreq =
414
98.6k
      samplingRate; /* wrong for stereoConfigIndex == 3 but value is unused */
415
98.6k
  pSpatialSpecificConfig->treeConfig = SPATIALDEC_MODE_RSVD7;
416
98.6k
  pSpatialSpecificConfig->nOttBoxes =
417
98.6k
      treePropertyTable[pSpatialSpecificConfig->treeConfig].numOttBoxes;
418
98.6k
  pSpatialSpecificConfig->nInputChannels =
419
98.6k
      treePropertyTable[pSpatialSpecificConfig->treeConfig].numInputChannels;
420
98.6k
  pSpatialSpecificConfig->nOutputChannels =
421
98.6k
      treePropertyTable[pSpatialSpecificConfig->treeConfig].numOutputChannels;
422
423
98.6k
  pSpatialSpecificConfig->bArbitraryDownmix = 0;
424
425
197k
  for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
426
98.6k
    pSpatialSpecificConfig->OttConfig[i].nOttBands = 0;
427
98.6k
  }
428
429
98.6k
  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
98.6k
  pSpatialSpecificConfig->coreCodec = coreCodec;
434
435
  /* Derive additional helper variables */
436
98.6k
  SpatialDecDecodeHelperInfo(pSpatialSpecificConfig, UPMIXTYPE_NORMAL);
437
438
98.6k
  return MPS_OK;
439
98.6k
}
440
441
SACDEC_ERROR SpatialDecParseSpecificConfig(
442
    HANDLE_FDK_BITSTREAM bitstream,
443
    SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig, int sacHeaderLen,
444
84.1k
    AUDIO_OBJECT_TYPE coreCodec) {
445
84.1k
  SACDEC_ERROR err = MPS_OK;
446
84.1k
  int i;
447
84.1k
  int bsSamplingFreqIndex;
448
84.1k
  int bsFreqRes, b3DaudioMode = 0;
449
84.1k
  int numHeaderBits;
450
84.1k
  int cfgStartPos, bitsAvailable;
451
452
84.1k
  FDKmemclear(pSpatialSpecificConfig, sizeof(SPATIAL_SPECIFIC_CONFIG));
453
454
84.1k
  cfgStartPos = FDKgetValidBits(bitstream);
455
  /* It might be that we do not know the SSC length beforehand. */
456
84.1k
  if (sacHeaderLen == 0) {
457
5.20k
    bitsAvailable = cfgStartPos;
458
78.9k
  } else {
459
78.9k
    bitsAvailable = 8 * sacHeaderLen;
460
78.9k
    if (bitsAvailable > cfgStartPos) {
461
349
      err = MPS_PARSE_ERROR;
462
349
      goto bail;
463
349
    }
464
78.9k
  }
465
466
83.7k
  bsSamplingFreqIndex = FDKreadBits(bitstream, 4);
467
468
83.7k
  if (bsSamplingFreqIndex == 15) {
469
3.20k
    pSpatialSpecificConfig->samplingFreq = FDKreadBits(bitstream, 24);
470
80.5k
  } else {
471
80.5k
    pSpatialSpecificConfig->samplingFreq =
472
80.5k
        samplingFreqTable[bsSamplingFreqIndex];
473
80.5k
    if (pSpatialSpecificConfig->samplingFreq == 0) {
474
2.08k
      err = MPS_PARSE_ERROR;
475
2.08k
      goto bail;
476
2.08k
    }
477
80.5k
  }
478
479
81.7k
  pSpatialSpecificConfig->nTimeSlots = FDKreadBits(bitstream, 5) + 1;
480
81.7k
  if ((pSpatialSpecificConfig->nTimeSlots < 1) ||
481
81.7k
      (pSpatialSpecificConfig->nTimeSlots > MAX_TIME_SLOTS)) {
482
0
    err = MPS_PARSE_ERROR;
483
0
    goto bail;
484
0
  }
485
486
81.7k
  bsFreqRes = FDKreadBits(bitstream, 3);
487
488
81.7k
  pSpatialSpecificConfig->freqRes =
489
81.7k
      (SPATIALDEC_FREQ_RES)freqResTable_LD[bsFreqRes];
490
491
81.7k
  {
492
81.7k
    UINT treeConfig = FDKreadBits(bitstream, 4);
493
494
81.7k
    switch (treeConfig) {
495
37.4k
      case SPATIALDEC_MODE_RSVD7:
496
37.4k
        pSpatialSpecificConfig->treeConfig = (SPATIALDEC_TREE_CONFIG)treeConfig;
497
37.4k
        break;
498
44.2k
      default:
499
44.2k
        err = MPS_UNSUPPORTED_CONFIG;
500
44.2k
        goto bail;
501
81.7k
    }
502
81.7k
  }
503
504
37.4k
  {
505
37.4k
    pSpatialSpecificConfig->nOttBoxes =
506
37.4k
        treePropertyTable[pSpatialSpecificConfig->treeConfig].numOttBoxes;
507
37.4k
    pSpatialSpecificConfig->nTttBoxes =
508
37.4k
        treePropertyTable[pSpatialSpecificConfig->treeConfig].numTttBoxes;
509
37.4k
    pSpatialSpecificConfig->nInputChannels =
510
37.4k
        treePropertyTable[pSpatialSpecificConfig->treeConfig].numInputChannels;
511
37.4k
    pSpatialSpecificConfig->nOutputChannels =
512
37.4k
        treePropertyTable[pSpatialSpecificConfig->treeConfig].numOutputChannels;
513
37.4k
  }
514
515
37.4k
  pSpatialSpecificConfig->quantMode =
516
37.4k
      (SPATIALDEC_QUANT_MODE)FDKreadBits(bitstream, 2);
517
518
37.4k
  pSpatialSpecificConfig->bArbitraryDownmix = FDKreadBits(bitstream, 1);
519
520
37.4k
  pSpatialSpecificConfig->bsFixedGainDMX =
521
37.4k
      (SPATIALDEC_FIXED_GAINS)FDKreadBits(bitstream, 3);
522
523
37.4k
  pSpatialSpecificConfig->tempShapeConfig =
524
37.4k
      (SPATIALDEC_TS_CONF)FDKreadBits(bitstream, 2);
525
37.4k
  if (pSpatialSpecificConfig->tempShapeConfig > 2) {
526
1.47k
    return MPS_PARSE_ERROR; /* reserved value */
527
1.47k
  }
528
529
35.9k
  pSpatialSpecificConfig->decorrConfig =
530
35.9k
      (SPATIALDEC_DECORR_CONF)FDKreadBits(bitstream, 2);
531
35.9k
  if (pSpatialSpecificConfig->decorrConfig > 2) {
532
335
    return MPS_PARSE_ERROR; /* reserved value */
533
335
  }
534
535
71.3k
  for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
536
35.6k
    pSpatialSpecificConfig->OttConfig[i].nOttBands = 0;
537
35.6k
  }
538
539
35.6k
  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
35.6k
  if (pSpatialSpecificConfig->tempShapeConfig == 2) {
549
15.7k
    pSpatialSpecificConfig->envQuantMode = FDKreadBits(bitstream, 1);
550
15.7k
  }
551
552
35.6k
  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
35.6k
  FDKbyteAlign(bitstream,
578
35.6k
               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
35.6k
  numHeaderBits = cfgStartPos - (INT)FDKgetValidBits(bitstream);
583
35.6k
  bitsAvailable -= numHeaderBits;
584
35.6k
  if (bitsAvailable < 0) {
585
222
    err = MPS_PARSE_ERROR;
586
222
    goto bail;
587
222
  }
588
589
35.4k
  pSpatialSpecificConfig->sacExtCnt = 0;
590
35.4k
  pSpatialSpecificConfig->bResidualCoding = 0;
591
592
35.4k
  err = SpatialDecParseExtensionConfig(
593
35.4k
      bitstream, pSpatialSpecificConfig, pSpatialSpecificConfig->nOttBoxes,
594
35.4k
      pSpatialSpecificConfig->nTttBoxes,
595
35.4k
      pSpatialSpecificConfig->nOutputChannels, bitsAvailable);
596
597
35.4k
  FDKbyteAlign(
598
35.4k
      bitstream,
599
35.4k
      cfgStartPos); /* Same alignment anchor as above because
600
                       SpatialExtensionConfig() always reads full bytes */
601
602
35.4k
  pSpatialSpecificConfig->coreCodec = coreCodec;
603
604
35.4k
  SpatialDecDecodeHelperInfo(pSpatialSpecificConfig, UPMIXTYPE_NORMAL);
605
606
82.3k
bail:
607
82.3k
  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
77.2k
    FDKpushBiDirectional(
611
77.2k
        bitstream,
612
77.2k
        (sacHeaderLen * 8) - (cfgStartPos - (INT)FDKgetValidBits(bitstream)));
613
77.2k
  }
614
615
82.3k
  return err;
616
35.4k
}
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
1.47k
{
624
1.47k
  int err = MPS_OK;
625
1.47k
  int i;
626
627
1.47k
  FDK_ASSERT(coreCodec != AOT_NONE);
628
1.47k
  FDK_ASSERT(nTimeSlots > 0);
629
1.47k
  FDK_ASSERT(samplingFreq > 0);
630
631
1.47k
  pSpatialSpecificConfig->coreCodec = coreCodec;
632
1.47k
  pSpatialSpecificConfig->samplingFreq = samplingFreq;
633
1.47k
  pSpatialSpecificConfig->nTimeSlots = nTimeSlots;
634
1.47k
  if ((pSpatialSpecificConfig->coreCodec == AOT_ER_AAC_ELD) ||
635
1.47k
      (pSpatialSpecificConfig->coreCodec == AOT_ER_AAC_LD))
636
1.47k
    pSpatialSpecificConfig->freqRes = SPATIALDEC_FREQ_RES_23;
637
0
  else
638
0
    pSpatialSpecificConfig->freqRes = SPATIALDEC_FREQ_RES_28;
639
640
1.47k
  {
641
1.47k
    pSpatialSpecificConfig->treeConfig =
642
1.47k
        SPATIALDEC_MODE_RSVD7; /* 212  configuration */
643
1.47k
  }
644
645
1.47k
  {
646
1.47k
    pSpatialSpecificConfig->nOttBoxes =
647
1.47k
        treePropertyTable[pSpatialSpecificConfig->treeConfig].numOttBoxes;
648
1.47k
    pSpatialSpecificConfig->nInputChannels =
649
1.47k
        treePropertyTable[pSpatialSpecificConfig->treeConfig].numInputChannels;
650
1.47k
    pSpatialSpecificConfig->nOutputChannels =
651
1.47k
        treePropertyTable[pSpatialSpecificConfig->treeConfig].numOutputChannels;
652
1.47k
  }
653
654
1.47k
  pSpatialSpecificConfig->quantMode = SPATIALDEC_QUANT_FINE_DEF;
655
1.47k
  pSpatialSpecificConfig->bArbitraryDownmix = 0;
656
1.47k
  pSpatialSpecificConfig->bResidualCoding = 0;
657
1.47k
  if ((pSpatialSpecificConfig->coreCodec == AOT_ER_AAC_ELD) ||
658
1.47k
      (pSpatialSpecificConfig->coreCodec == AOT_ER_AAC_LD))
659
1.47k
    pSpatialSpecificConfig->bsFixedGainDMX = SPATIALDEC_GAIN_RSVD2;
660
0
  else
661
0
    pSpatialSpecificConfig->bsFixedGainDMX = SPATIALDEC_GAIN_MODE0;
662
663
1.47k
  pSpatialSpecificConfig->tempShapeConfig = SPATIALDEC_TS_TPNOWHITE;
664
1.47k
  pSpatialSpecificConfig->decorrConfig = SPATIALDEC_DECORR_MODE0;
665
666
2.95k
  for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
667
1.47k
    pSpatialSpecificConfig->OttConfig[i].nOttBands = 0;
668
1.47k
  }
669
670
1.47k
  return err;
671
1.47k
}
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
55.7k
                        int numBands) {
689
55.7k
  int i;
690
691
597k
  for (i = startBand; i < startBand + numBands; i++) {
692
541k
    data[i] <<= 1;
693
541k
  }
694
695
55.7k
  if (dataType == t_CLD) {
696
257k
    for (i = startBand; i < startBand + numBands; i++) {
697
234k
      if (data[i] == -14)
698
16.1k
        data[i] = -15;
699
217k
      else if (data[i] == 14)
700
38.3k
        data[i] = 15;
701
234k
    }
702
23.5k
  }
703
55.7k
}
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
14.4k
                        int numBands) {
721
14.4k
  int i;
722
723
160k
  for (i = startBand; i < startBand + numBands; i++) {
724
    /* Note: the if cases below actually make a difference (negative values) */
725
145k
    if (dataType == t_CLD)
726
75.6k
      data[i] /= 2;
727
70.3k
    else
728
70.3k
      data[i] >>= 1;
729
145k
  }
730
14.4k
}
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
36.6k
                        int *aStrides) {
748
36.6k
  int i, pb, pbStride, dataBands, strOffset;
749
750
36.6k
  pbStride = pbStrideTable[freqResStride];
751
36.6k
  dataBands = (stopBand - startBand - 1) / pbStride + 1;
752
753
36.6k
  aStrides[0] = startBand;
754
210k
  for (pb = 1; pb <= dataBands; pb++) {
755
173k
    aStrides[pb] = aStrides[pb - 1] + pbStride;
756
173k
  }
757
36.6k
  strOffset = 0;
758
150k
  while (aStrides[dataBands] > stopBand) {
759
114k
    if (strOffset < dataBands) strOffset++;
760
234k
    for (i = strOffset; i <= dataBands; i++) {
761
120k
      aStrides[i]--;
762
120k
    }
763
114k
  }
764
765
36.6k
  return dataBands;
766
36.6k
}
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
225k
    int datatype, int boxIdx, int startBand, int stopBand, SCHAR defaultValue) {
789
225k
  SACDEC_ERROR err = MPS_OK;
790
225k
  int i, j, pb, dataSets, setIdx, bsDataPair, dataBands, oldQuantCoarseXXX;
791
225k
  INT aStrides[MAX_PARAMETER_BANDS + 1] = {0};
792
793
225k
  dataSets = 0;
794
548k
  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
325k
        (llData->bsXXXDataMode[i] == 1 ||
799
188k
         llData->bsXXXDataMode[i] == 2)) { /* This check catches bitstreams
800
                                              generated by older encoder that
801
                                              cause trouble */
802
138
      return MPS_PARSE_ERROR;
803
138
    }
804
325k
    if ((i >= frame->numParameterSets - 1) &&
805
325k
        (llData->bsXXXDataMode[i] ==
806
225k
         2)) { /* The interpolation mode must not be active for the last
807
                  parameter set */
808
1.48k
      return MPS_PARSE_ERROR;
809
1.48k
    }
810
811
323k
    if (llData->bsXXXDataMode[i] == 3) {
812
54.2k
      dataSets++;
813
54.2k
    }
814
323k
  }
815
816
223k
  setIdx = 0;
817
223k
  bsDataPair = 0;
818
223k
  oldQuantCoarseXXX = llData->state->bsQuantCoarseXXXprevParse;
819
820
546k
  for (i = 0; i < frame->numParameterSets; i++) {
821
323k
    if (llData->bsXXXDataMode[i] == 0) {
822
2.28M
      for (pb = startBand; pb < stopBand; pb++) {
823
2.05M
        lastdata[boxIdx][pb] = defaultValue;
824
2.05M
      }
825
826
234k
      oldQuantCoarseXXX = 0;
827
234k
    }
828
829
323k
    if (llData->bsXXXDataMode[i] == 3) {
830
54.0k
      if (bsDataPair) {
831
17.4k
        bsDataPair = 0;
832
36.6k
      } else {
833
36.6k
        bsDataPair = FDKreadBits(bitstream, 1);
834
36.6k
        llData->bsQuantCoarseXXX[setIdx] = (UCHAR)FDKreadBits(bitstream, 1);
835
36.6k
        llData->bsFreqResStrideXXX[setIdx] = (UCHAR)FDKreadBits(bitstream, 2);
836
837
36.6k
        if (llData->bsQuantCoarseXXX[setIdx] != oldQuantCoarseXXX) {
838
18.7k
          if (oldQuantCoarseXXX) {
839
4.31k
            coarse2fine(lastdata[boxIdx], (DATA_TYPE)datatype, startBand,
840
4.31k
                        stopBand - startBand);
841
14.4k
          } else {
842
14.4k
            fine2coarse(lastdata[boxIdx], (DATA_TYPE)datatype, startBand,
843
14.4k
                        stopBand - startBand);
844
14.4k
          }
845
18.7k
        }
846
847
36.6k
        dataBands = getStrideMap(llData->bsFreqResStrideXXX[setIdx], startBand,
848
36.6k
                                 stopBand, aStrides);
849
850
210k
        for (pb = 0; pb < dataBands; pb++) {
851
173k
          lastdata[boxIdx][startBand + pb] = lastdata[boxIdx][aStrides[pb]];
852
173k
        }
853
854
36.6k
        if (boxIdx > MAX_NUM_OTT) return MPS_INVALID_BOXIDX;
855
36.6k
        if ((setIdx + bsDataPair) > MAX_PARAMETER_SETS)
856
0
          return MPS_INVALID_SETIDX;
857
858
        /* DECODER_TYPE defined in FDK_tools */
859
36.6k
        DECODER_TYPE this_decoder_type = SAC_DECODER;
860
36.6k
        if (syntaxFlags & (SACDEC_SYNTAX_USAC | SACDEC_SYNTAX_RSVD50)) {
861
33.8k
          this_decoder_type = USAC_DECODER;
862
33.8k
        } else if (syntaxFlags & SACDEC_SYNTAX_LD) {
863
2.73k
          this_decoder_type = SAOC_DECODER;
864
2.73k
        }
865
866
36.6k
        err = (SACDEC_ERROR)EcDataPairDec(
867
36.6k
            this_decoder_type, bitstream, data[boxIdx][setIdx + 0],
868
36.6k
            data[boxIdx][setIdx + 1], lastdata[boxIdx], (DATA_TYPE)datatype,
869
36.6k
            startBand, dataBands, bsDataPair, llData->bsQuantCoarseXXX[setIdx],
870
36.6k
            !(frame->bsIndependencyFlag && (i == 0)) || (setIdx > 0));
871
36.6k
        if (err != MPS_OK) goto bail;
872
873
36.1k
        if (datatype == t_IPD) {
874
6.18k
          const SCHAR mask = (llData->bsQuantCoarseXXX[setIdx]) ? 7 : 15;
875
37.0k
          for (pb = 0; pb < dataBands; pb++) {
876
84.6k
            for (j = aStrides[pb]; j < aStrides[pb + 1]; j++) {
877
53.8k
              lastdata[boxIdx][j] =
878
53.8k
                  data[boxIdx][setIdx + bsDataPair][startBand + pb] & mask;
879
53.8k
            }
880
30.8k
          }
881
29.9k
        } else {
882
169k
          for (pb = 0; pb < dataBands; pb++) {
883
444k
            for (j = aStrides[pb]; j < aStrides[pb + 1]; j++) {
884
304k
              lastdata[boxIdx][j] =
885
304k
                  data[boxIdx][setIdx + bsDataPair][startBand + pb];
886
304k
            }
887
139k
          }
888
29.9k
        }
889
890
36.1k
        oldQuantCoarseXXX = llData->bsQuantCoarseXXX[setIdx];
891
892
36.1k
        if (bsDataPair) {
893
21.8k
          llData->bsQuantCoarseXXX[setIdx + 1] =
894
21.8k
              llData->bsQuantCoarseXXX[setIdx];
895
21.8k
          llData->bsFreqResStrideXXX[setIdx + 1] =
896
21.8k
              llData->bsFreqResStrideXXX[setIdx];
897
21.8k
        }
898
36.1k
        setIdx += bsDataPair + 1;
899
36.1k
      } /* !bsDataPair */
900
54.0k
    }   /* llData->bsXXXDataMode[i] == 3 */
901
323k
  }
902
903
223k
  llData->state->bsQuantCoarseXXXprevParse = oldQuantCoarseXXX;
904
905
223k
bail:
906
223k
  return err;
907
223k
}
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
2.24k
    HANDLE_FDK_BITSTREAM bitstream) {
924
2.24k
  SACDEC_ERROR err = MPS_OK;
925
2.24k
  int ch;
926
2.24k
  int offset = pSSC->nOttBoxes;
927
928
  /* CLD (arbitrary down-mix gains) */
929
4.22k
  for (ch = 0; ch < pSSC->nInputChannels; ch++) {
930
2.24k
    err = ecDataDec(frame, syntaxFlags, bitstream,
931
2.24k
                    &frame->CLDLosslessData[offset + ch],
932
2.24k
                    frame->cmpArbdmxGainIdx, self->cmpArbdmxGainIdxPrev, t_CLD,
933
2.24k
                    ch, 0, pSSC->freqRes, arbdmxGainDefault);
934
2.24k
    if (err != MPS_OK) return err;
935
2.24k
  }
936
937
1.97k
  return err;
938
939
2.24k
} /* parseArbitraryDownmixData */
940
941
/*******************************************************************************
942
 Functionname: SpatialDecParseFrame
943
 *******************************************************************************
944
945
 Description:
946
947
 Arguments:
948
949
 Input:
950
951
 Output:
952
953
*******************************************************************************/
954
955
3.79k
static int nBitsParamSlot(int i) {
956
3.79k
  int bitsParamSlot;
957
958
3.79k
  bitsParamSlot = fMax(0, DFRACT_BITS - 1 - fNormz((FIXP_DBL)i));
959
3.79k
  if ((1 << bitsParamSlot) < i) {
960
16
    bitsParamSlot++;
961
16
  }
962
3.79k
  FDK_ASSERT((bitsParamSlot >= 0) && (bitsParamSlot <= 32));
963
964
3.79k
  return bitsParamSlot;
965
3.79k
}
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
111k
    int fGlobalIndependencyFlag) {
972
111k
  SACDEC_ERROR err = MPS_OK;
973
111k
  int bsFramingType, dataBands, ps, pg, i;
974
111k
  int pb;
975
111k
  int numTempShapeChan = 0;
976
111k
  int bsNumOutputChannels =
977
111k
      treePropertyTable[pSpatialSpecificConfig->treeConfig]
978
111k
          .numOutputChannels; /* CAUTION: Maybe different to
979
                                 pSpatialSpecificConfig->treeConfig in some
980
                                 modes! */
981
111k
  int paramSetErr = 0;
982
111k
  UINT alignAnchor = FDKgetValidBits(
983
111k
      bitstream); /* Anchor for ByteAlign() function. See comment below. */
984
111k
  UINT syntaxFlags;
985
986
111k
  syntaxFlags = pSpatialSpecificConfig->syntaxFlags;
987
988
111k
  if ((syntaxFlags & (SACDEC_SYNTAX_USAC | SACDEC_SYNTAX_RSVD50)) &&
989
111k
      pSpatialSpecificConfig->bsHighRateMode == 0) {
990
20.4k
    bsFramingType = 0; /* fixed framing */
991
20.4k
    frame->numParameterSets = 1;
992
90.8k
  } else {
993
90.8k
    bsFramingType = FDKreadBits(bitstream, 1);
994
90.8k
    if (syntaxFlags & SACDEC_SYNTAX_LD)
995
4.01k
      frame->numParameterSets = FDKreadBits(bitstream, 1) + 1;
996
86.8k
    else
997
86.8k
      frame->numParameterSets = FDKreadBits(bitstream, 3) + 1;
998
90.8k
  }
999
1000
  /* Any error after this line shall trigger parameter invalidation at bail
1001
   * label. */
1002
111k
  paramSetErr = 1;
1003
1004
111k
  if (frame->numParameterSets >= MAX_PARAMETER_SETS) {
1005
0
    goto bail;
1006
0
  }
1007
1008
  /* Basic config check. */
1009
111k
  if (pSpatialSpecificConfig->nInputChannels <= 0 ||
1010
111k
      pSpatialSpecificConfig->nOutputChannels <= 0) {
1011
0
    err = MPS_UNSUPPORTED_CONFIG;
1012
0
    goto bail;
1013
0
  }
1014
1015
111k
  if (bsFramingType) {
1016
3.79k
    int prevParamSlot = -1;
1017
3.79k
    int bitsParamSlot;
1018
1019
3.79k
    {
1020
3.79k
      bitsParamSlot = nBitsParamSlot(pSpatialSpecificConfig->nTimeSlots);
1021
1022
9.37k
      for (i = 0; i < frame->numParameterSets; i++) {
1023
5.63k
        frame->paramSlot[i] = FDKreadBits(bitstream, bitsParamSlot);
1024
        /* Sanity check */
1025
5.63k
        if ((frame->paramSlot[i] <= prevParamSlot) ||
1026
5.63k
            (frame->paramSlot[i] >= pSpatialSpecificConfig->nTimeSlots)) {
1027
57
          err = MPS_PARSE_ERROR;
1028
57
          goto bail;
1029
57
        }
1030
5.58k
        prevParamSlot = frame->paramSlot[i];
1031
5.58k
      }
1032
3.79k
    }
1033
107k
  } else {
1034
257k
    for (i = 0; i < frame->numParameterSets; i++) {
1035
150k
      frame->paramSlot[i] = ((pSpatialSpecificConfig->nTimeSlots * (i + 1)) /
1036
150k
                             frame->numParameterSets) -
1037
150k
                            1;
1038
150k
    }
1039
107k
  }
1040
1041
111k
  if ((syntaxFlags & (SACDEC_SYNTAX_USAC | SACDEC_SYNTAX_RSVD50)) &&
1042
111k
      fGlobalIndependencyFlag) {
1043
92.2k
    frame->bsIndependencyFlag = 1;
1044
92.2k
  } else {
1045
18.9k
    frame->bsIndependencyFlag = (UCHAR)FDKreadBits(bitstream, 1);
1046
18.9k
  }
1047
1048
  /*
1049
   * OttData()
1050
   */
1051
221k
  for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
1052
111k
    err = ecDataDec(frame, syntaxFlags, bitstream, &frame->CLDLosslessData[i],
1053
111k
                    frame->cmpOttCLDidx, self->cmpOttCLDidxPrev, t_CLD, i, 0,
1054
111k
                    pSpatialSpecificConfig->bitstreamOttBands[i],
1055
111k
                    pSpatialSpecificConfig->ottCLDdefault[i]);
1056
111k
    if (err != MPS_OK) {
1057
1.29k
      goto bail;
1058
1.29k
    }
1059
111k
  } /* i < numOttBoxes */
1060
1061
109k
  {
1062
219k
    for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
1063
109k
      err = ecDataDec(frame, syntaxFlags, bitstream, &frame->ICCLosslessData[i],
1064
109k
                      frame->cmpOttICCidx, self->cmpOttICCidxPrev, t_ICC, i, 0,
1065
109k
                      pSpatialSpecificConfig->bitstreamOttBands[i], ICCdefault);
1066
109k
      if (err != MPS_OK) {
1067
500
        goto bail;
1068
500
      }
1069
109k
    } /* i < numOttBoxes */
1070
109k
  }   /* !oneICC */
1071
1072
109k
  if ((pSpatialSpecificConfig->treeConfig == SPATIALDEC_MODE_RSVD7) &&
1073
109k
      (pSpatialSpecificConfig->bsPhaseCoding)) {
1074
8.28k
    frame->phaseMode = FDKreadBits(bitstream, 1);
1075
1076
8.28k
    if (frame->phaseMode == 0) {
1077
49.7k
      for (pb = 0; pb < pSpatialSpecificConfig->numOttBandsIPD; pb++) {
1078
43.4k
        self->cmpOttIPDidxPrev[0][pb] = 0;
1079
311k
        for (i = 0; i < frame->numParameterSets; i++) {
1080
268k
          frame->cmpOttIPDidx[0][i][pb] = 0;
1081
          // frame->ottIPDidx[0][i][pb] = 0;
1082
268k
        }
1083
        /* self->ottIPDidxPrev[0][pb] = 0; */
1084
43.4k
      }
1085
6.36k
      frame->OpdSmoothingMode = 0;
1086
6.36k
    } else {
1087
1.92k
      frame->OpdSmoothingMode = FDKreadBits(bitstream, 1);
1088
1.92k
      err = ecDataDec(frame, syntaxFlags, bitstream, &frame->IPDLosslessData[0],
1089
1.92k
                      frame->cmpOttIPDidx, self->cmpOttIPDidxPrev, t_IPD, 0, 0,
1090
1.92k
                      pSpatialSpecificConfig->numOttBandsIPD, IPDdefault);
1091
1.92k
      if (err != MPS_OK) {
1092
34
        goto bail;
1093
34
      }
1094
1.92k
    }
1095
8.28k
  }
1096
1097
  /*
1098
   * SmgData()
1099
   */
1100
1101
109k
  {
1102
109k
    if (!pSpatialSpecificConfig->bsHighRateMode &&
1103
109k
        (syntaxFlags & SACDEC_SYNTAX_USAC)) {
1104
40.7k
      for (ps = 0; ps < frame->numParameterSets; ps++) {
1105
20.3k
        frame->bsSmoothMode[ps] = 0;
1106
20.3k
      }
1107
89.0k
    } else {
1108
221k
      for (ps = 0; ps < frame->numParameterSets; ps++) {
1109
132k
        frame->bsSmoothMode[ps] = (UCHAR)FDKreadBits(bitstream, 2);
1110
132k
        if (frame->bsSmoothMode[ps] >= 2) {
1111
12.9k
          frame->bsSmoothTime[ps] = (UCHAR)FDKreadBits(bitstream, 2);
1112
12.9k
        }
1113
132k
        if (frame->bsSmoothMode[ps] == 3) {
1114
7.20k
          frame->bsFreqResStrideSmg[ps] = (UCHAR)FDKreadBits(bitstream, 2);
1115
7.20k
          dataBands = (pSpatialSpecificConfig->freqRes - 1) /
1116
7.20k
                          pbStrideTable[frame->bsFreqResStrideSmg[ps]] +
1117
7.20k
                      1;
1118
33.6k
          for (pg = 0; pg < dataBands; pg++) {
1119
26.4k
            frame->bsSmgData[ps][pg] = (UCHAR)FDKreadBits(bitstream, 1);
1120
26.4k
          }
1121
7.20k
        }
1122
132k
      } /* ps < numParameterSets */
1123
89.0k
    }
1124
109k
  }
1125
1126
  /*
1127
   * TempShapeData()
1128
   */
1129
109k
  if ((pSpatialSpecificConfig->tempShapeConfig == 3) &&
1130
109k
      (syntaxFlags & SACDEC_SYNTAX_USAC)) {
1131
15.2k
    int TsdErr;
1132
15.2k
    TsdErr = TsdRead(bitstream, pSpatialSpecificConfig->nTimeSlots,
1133
15.2k
                     &frame->TsdData[0]);
1134
15.2k
    if (TsdErr) {
1135
0
      err = MPS_PARSE_ERROR;
1136
0
      goto bail;
1137
0
    }
1138
94.1k
  } else {
1139
94.1k
    frame->TsdData[0].bsTsdEnable = 0;
1140
94.1k
  }
1141
1142
328k
  for (i = 0; i < bsNumOutputChannels; i++) {
1143
218k
    frame->tempShapeEnableChannelSTP[i] = 0;
1144
218k
    frame->tempShapeEnableChannelGES[i] = 0;
1145
218k
  }
1146
1147
109k
  if ((pSpatialSpecificConfig->tempShapeConfig == 1) ||
1148
109k
      (pSpatialSpecificConfig->tempShapeConfig == 2)) {
1149
91.0k
    int bsTempShapeEnable = FDKreadBits(bitstream, 1);
1150
91.0k
    if (bsTempShapeEnable) {
1151
5.38k
      numTempShapeChan =
1152
5.38k
          tempShapeChanTable[pSpatialSpecificConfig->tempShapeConfig - 1]
1153
5.38k
                            [pSpatialSpecificConfig->treeConfig];
1154
5.38k
      switch (pSpatialSpecificConfig->tempShapeConfig) {
1155
1.65k
        case 1: /* STP */
1156
4.95k
          for (i = 0; i < numTempShapeChan; i++) {
1157
3.30k
            int stpEnable = FDKreadBits(bitstream, 1);
1158
3.30k
            frame->tempShapeEnableChannelSTP[i] = stpEnable;
1159
3.30k
          }
1160
1.65k
          break;
1161
3.73k
        case 2: /* GES */
1162
3.73k
        {
1163
3.73k
          UCHAR gesChannelEnable[MAX_OUTPUT_CHANNELS];
1164
1165
11.1k
          for (i = 0; i < numTempShapeChan; i++) {
1166
7.46k
            gesChannelEnable[i] = (UCHAR)FDKreadBits(bitstream, 1);
1167
7.46k
            frame->tempShapeEnableChannelGES[i] = gesChannelEnable[i];
1168
7.46k
          }
1169
11.1k
          for (i = 0; i < numTempShapeChan; i++) {
1170
7.43k
            if (gesChannelEnable[i]) {
1171
3.69k
              int envShapeData_tmp[MAX_TIME_SLOTS];
1172
3.69k
              if (huff_dec_reshape(bitstream, envShapeData_tmp,
1173
3.69k
                                   pSpatialSpecificConfig->nTimeSlots) != 0) {
1174
69
                err = MPS_PARSE_ERROR;
1175
69
                goto bail;
1176
69
              }
1177
118k
              for (int ts = 0; ts < pSpatialSpecificConfig->nTimeSlots; ts++) {
1178
115k
                if (!(envShapeData_tmp[ts] >= 0) &&
1179
115k
                    (envShapeData_tmp[ts] <= 4)) {
1180
0
                  err = MPS_PARSE_ERROR;
1181
0
                  goto bail;
1182
0
                }
1183
115k
                frame->bsEnvShapeData[i][ts] = (UCHAR)envShapeData_tmp[ts];
1184
115k
              }
1185
3.62k
            }
1186
7.43k
          }
1187
3.73k
        } break;
1188
3.66k
        default:
1189
0
          err = MPS_INVALID_TEMPSHAPE;
1190
0
          goto bail;
1191
5.38k
      }
1192
5.38k
    } /* bsTempShapeEnable */
1193
91.0k
  }   /* pSpatialSpecificConfig->tempShapeConfig != 0 */
1194
1195
109k
  if (pSpatialSpecificConfig->bArbitraryDownmix != 0) {
1196
2.24k
    err = parseArbitraryDownmixData(self, pSpatialSpecificConfig, syntaxFlags,
1197
2.24k
                                    frame, bitstream);
1198
2.24k
    if (err != MPS_OK) goto bail;
1199
2.24k
  }
1200
1201
109k
  if (1 && (!(syntaxFlags & (SACDEC_SYNTAX_USAC)))) {
1202
2.01k
    FDKbyteAlign(bitstream,
1203
2.01k
                 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.01k
  }
1207
1208
111k
bail:
1209
111k
  if (err != MPS_OK && paramSetErr != 0) {
1210
    /* Since the parameter set data has already been written to the instance we
1211
     * need to ... */
1212
2.21k
    frame->numParameterSets = 0; /* ... signal that it is corrupt ... */
1213
2.21k
  }
1214
1215
111k
  return err;
1216
1217
109k
} /* 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
70.1k
                          int stopBand, int stride) {
1232
70.1k
  int inBands, outBands, bandsAchived, bandsDiff, incr, k, i;
1233
70.1k
  int vDk[MAX_PARAMETER_BANDS + 1];
1234
70.1k
  inBands = stopBand - startBand;
1235
70.1k
  outBands = (inBands - 1) / stride + 1;
1236
1237
70.1k
  if (outBands < 1) {
1238
1.32k
    outBands = 1;
1239
1.32k
  }
1240
1241
70.1k
  bandsAchived = outBands * stride;
1242
70.1k
  bandsDiff = inBands - bandsAchived;
1243
337k
  for (i = 0; i < outBands; i++) {
1244
267k
    vDk[i] = stride;
1245
267k
  }
1246
1247
70.1k
  if (bandsDiff > 0) {
1248
0
    incr = -1;
1249
0
    k = outBands - 1;
1250
70.1k
  } else {
1251
70.1k
    incr = 1;
1252
70.1k
    k = 0;
1253
70.1k
  }
1254
1255
287k
  while (bandsDiff != 0) {
1256
217k
    vDk[k] = vDk[k] - incr;
1257
217k
    k = k + incr;
1258
217k
    bandsDiff = bandsDiff + incr;
1259
217k
    if (k >= outBands) {
1260
213k
      if (bandsDiff > 0) {
1261
0
        k = outBands - 1;
1262
213k
      } else if (bandsDiff < 0) {
1263
200k
        k = 0;
1264
200k
      }
1265
213k
    }
1266
217k
  }
1267
70.1k
  aMap[0] = startBand;
1268
337k
  for (i = 0; i < outBands; i++) {
1269
267k
    aMap[i + 1] = aMap[i] + vDk[i];
1270
267k
  }
1271
70.1k
} /* 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
63.5k
{
1289
63.5k
  int i, j;
1290
63.5k
  int startBand0 = pMap[0];
1291
1292
308k
  for (i = 0; i < dataBands; i++) {
1293
244k
    int startBand, stopBand, value;
1294
1295
244k
    value = pInput[i + startBand0];
1296
1297
244k
    startBand = pMap[i];
1298
244k
    stopBand = pMap[i + 1];
1299
855k
    for (j = startBand; j < stopBand; j++) {
1300
611k
      pOutput[j] = value;
1301
611k
    }
1302
244k
  }
1303
63.5k
}
1304
1305
/*******************************************************************************
1306
 Functionname: deq
1307
 *******************************************************************************
1308
1309
 Description:
1310
1311
 Arguments:
1312
1313
 Return:
1314
1315
*******************************************************************************/
1316
4.01M
static int deqIdx(int value, int paramType) {
1317
4.01M
  int idx = -1;
1318
1319
4.01M
  switch (paramType) {
1320
1.90M
    case t_CLD:
1321
1.90M
      if (((value + 15) >= 0) && ((value + 15) < 31)) {
1322
1.84M
        idx = (value + 15);
1323
1.84M
      }
1324
1.90M
      break;
1325
1326
1.71M
    case t_ICC:
1327
1.71M
      if ((value >= 0) && (value < 8)) {
1328
1.62M
        idx = value;
1329
1.62M
      }
1330
1.71M
      break;
1331
1332
386k
    case t_IPD:
1333
      /* (+/-)15 * MAX_PARAMETER_BANDS for differential coding in frequency
1334
       * domain (according to rbl) */
1335
386k
      if ((value >= -420) && (value <= 420)) {
1336
386k
        idx = (value & 0xf);
1337
386k
      }
1338
386k
      break;
1339
1340
0
    default:
1341
0
      FDK_ASSERT(0);
1342
4.01M
  }
1343
1344
4.01M
  return idx;
1345
4.01M
}
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
328k
    FIXP_DBL (*pOttVsTotDb2)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS]) {
1464
328k
  int aParamSlots[MAX_PARAMETER_SETS];
1465
328k
  int aInterpolate[MAX_PARAMETER_SETS] = {0};
1466
1467
328k
  int dataSets;
1468
328k
  int aMap[MAX_PARAMETER_BANDS + 1];
1469
1470
328k
  int setIdx, i, band, parmSlot;
1471
328k
  int dataBands;
1472
328k
  int ps, pb;
1473
328k
  int i1;
1474
1475
328k
  if (numParameterSets > MAX_PARAMETER_SETS) return MPS_WRONG_PARAMETERSETS;
1476
1477
328k
  dataSets = 0;
1478
777k
  for (i = 0; i < numParameterSets; i++) {
1479
448k
    if (llData->bsXXXDataMode[i] == 3) {
1480
67.7k
      aParamSlots[dataSets] = i;
1481
67.7k
      dataSets++;
1482
67.7k
    }
1483
448k
  }
1484
1485
328k
  setIdx = 0;
1486
1487
  /* Main concealment stage is here: */
1488
328k
  SpatialDecConcealment_Apply(
1489
328k
      concealmentInfo, cmpIdxData[xttIdx],
1490
328k
      (diffIdxData != NULL) ? diffIdxData[xttIdx] : NULL, idxPrev[xttIdx],
1491
328k
      llData->bsXXXDataMode, startBand, stopBand, defaultValue, paramType,
1492
328k
      numParameterSets);
1493
1494
  /* Prepare data */
1495
777k
  for (i = 0; i < numParameterSets; i++) {
1496
448k
    if (llData->bsXXXDataMode[i] == 0) {
1497
301k
      llData->nocmpQuantCoarseXXX[i] = 0;
1498
2.84M
      for (band = startBand; band < stopBand; band++) {
1499
2.53M
        outputIdxData[xttIdx][i][band] = defaultValue;
1500
2.53M
      }
1501
2.84M
      for (band = startBand; band < stopBand; band++) {
1502
2.53M
        idxPrev[xttIdx][band] = outputIdxData[xttIdx][i][band];
1503
2.53M
      }
1504
      /* Because the idxPrev are also set to the defaultValue -> signalize fine
1505
       */
1506
301k
      llData->state->bsQuantCoarseXXXprev = 0;
1507
301k
    }
1508
1509
448k
    if (llData->bsXXXDataMode[i] == 1) {
1510
671k
      for (band = startBand; band < stopBand; band++) {
1511
614k
        outputIdxData[xttIdx][i][band] = idxPrev[xttIdx][band];
1512
614k
      }
1513
57.4k
      llData->nocmpQuantCoarseXXX[i] = llData->state->bsQuantCoarseXXXprev;
1514
57.4k
    }
1515
1516
448k
    if (llData->bsXXXDataMode[i] == 2) {
1517
275k
      for (band = startBand; band < stopBand; band++) {
1518
249k
        outputIdxData[xttIdx][i][band] = idxPrev[xttIdx][band];
1519
249k
      }
1520
25.8k
      llData->nocmpQuantCoarseXXX[i] = llData->state->bsQuantCoarseXXXprev;
1521
25.8k
      aInterpolate[i] = 1;
1522
422k
    } else {
1523
422k
      aInterpolate[i] = 0;
1524
422k
    }
1525
1526
448k
    if (llData->bsXXXDataMode[i] == 3) {
1527
63.5k
      int stride;
1528
1529
63.5k
      parmSlot = aParamSlots[setIdx];
1530
63.5k
      stride = pbStrideTable[llData->bsFreqResStrideXXX[setIdx]];
1531
63.5k
      dataBands = (stopBand - startBand - 1) / stride + 1;
1532
63.5k
      createMapping(aMap, startBand, stopBand, stride);
1533
63.5k
      mapFrequency(&cmpIdxData[xttIdx][setIdx][0],
1534
63.5k
                   &outputIdxData[xttIdx][parmSlot][0], aMap, dataBands);
1535
674k
      for (band = startBand; band < stopBand; band++) {
1536
611k
        idxPrev[xttIdx][band] = outputIdxData[xttIdx][parmSlot][band];
1537
611k
      }
1538
63.5k
      llData->state->bsQuantCoarseXXXprev = llData->bsQuantCoarseXXX[setIdx];
1539
63.5k
      llData->nocmpQuantCoarseXXX[i] = llData->bsQuantCoarseXXX[setIdx];
1540
1541
63.5k
      setIdx++;
1542
63.5k
    }
1543
448k
    if (diffIdxData != NULL) {
1544
1.90M
      for (band = startBand; band < stopBand; band++) {
1545
1.71M
        outputIdxData[xttIdx][i][band] += diffIdxData[xttIdx][i][band];
1546
1.71M
      }
1547
187k
    }
1548
448k
  } /* for( i = 0 ; i < numParameterSets; i++ ) */
1549
1550
  /* Map all coarse data to fine */
1551
777k
  for (i = 0; i < numParameterSets; i++) {
1552
448k
    if (llData->nocmpQuantCoarseXXX[i] == 1) {
1553
51.4k
      coarse2fine(outputIdxData[xttIdx][i], (DATA_TYPE)paramType, startBand,
1554
51.4k
                  stopBand - startBand);
1555
51.4k
      llData->nocmpQuantCoarseXXX[i] = 0;
1556
51.4k
    }
1557
448k
  }
1558
1559
  /* Interpolate */
1560
328k
  i1 = 0;
1561
777k
  for (i = 0; i < numParameterSets; i++) {
1562
448k
    if (aInterpolate[i] != 1) {
1563
422k
      i1 = i;
1564
422k
    } else {
1565
25.8k
      int xi, i2, x1, x2;
1566
1567
66.3k
      for (i2 = i; i2 < numParameterSets; i2++) {
1568
66.1k
        if (aInterpolate[i2] != 1) break;
1569
66.1k
      }
1570
25.8k
      if (i2 >= numParameterSets) return MPS_WRONG_PARAMETERSETS;
1571
1572
25.6k
      x1 = paramSlot[i1];
1573
25.6k
      xi = paramSlot[i];
1574
25.6k
      x2 = paramSlot[i2];
1575
1576
274k
      for (band = startBand; band < stopBand; band++) {
1577
249k
        int yi, y1, y2;
1578
249k
        y1 = outputIdxData[xttIdx][i1][band];
1579
249k
        y2 = outputIdxData[xttIdx][i2][band];
1580
249k
        if (x1 != x2) {
1581
249k
          yi = y1 + (xi - x1) * (y2 - y1) / (x2 - x1);
1582
249k
        } else {
1583
0
          yi = y1 /*+ (xi-x1)*(y2-y1)/1e-12*/;
1584
0
        }
1585
249k
        outputIdxData[xttIdx][i][band] = yi;
1586
249k
      }
1587
25.6k
    }
1588
448k
  } /* for( i = 0 ; i < numParameterSets; i++ ) */
1589
1590
  /* Dequantize data and apply factorCLD if necessary */
1591
776k
  for (ps = 0; ps < numParameterSets; ps++) {
1592
448k
    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.46M
    for (band = startBand; band < stopBand; band++) {
1609
4.01M
      outputDataIdx[xttIdx][ps][band] =
1610
4.01M
          deqIdx(outputIdxData[xttIdx][ps][band], paramType);
1611
4.01M
      if (outputDataIdx[xttIdx][ps][band] == -1) {
1612
154k
        outputDataIdx[xttIdx][ps][band] = defaultValue;
1613
154k
      }
1614
4.01M
    }
1615
448k
  } /* for( i = 0 ; i < numParameterSets; i++ ) */
1616
1617
328k
  if (extendFrame) {
1618
5.12k
    if (paramType == t_IPD) {
1619
492
      llData->bsQuantCoarseXXX[numParameterSets] =
1620
492
          llData->bsQuantCoarseXXX[numParameterSets - 1];
1621
492
    }
1622
41.3k
    for (band = startBand; band < stopBand; band++) {
1623
36.2k
      outputDataIdx[xttIdx][numParameterSets][band] =
1624
36.2k
          outputDataIdx[xttIdx][numParameterSets - 1][band];
1625
36.2k
    }
1626
5.12k
  }
1627
1628
328k
  return MPS_OK;
1629
328k
}
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
158k
                                         SPATIAL_BS_FRAME *pCurBs) {
1647
158k
  int i, ottIdx;
1648
158k
  int numOttBoxes;
1649
1650
158k
  SACDEC_ERROR err = MPS_OK;
1651
1652
158k
  numOttBoxes = self->numOttBoxes;
1653
1654
158k
  switch (self->treeConfig) {
1655
158k
    default: {
1656
158k
      if (self->quantMode != 0) {
1657
12.6k
        goto bail;
1658
12.6k
      }
1659
158k
    }
1660
291k
      for (i = 0; i < numOttBoxes; i++) {
1661
145k
        err = mapIndexData(
1662
145k
            &pCurBs->CLDLosslessData[i], /* LOSSLESSDATA *llData,*/
1663
145k
            self->ottCLD__FDK, self->outIdxData,
1664
145k
            pCurBs
1665
145k
                ->cmpOttCLDidx, /* int
1666
                                   cmpIdxData[MAX_NUM_OTT][MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS],
1667
                                 */
1668
145k
            NULL,               /* no differential data */
1669
145k
            i, /*  int   xttIdx,  Which ott/ttt index to use for input and
1670
                  output buffers */
1671
145k
            self->ottCLDidxPrev,                        /* int
1672
                                                           idxPrev[MAX_NUM_OTT][MAX_PARAMETER_BANDS],
1673
                                                         */
1674
145k
            i, t_CLD, 0,                                /* int   startBand, */
1675
145k
            self->pConfigCurrent->bitstreamOttBands[i], /*  int   stopBand, */
1676
145k
            self->pConfigCurrent->ottCLDdefault[i], /* int   defaultValue, */
1677
145k
            pCurBs->numParameterSets, /* int   numParameterSets) */
1678
145k
            pCurBs->paramSlot, self->extendFrame, self->quantMode,
1679
145k
            &(self->concealInfo), ottVsTotInactiv, NULL, NULL, NULL);
1680
145k
        if (err != MPS_OK) goto bail;
1681
1682
145k
      } /* for(i = 0; i < numOttBoxes ; i++ ) */
1683
145k
      break;
1684
158k
  } /* case */
1685
1686
291k
  for (ottIdx = 0; ottIdx < numOttBoxes; ottIdx++) {
1687
    /* Read ICC */
1688
145k
    err = mapIndexData(
1689
145k
        &pCurBs->ICCLosslessData[ottIdx], /* LOSSLESSDATA *llData,*/
1690
145k
        self->ottICC__FDK, self->outIdxData,
1691
145k
        pCurBs
1692
145k
            ->cmpOttICCidx,  /* int
1693
                                cmpIdxData[MAX_NUM_OTT][MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS],
1694
                              */
1695
145k
        self->ottICCdiffidx, /* differential data */
1696
145k
        ottIdx, /* int   xttIdx,  Which ott/ttt index to use for input and
1697
                   output buffers */
1698
145k
        self->ottICCidxPrev, /* int   idxPrev[MAX_NUM_OTT][MAX_PARAMETER_BANDS],
1699
                              */
1700
145k
        ottIdx, t_ICC, 0,    /* int   startBand, */
1701
145k
        self->pConfigCurrent->bitstreamOttBands[ottIdx], /* int   stopBand, */
1702
145k
        ICCdefault,               /* int   defaultValue, */
1703
145k
        pCurBs->numParameterSets, /* int   numParameterSets) */
1704
145k
        pCurBs->paramSlot, self->extendFrame, self->quantMode,
1705
145k
        &(self->concealInfo), ottVsTotInactiv, NULL, NULL, NULL);
1706
145k
    if (err != MPS_OK) goto bail;
1707
145k
  } /* ottIdx */
1708
1709
145k
  if ((self->treeConfig == TREE_212) && (self->phaseCoding)) {
1710
10.8k
    if (pCurBs->phaseMode == 0) {
1711
64.1k
      for (int pb = 0; pb < self->pConfigCurrent->numOttBandsIPD; pb++) {
1712
55.6k
        self->ottIPDidxPrev[0][pb] = 0;
1713
55.6k
      }
1714
8.56k
    }
1715
21.7k
    for (ottIdx = 0; ottIdx < numOttBoxes; ottIdx++) {
1716
10.8k
      err = mapIndexData(
1717
10.8k
          &pCurBs->IPDLosslessData[ottIdx], self->ottIPD__FDK, self->outIdxData,
1718
10.8k
          pCurBs->cmpOttIPDidx, NULL, ottIdx, self->ottIPDidxPrev, ottIdx,
1719
10.8k
          t_IPD, 0, self->numOttBandsIPD, IPDdefault, pCurBs->numParameterSets,
1720
10.8k
          pCurBs->paramSlot, self->extendFrame, self->quantMode,
1721
10.8k
          &(self->concealInfo), ottVsTotInactiv, NULL, NULL, NULL);
1722
10.8k
    }
1723
10.8k
  }
1724
1725
158k
bail:
1726
1727
158k
  return MPS_OK;
1728
1729
145k
} /* 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
158k
                                         const SPATIAL_BS_FRAME *frame) {
1748
158k
  int ps, pb, pg, pbStride, dataBands, pbStart, pbStop,
1749
158k
      aGroupToBand[MAX_PARAMETER_BANDS + 1];
1750
1751
158k
  if (frame->numParameterSets > MAX_PARAMETER_SETS)
1752
0
    return MPS_WRONG_PARAMETERSETS;
1753
158k
  if (self->bitstreamParameterBands > MAX_PARAMETER_BANDS)
1754
0
    return MPS_WRONG_PARAMETERBANDS;
1755
1756
358k
  for (ps = 0; ps < frame->numParameterSets; ps++) {
1757
200k
    switch (frame->bsSmoothMode[ps]) {
1758
174k
      case 0:
1759
174k
        self->smgTime[ps] = 256;
1760
174k
        FDKmemclear(self->smgData[ps],
1761
174k
                    self->bitstreamParameterBands * sizeof(UCHAR));
1762
174k
        break;
1763
1764
13.9k
      case 1:
1765
13.9k
        if (ps > 0) {
1766
13.0k
          self->smgTime[ps] = self->smgTime[ps - 1];
1767
13.0k
          FDKmemcpy(self->smgData[ps], self->smgData[ps - 1],
1768
13.0k
                    self->bitstreamParameterBands * sizeof(UCHAR));
1769
13.0k
        } else {
1770
829
          self->smgTime[ps] = self->smoothState->prevSmgTime;
1771
829
          FDKmemcpy(self->smgData[ps], self->smoothState->prevSmgData,
1772
829
                    self->bitstreamParameterBands * sizeof(UCHAR));
1773
829
        }
1774
13.9k
        break;
1775
1776
5.34k
      case 2:
1777
5.34k
        self->smgTime[ps] = smgTimeTable[frame->bsSmoothTime[ps]];
1778
57.8k
        for (pb = 0; pb < self->bitstreamParameterBands; pb++) {
1779
52.4k
          self->smgData[ps][pb] = 1;
1780
52.4k
        }
1781
5.34k
        break;
1782
1783
6.62k
      case 3:
1784
6.62k
        self->smgTime[ps] = smgTimeTable[frame->bsSmoothTime[ps]];
1785
6.62k
        pbStride = pbStrideTable[frame->bsFreqResStrideSmg[ps]];
1786
6.62k
        dataBands = (self->bitstreamParameterBands - 1) / pbStride + 1;
1787
6.62k
        createMapping(aGroupToBand, 0, self->bitstreamParameterBands, pbStride);
1788
27.8k
        for (pg = 0; pg < dataBands; pg++) {
1789
21.2k
          pbStart = aGroupToBand[pg];
1790
21.2k
          pbStop = aGroupToBand[pg + 1];
1791
86.4k
          for (pb = pbStart; pb < pbStop; pb++) {
1792
65.2k
            self->smgData[ps][pb] = frame->bsSmgData[ps][pg];
1793
65.2k
          }
1794
21.2k
        }
1795
6.62k
        break;
1796
200k
    }
1797
200k
  }
1798
1799
158k
  self->smoothState->prevSmgTime = self->smgTime[frame->numParameterSets - 1];
1800
158k
  FDKmemcpy(self->smoothState->prevSmgData,
1801
158k
            self->smgData[frame->numParameterSets - 1],
1802
158k
            self->bitstreamParameterBands * sizeof(UCHAR));
1803
1804
158k
  if (self->extendFrame) {
1805
2.33k
    self->smgTime[frame->numParameterSets] =
1806
2.33k
        self->smgTime[frame->numParameterSets - 1];
1807
2.33k
    FDKmemcpy(self->smgData[frame->numParameterSets],
1808
2.33k
              self->smgData[frame->numParameterSets - 1],
1809
2.33k
              self->bitstreamParameterBands * sizeof(UCHAR));
1810
2.33k
  }
1811
1812
158k
  return MPS_OK;
1813
158k
}
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
26.3k
                                            const SPATIAL_BS_FRAME *frame) {
1831
26.3k
  SACDEC_ERROR err = MPS_OK;
1832
26.3k
  int ch;
1833
26.3k
  int offset = self->numOttBoxes;
1834
1835
52.7k
  for (ch = 0; ch < self->numInputChannels; ch++) {
1836
26.3k
    err = mapIndexData(&frame->CLDLosslessData[offset + ch],
1837
26.3k
                       self->arbdmxGain__FDK, self->outIdxData,
1838
26.3k
                       frame->cmpArbdmxGainIdx, NULL, /* no differential data */
1839
26.3k
                       ch, self->arbdmxGainIdxPrev, offset + ch, t_CLD, 0,
1840
26.3k
                       self->bitstreamParameterBands,
1841
26.3k
                       0 /*self->arbdmxGainDefault*/, frame->numParameterSets,
1842
26.3k
                       frame->paramSlot, self->extendFrame, 0,
1843
26.3k
                       &(self->concealInfo), ottVsTotInactiv, NULL, NULL, NULL);
1844
26.3k
    if (err != MPS_OK) goto bail;
1845
26.3k
  }
1846
1847
26.3k
bail:
1848
26.3k
  return err;
1849
26.3k
} /* decodeAndMapFrameArbdmx */
1850
1851
/*******************************************************************************
1852
 Functionname: SpatialDecDecodeFrame
1853
 *******************************************************************************
1854
1855
 Description:
1856
1857
 Arguments:
1858
1859
 Return:
1860
1861
*******************************************************************************/
1862
158k
SACDEC_ERROR SpatialDecDecodeFrame(spatialDec *self, SPATIAL_BS_FRAME *frame) {
1863
158k
  SACDEC_ERROR err = MPS_OK;
1864
1865
158k
  self->extendFrame = 0;
1866
158k
  if (frame->paramSlot[frame->numParameterSets - 1] != self->timeSlots - 1) {
1867
2.33k
    self->extendFrame = 1;
1868
2.33k
  }
1869
1870
158k
  self->TsdTs = 0;
1871
1872
  /****** DTDF and MAP DATA ********/
1873
158k
  if ((err = decodeAndMapFrameOtt(self, frame)) != MPS_OK) goto bail;
1874
1875
158k
  if ((err = decodeAndMapFrameSmg(self, frame)) != MPS_OK) goto bail;
1876
1877
158k
  if (self->arbitraryDownmix != 0) {
1878
26.3k
    if ((err = decodeAndMapFrameArbdmx(self, frame)) != MPS_OK) goto bail;
1879
26.3k
  }
1880
1881
158k
  if (self->extendFrame) {
1882
2.33k
    frame->numParameterSets =
1883
2.33k
        fixMin(MAX_PARAMETER_SETS, frame->numParameterSets + 1);
1884
2.33k
    frame->paramSlot[frame->numParameterSets - 1] = self->timeSlots - 1;
1885
1886
8.72k
    for (int p = 0; p < frame->numParameterSets; p++) {
1887
6.39k
      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.39k
    }
1892
2.33k
    if (err != MPS_OK) {
1893
0
      goto bail;
1894
0
    }
1895
2.33k
  }
1896
1897
158k
bail:
1898
158k
  return err;
1899
158k
} /* SpatialDecDecodeFrame() */
1900
1901
/*******************************************************************************
1902
 Functionname: SpatialDecodeHeader
1903
 *******************************************************************************
1904
1905
 Description:
1906
1907
 Arguments:
1908
1909
 Return:
1910
1911
*******************************************************************************/
1912
1913
SACDEC_ERROR SpatialDecDecodeHeader(
1914
45.5k
    spatialDec *self, SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig) {
1915
45.5k
  SACDEC_ERROR err = MPS_OK;
1916
45.5k
  int i;
1917
1918
45.5k
  self->samplingFreq = pSpatialSpecificConfig->samplingFreq;
1919
45.5k
  self->timeSlots = pSpatialSpecificConfig->nTimeSlots;
1920
45.5k
  self->frameLength = self->timeSlots * self->qmfBands;
1921
45.5k
  self->bitstreamParameterBands = pSpatialSpecificConfig->freqRes;
1922
1923
45.5k
  if (self->pConfigCurrent->syntaxFlags & SACDEC_SYNTAX_LD)
1924
24.5k
    self->hybridBands = self->qmfBands;
1925
21.0k
  else
1926
21.0k
    self->hybridBands = SacGetHybridSubbands(self->qmfBands);
1927
45.5k
  self->tp_hybBandBorder = 12;
1928
1929
45.5k
  self->numParameterBands = self->bitstreamParameterBands;
1930
1931
45.5k
  if (self->pConfigCurrent->syntaxFlags & SACDEC_SYNTAX_LD) {
1932
24.5k
    switch (self->numParameterBands) {
1933
134
      case 4:
1934
134
        self->kernels = kernels_4_to_64;
1935
134
        break;
1936
8.71k
      case 5:
1937
8.71k
        self->kernels = kernels_5_to_64;
1938
8.71k
        break;
1939
136
      case 7:
1940
136
        self->kernels = kernels_7_to_64;
1941
136
        break;
1942
10.2k
      case 9:
1943
10.2k
        self->kernels = kernels_9_to_64;
1944
10.2k
        break;
1945
135
      case 12:
1946
135
        self->kernels = kernels_12_to_64;
1947
135
        break;
1948
463
      case 15:
1949
463
        self->kernels = kernels_15_to_64;
1950
463
        break;
1951
3.23k
      case 23:
1952
3.23k
        self->kernels = kernels_23_to_64;
1953
3.23k
        break;
1954
1.48k
      default:
1955
1.48k
        return MPS_INVALID_PARAMETERBANDS; /* unsupported numParameterBands */
1956
24.5k
    }
1957
24.5k
  } else {
1958
21.0k
    switch (self->numParameterBands) {
1959
670
      case 4:
1960
670
        self->kernels = kernels_4_to_71;
1961
670
        break;
1962
1.20k
      case 5:
1963
1.20k
        self->kernels = kernels_5_to_71;
1964
1.20k
        break;
1965
10.3k
      case 7:
1966
10.3k
        self->kernels = kernels_7_to_71;
1967
10.3k
        break;
1968
4.32k
      case 10:
1969
4.32k
        self->kernels = kernels_10_to_71;
1970
4.32k
        break;
1971
378
      case 14:
1972
378
        self->kernels = kernels_14_to_71;
1973
378
        break;
1974
2.50k
      case 20:
1975
2.50k
        self->kernels = kernels_20_to_71;
1976
2.50k
        break;
1977
1.61k
      case 28:
1978
1.61k
        self->kernels = kernels_28_to_71;
1979
1.61k
        break;
1980
0
      default:
1981
0
        return MPS_INVALID_PARAMETERBANDS; /* unsupported numParameterBands */
1982
21.0k
    }
1983
21.0k
  }
1984
1985
  /* create param to hyb band table */
1986
44.0k
  FDKmemclear(self->param2hyb, (MAX_PARAMETER_BANDS + 1) * sizeof(int));
1987
2.11M
  for (i = 0; i < self->hybridBands; i++) {
1988
2.06M
    self->param2hyb[self->kernels[i] + 1] = i + 1;
1989
2.06M
  }
1990
44.0k
  {
1991
44.0k
    int pb = self->kernels[i - 1] + 2;
1992
851k
    for (; pb < (MAX_PARAMETER_BANDS + 1); pb++) {
1993
807k
      self->param2hyb[pb] = i;
1994
807k
    }
1995
1.27M
    for (pb = 0; pb < MAX_PARAMETER_BANDS; pb += 1) {
1996
1.23M
      self->kernels_width[pb] = self->param2hyb[pb + 1] - self->param2hyb[pb];
1997
1.23M
    }
1998
44.0k
  }
1999
2000
44.0k
  self->treeConfig = pSpatialSpecificConfig->treeConfig;
2001
2002
44.0k
  self->numOttBoxes = pSpatialSpecificConfig->nOttBoxes;
2003
2004
44.0k
  self->numInputChannels = pSpatialSpecificConfig->nInputChannels;
2005
2006
44.0k
  self->numOutputChannels = pSpatialSpecificConfig->nOutputChannels;
2007
2008
44.0k
  self->quantMode = pSpatialSpecificConfig->quantMode;
2009
2010
44.0k
  self->arbitraryDownmix = pSpatialSpecificConfig->bArbitraryDownmix;
2011
2012
44.0k
  self->numM2rows = self->numOutputChannels;
2013
2014
44.0k
  {
2015
44.0k
    self->residualCoding = 0;
2016
44.0k
    if (self->arbitraryDownmix == 2)
2017
0
      self->arbitraryDownmix = 1; /* no arbitrary downmix residuals */
2018
44.0k
  }
2019
44.0k
  if ((self->pConfigCurrent->syntaxFlags & SACDEC_SYNTAX_USAC)) {
2020
21.0k
    self->residualCoding = pSpatialSpecificConfig->bResidualCoding;
2021
21.0k
  }
2022
2023
44.0k
  self->clipProtectGain__FDK =
2024
44.0k
      FX_CFG2FX_DBL(clipGainTable__FDK[pSpatialSpecificConfig->bsFixedGainDMX]);
2025
44.0k
  self->clipProtectGainSF__FDK =
2026
44.0k
      clipGainSFTable__FDK[pSpatialSpecificConfig->bsFixedGainDMX];
2027
2028
44.0k
  self->tempShapeConfig = pSpatialSpecificConfig->tempShapeConfig;
2029
2030
44.0k
  self->decorrConfig = pSpatialSpecificConfig->decorrConfig;
2031
2032
44.0k
  if (self->upmixType == UPMIXTYPE_BYPASS) {
2033
0
    self->numOutputChannels = self->numInputChannels;
2034
0
  }
2035
2036
44.0k
  self->numOutputChannelsAT = self->numOutputChannels;
2037
2038
44.0k
  self->numOttBandsIPD = pSpatialSpecificConfig->numOttBandsIPD;
2039
44.0k
  self->phaseCoding = pSpatialSpecificConfig->bsPhaseCoding;
2040
88.1k
  for (i = 0; i < self->numOttBoxes; i++) {
2041
44.0k
    {
2042
44.0k
      self->pConfigCurrent->bitstreamOttBands[i] =
2043
44.0k
          self->bitstreamParameterBands;
2044
44.0k
    }
2045
44.0k
    self->numOttBands[i] = self->pConfigCurrent->bitstreamOttBands[i];
2046
44.0k
  } /* i */
2047
2048
44.0k
  if (self->residualCoding) {
2049
4.66k
    int numBoxes = self->numOttBoxes;
2050
9.32k
    for (i = 0; i < numBoxes; i++) {
2051
4.66k
      self->residualPresent[i] =
2052
4.66k
          pSpatialSpecificConfig->ResidualConfig[i].bResidualPresent;
2053
2054
4.66k
      if (self->residualPresent[i]) {
2055
4.66k
        self->residualBands[i] =
2056
4.66k
            pSpatialSpecificConfig->ResidualConfig[i].nResidualBands;
2057
        /* conversion from hybrid bands to qmf bands */
2058
4.66k
        self->residualQMFBands[i] =
2059
4.66k
            fMax(self->param2hyb[self->residualBands[i]] + 3 - 10,
2060
4.66k
                 3); /* simplification for the lowest 10 hybrid bands */
2061
4.66k
      } else {
2062
0
        self->residualBands[i] = 0;
2063
0
        self->residualQMFBands[i] = 0;
2064
0
      }
2065
4.66k
    }
2066
4.66k
  } /* self->residualCoding */
2067
39.4k
  else {
2068
39.4k
    int boxes = self->numOttBoxes;
2069
78.8k
    for (i = 0; i < boxes; i += 1) {
2070
39.4k
      self->residualPresent[i] = 0;
2071
39.4k
      self->residualBands[i] = 0;
2072
39.4k
    }
2073
39.4k
  }
2074
2075
44.0k
  switch (self->treeConfig) {
2076
44.0k
    case TREE_212:
2077
44.0k
      self->numDirektSignals = 1;
2078
44.0k
      self->numDecorSignals = 1;
2079
44.0k
      self->numXChannels = 1;
2080
44.0k
      if (self->arbitraryDownmix == 2) {
2081
0
        self->numXChannels += 1;
2082
0
      }
2083
44.0k
      self->numVChannels = self->numDirektSignals + self->numDecorSignals;
2084
44.0k
      break;
2085
0
    default:
2086
0
      return MPS_INVALID_TREECONFIG;
2087
44.0k
  }
2088
2089
44.0k
  self->highRateMode = pSpatialSpecificConfig->bsHighRateMode;
2090
44.0k
  self->decorrType = pSpatialSpecificConfig->bsDecorrType;
2091
2092
44.0k
  SpatialDecDecodeHelperInfo(pSpatialSpecificConfig, UPMIXTYPE_NORMAL);
2093
2094
44.0k
  return err;
2095
44.0k
}
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
28.4k
                                     BS_LL_STATE *llState) {
2111
28.4k
  SPATIAL_BS_FRAME *pBs = bsFrame;
2112
2113
28.4k
  const int maxNumOtt = MAX_NUM_OTT;
2114
28.4k
  const int maxNumInputChannels = MAX_INPUT_CHANNELS;
2115
2116
28.4k
  FDK_ALLOCATE_MEMORY_1D_P(
2117
28.4k
      pBs->cmpOttIPDidx, maxNumOtt * MAX_PARAMETER_SETS * MAX_PARAMETER_BANDS,
2118
28.4k
      SCHAR, SCHAR(*)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS])
2119
2120
  /* Arbitrary Downmix */
2121
28.4k
  FDK_ALLOCATE_MEMORY_1D_P(
2122
28.4k
      pBs->cmpArbdmxGainIdx,
2123
28.4k
      maxNumInputChannels * MAX_PARAMETER_SETS * MAX_PARAMETER_BANDS, SCHAR,
2124
28.4k
      SCHAR(*)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS])
2125
2126
  /* Lossless control */
2127
28.4k
  FDK_ALLOCATE_MEMORY_1D(pBs->CLDLosslessData, MAX_NUM_PARAMETERS, LOSSLESSDATA)
2128
28.4k
  FDK_ALLOCATE_MEMORY_1D(pBs->ICCLosslessData, MAX_NUM_PARAMETERS, LOSSLESSDATA)
2129
2130
28.4k
  FDK_ALLOCATE_MEMORY_1D(pBs->IPDLosslessData, MAX_NUM_PARAMETERS, LOSSLESSDATA)
2131
2132
28.4k
  pBs->newBsData = 0;
2133
28.4k
  pBs->numParameterSets = 1;
2134
2135
  /* Link lossless states */
2136
198k
  for (int x = 0; x < MAX_NUM_PARAMETERS; x++) {
2137
170k
    pBs->CLDLosslessData[x].state = &llState->CLDLosslessState[x];
2138
170k
    pBs->ICCLosslessData[x].state = &llState->ICCLosslessState[x];
2139
2140
170k
    pBs->IPDLosslessData[x].state = &llState->IPDLosslessState[x];
2141
170k
  }
2142
2143
28.4k
  return MPS_OK;
2144
2145
0
bail:
2146
0
  return MPS_OUTOFMEMORY;
2147
28.4k
}
2148
2149
/*******************************************************************************
2150
 Functionname: SpatialDecCloseBsFrame
2151
 *******************************************************************************
2152
2153
 Description: Close spatial bitstream structure
2154
2155
 Arguments:   spatialDec* self
2156
2157
 Return:      -
2158
2159
*******************************************************************************/
2160
28.4k
void SpatialDecCloseBsFrame(SPATIAL_BS_FRAME *pBs) {
2161
28.4k
  if (pBs != NULL) {
2162
    /* These arrays contain the compact indices, only one value per pbstride,
2163
     * only paramsets actually containing data. */
2164
2165
28.4k
    FDK_FREE_MEMORY_1D(pBs->cmpOttIPDidx);
2166
2167
    /* Arbitrary Downmix */
2168
28.4k
    FDK_FREE_MEMORY_1D(pBs->cmpArbdmxGainIdx);
2169
2170
    /* Lossless control */
2171
28.4k
    FDK_FREE_MEMORY_1D(pBs->IPDLosslessData);
2172
28.4k
    FDK_FREE_MEMORY_1D(pBs->CLDLosslessData);
2173
28.4k
    FDK_FREE_MEMORY_1D(pBs->ICCLosslessData);
2174
28.4k
  }
2175
28.4k
}