Coverage Report

Created: 2026-02-14 06:59

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