• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3 
4 © Copyright  1995 - 2018 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) || (CP_active && (max_sfb_ste < noSfbs)) ||
269           ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) &&
270            (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_on_lr ==
271             0))) {
272         CBlock_ScaleSpectralData(pAacDecoderChannelInfo[ch], noSfbs,
273                                  pSamplingRateInfo);
274 
275         /*Active for the case of TNS applied before MS/CP*/
276         if ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) &&
277             (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_on_lr ==
278              0)) {
279           if (IsLongBlock(&pAacDecoderChannelInfo[ch]->icsInfo)) {
280             for (int i = 0; i < noSfbs; i++) {
281               pAacDecoderChannelInfo[ch]->pDynData->aSfbScale[i] =
282                   pAacDecoderChannelInfo[ch]->specScale[0];
283             }
284           } else {
285             for (int i = 0; i < 8; i++) {
286               for (int j = 0; j < noSfbs; j++) {
287                 pAacDecoderChannelInfo[ch]->pDynData->aSfbScale[i * 16 + j] =
288                     pAacDecoderChannelInfo[ch]->specScale[i];
289               }
290             }
291           }
292         }
293       }
294     }
295   } /* End "for (ch = 0; ch < el_channels; ch++)" */
296 
297   if (maybe_jstereo) {
298     /* apply ms */
299     if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow) {
300     } /* CommonWindow */
301     else {
302       if (elFlags & AC_EL_USAC_CP_POSSIBLE) {
303         FDKmemclear(
304             pAacDecoderStaticChannelInfo[L]
305                 ->pCpeStaticData->jointStereoPersistentData.alpha_q_re_prev,
306             JointStereoMaximumGroups * JointStereoMaximumBands * sizeof(SHORT));
307         FDKmemclear(
308             pAacDecoderStaticChannelInfo[L]
309                 ->pCpeStaticData->jointStereoPersistentData.alpha_q_im_prev,
310             JointStereoMaximumGroups * JointStereoMaximumBands * sizeof(SHORT));
311       }
312     }
313 
314   } /* if (maybe_jstereo) */
315 
316   for (ch = 0; ch < el_channels; ch++) {
317     if (pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_LPD) {
318     } else {
319       if (!(flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA))) {
320         /* Use same seed for coupled channels (CPE) */
321         int pnsCh = (ch > 0) ? L : ch;
322         CPns_UpdateNoiseState(
323             &pAacDecoderChannelInfo[ch]->data.aac.PnsData,
324             pAacDecoderChannelInfo[pnsCh]->data.aac.PnsData.currentSeed,
325             pAacDecoderChannelInfo[ch]->pComData->pnsRandomSeed);
326       }
327 
328       if ((!(flags & (AC_USAC))) ||
329           ((flags & (AC_USAC)) &&
330            (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_active ==
331             1)) ||
332           (maybe_jstereo == 0)) {
333         ApplyTools(
334             pAacDecoderChannelInfo, pSamplingRateInfo, flags, elFlags, ch,
335             pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow);
336       }
337     } /* End "} else" */
338   }   /* End "for (ch = 0; ch < el_channels; ch++)" */
339 
340   if (maybe_jstereo) {
341     /* apply ms */
342     if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow) {
343       /* if tns_on_lr == 0 run MS */
344       if ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) &&
345           (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_on_lr ==
346            0)) {
347         int max_sfb_ste = (INT)(pAacDecoderChannelInfo[L]->icsInfo.max_sfb_ste);
348 
349         CJointStereo_ApplyMS(
350             pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo,
351             pAacDecoderChannelInfo[L]->pSpectralCoefficient,
352             pAacDecoderChannelInfo[R]->pSpectralCoefficient,
353             pAacDecoderChannelInfo[L]->pDynData->aSfbScale,
354             pAacDecoderChannelInfo[R]->pDynData->aSfbScale,
355             pAacDecoderChannelInfo[L]->specScale,
356             pAacDecoderChannelInfo[R]->specScale,
357             GetScaleFactorBandOffsets(&pAacDecoderChannelInfo[L]->icsInfo,
358                                       pSamplingRateInfo),
359             GetWindowGroupLengthTable(&pAacDecoderChannelInfo[L]->icsInfo),
360             GetWindowGroups(&pAacDecoderChannelInfo[L]->icsInfo), max_sfb_ste,
361             maxSfBandsL, maxSfBandsR,
362             pAacDecoderChannelInfo[L]
363                 ->pComData->jointStereoData.store_dmx_re_prev,
364             &(pAacDecoderChannelInfo[L]
365                   ->pComData->jointStereoData.store_dmx_re_prev_e),
366             1);
367       }
368 
369     } /* if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow) */
370 
371   } /* if (maybe_jstereo) */
372 
373   for (ch = 0; ch < el_channels; ch++) {
374     if (elFlags & AC_EL_USAC_CP_POSSIBLE) {
375       pAacDecoderStaticChannelInfo[L]
376           ->pCpeStaticData->jointStereoPersistentData.clearSpectralCoeffs = 0;
377     }
378   }
379 
380   CRvlc_ElementCheck(pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo,
381                      flags, el_channels);
382 }
383 
CChannel_CodebookTableInit(CAacDecoderChannelInfo * pAacDecoderChannelInfo)384 void CChannel_CodebookTableInit(
385     CAacDecoderChannelInfo *pAacDecoderChannelInfo) {
386   int b, w, maxBands, maxWindows;
387   int maxSfb = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
388   UCHAR *pCodeBook = pAacDecoderChannelInfo->pDynData->aCodeBook;
389 
390   if (IsLongBlock(&pAacDecoderChannelInfo->icsInfo)) {
391     maxBands = 64;
392     maxWindows = 1;
393   } else {
394     maxBands = 16;
395     maxWindows = 8;
396   }
397 
398   for (w = 0; w < maxWindows; w++) {
399     for (b = 0; b < maxSfb; b++) {
400       pCodeBook[b] = ESCBOOK;
401     }
402     for (; b < maxBands; b++) {
403       pCodeBook[b] = ZERO_HCB;
404     }
405     pCodeBook += maxBands;
406   }
407 }
408 
409 /*
410  * Arbitrary order bitstream parser
411  */
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)412 AAC_DECODER_ERROR CChannelElement_Read(
413     HANDLE_FDK_BITSTREAM hBs, CAacDecoderChannelInfo *pAacDecoderChannelInfo[],
414     CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo[],
415     const AUDIO_OBJECT_TYPE aot, SamplingRateInfo *pSamplingRateInfo,
416     const UINT flags, const UINT elFlags, const UINT frame_length,
417     const UCHAR numberOfChannels, const SCHAR epConfig,
418     HANDLE_TRANSPORTDEC pTpDec) {
419   AAC_DECODER_ERROR error = AAC_DEC_OK;
420   const element_list_t *list;
421   int i, ch, decision_bit;
422   int crcReg1 = -1, crcReg2 = -1;
423   int cplxPred;
424   int ind_sw_cce_flag = 0, num_gain_element_lists = 0;
425 
426   FDK_ASSERT((numberOfChannels == 1) || (numberOfChannels == 2));
427 
428   /* Get channel element sequence table */
429   list = getBitstreamElementList(aot, epConfig, numberOfChannels, 0, elFlags);
430   if (list == NULL) {
431     error = AAC_DEC_UNSUPPORTED_FORMAT;
432     goto bail;
433   }
434 
435   CTns_Reset(&pAacDecoderChannelInfo[0]->pDynData->TnsData);
436   /* Set common window to 0 by default. If signalized in the bit stream it will
437    * be overwritten later explicitely */
438   pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow = 0;
439   if (flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) {
440     pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_active = 0;
441     pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_on_lr = 0;
442   }
443   if (numberOfChannels == 2) {
444     CTns_Reset(&pAacDecoderChannelInfo[1]->pDynData->TnsData);
445     pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow = 0;
446   }
447 
448   cplxPred = 0;
449   if (pAacDecoderStaticChannelInfo != NULL) {
450     if (elFlags & AC_EL_USAC_CP_POSSIBLE) {
451       pAacDecoderChannelInfo[0]->pComData->jointStereoData.cplx_pred_flag = 0;
452       cplxPred = 1;
453     }
454   }
455 
456   if (0 || (flags & (AC_ELD | AC_SCALABLE))) {
457     pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow = 1;
458     if (numberOfChannels == 2) {
459       pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow =
460           pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow;
461     }
462   }
463 
464   /* Iterate through sequence table */
465   i = 0;
466   ch = 0;
467   decision_bit = 0;
468   do {
469     switch (list->id[i]) {
470       case element_instance_tag:
471         pAacDecoderChannelInfo[0]->ElementInstanceTag = FDKreadBits(hBs, 4);
472         if (numberOfChannels == 2) {
473           pAacDecoderChannelInfo[1]->ElementInstanceTag =
474               pAacDecoderChannelInfo[0]->ElementInstanceTag;
475         }
476         break;
477       case common_window:
478         decision_bit =
479             pAacDecoderChannelInfo[ch]->pDynData->RawDataInfo.CommonWindow =
480                 FDKreadBits(hBs, 1);
481         if (numberOfChannels == 2) {
482           pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow =
483               pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow;
484         }
485         break;
486       case ics_info:
487         /* store last window sequence (utilized in complex stereo prediction)
488          * before reading new channel-info */
489         if (cplxPred) {
490           if (pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow) {
491             pAacDecoderStaticChannelInfo[0]
492                 ->pCpeStaticData->jointStereoPersistentData.winSeqPrev =
493                 pAacDecoderChannelInfo[0]->icsInfo.WindowSequence;
494             pAacDecoderStaticChannelInfo[0]
495                 ->pCpeStaticData->jointStereoPersistentData.winShapePrev =
496                 pAacDecoderChannelInfo[0]->icsInfo.WindowShape;
497           }
498         }
499         /* Read individual channel info */
500         error = IcsRead(hBs, &pAacDecoderChannelInfo[ch]->icsInfo,
501                         pSamplingRateInfo, flags);
502 
503         if (elFlags & AC_EL_LFE &&
504             GetWindowSequence(&pAacDecoderChannelInfo[ch]->icsInfo) !=
505                 BLOCK_LONG) {
506           error = AAC_DEC_PARSE_ERROR;
507           break;
508         }
509 
510         if (numberOfChannels == 2 &&
511             pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow) {
512           pAacDecoderChannelInfo[1]->icsInfo =
513               pAacDecoderChannelInfo[0]->icsInfo;
514         }
515         break;
516 
517       case common_max_sfb:
518         if (FDKreadBit(hBs) == 0) {
519           error = IcsReadMaxSfb(hBs, &pAacDecoderChannelInfo[1]->icsInfo,
520                                 pSamplingRateInfo);
521         }
522         break;
523 
524       case ltp_data_present:
525         if (FDKreadBits(hBs, 1) != 0) {
526           error = AAC_DEC_UNSUPPORTED_PREDICTION;
527         }
528         break;
529 
530       case ms:
531 
532         INT max_sfb_ste;
533         INT max_sfb_ste_clear;
534 
535         max_sfb_ste = GetScaleMaxFactorBandsTransmitted(
536             &pAacDecoderChannelInfo[0]->icsInfo,
537             &pAacDecoderChannelInfo[1]->icsInfo);
538 
539         max_sfb_ste_clear = 64;
540 
541         pAacDecoderChannelInfo[0]->icsInfo.max_sfb_ste = (UCHAR)max_sfb_ste;
542         pAacDecoderChannelInfo[1]->icsInfo.max_sfb_ste = (UCHAR)max_sfb_ste;
543 
544         if (flags & (AC_USAC | AC_RSV603DA) &&
545             pAacDecoderChannelInfo[ch]->pDynData->RawDataInfo.CommonWindow ==
546                 0) {
547           Clean_Complex_Prediction_coefficients(
548               &pAacDecoderStaticChannelInfo[0]
549                    ->pCpeStaticData->jointStereoPersistentData,
550               GetWindowGroups(&pAacDecoderChannelInfo[0]->icsInfo), 0, 64);
551         }
552 
553         if (CJointStereo_Read(
554                 hBs, &pAacDecoderChannelInfo[0]->pComData->jointStereoData,
555                 GetWindowGroups(&pAacDecoderChannelInfo[0]->icsInfo),
556                 max_sfb_ste, max_sfb_ste_clear,
557                 /* jointStereoPersistentData and cplxPredictionData are only
558                    available/allocated if cplxPred is active. */
559                 ((cplxPred == 0) || (pAacDecoderStaticChannelInfo == NULL))
560                     ? NULL
561                     : &pAacDecoderStaticChannelInfo[0]
562                            ->pCpeStaticData->jointStereoPersistentData,
563                 ((cplxPred == 0) || (pAacDecoderChannelInfo[0] == NULL))
564                     ? NULL
565                     : pAacDecoderChannelInfo[0]
566                           ->pComStaticData->cplxPredictionData,
567                 cplxPred,
568                 GetScaleFactorBandsTotal(&pAacDecoderChannelInfo[0]->icsInfo),
569                 GetWindowSequence(&pAacDecoderChannelInfo[0]->icsInfo),
570                 flags)) {
571           error = AAC_DEC_PARSE_ERROR;
572         }
573 
574         break;
575 
576       case global_gain:
577         pAacDecoderChannelInfo[ch]->pDynData->RawDataInfo.GlobalGain =
578             (UCHAR)FDKreadBits(hBs, 8);
579         break;
580 
581       case section_data:
582         error = CBlock_ReadSectionData(hBs, pAacDecoderChannelInfo[ch],
583                                        pSamplingRateInfo, flags);
584         break;
585 
586       case scale_factor_data_usac:
587         pAacDecoderChannelInfo[ch]->currAliasingSymmetry = 0;
588         /* Set active sfb codebook indexes to HCB_ESC to make them "active" */
589         CChannel_CodebookTableInit(
590             pAacDecoderChannelInfo[ch]); /*  equals ReadSectionData(self,
591                                             bs) in float soft. block.c
592                                             line: ~599 */
593         /* Note: The missing "break" is intentional here, since we need to call
594          * CBlock_ReadScaleFactorData(). */
595         FDK_FALLTHROUGH;
596 
597       case scale_factor_data:
598         if (flags & AC_ER_RVLC) {
599           /* read RVLC data from bitstream (error sens. cat. 1) */
600           CRvlc_Read(pAacDecoderChannelInfo[ch], hBs);
601         } else {
602           error = CBlock_ReadScaleFactorData(pAacDecoderChannelInfo[ch], hBs,
603                                              flags);
604         }
605         break;
606 
607       case pulse:
608         if (CPulseData_Read(
609                 hBs,
610                 &pAacDecoderChannelInfo[ch]->pDynData->specificTo.aac.PulseData,
611                 pSamplingRateInfo->ScaleFactorBands_Long, /* pulse data is only
612                                                              allowed to be
613                                                              present in long
614                                                              blocks! */
615                 (void *)&pAacDecoderChannelInfo[ch]->icsInfo,
616                 frame_length) != 0) {
617           error = AAC_DEC_DECODE_FRAME_ERROR;
618         }
619         break;
620       case tns_data_present:
621         CTns_ReadDataPresentFlag(
622             hBs, &pAacDecoderChannelInfo[ch]->pDynData->TnsData);
623         if (elFlags & AC_EL_LFE &&
624             pAacDecoderChannelInfo[ch]->pDynData->TnsData.DataPresent) {
625           error = AAC_DEC_PARSE_ERROR;
626         }
627         break;
628       case tns_data:
629         /* tns_data_present is checked inside CTns_Read(). */
630         error = CTns_Read(hBs, &pAacDecoderChannelInfo[ch]->pDynData->TnsData,
631                           &pAacDecoderChannelInfo[ch]->icsInfo, flags);
632 
633         break;
634 
635       case gain_control_data:
636         break;
637 
638       case gain_control_data_present:
639         if (FDKreadBits(hBs, 1)) {
640           error = AAC_DEC_UNSUPPORTED_GAIN_CONTROL_DATA;
641         }
642         break;
643 
644       case tw_data:
645         break;
646       case common_tw:
647         break;
648       case tns_data_present_usac:
649         if (pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_active) {
650           CTns_ReadDataPresentUsac(
651               hBs, &pAacDecoderChannelInfo[0]->pDynData->TnsData,
652               &pAacDecoderChannelInfo[1]->pDynData->TnsData,
653               &pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_on_lr,
654               &pAacDecoderChannelInfo[0]->icsInfo, flags, elFlags,
655               pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow);
656         } else {
657           pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_on_lr =
658               (UCHAR)1;
659         }
660         break;
661       case core_mode:
662         decision_bit = FDKreadBits(hBs, 1);
663         pAacDecoderChannelInfo[ch]->data.usac.core_mode = decision_bit;
664         if ((ch == 1) && (pAacDecoderChannelInfo[0]->data.usac.core_mode !=
665                           pAacDecoderChannelInfo[1]->data.usac.core_mode)) {
666           /* StereoCoreToolInfo(core_mode[ch] ) */
667           pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow = 0;
668           pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow = 0;
669         }
670         break;
671       case tns_active:
672         pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_active =
673             FDKreadBit(hBs);
674         break;
675       case noise:
676         if (elFlags & AC_EL_USAC_NOISE) {
677           pAacDecoderChannelInfo[ch]
678               ->pDynData->specificTo.usac.fd_noise_level_and_offset =
679               FDKreadBits(hBs, 3 + 5); /* Noise level */
680         }
681         break;
682       case lpd_channel_stream:
683 
684       {
685         error = CLpdChannelStream_Read(/* = lpd_channel_stream() */
686                                        hBs, pAacDecoderChannelInfo[ch],
687                                        pAacDecoderStaticChannelInfo[ch],
688                                        pSamplingRateInfo, flags);
689       }
690 
691         pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_LPD;
692         break;
693       case fac_data: {
694         int fFacDatPresent = FDKreadBit(hBs);
695 
696         /* Wee need a valid fac_data[0] even if no FAC data is present (as
697          * temporal buffer) */
698         pAacDecoderChannelInfo[ch]->data.usac.fac_data[0] =
699             pAacDecoderChannelInfo[ch]->data.usac.fac_data0;
700 
701         if (fFacDatPresent) {
702           if (elFlags & AC_EL_LFE) {
703             error = AAC_DEC_PARSE_ERROR;
704             break;
705           }
706           /* FAC data present, this frame is FD, so the last mode had to be
707            * ACELP. */
708           if (pAacDecoderStaticChannelInfo[ch]->last_core_mode != LPD ||
709               pAacDecoderStaticChannelInfo[ch]->last_lpd_mode != 0) {
710             pAacDecoderChannelInfo[ch]->data.usac.core_mode_last = LPD;
711             pAacDecoderChannelInfo[ch]->data.usac.lpd_mode_last = 0;
712             /* We can't change the past! So look to the future and go ahead! */
713           }
714           CLpd_FAC_Read(hBs, pAacDecoderChannelInfo[ch]->data.usac.fac_data[0],
715                         pAacDecoderChannelInfo[ch]->data.usac.fac_data_e,
716                         CLpd_FAC_getLength(
717                             IsLongBlock(&pAacDecoderChannelInfo[ch]->icsInfo),
718                             pAacDecoderChannelInfo[ch]->granuleLength),
719                         1, 0);
720         } else {
721           if (pAacDecoderStaticChannelInfo[ch]->last_core_mode == LPD &&
722               pAacDecoderStaticChannelInfo[ch]->last_lpd_mode == 0) {
723             /* ACELP to FD transitons without FAC are possible. That is why we
724             zero it out (i.e FAC will not be considered in the subsequent
725             calculations */
726             FDKmemclear(pAacDecoderChannelInfo[ch]->data.usac.fac_data0,
727                         LFAC * sizeof(FIXP_DBL));
728           }
729         }
730       } break;
731       case esc2_rvlc:
732         if (flags & AC_ER_RVLC) {
733           CRvlc_Decode(pAacDecoderChannelInfo[ch],
734                        pAacDecoderStaticChannelInfo[ch], hBs);
735         }
736         break;
737 
738       case esc1_hcr:
739         if (flags & AC_ER_HCR) {
740           CHcr_Read(hBs, pAacDecoderChannelInfo[ch],
741                     numberOfChannels == 2 ? ID_CPE : ID_SCE);
742         }
743         break;
744 
745       case spectral_data:
746         error = CBlock_ReadSpectralData(hBs, pAacDecoderChannelInfo[ch],
747                                         pSamplingRateInfo, flags);
748         if (flags & AC_ELD) {
749           pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_ELDFB;
750         } else {
751           if (flags & AC_HDAAC) {
752             pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_INTIMDCT;
753           } else {
754             pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_IMDCT;
755           }
756         }
757         break;
758 
759       case ac_spectral_data:
760         error = CBlock_ReadAcSpectralData(
761             hBs, pAacDecoderChannelInfo[ch], pAacDecoderStaticChannelInfo[ch],
762             pSamplingRateInfo, frame_length, flags);
763         pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_IMDCT;
764         break;
765 
766       case coupled_elements: {
767         int num_coupled_elements, c;
768 
769         ind_sw_cce_flag = FDKreadBit(hBs);
770         num_coupled_elements = FDKreadBits(hBs, 3);
771 
772         for (c = 0; c < (num_coupled_elements + 1); c++) {
773           int cc_target_is_cpe;
774 
775           num_gain_element_lists++;
776           cc_target_is_cpe = FDKreadBit(hBs); /* cc_target_is_cpe[c] */
777           FDKreadBits(hBs, 4);                /* cc_target_tag_select[c] */
778 
779           if (cc_target_is_cpe) {
780             int cc_l, cc_r;
781 
782             cc_l = FDKreadBit(hBs); /* cc_l[c] */
783             cc_r = FDKreadBit(hBs); /* cc_r[c] */
784 
785             if (cc_l && cc_r) {
786               num_gain_element_lists++;
787             }
788           }
789         }
790         FDKreadBit(hBs);     /* cc_domain */
791         FDKreadBit(hBs);     /* gain_element_sign  */
792         FDKreadBits(hBs, 2); /* gain_element_scale */
793       } break;
794 
795       case gain_element_lists: {
796         const CodeBookDescription *hcb;
797         UCHAR *pCodeBook;
798         int c;
799 
800         hcb = &AACcodeBookDescriptionTable[BOOKSCL];
801         pCodeBook = pAacDecoderChannelInfo[ch]->pDynData->aCodeBook;
802 
803         for (c = 1; c < num_gain_element_lists; c++) {
804           int cge;
805           if (ind_sw_cce_flag) {
806             cge = 1;
807           } else {
808             cge = FDKreadBits(hBs, 1); /* common_gain_element_present[c] */
809           }
810           if (cge) {
811             /* Huffman */
812             CBlock_DecodeHuffmanWord(
813                 hBs, hcb); /* hcod_sf[common_gain_element[c]] 1..19 */
814           } else {
815             int g, sfb;
816             for (g = 0;
817                  g < GetWindowGroups(&pAacDecoderChannelInfo[ch]->icsInfo);
818                  g++) {
819               for (sfb = 0; sfb < GetScaleFactorBandsTransmitted(
820                                       &pAacDecoderChannelInfo[ch]->icsInfo);
821                    sfb++) {
822                 if (pCodeBook[sfb] != ZERO_HCB) {
823                   /* Huffman */
824                   CBlock_DecodeHuffmanWord(
825                       hBs,
826                       hcb); /* hcod_sf[dpcm_gain_element[c][g][sfb]] 1..19 */
827                 }
828               }
829             }
830           }
831         }
832       } break;
833 
834         /* CRC handling */
835       case adtscrc_start_reg1:
836         if (pTpDec != NULL) {
837           crcReg1 = transportDec_CrcStartReg(pTpDec, 192);
838         }
839         break;
840       case adtscrc_start_reg2:
841         if (pTpDec != NULL) {
842           crcReg2 = transportDec_CrcStartReg(pTpDec, 128);
843         }
844         break;
845       case adtscrc_end_reg1:
846       case drmcrc_end_reg:
847         if (pTpDec != NULL) {
848           transportDec_CrcEndReg(pTpDec, crcReg1);
849           crcReg1 = -1;
850         }
851         break;
852       case adtscrc_end_reg2:
853         if (crcReg1 != -1) {
854           error = AAC_DEC_DECODE_FRAME_ERROR;
855         } else if (pTpDec != NULL) {
856           transportDec_CrcEndReg(pTpDec, crcReg2);
857           crcReg2 = -1;
858         }
859         break;
860       case drmcrc_start_reg:
861         if (pTpDec != NULL) {
862           crcReg1 = transportDec_CrcStartReg(pTpDec, 0);
863         }
864         break;
865 
866         /* Non data cases */
867       case next_channel:
868         ch = (ch + 1) % numberOfChannels;
869         break;
870       case link_sequence:
871         list = list->next[decision_bit];
872         i = -1;
873         break;
874 
875       default:
876         error = AAC_DEC_UNSUPPORTED_FORMAT;
877         break;
878     }
879 
880     if (error != AAC_DEC_OK) {
881       goto bail;
882     }
883 
884     i++;
885 
886   } while (list->id[i] != end_of_sequence);
887 
888   for (ch = 0; ch < numberOfChannels; ch++) {
889     if (pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_IMDCT ||
890         pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_ELDFB) {
891       /* Shows which bands are empty. */
892       UCHAR *band_is_noise =
893           pAacDecoderChannelInfo[ch]->pDynData->band_is_noise;
894       FDKmemset(band_is_noise, (UCHAR)1, sizeof(UCHAR) * (8 * 16));
895 
896       error = CBlock_InverseQuantizeSpectralData(
897           pAacDecoderChannelInfo[ch], pSamplingRateInfo, band_is_noise, 1);
898       if (error != AAC_DEC_OK) {
899         return error;
900       }
901 
902       if (elFlags & AC_EL_USAC_NOISE) {
903         CBlock_ApplyNoise(pAacDecoderChannelInfo[ch], pSamplingRateInfo,
904                           &pAacDecoderStaticChannelInfo[ch]->nfRandomSeed,
905                           band_is_noise);
906 
907       } /* if (elFlags & AC_EL_USAC_NOISE) */
908     }
909   }
910 
911 bail:
912   if (crcReg1 != -1 || crcReg2 != -1) {
913     if (error == AAC_DEC_OK) {
914       error = AAC_DEC_DECODE_FRAME_ERROR;
915     }
916     if (crcReg1 != -1) {
917       transportDec_CrcEndReg(pTpDec, crcReg1);
918     }
919     if (crcReg2 != -1) {
920       transportDec_CrcEndReg(pTpDec, crcReg2);
921     }
922   }
923   return error;
924 }
925