Coverage Report

Created: 2025-07-01 06:21

/src/aac/libAACdec/src/channel.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2020 Fraunhofer-Gesellschaft zur Förderung der angewandten
5
Forschung e.V. All rights reserved.
6
7
 1.    INTRODUCTION
8
The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9
that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10
scheme for digital audio. This FDK AAC Codec software is intended to be used on
11
a wide variety of Android devices.
12
13
AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14
general perceptual audio codecs. AAC-ELD is considered the best-performing
15
full-bandwidth communications codec by independent studies and is widely
16
deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17
specifications.
18
19
Patent licenses for necessary patent claims for the FDK AAC Codec (including
20
those of Fraunhofer) may be obtained through Via Licensing
21
(www.vialicensing.com) or through the respective patent owners individually for
22
the purpose of encoding or decoding bit streams in products that are compliant
23
with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24
Android devices already license these patent claims through Via Licensing or
25
directly from the patent owners, and therefore FDK AAC Codec software may
26
already be covered under those patent licenses when it is used for those
27
licensed purposes only.
28
29
Commercially-licensed AAC software libraries, including floating-point versions
30
with enhanced sound quality, are also available from Fraunhofer. Users are
31
encouraged to check the Fraunhofer website for additional applications
32
information and documentation.
33
34
2.    COPYRIGHT LICENSE
35
36
Redistribution and use in source and binary forms, with or without modification,
37
are permitted without payment of copyright license fees provided that you
38
satisfy the following conditions:
39
40
You must retain the complete text of this software license in redistributions of
41
the FDK AAC Codec or your modifications thereto in source code form.
42
43
You must retain the complete text of this software license in the documentation
44
and/or other materials provided with redistributions of the FDK AAC Codec or
45
your modifications thereto in binary form. You must make available free of
46
charge copies of the complete source code of the FDK AAC Codec and your
47
modifications thereto to recipients of copies in binary form.
48
49
The name of Fraunhofer may not be used to endorse or promote products derived
50
from this library without prior written permission.
51
52
You may not charge copyright license fees for anyone to use, copy or distribute
53
the FDK AAC Codec software or your modifications thereto.
54
55
Your modified versions of the FDK AAC Codec must carry prominent notices stating
56
that you changed the software and the date of any change. For modified versions
57
of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58
must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59
AAC Codec Library for Android."
60
61
3.    NO PATENT LICENSE
62
63
NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64
limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65
Fraunhofer provides no warranty of patent non-infringement with respect to this
66
software.
67
68
You may use this FDK AAC Codec software or modifications thereto only for
69
purposes that are authorized by appropriate patent licenses.
70
71
4.    DISCLAIMER
72
73
This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74
holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75
including but not limited to the implied warranties of merchantability and
76
fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77
CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78
or consequential damages, including but not limited to procurement of substitute
79
goods or services; loss of use, data, or profits, or business interruption,
80
however caused and on any theory of liability, whether in contract, strict
81
liability, or tort (including negligence), arising in any way out of the use of
82
this software, even if advised of the possibility of such damage.
83
84
5.    CONTACT INFORMATION
85
86
Fraunhofer Institute for Integrated Circuits IIS
87
Attention: Audio and Multimedia Departments - FDK AAC LL
88
Am Wolfsmantel 33
89
91058 Erlangen, Germany
90
91
www.iis.fraunhofer.de/amm
92
amm-info@iis.fraunhofer.de
93
----------------------------------------------------------------------------- */
94
95
/**************************** AAC decoder library ******************************
96
97
   Author(s):   Josef Hoepfl
98
99
   Description:
100
101
*******************************************************************************/
102
103
#include "channel.h"
104
#include "aacdecoder.h"
105
#include "block.h"
106
#include "aacdec_tns.h"
107
#include "FDK_bitstream.h"
108
109
#include "conceal.h"
110
111
#include "rvlc.h"
112
113
#include "aacdec_hcr.h"
114
115
#include "usacdec_lpd.h"
116
#include "usacdec_fac.h"
117
118
static void MapMidSideMaskToPnsCorrelation(
119
0
    CAacDecoderChannelInfo *pAacDecoderChannelInfo[2]) {
120
0
  int group;
121
122
0
  for (group = 0; group < pAacDecoderChannelInfo[L]->icsInfo.WindowGroups;
123
0
       group++) {
124
0
    UCHAR groupMask = 1 << group;
125
126
0
    for (UCHAR band = 0; band < pAacDecoderChannelInfo[L]->icsInfo.MaxSfBands;
127
0
         band++) {
128
0
      if (pAacDecoderChannelInfo[L]->pComData->jointStereoData.MsUsed[band] &
129
0
          groupMask) { /* channels are correlated */
130
0
        CPns_SetCorrelation(&pAacDecoderChannelInfo[L]->data.aac.PnsData, group,
131
0
                            band, 0);
132
133
0
        if (CPns_IsPnsUsed(&pAacDecoderChannelInfo[L]->data.aac.PnsData, group,
134
0
                           band) &&
135
0
            CPns_IsPnsUsed(&pAacDecoderChannelInfo[R]->data.aac.PnsData, group,
136
0
                           band))
137
0
          pAacDecoderChannelInfo[L]->pComData->jointStereoData.MsUsed[band] ^=
138
0
              groupMask; /* clear the groupMask-bit */
139
0
      }
140
0
    }
141
0
  }
142
0
}
143
144
static void Clean_Complex_Prediction_coefficients(
145
    CJointStereoPersistentData *pJointStereoPersistentData, int windowGroups,
146
0
    const int low_limit, const int high_limit) {
147
0
  for (int group = 0; group < windowGroups; group++) {
148
0
    for (int sfb = low_limit; sfb < high_limit; sfb++) {
149
0
      pJointStereoPersistentData->alpha_q_re_prev[group][sfb] = 0;
150
0
      pJointStereoPersistentData->alpha_q_im_prev[group][sfb] = 0;
151
0
    }
152
0
  }
153
0
}
154
155
/*!
156
  \brief Decode channel pair element
157
158
  The function decodes a channel pair element.
159
160
  \return  none
161
*/
162
void CChannelElement_Decode(
163
    CAacDecoderChannelInfo
164
        *pAacDecoderChannelInfo[2], /*!< pointer to aac decoder channel info */
165
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo[2],
166
    SamplingRateInfo *pSamplingRateInfo, UINT flags, UINT elFlags,
167
0
    int el_channels) {
168
0
  int ch = 0;
169
170
0
  int maxSfBandsL = 0, maxSfBandsR = 0;
171
0
  int maybe_jstereo = (el_channels > 1);
172
173
0
  if (flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA) && el_channels == 2) {
174
0
    if (pAacDecoderChannelInfo[L]->data.usac.core_mode ||
175
0
        pAacDecoderChannelInfo[R]->data.usac.core_mode) {
176
0
      maybe_jstereo = 0;
177
0
    }
178
0
  }
179
180
0
  if (maybe_jstereo) {
181
0
    maxSfBandsL =
182
0
        GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo[L]->icsInfo);
183
0
    maxSfBandsR =
184
0
        GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo[R]->icsInfo);
185
186
    /* apply ms */
187
0
    if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow) {
188
0
      if (!(flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA))) {
189
0
        if (pAacDecoderChannelInfo[L]->data.aac.PnsData.PnsActive ||
190
0
            pAacDecoderChannelInfo[R]->data.aac.PnsData.PnsActive) {
191
0
          MapMidSideMaskToPnsCorrelation(pAacDecoderChannelInfo);
192
0
        }
193
0
      }
194
      /* if tns_on_lr == 1 run MS */ /* &&
195
                                        (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_active
196
                                        == 1) */
197
0
      if (((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) &&
198
0
           (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_on_lr ==
199
0
            1)) ||
200
0
          ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) == 0)) {
201
0
        int max_sfb_ste = (INT)(pAacDecoderChannelInfo[L]->icsInfo.max_sfb_ste);
202
203
0
        CJointStereo_ApplyMS(
204
0
            pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo,
205
0
            pAacDecoderChannelInfo[L]->pSpectralCoefficient,
206
0
            pAacDecoderChannelInfo[R]->pSpectralCoefficient,
207
0
            pAacDecoderChannelInfo[L]->pDynData->aSfbScale,
208
0
            pAacDecoderChannelInfo[R]->pDynData->aSfbScale,
209
0
            pAacDecoderChannelInfo[L]->specScale,
210
0
            pAacDecoderChannelInfo[R]->specScale,
211
0
            GetScaleFactorBandOffsets(&pAacDecoderChannelInfo[L]->icsInfo,
212
0
                                      pSamplingRateInfo),
213
0
            GetWindowGroupLengthTable(&pAacDecoderChannelInfo[L]->icsInfo),
214
0
            GetWindowGroups(&pAacDecoderChannelInfo[L]->icsInfo), max_sfb_ste,
215
0
            maxSfBandsL, maxSfBandsR,
216
0
            pAacDecoderChannelInfo[L]
217
0
                ->pComData->jointStereoData.store_dmx_re_prev,
218
0
            &(pAacDecoderChannelInfo[L]
219
0
                  ->pComData->jointStereoData.store_dmx_re_prev_e),
220
0
            1);
221
222
0
      } /* if ( ((elFlags & AC_EL_USAC_CP_POSSIBLE).... */
223
0
    }   /* if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow)*/
224
225
    /* apply intensity stereo */ /* modifies pAacDecoderChannelInfo[]->aSpecSfb
226
                                  */
227
0
    if (!(flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA))) {
228
0
      if ((pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow ==
229
0
           1) &&
230
0
          (el_channels == 2)) {
231
0
        CJointStereo_ApplyIS(
232
0
            pAacDecoderChannelInfo,
233
0
            GetScaleFactorBandOffsets(&pAacDecoderChannelInfo[L]->icsInfo,
234
0
                                      pSamplingRateInfo),
235
0
            GetWindowGroupLengthTable(&pAacDecoderChannelInfo[L]->icsInfo),
236
0
            GetWindowGroups(&pAacDecoderChannelInfo[L]->icsInfo),
237
0
            GetScaleFactorBandsTransmitted(
238
0
                &pAacDecoderChannelInfo[L]->icsInfo));
239
0
      }
240
0
    }
241
0
  } /* maybe_stereo */
242
243
0
  for (ch = 0; ch < el_channels; ch++) {
244
0
    if (pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_LPD) {
245
      /* Decode LPD data */
246
0
      CLpdChannelStream_Decode(pAacDecoderChannelInfo[ch],
247
0
                               pAacDecoderStaticChannelInfo[ch], flags);
248
0
    } else {
249
0
      UCHAR noSfbs =
250
0
          GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo[ch]->icsInfo);
251
      /* For USAC common window: max_sfb of both channels may differ
252
       * (common_max_sfb == 0). */
253
0
      if ((maybe_jstereo == 1) &&
254
0
          (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow ==
255
0
           1)) {
256
0
        noSfbs = fMax(maxSfBandsL, maxSfBandsR);
257
0
      }
258
0
      int CP_active = 0;
259
0
      if (elFlags & AC_EL_USAC_CP_POSSIBLE) {
260
0
        CP_active = pAacDecoderChannelInfo[ch]
261
0
                        ->pComData->jointStereoData.cplx_pred_flag;
262
0
      }
263
264
      /* Omit writing of pAacDecoderChannelInfo[ch]->specScale for complex
265
         stereo prediction since scaling has already been carried out. */
266
0
      int max_sfb_ste = (INT)(pAacDecoderChannelInfo[L]->icsInfo.max_sfb_ste);
267
268
0
      if (!(CP_active && (max_sfb_ste == noSfbs)) ||
269
0
          !(CP_active &&
270
0
            !(pAacDecoderChannelInfo[ch]->pDynData->TnsData.Active)) ||
271
0
          ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) &&
272
0
           (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_on_lr ==
273
0
            0))) {
274
0
        CBlock_ScaleSpectralData(pAacDecoderChannelInfo[ch], noSfbs,
275
0
                                 pSamplingRateInfo);
276
277
        /*Active for the case of TNS applied before MS/CP*/
278
0
        if ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) &&
279
0
            (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_on_lr ==
280
0
             0)) {
281
0
          if (IsLongBlock(&pAacDecoderChannelInfo[ch]->icsInfo)) {
282
0
            for (int i = 0; i < noSfbs; i++) {
283
0
              pAacDecoderChannelInfo[ch]->pDynData->aSfbScale[i] =
284
0
                  pAacDecoderChannelInfo[ch]->specScale[0];
285
0
            }
286
0
          } else {
287
0
            for (int i = 0; i < 8; i++) {
288
0
              for (int j = 0; j < noSfbs; j++) {
289
0
                pAacDecoderChannelInfo[ch]->pDynData->aSfbScale[i * 16 + j] =
290
0
                    pAacDecoderChannelInfo[ch]->specScale[i];
291
0
              }
292
0
            }
293
0
          }
294
0
        }
295
0
      }
296
0
    }
297
0
  } /* End "for (ch = 0; ch < el_channels; ch++)" */
298
299
0
  if (maybe_jstereo) {
300
    /* apply ms */
301
0
    if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow) {
302
0
    } /* CommonWindow */
303
0
    else {
304
0
      if (elFlags & AC_EL_USAC_CP_POSSIBLE) {
305
0
        FDKmemclear(
306
0
            pAacDecoderStaticChannelInfo[L]
307
0
                ->pCpeStaticData->jointStereoPersistentData.alpha_q_re_prev,
308
0
            JointStereoMaximumGroups * JointStereoMaximumBands * sizeof(SHORT));
309
0
        FDKmemclear(
310
0
            pAacDecoderStaticChannelInfo[L]
311
0
                ->pCpeStaticData->jointStereoPersistentData.alpha_q_im_prev,
312
0
            JointStereoMaximumGroups * JointStereoMaximumBands * sizeof(SHORT));
313
0
      }
314
0
    }
315
316
0
  } /* if (maybe_jstereo) */
317
318
0
  for (ch = 0; ch < el_channels; ch++) {
319
0
    if (pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_LPD) {
320
0
    } else {
321
0
      if (!(flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA))) {
322
        /* Use same seed for coupled channels (CPE) */
323
0
        int pnsCh = (ch > 0) ? L : ch;
324
0
        CPns_UpdateNoiseState(
325
0
            &pAacDecoderChannelInfo[ch]->data.aac.PnsData,
326
0
            pAacDecoderChannelInfo[pnsCh]->data.aac.PnsData.currentSeed,
327
0
            pAacDecoderChannelInfo[ch]->pComData->pnsRandomSeed);
328
0
      }
329
330
0
      if ((!(flags & (AC_USAC))) ||
331
0
          ((flags & (AC_USAC)) &&
332
0
           (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_active ==
333
0
            1)) ||
334
0
          (maybe_jstereo == 0)) {
335
0
        ApplyTools(
336
0
            pAacDecoderChannelInfo, pSamplingRateInfo, flags, elFlags, ch,
337
0
            pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow);
338
0
      }
339
0
    } /* End "} else" */
340
0
  }   /* End "for (ch = 0; ch < el_channels; ch++)" */
341
342
0
  if (maybe_jstereo) {
343
    /* apply ms */
344
0
    if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow) {
345
      /* if tns_on_lr == 0 run MS */
346
0
      if ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) &&
347
0
          (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_on_lr ==
348
0
           0)) {
349
0
        int max_sfb_ste = (INT)(pAacDecoderChannelInfo[L]->icsInfo.max_sfb_ste);
350
351
0
        CJointStereo_ApplyMS(
352
0
            pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo,
353
0
            pAacDecoderChannelInfo[L]->pSpectralCoefficient,
354
0
            pAacDecoderChannelInfo[R]->pSpectralCoefficient,
355
0
            pAacDecoderChannelInfo[L]->pDynData->aSfbScale,
356
0
            pAacDecoderChannelInfo[R]->pDynData->aSfbScale,
357
0
            pAacDecoderChannelInfo[L]->specScale,
358
0
            pAacDecoderChannelInfo[R]->specScale,
359
0
            GetScaleFactorBandOffsets(&pAacDecoderChannelInfo[L]->icsInfo,
360
0
                                      pSamplingRateInfo),
361
0
            GetWindowGroupLengthTable(&pAacDecoderChannelInfo[L]->icsInfo),
362
0
            GetWindowGroups(&pAacDecoderChannelInfo[L]->icsInfo), max_sfb_ste,
363
0
            maxSfBandsL, maxSfBandsR,
364
0
            pAacDecoderChannelInfo[L]
365
0
                ->pComData->jointStereoData.store_dmx_re_prev,
366
0
            &(pAacDecoderChannelInfo[L]
367
0
                  ->pComData->jointStereoData.store_dmx_re_prev_e),
368
0
            1);
369
0
      }
370
371
0
    } /* if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow) */
372
373
0
  } /* if (maybe_jstereo) */
374
375
0
  for (ch = 0; ch < el_channels; ch++) {
376
0
    if (elFlags & AC_EL_USAC_CP_POSSIBLE) {
377
0
      pAacDecoderStaticChannelInfo[L]
378
0
          ->pCpeStaticData->jointStereoPersistentData.clearSpectralCoeffs = 0;
379
0
    }
380
0
  }
381
382
0
  CRvlc_ElementCheck(pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo,
383
0
                     flags, el_channels);
384
0
}
385
386
void CChannel_CodebookTableInit(
387
0
    CAacDecoderChannelInfo *pAacDecoderChannelInfo) {
388
0
  int b, w, maxBands, maxWindows;
389
0
  int maxSfb = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
390
0
  UCHAR *pCodeBook = pAacDecoderChannelInfo->pDynData->aCodeBook;
391
392
0
  if (IsLongBlock(&pAacDecoderChannelInfo->icsInfo)) {
393
0
    maxBands = 64;
394
0
    maxWindows = 1;
395
0
  } else {
396
0
    maxBands = 16;
397
0
    maxWindows = 8;
398
0
  }
399
400
0
  for (w = 0; w < maxWindows; w++) {
401
0
    for (b = 0; b < maxSfb; b++) {
402
0
      pCodeBook[b] = ESCBOOK;
403
0
    }
404
0
    for (; b < maxBands; b++) {
405
0
      pCodeBook[b] = ZERO_HCB;
406
0
    }
407
0
    pCodeBook += maxBands;
408
0
  }
409
0
}
410
411
/*
412
 * Arbitrary order bitstream parser
413
 */
414
AAC_DECODER_ERROR CChannelElement_Read(
415
    HANDLE_FDK_BITSTREAM hBs, CAacDecoderChannelInfo *pAacDecoderChannelInfo[],
416
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo[],
417
    const AUDIO_OBJECT_TYPE aot, SamplingRateInfo *pSamplingRateInfo,
418
    const UINT flags, const UINT elFlags, const UINT frame_length,
419
    const UCHAR numberOfChannels, const SCHAR epConfig,
420
0
    HANDLE_TRANSPORTDEC pTpDec) {
421
0
  AAC_DECODER_ERROR error = AAC_DEC_OK;
422
0
  const element_list_t *list;
423
0
  int i, ch, decision_bit;
424
0
  int crcReg1 = -1, crcReg2 = -1;
425
0
  int cplxPred;
426
0
  int ind_sw_cce_flag = 0, num_gain_element_lists = 0;
427
428
0
  FDK_ASSERT((numberOfChannels == 1) || (numberOfChannels == 2));
429
430
  /* Get channel element sequence table */
431
0
  list = getBitstreamElementList(aot, epConfig, numberOfChannels, 0, elFlags);
432
0
  if (list == NULL) {
433
0
    error = AAC_DEC_UNSUPPORTED_FORMAT;
434
0
    goto bail;
435
0
  }
436
437
0
  CTns_Reset(&pAacDecoderChannelInfo[0]->pDynData->TnsData);
438
  /* Set common window to 0 by default. If signalized in the bit stream it will
439
   * be overwritten later explicitely */
440
0
  pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow = 0;
441
0
  if (flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) {
442
0
    pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_active = 0;
443
0
    pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_on_lr = 0;
444
0
  }
445
0
  if (numberOfChannels == 2) {
446
0
    CTns_Reset(&pAacDecoderChannelInfo[1]->pDynData->TnsData);
447
0
    pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow = 0;
448
0
  }
449
450
0
  cplxPred = 0;
451
0
  if (pAacDecoderStaticChannelInfo != NULL) {
452
0
    if (elFlags & AC_EL_USAC_CP_POSSIBLE) {
453
0
      pAacDecoderChannelInfo[0]->pComData->jointStereoData.cplx_pred_flag = 0;
454
0
      cplxPred = 1;
455
0
    }
456
0
  }
457
458
0
  if (0 || (flags & (AC_ELD | AC_SCALABLE))) {
459
0
    pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow = 1;
460
0
    if (numberOfChannels == 2) {
461
0
      pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow =
462
0
          pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow;
463
0
    }
464
0
  }
465
466
  /* Iterate through sequence table */
467
0
  i = 0;
468
0
  ch = 0;
469
0
  decision_bit = 0;
470
0
  do {
471
0
    switch (list->id[i]) {
472
0
      case element_instance_tag:
473
0
        pAacDecoderChannelInfo[0]->ElementInstanceTag = FDKreadBits(hBs, 4);
474
0
        if (numberOfChannels == 2) {
475
0
          pAacDecoderChannelInfo[1]->ElementInstanceTag =
476
0
              pAacDecoderChannelInfo[0]->ElementInstanceTag;
477
0
        }
478
0
        break;
479
0
      case common_window:
480
0
        decision_bit =
481
0
            pAacDecoderChannelInfo[ch]->pDynData->RawDataInfo.CommonWindow =
482
0
                FDKreadBits(hBs, 1);
483
0
        if (numberOfChannels == 2) {
484
0
          pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow =
485
0
              pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow;
486
0
        }
487
0
        break;
488
0
      case ics_info:
489
        /* store last window sequence (utilized in complex stereo prediction)
490
         * before reading new channel-info */
491
0
        if (cplxPred) {
492
0
          if (pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow) {
493
0
            pAacDecoderStaticChannelInfo[0]
494
0
                ->pCpeStaticData->jointStereoPersistentData.winSeqPrev =
495
0
                pAacDecoderChannelInfo[0]->icsInfo.WindowSequence;
496
0
            pAacDecoderStaticChannelInfo[0]
497
0
                ->pCpeStaticData->jointStereoPersistentData.winShapePrev =
498
0
                pAacDecoderChannelInfo[0]->icsInfo.WindowShape;
499
0
          }
500
0
        }
501
        /* Read individual channel info */
502
0
        error = IcsRead(hBs, &pAacDecoderChannelInfo[ch]->icsInfo,
503
0
                        pSamplingRateInfo, flags);
504
505
0
        if (elFlags & AC_EL_LFE &&
506
0
            GetWindowSequence(&pAacDecoderChannelInfo[ch]->icsInfo) !=
507
0
                BLOCK_LONG) {
508
0
          error = AAC_DEC_PARSE_ERROR;
509
0
          break;
510
0
        }
511
512
0
        if (numberOfChannels == 2 &&
513
0
            pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow) {
514
0
          pAacDecoderChannelInfo[1]->icsInfo =
515
0
              pAacDecoderChannelInfo[0]->icsInfo;
516
0
        }
517
0
        break;
518
519
0
      case common_max_sfb:
520
0
        if (FDKreadBit(hBs) == 0) {
521
0
          error = IcsReadMaxSfb(hBs, &pAacDecoderChannelInfo[1]->icsInfo,
522
0
                                pSamplingRateInfo);
523
0
        }
524
0
        break;
525
526
0
      case ltp_data_present:
527
0
        if (FDKreadBits(hBs, 1) != 0) {
528
0
          error = AAC_DEC_UNSUPPORTED_PREDICTION;
529
0
        }
530
0
        break;
531
532
0
      case ms:
533
534
0
        INT max_sfb_ste;
535
0
        INT max_sfb_ste_clear;
536
537
0
        max_sfb_ste = GetScaleMaxFactorBandsTransmitted(
538
0
            &pAacDecoderChannelInfo[0]->icsInfo,
539
0
            &pAacDecoderChannelInfo[1]->icsInfo);
540
541
0
        max_sfb_ste_clear = 64;
542
543
0
        pAacDecoderChannelInfo[0]->icsInfo.max_sfb_ste = (UCHAR)max_sfb_ste;
544
0
        pAacDecoderChannelInfo[1]->icsInfo.max_sfb_ste = (UCHAR)max_sfb_ste;
545
546
0
        if (flags & (AC_USAC | AC_RSV603DA) &&
547
0
            pAacDecoderChannelInfo[ch]->pDynData->RawDataInfo.CommonWindow ==
548
0
                0) {
549
0
          Clean_Complex_Prediction_coefficients(
550
0
              &pAacDecoderStaticChannelInfo[0]
551
0
                   ->pCpeStaticData->jointStereoPersistentData,
552
0
              GetWindowGroups(&pAacDecoderChannelInfo[0]->icsInfo), 0, 64);
553
0
        }
554
555
0
        if (CJointStereo_Read(
556
0
                hBs, &pAacDecoderChannelInfo[0]->pComData->jointStereoData,
557
0
                GetWindowGroups(&pAacDecoderChannelInfo[0]->icsInfo),
558
0
                max_sfb_ste, max_sfb_ste_clear,
559
                /* jointStereoPersistentData and cplxPredictionData are only
560
                   available/allocated if cplxPred is active. */
561
0
                ((cplxPred == 0) || (pAacDecoderStaticChannelInfo == NULL))
562
0
                    ? NULL
563
0
                    : &pAacDecoderStaticChannelInfo[0]
564
0
                           ->pCpeStaticData->jointStereoPersistentData,
565
0
                ((cplxPred == 0) || (pAacDecoderChannelInfo[0] == NULL))
566
0
                    ? NULL
567
0
                    : pAacDecoderChannelInfo[0]
568
0
                          ->pComStaticData->cplxPredictionData,
569
0
                cplxPred,
570
0
                GetScaleFactorBandsTotal(&pAacDecoderChannelInfo[0]->icsInfo),
571
0
                GetWindowSequence(&pAacDecoderChannelInfo[0]->icsInfo),
572
0
                flags)) {
573
0
          error = AAC_DEC_PARSE_ERROR;
574
0
        }
575
576
0
        break;
577
578
0
      case global_gain:
579
0
        pAacDecoderChannelInfo[ch]->pDynData->RawDataInfo.GlobalGain =
580
0
            (UCHAR)FDKreadBits(hBs, 8);
581
0
        break;
582
583
0
      case section_data:
584
0
        error = CBlock_ReadSectionData(hBs, pAacDecoderChannelInfo[ch],
585
0
                                       pSamplingRateInfo, flags);
586
0
        break;
587
588
0
      case scale_factor_data_usac:
589
0
        pAacDecoderChannelInfo[ch]->currAliasingSymmetry = 0;
590
        /* Set active sfb codebook indexes to HCB_ESC to make them "active" */
591
0
        CChannel_CodebookTableInit(
592
0
            pAacDecoderChannelInfo[ch]); /*  equals ReadSectionData(self,
593
                                            bs) in float soft. block.c
594
                                            line: ~599 */
595
        /* Note: The missing "break" is intentional here, since we need to call
596
         * CBlock_ReadScaleFactorData(). */
597
0
        FDK_FALLTHROUGH;
598
599
0
      case scale_factor_data:
600
0
        if (flags & AC_ER_RVLC) {
601
          /* read RVLC data from bitstream (error sens. cat. 1) */
602
0
          CRvlc_Read(pAacDecoderChannelInfo[ch], hBs);
603
0
        } else {
604
0
          error = CBlock_ReadScaleFactorData(pAacDecoderChannelInfo[ch], hBs,
605
0
                                             flags);
606
0
        }
607
0
        break;
608
609
0
      case pulse:
610
0
        if (CPulseData_Read(
611
0
                hBs,
612
0
                &pAacDecoderChannelInfo[ch]->pDynData->specificTo.aac.PulseData,
613
0
                pSamplingRateInfo->ScaleFactorBands_Long, /* pulse data is only
614
                                                             allowed to be
615
                                                             present in long
616
                                                             blocks! */
617
0
                (void *)&pAacDecoderChannelInfo[ch]->icsInfo,
618
0
                frame_length) != 0) {
619
0
          error = AAC_DEC_DECODE_FRAME_ERROR;
620
0
        }
621
0
        break;
622
0
      case tns_data_present:
623
0
        CTns_ReadDataPresentFlag(
624
0
            hBs, &pAacDecoderChannelInfo[ch]->pDynData->TnsData);
625
0
        if (elFlags & AC_EL_LFE &&
626
0
            pAacDecoderChannelInfo[ch]->pDynData->TnsData.DataPresent) {
627
0
          error = AAC_DEC_PARSE_ERROR;
628
0
        }
629
0
        break;
630
0
      case tns_data:
631
        /* tns_data_present is checked inside CTns_Read(). */
632
0
        error = CTns_Read(hBs, &pAacDecoderChannelInfo[ch]->pDynData->TnsData,
633
0
                          &pAacDecoderChannelInfo[ch]->icsInfo, flags);
634
635
0
        break;
636
637
0
      case gain_control_data:
638
0
        break;
639
640
0
      case gain_control_data_present:
641
0
        if (FDKreadBits(hBs, 1)) {
642
0
          error = AAC_DEC_UNSUPPORTED_GAIN_CONTROL_DATA;
643
0
        }
644
0
        break;
645
646
0
      case tw_data:
647
0
        break;
648
0
      case common_tw:
649
0
        break;
650
0
      case tns_data_present_usac:
651
0
        if (pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_active) {
652
0
          CTns_ReadDataPresentUsac(
653
0
              hBs, &pAacDecoderChannelInfo[0]->pDynData->TnsData,
654
0
              &pAacDecoderChannelInfo[1]->pDynData->TnsData,
655
0
              &pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_on_lr,
656
0
              &pAacDecoderChannelInfo[0]->icsInfo, flags, elFlags,
657
0
              pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow);
658
0
        } else {
659
0
          pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_on_lr =
660
0
              (UCHAR)1;
661
0
        }
662
0
        break;
663
0
      case core_mode:
664
0
        decision_bit = FDKreadBits(hBs, 1);
665
0
        pAacDecoderChannelInfo[ch]->data.usac.core_mode = decision_bit;
666
0
        if ((ch == 1) && (pAacDecoderChannelInfo[0]->data.usac.core_mode !=
667
0
                          pAacDecoderChannelInfo[1]->data.usac.core_mode)) {
668
          /* StereoCoreToolInfo(core_mode[ch] ) */
669
0
          pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow = 0;
670
0
          pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow = 0;
671
0
        }
672
0
        break;
673
0
      case tns_active:
674
0
        pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_active =
675
0
            FDKreadBit(hBs);
676
0
        break;
677
0
      case noise:
678
0
        if (elFlags & AC_EL_USAC_NOISE) {
679
0
          pAacDecoderChannelInfo[ch]
680
0
              ->pDynData->specificTo.usac.fd_noise_level_and_offset =
681
0
              FDKreadBits(hBs, 3 + 5); /* Noise level */
682
0
        }
683
0
        break;
684
0
      case lpd_channel_stream:
685
686
0
      {
687
0
        error = CLpdChannelStream_Read(/* = lpd_channel_stream() */
688
0
                                       hBs, pAacDecoderChannelInfo[ch],
689
0
                                       pAacDecoderStaticChannelInfo[ch],
690
0
                                       pSamplingRateInfo, flags);
691
0
      }
692
693
0
        pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_LPD;
694
0
        break;
695
0
      case fac_data: {
696
0
        int fFacDatPresent = FDKreadBit(hBs);
697
698
        /* Wee need a valid fac_data[0] even if no FAC data is present (as
699
         * temporal buffer) */
700
0
        pAacDecoderChannelInfo[ch]->data.usac.fac_data[0] =
701
0
            pAacDecoderChannelInfo[ch]->data.usac.fac_data0;
702
703
0
        if (fFacDatPresent) {
704
0
          if (elFlags & AC_EL_LFE) {
705
0
            error = AAC_DEC_PARSE_ERROR;
706
0
            break;
707
0
          }
708
          /* FAC data present, this frame is FD, so the last mode had to be
709
           * ACELP. */
710
0
          if (pAacDecoderStaticChannelInfo[ch]->last_core_mode != LPD ||
711
0
              pAacDecoderStaticChannelInfo[ch]->last_lpd_mode != 0) {
712
0
            pAacDecoderChannelInfo[ch]->data.usac.core_mode_last = LPD;
713
0
            pAacDecoderChannelInfo[ch]->data.usac.lpd_mode_last = 0;
714
            /* We can't change the past! So look to the future and go ahead! */
715
0
          }
716
0
          CLpd_FAC_Read(hBs, pAacDecoderChannelInfo[ch]->data.usac.fac_data[0],
717
0
                        pAacDecoderChannelInfo[ch]->data.usac.fac_data_e,
718
0
                        CLpd_FAC_getLength(
719
0
                            IsLongBlock(&pAacDecoderChannelInfo[ch]->icsInfo),
720
0
                            pAacDecoderChannelInfo[ch]->granuleLength),
721
0
                        1, 0);
722
0
        } else {
723
0
          if (pAacDecoderStaticChannelInfo[ch]->last_core_mode == LPD &&
724
0
              pAacDecoderStaticChannelInfo[ch]->last_lpd_mode == 0) {
725
            /* ACELP to FD transitons without FAC are possible. That is why we
726
            zero it out (i.e FAC will not be considered in the subsequent
727
            calculations */
728
0
            FDKmemclear(pAacDecoderChannelInfo[ch]->data.usac.fac_data0,
729
0
                        LFAC * sizeof(FIXP_DBL));
730
0
          }
731
0
        }
732
0
      } break;
733
0
      case esc2_rvlc:
734
0
        if (flags & AC_ER_RVLC) {
735
0
          CRvlc_Decode(pAacDecoderChannelInfo[ch],
736
0
                       pAacDecoderStaticChannelInfo[ch], hBs);
737
0
        }
738
0
        break;
739
740
0
      case esc1_hcr:
741
0
        if (flags & AC_ER_HCR) {
742
0
          CHcr_Read(hBs, pAacDecoderChannelInfo[ch],
743
0
                    numberOfChannels == 2 ? ID_CPE : ID_SCE);
744
0
        }
745
0
        break;
746
747
0
      case spectral_data:
748
0
        error = CBlock_ReadSpectralData(hBs, pAacDecoderChannelInfo[ch],
749
0
                                        pSamplingRateInfo, flags);
750
0
        if (flags & AC_ELD) {
751
0
          pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_ELDFB;
752
0
        } else {
753
0
          if (flags & AC_HDAAC) {
754
0
            pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_INTIMDCT;
755
0
          } else {
756
0
            pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_IMDCT;
757
0
          }
758
0
        }
759
0
        break;
760
761
0
      case ac_spectral_data:
762
0
        error = CBlock_ReadAcSpectralData(
763
0
            hBs, pAacDecoderChannelInfo[ch], pAacDecoderStaticChannelInfo[ch],
764
0
            pSamplingRateInfo, frame_length, flags);
765
0
        pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_IMDCT;
766
0
        break;
767
768
0
      case coupled_elements: {
769
0
        int num_coupled_elements, c;
770
771
0
        ind_sw_cce_flag = FDKreadBit(hBs);
772
0
        num_coupled_elements = FDKreadBits(hBs, 3);
773
774
0
        for (c = 0; c < (num_coupled_elements + 1); c++) {
775
0
          int cc_target_is_cpe;
776
777
0
          num_gain_element_lists++;
778
0
          cc_target_is_cpe = FDKreadBit(hBs); /* cc_target_is_cpe[c] */
779
0
          FDKreadBits(hBs, 4);                /* cc_target_tag_select[c] */
780
781
0
          if (cc_target_is_cpe) {
782
0
            int cc_l, cc_r;
783
784
0
            cc_l = FDKreadBit(hBs); /* cc_l[c] */
785
0
            cc_r = FDKreadBit(hBs); /* cc_r[c] */
786
787
0
            if (cc_l && cc_r) {
788
0
              num_gain_element_lists++;
789
0
            }
790
0
          }
791
0
        }
792
0
        FDKreadBit(hBs);     /* cc_domain */
793
0
        FDKreadBit(hBs);     /* gain_element_sign  */
794
0
        FDKreadBits(hBs, 2); /* gain_element_scale */
795
0
      } break;
796
797
0
      case gain_element_lists: {
798
0
        const CodeBookDescription *hcb;
799
0
        UCHAR *pCodeBook;
800
0
        int c;
801
802
0
        hcb = &AACcodeBookDescriptionTable[BOOKSCL];
803
0
        pCodeBook = pAacDecoderChannelInfo[ch]->pDynData->aCodeBook;
804
805
0
        for (c = 1; c < num_gain_element_lists; c++) {
806
0
          int cge;
807
0
          if (ind_sw_cce_flag) {
808
0
            cge = 1;
809
0
          } else {
810
0
            cge = FDKreadBits(hBs, 1); /* common_gain_element_present[c] */
811
0
          }
812
0
          if (cge) {
813
            /* Huffman */
814
0
            CBlock_DecodeHuffmanWord(
815
0
                hBs, hcb); /* hcod_sf[common_gain_element[c]] 1..19 */
816
0
          } else {
817
0
            int g, sfb;
818
0
            for (g = 0;
819
0
                 g < GetWindowGroups(&pAacDecoderChannelInfo[ch]->icsInfo);
820
0
                 g++) {
821
0
              for (sfb = 0; sfb < GetScaleFactorBandsTransmitted(
822
0
                                      &pAacDecoderChannelInfo[ch]->icsInfo);
823
0
                   sfb++) {
824
0
                if (pCodeBook[sfb] != ZERO_HCB) {
825
                  /* Huffman */
826
0
                  CBlock_DecodeHuffmanWord(
827
0
                      hBs,
828
0
                      hcb); /* hcod_sf[dpcm_gain_element[c][g][sfb]] 1..19 */
829
0
                }
830
0
              }
831
0
            }
832
0
          }
833
0
        }
834
0
      } break;
835
836
        /* CRC handling */
837
0
      case adtscrc_start_reg1:
838
0
        if (pTpDec != NULL) {
839
0
          crcReg1 = transportDec_CrcStartReg(pTpDec, 192);
840
0
        }
841
0
        break;
842
0
      case adtscrc_start_reg2:
843
0
        if (pTpDec != NULL) {
844
0
          crcReg2 = transportDec_CrcStartReg(pTpDec, 128);
845
0
        }
846
0
        break;
847
0
      case adtscrc_end_reg1:
848
0
      case drmcrc_end_reg:
849
0
        if (pTpDec != NULL) {
850
0
          transportDec_CrcEndReg(pTpDec, crcReg1);
851
0
          crcReg1 = -1;
852
0
        }
853
0
        break;
854
0
      case adtscrc_end_reg2:
855
0
        if (crcReg1 != -1) {
856
0
          error = AAC_DEC_DECODE_FRAME_ERROR;
857
0
        } else if (pTpDec != NULL) {
858
0
          transportDec_CrcEndReg(pTpDec, crcReg2);
859
0
          crcReg2 = -1;
860
0
        }
861
0
        break;
862
0
      case drmcrc_start_reg:
863
0
        if (pTpDec != NULL) {
864
0
          crcReg1 = transportDec_CrcStartReg(pTpDec, 0);
865
0
        }
866
0
        break;
867
868
        /* Non data cases */
869
0
      case next_channel:
870
0
        ch = (ch + 1) % numberOfChannels;
871
0
        break;
872
0
      case link_sequence:
873
0
        list = list->next[decision_bit];
874
0
        i = -1;
875
0
        break;
876
877
0
      default:
878
0
        error = AAC_DEC_UNSUPPORTED_FORMAT;
879
0
        break;
880
0
    }
881
882
0
    if (error != AAC_DEC_OK) {
883
0
      goto bail;
884
0
    }
885
886
0
    i++;
887
888
0
  } while (list->id[i] != end_of_sequence);
889
890
0
  for (ch = 0; ch < numberOfChannels; ch++) {
891
0
    if (pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_IMDCT ||
892
0
        pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_ELDFB) {
893
      /* Shows which bands are empty. */
894
0
      UCHAR *band_is_noise =
895
0
          pAacDecoderChannelInfo[ch]->pDynData->band_is_noise;
896
0
      FDKmemset(band_is_noise, (UCHAR)1, sizeof(UCHAR) * (8 * 16));
897
898
0
      error = CBlock_InverseQuantizeSpectralData(
899
0
          pAacDecoderChannelInfo[ch], pSamplingRateInfo, band_is_noise, 1);
900
0
      if (error != AAC_DEC_OK) {
901
0
        return error;
902
0
      }
903
904
0
      if (elFlags & AC_EL_USAC_NOISE) {
905
0
        CBlock_ApplyNoise(pAacDecoderChannelInfo[ch], pSamplingRateInfo,
906
0
                          &pAacDecoderStaticChannelInfo[ch]->nfRandomSeed,
907
0
                          band_is_noise);
908
909
0
      } /* if (elFlags & AC_EL_USAC_NOISE) */
910
0
    }
911
0
  }
912
913
0
bail:
914
0
  if (crcReg1 != -1 || crcReg2 != -1) {
915
0
    if (error == AAC_DEC_OK) {
916
0
      error = AAC_DEC_DECODE_FRAME_ERROR;
917
0
    }
918
0
    if (crcReg1 != -1) {
919
0
      transportDec_CrcEndReg(pTpDec, crcReg1);
920
0
    }
921
0
    if (crcReg2 != -1) {
922
0
      transportDec_CrcEndReg(pTpDec, crcReg2);
923
0
    }
924
0
  }
925
0
  return error;
926
0
}