1 
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4 
5 � Copyright  1995 - 2013 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
6   All rights reserved.
7 
8  1.    INTRODUCTION
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
17 
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
24 
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
28 
29 2.    COPYRIGHT LICENSE
30 
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
33 
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
36 
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
41 
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44 
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
47 
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and the date of any change. For modified versions of the FDK AAC Codec, the term
50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
52 
53 3.    NO PATENT LICENSE
54 
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
58 
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61 
62 4.    DISCLAIMER
63 
64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
72 
73 5.    CONTACT INFORMATION
74 
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79 
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
83 
84 #include "psbitdec.h"
85 
86 
87 #include "sbr_rom.h"
88 #include "huff_dec.h"
89 
90 /* PS dec privat functions */
91 SBR_ERROR ResetPsDec(HANDLE_PS_DEC h_ps_d);
92 void ResetPsDeCor   (HANDLE_PS_DEC h_ps_d);
93 
94 /***************************************************************************/
95 /*!
96   \brief  huffman decoding by codebook table
97 
98   \return index of huffman codebook table
99 
100 ****************************************************************************/
101 static SCHAR
decode_huff_cw(Huffman h,HANDLE_FDK_BITSTREAM hBitBuf,int * length)102 decode_huff_cw (Huffman h,                      /*!< pointer to huffman codebook table */
103                 HANDLE_FDK_BITSTREAM hBitBuf,   /*!< Handle to Bitbuffer */
104                 int *length)                    /*!< length of huffman codeword (or NULL) */
105 {
106   UCHAR bit = 0;
107   SCHAR index = 0;
108   UCHAR bitCount = 0;
109 
110   while (index >= 0) {
111     bit = FDKreadBits (hBitBuf, 1);
112     bitCount++;
113     index = h[index][bit];
114   }
115   if (length) {
116     *length = bitCount;
117   }
118   return( index+64 ); /* Add offset */
119 }
120 
121 /***************************************************************************/
122 /*!
123   \brief  helper function - limiting of value to min/max values
124 
125   \return limited value
126 
127 ****************************************************************************/
128 
129 static SCHAR
limitMinMax(SCHAR i,SCHAR min,SCHAR max)130 limitMinMax(SCHAR i,
131             SCHAR min,
132             SCHAR max)
133 {
134   if (i<min)
135     return min;
136   else if (i>max)
137     return max;
138   else
139     return i;
140 }
141 
142 /***************************************************************************/
143 /*!
144   \brief  Decodes delta values in-place and updates
145           data buffers according to quantization classes.
146 
147   When delta coded in frequency the first element is deltacode from zero.
148   aIndex buffer is decoded from delta values to actual values.
149 
150   \return none
151 
152 ****************************************************************************/
153 static void
deltaDecodeArray(SCHAR enable,SCHAR * aIndex,SCHAR * aPrevFrameIndex,SCHAR DtDf,UCHAR nrElements,UCHAR stride,SCHAR minIdx,SCHAR maxIdx)154 deltaDecodeArray(SCHAR enable,
155                  SCHAR *aIndex,          /*!< ICC/IID parameters */
156                  SCHAR *aPrevFrameIndex, /*!< ICC/IID parameters  of previous frame */
157                  SCHAR DtDf,
158                  UCHAR nrElements,       /*!< as conveyed in bitstream */
159                                          /*!< output array size: nrElements*stride */
160                  UCHAR stride,           /*!< 1=dflt, 2=half freq. resolution */
161                  SCHAR minIdx,
162                  SCHAR maxIdx)
163 {
164   int i;
165 
166   /* Delta decode */
167   if ( enable==1 ) {
168     if (DtDf == 0)  {   /* Delta coded in freq */
169       aIndex[0] = 0 + aIndex[0];
170       aIndex[0] = limitMinMax(aIndex[0],minIdx,maxIdx);
171       for (i = 1; i < nrElements; i++) {
172         aIndex[i] = aIndex[i-1] + aIndex[i];
173         aIndex[i] = limitMinMax(aIndex[i],minIdx,maxIdx);
174       }
175     }
176     else { /* Delta time */
177       for (i = 0; i < nrElements; i++) {
178         aIndex[i] = aPrevFrameIndex[i*stride] + aIndex[i];
179         aIndex[i] = limitMinMax(aIndex[i],minIdx,maxIdx);
180       }
181     }
182   }
183   else { /* No data is sent, set index to zero */
184     for (i = 0; i < nrElements; i++) {
185       aIndex[i] = 0;
186     }
187   }
188   if (stride==2) {
189     for (i=nrElements*stride-1; i>0; i--) {
190       aIndex[i] = aIndex[i>>1];
191     }
192   }
193 }
194 
195 /***************************************************************************/
196 /*!
197   \brief Mapping of ICC/IID parameters to 20 stereo bands
198 
199   \return none
200 
201 ****************************************************************************/
map34IndexTo20(SCHAR * aIndex,UCHAR noBins)202 static void map34IndexTo20 (SCHAR *aIndex, /*!< decoded ICC/IID parameters */
203                             UCHAR noBins)  /*!< number of stereo bands     */
204 {
205   aIndex[0]  = (2*aIndex[0]+aIndex[1])/3;
206   aIndex[1]  = (aIndex[1]+2*aIndex[2])/3;
207   aIndex[2]  = (2*aIndex[3]+aIndex[4])/3;
208   aIndex[3]  = (aIndex[4]+2*aIndex[5])/3;
209   aIndex[4]  = (aIndex[6]+aIndex[7])/2;
210   aIndex[5]  = (aIndex[8]+aIndex[9])/2;
211   aIndex[6]  = aIndex[10];
212   aIndex[7]  = aIndex[11];
213   aIndex[8]  = (aIndex[12]+aIndex[13])/2;
214   aIndex[9]  = (aIndex[14]+aIndex[15])/2;
215   aIndex[10] = aIndex[16];
216   /* For IPD/OPD it stops here */
217 
218   if (noBins == NO_HI_RES_BINS)
219   {
220     aIndex[11] = aIndex[17];
221     aIndex[12] = aIndex[18];
222     aIndex[13] = aIndex[19];
223     aIndex[14] = (aIndex[20]+aIndex[21])/2;
224     aIndex[15] = (aIndex[22]+aIndex[23])/2;
225     aIndex[16] = (aIndex[24]+aIndex[25])/2;
226     aIndex[17] = (aIndex[26]+aIndex[27])/2;
227     aIndex[18] = (aIndex[28]+aIndex[29]+aIndex[30]+aIndex[31])/4;
228     aIndex[19] = (aIndex[32]+aIndex[33])/2;
229   }
230 }
231 
232 /***************************************************************************/
233 /*!
234   \brief  Decodes delta coded IID, ICC, IPD and OPD indices
235 
236   \return PS processing flag. If set to 1
237 
238 ****************************************************************************/
239 int
DecodePs(struct PS_DEC * h_ps_d,const UCHAR frameError)240 DecodePs( struct PS_DEC *h_ps_d,      /*!< PS handle */
241           const UCHAR    frameError ) /*!< Flag telling that frame had errors */
242 {
243   MPEG_PS_BS_DATA *pBsData;
244   UCHAR gr, env;
245   int   bPsHeaderValid, bPsDataAvail;
246 
247   /* Shortcuts to avoid deferencing and keep the code readable */
248   pBsData = &h_ps_d->bsData[h_ps_d->processSlot].mpeg;
249   bPsHeaderValid = pBsData->bPsHeaderValid;
250   bPsDataAvail = (h_ps_d->bPsDataAvail[h_ps_d->processSlot] == ppt_mpeg) ? 1 : 0;
251 
252  /***************************************************************************************
253   * Decide whether to process or to conceal PS data or not.                             */
254 
255   if ( ( h_ps_d->psDecodedPrv && !frameError && !bPsDataAvail)
256     || (!h_ps_d->psDecodedPrv && (frameError || !bPsDataAvail || !bPsHeaderValid)) ) {
257     /* Don't apply PS processing.
258      * Declare current PS header and bitstream data invalid. */
259     pBsData->bPsHeaderValid = 0;
260     h_ps_d->bPsDataAvail[h_ps_d->processSlot] = ppt_none;
261     return (0);
262   }
263 
264   if (frameError || !bPsHeaderValid)
265   { /* no new PS data available (e.g. frame loss) */
266     /* => keep latest data constant (i.e. FIX with noEnv=0) */
267     pBsData->noEnv = 0;
268   }
269 
270  /***************************************************************************************
271   * Decode bitstream payload or prepare parameter for concealment:
272   */
273   for (env=0; env<pBsData->noEnv; env++) {
274     SCHAR *aPrevIidIndex;
275     SCHAR *aPrevIccIndex;
276 
277     UCHAR noIidSteps = pBsData->bFineIidQ?NO_IID_STEPS_FINE:NO_IID_STEPS;
278 
279     if (env==0) {
280       aPrevIidIndex = h_ps_d->specificTo.mpeg.aIidPrevFrameIndex;
281       aPrevIccIndex = h_ps_d->specificTo.mpeg.aIccPrevFrameIndex;
282     }
283     else {
284       aPrevIidIndex = pBsData->aaIidIndex[env-1];
285       aPrevIccIndex = pBsData->aaIccIndex[env-1];
286     }
287 
288     deltaDecodeArray(pBsData->bEnableIid,
289                      pBsData->aaIidIndex[env],
290                      aPrevIidIndex,
291                      pBsData->abIidDtFlag[env],
292                      FDK_sbrDecoder_aNoIidBins[pBsData->freqResIid],
293                      (pBsData->freqResIid)?1:2,
294                      -noIidSteps,
295                      noIidSteps);
296 
297     deltaDecodeArray(pBsData->bEnableIcc,
298                      pBsData->aaIccIndex[env],
299                      aPrevIccIndex,
300                      pBsData->abIccDtFlag[env],
301                      FDK_sbrDecoder_aNoIccBins[pBsData->freqResIcc],
302                      (pBsData->freqResIcc)?1:2,
303                      0,
304                      NO_ICC_STEPS-1);
305   }   /* for (env=0; env<pBsData->noEnv; env++) */
306 
307   /* handling of FIX noEnv=0 */
308   if (pBsData->noEnv==0) {
309     /* set noEnv=1, keep last parameters or force 0 if not enabled */
310     pBsData->noEnv = 1;
311 
312     if (pBsData->bEnableIid) {
313       for (gr = 0; gr < NO_HI_RES_IID_BINS; gr++) {
314         pBsData->aaIidIndex[pBsData->noEnv-1][gr] =
315           h_ps_d->specificTo.mpeg.aIidPrevFrameIndex[gr];
316       }
317     }
318     else {
319       for (gr = 0; gr < NO_HI_RES_IID_BINS; gr++) {
320         pBsData->aaIidIndex[pBsData->noEnv-1][gr] = 0;
321       }
322     }
323 
324     if (pBsData->bEnableIcc) {
325       for (gr = 0; gr < NO_HI_RES_ICC_BINS; gr++) {
326         pBsData->aaIccIndex[pBsData->noEnv-1][gr] =
327           h_ps_d->specificTo.mpeg.aIccPrevFrameIndex[gr];
328       }
329     }
330     else {
331       for (gr = 0; gr < NO_HI_RES_ICC_BINS; gr++) {
332         pBsData->aaIccIndex[pBsData->noEnv-1][gr] = 0;
333       }
334     }
335   }
336 
337   /* Update previous frame index buffers */
338   for (gr = 0; gr < NO_HI_RES_IID_BINS; gr++) {
339     h_ps_d->specificTo.mpeg.aIidPrevFrameIndex[gr] =
340       pBsData->aaIidIndex[pBsData->noEnv-1][gr];
341   }
342   for (gr = 0; gr < NO_HI_RES_ICC_BINS; gr++) {
343     h_ps_d->specificTo.mpeg.aIccPrevFrameIndex[gr] =
344       pBsData->aaIccIndex[pBsData->noEnv-1][gr];
345   }
346 
347   /* PS data from bitstream (if avail) was decoded now */
348   h_ps_d->bPsDataAvail[h_ps_d->processSlot] = ppt_none;
349 
350   /* handling of env borders for FIX & VAR */
351   if (pBsData->bFrameClass == 0) {
352     /* FIX_BORDERS NoEnv=0,1,2,4 */
353     pBsData->aEnvStartStop[0] = 0;
354     for (env=1; env<pBsData->noEnv; env++) {
355       pBsData->aEnvStartStop[env] =
356         (env * h_ps_d->noSubSamples) / pBsData->noEnv;
357     }
358     pBsData->aEnvStartStop[pBsData->noEnv] = h_ps_d->noSubSamples;
359     /* 1024 (32 slots) env borders:  0, 8, 16, 24, 32 */
360     /*  960 (30 slots) env borders:  0, 7, 15, 22, 30 */
361   }
362   else {   /* if (h_ps_d->bFrameClass == 0) */
363     /* VAR_BORDERS NoEnv=1,2,3,4 */
364     pBsData->aEnvStartStop[0] = 0;
365 
366     /* handle case aEnvStartStop[noEnv]<noSubSample for VAR_BORDERS by
367        duplicating last PS parameters and incrementing noEnv */
368     if (pBsData->aEnvStartStop[pBsData->noEnv] < h_ps_d->noSubSamples) {
369       for (gr = 0; gr < NO_HI_RES_IID_BINS; gr++) {
370         pBsData->aaIidIndex[pBsData->noEnv][gr] =
371           pBsData->aaIidIndex[pBsData->noEnv-1][gr];
372       }
373       for (gr = 0; gr < NO_HI_RES_ICC_BINS; gr++) {
374         pBsData->aaIccIndex[pBsData->noEnv][gr] =
375           pBsData->aaIccIndex[pBsData->noEnv-1][gr];
376       }
377       pBsData->noEnv++;
378       pBsData->aEnvStartStop[pBsData->noEnv] = h_ps_d->noSubSamples;
379     }
380 
381     /* enforce strictly monotonic increasing borders */
382     for (env=1; env<pBsData->noEnv; env++) {
383       UCHAR thr;
384       thr = (UCHAR)h_ps_d->noSubSamples - (pBsData->noEnv - env);
385       if (pBsData->aEnvStartStop[env] > thr) {
386         pBsData->aEnvStartStop[env] = thr;
387       }
388       else {
389         thr = pBsData->aEnvStartStop[env-1]+1;
390         if (pBsData->aEnvStartStop[env] < thr) {
391           pBsData->aEnvStartStop[env] = thr;
392         }
393       }
394     }
395   }   /* if (h_ps_d->bFrameClass == 0) ... else */
396 
397   /* copy data prior to possible 20<->34 in-place mapping */
398   for (env=0; env<pBsData->noEnv; env++) {
399     UCHAR i;
400     for (i=0; i<NO_HI_RES_IID_BINS; i++) {
401       h_ps_d->specificTo.mpeg.coef.aaIidIndexMapped[env][i] = pBsData->aaIidIndex[env][i];
402     }
403     for (i=0; i<NO_HI_RES_ICC_BINS; i++) {
404       h_ps_d->specificTo.mpeg.coef.aaIccIndexMapped[env][i] = pBsData->aaIccIndex[env][i];
405     }
406   }
407 
408 
409   /* MPEG baseline PS */
410   /* Baseline version of PS always uses the hybrid filter structure with 20 stereo bands. */
411   /* If ICC/IID parameters for 34 stereo bands are decoded they have to be mapped to 20   */
412   /* stereo bands.                                                                        */
413   /* Additionaly the IPD/OPD parameters won't be used.                                    */
414 
415   for (env=0; env<pBsData->noEnv; env++) {
416     if (pBsData->freqResIid == 2)
417       map34IndexTo20 (h_ps_d->specificTo.mpeg.coef.aaIidIndexMapped[env], NO_HI_RES_IID_BINS);
418     if (pBsData->freqResIcc == 2)
419       map34IndexTo20 (h_ps_d->specificTo.mpeg.coef.aaIccIndexMapped[env], NO_HI_RES_ICC_BINS);
420 
421     /* IPD/OPD is disabled in baseline version and thus was removed here */
422   }
423 
424   return (1);
425 }
426 
427 
428 /***************************************************************************/
429 /*!
430 
431   \brief  Reads parametric stereo data from bitstream
432 
433   \return
434 
435 ****************************************************************************/
436 unsigned int
ReadPsData(HANDLE_PS_DEC h_ps_d,HANDLE_FDK_BITSTREAM hBitBuf,int nBitsLeft)437 ReadPsData (HANDLE_PS_DEC h_ps_d,          /*!< handle to struct PS_DEC */
438             HANDLE_FDK_BITSTREAM hBitBuf,  /*!< handle to struct BIT_BUF */
439             int nBitsLeft                  /*!< max number of bits available */
440            )
441 {
442   MPEG_PS_BS_DATA *pBsData;
443 
444   UCHAR     gr, env;
445   SCHAR     dtFlag;
446   INT       startbits;
447   Huffman   CurrentTable;
448   SCHAR     bEnableHeader;
449 
450   if (!h_ps_d)
451     return 0;
452 
453   pBsData = &h_ps_d->bsData[h_ps_d->bsReadSlot].mpeg;
454 
455   if (h_ps_d->bsReadSlot != h_ps_d->bsLastSlot) {
456     /* Copy last header data */
457     FDKmemcpy(pBsData, &h_ps_d->bsData[h_ps_d->bsLastSlot].mpeg, sizeof(MPEG_PS_BS_DATA));
458   }
459 
460 
461   startbits = (INT) FDKgetValidBits(hBitBuf);
462 
463   bEnableHeader = (SCHAR) FDKreadBits (hBitBuf, 1);
464 
465   /* Read header */
466   if (bEnableHeader) {
467     pBsData->bPsHeaderValid = 1;
468     pBsData->bEnableIid = (UCHAR) FDKreadBits (hBitBuf, 1);
469     if (pBsData->bEnableIid) {
470       pBsData->modeIid = (UCHAR) FDKreadBits (hBitBuf, 3);
471     }
472 
473     pBsData->bEnableIcc = (UCHAR) FDKreadBits (hBitBuf, 1);
474     if (pBsData->bEnableIcc) {
475       pBsData->modeIcc = (UCHAR) FDKreadBits (hBitBuf, 3);
476     }
477 
478     pBsData->bEnableExt = (UCHAR) FDKreadBits (hBitBuf, 1);
479   }
480 
481   pBsData->bFrameClass = (UCHAR) FDKreadBits (hBitBuf, 1);
482   if (pBsData->bFrameClass == 0) {
483     /* FIX_BORDERS NoEnv=0,1,2,4 */
484     pBsData->noEnv = FDK_sbrDecoder_aFixNoEnvDecode[(UCHAR) FDKreadBits (hBitBuf, 2)];
485     /* all additional handling of env borders is now in DecodePs() */
486   }
487   else {
488     /* VAR_BORDERS NoEnv=1,2,3,4 */
489     pBsData->noEnv = 1+(UCHAR) FDKreadBits (hBitBuf, 2);
490     for (env=1; env<pBsData->noEnv+1; env++)
491       pBsData->aEnvStartStop[env] = ((UCHAR) FDKreadBits (hBitBuf, 5)) + 1;
492     /* all additional handling of env borders is now in DecodePs() */
493   }
494 
495   /* verify that IID & ICC modes (quant grid, freq res) are supported */
496   if ((pBsData->modeIid > 5) || (pBsData->modeIcc > 5)) {
497     /* no useful PS data could be read from bitstream */
498     h_ps_d->bPsDataAvail[h_ps_d->bsReadSlot] = ppt_none;
499     /* discard all remaining bits */
500     nBitsLeft -= startbits - FDKgetValidBits(hBitBuf);
501     while (nBitsLeft) {
502       int i = nBitsLeft;
503       if (i>8) {
504         i = 8;
505       }
506       FDKreadBits (hBitBuf, i);
507       nBitsLeft -= i;
508     }
509     return (startbits - FDKgetValidBits(hBitBuf));
510   }
511 
512   if (pBsData->modeIid > 2){
513     pBsData->freqResIid = pBsData->modeIid-3;
514     pBsData->bFineIidQ = 1;
515   }
516   else{
517     pBsData->freqResIid = pBsData->modeIid;
518     pBsData->bFineIidQ = 0;
519   }
520 
521   if (pBsData->modeIcc > 2){
522     pBsData->freqResIcc = pBsData->modeIcc-3;
523   }
524   else{
525     pBsData->freqResIcc = pBsData->modeIcc;
526   }
527 
528 
529   /* Extract IID data */
530   if (pBsData->bEnableIid) {
531     for (env=0; env<pBsData->noEnv; env++) {
532       dtFlag = (SCHAR)FDKreadBits (hBitBuf, 1);
533       if (!dtFlag)
534       {
535         if (pBsData->bFineIidQ)
536           CurrentTable = (Huffman)&aBookPsIidFineFreqDecode;
537         else
538           CurrentTable = (Huffman)&aBookPsIidFreqDecode;
539       }
540       else
541       {
542         if (pBsData->bFineIidQ)
543          CurrentTable = (Huffman)&aBookPsIidFineTimeDecode;
544         else
545           CurrentTable = (Huffman)&aBookPsIidTimeDecode;
546       }
547 
548       for (gr = 0; gr < FDK_sbrDecoder_aNoIidBins[pBsData->freqResIid]; gr++)
549         pBsData->aaIidIndex[env][gr] = decode_huff_cw(CurrentTable,hBitBuf,NULL);
550       pBsData->abIidDtFlag[env] = dtFlag;
551     }
552   }
553 
554   /* Extract ICC data */
555   if (pBsData->bEnableIcc) {
556     for (env=0; env<pBsData->noEnv; env++) {
557       dtFlag = (SCHAR)FDKreadBits (hBitBuf, 1);
558       if (!dtFlag)
559         CurrentTable = (Huffman)&aBookPsIccFreqDecode;
560       else
561         CurrentTable = (Huffman)&aBookPsIccTimeDecode;
562 
563       for (gr = 0; gr < FDK_sbrDecoder_aNoIccBins[pBsData->freqResIcc]; gr++)
564         pBsData->aaIccIndex[env][gr] = decode_huff_cw(CurrentTable,hBitBuf,NULL);
565       pBsData->abIccDtFlag[env] = dtFlag;
566     }
567   }
568 
569   if (pBsData->bEnableExt) {
570 
571     /*!
572     Decoders that support only the baseline version of the PS tool are allowed
573     to ignore the IPD/OPD data, but according header data has to be parsed.
574     ISO/IEC 14496-3 Subpart 8 Annex 4
575     */
576 
577     int cnt = FDKreadBits(hBitBuf, PS_EXTENSION_SIZE_BITS);
578     if (cnt == (1<<PS_EXTENSION_SIZE_BITS)-1) {
579       cnt += FDKreadBits(hBitBuf, PS_EXTENSION_ESC_COUNT_BITS);
580     }
581     while (cnt--)
582       FDKreadBits(hBitBuf, 8);
583   }
584 
585 
586   /* new PS data was read from bitstream */
587   h_ps_d->bPsDataAvail[h_ps_d->bsReadSlot] = ppt_mpeg;
588 
589 
590 
591   return (startbits - FDKgetValidBits(hBitBuf));
592 }
593 
594