Home | History | Annotate | Download | only in aacdec
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 /*
     19 
     20   Filename: extractframeInfo.c
     21 
     22 ------------------------------------------------------------------------------
     23  REVISION HISTORY
     24 
     25 
     26  Who:                                   Date: MM/DD/YYYY
     27  Description:
     28 
     29 ------------------------------------------------------------------------------
     30  INPUT AND OUTPUT DEFINITIONS
     31 
     32  Arguments:   hBitBuf      - bitbuffer handle
     33               v_frame_info - pointer to memorylocation where the frame-info will
     34                              be stored.
     35 
     36  Return:     none.
     37 
     38 
     39 ------------------------------------------------------------------------------
     40  FUNCTION DESCRIPTION
     41 
     42 Extracts a frame_info vector from control data read from the bitstream.
     43 
     44 ------------------------------------------------------------------------------
     45  REQUIREMENTS
     46 
     47 
     48 ------------------------------------------------------------------------------
     49  REFERENCES
     50 
     51 SC 29 Software Copyright Licencing Disclaimer:
     52 
     53 This software module was originally developed by
     54   Coding Technologies
     55 
     56 and edited by
     57   -
     58 
     59 in the course of development of the ISO/IEC 13818-7 and ISO/IEC 14496-3
     60 standards for reference purposes and its performance may not have been
     61 optimized. This software module is an implementation of one or more tools as
     62 specified by the ISO/IEC 13818-7 and ISO/IEC 14496-3 standards.
     63 ISO/IEC gives users free license to this software module or modifications
     64 thereof for use in products claiming conformance to audiovisual and
     65 image-coding related ITU Recommendations and/or ISO/IEC International
     66 Standards. ISO/IEC gives users the same free license to this software module or
     67 modifications thereof for research purposes and further ISO/IEC standardisation.
     68 Those intending to use this software module in products are advised that its
     69 use may infringe existing patents. ISO/IEC have no liability for use of this
     70 software module or modifications thereof. Copyright is not released for
     71 products that do not conform to audiovisual and image-coding related ITU
     72 Recommendations and/or ISO/IEC International Standards.
     73 The original developer retains full right to modify and use the code for its
     74 own purpose, assign or donate the code to a third party and to inhibit third
     75 parties from using the code for products that do not conform to audiovisual and
     76 image-coding related ITU Recommendations and/or ISO/IEC International Standards.
     77 This copyright notice must be included in all copies or derivative works.
     78 Copyright (c) ISO/IEC 2002.
     79 
     80 ------------------------------------------------------------------------------
     81  PSEUDO-CODE
     82 
     83 ------------------------------------------------------------------------------
     84 */
     85 
     86 
     87 /*----------------------------------------------------------------------------
     88 ; INCLUDES
     89 ----------------------------------------------------------------------------*/
     90 
     91 #ifdef AAC_PLUS
     92 
     93 
     94 #include    "extractframeinfo.h"
     95 #include    "buf_getbits.h"
     96 #include    "aac_mem_funcs.h"
     97 
     98 
     99 /*----------------------------------------------------------------------------
    100 ; MACROS
    101 ; Define module specific macros here
    102 ----------------------------------------------------------------------------*/
    103 
    104 
    105 /*----------------------------------------------------------------------------
    106 ; DEFINES
    107 ; Include all pre-processor statements here. Include conditional
    108 ; compile variables also.
    109 ----------------------------------------------------------------------------*/
    110 
    111 /*----------------------------------------------------------------------------
    112 ; LOCAL FUNCTION DEFINITIONS
    113 ; Function Prototype declaration
    114 ----------------------------------------------------------------------------*/
    115 
    116 /*----------------------------------------------------------------------------
    117 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
    118 ; Variable declaration - defined here and used outside this module
    119 ----------------------------------------------------------------------------*/
    120 
    121 
    122 /*
    123  *  (int) ceil (log (bs_num_env + 1) / log (2))
    124  *  ceil(log([0:5]+1)/log(2))
    125  */
    126 
    127 const Int32 bs_pointer_bits_tbl[MAX_ENVELOPES + 1] = { 0, 1, 2, 2, 3, 3};
    128 
    129 /*
    130  *  (int)((float)numTimeSlots/bs_num_env + 0.5f)
    131  *  floor(16./[0:5] + 0.5)
    132  */
    133 
    134 const Int32 T_16_ov_bs_num_env_tbl[MAX_ENVELOPES + 1] = { 2147483647, 16, 8,
    135         5,  4, 3
    136                                                         };
    137 
    138 
    139 /*----------------------------------------------------------------------------
    140 ; EXTERNAL FUNCTION REFERENCES
    141 ; Declare functions defined elsewhere and referenced in this module
    142 ----------------------------------------------------------------------------*/
    143 
    144 /*----------------------------------------------------------------------------
    145 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
    146 ; Declare variables used in this module but defined elsewhere
    147 ----------------------------------------------------------------------------*/
    148 
    149 /*----------------------------------------------------------------------------
    150 ; FUNCTION CODE
    151 ----------------------------------------------------------------------------*/
    152 
    153 
    154 SBR_ERROR extractFrameInfo(BIT_BUFFER     * hBitBuf,
    155                            SBR_FRAME_DATA * h_frame_data)
    156 {
    157 
    158     Int32 absBordLead = 0;
    159     Int32 nRelLead = 0;
    160     Int32 nRelTrail = 0;
    161     Int32 bs_num_env = 0;
    162     Int32 bs_num_rel = 0;
    163     Int32 bs_var_bord = 0;
    164     Int32 bs_var_bord_0 = 0;
    165     Int32 bs_var_bord_1 = 0;
    166     Int32 bs_pointer = 0;
    167     Int32 bs_pointer_bits;
    168     Int32 frameClass;
    169     Int32 temp;
    170     Int32 env;
    171     Int32 k;
    172     Int32 bs_num_rel_0 = 0;
    173     Int32 bs_num_rel_1 = 0;
    174     Int32 absBordTrail = 0;
    175     Int32 middleBorder = 0;
    176     Int32 bs_num_noise;
    177     Int32 lA = 0;
    178 
    179     Int32 tE[MAX_ENVELOPES + 1];
    180     Int32 tQ[2 + 1];
    181     Int32 f[MAX_ENVELOPES + 1];
    182     Int32 bs_rel_bord[3];
    183     Int32 bs_rel_bord_0[3];
    184     Int32 bs_rel_bord_1[3];
    185     Int32 relBordLead[3];
    186     Int32 relBordTrail[3];
    187 
    188 
    189     Int32 *v_frame_info = h_frame_data->frameInfo;
    190 
    191     SBR_ERROR err =  SBRDEC_OK;
    192 
    193 
    194     /*
    195      * First read from the bitstream.
    196      */
    197 
    198     /* Read frame class */
    199     h_frame_data->frameClass = frameClass = buf_getbits(hBitBuf, SBR_CLA_BITS);
    200 
    201 
    202     switch (frameClass)
    203     {
    204 
    205         case FIXFIX:
    206             temp = buf_getbits(hBitBuf, SBR_ENV_BITS);   /* 2 bits */
    207 
    208             bs_num_env = 1 << temp;
    209 
    210 
    211             f[0] = buf_getbits(hBitBuf, SBR_RES_BITS);   /* 1 bit */
    212 
    213             for (env = 1; env < bs_num_env; env++)
    214             {
    215                 f[env] = f[0];
    216             }
    217 
    218             nRelLead     = bs_num_env - 1;
    219             absBordTrail  = 16;
    220 
    221 
    222             break;
    223 
    224         case FIXVAR:
    225             bs_var_bord = buf_getbits(hBitBuf, SBR_ABS_BITS);   /* 2 bits */
    226             bs_num_rel  = buf_getbits(hBitBuf, SBR_NUM_BITS);   /* 2 bits */
    227             bs_num_env  = bs_num_rel + 1;
    228 
    229             for (k = 0; k < bs_num_env - 1; k++)
    230             {
    231                 bs_rel_bord[k] = (buf_getbits(hBitBuf, SBR_REL_BITS) + 1) << 1;
    232             }
    233 
    234             bs_pointer_bits = bs_pointer_bits_tbl[bs_num_env];
    235 
    236             bs_pointer = buf_getbits(hBitBuf, bs_pointer_bits);
    237 
    238             for (env = 0; env < bs_num_env; env++)
    239             {                                                    /* 1 bit */
    240                 f[bs_num_env - 1 - env] = buf_getbits(hBitBuf, SBR_RES_BITS);
    241             }
    242 
    243             absBordTrail  = 16 + bs_var_bord;
    244             nRelTrail     = bs_num_rel;
    245 
    246             break;
    247 
    248         case VARFIX:
    249             bs_var_bord = buf_getbits(hBitBuf, SBR_ABS_BITS);   /* 2 bits */
    250             bs_num_rel  = buf_getbits(hBitBuf, SBR_NUM_BITS);   /* 2 bits */
    251             bs_num_env  = bs_num_rel + 1;
    252 
    253             for (k = 0; k < bs_num_env - 1; k++)
    254             {
    255                 bs_rel_bord[k] = (buf_getbits(hBitBuf, SBR_REL_BITS) + 1) << 1;
    256             }
    257 
    258             bs_pointer_bits = bs_pointer_bits_tbl[bs_num_env];
    259 
    260             bs_pointer = buf_getbits(hBitBuf, bs_pointer_bits);
    261 
    262             for (env = 0; env < bs_num_env; env++)
    263             {                                  /* 1 bit */
    264                 f[env] = buf_getbits(hBitBuf, SBR_RES_BITS);
    265             }
    266 
    267             absBordTrail = 16;
    268             absBordLead  = bs_var_bord;
    269             nRelLead     = bs_num_rel;
    270 
    271             break;
    272 
    273         case VARVAR:
    274             bs_var_bord_0 = buf_getbits(hBitBuf, SBR_ABS_BITS);   /* 2 bits */
    275             bs_var_bord_1 = buf_getbits(hBitBuf, SBR_ABS_BITS);
    276             bs_num_rel_0  = buf_getbits(hBitBuf, SBR_NUM_BITS);   /* 2 bits */
    277             bs_num_rel_1  = buf_getbits(hBitBuf, SBR_NUM_BITS);
    278 
    279             bs_num_env = bs_num_rel_0 + bs_num_rel_1 + 1;
    280 
    281             for (k = 0; k < bs_num_rel_0; k++)
    282             {                                                 /* 2 bits */
    283                 bs_rel_bord_0[k] = (buf_getbits(hBitBuf, SBR_REL_BITS) + 1) << 1;
    284             }
    285 
    286             for (k = 0; k < bs_num_rel_1; k++)
    287             {                                                 /* 2 bits */
    288                 bs_rel_bord_1[k] = (buf_getbits(hBitBuf, SBR_REL_BITS) + 1) << 1;
    289             }
    290 
    291 
    292             bs_pointer_bits = bs_pointer_bits_tbl[bs_num_env];
    293 
    294             bs_pointer = buf_getbits(hBitBuf, bs_pointer_bits);
    295 
    296             for (env = 0; env < bs_num_env; env++)
    297             {                                  /* 1 bit */
    298                 f[env] = buf_getbits(hBitBuf, SBR_RES_BITS);
    299             }
    300 
    301             absBordLead   = bs_var_bord_0;
    302             absBordTrail  = 16 + bs_var_bord_1;
    303             nRelLead      = bs_num_rel_0;
    304             nRelTrail     = bs_num_rel_1;
    305 
    306             break;
    307 
    308     };
    309 
    310 
    311     /*
    312      * Calculate the framing.
    313      */
    314 
    315 
    316     switch (frameClass)
    317     {
    318         case FIXFIX:
    319             for (k = 0; k < nRelLead; k++)
    320             {
    321                 relBordLead[k] = T_16_ov_bs_num_env_tbl[bs_num_env];
    322             }
    323             break;
    324         case VARFIX:
    325             for (k = 0; k < nRelLead; k++)
    326             {
    327                 relBordLead[k] = bs_rel_bord[k];
    328             }
    329             break;
    330         case VARVAR:
    331             for (k = 0; k < nRelLead; k++)
    332             {
    333                 relBordLead[k] = bs_rel_bord_0[k];
    334             }
    335             for (k = 0; k < nRelTrail; k++)
    336             {
    337                 relBordTrail[k] = bs_rel_bord_1[k];
    338             }
    339             break;
    340         case FIXVAR:
    341             for (k = 0; k < nRelTrail; k++)
    342             {
    343                 relBordTrail[k] = bs_rel_bord[k];
    344             }
    345             break;
    346     }
    347 
    348 
    349     tE[0]          = absBordLead;
    350     tE[bs_num_env] = absBordTrail;
    351 
    352     for (env = 1; env <= nRelLead; env++)
    353     {
    354         tE[env] = absBordLead;
    355         for (k = 0; k <= env - 1; k++)
    356         {
    357             tE[env] += relBordLead[k];
    358         }
    359     }
    360 
    361     for (env = nRelLead + 1; env < bs_num_env; env++)
    362     {
    363         tE[env] = absBordTrail;
    364         for (k = 0; k <= bs_num_env - env - 1; k++)
    365         {
    366             tE[env] -= relBordTrail[k];
    367         }
    368     }
    369 
    370 
    371 
    372     switch (frameClass)
    373     {
    374         case  FIXFIX:
    375             middleBorder = bs_num_env >> 1;
    376             break;
    377         case VARFIX:
    378             switch (bs_pointer)
    379             {
    380                 case 0:
    381                     middleBorder = 1;
    382                     break;
    383                 case 1:
    384                     middleBorder = bs_num_env - 1;
    385                     break;
    386                 default:
    387                     middleBorder = bs_pointer - 1;
    388                     break;
    389             };
    390             break;
    391         case FIXVAR:
    392         case VARVAR:
    393             switch (bs_pointer)
    394             {
    395                 case 0:
    396                 case 1:
    397                     middleBorder = bs_num_env - 1;
    398                     break;
    399                 default:
    400                     middleBorder = bs_num_env + 1 - bs_pointer;
    401                     break;
    402             };
    403             break;
    404     };
    405 
    406 
    407     tQ[0] = tE[0];
    408     if (bs_num_env > 1)
    409     {
    410         tQ[1] = tE[middleBorder];
    411         tQ[2] = tE[bs_num_env];
    412         bs_num_noise = 2;
    413     }
    414     else
    415     {
    416         tQ[1] = tE[bs_num_env];
    417         bs_num_noise = 1;
    418     }
    419 
    420     /*
    421      *  Check consistency on freq bands
    422      */
    423 
    424     if ((tE[bs_num_env] < tE[0]) || (tE[0] < 0))
    425     {
    426         err = SBRDEC_INVALID_BITSTREAM;
    427     }
    428 
    429 
    430     switch (frameClass)
    431     {
    432         case  FIXFIX:
    433             lA = -1;
    434             break;
    435         case VARFIX:
    436             switch (bs_pointer)
    437             {
    438                 case 0:
    439                 case 1:
    440                     lA = -1;
    441                     break;
    442                 default:
    443                     lA = bs_pointer - 1;
    444                     break;
    445             };
    446             break;
    447         case FIXVAR:
    448         case VARVAR:
    449             switch (bs_pointer)
    450             {
    451                 case 0:
    452                     lA = - 1;
    453                     break;
    454                 default:
    455                     lA = bs_num_env + 1 - bs_pointer;
    456                     break;
    457             };
    458             break;
    459     };
    460 
    461     /*
    462      * Build the frameInfo vector...
    463      */
    464 
    465     v_frame_info[0] = bs_num_env;   /* Number of envelopes*/
    466     pv_memcpy(v_frame_info + 1, tE, (bs_num_env + 1)*sizeof(Int32));    /* time borders*/
    467     /* frequency resolution */
    468     pv_memcpy(v_frame_info + 1 + bs_num_env + 1, f, bs_num_env*sizeof(Int32));
    469 
    470     temp = (1 + bs_num_env) << 1;
    471     v_frame_info[temp] = lA;                     /* transient envelope*/
    472     v_frame_info[temp + 1] = bs_num_noise;       /* Number of noise envelopes */
    473     /* noise borders */
    474     pv_memcpy(v_frame_info + temp + 2, tQ, (bs_num_noise + 1)*sizeof(Int32));
    475 
    476 
    477     return (err);
    478 
    479 }
    480 
    481 
    482 
    483 
    484 
    485 
    486 #endif
    487 
    488