Coverage Report

Created: 2026-10-17 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aac/libSBRdec/src/sbrdecoder.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2021 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
/**************************** SBR decoder library ******************************
96
97
   Author(s):
98
99
   Description:
100
101
*******************************************************************************/
102
103
/*!
104
  \file
105
  \brief  SBR decoder frontend
106
  This module provides a frontend to the SBR decoder. The function openSBR() is
107
  called for initialization. The function sbrDecoder_Apply() is called for each
108
  frame. sbr_Apply() will call the required functions to decode the raw SBR data
109
  (provided by env_extr.cpp), to decode the envelope data and noise floor levels
110
  [decodeSbrData()], and to finally apply SBR to the current frame [sbr_dec()].
111
112
  \sa sbrDecoder_Apply(), \ref documentationOverview
113
*/
114
115
/*!
116
  \page documentationOverview Overview of important information resources and
117
  source code documentation
118
119
  As part of this documentation you can find more extensive descriptions about
120
  key concepts and algorithms at the following locations:
121
122
  <h2>Programming</h2>
123
124
  \li Buffer management: sbrDecoder_Apply() and sbr_dec()
125
  \li Internal scale factors to maximize SNR on fixed point processors:
126
  #QMF_SCALE_FACTOR \li Special mantissa-exponent format: Created in
127
  requantizeEnvelopeData() and used in calculateSbrEnvelope()
128
129
  <h2>Algorithmic details</h2>
130
  \li About the SBR data format: \ref SBR_HEADER_ELEMENT and \ref
131
  SBR_STANDARD_ELEMENT \li Details about the bitstream decoder: env_extr.cpp \li
132
  Details about the QMF filterbank and the provided polyphase implementation:
133
  qmf_dec.cpp \li Details about the transposer: lpp_tran.cpp \li Details about
134
  the envelope adjuster: env_calc.cpp
135
136
*/
137
138
#include "sbrdecoder.h"
139
140
#include "FDK_bitstream.h"
141
142
#include "sbrdec_freq_sca.h"
143
#include "env_extr.h"
144
#include "sbr_dec.h"
145
#include "env_dec.h"
146
#include "FDK_crc.h"
147
#include "sbr_ram.h"
148
#include "sbr_rom.h"
149
#include "lpp_tran.h"
150
#include "transcendent.h"
151
152
#include "sbrdec_drc.h"
153
154
#include "psbitdec.h"
155
156
/* Decoder library info */
157
#define SBRDECODER_LIB_VL0 3
158
#define SBRDECODER_LIB_VL1 1
159
#define SBRDECODER_LIB_VL2 0
160
0
#define SBRDECODER_LIB_TITLE "SBR Decoder"
161
#ifdef SUPPRESS_BUILD_DATE_INFO
162
#define SBRDECODER_LIB_BUILD_DATE ""
163
#define SBRDECODER_LIB_BUILD_TIME ""
164
#else
165
0
#define SBRDECODER_LIB_BUILD_DATE __DATE__
166
0
#define SBRDECODER_LIB_BUILD_TIME __TIME__
167
#endif
168
169
476k
static void setFrameErrorFlag(SBR_DECODER_ELEMENT *pSbrElement, UCHAR value) {
170
476k
  if (pSbrElement != NULL) {
171
476k
    switch (value) {
172
123k
      case FRAME_ERROR_ALLSLOTS:
173
123k
        FDKmemset(pSbrElement->frameErrorFlag, FRAME_ERROR,
174
123k
                  sizeof(pSbrElement->frameErrorFlag));
175
123k
        break;
176
353k
      default:
177
353k
        pSbrElement->frameErrorFlag[pSbrElement->useFrameSlot] = value;
178
476k
    }
179
476k
  }
180
476k
}
181
182
622k
static UCHAR getHeaderSlot(UCHAR currentSlot, UCHAR hdrSlotUsage[(1) + 1]) {
183
622k
  UINT occupied = 0;
184
622k
  int s;
185
622k
  UCHAR slot = hdrSlotUsage[currentSlot];
186
187
622k
  FDK_ASSERT((1) + 1 < 32);
188
189
1.66M
  for (s = 0; s < (1) + 1; s++) {
190
1.21M
    if ((hdrSlotUsage[s] == slot) && (s != slot)) {
191
173k
      occupied = 1;
192
173k
      break;
193
173k
    }
194
1.21M
  }
195
196
622k
  if (occupied) {
197
173k
    occupied = 0;
198
199
520k
    for (s = 0; s < (1) + 1; s++) {
200
347k
      occupied |= 1 << hdrSlotUsage[s];
201
347k
    }
202
322k
    for (s = 0; s < (1) + 1; s++) {
203
320k
      if (!(occupied & 0x1)) {
204
172k
        slot = s;
205
172k
        break;
206
172k
      }
207
148k
      occupied >>= 1;
208
148k
    }
209
173k
  }
210
211
622k
  return slot;
212
622k
}
213
214
static void copySbrHeader(HANDLE_SBR_HEADER_DATA hDst,
215
38.3k
                          const HANDLE_SBR_HEADER_DATA hSrc) {
216
  /* copy the whole header memory (including pointers) */
217
38.3k
  FDKmemcpy(hDst, hSrc, sizeof(SBR_HEADER_DATA));
218
219
  /* update pointers */
220
38.3k
  hDst->freqBandData.freqBandTable[0] = hDst->freqBandData.freqBandTableLo;
221
38.3k
  hDst->freqBandData.freqBandTable[1] = hDst->freqBandData.freqBandTableHi;
222
38.3k
}
223
224
static int compareSbrHeader(const HANDLE_SBR_HEADER_DATA hHdr1,
225
9.26k
                            const HANDLE_SBR_HEADER_DATA hHdr2) {
226
9.26k
  int result = 0;
227
228
  /* compare basic data */
229
9.26k
  result |= (hHdr1->syncState != hHdr2->syncState) ? 1 : 0;
230
9.26k
  result |= (hHdr1->status != hHdr2->status) ? 1 : 0;
231
9.26k
  result |= (hHdr1->frameErrorFlag != hHdr2->frameErrorFlag) ? 1 : 0;
232
9.26k
  result |= (hHdr1->numberTimeSlots != hHdr2->numberTimeSlots) ? 1 : 0;
233
9.26k
  result |=
234
9.26k
      (hHdr1->numberOfAnalysisBands != hHdr2->numberOfAnalysisBands) ? 1 : 0;
235
9.26k
  result |= (hHdr1->timeStep != hHdr2->timeStep) ? 1 : 0;
236
9.26k
  result |= (hHdr1->sbrProcSmplRate != hHdr2->sbrProcSmplRate) ? 1 : 0;
237
238
  /* compare bitstream data */
239
9.26k
  result |=
240
9.26k
      FDKmemcmp(&hHdr1->bs_data, &hHdr2->bs_data, sizeof(SBR_HEADER_DATA_BS));
241
9.26k
  result |=
242
9.26k
      FDKmemcmp(&hHdr1->bs_dflt, &hHdr2->bs_dflt, sizeof(SBR_HEADER_DATA_BS));
243
9.26k
  result |= FDKmemcmp(&hHdr1->bs_info, &hHdr2->bs_info,
244
9.26k
                      sizeof(SBR_HEADER_DATA_BS_INFO));
245
246
  /* compare frequency band data */
247
9.26k
  result |= FDKmemcmp(&hHdr1->freqBandData, &hHdr2->freqBandData,
248
9.26k
                      (8 + MAX_NUM_LIMITERS + 1) * sizeof(UCHAR));
249
9.26k
  result |= FDKmemcmp(hHdr1->freqBandData.freqBandTableLo,
250
9.26k
                      hHdr2->freqBandData.freqBandTableLo,
251
9.26k
                      (MAX_FREQ_COEFFS / 2 + 1) * sizeof(UCHAR));
252
9.26k
  result |= FDKmemcmp(hHdr1->freqBandData.freqBandTableHi,
253
9.26k
                      hHdr2->freqBandData.freqBandTableHi,
254
9.26k
                      (MAX_FREQ_COEFFS + 1) * sizeof(UCHAR));
255
9.26k
  result |= FDKmemcmp(hHdr1->freqBandData.freqBandTableNoise,
256
9.26k
                      hHdr2->freqBandData.freqBandTableNoise,
257
9.26k
                      (MAX_NOISE_COEFFS + 1) * sizeof(UCHAR));
258
9.26k
  result |=
259
9.26k
      FDKmemcmp(hHdr1->freqBandData.v_k_master, hHdr2->freqBandData.v_k_master,
260
9.26k
                (MAX_FREQ_COEFFS + 1) * sizeof(UCHAR));
261
262
9.26k
  return result;
263
9.26k
}
264
265
/*!
266
  \brief Reset SBR decoder.
267
268
  Reset should only be called if SBR has been sucessfully detected by
269
  an appropriate checkForPayload() function.
270
271
  \return Error code.
272
*/
273
static SBR_ERROR sbrDecoder_ResetElement(HANDLE_SBRDECODER self,
274
                                         int sampleRateIn, int sampleRateOut,
275
                                         int samplesPerFrame,
276
                                         const MP4_ELEMENT_ID elementID,
277
                                         const int elementIndex,
278
94.4k
                                         const int overlap) {
279
94.4k
  SBR_ERROR sbrError = SBRDEC_OK;
280
94.4k
  HANDLE_SBR_HEADER_DATA hSbrHeader;
281
94.4k
  UINT qmfFlags = 0;
282
283
94.4k
  int i, synDownsampleFac;
284
285
  /* USAC: assuming theoretical case 8 kHz output sample rate with 4:1 SBR */
286
94.4k
  const int sbr_min_sample_rate_in = IS_USAC(self->coreCodec) ? 2000 : 6400;
287
288
  /* Check in/out samplerates */
289
94.4k
  if (sampleRateIn < sbr_min_sample_rate_in || sampleRateIn > (96000)) {
290
1.07k
    sbrError = SBRDEC_UNSUPPORTED_CONFIG;
291
1.07k
    goto bail;
292
1.07k
  }
293
294
93.3k
  if (sampleRateOut > (96000)) {
295
334
    sbrError = SBRDEC_UNSUPPORTED_CONFIG;
296
334
    goto bail;
297
334
  }
298
299
  /* Set QMF mode flags */
300
93.0k
  if (self->flags & SBRDEC_LOW_POWER) qmfFlags |= QMF_FLAG_LP;
301
302
93.0k
  if (self->coreCodec == AOT_ER_AAC_ELD) {
303
47.0k
    if (self->flags & SBRDEC_LD_MPS_QMF) {
304
0
      qmfFlags |= QMF_FLAG_MPSLDFB;
305
47.0k
    } else {
306
47.0k
      qmfFlags |= QMF_FLAG_CLDFB;
307
47.0k
    }
308
47.0k
  }
309
310
  /* Set downsampling factor for synthesis filter bank */
311
93.0k
  if (sampleRateOut == 0) {
312
    /* no single rate mode */
313
5.68k
    sampleRateOut =
314
5.68k
        sampleRateIn
315
5.68k
        << 1; /* In case of implicit signalling, assume dual rate SBR */
316
5.68k
  }
317
318
93.0k
  if (sampleRateIn == sampleRateOut) {
319
34.4k
    synDownsampleFac = 2;
320
34.4k
    self->flags |= SBRDEC_DOWNSAMPLE;
321
58.5k
  } else {
322
58.5k
    synDownsampleFac = 1;
323
58.5k
    self->flags &= ~SBRDEC_DOWNSAMPLE;
324
58.5k
  }
325
326
93.0k
  self->synDownsampleFac = synDownsampleFac;
327
93.0k
  self->sampleRateOut = sampleRateOut;
328
329
93.0k
  {
330
279k
    for (i = 0; i < (1) + 1; i++) {
331
186k
      int setDflt;
332
186k
      hSbrHeader = &(self->sbrHeader[elementIndex][i]);
333
186k
      setDflt = ((hSbrHeader->syncState == SBR_NOT_INITIALIZED) ||
334
2.21k
                 (self->flags & SBRDEC_FORCE_RESET))
335
186k
                    ? 1
336
186k
                    : 0;
337
338
      /* init a default header such that we can at least do upsampling later */
339
186k
      sbrError = initHeaderData(hSbrHeader, sampleRateIn, sampleRateOut,
340
186k
                                self->downscaleFactor, samplesPerFrame,
341
186k
                                self->flags, setDflt);
342
343
      /* Set synchState to UPSAMPLING in case it already is initialized */
344
186k
      hSbrHeader->syncState = hSbrHeader->syncState > UPSAMPLING
345
186k
                                  ? UPSAMPLING
346
186k
                                  : hSbrHeader->syncState;
347
186k
    }
348
93.0k
  }
349
350
93.0k
  if (sbrError != SBRDEC_OK) {
351
39
    goto bail;
352
39
  }
353
354
92.9k
  if (!self->pQmfDomain->globalConf.qmfDomainExplicitConfig) {
355
92.9k
    self->pQmfDomain->globalConf.flags_requested |= qmfFlags;
356
92.9k
    self->pQmfDomain->globalConf.nBandsAnalysis_requested =
357
92.9k
        self->sbrHeader[elementIndex][0].numberOfAnalysisBands;
358
92.9k
    self->pQmfDomain->globalConf.nBandsSynthesis_requested =
359
92.9k
        (synDownsampleFac == 1) ? 64 : 32; /* may be overwritten by MPS */
360
92.9k
    self->pQmfDomain->globalConf.nBandsSynthesis_requested /=
361
92.9k
        self->downscaleFactor;
362
92.9k
    self->pQmfDomain->globalConf.nQmfTimeSlots_requested =
363
92.9k
        self->sbrHeader[elementIndex][0].numberTimeSlots *
364
92.9k
        self->sbrHeader[elementIndex][0].timeStep;
365
92.9k
    self->pQmfDomain->globalConf.nQmfOvTimeSlots_requested = overlap;
366
92.9k
    self->pQmfDomain->globalConf.nQmfProcBands_requested = 64; /* always 64 */
367
92.9k
    self->pQmfDomain->globalConf.nQmfProcChannels_requested =
368
92.9k
        1; /* may be overwritten by MPS */
369
92.9k
  }
370
371
  /* Init SBR channels going to be assigned to a SBR element */
372
92.9k
  {
373
92.9k
    int ch;
374
232k
    for (ch = 0; ch < self->pSbrElement[elementIndex]->nChannels; ch++) {
375
142k
      int headerIndex =
376
142k
          getHeaderSlot(self->pSbrElement[elementIndex]->useFrameSlot,
377
142k
                        self->pSbrElement[elementIndex]->useHeaderSlot);
378
379
      /* and create sbrDec */
380
142k
      sbrError =
381
142k
          createSbrDec(self->pSbrElement[elementIndex]->pSbrChannel[ch],
382
142k
                       &self->sbrHeader[elementIndex][headerIndex],
383
142k
                       &self->pSbrElement[elementIndex]->transposerSettings,
384
142k
                       synDownsampleFac, qmfFlags, self->flags, overlap, ch,
385
142k
                       self->codecFrameSize);
386
387
142k
      if (sbrError != SBRDEC_OK) {
388
2.86k
        goto bail;
389
2.86k
      }
390
142k
    }
391
92.9k
  }
392
393
  // FDKmemclear(sbr_OverlapBuffer, sizeof(sbr_OverlapBuffer));
394
395
90.0k
  if (self->numSbrElements == 1) {
396
64.6k
    switch (self->coreCodec) {
397
1.74k
      case AOT_AAC_LC:
398
1.74k
      case AOT_SBR:
399
1.74k
      case AOT_PS:
400
4.83k
      case AOT_ER_AAC_SCAL:
401
4.83k
      case AOT_DRM_AAC:
402
4.83k
      case AOT_DRM_SURROUND:
403
4.83k
        if (CreatePsDec(&self->hParametricStereoDec, samplesPerFrame)) {
404
0
          sbrError = SBRDEC_CREATE_ERROR;
405
0
          goto bail;
406
0
        }
407
4.83k
        break;
408
59.7k
      default:
409
59.7k
        break;
410
64.6k
    }
411
64.6k
  }
412
413
  /* Init frame delay slot handling */
414
90.0k
  self->pSbrElement[elementIndex]->useFrameSlot = 0;
415
270k
  for (i = 0; i < ((1) + 1); i++) {
416
180k
    self->pSbrElement[elementIndex]->useHeaderSlot[i] = i;
417
180k
  }
418
419
94.4k
bail:
420
421
94.4k
  return sbrError;
422
90.0k
}
423
424
/*!
425
  \brief Assign QMF domain provided QMF channels to SBR channels.
426
427
  \return void
428
*/
429
94.4k
static void sbrDecoder_AssignQmfChannels2SbrChannels(HANDLE_SBRDECODER self) {
430
94.4k
  int ch, el, absCh_offset = 0;
431
246k
  for (el = 0; el < self->numSbrElements; el++) {
432
152k
    if (self->pSbrElement[el] != NULL) {
433
385k
      for (ch = 0; ch < self->pSbrElement[el]->nChannels; ch++) {
434
234k
        FDK_ASSERT(((absCh_offset + ch) < ((8) + (1))) &&
435
234k
                   ((absCh_offset + ch) < ((8) + (1))));
436
234k
        self->pSbrElement[el]->pSbrChannel[ch]->SbrDec.qmfDomainInCh =
437
234k
            &self->pQmfDomain->QmfDomainIn[absCh_offset + ch];
438
234k
        self->pSbrElement[el]->pSbrChannel[ch]->SbrDec.qmfDomainOutCh =
439
234k
            &self->pQmfDomain->QmfDomainOut[absCh_offset + ch];
440
234k
      }
441
151k
      absCh_offset += self->pSbrElement[el]->nChannels;
442
151k
    }
443
152k
  }
444
94.4k
}
445
446
SBR_ERROR sbrDecoder_Open(HANDLE_SBRDECODER *pSelf,
447
34.5k
                          HANDLE_FDK_QMF_DOMAIN pQmfDomain) {
448
34.5k
  HANDLE_SBRDECODER self = NULL;
449
34.5k
  SBR_ERROR sbrError = SBRDEC_OK;
450
34.5k
  int elIdx;
451
452
34.5k
  if ((pSelf == NULL) || (pQmfDomain == NULL)) {
453
0
    return SBRDEC_INVALID_ARGUMENT;
454
0
  }
455
456
  /* Get memory for this instance */
457
34.5k
  self = GetRam_SbrDecoder();
458
34.5k
  if (self == NULL) {
459
0
    sbrError = SBRDEC_MEM_ALLOC_FAILED;
460
0
    goto bail;
461
0
  }
462
463
34.5k
  self->pQmfDomain = pQmfDomain;
464
465
  /*
466
  Already zero because of calloc
467
  self->numSbrElements = 0;
468
  self->numSbrChannels = 0;
469
  self->codecFrameSize = 0;
470
  */
471
472
34.5k
  self->numDelayFrames = (1); /* set to the max value by default */
473
474
  /* Initialize header sync state */
475
311k
  for (elIdx = 0; elIdx < (8); elIdx += 1) {
476
276k
    int i;
477
829k
    for (i = 0; i < (1) + 1; i += 1) {
478
553k
      self->sbrHeader[elIdx][i].syncState = SBR_NOT_INITIALIZED;
479
553k
    }
480
276k
  }
481
482
34.5k
  *pSelf = self;
483
484
34.5k
bail:
485
34.5k
  return sbrError;
486
34.5k
}
487
488
/**
489
 * \brief determine if the given core codec AOT can be processed or not.
490
 * \param coreCodec core codec audio object type.
491
 * \return 1 if SBR can be processed, 0 if SBR cannot be processed/applied.
492
 */
493
1.18M
static int sbrDecoder_isCoreCodecValid(AUDIO_OBJECT_TYPE coreCodec) {
494
1.18M
  switch (coreCodec) {
495
24.8k
    case AOT_AAC_LC:
496
24.8k
    case AOT_SBR:
497
24.8k
    case AOT_PS:
498
72.3k
    case AOT_ER_AAC_SCAL:
499
731k
    case AOT_ER_AAC_ELD:
500
731k
    case AOT_DRM_AAC:
501
731k
    case AOT_DRM_SURROUND:
502
1.18M
    case AOT_USAC:
503
1.18M
      return 1;
504
852
    default:
505
852
      return 0;
506
1.18M
  }
507
1.18M
}
508
509
static void sbrDecoder_DestroyElement(HANDLE_SBRDECODER self,
510
2.18M
                                      const int elementIndex) {
511
2.18M
  if (self->pSbrElement[elementIndex] != NULL) {
512
93.0k
    int ch;
513
514
279k
    for (ch = 0; ch < SBRDEC_MAX_CH_PER_ELEMENT; ch++) {
515
186k
      if (self->pSbrElement[elementIndex]->pSbrChannel[ch] != NULL) {
516
145k
        deleteSbrDec(self->pSbrElement[elementIndex]->pSbrChannel[ch]);
517
145k
        FreeRam_SbrDecChannel(
518
145k
            &self->pSbrElement[elementIndex]->pSbrChannel[ch]);
519
145k
        self->numSbrChannels -= 1;
520
145k
      }
521
186k
    }
522
93.0k
    FreeRam_SbrDecElement(&self->pSbrElement[elementIndex]);
523
93.0k
    self->numSbrElements -= 1;
524
93.0k
  }
525
2.18M
}
526
527
SBR_ERROR sbrDecoder_InitElement(
528
    HANDLE_SBRDECODER self, const int sampleRateIn, const int sampleRateOut,
529
    const int samplesPerFrame, const AUDIO_OBJECT_TYPE coreCodec,
530
    const MP4_ELEMENT_ID elementID, const int elementIndex,
531
    const UCHAR harmonicSBR, const UCHAR stereoConfigIndex,
532
666k
    const UCHAR configMode, UCHAR *configChanged, const INT downscaleFactor) {
533
666k
  SBR_ERROR sbrError = SBRDEC_OK;
534
666k
  int chCnt = 0;
535
666k
  int nSbrElementsStart;
536
666k
  int nSbrChannelsStart;
537
666k
  if (self == NULL) {
538
0
    return SBRDEC_INVALID_ARGUMENT;
539
0
  }
540
541
666k
  nSbrElementsStart = self->numSbrElements;
542
666k
  nSbrChannelsStart = self->numSbrChannels;
543
544
  /* Check core codec AOT */
545
666k
  if (!sbrDecoder_isCoreCodecValid(coreCodec) || elementIndex >= (8)) {
546
852
    sbrError = SBRDEC_UNSUPPORTED_CONFIG;
547
852
    goto bail;
548
852
  }
549
550
665k
  if (elementID != ID_SCE && elementID != ID_CPE && elementID != ID_LFE) {
551
0
    sbrError = SBRDEC_UNSUPPORTED_CONFIG;
552
0
    goto bail;
553
0
  }
554
555
665k
  if (self->sampleRateIn == sampleRateIn &&
556
550k
      self->codecFrameSize == samplesPerFrame && self->coreCodec == coreCodec &&
557
512k
      self->pSbrElement[elementIndex] != NULL &&
558
460k
      self->pSbrElement[elementIndex]->elementID == elementID &&
559
458k
      !(self->flags & SBRDEC_FORCE_RESET) &&
560
458k
      ((sampleRateOut == 0) ? 1 : (self->sampleRateOut == sampleRateOut)) &&
561
454k
      ((harmonicSBR == 2) ? 1
562
454k
                          : (self->harmonicSBR ==
563
309k
                             harmonicSBR)) /* The value 2 signalizes that
564
                                              harmonicSBR shall be ignored in
565
                                              the config change detection */
566
665k
  ) {
567
    /* Nothing to do */
568
451k
    return SBRDEC_OK;
569
451k
  } else {
570
213k
    if (configMode & AC_CM_DET_CFG_CHANGE) {
571
119k
      *configChanged = 1;
572
119k
    }
573
213k
  }
574
575
  /* reaching this point the SBR-decoder gets (re-)configured */
576
577
  /* The flags field is used for all elements! */
578
213k
  self->flags &=
579
213k
      (SBRDEC_FORCE_RESET | SBRDEC_FLUSH); /* Keep the global flags. They will
580
                                              be reset after decoding. */
581
213k
  self->flags |= (downscaleFactor > 1) ? SBRDEC_ELD_DOWNSCALE : 0;
582
213k
  self->flags |= (coreCodec == AOT_ER_AAC_ELD) ? SBRDEC_ELD_GRID : 0;
583
213k
  self->flags |= (coreCodec == AOT_ER_AAC_SCAL) ? SBRDEC_SYNTAX_SCAL : 0;
584
213k
  self->flags |=
585
213k
      (coreCodec == AOT_DRM_AAC) ? SBRDEC_SYNTAX_SCAL | SBRDEC_SYNTAX_DRM : 0;
586
213k
  self->flags |= (coreCodec == AOT_DRM_SURROUND)
587
213k
                     ? SBRDEC_SYNTAX_SCAL | SBRDEC_SYNTAX_DRM
588
213k
                     : 0;
589
213k
  self->flags |= (coreCodec == AOT_USAC) ? SBRDEC_SYNTAX_USAC : 0;
590
  /* Robustness: Take integer division rounding into consideration. E.g. 22050
591
   * Hz with 4:1 SBR => 5512 Hz core sampling rate. */
592
213k
  self->flags |= (sampleRateIn == sampleRateOut / 4) ? SBRDEC_QUAD_RATE : 0;
593
213k
  self->flags |= (harmonicSBR == 1) ? SBRDEC_USAC_HARMONICSBR : 0;
594
595
213k
  if (configMode & AC_CM_DET_CFG_CHANGE) {
596
119k
    return SBRDEC_OK;
597
119k
  }
598
599
94.4k
  self->sampleRateIn = sampleRateIn;
600
94.4k
  self->codecFrameSize = samplesPerFrame;
601
94.4k
  self->coreCodec = coreCodec;
602
94.4k
  self->harmonicSBR = harmonicSBR;
603
94.4k
  self->downscaleFactor = downscaleFactor;
604
605
  /* Init SBR elements */
606
94.4k
  {
607
94.4k
    int elChannels, ch;
608
609
94.4k
    if (self->pSbrElement[elementIndex] == NULL) {
610
93.0k
      self->pSbrElement[elementIndex] = GetRam_SbrDecElement(elementIndex);
611
93.0k
      if (self->pSbrElement[elementIndex] == NULL) {
612
0
        sbrError = SBRDEC_MEM_ALLOC_FAILED;
613
0
        goto bail;
614
0
      }
615
93.0k
      self->numSbrElements++;
616
93.0k
    } else {
617
1.32k
      self->numSbrChannels -= self->pSbrElement[elementIndex]->nChannels;
618
1.32k
    }
619
620
    /* Determine amount of channels for this element */
621
94.4k
    switch (elementID) {
622
0
      case ID_NONE:
623
49.7k
      case ID_CPE:
624
49.7k
        elChannels = 2;
625
49.7k
        break;
626
5.23k
      case ID_LFE:
627
44.6k
      case ID_SCE:
628
44.6k
        elChannels = 1;
629
44.6k
        break;
630
0
      default:
631
0
        elChannels = 0;
632
0
        break;
633
94.4k
    }
634
635
    /* Handle case of Parametric Stereo */
636
94.4k
    if (elementIndex == 0 && elementID == ID_SCE) {
637
37.5k
      switch (coreCodec) {
638
1.53k
        case AOT_AAC_LC:
639
1.53k
        case AOT_SBR:
640
1.53k
        case AOT_PS:
641
3.64k
        case AOT_ER_AAC_SCAL:
642
3.64k
        case AOT_DRM_AAC:
643
3.64k
        case AOT_DRM_SURROUND:
644
3.64k
          elChannels = 2;
645
3.64k
          break;
646
33.9k
        default:
647
33.9k
          break;
648
37.5k
      }
649
37.5k
    }
650
651
    /* Sanity check to avoid memory leaks */
652
94.4k
    if (elChannels < self->pSbrElement[elementIndex]->nChannels ||
653
94.4k
        (self->numSbrChannels + elChannels) > (8) + (1)) {
654
0
      self->numSbrChannels += self->pSbrElement[elementIndex]->nChannels;
655
0
      sbrError = SBRDEC_PARSE_ERROR;
656
0
      goto bail;
657
0
    }
658
659
    /* Save element ID for sanity checks and to have a fallback for concealment.
660
     */
661
94.4k
    self->pSbrElement[elementIndex]->elementID = elementID;
662
94.4k
    self->pSbrElement[elementIndex]->nChannels = elChannels;
663
664
242k
    for (ch = 0; ch < elChannels; ch++) {
665
147k
      if (self->pSbrElement[elementIndex]->pSbrChannel[ch] == NULL) {
666
145k
        self->pSbrElement[elementIndex]->pSbrChannel[ch] =
667
145k
            GetRam_SbrDecChannel(chCnt);
668
145k
        if (self->pSbrElement[elementIndex]->pSbrChannel[ch] == NULL) {
669
0
          sbrError = SBRDEC_MEM_ALLOC_FAILED;
670
0
          goto bail;
671
0
        }
672
145k
      }
673
147k
      self->numSbrChannels++;
674
675
147k
      sbrDecoder_drcInitChannel(&self->pSbrElement[elementIndex]
676
147k
                                     ->pSbrChannel[ch]
677
147k
                                     ->SbrDec.sbrDrcChannel);
678
679
147k
      chCnt++;
680
147k
    }
681
94.4k
  }
682
683
94.4k
  if (!self->pQmfDomain->globalConf.qmfDomainExplicitConfig) {
684
94.4k
    self->pQmfDomain->globalConf.nInputChannels_requested =
685
94.4k
        self->numSbrChannels;
686
94.4k
    self->pQmfDomain->globalConf.nOutputChannels_requested =
687
94.4k
        fMax((INT)self->numSbrChannels,
688
94.4k
             (INT)self->pQmfDomain->globalConf.nOutputChannels_requested);
689
94.4k
  }
690
691
  /* Make sure each SBR channel has one QMF channel assigned even if
692
   * numSbrChannels or element set-up has changed. */
693
94.4k
  sbrDecoder_AssignQmfChannels2SbrChannels(self);
694
695
  /* clear error flags for all delay slots */
696
94.4k
  FDKmemclear(self->pSbrElement[elementIndex]->frameErrorFlag,
697
94.4k
              ((1) + 1) * sizeof(UCHAR));
698
699
94.4k
  {
700
94.4k
    int overlap;
701
702
94.4k
    if (coreCodec == AOT_ER_AAC_ELD) {
703
47.9k
      overlap = 0;
704
47.9k
    } else if (self->flags & SBRDEC_QUAD_RATE) {
705
14.2k
      overlap = (3 * 4);
706
32.2k
    } else {
707
32.2k
      overlap = (3 * 2);
708
32.2k
    }
709
    /* Initialize this instance */
710
94.4k
    sbrError = sbrDecoder_ResetElement(self, sampleRateIn, sampleRateOut,
711
94.4k
                                       samplesPerFrame, elementID, elementIndex,
712
94.4k
                                       overlap);
713
94.4k
  }
714
715
95.2k
bail:
716
95.2k
  if (sbrError != SBRDEC_OK) {
717
5.16k
    if ((nSbrElementsStart < self->numSbrElements) ||
718
4.31k
        (nSbrChannelsStart < self->numSbrChannels)) {
719
      /* Free the memory allocated for this element */
720
4.31k
      sbrDecoder_DestroyElement(self, elementIndex);
721
4.31k
    } else if ((elementIndex < (8)) &&
722
852
               (self->pSbrElement[elementIndex] !=
723
852
                NULL)) { /* Set error flag to trigger concealment */
724
0
      setFrameErrorFlag(self->pSbrElement[elementIndex], FRAME_ERROR);
725
0
    }
726
5.16k
  }
727
728
95.2k
  return sbrError;
729
94.4k
}
730
731
/**
732
 * \brief Free config dependent SBR memory.
733
 * \param self SBR decoder instance handle
734
 */
735
238k
SBR_ERROR sbrDecoder_FreeMem(HANDLE_SBRDECODER *self) {
736
238k
  int i;
737
238k
  int elIdx;
738
739
238k
  if (self != NULL && *self != NULL) {
740
2.14M
    for (i = 0; i < (8); i++) {
741
1.90M
      sbrDecoder_DestroyElement(*self, i);
742
1.90M
    }
743
744
2.14M
    for (elIdx = 0; elIdx < (8); elIdx += 1) {
745
5.71M
      for (i = 0; i < (1) + 1; i += 1) {
746
3.81M
        (*self)->sbrHeader[elIdx][i].syncState = SBR_NOT_INITIALIZED;
747
3.81M
      }
748
1.90M
    }
749
238k
  }
750
751
238k
  return SBRDEC_OK;
752
238k
}
753
754
/**
755
 * \brief Apply decoded SBR header for one element.
756
 * \param self SBR decoder instance handle
757
 * \param hSbrHeader SBR header handle to be processed.
758
 * \param hSbrChannel pointer array to the SBR element channels corresponding to
759
 * the SBR header.
760
 * \param headerStatus header status value returned from SBR header parser.
761
 * \param numElementChannels amount of channels for the SBR element whos header
762
 * is to be processed.
763
 */
764
static SBR_ERROR sbrDecoder_HeaderUpdate(HANDLE_SBRDECODER self,
765
                                         HANDLE_SBR_HEADER_DATA hSbrHeader,
766
                                         SBR_HEADER_STATUS headerStatus,
767
                                         HANDLE_SBR_CHANNEL hSbrChannel[],
768
232k
                                         const int numElementChannels) {
769
232k
  SBR_ERROR errorStatus = SBRDEC_OK;
770
771
  /*
772
    change of control data, reset decoder
773
  */
774
232k
  errorStatus = resetFreqBandTables(hSbrHeader, self->flags);
775
776
232k
  if (errorStatus == SBRDEC_OK) {
777
194k
    if (hSbrHeader->syncState == UPSAMPLING && headerStatus != HEADER_RESET) {
778
#if (SBRDEC_MAX_HB_FADE_FRAMES > 0)
779
      int ch;
780
      for (ch = 0; ch < numElementChannels; ch += 1) {
781
        hSbrChannel[ch]->SbrDec.highBandFadeCnt = SBRDEC_MAX_HB_FADE_FRAMES;
782
      }
783
784
#endif
785
      /* As the default header would limit the frequency range,
786
         lowSubband and highSubband must be patched. */
787
50.3k
      hSbrHeader->freqBandData.lowSubband = hSbrHeader->numberOfAnalysisBands;
788
50.3k
      hSbrHeader->freqBandData.highSubband = hSbrHeader->numberOfAnalysisBands;
789
50.3k
    }
790
791
    /* Trigger a reset before processing this slot */
792
194k
    hSbrHeader->status |= SBRDEC_HDR_STAT_RESET;
793
194k
  }
794
795
232k
  return errorStatus;
796
232k
}
797
798
INT sbrDecoder_Header(HANDLE_SBRDECODER self, HANDLE_FDK_BITSTREAM hBs,
799
                      const INT sampleRateIn, const INT sampleRateOut,
800
                      const INT samplesPerFrame,
801
                      const AUDIO_OBJECT_TYPE coreCodec,
802
                      const MP4_ELEMENT_ID elementID, const INT elementIndex,
803
                      const UCHAR harmonicSBR, const UCHAR stereoConfigIndex,
804
                      const UCHAR configMode, UCHAR *configChanged,
805
514k
                      const INT downscaleFactor) {
806
514k
  SBR_HEADER_STATUS headerStatus;
807
514k
  HANDLE_SBR_HEADER_DATA hSbrHeader;
808
514k
  SBR_ERROR sbrError = SBRDEC_OK;
809
514k
  int headerIndex;
810
514k
  UINT flagsSaved =
811
514k
      0; /* flags should not be changed in AC_CM_DET_CFG_CHANGE - mode after
812
            parsing */
813
814
514k
  if (self == NULL || elementIndex >= (8)) {
815
0
    return SBRDEC_UNSUPPORTED_CONFIG;
816
0
  }
817
818
514k
  if (!sbrDecoder_isCoreCodecValid(coreCodec)) {
819
0
    return SBRDEC_UNSUPPORTED_CONFIG;
820
0
  }
821
822
514k
  if (configMode & AC_CM_DET_CFG_CHANGE) {
823
328k
    flagsSaved = self->flags; /* store */
824
328k
  }
825
826
514k
  sbrError = sbrDecoder_InitElement(
827
514k
      self, sampleRateIn, sampleRateOut, samplesPerFrame, coreCodec, elementID,
828
514k
      elementIndex, harmonicSBR, stereoConfigIndex, configMode, configChanged,
829
514k
      downscaleFactor);
830
831
514k
  if ((sbrError != SBRDEC_OK) || (elementID == ID_LFE)) {
832
50.8k
    goto bail;
833
50.8k
  }
834
835
464k
  if (configMode & AC_CM_DET_CFG_CHANGE) {
836
299k
    hSbrHeader = NULL;
837
299k
  } else {
838
164k
    headerIndex = getHeaderSlot(self->pSbrElement[elementIndex]->useFrameSlot,
839
164k
                                self->pSbrElement[elementIndex]->useHeaderSlot);
840
841
164k
    hSbrHeader = &(self->sbrHeader[elementIndex][headerIndex]);
842
164k
  }
843
844
464k
  headerStatus = sbrGetHeaderData(hSbrHeader, hBs, self->flags, 0, configMode);
845
846
464k
  if (coreCodec == AOT_USAC) {
847
220k
    if (configMode & AC_CM_DET_CFG_CHANGE) {
848
144k
      self->flags = flagsSaved; /* restore */
849
144k
    }
850
220k
    return sbrError;
851
220k
  }
852
853
243k
  if (configMode & AC_CM_ALLOC_MEM) {
854
88.4k
    SBR_DECODER_ELEMENT *pSbrElement;
855
856
88.4k
    pSbrElement = self->pSbrElement[elementIndex];
857
858
    /* Sanity check */
859
88.4k
    if (pSbrElement != NULL) {
860
88.4k
      if ((elementID == ID_CPE && pSbrElement->nChannels != 2) ||
861
88.4k
          (elementID != ID_CPE && pSbrElement->nChannels != 1)) {
862
0
        return SBRDEC_UNSUPPORTED_CONFIG;
863
0
      }
864
88.4k
      if (headerStatus == HEADER_RESET) {
865
62.3k
        sbrError = sbrDecoder_HeaderUpdate(self, hSbrHeader, headerStatus,
866
62.3k
                                           pSbrElement->pSbrChannel,
867
62.3k
                                           pSbrElement->nChannels);
868
869
62.3k
        if (sbrError == SBRDEC_OK) {
870
55.0k
          hSbrHeader->syncState = SBR_HEADER;
871
55.0k
          hSbrHeader->status |= SBRDEC_HDR_STAT_UPDATE;
872
55.0k
        } else {
873
7.26k
          hSbrHeader->syncState = SBR_NOT_INITIALIZED;
874
7.26k
          hSbrHeader->status = HEADER_ERROR;
875
7.26k
        }
876
62.3k
      }
877
88.4k
    }
878
88.4k
  }
879
293k
bail:
880
293k
  if (configMode & AC_CM_DET_CFG_CHANGE) {
881
184k
    self->flags = flagsSaved; /* restore */
882
184k
  }
883
293k
  return sbrError;
884
243k
}
885
886
SBR_ERROR sbrDecoder_SetParam(HANDLE_SBRDECODER self, const SBRDEC_PARAM param,
887
1.44M
                              const INT value) {
888
1.44M
  SBR_ERROR errorStatus = SBRDEC_OK;
889
890
  /* configure the subsystems */
891
1.44M
  switch (param) {
892
352k
    case SBR_SYSTEM_BITSTREAM_DELAY:
893
352k
      if (value < 0 || value > (1)) {
894
0
        errorStatus = SBRDEC_SET_PARAM_FAIL;
895
0
        break;
896
0
      }
897
352k
      if (self == NULL) {
898
0
        errorStatus = SBRDEC_NOT_INITIALIZED;
899
352k
      } else {
900
352k
        self->numDelayFrames = (UCHAR)value;
901
352k
      }
902
352k
      break;
903
451k
    case SBR_QMF_MODE:
904
451k
      if (self == NULL) {
905
0
        errorStatus = SBRDEC_NOT_INITIALIZED;
906
451k
      } else {
907
451k
        if (value == 1) {
908
121k
          self->flags |= SBRDEC_LOW_POWER;
909
330k
        } else {
910
330k
          self->flags &= ~SBRDEC_LOW_POWER;
911
330k
        }
912
451k
      }
913
451k
      break;
914
34.5k
    case SBR_LD_QMF_TIME_ALIGN:
915
34.5k
      if (self == NULL) {
916
0
        errorStatus = SBRDEC_NOT_INITIALIZED;
917
34.5k
      } else {
918
34.5k
        if (value == 1) {
919
1.99k
          self->flags |= SBRDEC_LD_MPS_QMF;
920
32.5k
        } else {
921
32.5k
          self->flags &= ~SBRDEC_LD_MPS_QMF;
922
32.5k
        }
923
34.5k
      }
924
34.5k
      break;
925
297k
    case SBR_FLUSH_DATA:
926
297k
      if (value != 0) {
927
30
        if (self == NULL) {
928
0
          errorStatus = SBRDEC_NOT_INITIALIZED;
929
30
        } else {
930
30
          self->flags |= SBRDEC_FLUSH;
931
30
        }
932
30
      }
933
297k
      break;
934
0
    case SBR_CLEAR_HISTORY:
935
0
      if (value != 0) {
936
0
        if (self == NULL) {
937
0
          errorStatus = SBRDEC_NOT_INITIALIZED;
938
0
        } else {
939
0
          self->flags |= SBRDEC_FORCE_RESET;
940
0
        }
941
0
      }
942
0
      break;
943
7.98k
    case SBR_BS_INTERRUPTION: {
944
7.98k
      int elementIndex;
945
946
7.98k
      if (self == NULL) {
947
0
        errorStatus = SBRDEC_NOT_INITIALIZED;
948
0
        break;
949
0
      }
950
951
      /* Loop over SBR elements */
952
14.6k
      for (elementIndex = 0; elementIndex < self->numSbrElements;
953
7.98k
           elementIndex++) {
954
6.66k
        if (self->pSbrElement[elementIndex] != NULL) {
955
6.65k
          HANDLE_SBR_HEADER_DATA hSbrHeader;
956
6.65k
          int headerIndex =
957
6.65k
              getHeaderSlot(self->pSbrElement[elementIndex]->useFrameSlot,
958
6.65k
                            self->pSbrElement[elementIndex]->useHeaderSlot);
959
960
6.65k
          hSbrHeader = &(self->sbrHeader[elementIndex][headerIndex]);
961
962
          /* Set sync state UPSAMPLING for the corresponding slot.
963
             This switches off bitstream parsing until a new header arrives. */
964
6.65k
          if (hSbrHeader->syncState != SBR_NOT_INITIALIZED) {
965
6.56k
            hSbrHeader->syncState = UPSAMPLING;
966
6.56k
            hSbrHeader->status |= SBRDEC_HDR_STAT_UPDATE;
967
6.56k
          }
968
6.65k
        }
969
6.66k
      }
970
7.98k
    } break;
971
972
297k
    case SBR_SKIP_QMF:
973
297k
      if (self == NULL) {
974
0
        errorStatus = SBRDEC_NOT_INITIALIZED;
975
297k
      } else {
976
297k
        if (value == 1) {
977
13.8k
          self->flags |= SBRDEC_SKIP_QMF_ANA;
978
283k
        } else {
979
283k
          self->flags &= ~SBRDEC_SKIP_QMF_ANA;
980
283k
        }
981
297k
        if (value == 2) {
982
120k
          self->flags |= SBRDEC_SKIP_QMF_SYN;
983
176k
        } else {
984
176k
          self->flags &= ~SBRDEC_SKIP_QMF_SYN;
985
176k
        }
986
297k
      }
987
297k
      break;
988
0
    default:
989
0
      errorStatus = SBRDEC_SET_PARAM_FAIL;
990
0
      break;
991
1.44M
  } /* switch(param) */
992
993
1.44M
  return (errorStatus);
994
1.44M
}
995
996
static SBRDEC_DRC_CHANNEL *sbrDecoder_drcGetChannel(
997
492k
    const HANDLE_SBRDECODER self, const INT channel) {
998
492k
  SBRDEC_DRC_CHANNEL *pSbrDrcChannelData = NULL;
999
492k
  int elementIndex, elChanIdx = 0, numCh = 0;
1000
1001
1.16M
  for (elementIndex = 0; (elementIndex < (8)) && (numCh <= channel);
1002
680k
       elementIndex++) {
1003
680k
    SBR_DECODER_ELEMENT *pSbrElement = self->pSbrElement[elementIndex];
1004
680k
    int c, elChannels;
1005
1006
680k
    elChanIdx = 0;
1007
680k
    if (pSbrElement == NULL) break;
1008
1009
    /* Determine amount of channels for this element */
1010
674k
    switch (pSbrElement->elementID) {
1011
377k
      case ID_CPE:
1012
377k
        elChannels = 2;
1013
377k
        break;
1014
25.7k
      case ID_LFE:
1015
297k
      case ID_SCE:
1016
297k
        elChannels = 1;
1017
297k
        break;
1018
0
      case ID_NONE:
1019
0
      default:
1020
0
        elChannels = 0;
1021
0
        break;
1022
674k
    }
1023
1024
    /* Limit with actual allocated element channels */
1025
674k
    elChannels = fMin(elChannels, pSbrElement->nChannels);
1026
1027
1.58M
    for (c = 0; (c < elChannels) && (numCh <= channel); c++) {
1028
907k
      if (pSbrElement->pSbrChannel[elChanIdx] != NULL) {
1029
907k
        numCh++;
1030
907k
        elChanIdx++;
1031
907k
      }
1032
907k
    }
1033
674k
  }
1034
492k
  elementIndex -= 1;
1035
492k
  elChanIdx -= 1;
1036
1037
492k
  if (elChanIdx < 0 || elementIndex < 0) {
1038
5.13k
    return NULL;
1039
5.13k
  }
1040
1041
487k
  if (self->pSbrElement[elementIndex] != NULL) {
1042
487k
    if (self->pSbrElement[elementIndex]->pSbrChannel[elChanIdx] != NULL) {
1043
487k
      pSbrDrcChannelData = &self->pSbrElement[elementIndex]
1044
487k
                                ->pSbrChannel[elChanIdx]
1045
487k
                                ->SbrDec.sbrDrcChannel;
1046
487k
    }
1047
487k
  }
1048
1049
487k
  return (pSbrDrcChannelData);
1050
492k
}
1051
1052
SBR_ERROR sbrDecoder_drcFeedChannel(HANDLE_SBRDECODER self, INT ch,
1053
                                    UINT numBands, FIXP_DBL *pNextFact_mag,
1054
                                    INT nextFact_exp,
1055
                                    SHORT drcInterpolationScheme,
1056
172k
                                    UCHAR winSequence, USHORT *pBandTop) {
1057
172k
  SBRDEC_DRC_CHANNEL *pSbrDrcChannelData = NULL;
1058
172k
  int band, isValidData = 0;
1059
1060
172k
  if (self == NULL) {
1061
0
    return SBRDEC_NOT_INITIALIZED;
1062
0
  }
1063
172k
  if (ch > (8) || pNextFact_mag == NULL) {
1064
0
    return SBRDEC_SET_PARAM_FAIL;
1065
0
  }
1066
1067
  /* Search for gain values different to 1.0f */
1068
334k
  for (band = 0; band < (int)numBands; band += 1) {
1069
174k
    if (!((pNextFact_mag[band] == FL2FXCONST_DBL(0.5)) &&
1070
161k
          (nextFact_exp == 1)) &&
1071
12.3k
        !((pNextFact_mag[band] == (FIXP_DBL)MAXVAL_DBL) &&
1072
12.3k
          (nextFact_exp == 0))) {
1073
12.3k
      isValidData = 1;
1074
12.3k
      break;
1075
12.3k
    }
1076
174k
  }
1077
1078
  /* Find the right SBR channel */
1079
172k
  pSbrDrcChannelData = sbrDecoder_drcGetChannel(self, ch);
1080
1081
172k
  if (pSbrDrcChannelData != NULL) {
1082
172k
    if (pSbrDrcChannelData->enable ||
1083
159k
        isValidData) { /* Activate processing only with real and valid data */
1084
15.6k
      int i;
1085
1086
15.6k
      pSbrDrcChannelData->enable = 1;
1087
15.6k
      pSbrDrcChannelData->numBandsNext = numBands;
1088
1089
15.6k
      pSbrDrcChannelData->winSequenceNext = winSequence;
1090
15.6k
      pSbrDrcChannelData->drcInterpolationSchemeNext = drcInterpolationScheme;
1091
15.6k
      pSbrDrcChannelData->nextFact_exp = nextFact_exp;
1092
1093
79.7k
      for (i = 0; i < (int)numBands; i++) {
1094
64.0k
        pSbrDrcChannelData->bandTopNext[i] = pBandTop[i];
1095
64.0k
        pSbrDrcChannelData->nextFact_mag[i] = pNextFact_mag[i];
1096
64.0k
      }
1097
15.6k
    }
1098
172k
  }
1099
1100
172k
  return SBRDEC_OK;
1101
172k
}
1102
1103
363k
void sbrDecoder_drcDisable(HANDLE_SBRDECODER self, INT ch) {
1104
363k
  SBRDEC_DRC_CHANNEL *pSbrDrcChannelData = NULL;
1105
1106
363k
  if ((self == NULL) || (ch > (8)) || (self->numSbrElements == 0) ||
1107
319k
      (self->numSbrChannels == 0)) {
1108
44.3k
    return;
1109
44.3k
  }
1110
1111
  /* Find the right SBR channel */
1112
319k
  pSbrDrcChannelData = sbrDecoder_drcGetChannel(self, ch);
1113
1114
319k
  if (pSbrDrcChannelData != NULL) {
1115
314k
    sbrDecoder_drcInitChannel(pSbrDrcChannelData);
1116
314k
  }
1117
319k
}
1118
1119
SBR_ERROR sbrDecoder_Parse(HANDLE_SBRDECODER self, HANDLE_FDK_BITSTREAM hBs,
1120
                           UCHAR *pDrmBsBuffer, USHORT drmBsBufferSize,
1121
                           int *count, int bsPayLen, int crcFlag,
1122
                           MP4_ELEMENT_ID prevElement, int elementIndex,
1123
353k
                           UINT acFlags, UINT acElFlags[]) {
1124
353k
  SBR_DECODER_ELEMENT *hSbrElement = NULL;
1125
353k
  HANDLE_SBR_HEADER_DATA hSbrHeader = NULL;
1126
353k
  HANDLE_SBR_CHANNEL *pSbrChannel;
1127
1128
353k
  SBR_FRAME_DATA *hFrameDataLeft = NULL;
1129
353k
  SBR_FRAME_DATA *hFrameDataRight = NULL;
1130
353k
  SBR_FRAME_DATA frameDataLeftCopy;
1131
353k
  SBR_FRAME_DATA frameDataRightCopy;
1132
1133
353k
  SBR_ERROR errorStatus = SBRDEC_OK;
1134
353k
  SBR_HEADER_STATUS headerStatus = HEADER_NOT_PRESENT;
1135
1136
353k
  INT startPos = FDKgetValidBits(hBs);
1137
353k
  FDK_CRCINFO crcInfo;
1138
353k
  INT crcReg = 0;
1139
353k
  USHORT sbrCrc = 0;
1140
353k
  UINT crcPoly;
1141
353k
  UINT crcStartValue = 0;
1142
353k
  UINT crcLen;
1143
1144
353k
  HANDLE_FDK_BITSTREAM hBsOriginal = hBs;
1145
353k
  FDK_BITSTREAM bsBwd;
1146
1147
353k
  const int fGlobalIndependencyFlag = acFlags & AC_INDEP;
1148
353k
  const int bs_pvc = acElFlags[elementIndex] & AC_EL_USAC_PVC;
1149
353k
  const int bs_interTes = acElFlags[elementIndex] & AC_EL_USAC_ITES;
1150
353k
  int stereo;
1151
353k
  int fDoDecodeSbrData = 1;
1152
353k
  int alignBits = 0;
1153
1154
353k
  int lastSlot, lastHdrSlot = 0, thisHdrSlot = 0;
1155
1156
353k
  if (*count <= 0) {
1157
44.7k
    setFrameErrorFlag(self->pSbrElement[elementIndex], FRAME_ERROR);
1158
44.7k
    return SBRDEC_OK;
1159
44.7k
  }
1160
1161
  /* SBR sanity checks */
1162
308k
  if (self == NULL) {
1163
0
    errorStatus = SBRDEC_NOT_INITIALIZED;
1164
0
    goto bail;
1165
0
  }
1166
1167
  /* Reverse bits of DRM SBR payload */
1168
308k
  if ((self->flags & SBRDEC_SYNTAX_DRM) && *count > 0) {
1169
0
    int dataBytes, dataBits;
1170
1171
0
    FDK_ASSERT(drmBsBufferSize >= (512));
1172
0
    dataBits = *count;
1173
1174
0
    if (dataBits > ((512) * 8)) {
1175
      /* do not flip more data than needed */
1176
0
      dataBits = (512) * 8;
1177
0
    }
1178
1179
0
    dataBytes = (dataBits + 7) >> 3;
1180
1181
0
    int j;
1182
1183
0
    if ((j = (int)FDKgetValidBits(hBs)) != 8) {
1184
0
      FDKpushBiDirectional(hBs, (j - 8));
1185
0
    }
1186
1187
0
    j = 0;
1188
0
    for (; dataBytes > 0; dataBytes--) {
1189
0
      int i;
1190
0
      UCHAR tmpByte;
1191
0
      UCHAR buffer = 0x00;
1192
1193
0
      tmpByte = (UCHAR)FDKreadBits(hBs, 8);
1194
0
      for (i = 0; i < 4; i++) {
1195
0
        int shift = 2 * i + 1;
1196
0
        buffer |= (tmpByte & (0x08 >> i)) << shift;
1197
0
        buffer |= (tmpByte & (0x10 << i)) >> shift;
1198
0
      }
1199
0
      pDrmBsBuffer[j++] = buffer;
1200
0
      FDKpushBack(hBs, 16);
1201
0
    }
1202
1203
0
    FDKinitBitStream(&bsBwd, pDrmBsBuffer, (512), dataBits, BS_READER);
1204
1205
    /* Use reversed data */
1206
0
    hBs = &bsBwd;
1207
0
    bsPayLen = *count;
1208
0
  }
1209
1210
  /* Remember start position of  SBR element */
1211
308k
  startPos = FDKgetValidBits(hBs);
1212
1213
  /* SBR sanity checks */
1214
308k
  if (self->pSbrElement[elementIndex] == NULL) {
1215
0
    errorStatus = SBRDEC_NOT_INITIALIZED;
1216
0
    goto bail;
1217
0
  }
1218
308k
  hSbrElement = self->pSbrElement[elementIndex];
1219
1220
308k
  lastSlot = (hSbrElement->useFrameSlot > 0) ? hSbrElement->useFrameSlot - 1
1221
308k
                                             : self->numDelayFrames;
1222
308k
  lastHdrSlot = hSbrElement->useHeaderSlot[lastSlot];
1223
308k
  thisHdrSlot = getHeaderSlot(
1224
308k
      hSbrElement->useFrameSlot,
1225
308k
      hSbrElement->useHeaderSlot); /* Get a free header slot not used by
1226
                                      frames not processed yet. */
1227
1228
  /* Assign the free slot to store a new header if there is one. */
1229
308k
  hSbrHeader = &self->sbrHeader[elementIndex][thisHdrSlot];
1230
1231
308k
  pSbrChannel = hSbrElement->pSbrChannel;
1232
308k
  stereo = (hSbrElement->elementID == ID_CPE) ? 1 : 0;
1233
1234
308k
  hFrameDataLeft = &self->pSbrElement[elementIndex]
1235
308k
                        ->pSbrChannel[0]
1236
308k
                        ->frameData[hSbrElement->useFrameSlot];
1237
308k
  if (stereo) {
1238
116k
    hFrameDataRight = &self->pSbrElement[elementIndex]
1239
116k
                           ->pSbrChannel[1]
1240
116k
                           ->frameData[hSbrElement->useFrameSlot];
1241
116k
  }
1242
1243
  /* store frameData; new parsed frameData possibly corrupted */
1244
308k
  FDKmemcpy(&frameDataLeftCopy, hFrameDataLeft, sizeof(SBR_FRAME_DATA));
1245
308k
  if (stereo) {
1246
116k
    FDKmemcpy(&frameDataRightCopy, hFrameDataRight, sizeof(SBR_FRAME_DATA));
1247
116k
  }
1248
1249
  /* reset PS flag; will be set after PS was found */
1250
308k
  self->flags &= ~SBRDEC_PS_DECODED;
1251
1252
308k
  if (hSbrHeader->status & SBRDEC_HDR_STAT_UPDATE) {
1253
    /* Got a new header from extern (e.g. from an ASC) */
1254
26.5k
    headerStatus = HEADER_OK;
1255
26.5k
    hSbrHeader->status &= ~SBRDEC_HDR_STAT_UPDATE;
1256
281k
  } else if (thisHdrSlot != lastHdrSlot) {
1257
    /* Copy the last header into this slot otherwise the
1258
       header compare will trigger more HEADER_RESETs than needed. */
1259
38.3k
    copySbrHeader(hSbrHeader, &self->sbrHeader[elementIndex][lastHdrSlot]);
1260
38.3k
  }
1261
1262
  /*
1263
     Check if bit stream data is valid and matches the element context
1264
  */
1265
308k
  if (((prevElement != ID_SCE) && (prevElement != ID_CPE)) ||
1266
307k
      prevElement != hSbrElement->elementID) {
1267
    /* In case of LFE we also land here, since there is no LFE SBR element (do
1268
     * upsampling only) */
1269
1.21k
    fDoDecodeSbrData = 0;
1270
1.21k
  }
1271
1272
308k
  if (fDoDecodeSbrData) {
1273
307k
    if ((INT)FDKgetValidBits(hBs) <= 0) {
1274
5
      fDoDecodeSbrData = 0;
1275
5
    }
1276
307k
  }
1277
1278
  /*
1279
     SBR CRC-check
1280
  */
1281
308k
  if (fDoDecodeSbrData) {
1282
307k
    if (crcFlag) {
1283
40.7k
      switch (self->coreCodec) {
1284
0
        case AOT_DRM_AAC:
1285
0
        case AOT_DRM_SURROUND:
1286
0
          crcPoly = 0x001d;
1287
0
          crcLen = 8;
1288
0
          crcStartValue = 0x000000ff;
1289
0
          break;
1290
40.7k
        default:
1291
40.7k
          crcPoly = 0x0633;
1292
40.7k
          crcLen = 10;
1293
40.7k
          crcStartValue = 0x00000000;
1294
40.7k
          break;
1295
40.7k
      }
1296
40.7k
      sbrCrc = (USHORT)FDKreadBits(hBs, crcLen);
1297
      /* Setup CRC decoder */
1298
40.7k
      FDKcrcInit(&crcInfo, crcPoly, crcStartValue, crcLen);
1299
      /* Start CRC region */
1300
40.7k
      crcReg = FDKcrcStartReg(&crcInfo, hBs, 0);
1301
40.7k
    }
1302
307k
  } /* if (fDoDecodeSbrData) */
1303
1304
  /*
1305
     Read in the header data and issue a reset if change occured
1306
  */
1307
308k
  if (fDoDecodeSbrData) {
1308
307k
    int sbrHeaderPresent;
1309
1310
307k
    if (self->flags & (SBRDEC_SYNTAX_RSVD50 | SBRDEC_SYNTAX_USAC)) {
1311
221k
      SBR_HEADER_DATA_BS_INFO newSbrInfo;
1312
221k
      int sbrInfoPresent;
1313
1314
221k
      if (bs_interTes) {
1315
69.5k
        self->flags |= SBRDEC_USAC_ITES;
1316
151k
      } else {
1317
151k
        self->flags &= ~SBRDEC_USAC_ITES;
1318
151k
      }
1319
1320
221k
      if (fGlobalIndependencyFlag) {
1321
171k
        self->flags |= SBRDEC_USAC_INDEP;
1322
171k
        sbrInfoPresent = 1;
1323
171k
        sbrHeaderPresent = 1;
1324
171k
      } else {
1325
49.8k
        self->flags &= ~SBRDEC_USAC_INDEP;
1326
49.8k
        sbrInfoPresent = FDKreadBit(hBs);
1327
49.8k
        if (sbrInfoPresent) {
1328
8.06k
          sbrHeaderPresent = FDKreadBit(hBs);
1329
41.7k
        } else {
1330
41.7k
          sbrHeaderPresent = 0;
1331
41.7k
        }
1332
49.8k
      }
1333
1334
221k
      if (sbrInfoPresent) {
1335
179k
        newSbrInfo.ampResolution = FDKreadBit(hBs);
1336
179k
        newSbrInfo.xover_band = FDKreadBits(hBs, 4);
1337
179k
        newSbrInfo.sbr_preprocessing = FDKreadBit(hBs);
1338
179k
        if (bs_pvc) {
1339
107k
          newSbrInfo.pvc_mode = FDKreadBits(hBs, 2);
1340
          /* bs_pvc_mode: 0 -> no PVC, 1 -> PVC mode 1, 2 -> PVC mode 2, 3 ->
1341
           * reserved */
1342
107k
          if (newSbrInfo.pvc_mode > 2) {
1343
10.3k
            headerStatus = HEADER_ERROR;
1344
10.3k
          }
1345
107k
          if (stereo && newSbrInfo.pvc_mode > 0) {
1346
            /* bs_pvc is always transmitted but pvc_mode is set to zero in case
1347
             * of stereo SBR. The config might be wrong but we cannot tell for
1348
             * sure. */
1349
11.9k
            newSbrInfo.pvc_mode = 0;
1350
11.9k
          }
1351
107k
        } else {
1352
72.6k
          newSbrInfo.pvc_mode = 0;
1353
72.6k
        }
1354
179k
        if (headerStatus != HEADER_ERROR) {
1355
169k
          if (FDKmemcmp(&hSbrHeader->bs_info, &newSbrInfo,
1356
169k
                        sizeof(SBR_HEADER_DATA_BS_INFO))) {
1357
            /* in case of ampResolution and preprocessing change no full reset
1358
             * required    */
1359
            /* HEADER reset would trigger HBE transposer reset which breaks
1360
             * eSbr_3_Eaa.mp4 */
1361
68.1k
            if ((hSbrHeader->bs_info.pvc_mode != newSbrInfo.pvc_mode) ||
1362
59.0k
                (hSbrHeader->bs_info.xover_band != newSbrInfo.xover_band)) {
1363
59.0k
              headerStatus = HEADER_RESET;
1364
59.0k
            } else {
1365
9.02k
              headerStatus = HEADER_OK;
1366
9.02k
            }
1367
1368
68.1k
            hSbrHeader->bs_info = newSbrInfo;
1369
101k
          } else {
1370
101k
            headerStatus = HEADER_OK;
1371
101k
          }
1372
169k
        }
1373
179k
      }
1374
221k
      if (headerStatus == HEADER_ERROR) {
1375
        /* Corrupt SBR info data, do not decode and switch to UPSAMPLING */
1376
10.3k
        hSbrHeader->syncState = hSbrHeader->syncState > UPSAMPLING
1377
10.3k
                                    ? UPSAMPLING
1378
10.3k
                                    : hSbrHeader->syncState;
1379
10.3k
        fDoDecodeSbrData = 0;
1380
10.3k
        sbrHeaderPresent = 0;
1381
10.3k
      }
1382
1383
221k
      if (sbrHeaderPresent && fDoDecodeSbrData) {
1384
166k
        int useDfltHeader;
1385
1386
166k
        useDfltHeader = FDKreadBit(hBs);
1387
1388
166k
        if (useDfltHeader) {
1389
91.9k
          sbrHeaderPresent = 0;
1390
91.9k
          if (FDKmemcmp(&hSbrHeader->bs_data, &hSbrHeader->bs_dflt,
1391
91.9k
                        sizeof(SBR_HEADER_DATA_BS)) ||
1392
73.8k
              hSbrHeader->syncState != SBR_ACTIVE) {
1393
20.6k
            hSbrHeader->bs_data = hSbrHeader->bs_dflt;
1394
20.6k
            headerStatus = HEADER_RESET;
1395
20.6k
          }
1396
91.9k
        }
1397
166k
      }
1398
221k
    } else {
1399
85.5k
      sbrHeaderPresent = FDKreadBit(hBs);
1400
85.5k
    }
1401
1402
307k
    if (sbrHeaderPresent) {
1403
129k
      headerStatus = sbrGetHeaderData(hSbrHeader, hBs, self->flags, 1, 0);
1404
129k
    }
1405
1406
307k
    if (headerStatus == HEADER_RESET) {
1407
119k
      errorStatus = sbrDecoder_HeaderUpdate(
1408
119k
          self, hSbrHeader, headerStatus, pSbrChannel, hSbrElement->nChannels);
1409
1410
119k
      if (errorStatus == SBRDEC_OK) {
1411
88.8k
        hSbrHeader->syncState = SBR_HEADER;
1412
88.8k
      } else {
1413
30.7k
        hSbrHeader->syncState = SBR_NOT_INITIALIZED;
1414
30.7k
        headerStatus = HEADER_ERROR;
1415
30.7k
      }
1416
119k
    }
1417
1418
307k
    if (errorStatus != SBRDEC_OK) {
1419
30.7k
      fDoDecodeSbrData = 0;
1420
30.7k
    }
1421
307k
  } /* if (fDoDecodeSbrData) */
1422
1423
  /*
1424
    Print debugging output only if state has changed
1425
  */
1426
1427
  /* read frame data */
1428
308k
  if ((hSbrHeader->syncState >= SBR_HEADER) && fDoDecodeSbrData) {
1429
259k
    int sbrFrameOk;
1430
    /* read the SBR element data */
1431
259k
    if (!stereo && (self->hParametricStereoDec != NULL)) {
1432
      /* update slot index for PS bitstream parsing */
1433
30.2k
      self->hParametricStereoDec->bsLastSlot =
1434
30.2k
          self->hParametricStereoDec->bsReadSlot;
1435
30.2k
      self->hParametricStereoDec->bsReadSlot = hSbrElement->useFrameSlot;
1436
30.2k
    }
1437
259k
    sbrFrameOk = sbrGetChannelElement(
1438
259k
        hSbrHeader, hFrameDataLeft, (stereo) ? hFrameDataRight : NULL,
1439
259k
        &pSbrChannel[0]->prevFrameData,
1440
259k
        pSbrChannel[0]->SbrDec.PvcStaticData.pvc_mode_last, hBs,
1441
259k
        (stereo) ? NULL : self->hParametricStereoDec, self->flags,
1442
259k
        self->pSbrElement[elementIndex]->transposerSettings.overlap);
1443
1444
259k
    if (!sbrFrameOk) {
1445
18.8k
      fDoDecodeSbrData = 0;
1446
240k
    } else {
1447
240k
      INT valBits;
1448
1449
240k
      if (bsPayLen > 0) {
1450
31.7k
        valBits = bsPayLen - ((INT)startPos - (INT)FDKgetValidBits(hBs));
1451
208k
      } else {
1452
208k
        valBits = (INT)FDKgetValidBits(hBs);
1453
208k
      }
1454
1455
      /* sanity check of remaining bits */
1456
240k
      if (valBits < 0) {
1457
29.5k
        fDoDecodeSbrData = 0;
1458
210k
      } else {
1459
210k
        switch (self->coreCodec) {
1460
0
          case AOT_SBR:
1461
0
          case AOT_PS:
1462
6.87k
          case AOT_AAC_LC: {
1463
            /* This sanity check is only meaningful with General Audio
1464
             * bitstreams */
1465
6.87k
            alignBits = valBits & 0x7;
1466
1467
6.87k
            if (valBits > alignBits) {
1468
825
              fDoDecodeSbrData = 0;
1469
825
            }
1470
6.87k
          } break;
1471
204k
          default:
1472
            /* No sanity check available */
1473
204k
            break;
1474
210k
        }
1475
210k
      }
1476
240k
    }
1477
259k
  } else {
1478
    /* The returned bit count will not be the actual payload size since we did
1479
       not parse the frame data. Return an error so that the caller can react
1480
       respectively. */
1481
48.9k
    errorStatus = SBRDEC_PARSE_ERROR;
1482
48.9k
  }
1483
1484
308k
  if (crcFlag && (hSbrHeader->syncState >= SBR_HEADER) && fDoDecodeSbrData) {
1485
18.9k
    FDKpushFor(hBs, alignBits);
1486
18.9k
    FDKcrcEndReg(&crcInfo, hBs, crcReg); /* End CRC region */
1487
18.9k
    FDKpushBack(hBs, alignBits);
1488
    /* Check CRC */
1489
18.9k
    if ((FDKcrcGetCRC(&crcInfo) ^ crcStartValue) != sbrCrc) {
1490
17.0k
      fDoDecodeSbrData = 0;
1491
17.0k
      if (headerStatus != HEADER_NOT_PRESENT) {
1492
16.1k
        headerStatus = HEADER_ERROR;
1493
16.1k
        hSbrHeader->syncState = SBR_NOT_INITIALIZED;
1494
16.1k
      }
1495
17.0k
    }
1496
18.9k
  }
1497
1498
308k
  if (!fDoDecodeSbrData) {
1499
    /* Set error flag for this slot to trigger concealment */
1500
108k
    setFrameErrorFlag(self->pSbrElement[elementIndex], FRAME_ERROR);
1501
    /* restore old frameData for concealment */
1502
108k
    FDKmemcpy(hFrameDataLeft, &frameDataLeftCopy, sizeof(SBR_FRAME_DATA));
1503
108k
    if (stereo) {
1504
44.6k
      FDKmemcpy(hFrameDataRight, &frameDataRightCopy, sizeof(SBR_FRAME_DATA));
1505
44.6k
    }
1506
108k
    errorStatus = SBRDEC_PARSE_ERROR;
1507
199k
  } else {
1508
    /* Everything seems to be ok so clear the error flag */
1509
199k
    setFrameErrorFlag(self->pSbrElement[elementIndex], FRAME_OK);
1510
199k
  }
1511
1512
308k
  if (!stereo) {
1513
    /* Turn coupling off explicitely to avoid access to absent right frame data
1514
       that might occur with corrupt bitstreams. */
1515
192k
    hFrameDataLeft->coupling = COUPLING_OFF;
1516
192k
  }
1517
1518
308k
bail:
1519
1520
308k
  if (self != NULL) {
1521
308k
    if (self->flags & SBRDEC_SYNTAX_DRM) {
1522
0
      hBs = hBsOriginal;
1523
0
    }
1524
1525
308k
    if (errorStatus != SBRDEC_NOT_INITIALIZED) {
1526
308k
      int useOldHdr =
1527
308k
          ((headerStatus == HEADER_NOT_PRESENT) ||
1528
251k
           (headerStatus == HEADER_ERROR) ||
1529
194k
           (headerStatus == HEADER_RESET && errorStatus == SBRDEC_PARSE_ERROR))
1530
308k
              ? 1
1531
308k
              : 0;
1532
1533
308k
      if (!useOldHdr && (thisHdrSlot != lastHdrSlot) && (hSbrHeader != NULL)) {
1534
9.26k
        useOldHdr |=
1535
9.26k
            (compareSbrHeader(hSbrHeader,
1536
9.26k
                              &self->sbrHeader[elementIndex][lastHdrSlot]) == 0)
1537
9.26k
                ? 1
1538
9.26k
                : 0;
1539
9.26k
      }
1540
1541
308k
      if (hSbrElement != NULL) {
1542
308k
        if (useOldHdr != 0) {
1543
          /* Use the old header for this frame */
1544
152k
          hSbrElement->useHeaderSlot[hSbrElement->useFrameSlot] = lastHdrSlot;
1545
155k
        } else {
1546
          /* Use the new header for this frame */
1547
155k
          hSbrElement->useHeaderSlot[hSbrElement->useFrameSlot] = thisHdrSlot;
1548
155k
        }
1549
1550
        /* Move frame pointer to the next slot which is up to be decoded/applied
1551
         * next */
1552
308k
        hSbrElement->useFrameSlot =
1553
308k
            (hSbrElement->useFrameSlot + 1) % (self->numDelayFrames + 1);
1554
308k
      }
1555
308k
    }
1556
308k
  }
1557
1558
308k
  *count -= startPos - (INT)FDKgetValidBits(hBs);
1559
1560
308k
  return errorStatus;
1561
308k
}
1562
1563
/**
1564
 * \brief Render one SBR element into time domain signal.
1565
 * \param self SBR decoder handle
1566
 * \param timeData pointer to output buffer
1567
 * \param channelMapping pointer to UCHAR array where next 2 channel offsets are
1568
 * stored.
1569
 * \param elementIndex enumerating index of the SBR element to render.
1570
 * \param numInChannels number of channels from core coder.
1571
 * \param numOutChannels pointer to a location to return number of output
1572
 * channels.
1573
 * \param psPossible flag indicating if PS is possible or not.
1574
 * \return SBRDEC_OK if successfull, else error code
1575
 */
1576
static SBR_ERROR sbrDecoder_DecodeElement(
1577
    HANDLE_SBRDECODER self, LONG *input, LONG *timeData, const int timeDataSize,
1578
    const FDK_channelMapDescr *const mapDescr, const int mapIdx,
1579
    int channelIndex, const int elementIndex, const int numInChannels,
1580
342k
    int *numOutChannels, const int psPossible) {
1581
342k
  SBR_DECODER_ELEMENT *hSbrElement = self->pSbrElement[elementIndex];
1582
342k
  HANDLE_SBR_CHANNEL *pSbrChannel =
1583
342k
      self->pSbrElement[elementIndex]->pSbrChannel;
1584
342k
  HANDLE_SBR_HEADER_DATA hSbrHeader =
1585
342k
      &self->sbrHeader[elementIndex]
1586
342k
                      [hSbrElement->useHeaderSlot[hSbrElement->useFrameSlot]];
1587
342k
  HANDLE_PS_DEC h_ps_d = self->hParametricStereoDec;
1588
1589
  /* get memory for frame data from scratch */
1590
342k
  SBR_FRAME_DATA *hFrameDataLeft = NULL;
1591
342k
  SBR_FRAME_DATA *hFrameDataRight = NULL;
1592
1593
342k
  SBR_ERROR errorStatus = SBRDEC_OK;
1594
1595
342k
  INT strideOut, offset0 = 255, offset0_block = 0, offset1 = 255,
1596
342k
                 offset1_block = 0;
1597
342k
  INT codecFrameSize = self->codecFrameSize;
1598
1599
342k
  int stereo = (hSbrElement->elementID == ID_CPE) ? 1 : 0;
1600
342k
  int numElementChannels =
1601
342k
      hSbrElement
1602
342k
          ->nChannels; /* Number of channels of the current SBR element */
1603
1604
342k
  hFrameDataLeft =
1605
342k
      &hSbrElement->pSbrChannel[0]->frameData[hSbrElement->useFrameSlot];
1606
342k
  if (stereo) {
1607
144k
    hFrameDataRight =
1608
144k
        &hSbrElement->pSbrChannel[1]->frameData[hSbrElement->useFrameSlot];
1609
144k
  }
1610
1611
342k
  if (self->flags & SBRDEC_FLUSH) {
1612
30
    if (self->numFlushedFrames > self->numDelayFrames) {
1613
30
      int hdrIdx;
1614
      /* No valid SBR payload available, hence switch to upsampling (in all
1615
       * headers) */
1616
90
      for (hdrIdx = 0; hdrIdx < ((1) + 1); hdrIdx += 1) {
1617
60
        if (self->sbrHeader[elementIndex][hdrIdx].syncState > UPSAMPLING) {
1618
24
          self->sbrHeader[elementIndex][hdrIdx].syncState = UPSAMPLING;
1619
24
        }
1620
60
      }
1621
30
    } else {
1622
      /* Move frame pointer to the next slot which is up to be decoded/applied
1623
       * next */
1624
0
      hSbrElement->useFrameSlot =
1625
0
          (hSbrElement->useFrameSlot + 1) % (self->numDelayFrames + 1);
1626
      /* Update header and frame data pointer because they have already been set
1627
       */
1628
0
      hSbrHeader =
1629
0
          &self->sbrHeader[elementIndex]
1630
0
                          [hSbrElement
1631
0
                               ->useHeaderSlot[hSbrElement->useFrameSlot]];
1632
0
      hFrameDataLeft =
1633
0
          &hSbrElement->pSbrChannel[0]->frameData[hSbrElement->useFrameSlot];
1634
0
      if (stereo) {
1635
0
        hFrameDataRight =
1636
0
            &hSbrElement->pSbrChannel[1]->frameData[hSbrElement->useFrameSlot];
1637
0
      }
1638
0
    }
1639
30
  }
1640
1641
  /* Update the header error flag */
1642
342k
  hSbrHeader->frameErrorFlag =
1643
342k
      hSbrElement->frameErrorFlag[hSbrElement->useFrameSlot];
1644
1645
  /*
1646
     Prepare filterbank for upsampling if no valid bit stream data is available.
1647
   */
1648
342k
  if (hSbrHeader->syncState == SBR_NOT_INITIALIZED) {
1649
50.3k
    errorStatus =
1650
50.3k
        initHeaderData(hSbrHeader, self->sampleRateIn, self->sampleRateOut,
1651
50.3k
                       self->downscaleFactor, codecFrameSize, self->flags,
1652
50.3k
                       1 /* SET_DEFAULT_HDR */
1653
50.3k
        );
1654
1655
50.3k
    if (errorStatus != SBRDEC_OK) {
1656
0
      return errorStatus;
1657
0
    }
1658
1659
50.3k
    hSbrHeader->syncState = UPSAMPLING;
1660
1661
50.3k
    errorStatus = sbrDecoder_HeaderUpdate(self, hSbrHeader, HEADER_NOT_PRESENT,
1662
50.3k
                                          pSbrChannel, hSbrElement->nChannels);
1663
1664
50.3k
    if (errorStatus != SBRDEC_OK) {
1665
0
      hSbrHeader->syncState = SBR_NOT_INITIALIZED;
1666
0
      return errorStatus;
1667
0
    }
1668
50.3k
  }
1669
1670
  /* reset */
1671
342k
  if (hSbrHeader->status & SBRDEC_HDR_STAT_RESET) {
1672
177k
    int ch;
1673
177k
    int applySbrProc = (hSbrHeader->syncState == SBR_ACTIVE ||
1674
177k
                        (hSbrHeader->frameErrorFlag == 0 &&
1675
108k
                         hSbrHeader->syncState == SBR_HEADER));
1676
461k
    for (ch = 0; ch < numElementChannels; ch++) {
1677
283k
      SBR_ERROR errorStatusTmp = SBRDEC_OK;
1678
1679
283k
      errorStatusTmp = resetSbrDec(
1680
283k
          &pSbrChannel[ch]->SbrDec, hSbrHeader, &pSbrChannel[ch]->prevFrameData,
1681
283k
          self->synDownsampleFac, self->flags, pSbrChannel[ch]->frameData);
1682
1683
283k
      if (errorStatusTmp != SBRDEC_OK) {
1684
7.68k
        hSbrHeader->syncState = UPSAMPLING;
1685
7.68k
      }
1686
283k
    }
1687
177k
    if (applySbrProc) {
1688
49.7k
      hSbrHeader->status &= ~SBRDEC_HDR_STAT_RESET;
1689
49.7k
    }
1690
177k
  }
1691
1692
  /* decoding */
1693
342k
  if ((hSbrHeader->syncState == SBR_ACTIVE) ||
1694
182k
      ((hSbrHeader->syncState == SBR_HEADER) &&
1695
209k
       (hSbrHeader->frameErrorFlag == 0))) {
1696
209k
    errorStatus = SBRDEC_OK;
1697
1698
209k
    decodeSbrData(hSbrHeader, hFrameDataLeft, &pSbrChannel[0]->prevFrameData,
1699
209k
                  (stereo) ? hFrameDataRight : NULL,
1700
209k
                  (stereo) ? &pSbrChannel[1]->prevFrameData : NULL);
1701
1702
    /* Now we have a full parameter set and can do parameter
1703
       based concealment instead of plain upsampling. */
1704
209k
    hSbrHeader->syncState = SBR_ACTIVE;
1705
209k
  }
1706
1707
342k
  if (timeDataSize <
1708
342k
      hSbrHeader->numberTimeSlots * hSbrHeader->timeStep *
1709
342k
          self->pQmfDomain->globalConf.nBandsSynthesis *
1710
342k
          (psPossible ? fMax(2, numInChannels) : numInChannels)) {
1711
0
    return SBRDEC_OUTPUT_BUFFER_TOO_SMALL;
1712
0
  }
1713
1714
342k
  {
1715
342k
    self->flags &= ~SBRDEC_PS_DECODED;
1716
342k
    C_ALLOC_SCRATCH_START(pPsScratch, struct PS_DEC_COEFFICIENTS, 1)
1717
1718
    /* decode PS data if available */
1719
342k
    if (h_ps_d != NULL && psPossible && (hSbrHeader->syncState == SBR_ACTIVE)) {
1720
9.09k
      int applyPs = 1;
1721
1722
      /* define which frame delay line slot to process */
1723
9.09k
      h_ps_d->processSlot = hSbrElement->useFrameSlot;
1724
1725
9.09k
      applyPs = DecodePs(h_ps_d, hSbrHeader->frameErrorFlag, pPsScratch);
1726
9.09k
      self->flags |= (applyPs) ? SBRDEC_PS_DECODED : 0;
1727
9.09k
    }
1728
1729
342k
    offset0 = FDK_chMapDescr_getMapValue(mapDescr, channelIndex, mapIdx);
1730
342k
    offset0_block = offset0 * codecFrameSize;
1731
342k
    if (stereo || psPossible) {
1732
      /* the value of offset1 only matters if the condition is true, however if
1733
      it is not true channelIndex+1 may exceed the channel map resutling in an
1734
      error, though the value of offset1 is actually meaningless. This is
1735
      prevented here. */
1736
156k
      offset1 = FDK_chMapDescr_getMapValue(mapDescr, channelIndex + 1, mapIdx);
1737
156k
      offset1_block = offset1 * codecFrameSize;
1738
156k
    }
1739
    /* Set strides for reading and writing */
1740
342k
    if (psPossible)
1741
12.2k
      strideOut = (numInChannels < 2) ? 2 : numInChannels;
1742
330k
    else
1743
330k
      strideOut = numInChannels;
1744
1745
    /* use same buffers for left and right channel and apply PS per timeslot */
1746
    /* Process left channel */
1747
342k
    sbr_dec(&pSbrChannel[0]->SbrDec, input + offset0_block, timeData + offset0,
1748
342k
            (self->flags & SBRDEC_PS_DECODED) ? &pSbrChannel[1]->SbrDec : NULL,
1749
342k
            timeData + offset1, strideOut, hSbrHeader, hFrameDataLeft,
1750
342k
            &pSbrChannel[0]->prevFrameData,
1751
342k
            (hSbrHeader->syncState == SBR_ACTIVE), h_ps_d, self->flags,
1752
342k
            codecFrameSize, self->sbrInDataHeadroom);
1753
1754
342k
    if (stereo) {
1755
      /* Process right channel */
1756
144k
      sbr_dec(&pSbrChannel[1]->SbrDec, input + offset1_block,
1757
144k
              timeData + offset1, NULL, NULL, strideOut, hSbrHeader,
1758
144k
              hFrameDataRight, &pSbrChannel[1]->prevFrameData,
1759
144k
              (hSbrHeader->syncState == SBR_ACTIVE), NULL, self->flags,
1760
144k
              codecFrameSize, self->sbrInDataHeadroom);
1761
144k
    }
1762
1763
342k
    C_ALLOC_SCRATCH_END(pPsScratch, struct PS_DEC_COEFFICIENTS, 1)
1764
342k
  }
1765
1766
342k
  if (h_ps_d != NULL) {
1767
    /* save PS status for next run */
1768
32.6k
    h_ps_d->psDecodedPrv = (self->flags & SBRDEC_PS_DECODED) ? 1 : 0;
1769
32.6k
  }
1770
1771
342k
  if (psPossible && !(self->flags & SBRDEC_SKIP_QMF_SYN)) {
1772
12.2k
    FDK_ASSERT(strideOut > 1);
1773
12.2k
    if (!(self->flags & SBRDEC_PS_DECODED)) {
1774
      /* A decoder which is able to decode PS has to produce a stereo output
1775
       * even if no PS data is available. */
1776
      /* So copy left channel to right channel. */
1777
3.75k
      int copyFrameSize =
1778
3.75k
          codecFrameSize * self->pQmfDomain->QmfDomainOut->fb.no_channels;
1779
3.75k
      copyFrameSize /= self->pQmfDomain->QmfDomainIn->fb.no_channels;
1780
3.75k
      LONG *ptr;
1781
3.75k
      INT i;
1782
3.75k
      FDK_ASSERT(strideOut == 2);
1783
1784
3.75k
      ptr = timeData;
1785
3.73M
      for (i = copyFrameSize >> 1; i--;) {
1786
3.72M
        LONG tmp; /* This temporal variable is required because some compilers
1787
                     can't do *ptr++ = *ptr++ correctly. */
1788
3.72M
        tmp = *ptr++;
1789
3.72M
        *ptr++ = tmp;
1790
3.72M
        tmp = *ptr++;
1791
3.72M
        *ptr++ = tmp;
1792
3.72M
      }
1793
3.75k
    }
1794
12.2k
    *numOutChannels = 2; /* Output minimum two channels when PS is enabled. */
1795
12.2k
  }
1796
1797
342k
  return errorStatus;
1798
342k
}
1799
1800
SBR_ERROR sbrDecoder_Apply(HANDLE_SBRDECODER self, LONG *input, LONG *timeData,
1801
                           const int timeDataSize, int *numChannels,
1802
                           int *sampleRate,
1803
                           const FDK_channelMapDescr *const mapDescr,
1804
                           const int mapIdx, const int coreDecodedOk,
1805
                           UCHAR *psDecoded, const INT inDataHeadroom,
1806
297k
                           INT *outDataHeadroom) {
1807
297k
  SBR_ERROR errorStatus = SBRDEC_OK;
1808
1809
297k
  int psPossible;
1810
297k
  int sbrElementNum;
1811
297k
  int numCoreChannels;
1812
297k
  int numSbrChannels = 0;
1813
1814
297k
  if ((self == NULL) || (timeData == NULL) || (numChannels == NULL) ||
1815
297k
      (sampleRate == NULL) || (psDecoded == NULL) ||
1816
297k
      !FDK_chMapDescr_isValid(mapDescr)) {
1817
0
    return SBRDEC_INVALID_ARGUMENT;
1818
0
  }
1819
1820
297k
  psPossible = *psDecoded;
1821
297k
  numCoreChannels = *numChannels;
1822
297k
  if (numCoreChannels <= 0) {
1823
0
    return SBRDEC_INVALID_ARGUMENT;
1824
0
  }
1825
1826
297k
  if (self->numSbrElements < 1) {
1827
    /* exit immediately to avoid access violations */
1828
0
    return SBRDEC_NOT_INITIALIZED;
1829
0
  }
1830
1831
  /* Sanity check of allocated SBR elements. */
1832
641k
  for (sbrElementNum = 0; sbrElementNum < self->numSbrElements;
1833
344k
       sbrElementNum++) {
1834
344k
    if (self->pSbrElement[sbrElementNum] == NULL) {
1835
193
      return SBRDEC_NOT_INITIALIZED;
1836
193
    }
1837
344k
  }
1838
1839
297k
  if (self->numSbrElements != 1 || self->pSbrElement[0]->elementID != ID_SCE) {
1840
124k
    psPossible = 0;
1841
124k
  }
1842
1843
297k
  self->sbrInDataHeadroom = inDataHeadroom;
1844
297k
  *outDataHeadroom = (INT)(8);
1845
1846
  /* Make sure that even if no SBR data was found/parsed *psDecoded is returned
1847
   * 1 if psPossible was 0. */
1848
297k
  if (psPossible == 0) {
1849
284k
    self->flags &= ~SBRDEC_PS_DECODED;
1850
284k
  }
1851
1852
  /* replaces channel based reset inside sbr_dec() */
1853
297k
  if (((self->flags & SBRDEC_LOW_POWER) ? 1 : 0) !=
1854
297k
      ((self->pQmfDomain->globalConf.flags & QMF_FLAG_LP) ? 1 : 0)) {
1855
17.5k
    if (self->flags & SBRDEC_LOW_POWER) {
1856
16.6k
      self->pQmfDomain->globalConf.flags |= QMF_FLAG_LP;
1857
16.6k
      self->pQmfDomain->globalConf.flags_requested |= QMF_FLAG_LP;
1858
16.6k
    } else {
1859
910
      self->pQmfDomain->globalConf.flags &= ~QMF_FLAG_LP;
1860
910
      self->pQmfDomain->globalConf.flags_requested &= ~QMF_FLAG_LP;
1861
910
    }
1862
17.5k
    if (FDK_QmfDomain_InitFilterBank(self->pQmfDomain, QMF_FLAG_KEEP_STATES)) {
1863
0
      return SBRDEC_UNSUPPORTED_CONFIG;
1864
0
    }
1865
17.5k
  }
1866
297k
  if (self->numSbrChannels > self->pQmfDomain->globalConf.nInputChannels) {
1867
0
    return SBRDEC_UNSUPPORTED_CONFIG;
1868
0
  }
1869
1870
297k
  if (self->flags & SBRDEC_FLUSH) {
1871
    /* flushing is signalized, hence increment the flush frame counter */
1872
30
    self->numFlushedFrames++;
1873
297k
  } else {
1874
    /* no flushing is signalized, hence reset the flush frame counter */
1875
297k
    self->numFlushedFrames = 0;
1876
297k
  }
1877
1878
  /* Loop over SBR elements */
1879
347k
  for (sbrElementNum = 0; sbrElementNum < self->numSbrElements;
1880
342k
       sbrElementNum++) {
1881
342k
    int numElementChan;
1882
1883
342k
    if (psPossible &&
1884
12.2k
        self->pSbrElement[sbrElementNum]->pSbrChannel[1] == NULL) {
1885
      /* Disable PS and try decoding SBR mono. */
1886
0
      psPossible = 0;
1887
0
    }
1888
1889
342k
    numElementChan =
1890
342k
        (self->pSbrElement[sbrElementNum]->elementID == ID_CPE) ? 2 : 1;
1891
1892
    /* If core signal is bad then force upsampling */
1893
342k
    if (!coreDecodedOk) {
1894
123k
      setFrameErrorFlag(self->pSbrElement[sbrElementNum], FRAME_ERROR_ALLSLOTS);
1895
123k
    }
1896
1897
342k
    errorStatus = sbrDecoder_DecodeElement(
1898
342k
        self, input, timeData, timeDataSize, mapDescr, mapIdx, numSbrChannels,
1899
342k
        sbrElementNum,
1900
342k
        numCoreChannels, /* is correct even for USC SCI==2 case */
1901
342k
        &numElementChan, psPossible);
1902
1903
342k
    if (errorStatus != SBRDEC_OK) {
1904
0
      goto bail;
1905
0
    }
1906
1907
342k
    numSbrChannels += numElementChan;
1908
1909
342k
    if (numSbrChannels >= numCoreChannels) {
1910
292k
      break;
1911
292k
    }
1912
342k
  }
1913
1914
  /* Update numChannels and samplerate */
1915
  /* Do not mess with output channels in case of USAC. numSbrChannels !=
1916
   * numChannels for stereoConfigIndex == 2 */
1917
297k
  if (!(self->flags & SBRDEC_SYNTAX_USAC)) {
1918
65.7k
    *numChannels = numSbrChannels;
1919
65.7k
  }
1920
297k
  *sampleRate = self->sampleRateOut;
1921
297k
  *psDecoded = (self->flags & SBRDEC_PS_DECODED) ? 1 : 0;
1922
1923
  /* Clear reset and flush flag because everything seems to be done
1924
   * successfully. */
1925
297k
  self->flags &= ~SBRDEC_FORCE_RESET;
1926
297k
  self->flags &= ~SBRDEC_FLUSH;
1927
1928
297k
bail:
1929
1930
297k
  return errorStatus;
1931
297k
}
1932
1933
34.5k
SBR_ERROR sbrDecoder_Close(HANDLE_SBRDECODER *pSelf) {
1934
34.5k
  HANDLE_SBRDECODER self = *pSelf;
1935
34.5k
  int i;
1936
1937
34.5k
  if (self != NULL) {
1938
34.5k
    if (self->hParametricStereoDec != NULL) {
1939
2.07k
      DeletePsDec(&self->hParametricStereoDec);
1940
2.07k
    }
1941
1942
311k
    for (i = 0; i < (8); i++) {
1943
276k
      sbrDecoder_DestroyElement(self, i);
1944
276k
    }
1945
1946
34.5k
    FreeRam_SbrDecoder(pSelf);
1947
34.5k
  }
1948
1949
34.5k
  return SBRDEC_OK;
1950
34.5k
}
1951
1952
0
INT sbrDecoder_GetLibInfo(LIB_INFO *info) {
1953
0
  int i;
1954
1955
0
  if (info == NULL) {
1956
0
    return -1;
1957
0
  }
1958
1959
  /* search for next free tab */
1960
0
  for (i = 0; i < FDK_MODULE_LAST; i++) {
1961
0
    if (info[i].module_id == FDK_NONE) break;
1962
0
  }
1963
0
  if (i == FDK_MODULE_LAST) return -1;
1964
0
  info += i;
1965
1966
0
  info->module_id = FDK_SBRDEC;
1967
0
  info->version =
1968
0
      LIB_VERSION(SBRDECODER_LIB_VL0, SBRDECODER_LIB_VL1, SBRDECODER_LIB_VL2);
1969
0
  LIB_VERSION_STRING(info);
1970
0
  info->build_date = SBRDECODER_LIB_BUILD_DATE;
1971
0
  info->build_time = SBRDECODER_LIB_BUILD_TIME;
1972
0
  info->title = SBRDECODER_LIB_TITLE;
1973
1974
  /* Set flags */
1975
0
  info->flags = 0 | CAPF_SBR_HQ | CAPF_SBR_LP | CAPF_SBR_PS_MPEG |
1976
0
                CAPF_SBR_DRM_BS | CAPF_SBR_CONCEALMENT | CAPF_SBR_DRC |
1977
0
                CAPF_SBR_ELD_DOWNSCALE | CAPF_SBR_HBEHQ;
1978
  /* End of flags */
1979
1980
0
  return 0;
1981
0
}
1982
1983
283k
UINT sbrDecoder_GetDelay(const HANDLE_SBRDECODER self) {
1984
283k
  UINT outputDelay = 0;
1985
1986
283k
  if (self != NULL) {
1987
283k
    UINT flags = self->flags;
1988
1989
    /* See chapter 1.6.7.2 of ISO/IEC 14496-3 for the GA-SBR figures below. */
1990
1991
    /* Are we initialized? */
1992
283k
    if ((self->numSbrChannels > 0) && (self->numSbrElements > 0)) {
1993
      /* Add QMF synthesis delay */
1994
283k
      if ((flags & SBRDEC_ELD_GRID) && IS_LOWDELAY(self->coreCodec)) {
1995
        /* Low delay SBR: */
1996
34.5k
        if (!(flags & SBRDEC_SKIP_QMF_SYN)) {
1997
33.2k
          outputDelay +=
1998
33.2k
              (flags & SBRDEC_DOWNSAMPLE) ? 32 : 64; /* QMF synthesis */
1999
33.2k
          if (flags & SBRDEC_LD_MPS_QMF) {
2000
728
            outputDelay += 32;
2001
728
          }
2002
33.2k
        }
2003
248k
      } else if (!IS_USAC(self->coreCodec)) {
2004
        /* By the method of elimination this is the GA (AAC-LC, HE-AAC, ...)
2005
         * branch: */
2006
31.1k
        outputDelay += (flags & SBRDEC_DOWNSAMPLE) ? 481 : 962;
2007
31.1k
        if (flags & SBRDEC_SKIP_QMF_SYN) {
2008
0
          outputDelay -= 257; /* QMF synthesis */
2009
0
        }
2010
31.1k
      }
2011
283k
    }
2012
283k
  }
2013
2014
283k
  return (outputDelay);
2015
283k
}