videoeditorengine/mp3aacManipLib/AACGain/src/huffdec1.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   huffdec1.cpp - Bit parsing routines for decoding LFE, SCE and CPE audio elements.
       
    22  
       
    23   Author(s): Juha Ojanpera
       
    24   Copyright (c) 2000-2004 by Nokia Research Center, Speech and Audio Systems.
       
    25   *************************************************************************/
       
    26 
       
    27 /*-- Project Headers. --*/
       
    28 #include "tool.h"
       
    29 #include "tool2.h"
       
    30 
       
    31 #define SCE_START_BITS (LEN_TAG + LEN_SCL_PCM)
       
    32 #define CPE_START_BITS (LEN_TAG + LEN_COM_WIN)
       
    33 
       
    34 /**************************************************************************
       
    35   Title:        getmask
       
    36 
       
    37   Purpose:      Reads MS mask and MS flag bits from the bitstream.
       
    38 
       
    39   Usage:        y = getmask(bs, info, group, max_sfb, mask)
       
    40 
       
    41   Input:        bs      - bitstream parameters
       
    42                 info    - block (long/short) parameters
       
    43                 group   - grouping info for short blocks
       
    44                 max_sfb - # of sfb's present in the channel pair
       
    45 
       
    46   Output:       y       - value of MS mask in the bitstream
       
    47                 mask    - flag bit for each sfb (only if y == 1)
       
    48 
       
    49   Author(s):    Juha Ojanpera
       
    50   *************************************************************************/
       
    51 
       
    52 INLINE int16
       
    53 getmask(TBitStream *bs, CInfo *info, uint8 *group, uint8 max_sfb, uint8 *mask)
       
    54 {
       
    55   int16 b, i, mp;
       
    56   
       
    57   mp = (int16) BsGetBits(bs, LEN_MASK_PRES);
       
    58   
       
    59   /*--Get mask. --*/
       
    60   if(mp == 1)
       
    61   {
       
    62     for(b = 0; b < info->nsbk; b = *group++)
       
    63     {
       
    64       int16 sfb;
       
    65       uint32 tmp, flag;
       
    66 
       
    67 #if 1
       
    68       /*
       
    69        * Speed up the reading of the flags.
       
    70        */
       
    71       sfb = max_sfb;
       
    72       if(sfb > 32) sfb = 32;
       
    73       flag = 1 << (sfb - 1);
       
    74       tmp = BsGetBits(bs, sfb);
       
    75       
       
    76       for(i = 0; i < sfb; i++, mask++, flag >>= 1)
       
    77         *mask = (tmp & flag) ? (uint8) 1 : (uint8) 0;
       
    78 
       
    79       if(sfb == 32)
       
    80       {
       
    81         sfb = int16(max_sfb - 32);
       
    82         flag = 1 << (sfb - 1);
       
    83         tmp = BsGetBits(bs, sfb);
       
    84 
       
    85         for( ; i < max_sfb; i++, mask++, flag >>= 1)
       
    86           *mask = (tmp & flag) ? (uint8) 1 : (uint8) 0;
       
    87       }
       
    88 
       
    89 #else
       
    90       for(i = 0; i < max_sfb; i++, mask++)
       
    91         *mask = (uint8) BsGetBits(bs, LEN_MASK);
       
    92       mask += info->sfb_per_sbk[0] - i;
       
    93 #endif
       
    94     }
       
    95   }
       
    96   else if(mp == 2)
       
    97   {
       
    98     for(b = 0; b < info->nsbk; b = *group++)
       
    99     {
       
   100       SET_MEMORY(mask, max_sfb, 1);
       
   101       mask += info->sfb_per_sbk[0];
       
   102     }
       
   103   }
       
   104   else
       
   105   {
       
   106     for(b = 0; b < info->nsbk; b = *group++)
       
   107     {
       
   108       ZERO_MEMORY(mask, max_sfb);
       
   109       mask += info->sfb_per_sbk[0];
       
   110     }
       
   111   }
       
   112   
       
   113   return (mp);
       
   114 }
       
   115 
       
   116 /**************************************************************************
       
   117   Object Definitions
       
   118   *************************************************************************/
       
   119 
       
   120 /**************************************************************************
       
   121   Title:        GetSCE
       
   122 
       
   123   Purpose:      Decodes LFE/SCE audio element from the bitstream.
       
   124 
       
   125   Usage:        y = GetSCE(aac, bs, mip, gains, gainPos, bufBitOffset)
       
   126 
       
   127   Input:        bs           - bitstream parameters
       
   128                 bufBitOffset - # of bits read read so far
       
   129 
       
   130   Output:       y            - 0 on success, -1 otherwise
       
   131                 mip
       
   132                   ch_info    - channel mapping parameters
       
   133                 aac
       
   134                   winInfo    - block/window parameters for each channel
       
   135                   toolInfo
       
   136                     quant    - quantized spectral coefficients
       
   137                 gains        - 'global_gain' bitstream element
       
   138                 gainPos      - location of 'global_gain' within the bitstream
       
   139 
       
   140   Author(s):    Juha Ojanpera
       
   141   *************************************************************************/
       
   142 
       
   143 int16
       
   144 GetSCE(CAACAudDec *aac, TBitStream *bs, CMC_Info *mip, uint8 *gains, 
       
   145        uint32 *gainPos, uint32 bufBitOffset)
       
   146 {
       
   147   int16 ch, global_gain;
       
   148   CWindowInfo **winInfo;
       
   149   CToolInfo **toolInfo;
       
   150   CWindowInfo *win;
       
   151   CToolInfo *tool;
       
   152   TCh_Info *cip;
       
   153   uint32 tmp;
       
   154 
       
   155   toolInfo = aac->tool;
       
   156   winInfo = aac->winInfo;
       
   157 
       
   158   tmp = BsGetBits(bs, SCE_START_BITS);
       
   159   
       
   160   ch = ChIndex(1, (int16) (tmp >> LEN_SCL_PCM), 0, mip);
       
   161   cip = &mip->ch_info[ch];
       
   162   tool = toolInfo[ch];
       
   163   win = winInfo[ch];
       
   164 
       
   165   /*-- Global gain. --*/
       
   166   global_gain = (int16) (tmp & 0xFF);
       
   167 
       
   168   /*-- Save global gain element and its position. --*/
       
   169   if(gainPos)
       
   170   {
       
   171     gains[0] = (uint8) global_gain;
       
   172     gainPos[0] = BsGetBitsRead(bs) - bufBitOffset - LEN_SCL_PCM;
       
   173   }
       
   174 
       
   175   if(!GetICSInfo(bs, win, tool->ltp, NULL)) return (-1);
       
   176   
       
   177   cip->info = mip->sfbInfo->winmap[win->wnd];
       
   178 
       
   179   win->hasmask = 0;
       
   180   tool = toolInfo[ch];
       
   181 
       
   182   if(!GetICS(bs, cip, win->group, win->max_sfb, win->cb_map,
       
   183              tool->quant, global_gain, win->sfac))
       
   184     return (-1);
       
   185 
       
   186   return (0);
       
   187 }
       
   188 
       
   189 /**************************************************************************
       
   190   Title:        GetCPE
       
   191 
       
   192   Purpose:      Decodes CPE audio element from the bitstream.
       
   193 
       
   194   Usage:        y = GetCPE(aac, bs, mip, gains, gainPos, bufBitOffset
       
   195 
       
   196   Input:        bs           - bitstream parameters
       
   197                 bufBitOffset - # of bits read read so far
       
   198 
       
   199   Output:       y            - 0 on success, -1 otherwise
       
   200                 mip
       
   201                   ch_info    - channel mapping parameters
       
   202                 aac
       
   203                   winInfo    - block/window parameters for each channel
       
   204                   toolInfo
       
   205                     quant    - quantized spectral coefficients
       
   206                 gains        - 'global_gain' bitstream element
       
   207                 gainPos      - location of 'global_gain' within the bitstream
       
   208 
       
   209   Author(s):    Juha Ojanpera
       
   210   *************************************************************************/
       
   211 
       
   212 int16
       
   213 GetCPE(CAACAudDec *aac, TBitStream *bs, CMC_Info *mip, uint8 *gains, 
       
   214        uint32 *gainPos, uint32 bufBitOffset)
       
   215 {
       
   216   int16 i, common_window, ch;
       
   217   CWindowInfo **winInfo;
       
   218   CToolInfo **toolInfo;
       
   219   int16 global_gain;
       
   220   CWindowInfo *win;
       
   221   CToolInfo **tool;
       
   222   TCh_Info *cip;
       
   223 
       
   224   toolInfo = aac->tool;
       
   225   winInfo = aac->winInfo;
       
   226 
       
   227   uint16 tmp = (uint16) BsGetBits(bs, CPE_START_BITS);
       
   228   
       
   229   common_window = (int16) (tmp & 0x1);
       
   230   
       
   231   ch = ChIndex(2, (int16) (tmp >> 1), common_window, mip);
       
   232   
       
   233   if(common_window)
       
   234   {
       
   235     win = winInfo[ch];
       
   236     
       
   237     if(!GetICSInfo(bs, win, toolInfo[ch]->ltp, toolInfo[ch + 1]->ltp))
       
   238       return (-1);
       
   239     
       
   240     win->hasmask = (uint8) getmask(bs, mip->sfbInfo->winmap[win->wnd], win->group, win->max_sfb, win->mask);
       
   241   }
       
   242   else
       
   243     winInfo[ch]->hasmask = winInfo[ch + 1]->hasmask = 0;
       
   244 
       
   245   tool = toolInfo + ch;
       
   246   cip = &mip->ch_info[ch];
       
   247   for(i = ch; i < ch + 2; i++, cip++, tool++)
       
   248   {
       
   249     CWindowInfo *wi = winInfo[i];
       
   250 
       
   251     win = winInfo[cip->widx];
       
   252     
       
   253     /*-- Global gain. --*/
       
   254     global_gain = (int16) BsGetBits(bs, LEN_SCL_PCM);
       
   255 
       
   256     /*-- Save global gain element and its position. --*/
       
   257     if(gainPos)
       
   258     {
       
   259       gains[i - ch] = (uint8) global_gain;
       
   260       gainPos[i - ch] = BsGetBitsRead(bs) - bufBitOffset - LEN_SCL_PCM;
       
   261       bufBitOffset = BsGetBitsRead(bs);
       
   262     }
       
   263 
       
   264     if(!common_window)
       
   265       if(!GetICSInfo(bs, win, (*tool)->ltp, NULL))
       
   266         return (-1);
       
   267 
       
   268     cip->info = mip->sfbInfo->winmap[win->wnd];
       
   269 
       
   270     if(!GetICS(bs, cip, win->group, win->max_sfb, wi->cb_map, 
       
   271                (*tool)->quant, global_gain, wi->sfac))
       
   272       return (-1);
       
   273   }
       
   274   
       
   275   return (0);
       
   276 }
       
   277 
       
   278 /**************************************************************************
       
   279   Title       : LTP_Decode
       
   280 
       
   281   Purpose     :    Decodes the bitstream elements for LTP tool.
       
   282 
       
   283   Usage       : LTP_Decode(bs, ltp_info, max_sfb)
       
   284 
       
   285   Input       : bs       - input bitstream
       
   286                 max_sfb  - # scalefactor bands to be used for current frame
       
   287 
       
   288   Output      : ltp_info - LTP parameters for this channel
       
   289 
       
   290   Author(s)   : Juha Ojanpera
       
   291   *************************************************************************/
       
   292 
       
   293 INLINE void
       
   294 LTP_Decode(TBitStream *bs, CLTP_Info *ltp_info, int16 max_sfb)
       
   295 {
       
   296   /*
       
   297   Purpose:    1 bits is used to indicate the presence of LTP. */
       
   298 #define    LEN_LTP_DATA_PRESENT 1
       
   299 
       
   300 /*
       
   301   Purpose:    # of bits used for the pitch lag. */
       
   302 #define    LEN_LTP_LAG 11
       
   303 
       
   304 /*
       
   305   Purpose:    # of bits used for the gain value. */
       
   306 #define    LEN_LTP_COEF 3
       
   307 
       
   308   ltp_info->ltp_present = (uint8) BsGetBits(bs, LEN_LTP_DATA_PRESENT);
       
   309   if(ltp_info->ltp_present)
       
   310   {
       
   311     uint32 bits = BsGetBits(bs, LEN_LTP_LAG + LEN_LTP_COEF);
       
   312 
       
   313     /*-- LTP lag. --*/
       
   314     ltp_info->delay[0] = (int16) (bits >> LEN_LTP_COEF);
       
   315     
       
   316     /*-- LTP gain. --*/
       
   317     ltp_info->cbIdx = (uint8) (bits & 0x7);
       
   318 
       
   319     /*-- Sfb flags. --*/
       
   320     ltp_info->max_sfb = max_sfb;
       
   321     if(max_sfb < 33)
       
   322       ltp_info->sfbflags[0] = BsGetBits(bs, max_sfb);
       
   323     else
       
   324     {
       
   325       ltp_info->sfbflags[0] = BsGetBits(bs, 32);
       
   326       ltp_info->sfbflags[1] = BsGetBits(bs, (int16) (max_sfb - 32));
       
   327     }
       
   328   }
       
   329 }
       
   330 
       
   331 /**************************************************************************
       
   332   Title:        GetICSInfo
       
   333 
       
   334   Purpose:      Reads side information for individual channel element.
       
   335                 Individual channel elements within channel pair element may 
       
   336                 share this information.
       
   337 
       
   338   Usage:        y = GetICSInfo(bs, winInfo, ltp_left, ltp_right)
       
   339 
       
   340   Input:        bs         - bitstream parameters
       
   341 
       
   342   Output:       y          - # of sfb's present in this channel
       
   343                 winInfo :
       
   344                  wnd       - window type for this channel
       
   345                  wshape    - window shape for this channel
       
   346                  group     - grouping of short blocks
       
   347                  lpflag    - BWAP prediction flags for each sfb
       
   348                  prstflag  - reset flags for BWAP
       
   349                 ltp_left   - LTP parameters for left channel of the audio element
       
   350                 ltp_right  - LTP parameters for right channel of the audio element
       
   351 
       
   352   Author(s):    Juha Ojanpera
       
   353   *************************************************************************/
       
   354 
       
   355 int16
       
   356 GetICSInfo(TBitStream *bs, CWindowInfo *winInfo, CLTP_Info *ltp_left, CLTP_Info *ltp_right)
       
   357 {
       
   358 #define INFO_BITS (LEN_ICS_RESERV + LEN_WIN_SEQ + LEN_WIN_SH + LEN_MAX_SFBL + LEN_PRED_PRES)
       
   359   int16 i, j;
       
   360   uint32 tmp;
       
   361 
       
   362   tmp =  BsGetBits(bs, INFO_BITS);
       
   363   winInfo->wnd = (uint8) ((tmp >> 8) & 0x3);
       
   364   winInfo->wshape[0].this_bk = (uint8) ((tmp >> 7) & 0x1);
       
   365 
       
   366   /*-- Max scalefactor, scalefactor grouping and prediction flags. --*/
       
   367   winInfo->prstflag[0] = 0;
       
   368   if(winInfo->wnd == 2)
       
   369   {
       
   370     tmp <<= 4;
       
   371     tmp |= BsGetBits(bs, 4);
       
   372     winInfo->max_sfb = (uint8) ((tmp >> 7) & 0xF);
       
   373     if(winInfo->max_sfb > MAXLONGSFBBANDS) return (FALSE);
       
   374 
       
   375     /*-- Get grouping info for short blocks. --*/
       
   376     j = 0;
       
   377     if(!(tmp & 64)) winInfo->group[j++] = 1;
       
   378     if(!(tmp & 32)) winInfo->group[j++] = 2;
       
   379     if(!(tmp & 16)) winInfo->group[j++] = 3;
       
   380     if(!(tmp & 8))  winInfo->group[j++] = 4;
       
   381     if(!(tmp & 4))  winInfo->group[j++] = 5;
       
   382     if(!(tmp & 2))  winInfo->group[j++] = 6;
       
   383     if(!(tmp & 1))  winInfo->group[j++] = 7;
       
   384     winInfo->group[j] = NSHORT;
       
   385 
       
   386     /*-- Prediction (BWAP) is disabled. --*/
       
   387     winInfo->lpflag[0] = 0;
       
   388   }
       
   389   else
       
   390   {    
       
   391     winInfo->group[0] = 1;
       
   392     winInfo->max_sfb = (uint8) ((tmp >> 1) & 0x3F);
       
   393     if(winInfo->max_sfb > MAXLONGSFBBANDS) return (FALSE);
       
   394 
       
   395     if(winInfo->predType == BWAP_PRED)
       
   396     {
       
   397       /*-- Read BWAP predictor parameters. --*/
       
   398       winInfo->lpflag[0] = (uint8) (tmp & 0x1);
       
   399       if(winInfo->lpflag[0])
       
   400       {
       
   401         /*-- Predictor reset pattern. --*/
       
   402         winInfo->prstflag[0] = (uint8) BsGetBits(bs, LEN_PRED_RST);
       
   403         if(winInfo->prstflag[0])
       
   404           winInfo->prstflag[1] = (uint8) BsGetBits(bs, LEN_PRED_RSTGRP);
       
   405     
       
   406         /*-- Sfb flags for each predictor band. --*/
       
   407         j = (winInfo->max_sfb < winInfo->predBands) ? winInfo->max_sfb : winInfo->predBands;
       
   408     
       
   409         for(i = 1; i < j + 1; i++)
       
   410           winInfo->lpflag[i] = (uint8) BsGetBits(bs, LEN_PRED_ENAB);
       
   411 
       
   412         for(; i < winInfo->predBands + 1; i++)
       
   413           winInfo->lpflag[i] = 0;
       
   414       }
       
   415     }
       
   416 
       
   417     else if(winInfo->predType == LTP_PRED)
       
   418     {
       
   419       /*-- Is LTP used in this channel ? --*/
       
   420       ltp_left->ltp_present = (uint8) (tmp & 1);
       
   421       if(ltp_left->ltp_present)
       
   422       {
       
   423         int16 nbands = MIN(winInfo->max_sfb, winInfo->predBands);
       
   424         LTP_Decode(bs, ltp_left, nbands);
       
   425         if(ltp_right)
       
   426           LTP_Decode(bs, ltp_right, nbands);
       
   427       }
       
   428       else if(ltp_right) ltp_right->ltp_present = 0;
       
   429     }
       
   430     
       
   431     /*-- Bitstream syntax error. --*/
       
   432     else if(tmp & 0x1) return (FALSE);
       
   433   }
       
   434 
       
   435   return (TRUE);
       
   436 }
       
   437 
       
   438 /*
       
   439  * Retrieves 'global_gain' bitstream elements from single channel element.
       
   440  */
       
   441 int16
       
   442 GetSCEGain(CAACAudDec *aac, TBitStream *bs, uint8 *gains, uint32 *gainPos, uint32 bufBitOffset)
       
   443 { 
       
   444   return GetSCE(aac, bs, aac->mc_info, gains, gainPos, bufBitOffset);
       
   445 }
       
   446 
       
   447 /*
       
   448  * Retrieves 'global_gain' bitstream elements from channel pair element.
       
   449  */
       
   450 int16 
       
   451 GetCPEGain(CAACAudDec *aac, TBitStream *bs, uint8 *gains, uint32 *gainPos, uint32 bufBitOffset)
       
   452 {
       
   453   return GetCPE(aac, bs, aac->mc_info, gains, gainPos, bufBitOffset);
       
   454 }