videoeditorengine/mp3aacManipLib/AACGain/src/huffdec2.cpp
changeset 9 d87d32eab1a9
parent 0 951a5db380a0
equal deleted inserted replaced
0:951a5db380a0 9:d87d32eab1a9
     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   huffdec2.cpp - Bit parsing routines for decoding individual channel 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 "tool2.h"
       
    29 
       
    30 /**************************************************************************
       
    31   Title:        calc_gsfb_table
       
    32 
       
    33   Purpose:      Calculates reordering parameters for short blocks.
       
    34 
       
    35   Usage:        calc_gsfb_table(info, group)
       
    36 
       
    37   Input:        info  - block (long/short) parameters
       
    38                 group - grouping info for short blocks
       
    39 
       
    40   Author(s):    Juha Ojanpera
       
    41   *************************************************************************/
       
    42 
       
    43 INLINE void
       
    44 calc_gsfb_table(CInfo *info, uint8 *group)
       
    45 {
       
    46   int16 group_offset, group_idx, offset;
       
    47   int16 sfb, len, *group_offset_p;
       
    48 
       
    49   /*-- First calc the group length. --*/
       
    50   group_offset = 0;
       
    51   group_idx = 0;
       
    52   do
       
    53   {
       
    54     info->group_len[group_idx] = (int16) (group[group_idx] - group_offset);
       
    55     group_offset = group[group_idx];
       
    56     group_idx++;
       
    57   }
       
    58   while(group_offset < NSHORT);
       
    59   
       
    60   info->num_groups = group_idx;
       
    61   group_offset_p = info->bk_sfb_top;
       
    62   offset = 0;
       
    63   for(group_idx = 0; group_idx < info->num_groups; group_idx++)
       
    64   {
       
    65     len = info->group_len[group_idx];
       
    66     for(sfb = 0; sfb < info->sfb_per_sbk[group_idx]; sfb++)
       
    67     {
       
    68       offset += info->sfb_width_128[sfb] * len;
       
    69       *group_offset_p++ = offset;
       
    70     }
       
    71   }
       
    72 }
       
    73 
       
    74 /**************************************************************************
       
    75   Title:        get_pulse_nc
       
    76 
       
    77   Purpose:      Reads pulse data from the bitstream.
       
    78 
       
    79   Usage:        get_pulse_nc(bs, pulse_info)
       
    80 
       
    81   Input:        bs - bitstream parameters
       
    82 
       
    83   Output:       pulse_info - pulse data parameters for this channel
       
    84 
       
    85   Author(s):    Juha Ojanpera
       
    86   *************************************************************************/
       
    87 
       
    88 INLINE void
       
    89 get_pulse_nc(TBitStream *bs, PulseInfo *pulse_info)
       
    90 {
       
    91   int16 i, mask;
       
    92 
       
    93   mask = (1 << LEN_PULSE_PAMP) - 1;
       
    94   pulse_info->number_pulse = (int16) BsGetBits(bs, LEN_PULSE_NPULSE);
       
    95   pulse_info->pulse_start_sfb = (int16) BsGetBits(bs, LEN_PULSE_ST_SFB);
       
    96 
       
    97   for(i = 0; i < pulse_info->number_pulse + 1; i++)
       
    98   {
       
    99     uint32 codeword;
       
   100 
       
   101     codeword = BsGetBits(bs, LEN_PULSE_POFF + LEN_PULSE_PAMP);
       
   102 
       
   103     pulse_info->pulse_offset[i] = (int16) (codeword >> LEN_PULSE_PAMP);
       
   104     pulse_info->pulse_amp[i] = (int16) (codeword & mask);
       
   105   }
       
   106 }
       
   107 
       
   108 /**************************************************************************
       
   109   Title:        huf_cb
       
   110 
       
   111   Purpose:      Reads the Huffman codebook number and boundaries for each section.
       
   112 
       
   113   Usage:        y = huf_cb(bs, sect, info, tot_sfb, max_sfb, cb_map)
       
   114 
       
   115   Input:        bs      - bitstream parameters
       
   116                 info    - block (long/short) parameters
       
   117                 tot_sfb - total # of sfb's present
       
   118                 max_sfb - max # of sfb's per block present
       
   119 
       
   120   Output:       y      - # of sections
       
   121                 sect   - sectioning info (cb, cb_length; cb, cb_length; ...)
       
   122                 cb_map - Huffman codebook number for each sfb
       
   123 
       
   124   Author(s):    Juha Ojanpera
       
   125   *************************************************************************/
       
   126 
       
   127 INLINE int16
       
   128 huf_cb(TBitStream *bs, uint8 *sect, CInfo *info, int16 tot_sfb, uint8 max_sfb, uint8 *cb_map)
       
   129 {
       
   130   int16 nsect, n, base, bits, len, cb, diff, cb_len, bot;
       
   131 
       
   132   bits = (info->islong) ? (int16) LONG_SECT_BITS : (int16) SHORT_SECT_BITS;
       
   133 
       
   134   nsect = 0;
       
   135   len = (int16) ((1 << bits) - 1);
       
   136   diff = info->sfb_per_sbk[0] - max_sfb;
       
   137 
       
   138   for(bot = base = 0; base < tot_sfb && nsect < tot_sfb; )
       
   139   {
       
   140     *sect++ = uint8(cb = BsGetBits(bs, LEN_CB));
       
   141 
       
   142     n = BsGetBits(bs, bits);
       
   143     while(n == len && base < tot_sfb)
       
   144     {
       
   145       base += len;
       
   146       n = BsGetBits(bs, bits);
       
   147     }
       
   148     base += n; 
       
   149     *sect++ = uint8(base); 
       
   150     nsect++;
       
   151 
       
   152     cb_len = base - bot; SET_MEMORY(cb_map, cb_len, cb); cb_map += cb_len;
       
   153     
       
   154     if(base == max_sfb)
       
   155     {
       
   156       base += diff;
       
   157       if(info->islong) break;
       
   158 
       
   159       if(diff)
       
   160       {
       
   161         *sect++ = 0; *sect++ = uint8(base);
       
   162         ZERO_MEMORY(cb_map, diff); cb_map += diff;
       
   163         nsect++;
       
   164       }
       
   165       max_sfb += info->sfb_per_sbk[0];
       
   166     }
       
   167     bot = base;
       
   168   }
       
   169   
       
   170   if(base != tot_sfb || nsect > tot_sfb)
       
   171     nsect = -1;
       
   172   
       
   173   return (nsect);
       
   174 }
       
   175 
       
   176 /**************************************************************************
       
   177   Title:        TNS_Decode
       
   178 
       
   179   Purpose:      Reads TNS bitstream elements.
       
   180 
       
   181   Usage:        TNS_Decode(bs, info)
       
   182 
       
   183   Input:        bs   - bitstream parameters
       
   184                 info - block (long/short) parameters
       
   185 
       
   186   Author(s):    Juha Ojanpera
       
   187   *************************************************************************/
       
   188 
       
   189 INLINE void
       
   190 TNS_Decode(TBitStream *bs, CInfo *info)
       
   191 {
       
   192   int16 f, s, res, res2, nBlockBits, nOrderBits, nOrderBitsMask;
       
   193 
       
   194   nBlockBits = (!info->islong) ? (int16) 4 : (int16) 6;
       
   195   nOrderBits = (!info->islong) ? (int16) 3 : (int16) 5;
       
   196   nOrderBitsMask = (int16) ((1 << nOrderBits) - 1);
       
   197 
       
   198   for(s = 0; s < info->nsbk; s++)
       
   199   {
       
   200     int16 nTnsFilt;
       
   201     
       
   202     nTnsFilt = (int16) BsGetBits(bs, (!info->islong) ? (int16) 1 : (int16) 2);
       
   203     if(!nTnsFilt)
       
   204       continue;
       
   205     
       
   206     res = (int16) (BsGetBits(bs, 1) + 3);
       
   207     for(f = nTnsFilt; f > 0; f--)
       
   208     {
       
   209       int16 t, order;
       
   210 
       
   211       t = (int16) BsGetBits(bs, (int16) (nBlockBits + nOrderBits));
       
   212       order = (int16) (t & nOrderBitsMask);
       
   213       
       
   214       if(order)
       
   215       {
       
   216         t = (int16) BsGetBits(bs, 2);
       
   217         res2 = (int16) (res - (t & 0x1));
       
   218         
       
   219         BsSkipNBits(bs, res2 * order);
       
   220       }
       
   221     }
       
   222   }
       
   223 }
       
   224 
       
   225 /**************************************************************************
       
   226   Title:        GetICS
       
   227 
       
   228   Purpose:      Decodes individual channel elements.
       
   229 
       
   230   Usage:        y = GetICS(bs, cip, group, max_sfb, cb_map, coef, gain, factors)
       
   231 
       
   232   Input:        bs           - bitstream parameters
       
   233                 cip :
       
   234                  info        - block (long/short) parameters
       
   235                 group        - grouping info for short blocks
       
   236                 max_sfb      - max # of sfb's present
       
   237                 gain         - start value for scalefactor decoding
       
   238 
       
   239   Output:       y            - TRUE on success, FALSE otherwise
       
   240                 cb_map       - Huffman codebook number for each sfb
       
   241                 factors      - scalefactors for this channel
       
   242                 cip :
       
   243                  is_present  - TRUE if IS is used, FALSE otherwise
       
   244                  tns_present - TRUE if TNS is used, FALSE otherwise
       
   245 
       
   246   Author(s):    Juha Ojanpera
       
   247 *************************************************************************/
       
   248 
       
   249 int16
       
   250 GetICS(TBitStream *bs, TCh_Info *cip, uint8 *group, uint8 max_sfb, uint8 *cb_map, 
       
   251        int16 *quant, int16 gain, int16 *factors)
       
   252 {
       
   253   CInfo *info;
       
   254   int16 nsect;
       
   255   PulseInfo pulse_info;
       
   256   uint8 sect[(MAXBANDS + 1) << 1];
       
   257 
       
   258   info = cip->info;
       
   259   
       
   260   /*-- Calculate total number of sfb for this grouping. --*/
       
   261   if(max_sfb == 0)
       
   262   {
       
   263     nsect = 0;
       
   264     ZERO_MEMORY(cb_map, MAXBANDS);
       
   265     ZERO_MEMORY(factors, MAXBANDS * sizeof(int16));
       
   266   }
       
   267   else
       
   268   {    
       
   269     int16 tot_sfb = info->sfb_per_sbk[0];
       
   270     if(!info->islong)
       
   271     {
       
   272       int16 i = 0;
       
   273       while(group[i++] < info->nsbk)
       
   274         tot_sfb += info->sfb_per_sbk[0];
       
   275 
       
   276       /*-- Calculate band offsets. --*/
       
   277       calc_gsfb_table(info, group);
       
   278     }
       
   279 
       
   280     /*-- Section data. --*/
       
   281     nsect = huf_cb(bs, sect, info, tot_sfb, max_sfb, cb_map);
       
   282     if(nsect < 0) return (FALSE);
       
   283 
       
   284     /*-- Scalefactor data. --*/
       
   285     if(!huf_sfac(bs, cip, group, cb_map, gain, factors, max_sfb))
       
   286       return (FALSE);
       
   287   }
       
   288 
       
   289   cip->tns_present = FALSE;
       
   290   pulse_info.pulse_data_present = FALSE;
       
   291 
       
   292   /*-- PULSE noiseless coding. --*/
       
   293   pulse_info.pulse_data_present = (BOOL) BsGetBits(bs, 1);
       
   294   if(pulse_info.pulse_data_present)
       
   295   {
       
   296     if(info->islong)
       
   297       get_pulse_nc(bs, &pulse_info);
       
   298     else
       
   299       return (FALSE);
       
   300   }
       
   301 
       
   302   /*-- TNS data. --*/
       
   303   if(BsGetBits(bs, 1))
       
   304   {
       
   305     cip->tns_present = TRUE;
       
   306     TNS_Decode(bs, info);
       
   307   }
       
   308 
       
   309   /*-- Gain control. --*/
       
   310   if(BsGetBits(bs, 1))
       
   311     return (FALSE);
       
   312     
       
   313   /*-- Quantized spectral data. --*/
       
   314   if(!(!nsect && max_sfb))
       
   315     cip->num_bins = huf_spec(bs, info, nsect, sect, quant, cip->huf, 1);
       
   316   else 
       
   317     return (FALSE);
       
   318   
       
   319   return (TRUE);
       
   320 }