• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
MapMidSideMaskToPnsCorrelation(CAacDecoderChannelInfo * pAacDecoderChannelInfo[2])118 static void MapMidSideMaskToPnsCorrelation(
119     CAacDecoderChannelInfo *pAacDecoderChannelInfo[2]) {
120   int group;
121 
122   for (group = 0; group < pAacDecoderChannelInfo[L]->icsInfo.WindowGroups;
123        group++) {
124     UCHAR groupMask = 1 << group;
125 
126     for (UCHAR band = 0; band < pAacDecoderChannelInfo[L]->icsInfo.MaxSfBands;
127          band++) {
128       if (pAacDecoderChannelInfo[L]->pComData->jointStereoData.MsUsed[band] &
129           groupMask) { /* channels are correlated */
130         CPns_SetCorrelation(&pAacDecoderChannelInfo[L]->data.aac.PnsData, group,
131                             band, 0);
132 
133         if (CPns_IsPnsUsed(&pAacDecoderChannelInfo[L]->data.aac.PnsData, group,
134                            band) &&
135             CPns_IsPnsUsed(&pAacDecoderChannelInfo[R]->data.aac.PnsData, group,
136                            band))
137           pAacDecoderChannelInfo[L]->pComData->jointStereoData.MsUsed[band] ^=
138               groupMask; /* clear the groupMask-bit */
139       }
140     }
141   }
142 }
143 
Clean_Complex_Prediction_coefficients(CJointStereoPersistentData * pJointStereoPersistentData,int windowGroups,const int low_limit,const int high_limit)144 static void Clean_Complex_Prediction_coefficients(
145     CJointStereoPersistentData *pJointStereoPersistentData, int windowGroups,
146     const int low_limit, const int high_limit) {
147   for (int group = 0; group < windowGroups; group++) {
148     for (int sfb = low_limit; sfb < high_limit; sfb++) {
149       pJointStereoPersistentData->alpha_q_re_prev[group][sfb] = 0;
150       pJointStereoPersistentData->alpha_q_im_prev[group][sfb] = 0;
151     }
152   }
153 }
154 
155 /*!
156   \brief Decode channel pair element
157 
158   The function decodes a channel pair element.
159 
160   \return  none
161 */
CChannelElement_Decode(CAacDecoderChannelInfo * pAacDecoderChannelInfo[2],CAacDecoderStaticChannelInfo * pAacDecoderStaticChannelInfo[2],SamplingRateInfo * pSamplingRateInfo,UINT flags,UINT elFlags,int el_channels)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     int el_channels) {
168   int ch = 0;
169 
170   int maxSfBandsL = 0, maxSfBandsR = 0;
171   int maybe_jstereo = (el_channels > 1);
172 
173   if (flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA) && el_channels == 2) {
174     if (pAacDecoderChannelInfo[L]->data.usac.core_mode ||
175         pAacDecoderChannelInfo[R]->data.usac.core_mode) {
176       maybe_jstereo = 0;
177     }
178   }
179 
180   if (maybe_jstereo) {
181     maxSfBandsL =
182         GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo[L]->icsInfo);
183     maxSfBandsR =
184         GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo[R]->icsInfo);
185 
186     /* apply ms */
187     if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow) {
188       if (!(flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA))) {
189         if (pAacDecoderChannelInfo[L]->data.aac.PnsData.PnsActive ||
190             pAacDecoderChannelInfo[R]->data.aac.PnsData.PnsActive) {
191           MapMidSideMaskToPnsCorrelation(pAacDecoderChannelInfo);
192         }
193       }
194       /* if tns_on_lr == 1 run MS */ /* &&
195                                         (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_active
196                                         == 1) */
197       if (((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) &&
198            (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_on_lr ==
199             1)) ||
200           ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) == 0)) {
201         int max_sfb_ste = (INT)(pAacDecoderChannelInfo[L]->icsInfo.max_sfb_ste);
202 
203         CJointStereo_ApplyMS(
204             pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo,
205             pAacDecoderChannelInfo[L]->pSpectralCoefficient,
206             pAacDecoderChannelInfo[R]->pSpectralCoefficient,
207             pAacDecoderChannelInfo[L]->pDynData->aSfbScale,
208             pAacDecoderChannelInfo[R]->pDynData->aSfbScale,
209             pAacDecoderChannelInfo[L]->specScale,
210             pAacDecoderChannelInfo[R]->specScale,
211             GetScaleFactorBandOffsets(&pAacDecoderChannelInfo[L]->icsInfo,
212                                       pSamplingRateInfo),
213             GetWindowGroupLengthTable(&pAacDecoderChannelInfo[L]->icsInfo),
214             GetWindowGroups(&pAacDecoderChannelInfo[L]->icsInfo), max_sfb_ste,
215             maxSfBandsL, maxSfBandsR,
216             pAacDecoderChannelInfo[L]
217                 ->pComData->jointStereoData.store_dmx_re_prev,
218             &(pAacDecoderChannelInfo[L]
219                   ->pComData->jointStereoData.store_dmx_re_prev_e),
220             1);
221 
222       } /* if ( ((elFlags & AC_EL_USAC_CP_POSSIBLE).... */
223     }   /* if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow)*/
224 
225     /* apply intensity stereo */ /* modifies pAacDecoderChannelInfo[]->aSpecSfb
226                                   */
227     if (!(flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA))) {
228       if ((pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow ==
229            1) &&
230           (el_channels == 2)) {
231         CJointStereo_ApplyIS(
232             pAacDecoderChannelInfo,
233             GetScaleFactorBandOffsets(&pAacDecoderChannelInfo[L]->icsInfo,
234                                       pSamplingRateInfo),
235             GetWindowGroupLengthTable(&pAacDecoderChannelInfo[L]->icsInfo),
236             GetWindowGroups(&pAacDecoderChannelInfo[L]->icsInfo),
237             GetScaleFactorBandsTransmitted(
238                 &pAacDecoderChannelInfo[L]->icsInfo));
239       }
240     }
241   } /* maybe_stereo */
242 
243   for (ch = 0; ch < el_channels; ch++) {
244     if (pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_LPD) {
245       /* Decode LPD data */
246       CLpdChannelStream_Decode(pAacDecoderChannelInfo[ch],
247                                pAacDecoderStaticChannelInfo[ch], flags);
248     } else {
249       UCHAR noSfbs =
250           GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo[ch]->icsInfo);
251       /* For USAC common window: max_sfb of both channels may differ
252        * (common_max_sfb == 0). */
253       if ((maybe_jstereo == 1) &&
254           (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow ==
255            1)) {
256         noSfbs = fMax(maxSfBandsL, maxSfBandsR);
257       }
258       int CP_active = 0;
259       if (elFlags & AC_EL_USAC_CP_POSSIBLE) {
260         CP_active = pAacDecoderChannelInfo[ch]
261                         ->pComData->jointStereoData.cplx_pred_flag;
262       }
263 
264       /* Omit writing of pAacDecoderChannelInfo[ch]->specScale for complex
265          stereo prediction since scaling has already been carried out. */
266       int max_sfb_ste = (INT)(pAacDecoderChannelInfo[L]->icsInfo.max_sfb_ste);
267 
268       if (!(CP_active && (max_sfb_ste == noSfbs)) ||
269           !(CP_active &&
270             !(pAacDecoderChannelInfo[ch]->pDynData->TnsData.Active)) ||
271           ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) &&
272            (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_on_lr ==
273             0))) {
274         CBlock_ScaleSpectralData(pAacDecoderChannelInfo[ch], noSfbs,
275                                  pSamplingRateInfo);
276 
277         /*Active for the case of TNS applied before MS/CP*/
278         if ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) &&
279             (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_on_lr ==
280              0)) {
281           if (IsLongBlock(&pAacDecoderChannelInfo[ch]->icsInfo)) {
282             for (int i = 0; i < noSfbs; i++) {
283               pAacDecoderChannelInfo[ch]->pDynData->aSfbScale[i] =
284                   pAacDecoderChannelInfo[ch]->specScale[0];
285             }
286           } else {
287             for (int i = 0; i < 8; i++) {
288               for (int j = 0; j < noSfbs; j++) {
289                 pAacDecoderChannelInfo[ch]->pDynData->aSfbScale[i * 16 + j] =
290                     pAacDecoderChannelInfo[ch]->specScale[i];
291               }
292             }
293           }
294         }
295       }
296     }
297   } /* End "for (ch = 0; ch < el_channels; ch++)" */
298 
299   if (maybe_jstereo) {
300     /* apply ms */
301     if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow) {
302     } /* CommonWindow */
303     else {
304       if (elFlags & AC_EL_USAC_CP_POSSIBLE) {
305         FDKmemclear(
306             pAacDecoderStaticChannelInfo[L]
307                 ->pCpeStaticData->jointStereoPersistentData.alpha_q_re_prev,
308             JointStereoMaximumGroups * JointStereoMaximumBands * sizeof(SHORT));
309         FDKmemclear(
310             pAacDecoderStaticChannelInfo[L]
311                 ->pCpeStaticData->jointStereoPersistentData.alpha_q_im_prev,
312             JointStereoMaximumGroups * JointStereoMaximumBands * sizeof(SHORT));
313       }
314     }
315 
316   } /* if (maybe_jstereo) */
317 
318   for (ch = 0; ch < el_channels; ch++) {
319     if (pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_LPD) {
320     } else {
321       if (!(flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA))) {
322         /* Use same seed for coupled channels (CPE) */
323         int pnsCh = (ch > 0) ? L : ch;
324         CPns_UpdateNoiseState(
325             &pAacDecoderChannelInfo[ch]->data.aac.PnsData,
326             pAacDecoderChannelInfo[pnsCh]->data.aac.PnsData.currentSeed,
327             pAacDecoderChannelInfo[ch]->pComData->pnsRandomSeed);
328       }
329 
330       if ((!(flags & (AC_USAC))) ||
331           ((flags & (AC_USAC)) &&
332            (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_active ==
333             1)) ||
334           (maybe_jstereo == 0)) {
335         ApplyTools(
336             pAacDecoderChannelInfo, pSamplingRateInfo, flags, elFlags, ch,
337             pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow);
338       }
339     } /* End "} else" */
340   }   /* End "for (ch = 0; ch < el_channels; ch++)" */
341 
342   if (maybe_jstereo) {
343     /* apply ms */
344     if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow) {
345       /* if tns_on_lr == 0 run MS */
346       if ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) &&
347           (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_on_lr ==
348            0)) {
349         int max_sfb_ste = (INT)(pAacDecoderChannelInfo[L]->icsInfo.max_sfb_ste);
350 
351         CJointStereo_ApplyMS(
352             pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo,
353             pAacDecoderChannelInfo[L]->pSpectralCoefficient,
354             pAacDecoderChannelInfo[R]->pSpectralCoefficient,
355             pAacDecoderChannelInfo[L]->pDynData->aSfbScale,
356             pAacDecoderChannelInfo[R]->pDynData->aSfbScale,
357             pAacDecoderChannelInfo[L]->specScale,
358             pAacDecoderChannelInfo[R]->specScale,
359             GetScaleFactorBandOffsets(&pAacDecoderChannelInfo[L]->icsInfo,
360                                       pSamplingRateInfo),
361             GetWindowGroupLengthTable(&pAacDecoderChannelInfo[L]->icsInfo),
362             GetWindowGroups(&pAacDecoderChannelInfo[L]->icsInfo), max_sfb_ste,
363             maxSfBandsL, maxSfBandsR,
364             pAacDecoderChannelInfo[L]
365                 ->pComData->jointStereoData.store_dmx_re_prev,
366             &(pAacDecoderChannelInfo[L]
367                   ->pComData->jointStereoData.store_dmx_re_prev_e),
368             1);
369       }
370 
371     } /* if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow) */
372 
373   } /* if (maybe_jstereo) */
374 
375   for (ch = 0; ch < el_channels; ch++) {
376     if (elFlags & AC_EL_USAC_CP_POSSIBLE) {
377       pAacDecoderStaticChannelInfo[L]
378           ->pCpeStaticData->jointStereoPersistentData.clearSpectralCoeffs = 0;
379     }
380   }
381 
382   CRvlc_ElementCheck(pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo,
383                      flags, el_channels);
384 }
385 
CChannel_CodebookTableInit(CAacDecoderChannelInfo * pAacDecoderChannelInfo)386 void CChannel_CodebookTableInit(
387     CAacDecoderChannelInfo *pAacDecoderChannelInfo) {
388   int b, w, maxBands, maxWindows;
389   int maxSfb = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
390   UCHAR *pCodeBook = pAacDecoderChannelInfo->pDynData->aCodeBook;
391 
392   if (IsLongBlock(&pAacDecoderChannelInfo->icsInfo)) {
393     maxBands = 64;
394     maxWindows = 1;
395   } else {
396     maxBands = 16;
397     maxWindows = 8;
398   }
399 
400   for (w = 0; w < maxWindows; w++) {
401     for (b = 0; b < maxSfb; b++) {
402       pCodeBook[b] = ESCBOOK;
403     }
404     for (; b < maxBands; b++) {
405       pCodeBook[b] = ZERO_HCB;
406     }
407     pCodeBook += maxBands;
408   }
409 }
410 
411 /*
412  * Arbitrary order bitstream parser
413  */
CChannelElement_Read(HANDLE_FDK_BITSTREAM hBs,CAacDecoderChannelInfo * pAacDecoderChannelInfo[],CAacDecoderStaticChannelInfo * pAacDecoderStaticChannelInfo[],const AUDIO_OBJECT_TYPE aot,SamplingRateInfo * pSamplingRateInfo,const UINT flags,const UINT elFlags,const UINT frame_length,const UCHAR numberOfChannels,const SCHAR epConfig,HANDLE_TRANSPORTDEC pTpDec)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     HANDLE_TRANSPORTDEC pTpDec) {
421   AAC_DECODER_ERROR error = AAC_DEC_OK;
422   const element_list_t *list;
423   int i, ch, decision_bit;
424   int crcReg1 = -1, crcReg2 = -1;
425   int cplxPred;
426   int ind_sw_cce_flag = 0, num_gain_element_lists = 0;
427 
428   FDK_ASSERT((numberOfChannels == 1) || (numberOfChannels == 2));
429 
430   /* Get channel element sequence table */
431   list = getBitstreamElementList(aot, epConfig, numberOfChannels, 0, elFlags);
432   if (list == NULL) {
433     error = AAC_DEC_UNSUPPORTED_FORMAT;
434     goto bail;
435   }
436 
437   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   pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow = 0;
441   if (flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) {
442     pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_active = 0;
443     pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_on_lr = 0;
444   }
445   if (numberOfChannels == 2) {
446     CTns_Reset(&pAacDecoderChannelInfo[1]->pDynData->TnsData);
447     pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow = 0;
448   }
449 
450   cplxPred = 0;
451   if (pAacDecoderStaticChannelInfo != NULL) {
452     if (elFlags & AC_EL_USAC_CP_POSSIBLE) {
453       pAacDecoderChannelInfo[0]->pComData->jointStereoData.cplx_pred_flag = 0;
454       cplxPred = 1;
455     }
456   }
457 
458   if (0 || (flags & (AC_ELD | AC_SCALABLE))) {
459     pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow = 1;
460     if (numberOfChannels == 2) {
461       pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow =
462           pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow;
463     }
464   }
465 
466   /* Iterate through sequence table */
467   i = 0;
468   ch = 0;
469   decision_bit = 0;
470   do {
471     switch (list->id[i]) {
472       case element_instance_tag:
473         pAacDecoderChannelInfo[0]->ElementInstanceTag = FDKreadBits(hBs, 4);
474         if (numberOfChannels == 2) {
475           pAacDecoderChannelInfo[1]->ElementInstanceTag =
476               pAacDecoderChannelInfo[0]->ElementInstanceTag;
477         }
478         break;
479       case common_window:
480         decision_bit =
481             pAacDecoderChannelInfo[ch]->pDynData->RawDataInfo.CommonWindow =
482                 FDKreadBits(hBs, 1);
483         if (numberOfChannels == 2) {
484           pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow =
485               pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow;
486         }
487         break;
488       case ics_info:
489         /* store last window sequence (utilized in complex stereo prediction)
490          * before reading new channel-info */
491         if (cplxPred) {
492           if (pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow) {
493             pAacDecoderStaticChannelInfo[0]
494                 ->pCpeStaticData->jointStereoPersistentData.winSeqPrev =
495                 pAacDecoderChannelInfo[0]->icsInfo.WindowSequence;
496             pAacDecoderStaticChannelInfo[0]
497                 ->pCpeStaticData->jointStereoPersistentData.winShapePrev =
498                 pAacDecoderChannelInfo[0]->icsInfo.WindowShape;
499           }
500         }
501         /* Read individual channel info */
502         error = IcsRead(hBs, &pAacDecoderChannelInfo[ch]->icsInfo,
503                         pSamplingRateInfo, flags);
504 
505         if (elFlags & AC_EL_LFE &&
506             GetWindowSequence(&pAacDecoderChannelInfo[ch]->icsInfo) !=
507                 BLOCK_LONG) {
508           error = AAC_DEC_PARSE_ERROR;
509           break;
510         }
511 
512         if (numberOfChannels == 2 &&
513             pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow) {
514           pAacDecoderChannelInfo[1]->icsInfo =
515               pAacDecoderChannelInfo[0]->icsInfo;
516         }
517         break;
518 
519       case common_max_sfb:
520         if (FDKreadBit(hBs) == 0) {
521           error = IcsReadMaxSfb(hBs, &pAacDecoderChannelInfo[1]->icsInfo,
522                                 pSamplingRateInfo);
523         }
524         break;
525 
526       case ltp_data_present:
527         if (FDKreadBits(hBs, 1) != 0) {
528           error = AAC_DEC_UNSUPPORTED_PREDICTION;
529         }
530         break;
531 
532       case ms:
533 
534         INT max_sfb_ste;
535         INT max_sfb_ste_clear;
536 
537         max_sfb_ste = GetScaleMaxFactorBandsTransmitted(
538             &pAacDecoderChannelInfo[0]->icsInfo,
539             &pAacDecoderChannelInfo[1]->icsInfo);
540 
541         max_sfb_ste_clear = 64;
542 
543         pAacDecoderChannelInfo[0]->icsInfo.max_sfb_ste = (UCHAR)max_sfb_ste;
544         pAacDecoderChannelInfo[1]->icsInfo.max_sfb_ste = (UCHAR)max_sfb_ste;
545 
546         if (flags & (AC_USAC | AC_RSV603DA) &&
547             pAacDecoderChannelInfo[ch]->pDynData->RawDataInfo.CommonWindow ==
548                 0) {
549           Clean_Complex_Prediction_coefficients(
550               &pAacDecoderStaticChannelInfo[0]
551                    ->pCpeStaticData->jointStereoPersistentData,
552               GetWindowGroups(&pAacDecoderChannelInfo[0]->icsInfo), 0, 64);
553         }
554 
555         if (CJointStereo_Read(
556                 hBs, &pAacDecoderChannelInfo[0]->pComData->jointStereoData,
557                 GetWindowGroups(&pAacDecoderChannelInfo[0]->icsInfo),
558                 max_sfb_ste, max_sfb_ste_clear,
559                 /* jointStereoPersistentData and cplxPredictionData are only
560                    available/allocated if cplxPred is active. */
561                 ((cplxPred == 0) || (pAacDecoderStaticChannelInfo == NULL))
562                     ? NULL
563                     : &pAacDecoderStaticChannelInfo[0]
564                            ->pCpeStaticData->jointStereoPersistentData,
565                 ((cplxPred == 0) || (pAacDecoderChannelInfo[0] == NULL))
566                     ? NULL
567                     : pAacDecoderChannelInfo[0]
568                           ->pComStaticData->cplxPredictionData,
569                 cplxPred,
570                 GetScaleFactorBandsTotal(&pAacDecoderChannelInfo[0]->icsInfo),
571                 GetWindowSequence(&pAacDecoderChannelInfo[0]->icsInfo),
572                 flags)) {
573           error = AAC_DEC_PARSE_ERROR;
574         }
575 
576         break;
577 
578       case global_gain:
579         pAacDecoderChannelInfo[ch]->pDynData->RawDataInfo.GlobalGain =
580             (UCHAR)FDKreadBits(hBs, 8);
581         break;
582 
583       case section_data:
584         error = CBlock_ReadSectionData(hBs, pAacDecoderChannelInfo[ch],
585                                        pSamplingRateInfo, flags);
586         break;
587 
588       case scale_factor_data_usac:
589         pAacDecoderChannelInfo[ch]->currAliasingSymmetry = 0;
590         /* Set active sfb codebook indexes to HCB_ESC to make them "active" */
591         CChannel_CodebookTableInit(
592             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         FDK_FALLTHROUGH;
598 
599       case scale_factor_data:
600         if (flags & AC_ER_RVLC) {
601           /* read RVLC data from bitstream (error sens. cat. 1) */
602           CRvlc_Read(pAacDecoderChannelInfo[ch], hBs);
603         } else {
604           error = CBlock_ReadScaleFactorData(pAacDecoderChannelInfo[ch], hBs,
605                                              flags);
606         }
607         break;
608 
609       case pulse:
610         if (CPulseData_Read(
611                 hBs,
612                 &pAacDecoderChannelInfo[ch]->pDynData->specificTo.aac.PulseData,
613                 pSamplingRateInfo->ScaleFactorBands_Long, /* pulse data is only
614                                                              allowed to be
615                                                              present in long
616                                                              blocks! */
617                 (void *)&pAacDecoderChannelInfo[ch]->icsInfo,
618                 frame_length) != 0) {
619           error = AAC_DEC_DECODE_FRAME_ERROR;
620         }
621         break;
622       case tns_data_present:
623         CTns_ReadDataPresentFlag(
624             hBs, &pAacDecoderChannelInfo[ch]->pDynData->TnsData);
625         if (elFlags & AC_EL_LFE &&
626             pAacDecoderChannelInfo[ch]->pDynData->TnsData.DataPresent) {
627           error = AAC_DEC_PARSE_ERROR;
628         }
629         break;
630       case tns_data:
631         /* tns_data_present is checked inside CTns_Read(). */
632         error = CTns_Read(hBs, &pAacDecoderChannelInfo[ch]->pDynData->TnsData,
633                           &pAacDecoderChannelInfo[ch]->icsInfo, flags);
634 
635         break;
636 
637       case gain_control_data:
638         break;
639 
640       case gain_control_data_present:
641         if (FDKreadBits(hBs, 1)) {
642           error = AAC_DEC_UNSUPPORTED_GAIN_CONTROL_DATA;
643         }
644         break;
645 
646       case tw_data:
647         break;
648       case common_tw:
649         break;
650       case tns_data_present_usac:
651         if (pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_active) {
652           CTns_ReadDataPresentUsac(
653               hBs, &pAacDecoderChannelInfo[0]->pDynData->TnsData,
654               &pAacDecoderChannelInfo[1]->pDynData->TnsData,
655               &pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_on_lr,
656               &pAacDecoderChannelInfo[0]->icsInfo, flags, elFlags,
657               pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow);
658         } else {
659           pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_on_lr =
660               (UCHAR)1;
661         }
662         break;
663       case core_mode:
664         decision_bit = FDKreadBits(hBs, 1);
665         pAacDecoderChannelInfo[ch]->data.usac.core_mode = decision_bit;
666         if ((ch == 1) && (pAacDecoderChannelInfo[0]->data.usac.core_mode !=
667                           pAacDecoderChannelInfo[1]->data.usac.core_mode)) {
668           /* StereoCoreToolInfo(core_mode[ch] ) */
669           pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow = 0;
670           pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow = 0;
671         }
672         break;
673       case tns_active:
674         pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_active =
675             FDKreadBit(hBs);
676         break;
677       case noise:
678         if (elFlags & AC_EL_USAC_NOISE) {
679           pAacDecoderChannelInfo[ch]
680               ->pDynData->specificTo.usac.fd_noise_level_and_offset =
681               FDKreadBits(hBs, 3 + 5); /* Noise level */
682         }
683         break;
684       case lpd_channel_stream:
685 
686       {
687         error = CLpdChannelStream_Read(/* = lpd_channel_stream() */
688                                        hBs, pAacDecoderChannelInfo[ch],
689                                        pAacDecoderStaticChannelInfo[ch],
690                                        pSamplingRateInfo, flags);
691       }
692 
693         pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_LPD;
694         break;
695       case fac_data: {
696         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         pAacDecoderChannelInfo[ch]->data.usac.fac_data[0] =
701             pAacDecoderChannelInfo[ch]->data.usac.fac_data0;
702 
703         if (fFacDatPresent) {
704           if (elFlags & AC_EL_LFE) {
705             error = AAC_DEC_PARSE_ERROR;
706             break;
707           }
708           /* FAC data present, this frame is FD, so the last mode had to be
709            * ACELP. */
710           if (pAacDecoderStaticChannelInfo[ch]->last_core_mode != LPD ||
711               pAacDecoderStaticChannelInfo[ch]->last_lpd_mode != 0) {
712             pAacDecoderChannelInfo[ch]->data.usac.core_mode_last = LPD;
713             pAacDecoderChannelInfo[ch]->data.usac.lpd_mode_last = 0;
714             /* We can't change the past! So look to the future and go ahead! */
715           }
716           CLpd_FAC_Read(hBs, pAacDecoderChannelInfo[ch]->data.usac.fac_data[0],
717                         pAacDecoderChannelInfo[ch]->data.usac.fac_data_e,
718                         CLpd_FAC_getLength(
719                             IsLongBlock(&pAacDecoderChannelInfo[ch]->icsInfo),
720                             pAacDecoderChannelInfo[ch]->granuleLength),
721                         1, 0);
722         } else {
723           if (pAacDecoderStaticChannelInfo[ch]->last_core_mode == LPD &&
724               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             FDKmemclear(pAacDecoderChannelInfo[ch]->data.usac.fac_data0,
729                         LFAC * sizeof(FIXP_DBL));
730           }
731         }
732       } break;
733       case esc2_rvlc:
734         if (flags & AC_ER_RVLC) {
735           CRvlc_Decode(pAacDecoderChannelInfo[ch],
736                        pAacDecoderStaticChannelInfo[ch], hBs);
737         }
738         break;
739 
740       case esc1_hcr:
741         if (flags & AC_ER_HCR) {
742           CHcr_Read(hBs, pAacDecoderChannelInfo[ch],
743                     numberOfChannels == 2 ? ID_CPE : ID_SCE);
744         }
745         break;
746 
747       case spectral_data:
748         error = CBlock_ReadSpectralData(hBs, pAacDecoderChannelInfo[ch],
749                                         pSamplingRateInfo, flags);
750         if (flags & AC_ELD) {
751           pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_ELDFB;
752         } else {
753           if (flags & AC_HDAAC) {
754             pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_INTIMDCT;
755           } else {
756             pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_IMDCT;
757           }
758         }
759         break;
760 
761       case ac_spectral_data:
762         error = CBlock_ReadAcSpectralData(
763             hBs, pAacDecoderChannelInfo[ch], pAacDecoderStaticChannelInfo[ch],
764             pSamplingRateInfo, frame_length, flags);
765         pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_IMDCT;
766         break;
767 
768       case coupled_elements: {
769         int num_coupled_elements, c;
770 
771         ind_sw_cce_flag = FDKreadBit(hBs);
772         num_coupled_elements = FDKreadBits(hBs, 3);
773 
774         for (c = 0; c < (num_coupled_elements + 1); c++) {
775           int cc_target_is_cpe;
776 
777           num_gain_element_lists++;
778           cc_target_is_cpe = FDKreadBit(hBs); /* cc_target_is_cpe[c] */
779           FDKreadBits(hBs, 4);                /* cc_target_tag_select[c] */
780 
781           if (cc_target_is_cpe) {
782             int cc_l, cc_r;
783 
784             cc_l = FDKreadBit(hBs); /* cc_l[c] */
785             cc_r = FDKreadBit(hBs); /* cc_r[c] */
786 
787             if (cc_l && cc_r) {
788               num_gain_element_lists++;
789             }
790           }
791         }
792         FDKreadBit(hBs);     /* cc_domain */
793         FDKreadBit(hBs);     /* gain_element_sign  */
794         FDKreadBits(hBs, 2); /* gain_element_scale */
795       } break;
796 
797       case gain_element_lists: {
798         const CodeBookDescription *hcb;
799         UCHAR *pCodeBook;
800         int c;
801 
802         hcb = &AACcodeBookDescriptionTable[BOOKSCL];
803         pCodeBook = pAacDecoderChannelInfo[ch]->pDynData->aCodeBook;
804 
805         for (c = 1; c < num_gain_element_lists; c++) {
806           int cge;
807           if (ind_sw_cce_flag) {
808             cge = 1;
809           } else {
810             cge = FDKreadBits(hBs, 1); /* common_gain_element_present[c] */
811           }
812           if (cge) {
813             /* Huffman */
814             CBlock_DecodeHuffmanWord(
815                 hBs, hcb); /* hcod_sf[common_gain_element[c]] 1..19 */
816           } else {
817             int g, sfb;
818             for (g = 0;
819                  g < GetWindowGroups(&pAacDecoderChannelInfo[ch]->icsInfo);
820                  g++) {
821               for (sfb = 0; sfb < GetScaleFactorBandsTransmitted(
822                                       &pAacDecoderChannelInfo[ch]->icsInfo);
823                    sfb++) {
824                 if (pCodeBook[sfb] != ZERO_HCB) {
825                   /* Huffman */
826                   CBlock_DecodeHuffmanWord(
827                       hBs,
828                       hcb); /* hcod_sf[dpcm_gain_element[c][g][sfb]] 1..19 */
829                 }
830               }
831             }
832           }
833         }
834       } break;
835 
836         /* CRC handling */
837       case adtscrc_start_reg1:
838         if (pTpDec != NULL) {
839           crcReg1 = transportDec_CrcStartReg(pTpDec, 192);
840         }
841         break;
842       case adtscrc_start_reg2:
843         if (pTpDec != NULL) {
844           crcReg2 = transportDec_CrcStartReg(pTpDec, 128);
845         }
846         break;
847       case adtscrc_end_reg1:
848       case drmcrc_end_reg:
849         if (pTpDec != NULL) {
850           transportDec_CrcEndReg(pTpDec, crcReg1);
851           crcReg1 = -1;
852         }
853         break;
854       case adtscrc_end_reg2:
855         if (crcReg1 != -1) {
856           error = AAC_DEC_DECODE_FRAME_ERROR;
857         } else if (pTpDec != NULL) {
858           transportDec_CrcEndReg(pTpDec, crcReg2);
859           crcReg2 = -1;
860         }
861         break;
862       case drmcrc_start_reg:
863         if (pTpDec != NULL) {
864           crcReg1 = transportDec_CrcStartReg(pTpDec, 0);
865         }
866         break;
867 
868         /* Non data cases */
869       case next_channel:
870         ch = (ch + 1) % numberOfChannels;
871         break;
872       case link_sequence:
873         list = list->next[decision_bit];
874         i = -1;
875         break;
876 
877       default:
878         error = AAC_DEC_UNSUPPORTED_FORMAT;
879         break;
880     }
881 
882     if (error != AAC_DEC_OK) {
883       goto bail;
884     }
885 
886     i++;
887 
888   } while (list->id[i] != end_of_sequence);
889 
890   for (ch = 0; ch < numberOfChannels; ch++) {
891     if (pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_IMDCT ||
892         pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_ELDFB) {
893       /* Shows which bands are empty. */
894       UCHAR *band_is_noise =
895           pAacDecoderChannelInfo[ch]->pDynData->band_is_noise;
896       FDKmemset(band_is_noise, (UCHAR)1, sizeof(UCHAR) * (8 * 16));
897 
898       error = CBlock_InverseQuantizeSpectralData(
899           pAacDecoderChannelInfo[ch], pSamplingRateInfo, band_is_noise, 1);
900       if (error != AAC_DEC_OK) {
901         return error;
902       }
903 
904       if (elFlags & AC_EL_USAC_NOISE) {
905         CBlock_ApplyNoise(pAacDecoderChannelInfo[ch], pSamplingRateInfo,
906                           &pAacDecoderStaticChannelInfo[ch]->nfRandomSeed,
907                           band_is_noise);
908 
909       } /* if (elFlags & AC_EL_USAC_NOISE) */
910     }
911   }
912 
913 bail:
914   if (crcReg1 != -1 || crcReg2 != -1) {
915     if (error == AAC_DEC_OK) {
916       error = AAC_DEC_DECODE_FRAME_ERROR;
917     }
918     if (crcReg1 != -1) {
919       transportDec_CrcEndReg(pTpDec, crcReg1);
920     }
921     if (crcReg2 != -1) {
922       transportDec_CrcEndReg(pTpDec, crcReg2);
923     }
924   }
925   return error;
926 }
927