videoeditorengine/mp3aacManipLib/AACGain/src/sbr_codec.cpp
branchRCL_3
changeset 3 e0b5df5c0969
parent 0 951a5db380a0
child 5 4c409de21d23
equal deleted inserted replaced
0:951a5db380a0 3:e0b5df5c0969
     1 /*
       
     2 * Copyright (c) 2010 Ixonos Plc.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - Initial contribution
       
    11 *
       
    12 * Contributors:
       
    13 * Ixonos Plc
       
    14 *
       
    15 * Description:
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /*
       
    21   \file
       
    22   \brief SBR codec implementation $Revision: 1.1.1.1.4.1 $
       
    23 */
       
    24 
       
    25 /**************************************************************************
       
    26   sbr_codec.cpp - SBR codec implementation.
       
    27  
       
    28   Author(s): Juha Ojanpera
       
    29   Copyright (c) 2004 by Nokia Research Center, Multimedia Technologies.
       
    30   *************************************************************************/
       
    31 
       
    32 /*-- Project Headers. --*/
       
    33 #include "sbr_codec.h"
       
    34 #include "env_extr.h"
       
    35 #include "sbr_rom.h"
       
    36 #include "sbr_bitmux.h"
       
    37 
       
    38 struct SBR_Decoder_Instance
       
    39 {
       
    40   SbrFrameData *frameData[2];
       
    41   SbrHeaderData *sbrHeader[2];
       
    42   FreqBandData *freqBandData[2];
       
    43 
       
    44   SbrExtensionData *sbrExtData;
       
    45 
       
    46 };
       
    47 
       
    48 
       
    49 FLOAT 
       
    50 FloatFR_logDualis(int16 a) 
       
    51 { 
       
    52   return (logDualisTable[a]); 
       
    53 }
       
    54 
       
    55 FLOAT 
       
    56 FloatFR_getNumOctaves(int16 a, int16 b) 
       
    57 {
       
    58   return (FloatFR_logDualis(b) - FloatFR_logDualis(a));
       
    59 }
       
    60 
       
    61 int16
       
    62 ReadSBRExtensionData(TBitStream *bs, SbrBitStream *streamSBR, 
       
    63                      int16 extension_type, int16 prev_element, 
       
    64                      int16 dataCount)
       
    65 {
       
    66   int16 i, sbrPresent;
       
    67 
       
    68   sbrPresent = 0;
       
    69   if(!(prev_element == SBR_ID_SCE || prev_element == SBR_ID_CPE))
       
    70     return (sbrPresent);
       
    71 
       
    72   if(!(extension_type == SBR_EXTENSION || extension_type == SBR_EXTENSION_CRC))
       
    73     return (sbrPresent);
       
    74 
       
    75   if(dataCount < MAX_SBR_BYTES && streamSBR->NrElements < MAX_NR_ELEMENTS)
       
    76   {
       
    77     streamSBR->sbrElement[streamSBR->NrElements].Data[0] = (uint8) BsGetBits(bs, 4);
       
    78 
       
    79     for(i = 1; i < dataCount; i++)
       
    80       streamSBR->sbrElement[streamSBR->NrElements].Data[i] = (uint8) BsGetBits(bs, 8);
       
    81 
       
    82     streamSBR->sbrElement[streamSBR->NrElements].ExtensionType = extension_type;
       
    83     streamSBR->sbrElement[streamSBR->NrElements].Payload = dataCount;
       
    84     streamSBR->NrElements += 1;
       
    85     sbrPresent = 1;
       
    86   }
       
    87 
       
    88   return (sbrPresent);
       
    89 }
       
    90 
       
    91 SbrBitStream *
       
    92 CloseSBRBitStream(SbrBitStream *Bitstr)
       
    93 {
       
    94   if(Bitstr)
       
    95   {
       
    96     int16 i;
       
    97 
       
    98     for(i = 0; i < MAX_NR_ELEMENTS; i++)
       
    99     {
       
   100       if(Bitstr->sbrElement[i].Data != 0)
       
   101         delete[] Bitstr->sbrElement[i].Data;
       
   102       Bitstr->sbrElement[i].Data = NULL;
       
   103     }
       
   104 
       
   105     
       
   106     delete Bitstr;
       
   107     Bitstr = NULL;
       
   108   }
       
   109 
       
   110   return (NULL);
       
   111 }
       
   112 
       
   113 SbrBitStream *
       
   114 OpenSBRBitStreamL(void)
       
   115 {
       
   116   int16 i;
       
   117   SbrBitStream *Bitstr;
       
   118 
       
   119   /*-- Create SBR bitstream handle. --*/
       
   120   Bitstr = (SbrBitStream *) new (ELeave) SbrBitStream[1];
       
   121   CleanupStack::PushL(Bitstr);
       
   122 
       
   123   ZERO_MEMORY(Bitstr, sizeof(SbrBitStream));
       
   124 
       
   125   /*-- Create payload handle for each supported element. --*/
       
   126   for(i = 0; i < MAX_NR_ELEMENTS; i++)
       
   127   {
       
   128     Bitstr->sbrElement[i].Data = (uint8 *) new (ELeave) uint8[MAX_SBR_BYTES];
       
   129     CleanupStack::PushL(Bitstr->sbrElement[i].Data);
       
   130 
       
   131     ZERO_MEMORY(Bitstr->sbrElement[i].Data, MAX_SBR_BYTES);
       
   132   }
       
   133 
       
   134   CleanupStack::Pop(MAX_NR_ELEMENTS + 1); /*-- 'Bitstr->sbrElement[i].Data' + 'Bitstr' --*/
       
   135 
       
   136   return (Bitstr);
       
   137 }
       
   138 
       
   139 /*!
       
   140   \brief     Set up SBR decoder
       
   141 
       
   142   \return    Handle
       
   143 */
       
   144 SBR_Decoder *
       
   145 OpenSBRDecoderL(int32 sampleRate, int16 samplesPerFrame, uint8 isStereo, uint8 isDualMono)
       
   146 {
       
   147   uint8 nPops;
       
   148   SBR_Decoder *sbrDecoder;
       
   149 
       
   150   /*-- Create main handle. --*/
       
   151   sbrDecoder = (SBR_Decoder *) new (ELeave) SBR_Decoder[1];
       
   152   CleanupStack::PushL(sbrDecoder); nPops = 1;
       
   153 
       
   154   ZERO_MEMORY(sbrDecoder, sizeof(SBR_Decoder));
       
   155 
       
   156   /*-- Create frame data for mono or left channel. --*/
       
   157   sbrDecoder->frameData[0] = (SbrFrameData *) new (ELeave) SbrFrameData[1];
       
   158   CleanupStack::PushL(sbrDecoder->frameData[0]); nPops++;
       
   159 
       
   160   ZERO_MEMORY(sbrDecoder->frameData[0], sizeof(SbrFrameData));
       
   161 
       
   162   /*-- Create frame data for right channel. --*/
       
   163   if(isStereo)
       
   164   {
       
   165     sbrDecoder->frameData[1] = (SbrFrameData *) new (ELeave) SbrFrameData[1];
       
   166     CleanupStack::PushL(sbrDecoder->frameData[1]); nPops++;
       
   167 
       
   168     ZERO_MEMORY(sbrDecoder->frameData[1], sizeof(SbrFrameData));
       
   169   }
       
   170 
       
   171   /*-- Create header data. --*/
       
   172   sbrDecoder->sbrHeader[0] = (SbrHeaderData *) new (ELeave) SbrHeaderData[1];
       
   173   CleanupStack::PushL(sbrDecoder->sbrHeader[0]); nPops++;
       
   174 
       
   175   ZERO_MEMORY(sbrDecoder->sbrHeader[0], sizeof(SbrHeaderData));
       
   176 
       
   177   /*
       
   178    * Create header data for dual channel if so needed. Remember that in
       
   179    * dual channel mode individual channels are not jointly coded,
       
   180    * each channel element is coded separately. Thus, also header data 
       
   181    * can change between frames of the individual channels.
       
   182    */
       
   183   if(isDualMono)
       
   184   {
       
   185     sbrDecoder->sbrHeader[1] = (SbrHeaderData *) new (ELeave) SbrHeaderData[1];
       
   186     CleanupStack::PushL(sbrDecoder->sbrHeader[1]); nPops++;
       
   187 
       
   188     ZERO_MEMORY(sbrDecoder->sbrHeader[1], sizeof(SbrHeaderData));
       
   189   }
       
   190 
       
   191   /*-- Create frequency band tables for mono or left channel. --*/
       
   192   sbrDecoder->freqBandData[0] = (FreqBandData *) new (ELeave) FreqBandData[1];
       
   193   CleanupStack::PushL(sbrDecoder->freqBandData[0]); nPops++;
       
   194 
       
   195   ZERO_MEMORY(sbrDecoder->freqBandData[0], sizeof(FreqBandData));
       
   196 
       
   197   /*-- Create frequency band data for dual channel. --*/
       
   198   if(isDualMono)
       
   199   {
       
   200     sbrDecoder->freqBandData[1] = (FreqBandData *) new (ELeave) FreqBandData[1];
       
   201     CleanupStack::PushL(sbrDecoder->freqBandData[1]); nPops++;
       
   202 
       
   203     ZERO_MEMORY(sbrDecoder->sbrHeader[1], sizeof(FreqBandData));
       
   204   }
       
   205 
       
   206   /*-- Create extension data handle. --*/
       
   207   sbrDecoder->sbrExtData = (SbrExtensionData *) new (ELeave) SbrExtensionData[1];
       
   208   CleanupStack::PushL(sbrDecoder->sbrExtData); nPops++;
       
   209 
       
   210   ZERO_MEMORY(sbrDecoder->sbrExtData, sizeof(SbrExtensionData));
       
   211 
       
   212   /*
       
   213    * Create data buffer for extension data. Data for parametric stereo 
       
   214    * will be stored here. Can exist only in mono mode.
       
   215    */
       
   216   if(!isStereo && !isDualMono)
       
   217   {
       
   218     sbrDecoder->sbrExtData->extDataBufLen = 128;
       
   219     sbrDecoder->sbrExtData->extensioData = (uint8 *) new (ELeave) uint8[sbrDecoder->sbrExtData->extDataBufLen];
       
   220     CleanupStack::PushL(sbrDecoder->sbrExtData->extensioData); nPops++;
       
   221 
       
   222     ZERO_MEMORY(sbrDecoder->sbrExtData->extensioData, sbrDecoder->sbrExtData->extDataBufLen);
       
   223   }
       
   224 
       
   225   /*-- Initialize header(s) with default values. --*/
       
   226   initHeaderData(sbrDecoder->sbrHeader[0], sbrDecoder->freqBandData[0], sampleRate, samplesPerFrame);
       
   227   if(isDualMono)
       
   228     initHeaderData(sbrDecoder->sbrHeader[1], sbrDecoder->freqBandData[1], sampleRate, samplesPerFrame);
       
   229 
       
   230   CleanupStack::Pop(nPops);
       
   231 
       
   232   return (sbrDecoder);
       
   233 }
       
   234 
       
   235 /*!
       
   236   \brief     Close SBR decoder resources
       
   237 
       
   238   \return    NULL
       
   239 */
       
   240 
       
   241 SBR_Decoder *
       
   242 CloseSBR(SBR_Decoder *sbrDecoder)
       
   243 {
       
   244   if(sbrDecoder)
       
   245   {
       
   246     if(sbrDecoder->frameData[0] != 0)
       
   247       delete sbrDecoder->frameData[0];
       
   248     sbrDecoder->frameData[0] = NULL;
       
   249 
       
   250     if(sbrDecoder->frameData[1] != 0)
       
   251       delete sbrDecoder->frameData[1];
       
   252     sbrDecoder->frameData[1] = NULL;
       
   253 
       
   254     if(sbrDecoder->sbrHeader[0] != 0)
       
   255       delete sbrDecoder->sbrHeader[0];
       
   256     sbrDecoder->sbrHeader[0] = NULL;
       
   257 
       
   258     if(sbrDecoder->sbrHeader[1] != 0)
       
   259       delete sbrDecoder->sbrHeader[1];
       
   260     sbrDecoder->sbrHeader[1] = NULL;
       
   261 
       
   262     if(sbrDecoder->freqBandData[0] != 0)
       
   263       delete sbrDecoder->freqBandData[0];
       
   264     sbrDecoder->freqBandData[0] = NULL;
       
   265 
       
   266     if(sbrDecoder->freqBandData[1] != 0)
       
   267       delete sbrDecoder->freqBandData[1];
       
   268     sbrDecoder->freqBandData[1] = NULL;
       
   269 
       
   270     if(sbrDecoder->sbrExtData != 0)
       
   271     {      
       
   272       if(sbrDecoder->sbrExtData->extensioData != 0)
       
   273         delete[] sbrDecoder->sbrExtData->extensioData;
       
   274       sbrDecoder->sbrExtData->extensioData = NULL;
       
   275      
       
   276       delete sbrDecoder->sbrExtData;
       
   277     }
       
   278     sbrDecoder->sbrExtData = NULL;
       
   279 
       
   280     delete sbrDecoder;
       
   281     sbrDecoder = NULL;
       
   282   }
       
   283 
       
   284   return (NULL);
       
   285 }
       
   286 
       
   287 int32
       
   288 SBR_WritePayload(TBitStream *bs, SbrFrameData *frameData[2], 
       
   289                  SbrHeaderData *hHeaderData, SbrExtensionData *sbrExtData,
       
   290                  uint8 headerStatus, uint8 isStereo, uint8 writeFlag)
       
   291 {
       
   292   int32 bitsWritten;
       
   293 
       
   294   /*-- Write header flag. --*/
       
   295   bitsWritten = 1;
       
   296   if(writeFlag) BsPutBits(bs, 1, headerStatus);
       
   297 
       
   298   /*-- Write header data. --*/
       
   299   if(headerStatus)
       
   300     bitsWritten += SBR_WriteHeaderData(hHeaderData, bs, writeFlag);
       
   301 
       
   302   /*-- Write payload data. --*/
       
   303   if(hHeaderData->syncState == SBR_ACTIVE)
       
   304   {
       
   305     if(isStereo)
       
   306       bitsWritten += SBR_WriteCPE(hHeaderData, frameData[0], frameData[1], sbrExtData, bs, writeFlag);
       
   307     else
       
   308       bitsWritten += SBR_WriteSCE(hHeaderData, frameData[0], sbrExtData, bs, 1, writeFlag);
       
   309   }
       
   310 
       
   311   return (bitsWritten);
       
   312 }
       
   313 
       
   314 INLINE int32
       
   315 WriteSBR(TBitStream *bs, SbrFrameData *frameData[2], SbrHeaderData *hHeaderData, 
       
   316          SbrExtensionData *sbrExtData, uint8 isStereo, uint8 headerPresent, 
       
   317          uint8 writeFlag)
       
   318 {
       
   319   int32 bitsWritten;
       
   320 
       
   321   bitsWritten = 0;
       
   322 
       
   323   /*-- Write extension tag. --*/
       
   324   bitsWritten += 4;
       
   325   if(writeFlag) BsPutBits(bs, 4, SBR_EXTENSION);
       
   326 
       
   327   /*-- Write actual SBR payload. --*/
       
   328   bitsWritten += SBR_WritePayload(bs, frameData, hHeaderData, sbrExtData, headerPresent, isStereo, writeFlag);
       
   329 
       
   330   /*-- Byte align. --*/
       
   331   if(bitsWritten & 0x7)
       
   332   {
       
   333     uint8 bitsLeft = (uint8) (8 - (bitsWritten & 0x7));
       
   334 
       
   335     bitsWritten += bitsLeft;      
       
   336     if(writeFlag) BsPutBits(bs, bitsLeft, 0);
       
   337   }
       
   338 
       
   339   return (bitsWritten);
       
   340 }
       
   341 
       
   342 /*
       
   343  * Writes dummy payload data.
       
   344  */
       
   345 INLINE int32 
       
   346 WriteDummyPayload(TBitStream *bs, int32 nDummyBytes)
       
   347 {
       
   348   int32 i, nBitsWritten;
       
   349 
       
   350   nBitsWritten = 4;
       
   351   BsPutBits(bs, 4, 0);
       
   352 
       
   353   nBitsWritten += 4;
       
   354   BsPutBits(bs, 4, 0);
       
   355   for(i = 0; i < (nDummyBytes - 1); i++)
       
   356   {
       
   357     nBitsWritten += 8;
       
   358     BsPutBits(bs, 8, 0xA5);
       
   359   }
       
   360 
       
   361   return (nBitsWritten);
       
   362 }
       
   363 
       
   364 /*
       
   365  * Writes the length of AAC Fill Element (FIL) as specified in the standard.
       
   366  */
       
   367 INLINE int32 
       
   368 WriteAACFilLength(TBitStream *bs, int32 nFilBytes)
       
   369 {
       
   370   int32 cnt, nBitsWritten;
       
   371 
       
   372   nBitsWritten = 0;
       
   373   cnt = (nFilBytes >= 15) ? 15 : nFilBytes;
       
   374 
       
   375   nBitsWritten += 4;
       
   376   BsPutBits(bs, 4, cnt);
       
   377   if(cnt == 15)
       
   378   {
       
   379     int32 diff;
       
   380 
       
   381     diff = nFilBytes - 15 + 1;
       
   382 
       
   383     nBitsWritten += 8;
       
   384     BsPutBits(bs, 8, diff);
       
   385   }
       
   386 
       
   387   return (nBitsWritten);
       
   388 }
       
   389 
       
   390 int32
       
   391 WriteSBRExtensionData2(TBitStream *bsOut, SbrFrameData *frameData[2], 
       
   392                        SbrHeaderData *hHeaderData, SbrExtensionData *sbrExtData, 
       
   393                        uint8 isStereo, uint8 headerPresent)
       
   394 {
       
   395   int32 bitsWritten, nFilBytes;
       
   396 
       
   397   /*-- Write fill element code. --*/
       
   398   BsPutBits(bsOut, 3, 0x6);
       
   399 
       
   400   /*-- Count SBR part. --*/
       
   401   bitsWritten = WriteSBR(NULL, frameData, hHeaderData, sbrExtData, isStereo, headerPresent, 0);
       
   402   nFilBytes = bitsWritten >> 3;
       
   403 
       
   404   /*-- Write length of FIL element. --*/
       
   405   WriteAACFilLength(bsOut, nFilBytes);
       
   406 
       
   407   /*-- Write SBR data. --*/
       
   408   if(nFilBytes > 0)
       
   409   {
       
   410     bitsWritten = WriteSBR(bsOut, frameData, hHeaderData, sbrExtData, isStereo, headerPresent, 1);
       
   411     nFilBytes -= bitsWritten >> 3;
       
   412   }
       
   413 
       
   414   /*-- Write dummy data if needed. --*/
       
   415   if(nFilBytes > 0)
       
   416   {
       
   417     /*-- Write fill element code. --*/
       
   418     BsPutBits(bsOut, 3, 0x6);
       
   419 
       
   420     /*-- Write length of FIL element. --*/
       
   421     bitsWritten += WriteAACFilLength(bsOut, nFilBytes);
       
   422 
       
   423     WriteDummyPayload(bsOut, nFilBytes);
       
   424   }
       
   425 
       
   426   return (BsGetBitsRead(bsOut));
       
   427 }
       
   428 
       
   429 int32
       
   430 WriteSBRExtensionData(TBitStream *bsIn, TBitStream *bsOut, int16 bitOffset,
       
   431                       SBR_Decoder *sbrDecoder, 
       
   432                       SbrHeaderData *hHeaderData,
       
   433                       uint8 isStereo, 
       
   434                       uint8 headerPresent)
       
   435 {
       
   436   int32 bitsWritten; 
       
   437 
       
   438   /*-- Locate start position for SBR data within the buffer. --*/
       
   439   BsCopyBits(bsIn, bsOut, bitOffset);
       
   440 
       
   441   bitsWritten = BsGetBitsRead(bsOut);
       
   442 
       
   443   WriteSBRExtensionData2(bsOut, sbrDecoder->frameData, hHeaderData, sbrDecoder->sbrExtData, isStereo, headerPresent);
       
   444 
       
   445   bitsWritten = BsGetBitsRead(bsOut) - bitsWritten;
       
   446 
       
   447   return (bitsWritten);
       
   448 }
       
   449 
       
   450 int16
       
   451 WriteSBRSilenceElement(SBR_Decoder *sbrDecoder, TBitStream *bsOut, uint8 isStereo)
       
   452 {
       
   453   int32 bitsWritten;
       
   454 
       
   455   bitsWritten = BsGetBitsRead(bsOut);
       
   456 
       
   457   WriteSBRExtensionData2(bsOut, sbrDecoder->frameData, sbrDecoder->sbrHeader[0], sbrDecoder->sbrExtData, isStereo, 1);
       
   458 
       
   459   bitsWritten = BsGetBitsRead(bsOut) - bitsWritten;
       
   460 
       
   461   return (bitsWritten);
       
   462 }
       
   463 
       
   464 void
       
   465 InitSBRSilenceData(SBR_Decoder *sbrDecoder, uint8 isStereo, uint8 isParametricStereo)
       
   466 {
       
   467   sbrDecoder->sbrHeader[0]->startFreq = 15;
       
   468   sbrDecoder->sbrHeader[0]->stopFreq = 14;
       
   469 
       
   470   sbrDecoder->frameData[0]->frameInfo.nEnvelopes = 1;
       
   471   sbrDecoder->frameData[0]->frameInfo.nNoiseEnvelopes = 1;
       
   472   if(isStereo)
       
   473   {
       
   474     sbrDecoder->frameData[1]->frameInfo.nEnvelopes = 1;
       
   475     sbrDecoder->frameData[1]->frameInfo.nNoiseEnvelopes = 1;
       
   476   }
       
   477 
       
   478   sbrDecoder->sbrExtData->writePsData = (uint8) ((isParametricStereo) ? 1 : 0);
       
   479 
       
   480   resetFreqBandTables(sbrDecoder->sbrHeader[0]);
       
   481   sbrDecoder->sbrHeader[0]->syncState = SBR_ACTIVE;
       
   482 }
       
   483 
       
   484 int16
       
   485 GenerateSBRSilenceDataL(uint8 *OutBuffer, int16 OutBufferSize, int32 sampleRate, 
       
   486                         uint8 isStereo, uint8 isParametricStereo)
       
   487 {
       
   488   int16 sbrBits;
       
   489   TBitStream bsOut;
       
   490   SBR_Decoder *sbrDecoder;
       
   491 
       
   492   sbrDecoder = OpenSBRDecoderL(sampleRate, 1024, isStereo, 0);
       
   493 
       
   494   InitSBRSilenceData(sbrDecoder, isStereo, isParametricStereo);
       
   495 
       
   496   BsInit(&bsOut, OutBuffer, OutBufferSize);
       
   497 
       
   498   sbrBits = (int16) WriteSBRExtensionData2(&bsOut, sbrDecoder->frameData, sbrDecoder->sbrHeader[0], sbrDecoder->sbrExtData, isStereo, 1);
       
   499 
       
   500   return (sbrBits);
       
   501 }
       
   502 
       
   503 int16
       
   504 WriteSBRExtensionSilenceData(TBitStream *bsOut, uint8 *SbrBuffer, 
       
   505                              int16 SbrBits, uint8 writeTerminationCode)
       
   506 {
       
   507   TBitStream bsSbr;
       
   508   int16 bitsWritten;
       
   509 
       
   510   /*-- Initialize bitstream parser. --*/
       
   511   BsInit(&bsSbr, SbrBuffer, (SbrBits + 8) >> 3);
       
   512 
       
   513   /*-- Write the silence data. --*/
       
   514   BsCopyBits(&bsSbr, bsOut, SbrBits);
       
   515 
       
   516   /*-- Write termination code. --*/
       
   517   if(writeTerminationCode)
       
   518     BsPutBits(bsOut, 3, 0x7);
       
   519 
       
   520   bitsWritten = BsGetBitsRead(bsOut);
       
   521 
       
   522   if(writeTerminationCode)
       
   523   {
       
   524     /*-- Byte align. --*/
       
   525     if(bitsWritten & 0x7)
       
   526     {
       
   527       int16 bitsLeft = 8 - (bitsWritten & 0x7);
       
   528 
       
   529       bitsWritten += bitsLeft;
       
   530       BsPutBits(bsOut, bitsLeft, 0);
       
   531     }
       
   532   }
       
   533 
       
   534   return (bitsWritten);
       
   535 }
       
   536 
       
   537 uint8
       
   538 ParseSBRPayload(SBR_Decoder *self, SbrHeaderData *hHeaderData, 
       
   539                 SbrElementStream *sbrElement, int16 decVal,
       
   540                 uint8 isStereo)
       
   541 {
       
   542   TBitStream bs;
       
   543 
       
   544   /*-- Initialize bitstream. --*/
       
   545   BsInit(&bs, sbrElement->Data, sbrElement->Payload);
       
   546 
       
   547   /*-- Remove invalid data from bit buffer. --*/
       
   548   BsGetBits(&bs, 4);
       
   549 
       
   550   /*-- CRC codeword present? --*/
       
   551   if(sbrElement->ExtensionType == SBR_EXTENSION_CRC)
       
   552     BsGetBits(&bs, 10);
       
   553 
       
   554   /*-- Header present? --*/
       
   555   sbrElement->headerStatus = (uint8) BsGetBits(&bs, 1);
       
   556 
       
   557   /*-- Read header data. --*/
       
   558   if(sbrElement->headerStatus) 
       
   559   {
       
   560     SBR_HEADER_STATUS headerStatus;
       
   561     
       
   562     headerStatus = sbrGetHeaderData(hHeaderData, &bs);
       
   563     if(headerStatus == HEADER_RESET) 
       
   564     {
       
   565       int16 err;
       
   566 
       
   567       /*-- Reset values. --*/
       
   568       err = resetFreqBandTables(hHeaderData);
       
   569       if(err == 0) hHeaderData->syncState = SBR_ACTIVE;
       
   570     }
       
   571   }
       
   572 
       
   573   /*-- Read payload data. --*/
       
   574   if(hHeaderData->syncState == SBR_ACTIVE) 
       
   575   {
       
   576     /*-- Read channel pair element related data. --*/
       
   577     if(isStereo)
       
   578       sbrGetCPE(hHeaderData, self->frameData[0], self->frameData[1], self->sbrExtData, &bs, decVal);
       
   579 
       
   580     /*-- Read mono data. --*/
       
   581     else
       
   582       sbrGetSCE(hHeaderData, self->frameData[0], self->sbrExtData, &bs, decVal, 1);
       
   583   }
       
   584 
       
   585   return (1);
       
   586 }
       
   587 
       
   588 /*!
       
   589   \brief     SBR bitstream parsing
       
   590 
       
   591   \return    Number of bytes written to output bitstream
       
   592 */
       
   593 int16
       
   594 ParseSBR(TBitStream *bsIn, TBitStream *bsOut, SBR_Decoder *self, 
       
   595          SbrBitStream *Bitstr, int16 decVal)
       
   596 {
       
   597   int32 i, bitsWritten = BsGetBitsRead(bsOut);
       
   598 
       
   599   /*
       
   600    * Write frame bits from the start of frame till 
       
   601    * the start of 1st channel element. 
       
   602    */
       
   603   BsCopyBits(bsIn, bsOut, Bitstr->sbrElement[0].elementOffset);
       
   604 
       
   605   if(Bitstr->NrElements) 
       
   606   {
       
   607     int16 SbrFrameOK, dualMono;
       
   608 
       
   609     SbrFrameOK = 1;
       
   610     dualMono = (Bitstr->NrElements == 2) ? 1 : 0;
       
   611 
       
   612     for(i = 0; i < Bitstr->NrElements; i++) 
       
   613     {
       
   614       uint8 stereo;
       
   615       SbrElementStream *sbrElement;
       
   616 
       
   617       sbrElement = &Bitstr->sbrElement[i];
       
   618       
       
   619       if(sbrElement->Payload < 1)
       
   620         continue;
       
   621 
       
   622       stereo = 0;
       
   623 
       
   624       switch(sbrElement->ElementID) 
       
   625       {
       
   626         case SBR_ID_SCE:
       
   627           stereo = 0;
       
   628           break;
       
   629 
       
   630         case SBR_ID_CPE:
       
   631           stereo = 1;
       
   632           break;
       
   633 
       
   634         default:
       
   635           SbrFrameOK = 0;
       
   636           break;
       
   637       }
       
   638 
       
   639       if(SbrFrameOK) 
       
   640       {
       
   641         ParseSBRPayload(self, self->sbrHeader[dualMono ? i : 0], sbrElement, decVal, stereo);
       
   642 
       
   643         WriteSBRExtensionData(bsIn, bsOut, sbrElement->chElementLen, self,
       
   644                               self->sbrHeader[dualMono ? i : 0],
       
   645                               stereo, sbrElement->headerStatus);
       
   646 
       
   647         if(i < (Bitstr->NrElements - 1))
       
   648         {
       
   649           int32 endElement, startNextElement;
       
   650 
       
   651           endElement = sbrElement->elementOffset + sbrElement->chElementLen;
       
   652           startNextElement = Bitstr->sbrElement[i + 1].elementOffset;
       
   653 
       
   654           BsSkipNBits(bsIn, startNextElement - endElement);
       
   655         }
       
   656       }
       
   657     }
       
   658 
       
   659     /*-- Write termination code. --*/
       
   660     BsPutBits(bsOut, 3, 0x7);
       
   661 
       
   662     bitsWritten = BsGetBitsRead(bsOut) - bitsWritten;
       
   663 
       
   664     /*-- Byte align. --*/
       
   665     if(bitsWritten & 0x7)
       
   666     {
       
   667       uint8 bitsLeft = (uint8) (8 - (bitsWritten & 0x7));
       
   668       
       
   669       bitsWritten += bitsLeft;
       
   670       BsPutBits(bsOut, bitsLeft, 0);
       
   671     }
       
   672   }
       
   673 
       
   674   return (int16) (bitsWritten >> 3);
       
   675 }
       
   676 
       
   677 uint8
       
   678 IsSBRParametricStereoEnabled(SBR_Decoder *self, SbrBitStream *Bitstr)
       
   679 {
       
   680   uint8 isParamStereoPresent;
       
   681 
       
   682   isParamStereoPresent = 0;
       
   683 
       
   684   if(self && Bitstr->NrElements == 1) 
       
   685   {
       
   686     SbrElementStream *sbrElement;
       
   687 
       
   688     sbrElement = &Bitstr->sbrElement[0];
       
   689 
       
   690     if(sbrElement->Payload > 0 && sbrElement->ElementID == SBR_ID_SCE)
       
   691     {
       
   692       ParseSBRPayload(self, self->sbrHeader[0], sbrElement, 0, 0);
       
   693 
       
   694       if(self->sbrExtData->extensionDataPresent)
       
   695         if(self->sbrExtData->extension_id == SBR_PARAMETRIC_STEREO_ID)
       
   696           isParamStereoPresent = 1;
       
   697     }
       
   698   }
       
   699 
       
   700   return (isParamStereoPresent);
       
   701 }
       
   702 
       
   703 uint8
       
   704 IsSBREnabled(SbrBitStream *Bitstr)
       
   705 {
       
   706   uint8 isSBR;
       
   707 
       
   708   isSBR = (Bitstr && Bitstr->NrElements) ? 1 : 0;
       
   709 
       
   710   return (isSBR);
       
   711 }
       
   712 
       
   713 int16
       
   714 WriteSBRSilence(TBitStream *bsIn, TBitStream *bsOut, SbrBitStream *streamSBR,
       
   715                 uint8 *SbrBuffer, int16 SbrBits)
       
   716 {
       
   717   int32 i;
       
   718 
       
   719   /*
       
   720    * Write frame bits from the start of frame till 
       
   721    * the start of 1st channel element. 
       
   722    */
       
   723   BsCopyBits(bsIn, bsOut, streamSBR->sbrElement[0].elementOffset);
       
   724 
       
   725   /*-- Write channel element. --*/
       
   726   BsCopyBits(bsIn, bsOut, streamSBR->sbrElement[0].chElementLen);
       
   727 
       
   728   for(i = 0; i < streamSBR->NrElements; i++)
       
   729   {
       
   730     uint8 writeEndCode = (i == (streamSBR->NrElements - 1)) ? 1 : 0;
       
   731 
       
   732     WriteSBRExtensionSilenceData(bsOut, SbrBuffer, SbrBits, writeEndCode);
       
   733 
       
   734     if(i < (streamSBR->NrElements - 1))
       
   735     {
       
   736       int32 endElement, startNextElement;
       
   737       
       
   738       endElement = streamSBR->sbrElement[i].elementOffset + streamSBR->sbrElement[i].chElementLen;
       
   739       startNextElement = streamSBR->sbrElement[i + 1].elementOffset;
       
   740 
       
   741       BsSkipNBits(bsIn, startNextElement - endElement);
       
   742 
       
   743       BsCopyBits(bsIn, bsOut, streamSBR->sbrElement[i + 1].chElementLen);
       
   744     }
       
   745   }
       
   746 
       
   747   return (int16) (BsGetBitsRead(bsOut) >> 3);
       
   748 }