Coverage Report

Created: 2025-10-10 07:00

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
442k
static void setFrameErrorFlag(SBR_DECODER_ELEMENT *pSbrElement, UCHAR value) {
170
442k
  if (pSbrElement != NULL) {
171
442k
    switch (value) {
172
113k
      case FRAME_ERROR_ALLSLOTS:
173
113k
        FDKmemset(pSbrElement->frameErrorFlag, FRAME_ERROR,
174
113k
                  sizeof(pSbrElement->frameErrorFlag));
175
113k
        break;
176
329k
      default:
177
329k
        pSbrElement->frameErrorFlag[pSbrElement->useFrameSlot] = value;
178
442k
    }
179
442k
  }
180
442k
}
181
182
657k
static UCHAR getHeaderSlot(UCHAR currentSlot, UCHAR hdrSlotUsage[(1) + 1]) {
183
657k
  UINT occupied = 0;
184
657k
  int s;
185
657k
  UCHAR slot = hdrSlotUsage[currentSlot];
186
187
657k
  FDK_ASSERT((1) + 1 < 32);
188
189
1.74M
  for (s = 0; s < (1) + 1; s++) {
190
1.29M
    if ((hdrSlotUsage[s] == slot) && (s != slot)) {
191
204k
      occupied = 1;
192
204k
      break;
193
204k
    }
194
1.29M
  }
195
196
657k
  if (occupied) {
197
204k
    occupied = 0;
198
199
612k
    for (s = 0; s < (1) + 1; s++) {
200
408k
      occupied |= 1 << hdrSlotUsage[s];
201
408k
    }
202
386k
    for (s = 0; s < (1) + 1; s++) {
203
385k
      if (!(occupied & 0x1)) {
204
202k
        slot = s;
205
202k
        break;
206
202k
      }
207
182k
      occupied >>= 1;
208
182k
    }
209
204k
  }
210
211
657k
  return slot;
212
657k
}
213
214
static void copySbrHeader(HANDLE_SBR_HEADER_DATA hDst,
215
31.2k
                          const HANDLE_SBR_HEADER_DATA hSrc) {
216
  /* copy the whole header memory (including pointers) */
217
31.2k
  FDKmemcpy(hDst, hSrc, sizeof(SBR_HEADER_DATA));
218
219
  /* update pointers */
220
31.2k
  hDst->freqBandData.freqBandTable[0] = hDst->freqBandData.freqBandTableLo;
221
31.2k
  hDst->freqBandData.freqBandTable[1] = hDst->freqBandData.freqBandTableHi;
222
31.2k
}
223
224
static int compareSbrHeader(const HANDLE_SBR_HEADER_DATA hHdr1,
225
5.50k
                            const HANDLE_SBR_HEADER_DATA hHdr2) {
226
5.50k
  int result = 0;
227
228
  /* compare basic data */
229
5.50k
  result |= (hHdr1->syncState != hHdr2->syncState) ? 1 : 0;
230
5.50k
  result |= (hHdr1->status != hHdr2->status) ? 1 : 0;
231
5.50k
  result |= (hHdr1->frameErrorFlag != hHdr2->frameErrorFlag) ? 1 : 0;
232
5.50k
  result |= (hHdr1->numberTimeSlots != hHdr2->numberTimeSlots) ? 1 : 0;
233
5.50k
  result |=
234
5.50k
      (hHdr1->numberOfAnalysisBands != hHdr2->numberOfAnalysisBands) ? 1 : 0;
235
5.50k
  result |= (hHdr1->timeStep != hHdr2->timeStep) ? 1 : 0;
236
5.50k
  result |= (hHdr1->sbrProcSmplRate != hHdr2->sbrProcSmplRate) ? 1 : 0;
237
238
  /* compare bitstream data */
239
5.50k
  result |=
240
5.50k
      FDKmemcmp(&hHdr1->bs_data, &hHdr2->bs_data, sizeof(SBR_HEADER_DATA_BS));
241
5.50k
  result |=
242
5.50k
      FDKmemcmp(&hHdr1->bs_dflt, &hHdr2->bs_dflt, sizeof(SBR_HEADER_DATA_BS));
243
5.50k
  result |= FDKmemcmp(&hHdr1->bs_info, &hHdr2->bs_info,
244
5.50k
                      sizeof(SBR_HEADER_DATA_BS_INFO));
245
246
  /* compare frequency band data */
247
5.50k
  result |= FDKmemcmp(&hHdr1->freqBandData, &hHdr2->freqBandData,
248
5.50k
                      (8 + MAX_NUM_LIMITERS + 1) * sizeof(UCHAR));
249
5.50k
  result |= FDKmemcmp(hHdr1->freqBandData.freqBandTableLo,
250
5.50k
                      hHdr2->freqBandData.freqBandTableLo,
251
5.50k
                      (MAX_FREQ_COEFFS / 2 + 1) * sizeof(UCHAR));
252
5.50k
  result |= FDKmemcmp(hHdr1->freqBandData.freqBandTableHi,
253
5.50k
                      hHdr2->freqBandData.freqBandTableHi,
254
5.50k
                      (MAX_FREQ_COEFFS + 1) * sizeof(UCHAR));
255
5.50k
  result |= FDKmemcmp(hHdr1->freqBandData.freqBandTableNoise,
256
5.50k
                      hHdr2->freqBandData.freqBandTableNoise,
257
5.50k
                      (MAX_NOISE_COEFFS + 1) * sizeof(UCHAR));
258
5.50k
  result |=
259
5.50k
      FDKmemcmp(hHdr1->freqBandData.v_k_master, hHdr2->freqBandData.v_k_master,
260
5.50k
                (MAX_FREQ_COEFFS + 1) * sizeof(UCHAR));
261
262
5.50k
  return result;
263
5.50k
}
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
116k
                                         const int overlap) {
279
116k
  SBR_ERROR sbrError = SBRDEC_OK;
280
116k
  HANDLE_SBR_HEADER_DATA hSbrHeader;
281
116k
  UINT qmfFlags = 0;
282
283
116k
  int i, synDownsampleFac;
284
285
  /* USAC: assuming theoretical case 8 kHz output sample rate with 4:1 SBR */
286
116k
  const int sbr_min_sample_rate_in = IS_USAC(self->coreCodec) ? 2000 : 6400;
287
288
  /* Check in/out samplerates */
289
116k
  if (sampleRateIn < sbr_min_sample_rate_in || sampleRateIn > (96000)) {
290
588
    sbrError = SBRDEC_UNSUPPORTED_CONFIG;
291
588
    goto bail;
292
588
  }
293
294
115k
  if (sampleRateOut > (96000)) {
295
324
    sbrError = SBRDEC_UNSUPPORTED_CONFIG;
296
324
    goto bail;
297
324
  }
298
299
  /* Set QMF mode flags */
300
115k
  if (self->flags & SBRDEC_LOW_POWER) qmfFlags |= QMF_FLAG_LP;
301
302
115k
  if (self->coreCodec == AOT_ER_AAC_ELD) {
303
77.9k
    if (self->flags & SBRDEC_LD_MPS_QMF) {
304
0
      qmfFlags |= QMF_FLAG_MPSLDFB;
305
77.9k
    } else {
306
77.9k
      qmfFlags |= QMF_FLAG_CLDFB;
307
77.9k
    }
308
77.9k
  }
309
310
  /* Set downsampling factor for synthesis filter bank */
311
115k
  if (sampleRateOut == 0) {
312
    /* no single rate mode */
313
4.83k
    sampleRateOut =
314
4.83k
        sampleRateIn
315
4.83k
        << 1; /* In case of implicit signalling, assume dual rate SBR */
316
4.83k
  }
317
318
115k
  if (sampleRateIn == sampleRateOut) {
319
60.9k
    synDownsampleFac = 2;
320
60.9k
    self->flags |= SBRDEC_DOWNSAMPLE;
321
60.9k
  } else {
322
54.3k
    synDownsampleFac = 1;
323
54.3k
    self->flags &= ~SBRDEC_DOWNSAMPLE;
324
54.3k
  }
325
326
115k
  self->synDownsampleFac = synDownsampleFac;
327
115k
  self->sampleRateOut = sampleRateOut;
328
329
115k
  {
330
345k
    for (i = 0; i < (1) + 1; i++) {
331
230k
      int setDflt;
332
230k
      hSbrHeader = &(self->sbrHeader[elementIndex][i]);
333
230k
      setDflt = ((hSbrHeader->syncState == SBR_NOT_INITIALIZED) ||
334
1.15k
                 (self->flags & SBRDEC_FORCE_RESET))
335
230k
                    ? 1
336
230k
                    : 0;
337
338
      /* init a default header such that we can at least do upsampling later */
339
230k
      sbrError = initHeaderData(hSbrHeader, sampleRateIn, sampleRateOut,
340
230k
                                self->downscaleFactor, samplesPerFrame,
341
230k
                                self->flags, setDflt);
342
343
      /* Set synchState to UPSAMPLING in case it already is initialized */
344
230k
      hSbrHeader->syncState = hSbrHeader->syncState > UPSAMPLING
345
230k
                                  ? UPSAMPLING
346
230k
                                  : hSbrHeader->syncState;
347
230k
    }
348
115k
  }
349
350
115k
  if (sbrError != SBRDEC_OK) {
351
50
    goto bail;
352
50
  }
353
354
115k
  if (!self->pQmfDomain->globalConf.qmfDomainExplicitConfig) {
355
115k
    self->pQmfDomain->globalConf.flags_requested |= qmfFlags;
356
115k
    self->pQmfDomain->globalConf.nBandsAnalysis_requested =
357
115k
        self->sbrHeader[elementIndex][0].numberOfAnalysisBands;
358
115k
    self->pQmfDomain->globalConf.nBandsSynthesis_requested =
359
115k
        (synDownsampleFac == 1) ? 64 : 32; /* may be overwritten by MPS */
360
115k
    self->pQmfDomain->globalConf.nBandsSynthesis_requested /=
361
115k
        self->downscaleFactor;
362
115k
    self->pQmfDomain->globalConf.nQmfTimeSlots_requested =
363
115k
        self->sbrHeader[elementIndex][0].numberTimeSlots *
364
115k
        self->sbrHeader[elementIndex][0].timeStep;
365
115k
    self->pQmfDomain->globalConf.nQmfOvTimeSlots_requested = overlap;
366
115k
    self->pQmfDomain->globalConf.nQmfProcBands_requested = 64; /* always 64 */
367
115k
    self->pQmfDomain->globalConf.nQmfProcChannels_requested =
368
115k
        1; /* may be overwritten by MPS */
369
115k
  }
370
371
  /* Init SBR channels going to be assigned to a SBR element */
372
115k
  {
373
115k
    int ch;
374
291k
    for (ch = 0; ch < self->pSbrElement[elementIndex]->nChannels; ch++) {
375
179k
      int headerIndex =
376
179k
          getHeaderSlot(self->pSbrElement[elementIndex]->useFrameSlot,
377
179k
                        self->pSbrElement[elementIndex]->useHeaderSlot);
378
379
      /* and create sbrDec */
380
179k
      sbrError =
381
179k
          createSbrDec(self->pSbrElement[elementIndex]->pSbrChannel[ch],
382
179k
                       &self->sbrHeader[elementIndex][headerIndex],
383
179k
                       &self->pSbrElement[elementIndex]->transposerSettings,
384
179k
                       synDownsampleFac, qmfFlags, self->flags, overlap, ch,
385
179k
                       self->codecFrameSize);
386
387
179k
      if (sbrError != SBRDEC_OK) {
388
2.43k
        goto bail;
389
2.43k
      }
390
179k
    }
391
115k
  }
392
393
  // FDKmemclear(sbr_OverlapBuffer, sizeof(sbr_OverlapBuffer));
394
395
112k
  if (self->numSbrElements == 1) {
396
59.4k
    switch (self->coreCodec) {
397
1.03k
      case AOT_AAC_LC:
398
1.03k
      case AOT_SBR:
399
1.03k
      case AOT_PS:
400
4.04k
      case AOT_ER_AAC_SCAL:
401
4.04k
      case AOT_DRM_AAC:
402
4.04k
      case AOT_DRM_SURROUND:
403
4.04k
        if (CreatePsDec(&self->hParametricStereoDec, samplesPerFrame)) {
404
0
          sbrError = SBRDEC_CREATE_ERROR;
405
0
          goto bail;
406
0
        }
407
4.04k
        break;
408
55.3k
      default:
409
55.3k
        break;
410
59.4k
    }
411
59.4k
  }
412
413
  /* Init frame delay slot handling */
414
112k
  self->pSbrElement[elementIndex]->useFrameSlot = 0;
415
338k
  for (i = 0; i < ((1) + 1); i++) {
416
225k
    self->pSbrElement[elementIndex]->useHeaderSlot[i] = i;
417
225k
  }
418
419
116k
bail:
420
421
116k
  return sbrError;
422
112k
}
423
424
/*!
425
  \brief Assign QMF domain provided QMF channels to SBR channels.
426
427
  \return void
428
*/
429
116k
static void sbrDecoder_AssignQmfChannels2SbrChannels(HANDLE_SBRDECODER self) {
430
116k
  int ch, el, absCh_offset = 0;
431
352k
  for (el = 0; el < self->numSbrElements; el++) {
432
236k
    if (self->pSbrElement[el] != NULL) {
433
601k
      for (ch = 0; ch < self->pSbrElement[el]->nChannels; ch++) {
434
365k
        FDK_ASSERT(((absCh_offset + ch) < ((8) + (1))) &&
435
365k
                   ((absCh_offset + ch) < ((8) + (1))));
436
365k
        self->pSbrElement[el]->pSbrChannel[ch]->SbrDec.qmfDomainInCh =
437
365k
            &self->pQmfDomain->QmfDomainIn[absCh_offset + ch];
438
365k
        self->pSbrElement[el]->pSbrChannel[ch]->SbrDec.qmfDomainOutCh =
439
365k
            &self->pQmfDomain->QmfDomainOut[absCh_offset + ch];
440
365k
      }
441
236k
      absCh_offset += self->pSbrElement[el]->nChannels;
442
236k
    }
443
236k
  }
444
116k
}
445
446
SBR_ERROR sbrDecoder_Open(HANDLE_SBRDECODER *pSelf,
447
31.7k
                          HANDLE_FDK_QMF_DOMAIN pQmfDomain) {
448
31.7k
  HANDLE_SBRDECODER self = NULL;
449
31.7k
  SBR_ERROR sbrError = SBRDEC_OK;
450
31.7k
  int elIdx;
451
452
31.7k
  if ((pSelf == NULL) || (pQmfDomain == NULL)) {
453
0
    return SBRDEC_INVALID_ARGUMENT;
454
0
  }
455
456
  /* Get memory for this instance */
457
31.7k
  self = GetRam_SbrDecoder();
458
31.7k
  if (self == NULL) {
459
0
    sbrError = SBRDEC_MEM_ALLOC_FAILED;
460
0
    goto bail;
461
0
  }
462
463
31.7k
  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
31.7k
  self->numDelayFrames = (1); /* set to the max value by default */
473
474
  /* Initialize header sync state */
475
286k
  for (elIdx = 0; elIdx < (8); elIdx += 1) {
476
254k
    int i;
477
762k
    for (i = 0; i < (1) + 1; i += 1) {
478
508k
      self->sbrHeader[elIdx][i].syncState = SBR_NOT_INITIALIZED;
479
508k
    }
480
254k
  }
481
482
31.7k
  *pSelf = self;
483
484
31.7k
bail:
485
31.7k
  return sbrError;
486
31.7k
}
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.34M
static int sbrDecoder_isCoreCodecValid(AUDIO_OBJECT_TYPE coreCodec) {
494
1.34M
  switch (coreCodec) {
495
11.8k
    case AOT_AAC_LC:
496
11.8k
    case AOT_SBR:
497
11.8k
    case AOT_PS:
498
60.2k
    case AOT_ER_AAC_SCAL:
499
1.01M
    case AOT_ER_AAC_ELD:
500
1.01M
    case AOT_DRM_AAC:
501
1.01M
    case AOT_DRM_SURROUND:
502
1.34M
    case AOT_USAC:
503
1.34M
      return 1;
504
798
    default:
505
798
      return 0;
506
1.34M
  }
507
1.34M
}
508
509
static void sbrDecoder_DestroyElement(HANDLE_SBRDECODER self,
510
1.94M
                                      const int elementIndex) {
511
1.94M
  if (self->pSbrElement[elementIndex] != NULL) {
512
115k
    int ch;
513
514
346k
    for (ch = 0; ch < SBRDEC_MAX_CH_PER_ELEMENT; ch++) {
515
231k
      if (self->pSbrElement[elementIndex]->pSbrChannel[ch] != NULL) {
516
181k
        deleteSbrDec(self->pSbrElement[elementIndex]->pSbrChannel[ch]);
517
181k
        FreeRam_SbrDecChannel(
518
181k
            &self->pSbrElement[elementIndex]->pSbrChannel[ch]);
519
181k
        self->numSbrChannels -= 1;
520
181k
      }
521
231k
    }
522
115k
    FreeRam_SbrDecElement(&self->pSbrElement[elementIndex]);
523
115k
    self->numSbrElements -= 1;
524
115k
  }
525
1.94M
}
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
740k
    const UCHAR configMode, UCHAR *configChanged, const INT downscaleFactor) {
533
740k
  SBR_ERROR sbrError = SBRDEC_OK;
534
740k
  int chCnt = 0;
535
740k
  int nSbrElementsStart;
536
740k
  int nSbrChannelsStart;
537
740k
  if (self == NULL) {
538
0
    return SBRDEC_INVALID_ARGUMENT;
539
0
  }
540
541
740k
  nSbrElementsStart = self->numSbrElements;
542
740k
  nSbrChannelsStart = self->numSbrChannels;
543
544
  /* Check core codec AOT */
545
740k
  if (!sbrDecoder_isCoreCodecValid(coreCodec) || elementIndex >= (8)) {
546
798
    sbrError = SBRDEC_UNSUPPORTED_CONFIG;
547
798
    goto bail;
548
798
  }
549
550
739k
  if (elementID != ID_SCE && elementID != ID_CPE && elementID != ID_LFE) {
551
0
    sbrError = SBRDEC_UNSUPPORTED_CONFIG;
552
0
    goto bail;
553
0
  }
554
555
739k
  if (self->sampleRateIn == sampleRateIn &&
556
620k
      self->codecFrameSize == samplesPerFrame && self->coreCodec == coreCodec &&
557
583k
      self->pSbrElement[elementIndex] != NULL &&
558
498k
      self->pSbrElement[elementIndex]->elementID == elementID &&
559
494k
      !(self->flags & SBRDEC_FORCE_RESET) &&
560
494k
      ((sampleRateOut == 0) ? 1 : (self->sampleRateOut == sampleRateOut)) &&
561
484k
      ((harmonicSBR == 2) ? 1
562
484k
                          : (self->harmonicSBR ==
563
354k
                             harmonicSBR)) /* The value 2 signalizes that
564
                                              harmonicSBR shall be ignored in
565
                                              the config change detection */
566
739k
  ) {
567
    /* Nothing to do */
568
481k
    return SBRDEC_OK;
569
481k
  } else {
570
258k
    if (configMode & AC_CM_DET_CFG_CHANGE) {
571
141k
      *configChanged = 1;
572
141k
    }
573
258k
  }
574
575
  /* reaching this point the SBR-decoder gets (re-)configured */
576
577
  /* The flags field is used for all elements! */
578
258k
  self->flags &=
579
258k
      (SBRDEC_FORCE_RESET | SBRDEC_FLUSH); /* Keep the global flags. They will
580
                                              be reset after decoding. */
581
258k
  self->flags |= (downscaleFactor > 1) ? SBRDEC_ELD_DOWNSCALE : 0;
582
258k
  self->flags |= (coreCodec == AOT_ER_AAC_ELD) ? SBRDEC_ELD_GRID : 0;
583
258k
  self->flags |= (coreCodec == AOT_ER_AAC_SCAL) ? SBRDEC_SYNTAX_SCAL : 0;
584
258k
  self->flags |=
585
258k
      (coreCodec == AOT_DRM_AAC) ? SBRDEC_SYNTAX_SCAL | SBRDEC_SYNTAX_DRM : 0;
586
258k
  self->flags |= (coreCodec == AOT_DRM_SURROUND)
587
258k
                     ? SBRDEC_SYNTAX_SCAL | SBRDEC_SYNTAX_DRM
588
258k
                     : 0;
589
258k
  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
258k
  self->flags |= (sampleRateIn == sampleRateOut / 4) ? SBRDEC_QUAD_RATE : 0;
593
258k
  self->flags |= (harmonicSBR == 1) ? SBRDEC_USAC_HARMONICSBR : 0;
594
595
258k
  if (configMode & AC_CM_DET_CFG_CHANGE) {
596
141k
    return SBRDEC_OK;
597
141k
  }
598
599
116k
  self->sampleRateIn = sampleRateIn;
600
116k
  self->codecFrameSize = samplesPerFrame;
601
116k
  self->coreCodec = coreCodec;
602
116k
  self->harmonicSBR = harmonicSBR;
603
116k
  self->downscaleFactor = downscaleFactor;
604
605
  /* Init SBR elements */
606
116k
  {
607
116k
    int elChannels, ch;
608
609
116k
    if (self->pSbrElement[elementIndex] == NULL) {
610
115k
      self->pSbrElement[elementIndex] = GetRam_SbrDecElement(elementIndex);
611
115k
      if (self->pSbrElement[elementIndex] == NULL) {
612
0
        sbrError = SBRDEC_MEM_ALLOC_FAILED;
613
0
        goto bail;
614
0
      }
615
115k
      self->numSbrElements++;
616
115k
    } else {
617
634
      self->numSbrChannels -= self->pSbrElement[elementIndex]->nChannels;
618
634
    }
619
620
    /* Determine amount of channels for this element */
621
116k
    switch (elementID) {
622
0
      case ID_NONE:
623
63.1k
      case ID_CPE:
624
63.1k
        elChannels = 2;
625
63.1k
        break;
626
10.6k
      case ID_LFE:
627
53.0k
      case ID_SCE:
628
53.0k
        elChannels = 1;
629
53.0k
        break;
630
0
      default:
631
0
        elChannels = 0;
632
0
        break;
633
116k
    }
634
635
    /* Handle case of Parametric Stereo */
636
116k
    if (elementIndex == 0 && elementID == ID_SCE) {
637
39.0k
      switch (coreCodec) {
638
1.05k
        case AOT_AAC_LC:
639
1.05k
        case AOT_SBR:
640
1.05k
        case AOT_PS:
641
3.14k
        case AOT_ER_AAC_SCAL:
642
3.14k
        case AOT_DRM_AAC:
643
3.14k
        case AOT_DRM_SURROUND:
644
3.14k
          elChannels = 2;
645
3.14k
          break;
646
35.9k
        default:
647
35.9k
          break;
648
39.0k
      }
649
39.0k
    }
650
651
    /* Sanity check to avoid memory leaks */
652
116k
    if (elChannels < self->pSbrElement[elementIndex]->nChannels ||
653
116k
        (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
116k
    self->pSbrElement[elementIndex]->elementID = elementID;
662
116k
    self->pSbrElement[elementIndex]->nChannels = elChannels;
663
664
298k
    for (ch = 0; ch < elChannels; ch++) {
665
182k
      if (self->pSbrElement[elementIndex]->pSbrChannel[ch] == NULL) {
666
181k
        self->pSbrElement[elementIndex]->pSbrChannel[ch] =
667
181k
            GetRam_SbrDecChannel(chCnt);
668
181k
        if (self->pSbrElement[elementIndex]->pSbrChannel[ch] == NULL) {
669
0
          sbrError = SBRDEC_MEM_ALLOC_FAILED;
670
0
          goto bail;
671
0
        }
672
181k
      }
673
182k
      self->numSbrChannels++;
674
675
182k
      sbrDecoder_drcInitChannel(&self->pSbrElement[elementIndex]
676
182k
                                     ->pSbrChannel[ch]
677
182k
                                     ->SbrDec.sbrDrcChannel);
678
679
182k
      chCnt++;
680
182k
    }
681
116k
  }
682
683
116k
  if (!self->pQmfDomain->globalConf.qmfDomainExplicitConfig) {
684
116k
    self->pQmfDomain->globalConf.nInputChannels_requested =
685
116k
        self->numSbrChannels;
686
116k
    self->pQmfDomain->globalConf.nOutputChannels_requested =
687
116k
        fMax((INT)self->numSbrChannels,
688
116k
             (INT)self->pQmfDomain->globalConf.nOutputChannels_requested);
689
116k
  }
690
691
  /* Make sure each SBR channel has one QMF channel assigned even if
692
   * numSbrChannels or element set-up has changed. */
693
116k
  sbrDecoder_AssignQmfChannels2SbrChannels(self);
694
695
  /* clear error flags for all delay slots */
696
116k
  FDKmemclear(self->pSbrElement[elementIndex]->frameErrorFlag,
697
116k
              ((1) + 1) * sizeof(UCHAR));
698
699
116k
  {
700
116k
    int overlap;
701
702
116k
    if (coreCodec == AOT_ER_AAC_ELD) {
703
78.6k
      overlap = 0;
704
78.6k
    } else if (self->flags & SBRDEC_QUAD_RATE) {
705
9.91k
      overlap = (3 * 4);
706
27.6k
    } else {
707
27.6k
      overlap = (3 * 2);
708
27.6k
    }
709
    /* Initialize this instance */
710
116k
    sbrError = sbrDecoder_ResetElement(self, sampleRateIn, sampleRateOut,
711
116k
                                       samplesPerFrame, elementID, elementIndex,
712
116k
                                       overlap);
713
116k
  }
714
715
116k
bail:
716
116k
  if (sbrError != SBRDEC_OK) {
717
4.19k
    if ((nSbrElementsStart < self->numSbrElements) ||
718
3.39k
        (nSbrChannelsStart < self->numSbrChannels)) {
719
      /* Free the memory allocated for this element */
720
3.39k
      sbrDecoder_DestroyElement(self, elementIndex);
721
3.39k
    } else if ((elementIndex < (8)) &&
722
798
               (self->pSbrElement[elementIndex] !=
723
798
                NULL)) { /* Set error flag to trigger concealment */
724
0
      setFrameErrorFlag(self->pSbrElement[elementIndex], FRAME_ERROR);
725
0
    }
726
4.19k
  }
727
728
116k
  return sbrError;
729
116k
}
730
731
/**
732
 * \brief Free config dependent SBR memory.
733
 * \param self SBR decoder instance handle
734
 */
735
211k
SBR_ERROR sbrDecoder_FreeMem(HANDLE_SBRDECODER *self) {
736
211k
  int i;
737
211k
  int elIdx;
738
739
211k
  if (self != NULL && *self != NULL) {
740
1.90M
    for (i = 0; i < (8); i++) {
741
1.68M
      sbrDecoder_DestroyElement(*self, i);
742
1.68M
    }
743
744
1.90M
    for (elIdx = 0; elIdx < (8); elIdx += 1) {
745
5.06M
      for (i = 0; i < (1) + 1; i += 1) {
746
3.37M
        (*self)->sbrHeader[elIdx][i].syncState = SBR_NOT_INITIALIZED;
747
3.37M
      }
748
1.68M
    }
749
211k
  }
750
751
211k
  return SBRDEC_OK;
752
211k
}
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
243k
                                         const int numElementChannels) {
769
243k
  SBR_ERROR errorStatus = SBRDEC_OK;
770
771
  /*
772
    change of control data, reset decoder
773
  */
774
243k
  errorStatus = resetFreqBandTables(hSbrHeader, self->flags);
775
776
243k
  if (errorStatus == SBRDEC_OK) {
777
208k
    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
38.3k
      hSbrHeader->freqBandData.lowSubband = hSbrHeader->numberOfAnalysisBands;
788
38.3k
      hSbrHeader->freqBandData.highSubband = hSbrHeader->numberOfAnalysisBands;
789
38.3k
    }
790
791
    /* Trigger a reset before processing this slot */
792
208k
    hSbrHeader->status |= SBRDEC_HDR_STAT_RESET;
793
208k
  }
794
795
243k
  return errorStatus;
796
243k
}
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
603k
                      const INT downscaleFactor) {
806
603k
  SBR_HEADER_STATUS headerStatus;
807
603k
  HANDLE_SBR_HEADER_DATA hSbrHeader;
808
603k
  SBR_ERROR sbrError = SBRDEC_OK;
809
603k
  int headerIndex;
810
603k
  UINT flagsSaved =
811
603k
      0; /* flags should not be changed in AC_CM_DET_CFG_CHANGE - mode after
812
            parsing */
813
814
603k
  if (self == NULL || elementIndex >= (8)) {
815
0
    return SBRDEC_UNSUPPORTED_CONFIG;
816
0
  }
817
818
603k
  if (!sbrDecoder_isCoreCodecValid(coreCodec)) {
819
0
    return SBRDEC_UNSUPPORTED_CONFIG;
820
0
  }
821
822
603k
  if (configMode & AC_CM_DET_CFG_CHANGE) {
823
381k
    flagsSaved = self->flags; /* store */
824
381k
  }
825
826
603k
  sbrError = sbrDecoder_InitElement(
827
603k
      self, sampleRateIn, sampleRateOut, samplesPerFrame, coreCodec, elementID,
828
603k
      elementIndex, harmonicSBR, stereoConfigIndex, configMode, configChanged,
829
603k
      downscaleFactor);
830
831
603k
  if ((sbrError != SBRDEC_OK) || (elementID == ID_LFE)) {
832
79.0k
    goto bail;
833
79.0k
  }
834
835
524k
  if (configMode & AC_CM_DET_CFG_CHANGE) {
836
332k
    hSbrHeader = NULL;
837
332k
  } else {
838
192k
    headerIndex = getHeaderSlot(self->pSbrElement[elementIndex]->useFrameSlot,
839
192k
                                self->pSbrElement[elementIndex]->useHeaderSlot);
840
841
192k
    hSbrHeader = &(self->sbrHeader[elementIndex][headerIndex]);
842
192k
  }
843
844
524k
  headerStatus = sbrGetHeaderData(hSbrHeader, hBs, self->flags, 0, configMode);
845
846
524k
  if (coreCodec == AOT_USAC) {
847
159k
    if (configMode & AC_CM_DET_CFG_CHANGE) {
848
102k
      self->flags = flagsSaved; /* restore */
849
102k
    }
850
159k
    return sbrError;
851
159k
  }
852
853
364k
  if (configMode & AC_CM_ALLOC_MEM) {
854
135k
    SBR_DECODER_ELEMENT *pSbrElement;
855
856
135k
    pSbrElement = self->pSbrElement[elementIndex];
857
858
    /* Sanity check */
859
135k
    if (pSbrElement != NULL) {
860
135k
      if ((elementID == ID_CPE && pSbrElement->nChannels != 2) ||
861
135k
          (elementID != ID_CPE && pSbrElement->nChannels != 1)) {
862
0
        return SBRDEC_UNSUPPORTED_CONFIG;
863
0
      }
864
135k
      if (headerStatus == HEADER_RESET) {
865
96.7k
        sbrError = sbrDecoder_HeaderUpdate(self, hSbrHeader, headerStatus,
866
96.7k
                                           pSbrElement->pSbrChannel,
867
96.7k
                                           pSbrElement->nChannels);
868
869
96.7k
        if (sbrError == SBRDEC_OK) {
870
85.4k
          hSbrHeader->syncState = SBR_HEADER;
871
85.4k
          hSbrHeader->status |= SBRDEC_HDR_STAT_UPDATE;
872
85.4k
        } else {
873
11.3k
          hSbrHeader->syncState = SBR_NOT_INITIALIZED;
874
11.3k
          hSbrHeader->status = HEADER_ERROR;
875
11.3k
        }
876
96.7k
      }
877
135k
    }
878
135k
  }
879
443k
bail:
880
443k
  if (configMode & AC_CM_DET_CFG_CHANGE) {
881
278k
    self->flags = flagsSaved; /* restore */
882
278k
  }
883
443k
  return sbrError;
884
364k
}
885
886
SBR_ERROR sbrDecoder_SetParam(HANDLE_SBRDECODER self, const SBRDEC_PARAM param,
887
1.30M
                              const INT value) {
888
1.30M
  SBR_ERROR errorStatus = SBRDEC_OK;
889
890
  /* configure the subsystems */
891
1.30M
  switch (param) {
892
322k
    case SBR_SYSTEM_BITSTREAM_DELAY:
893
322k
      if (value < 0 || value > (1)) {
894
0
        errorStatus = SBRDEC_SET_PARAM_FAIL;
895
0
        break;
896
0
      }
897
322k
      if (self == NULL) {
898
0
        errorStatus = SBRDEC_NOT_INITIALIZED;
899
322k
      } else {
900
322k
        self->numDelayFrames = (UCHAR)value;
901
322k
      }
902
322k
      break;
903
407k
    case SBR_QMF_MODE:
904
407k
      if (self == NULL) {
905
0
        errorStatus = SBRDEC_NOT_INITIALIZED;
906
407k
      } else {
907
407k
        if (value == 1) {
908
115k
          self->flags |= SBRDEC_LOW_POWER;
909
291k
        } else {
910
291k
          self->flags &= ~SBRDEC_LOW_POWER;
911
291k
        }
912
407k
      }
913
407k
      break;
914
27.8k
    case SBR_LD_QMF_TIME_ALIGN:
915
27.8k
      if (self == NULL) {
916
0
        errorStatus = SBRDEC_NOT_INITIALIZED;
917
27.8k
      } else {
918
27.8k
        if (value == 1) {
919
1.60k
          self->flags |= SBRDEC_LD_MPS_QMF;
920
26.2k
        } else {
921
26.2k
          self->flags &= ~SBRDEC_LD_MPS_QMF;
922
26.2k
        }
923
27.8k
      }
924
27.8k
      break;
925
271k
    case SBR_FLUSH_DATA:
926
271k
      if (value != 0) {
927
23
        if (self == NULL) {
928
0
          errorStatus = SBRDEC_NOT_INITIALIZED;
929
23
        } else {
930
23
          self->flags |= SBRDEC_FLUSH;
931
23
        }
932
23
      }
933
271k
      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.32k
    case SBR_BS_INTERRUPTION: {
944
7.32k
      int elementIndex;
945
946
7.32k
      if (self == NULL) {
947
0
        errorStatus = SBRDEC_NOT_INITIALIZED;
948
0
        break;
949
0
      }
950
951
      /* Loop over SBR elements */
952
13.5k
      for (elementIndex = 0; elementIndex < self->numSbrElements;
953
7.32k
           elementIndex++) {
954
6.18k
        if (self->pSbrElement[elementIndex] != NULL) {
955
6.18k
          HANDLE_SBR_HEADER_DATA hSbrHeader;
956
6.18k
          int headerIndex =
957
6.18k
              getHeaderSlot(self->pSbrElement[elementIndex]->useFrameSlot,
958
6.18k
                            self->pSbrElement[elementIndex]->useHeaderSlot);
959
960
6.18k
          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.18k
          if (hSbrHeader->syncState != SBR_NOT_INITIALIZED) {
965
6.09k
            hSbrHeader->syncState = UPSAMPLING;
966
6.09k
            hSbrHeader->status |= SBRDEC_HDR_STAT_UPDATE;
967
6.09k
          }
968
6.18k
        }
969
6.18k
      }
970
7.32k
    } break;
971
972
271k
    case SBR_SKIP_QMF:
973
271k
      if (self == NULL) {
974
0
        errorStatus = SBRDEC_NOT_INITIALIZED;
975
271k
      } else {
976
271k
        if (value == 1) {
977
18.4k
          self->flags |= SBRDEC_SKIP_QMF_ANA;
978
252k
        } else {
979
252k
          self->flags &= ~SBRDEC_SKIP_QMF_ANA;
980
252k
        }
981
271k
        if (value == 2) {
982
124k
          self->flags |= SBRDEC_SKIP_QMF_SYN;
983
146k
        } else {
984
146k
          self->flags &= ~SBRDEC_SKIP_QMF_SYN;
985
146k
        }
986
271k
      }
987
271k
      break;
988
0
    default:
989
0
      errorStatus = SBRDEC_SET_PARAM_FAIL;
990
0
      break;
991
1.30M
  } /* switch(param) */
992
993
1.30M
  return (errorStatus);
994
1.30M
}
995
996
static SBRDEC_DRC_CHANNEL *sbrDecoder_drcGetChannel(
997
457k
    const HANDLE_SBRDECODER self, const INT channel) {
998
457k
  SBRDEC_DRC_CHANNEL *pSbrDrcChannelData = NULL;
999
457k
  int elementIndex, elChanIdx = 0, numCh = 0;
1000
1001
1.11M
  for (elementIndex = 0; (elementIndex < (8)) && (numCh <= channel);
1002
660k
       elementIndex++) {
1003
660k
    SBR_DECODER_ELEMENT *pSbrElement = self->pSbrElement[elementIndex];
1004
660k
    int c, elChannels;
1005
1006
660k
    elChanIdx = 0;
1007
660k
    if (pSbrElement == NULL) break;
1008
1009
    /* Determine amount of channels for this element */
1010
656k
    switch (pSbrElement->elementID) {
1011
361k
      case ID_CPE:
1012
361k
        elChannels = 2;
1013
361k
        break;
1014
28.0k
      case ID_LFE:
1015
294k
      case ID_SCE:
1016
294k
        elChannels = 1;
1017
294k
        break;
1018
0
      case ID_NONE:
1019
0
      default:
1020
0
        elChannels = 0;
1021
0
        break;
1022
656k
    }
1023
1024
    /* Limit with actual allocated element channels */
1025
656k
    elChannels = fMin(elChannels, pSbrElement->nChannels);
1026
1027
1.54M
    for (c = 0; (c < elChannels) && (numCh <= channel); c++) {
1028
884k
      if (pSbrElement->pSbrChannel[elChanIdx] != NULL) {
1029
884k
        numCh++;
1030
884k
        elChanIdx++;
1031
884k
      }
1032
884k
    }
1033
656k
  }
1034
457k
  elementIndex -= 1;
1035
457k
  elChanIdx -= 1;
1036
1037
457k
  if (elChanIdx < 0 || elementIndex < 0) {
1038
4.21k
    return NULL;
1039
4.21k
  }
1040
1041
453k
  if (self->pSbrElement[elementIndex] != NULL) {
1042
453k
    if (self->pSbrElement[elementIndex]->pSbrChannel[elChanIdx] != NULL) {
1043
453k
      pSbrDrcChannelData = &self->pSbrElement[elementIndex]
1044
453k
                                ->pSbrChannel[elChanIdx]
1045
453k
                                ->SbrDec.sbrDrcChannel;
1046
453k
    }
1047
453k
  }
1048
1049
453k
  return (pSbrDrcChannelData);
1050
457k
}
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
163k
                                    UCHAR winSequence, USHORT *pBandTop) {
1057
163k
  SBRDEC_DRC_CHANNEL *pSbrDrcChannelData = NULL;
1058
163k
  int band, isValidData = 0;
1059
1060
163k
  if (self == NULL) {
1061
0
    return SBRDEC_NOT_INITIALIZED;
1062
0
  }
1063
163k
  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
317k
  for (band = 0; band < (int)numBands; band += 1) {
1069
164k
    if (!((pNextFact_mag[band] == FL2FXCONST_DBL(0.5)) &&
1070
153k
          (nextFact_exp == 1)) &&
1071
10.7k
        !((pNextFact_mag[band] == (FIXP_DBL)MAXVAL_DBL) &&
1072
10.7k
          (nextFact_exp == 0))) {
1073
10.7k
      isValidData = 1;
1074
10.7k
      break;
1075
10.7k
    }
1076
164k
  }
1077
1078
  /* Find the right SBR channel */
1079
163k
  pSbrDrcChannelData = sbrDecoder_drcGetChannel(self, ch);
1080
1081
163k
  if (pSbrDrcChannelData != NULL) {
1082
163k
    if (pSbrDrcChannelData->enable ||
1083
149k
        isValidData) { /* Activate processing only with real and valid data */
1084
15.5k
      int i;
1085
1086
15.5k
      pSbrDrcChannelData->enable = 1;
1087
15.5k
      pSbrDrcChannelData->numBandsNext = numBands;
1088
1089
15.5k
      pSbrDrcChannelData->winSequenceNext = winSequence;
1090
15.5k
      pSbrDrcChannelData->drcInterpolationSchemeNext = drcInterpolationScheme;
1091
15.5k
      pSbrDrcChannelData->nextFact_exp = nextFact_exp;
1092
1093
69.0k
      for (i = 0; i < (int)numBands; i++) {
1094
53.5k
        pSbrDrcChannelData->bandTopNext[i] = pBandTop[i];
1095
53.5k
        pSbrDrcChannelData->nextFact_mag[i] = pNextFact_mag[i];
1096
53.5k
      }
1097
15.5k
    }
1098
163k
  }
1099
1100
163k
  return SBRDEC_OK;
1101
163k
}
1102
1103
321k
void sbrDecoder_drcDisable(HANDLE_SBRDECODER self, INT ch) {
1104
321k
  SBRDEC_DRC_CHANNEL *pSbrDrcChannelData = NULL;
1105
1106
321k
  if ((self == NULL) || (ch > (8)) || (self->numSbrElements == 0) ||
1107
294k
      (self->numSbrChannels == 0)) {
1108
26.4k
    return;
1109
26.4k
  }
1110
1111
  /* Find the right SBR channel */
1112
294k
  pSbrDrcChannelData = sbrDecoder_drcGetChannel(self, ch);
1113
1114
294k
  if (pSbrDrcChannelData != NULL) {
1115
290k
    sbrDecoder_drcInitChannel(pSbrDrcChannelData);
1116
290k
  }
1117
294k
}
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
329k
                           UINT acFlags, UINT acElFlags[]) {
1124
329k
  SBR_DECODER_ELEMENT *hSbrElement = NULL;
1125
329k
  HANDLE_SBR_HEADER_DATA hSbrHeader = NULL;
1126
329k
  HANDLE_SBR_CHANNEL *pSbrChannel;
1127
1128
329k
  SBR_FRAME_DATA *hFrameDataLeft = NULL;
1129
329k
  SBR_FRAME_DATA *hFrameDataRight = NULL;
1130
329k
  SBR_FRAME_DATA frameDataLeftCopy;
1131
329k
  SBR_FRAME_DATA frameDataRightCopy;
1132
1133
329k
  SBR_ERROR errorStatus = SBRDEC_OK;
1134
329k
  SBR_HEADER_STATUS headerStatus = HEADER_NOT_PRESENT;
1135
1136
329k
  INT startPos = FDKgetValidBits(hBs);
1137
329k
  FDK_CRCINFO crcInfo;
1138
329k
  INT crcReg = 0;
1139
329k
  USHORT sbrCrc = 0;
1140
329k
  UINT crcPoly;
1141
329k
  UINT crcStartValue = 0;
1142
329k
  UINT crcLen;
1143
1144
329k
  HANDLE_FDK_BITSTREAM hBsOriginal = hBs;
1145
329k
  FDK_BITSTREAM bsBwd;
1146
1147
329k
  const int fGlobalIndependencyFlag = acFlags & AC_INDEP;
1148
329k
  const int bs_pvc = acElFlags[elementIndex] & AC_EL_USAC_PVC;
1149
329k
  const int bs_interTes = acElFlags[elementIndex] & AC_EL_USAC_ITES;
1150
329k
  int stereo;
1151
329k
  int fDoDecodeSbrData = 1;
1152
329k
  int alignBits = 0;
1153
1154
329k
  int lastSlot, lastHdrSlot = 0, thisHdrSlot = 0;
1155
1156
329k
  if (*count <= 0) {
1157
48.3k
    setFrameErrorFlag(self->pSbrElement[elementIndex], FRAME_ERROR);
1158
48.3k
    return SBRDEC_OK;
1159
48.3k
  }
1160
1161
  /* SBR sanity checks */
1162
280k
  if (self == NULL) {
1163
0
    errorStatus = SBRDEC_NOT_INITIALIZED;
1164
0
    goto bail;
1165
0
  }
1166
1167
  /* Reverse bits of DRM SBR payload */
1168
280k
  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
280k
  startPos = FDKgetValidBits(hBs);
1212
1213
  /* SBR sanity checks */
1214
280k
  if (self->pSbrElement[elementIndex] == NULL) {
1215
0
    errorStatus = SBRDEC_NOT_INITIALIZED;
1216
0
    goto bail;
1217
0
  }
1218
280k
  hSbrElement = self->pSbrElement[elementIndex];
1219
1220
280k
  lastSlot = (hSbrElement->useFrameSlot > 0) ? hSbrElement->useFrameSlot - 1
1221
280k
                                             : self->numDelayFrames;
1222
280k
  lastHdrSlot = hSbrElement->useHeaderSlot[lastSlot];
1223
280k
  thisHdrSlot = getHeaderSlot(
1224
280k
      hSbrElement->useFrameSlot,
1225
280k
      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
280k
  hSbrHeader = &self->sbrHeader[elementIndex][thisHdrSlot];
1230
1231
280k
  pSbrChannel = hSbrElement->pSbrChannel;
1232
280k
  stereo = (hSbrElement->elementID == ID_CPE) ? 1 : 0;
1233
1234
280k
  hFrameDataLeft = &self->pSbrElement[elementIndex]
1235
280k
                        ->pSbrChannel[0]
1236
280k
                        ->frameData[hSbrElement->useFrameSlot];
1237
280k
  if (stereo) {
1238
103k
    hFrameDataRight = &self->pSbrElement[elementIndex]
1239
103k
                           ->pSbrChannel[1]
1240
103k
                           ->frameData[hSbrElement->useFrameSlot];
1241
103k
  }
1242
1243
  /* store frameData; new parsed frameData possibly corrupted */
1244
280k
  FDKmemcpy(&frameDataLeftCopy, hFrameDataLeft, sizeof(SBR_FRAME_DATA));
1245
280k
  if (stereo) {
1246
103k
    FDKmemcpy(&frameDataRightCopy, hFrameDataRight, sizeof(SBR_FRAME_DATA));
1247
103k
  }
1248
1249
  /* reset PS flag; will be set after PS was found */
1250
280k
  self->flags &= ~SBRDEC_PS_DECODED;
1251
1252
280k
  if (hSbrHeader->status & SBRDEC_HDR_STAT_UPDATE) {
1253
    /* Got a new header from extern (e.g. from an ASC) */
1254
23.7k
    headerStatus = HEADER_OK;
1255
23.7k
    hSbrHeader->status &= ~SBRDEC_HDR_STAT_UPDATE;
1256
256k
  } 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
31.2k
    copySbrHeader(hSbrHeader, &self->sbrHeader[elementIndex][lastHdrSlot]);
1260
31.2k
  }
1261
1262
  /*
1263
     Check if bit stream data is valid and matches the element context
1264
  */
1265
280k
  if (((prevElement != ID_SCE) && (prevElement != ID_CPE)) ||
1266
278k
      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.84k
    fDoDecodeSbrData = 0;
1270
1.84k
  }
1271
1272
280k
  if (fDoDecodeSbrData) {
1273
278k
    if ((INT)FDKgetValidBits(hBs) <= 0) {
1274
4
      fDoDecodeSbrData = 0;
1275
4
    }
1276
278k
  }
1277
1278
  /*
1279
     SBR CRC-check
1280
  */
1281
280k
  if (fDoDecodeSbrData) {
1282
278k
    if (crcFlag) {
1283
31.8k
      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
31.8k
        default:
1291
31.8k
          crcPoly = 0x0633;
1292
31.8k
          crcLen = 10;
1293
31.8k
          crcStartValue = 0x00000000;
1294
31.8k
          break;
1295
31.8k
      }
1296
31.8k
      sbrCrc = (USHORT)FDKreadBits(hBs, crcLen);
1297
      /* Setup CRC decoder */
1298
31.8k
      FDKcrcInit(&crcInfo, crcPoly, crcStartValue, crcLen);
1299
      /* Start CRC region */
1300
31.8k
      crcReg = FDKcrcStartReg(&crcInfo, hBs, 0);
1301
31.8k
    }
1302
278k
  } /* if (fDoDecodeSbrData) */
1303
1304
  /*
1305
     Read in the header data and issue a reset if change occured
1306
  */
1307
280k
  if (fDoDecodeSbrData) {
1308
278k
    int sbrHeaderPresent;
1309
1310
278k
    if (self->flags & (SBRDEC_SYNTAX_RSVD50 | SBRDEC_SYNTAX_USAC)) {
1311
205k
      SBR_HEADER_DATA_BS_INFO newSbrInfo;
1312
205k
      int sbrInfoPresent;
1313
1314
205k
      if (bs_interTes) {
1315
63.7k
        self->flags |= SBRDEC_USAC_ITES;
1316
141k
      } else {
1317
141k
        self->flags &= ~SBRDEC_USAC_ITES;
1318
141k
      }
1319
1320
205k
      if (fGlobalIndependencyFlag) {
1321
158k
        self->flags |= SBRDEC_USAC_INDEP;
1322
158k
        sbrInfoPresent = 1;
1323
158k
        sbrHeaderPresent = 1;
1324
158k
      } else {
1325
46.6k
        self->flags &= ~SBRDEC_USAC_INDEP;
1326
46.6k
        sbrInfoPresent = FDKreadBit(hBs);
1327
46.6k
        if (sbrInfoPresent) {
1328
8.36k
          sbrHeaderPresent = FDKreadBit(hBs);
1329
38.3k
        } else {
1330
38.3k
          sbrHeaderPresent = 0;
1331
38.3k
        }
1332
46.6k
      }
1333
1334
205k
      if (sbrInfoPresent) {
1335
167k
        newSbrInfo.ampResolution = FDKreadBit(hBs);
1336
167k
        newSbrInfo.xover_band = FDKreadBits(hBs, 4);
1337
167k
        newSbrInfo.sbr_preprocessing = FDKreadBit(hBs);
1338
167k
        if (bs_pvc) {
1339
109k
          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
109k
          if (newSbrInfo.pvc_mode > 2) {
1343
9.16k
            headerStatus = HEADER_ERROR;
1344
9.16k
          }
1345
109k
          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.2k
            newSbrInfo.pvc_mode = 0;
1350
11.2k
          }
1351
109k
        } else {
1352
58.2k
          newSbrInfo.pvc_mode = 0;
1353
58.2k
        }
1354
167k
        if (headerStatus != HEADER_ERROR) {
1355
158k
          if (FDKmemcmp(&hSbrHeader->bs_info, &newSbrInfo,
1356
158k
                        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
60.6k
            if ((hSbrHeader->bs_info.pvc_mode != newSbrInfo.pvc_mode) ||
1362
54.1k
                (hSbrHeader->bs_info.xover_band != newSbrInfo.xover_band)) {
1363
54.1k
              headerStatus = HEADER_RESET;
1364
54.1k
            } else {
1365
6.52k
              headerStatus = HEADER_OK;
1366
6.52k
            }
1367
1368
60.6k
            hSbrHeader->bs_info = newSbrInfo;
1369
97.5k
          } else {
1370
97.5k
            headerStatus = HEADER_OK;
1371
97.5k
          }
1372
158k
        }
1373
167k
      }
1374
205k
      if (headerStatus == HEADER_ERROR) {
1375
        /* Corrupt SBR info data, do not decode and switch to UPSAMPLING */
1376
9.16k
        hSbrHeader->syncState = hSbrHeader->syncState > UPSAMPLING
1377
9.16k
                                    ? UPSAMPLING
1378
9.16k
                                    : hSbrHeader->syncState;
1379
9.16k
        fDoDecodeSbrData = 0;
1380
9.16k
        sbrHeaderPresent = 0;
1381
9.16k
      }
1382
1383
205k
      if (sbrHeaderPresent && fDoDecodeSbrData) {
1384
154k
        int useDfltHeader;
1385
1386
154k
        useDfltHeader = FDKreadBit(hBs);
1387
1388
154k
        if (useDfltHeader) {
1389
92.3k
          sbrHeaderPresent = 0;
1390
92.3k
          if (FDKmemcmp(&hSbrHeader->bs_data, &hSbrHeader->bs_dflt,
1391
92.3k
                        sizeof(SBR_HEADER_DATA_BS)) ||
1392
75.6k
              hSbrHeader->syncState != SBR_ACTIVE) {
1393
19.0k
            hSbrHeader->bs_data = hSbrHeader->bs_dflt;
1394
19.0k
            headerStatus = HEADER_RESET;
1395
19.0k
          }
1396
92.3k
        }
1397
154k
      }
1398
205k
    } else {
1399
73.1k
      sbrHeaderPresent = FDKreadBit(hBs);
1400
73.1k
    }
1401
1402
278k
    if (sbrHeaderPresent) {
1403
109k
      headerStatus = sbrGetHeaderData(hSbrHeader, hBs, self->flags, 1, 0);
1404
109k
    }
1405
1406
278k
    if (headerStatus == HEADER_RESET) {
1407
108k
      errorStatus = sbrDecoder_HeaderUpdate(
1408
108k
          self, hSbrHeader, headerStatus, pSbrChannel, hSbrElement->nChannels);
1409
1410
108k
      if (errorStatus == SBRDEC_OK) {
1411
85.1k
        hSbrHeader->syncState = SBR_HEADER;
1412
85.1k
      } else {
1413
22.9k
        hSbrHeader->syncState = SBR_NOT_INITIALIZED;
1414
22.9k
        headerStatus = HEADER_ERROR;
1415
22.9k
      }
1416
108k
    }
1417
1418
278k
    if (errorStatus != SBRDEC_OK) {
1419
22.9k
      fDoDecodeSbrData = 0;
1420
22.9k
    }
1421
278k
  } /* if (fDoDecodeSbrData) */
1422
1423
  /*
1424
    Print debugging output only if state has changed
1425
  */
1426
1427
  /* read frame data */
1428
280k
  if ((hSbrHeader->syncState >= SBR_HEADER) && fDoDecodeSbrData) {
1429
242k
    int sbrFrameOk;
1430
    /* read the SBR element data */
1431
242k
    if (!stereo && (self->hParametricStereoDec != NULL)) {
1432
      /* update slot index for PS bitstream parsing */
1433
22.4k
      self->hParametricStereoDec->bsLastSlot =
1434
22.4k
          self->hParametricStereoDec->bsReadSlot;
1435
22.4k
      self->hParametricStereoDec->bsReadSlot = hSbrElement->useFrameSlot;
1436
22.4k
    }
1437
242k
    sbrFrameOk = sbrGetChannelElement(
1438
242k
        hSbrHeader, hFrameDataLeft, (stereo) ? hFrameDataRight : NULL,
1439
242k
        &pSbrChannel[0]->prevFrameData,
1440
242k
        pSbrChannel[0]->SbrDec.PvcStaticData.pvc_mode_last, hBs,
1441
242k
        (stereo) ? NULL : self->hParametricStereoDec, self->flags,
1442
242k
        self->pSbrElement[elementIndex]->transposerSettings.overlap);
1443
1444
242k
    if (!sbrFrameOk) {
1445
14.7k
      fDoDecodeSbrData = 0;
1446
227k
    } else {
1447
227k
      INT valBits;
1448
1449
227k
      if (bsPayLen > 0) {
1450
26.8k
        valBits = bsPayLen - ((INT)startPos - (INT)FDKgetValidBits(hBs));
1451
200k
      } else {
1452
200k
        valBits = (INT)FDKgetValidBits(hBs);
1453
200k
      }
1454
1455
      /* sanity check of remaining bits */
1456
227k
      if (valBits < 0) {
1457
27.2k
        fDoDecodeSbrData = 0;
1458
199k
      } else {
1459
199k
        switch (self->coreCodec) {
1460
0
          case AOT_SBR:
1461
0
          case AOT_PS:
1462
2.10k
          case AOT_AAC_LC: {
1463
            /* This sanity check is only meaningful with General Audio
1464
             * bitstreams */
1465
2.10k
            alignBits = valBits & 0x7;
1466
1467
2.10k
            if (valBits > alignBits) {
1468
301
              fDoDecodeSbrData = 0;
1469
301
            }
1470
2.10k
          } break;
1471
197k
          default:
1472
            /* No sanity check available */
1473
197k
            break;
1474
199k
        }
1475
199k
      }
1476
227k
    }
1477
242k
  } 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
38.5k
    errorStatus = SBRDEC_PARSE_ERROR;
1482
38.5k
  }
1483
1484
280k
  if (crcFlag && (hSbrHeader->syncState >= SBR_HEADER) && fDoDecodeSbrData) {
1485
13.8k
    FDKpushFor(hBs, alignBits);
1486
13.8k
    FDKcrcEndReg(&crcInfo, hBs, crcReg); /* End CRC region */
1487
13.8k
    FDKpushBack(hBs, alignBits);
1488
    /* Check CRC */
1489
13.8k
    if ((FDKcrcGetCRC(&crcInfo) ^ crcStartValue) != sbrCrc) {
1490
12.8k
      fDoDecodeSbrData = 0;
1491
12.8k
      if (headerStatus != HEADER_NOT_PRESENT) {
1492
11.6k
        headerStatus = HEADER_ERROR;
1493
11.6k
        hSbrHeader->syncState = SBR_NOT_INITIALIZED;
1494
11.6k
      }
1495
12.8k
    }
1496
13.8k
  }
1497
1498
280k
  if (!fDoDecodeSbrData) {
1499
    /* Set error flag for this slot to trigger concealment */
1500
89.1k
    setFrameErrorFlag(self->pSbrElement[elementIndex], FRAME_ERROR);
1501
    /* restore old frameData for concealment */
1502
89.1k
    FDKmemcpy(hFrameDataLeft, &frameDataLeftCopy, sizeof(SBR_FRAME_DATA));
1503
89.1k
    if (stereo) {
1504
35.0k
      FDKmemcpy(hFrameDataRight, &frameDataRightCopy, sizeof(SBR_FRAME_DATA));
1505
35.0k
    }
1506
89.1k
    errorStatus = SBRDEC_PARSE_ERROR;
1507
191k
  } else {
1508
    /* Everything seems to be ok so clear the error flag */
1509
191k
    setFrameErrorFlag(self->pSbrElement[elementIndex], FRAME_OK);
1510
191k
  }
1511
1512
280k
  if (!stereo) {
1513
    /* Turn coupling off explicitely to avoid access to absent right frame data
1514
       that might occur with corrupt bitstreams. */
1515
177k
    hFrameDataLeft->coupling = COUPLING_OFF;
1516
177k
  }
1517
1518
280k
bail:
1519
1520
280k
  if (self != NULL) {
1521
280k
    if (self->flags & SBRDEC_SYNTAX_DRM) {
1522
0
      hBs = hBsOriginal;
1523
0
    }
1524
1525
280k
    if (errorStatus != SBRDEC_NOT_INITIALIZED) {
1526
280k
      int useOldHdr =
1527
280k
          ((headerStatus == HEADER_NOT_PRESENT) ||
1528
231k
           (headerStatus == HEADER_ERROR) ||
1529
187k
           (headerStatus == HEADER_RESET && errorStatus == SBRDEC_PARSE_ERROR))
1530
280k
              ? 1
1531
280k
              : 0;
1532
1533
280k
      if (!useOldHdr && (thisHdrSlot != lastHdrSlot) && (hSbrHeader != NULL)) {
1534
5.50k
        useOldHdr |=
1535
5.50k
            (compareSbrHeader(hSbrHeader,
1536
5.50k
                              &self->sbrHeader[elementIndex][lastHdrSlot]) == 0)
1537
5.50k
                ? 1
1538
5.50k
                : 0;
1539
5.50k
      }
1540
1541
280k
      if (hSbrElement != NULL) {
1542
280k
        if (useOldHdr != 0) {
1543
          /* Use the old header for this frame */
1544
125k
          hSbrElement->useHeaderSlot[hSbrElement->useFrameSlot] = lastHdrSlot;
1545
154k
        } else {
1546
          /* Use the new header for this frame */
1547
154k
          hSbrElement->useHeaderSlot[hSbrElement->useFrameSlot] = thisHdrSlot;
1548
154k
        }
1549
1550
        /* Move frame pointer to the next slot which is up to be decoded/applied
1551
         * next */
1552
280k
        hSbrElement->useFrameSlot =
1553
280k
            (hSbrElement->useFrameSlot + 1) % (self->numDelayFrames + 1);
1554
280k
      }
1555
280k
    }
1556
280k
  }
1557
1558
280k
  *count -= startPos - (INT)FDKgetValidBits(hBs);
1559
1560
280k
  return errorStatus;
1561
280k
}
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
320k
    int *numOutChannels, const int psPossible) {
1581
320k
  SBR_DECODER_ELEMENT *hSbrElement = self->pSbrElement[elementIndex];
1582
320k
  HANDLE_SBR_CHANNEL *pSbrChannel =
1583
320k
      self->pSbrElement[elementIndex]->pSbrChannel;
1584
320k
  HANDLE_SBR_HEADER_DATA hSbrHeader =
1585
320k
      &self->sbrHeader[elementIndex]
1586
320k
                      [hSbrElement->useHeaderSlot[hSbrElement->useFrameSlot]];
1587
320k
  HANDLE_PS_DEC h_ps_d = self->hParametricStereoDec;
1588
1589
  /* get memory for frame data from scratch */
1590
320k
  SBR_FRAME_DATA *hFrameDataLeft = NULL;
1591
320k
  SBR_FRAME_DATA *hFrameDataRight = NULL;
1592
1593
320k
  SBR_ERROR errorStatus = SBRDEC_OK;
1594
1595
320k
  INT strideOut, offset0 = 255, offset0_block = 0, offset1 = 255,
1596
320k
                 offset1_block = 0;
1597
320k
  INT codecFrameSize = self->codecFrameSize;
1598
1599
320k
  int stereo = (hSbrElement->elementID == ID_CPE) ? 1 : 0;
1600
320k
  int numElementChannels =
1601
320k
      hSbrElement
1602
320k
          ->nChannels; /* Number of channels of the current SBR element */
1603
1604
320k
  hFrameDataLeft =
1605
320k
      &hSbrElement->pSbrChannel[0]->frameData[hSbrElement->useFrameSlot];
1606
320k
  if (stereo) {
1607
132k
    hFrameDataRight =
1608
132k
        &hSbrElement->pSbrChannel[1]->frameData[hSbrElement->useFrameSlot];
1609
132k
  }
1610
1611
320k
  if (self->flags & SBRDEC_FLUSH) {
1612
23
    if (self->numFlushedFrames > self->numDelayFrames) {
1613
23
      int hdrIdx;
1614
      /* No valid SBR payload available, hence switch to upsampling (in all
1615
       * headers) */
1616
69
      for (hdrIdx = 0; hdrIdx < ((1) + 1); hdrIdx += 1) {
1617
46
        if (self->sbrHeader[elementIndex][hdrIdx].syncState > UPSAMPLING) {
1618
22
          self->sbrHeader[elementIndex][hdrIdx].syncState = UPSAMPLING;
1619
22
        }
1620
46
      }
1621
23
    } 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
23
  }
1640
1641
  /* Update the header error flag */
1642
320k
  hSbrHeader->frameErrorFlag =
1643
320k
      hSbrElement->frameErrorFlag[hSbrElement->useFrameSlot];
1644
1645
  /*
1646
     Prepare filterbank for upsampling if no valid bit stream data is available.
1647
   */
1648
320k
  if (hSbrHeader->syncState == SBR_NOT_INITIALIZED) {
1649
38.3k
    errorStatus =
1650
38.3k
        initHeaderData(hSbrHeader, self->sampleRateIn, self->sampleRateOut,
1651
38.3k
                       self->downscaleFactor, codecFrameSize, self->flags,
1652
38.3k
                       1 /* SET_DEFAULT_HDR */
1653
38.3k
        );
1654
1655
38.3k
    if (errorStatus != SBRDEC_OK) {
1656
0
      return errorStatus;
1657
0
    }
1658
1659
38.3k
    hSbrHeader->syncState = UPSAMPLING;
1660
1661
38.3k
    errorStatus = sbrDecoder_HeaderUpdate(self, hSbrHeader, HEADER_NOT_PRESENT,
1662
38.3k
                                          pSbrChannel, hSbrElement->nChannels);
1663
1664
38.3k
    if (errorStatus != SBRDEC_OK) {
1665
0
      hSbrHeader->syncState = SBR_NOT_INITIALIZED;
1666
0
      return errorStatus;
1667
0
    }
1668
38.3k
  }
1669
1670
  /* reset */
1671
320k
  if (hSbrHeader->status & SBRDEC_HDR_STAT_RESET) {
1672
160k
    int ch;
1673
160k
    int applySbrProc = (hSbrHeader->syncState == SBR_ACTIVE ||
1674
160k
                        (hSbrHeader->frameErrorFlag == 0 &&
1675
93.2k
                         hSbrHeader->syncState == SBR_HEADER));
1676
418k
    for (ch = 0; ch < numElementChannels; ch++) {
1677
258k
      SBR_ERROR errorStatusTmp = SBRDEC_OK;
1678
1679
258k
      errorStatusTmp = resetSbrDec(
1680
258k
          &pSbrChannel[ch]->SbrDec, hSbrHeader, &pSbrChannel[ch]->prevFrameData,
1681
258k
          self->synDownsampleFac, self->flags, pSbrChannel[ch]->frameData);
1682
1683
258k
      if (errorStatusTmp != SBRDEC_OK) {
1684
4.72k
        hSbrHeader->syncState = UPSAMPLING;
1685
4.72k
      }
1686
258k
    }
1687
160k
    if (applySbrProc) {
1688
48.8k
      hSbrHeader->status &= ~SBRDEC_HDR_STAT_RESET;
1689
48.8k
    }
1690
160k
  }
1691
1692
  /* decoding */
1693
320k
  if ((hSbrHeader->syncState == SBR_ACTIVE) ||
1694
165k
      ((hSbrHeader->syncState == SBR_HEADER) &&
1695
204k
       (hSbrHeader->frameErrorFlag == 0))) {
1696
204k
    errorStatus = SBRDEC_OK;
1697
1698
204k
    decodeSbrData(hSbrHeader, hFrameDataLeft, &pSbrChannel[0]->prevFrameData,
1699
204k
                  (stereo) ? hFrameDataRight : NULL,
1700
204k
                  (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
204k
    hSbrHeader->syncState = SBR_ACTIVE;
1705
204k
  }
1706
1707
320k
  if (timeDataSize <
1708
320k
      hSbrHeader->numberTimeSlots * hSbrHeader->timeStep *
1709
320k
          self->pQmfDomain->globalConf.nBandsSynthesis *
1710
320k
          (psPossible ? fMax(2, numInChannels) : numInChannels)) {
1711
0
    return SBRDEC_OUTPUT_BUFFER_TOO_SMALL;
1712
0
  }
1713
1714
320k
  {
1715
320k
    self->flags &= ~SBRDEC_PS_DECODED;
1716
320k
    C_ALLOC_SCRATCH_START(pPsScratch, struct PS_DEC_COEFFICIENTS, 1)
1717
1718
    /* decode PS data if available */
1719
320k
    if (h_ps_d != NULL && psPossible && (hSbrHeader->syncState == SBR_ACTIVE)) {
1720
3.76k
      int applyPs = 1;
1721
1722
      /* define which frame delay line slot to process */
1723
3.76k
      h_ps_d->processSlot = hSbrElement->useFrameSlot;
1724
1725
3.76k
      applyPs = DecodePs(h_ps_d, hSbrHeader->frameErrorFlag, pPsScratch);
1726
3.76k
      self->flags |= (applyPs) ? SBRDEC_PS_DECODED : 0;
1727
3.76k
    }
1728
1729
320k
    offset0 = FDK_chMapDescr_getMapValue(mapDescr, channelIndex, mapIdx);
1730
320k
    offset0_block = offset0 * codecFrameSize;
1731
320k
    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
138k
      offset1 = FDK_chMapDescr_getMapValue(mapDescr, channelIndex + 1, mapIdx);
1737
138k
      offset1_block = offset1 * codecFrameSize;
1738
138k
    }
1739
    /* Set strides for reading and writing */
1740
320k
    if (psPossible)
1741
5.59k
      strideOut = (numInChannels < 2) ? 2 : numInChannels;
1742
315k
    else
1743
315k
      strideOut = numInChannels;
1744
1745
    /* use same buffers for left and right channel and apply PS per timeslot */
1746
    /* Process left channel */
1747
320k
    sbr_dec(&pSbrChannel[0]->SbrDec, input + offset0_block, timeData + offset0,
1748
320k
            (self->flags & SBRDEC_PS_DECODED) ? &pSbrChannel[1]->SbrDec : NULL,
1749
320k
            timeData + offset1, strideOut, hSbrHeader, hFrameDataLeft,
1750
320k
            &pSbrChannel[0]->prevFrameData,
1751
320k
            (hSbrHeader->syncState == SBR_ACTIVE), h_ps_d, self->flags,
1752
320k
            codecFrameSize, self->sbrInDataHeadroom);
1753
1754
320k
    if (stereo) {
1755
      /* Process right channel */
1756
132k
      sbr_dec(&pSbrChannel[1]->SbrDec, input + offset1_block,
1757
132k
              timeData + offset1, NULL, NULL, strideOut, hSbrHeader,
1758
132k
              hFrameDataRight, &pSbrChannel[1]->prevFrameData,
1759
132k
              (hSbrHeader->syncState == SBR_ACTIVE), NULL, self->flags,
1760
132k
              codecFrameSize, self->sbrInDataHeadroom);
1761
132k
    }
1762
1763
320k
    C_ALLOC_SCRATCH_END(pPsScratch, struct PS_DEC_COEFFICIENTS, 1)
1764
320k
  }
1765
1766
320k
  if (h_ps_d != NULL) {
1767
    /* save PS status for next run */
1768
27.6k
    h_ps_d->psDecodedPrv = (self->flags & SBRDEC_PS_DECODED) ? 1 : 0;
1769
27.6k
  }
1770
1771
320k
  if (psPossible && !(self->flags & SBRDEC_SKIP_QMF_SYN)) {
1772
5.59k
    FDK_ASSERT(strideOut > 1);
1773
5.59k
    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
2.28k
      int copyFrameSize =
1778
2.28k
          codecFrameSize * self->pQmfDomain->QmfDomainOut->fb.no_channels;
1779
2.28k
      copyFrameSize /= self->pQmfDomain->QmfDomainIn->fb.no_channels;
1780
2.28k
      LONG *ptr;
1781
2.28k
      INT i;
1782
2.28k
      FDK_ASSERT(strideOut == 2);
1783
1784
2.28k
      ptr = timeData;
1785
2.25M
      for (i = copyFrameSize >> 1; i--;) {
1786
2.25M
        LONG tmp; /* This temporal variable is required because some compilers
1787
                     can't do *ptr++ = *ptr++ correctly. */
1788
2.25M
        tmp = *ptr++;
1789
2.25M
        *ptr++ = tmp;
1790
2.25M
        tmp = *ptr++;
1791
2.25M
        *ptr++ = tmp;
1792
2.25M
      }
1793
2.28k
    }
1794
5.59k
    *numOutChannels = 2; /* Output minimum two channels when PS is enabled. */
1795
5.59k
  }
1796
1797
320k
  return errorStatus;
1798
320k
}
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
271k
                           INT *outDataHeadroom) {
1807
271k
  SBR_ERROR errorStatus = SBRDEC_OK;
1808
1809
271k
  int psPossible;
1810
271k
  int sbrElementNum;
1811
271k
  int numCoreChannels;
1812
271k
  int numSbrChannels = 0;
1813
1814
271k
  if ((self == NULL) || (timeData == NULL) || (numChannels == NULL) ||
1815
271k
      (sampleRate == NULL) || (psDecoded == NULL) ||
1816
271k
      !FDK_chMapDescr_isValid(mapDescr)) {
1817
0
    return SBRDEC_INVALID_ARGUMENT;
1818
0
  }
1819
1820
271k
  psPossible = *psDecoded;
1821
271k
  numCoreChannels = *numChannels;
1822
271k
  if (numCoreChannels <= 0) {
1823
0
    return SBRDEC_INVALID_ARGUMENT;
1824
0
  }
1825
1826
271k
  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
593k
  for (sbrElementNum = 0; sbrElementNum < self->numSbrElements;
1833
322k
       sbrElementNum++) {
1834
322k
    if (self->pSbrElement[sbrElementNum] == NULL) {
1835
202
      return SBRDEC_NOT_INITIALIZED;
1836
202
    }
1837
322k
  }
1838
1839
271k
  if (self->numSbrElements != 1 || self->pSbrElement[0]->elementID != ID_SCE) {
1840
111k
    psPossible = 0;
1841
111k
  }
1842
1843
271k
  self->sbrInDataHeadroom = inDataHeadroom;
1844
271k
  *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
271k
  if (psPossible == 0) {
1849
265k
    self->flags &= ~SBRDEC_PS_DECODED;
1850
265k
  }
1851
1852
  /* replaces channel based reset inside sbr_dec() */
1853
271k
  if (((self->flags & SBRDEC_LOW_POWER) ? 1 : 0) !=
1854
271k
      ((self->pQmfDomain->globalConf.flags & QMF_FLAG_LP) ? 1 : 0)) {
1855
11.9k
    if (self->flags & SBRDEC_LOW_POWER) {
1856
11.3k
      self->pQmfDomain->globalConf.flags |= QMF_FLAG_LP;
1857
11.3k
      self->pQmfDomain->globalConf.flags_requested |= QMF_FLAG_LP;
1858
11.3k
    } else {
1859
603
      self->pQmfDomain->globalConf.flags &= ~QMF_FLAG_LP;
1860
603
      self->pQmfDomain->globalConf.flags_requested &= ~QMF_FLAG_LP;
1861
603
    }
1862
11.9k
    if (FDK_QmfDomain_InitFilterBank(self->pQmfDomain, QMF_FLAG_KEEP_STATES)) {
1863
0
      return SBRDEC_UNSUPPORTED_CONFIG;
1864
0
    }
1865
11.9k
  }
1866
271k
  if (self->numSbrChannels > self->pQmfDomain->globalConf.nInputChannels) {
1867
0
    return SBRDEC_UNSUPPORTED_CONFIG;
1868
0
  }
1869
1870
271k
  if (self->flags & SBRDEC_FLUSH) {
1871
    /* flushing is signalized, hence increment the flush frame counter */
1872
23
    self->numFlushedFrames++;
1873
271k
  } else {
1874
    /* no flushing is signalized, hence reset the flush frame counter */
1875
271k
    self->numFlushedFrames = 0;
1876
271k
  }
1877
1878
  /* Loop over SBR elements */
1879
324k
  for (sbrElementNum = 0; sbrElementNum < self->numSbrElements;
1880
320k
       sbrElementNum++) {
1881
320k
    int numElementChan;
1882
1883
320k
    if (psPossible &&
1884
5.59k
        self->pSbrElement[sbrElementNum]->pSbrChannel[1] == NULL) {
1885
      /* Disable PS and try decoding SBR mono. */
1886
0
      psPossible = 0;
1887
0
    }
1888
1889
320k
    numElementChan =
1890
320k
        (self->pSbrElement[sbrElementNum]->elementID == ID_CPE) ? 2 : 1;
1891
1892
    /* If core signal is bad then force upsampling */
1893
320k
    if (!coreDecodedOk) {
1894
113k
      setFrameErrorFlag(self->pSbrElement[sbrElementNum], FRAME_ERROR_ALLSLOTS);
1895
113k
    }
1896
1897
320k
    errorStatus = sbrDecoder_DecodeElement(
1898
320k
        self, input, timeData, timeDataSize, mapDescr, mapIdx, numSbrChannels,
1899
320k
        sbrElementNum,
1900
320k
        numCoreChannels, /* is correct even for USC SCI==2 case */
1901
320k
        &numElementChan, psPossible);
1902
1903
320k
    if (errorStatus != SBRDEC_OK) {
1904
0
      goto bail;
1905
0
    }
1906
1907
320k
    numSbrChannels += numElementChan;
1908
1909
320k
    if (numSbrChannels >= numCoreChannels) {
1910
267k
      break;
1911
267k
    }
1912
320k
  }
1913
1914
  /* Update numChannels and samplerate */
1915
  /* Do not mess with output channels in case of USAC. numSbrChannels !=
1916
   * numChannels for stereoConfigIndex == 2 */
1917
271k
  if (!(self->flags & SBRDEC_SYNTAX_USAC)) {
1918
53.9k
    *numChannels = numSbrChannels;
1919
53.9k
  }
1920
271k
  *sampleRate = self->sampleRateOut;
1921
271k
  *psDecoded = (self->flags & SBRDEC_PS_DECODED) ? 1 : 0;
1922
1923
  /* Clear reset and flush flag because everything seems to be done
1924
   * successfully. */
1925
271k
  self->flags &= ~SBRDEC_FORCE_RESET;
1926
271k
  self->flags &= ~SBRDEC_FLUSH;
1927
1928
271k
bail:
1929
1930
271k
  return errorStatus;
1931
271k
}
1932
1933
31.7k
SBR_ERROR sbrDecoder_Close(HANDLE_SBRDECODER *pSelf) {
1934
31.7k
  HANDLE_SBRDECODER self = *pSelf;
1935
31.7k
  int i;
1936
1937
31.7k
  if (self != NULL) {
1938
31.7k
    if (self->hParametricStereoDec != NULL) {
1939
1.90k
      DeletePsDec(&self->hParametricStereoDec);
1940
1.90k
    }
1941
1942
286k
    for (i = 0; i < (8); i++) {
1943
254k
      sbrDecoder_DestroyElement(self, i);
1944
254k
    }
1945
1946
31.7k
    FreeRam_SbrDecoder(pSelf);
1947
31.7k
  }
1948
1949
31.7k
  return SBRDEC_OK;
1950
31.7k
}
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
252k
UINT sbrDecoder_GetDelay(const HANDLE_SBRDECODER self) {
1984
252k
  UINT outputDelay = 0;
1985
1986
252k
  if (self != NULL) {
1987
252k
    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
252k
    if ((self->numSbrChannels > 0) && (self->numSbrElements > 0)) {
1993
      /* Add QMF synthesis delay */
1994
252k
      if ((flags & SBRDEC_ELD_GRID) && IS_LOWDELAY(self->coreCodec)) {
1995
        /* Low delay SBR: */
1996
27.8k
        if (!(flags & SBRDEC_SKIP_QMF_SYN)) {
1997
27.0k
          outputDelay +=
1998
27.0k
              (flags & SBRDEC_DOWNSAMPLE) ? 32 : 64; /* QMF synthesis */
1999
27.0k
          if (flags & SBRDEC_LD_MPS_QMF) {
2000
814
            outputDelay += 32;
2001
814
          }
2002
27.0k
        }
2003
224k
      } else if (!IS_USAC(self->coreCodec)) {
2004
        /* By the method of elimination this is the GA (AAC-LC, HE-AAC, ...)
2005
         * branch: */
2006
26.0k
        outputDelay += (flags & SBRDEC_DOWNSAMPLE) ? 481 : 962;
2007
26.0k
        if (flags & SBRDEC_SKIP_QMF_SYN) {
2008
0
          outputDelay -= 257; /* QMF synthesis */
2009
0
        }
2010
26.0k
      }
2011
252k
    }
2012
252k
  }
2013
2014
252k
  return (outputDelay);
2015
252k
}