Home | History | Annotate | Download | only in src
      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 Portions of this file are derived from the following 3GPP standard:
     20 
     21     3GPP TS 26.173
     22     ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
     23     Available from http://www.3gpp.org
     24 
     25 (C) 2007, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
     26 Permission to distribute, modify and use this file under the standard license
     27 terms listed above has been obtained from the copyright holder.
     28 ****************************************************************************************/
     29 /*
     30 ------------------------------------------------------------------------------
     31 
     32 
     33 
     34  Filename: dec_alg_codebook.cpp
     35 
     36      Date: 05/08/2004
     37 
     38 ------------------------------------------------------------------------------
     39  REVISION HISTORY
     40 
     41 
     42  Description:
     43 
     44 ------------------------------------------------------------------------------
     45  INPUT AND OUTPUT DEFINITIONS
     46 
     47 
     48 ------------------------------------------------------------------------------
     49  FUNCTION DESCRIPTION
     50 
     51         decoding of algebraic codebook
     52 
     53 ------------------------------------------------------------------------------
     54  REQUIREMENTS
     55 
     56 
     57 ------------------------------------------------------------------------------
     58  REFERENCES
     59 
     60 ------------------------------------------------------------------------------
     61  PSEUDO-CODE
     62 
     63 ------------------------------------------------------------------------------
     64 */
     65 
     66 
     67 /*----------------------------------------------------------------------------
     68 ; INCLUDES
     69 ----------------------------------------------------------------------------*/
     70 
     71 #include "pv_amr_wb_type_defs.h"
     72 #include "pvamrwbdecoder_basic_op.h"
     73 #include "q_pulse.h"
     74 
     75 /*----------------------------------------------------------------------------
     76 ; MACROS
     77 ; Define module specific macros here
     78 ----------------------------------------------------------------------------*/
     79 
     80 
     81 /*----------------------------------------------------------------------------
     82 ; DEFINES
     83 ; Include all pre-processor statements here. Include conditional
     84 ; compile variables also.
     85 ----------------------------------------------------------------------------*/
     86 
     87 #define NB_POS 16                          /* pos in track, mask for sign bit */
     88 
     89 /*----------------------------------------------------------------------------
     90 ; DEFINES
     91 ; Include all pre-processor statements here. Include conditional
     92 ; compile variables also.
     93 ----------------------------------------------------------------------------*/
     94 
     95 /*----------------------------------------------------------------------------
     96 ; LOCAL FUNCTION DEFINITIONS
     97 ; Function Prototype declaration
     98 ----------------------------------------------------------------------------*/
     99 
    100 /*----------------------------------------------------------------------------
    101 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
    102 ; Variable declaration - defined here and used outside this module
    103 ----------------------------------------------------------------------------*/
    104 
    105 /*----------------------------------------------------------------------------
    106 ; EXTERNAL FUNCTION REFERENCES
    107 ; Declare functions defined elsewhere and referenced in this module
    108 ----------------------------------------------------------------------------*/
    109 
    110 /*----------------------------------------------------------------------------
    111 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
    112 ; Declare variables used in this module but defined elsewhere
    113 ----------------------------------------------------------------------------*/
    114 
    115 /*----------------------------------------------------------------------------
    116 ; FUNCTION CODE
    117 ----------------------------------------------------------------------------*/
    118 void dec_1p_N1(int32 index, int16 N, int16 offset, int16 pos[])
    119 {
    120     int16 pos1;
    121     int32 mask, i;
    122 
    123     mask = ((1 << N) - 1);
    124     /*-------------------------------------------------------*
    125      * Decode 1 pulse with N+1 bits:                         *
    126      *-------------------------------------------------------*/
    127     pos1 = ((index & mask) + offset);
    128 
    129     i = ((index >> N) & 1L);            /* i = ((index >> N) & 1); */
    130 
    131     if (i == 1)
    132     {
    133         pos1 += NB_POS;
    134     }
    135     pos[0] = pos1;
    136 
    137 }
    138 
    139 
    140 
    141 /*----------------------------------------------------------------------------
    142 ; FUNCTION CODE
    143 ----------------------------------------------------------------------------*/
    144 
    145 void dec_2p_2N1(int32 index, int16 N, int16 offset, int16 pos[])
    146 {
    147     int16 pos1, pos2, tmp;
    148     int32 mask, i;
    149 
    150     mask = (int32)(sub_int16(shl_int16(1, N), 1)); /* mask = ((1<<N)-1); */
    151     /*-------------------------------------------------------*
    152      * Decode 2 pulses with 2*N+1 bits:                      *
    153      *-------------------------------------------------------*/
    154     /* pos1 = (((index >> N) & mask) + offset); */
    155     pos1 = (int16)(add_int32((shr_int32(index, N) & mask), (int32)(offset)));
    156     tmp = shl_int16(N, 1);
    157     i = (index >> tmp) & 1L;         /* i = (index >> (2*N)) & 1; */
    158     pos2 = add_int16((int16)(index & mask), offset); /* pos2 = ((index & mask) + offset); */
    159 
    160     if (pos2 < pos1)              /* ((pos2 - pos1) < 0) */
    161     {
    162         if (i == 1)
    163         {                                  /* (i == 1) */
    164             pos1 += NB_POS;      /* pos1 += NB_POS; */
    165         }
    166         else
    167         {
    168             pos2 += NB_POS;      /* pos2 += NB_POS;  */
    169         }
    170     }
    171     else
    172     {
    173         if (i == 1)
    174         {                                  /* (i == 1) */
    175             pos1 += NB_POS;      /* pos1 += NB_POS; */
    176             pos2 += NB_POS;      /* pos2 += NB_POS; */
    177         }
    178     }
    179 
    180     pos[0] = pos1;
    181     pos[1] = pos2;
    182 
    183     return;
    184 }
    185 
    186 
    187 
    188 /*----------------------------------------------------------------------------
    189 ; FUNCTION CODE
    190 ----------------------------------------------------------------------------*/
    191 
    192 void dec_3p_3N1(int32 index, int16 N, int16 offset, int16 pos[])
    193 {
    194     int16 j, tmp;
    195     int32 mask, idx;
    196 
    197     /*-------------------------------------------------------*
    198      * Decode 3 pulses with 3*N+1 bits:                      *
    199      *-------------------------------------------------------*/
    200     tmp = sub_int16(shl_int16(N, 1), 1);               /* mask = ((1<<((2*N)-1))-1); */
    201 
    202     mask = ((1 << ((2 * N) - 1)) - 1);
    203 
    204     idx = index & mask;
    205     j = offset;
    206     tmp = (N << 1) - 1;
    207 
    208 
    209     if (((index >> tmp) & 1L) != 0L)
    210     {                                      /* if (((index >> ((2*N)-1)) & 1) == 1){ */
    211         j += (1 << (N - 1)); /* j += (1<<(N-1)); */
    212     }
    213     dec_2p_2N1(idx, (int16)(N - 1), j, pos);
    214 
    215     mask = ((1 << (N + 1)) - 1);
    216     tmp = N << 1;                     /* idx = (index >> (2*N)) & mask; */
    217     idx = (index >> tmp) & mask;
    218 
    219     dec_1p_N1(idx, N, offset, pos + 2);
    220 
    221     return;
    222 }
    223 
    224 
    225 /*----------------------------------------------------------------------------
    226 ; FUNCTION CODE
    227 ----------------------------------------------------------------------------*/
    228 
    229 void dec_4p_4N1(int32 index, int16 N, int16 offset, int16 pos[])
    230 {
    231     int16 j, tmp;
    232     int32 mask, idx;
    233 
    234     /*-------------------------------------------------------*
    235      * Decode 4 pulses with 4*N+1 bits:                      *
    236      *-------------------------------------------------------*/
    237     tmp = (N << 1) - 1;
    238     mask = (1L << tmp) - 1L;
    239     idx = index & mask;
    240     j = offset;
    241     tmp = (N << 1) - 1;
    242 
    243 
    244     if (((index >> tmp) & 1L) != 0L)
    245     {                                      /* (((index >> ((2*N)-1)) & 1) == 1) */
    246         j += (1 << (N - 1)); /* j += (1<<(N-1)); */
    247     }
    248     dec_2p_2N1(idx, (int16)(N - 1), j, pos);
    249 
    250 
    251     tmp = (N << 1) + 1;             /* mask = ((1<<((2*N)+1))-1); */
    252     mask = (1L << tmp) - 1L;
    253     idx = (index >> (N << 1)) & mask;   /* idx = (index >> (2*N)) & mask; */
    254     dec_2p_2N1(idx, N, offset, pos + 2);      /* dec_2p_2N1(idx, N, offset, pos+2); */
    255 
    256     return;
    257 }
    258 
    259 
    260 
    261 /*----------------------------------------------------------------------------
    262 ; FUNCTION CODE
    263 ----------------------------------------------------------------------------*/
    264 
    265 void dec_4p_4N(int32 index, int16 N, int16 offset, int16 pos[])
    266 {
    267     int16 j, n_1, tmp;
    268 
    269     /*-------------------------------------------------------*
    270      * Decode 4 pulses with 4*N bits:                        *
    271      *-------------------------------------------------------*/
    272 
    273     n_1 = N - 1;
    274     j = offset + (1 << n_1);          /* j = offset + (1 << n_1); */
    275 
    276     tmp = (N << 2) - 2;
    277 
    278     switch ((index >> tmp) & 3)
    279     {                                      /* ((index >> ((4*N)-2)) & 3) */
    280         case 0:
    281             tmp = (n_1 << 2) + 1;
    282 
    283             if ((index >> tmp) & 1)
    284             {                                  /* (((index >> ((4*n_1)+1)) & 1) == 0) */
    285                 dec_4p_4N1(index, n_1, j, pos);
    286             }
    287             else
    288             {
    289                 dec_4p_4N1(index, n_1, offset, pos);
    290             }
    291             break;
    292         case 1:
    293             tmp = (3 * n_1) + 1; /* dec_1p_N1((index>>((3*n_1)+1)), n_1, offset, pos) */
    294             dec_1p_N1(index >> tmp, n_1, offset, pos);
    295             dec_3p_3N1(index, n_1, j, pos + 1);
    296             break;
    297         case 2:
    298             tmp = (n_1 << 1) + 1;       /* dec_2p_2N1((index>>((2*n_1)+1)), n_1, offset, pos); */
    299             dec_2p_2N1(index >> tmp, n_1, offset, pos);
    300             dec_2p_2N1(index, n_1, j, pos + 2);
    301             break;
    302         case 3:
    303             tmp = n_1 + 1;                 /* dec_3p_3N1((index>>(n_1+1)), n_1, offset, pos); */
    304             dec_3p_3N1(index >> tmp, n_1, offset, pos);
    305             dec_1p_N1(index, n_1, j, pos + 3);
    306             break;
    307     }
    308     return;
    309 }
    310 
    311 
    312 /*----------------------------------------------------------------------------
    313 ; FUNCTION CODE
    314 ----------------------------------------------------------------------------*/
    315 
    316 void dec_5p_5N(int32 index, int16 N, int16 offset, int16 pos[])
    317 {
    318     int16 j, n_1, tmp;
    319     int32 idx;
    320 
    321     /*-------------------------------------------------------*
    322      * Decode 5 pulses with 5*N bits:                        *
    323      *-------------------------------------------------------*/
    324 
    325     n_1 = (int16)(N - 1);
    326     j = add_int16(offset, shl_int16(1, n_1));          /* j = offset + (1 << n_1); */
    327     tmp = (N << 1) + 1;             /* idx = (index >> ((2*N)+1)); */
    328     idx = index >> tmp;
    329     tmp = (5 * N) - 1;    /* ((5*N)-1)) */
    330 
    331 
    332     if ((index >> tmp) & 1)    /* ((index >> ((5*N)-1)) & 1)  */
    333     {
    334         dec_3p_3N1(idx, n_1, j, pos);
    335         dec_2p_2N1(index, N, offset, pos + 3);
    336     }
    337     else
    338     {
    339         dec_3p_3N1(idx, n_1, offset, pos);
    340         dec_2p_2N1(index, N, offset, pos + 3);
    341     }
    342     return;
    343 }
    344 
    345 
    346 /*----------------------------------------------------------------------------
    347 ; FUNCTION CODE
    348 ----------------------------------------------------------------------------*/
    349 
    350 void dec_6p_6N_2(int32 index, int16 N, int16 offset, int16 pos[])
    351 {
    352     int16 j, n_1, offsetA, offsetB;
    353 
    354     n_1 = N - 1;
    355     j = offset + (1 << n_1);       /* j = offset + (1 << n_1); */
    356 
    357 
    358     /* !!  N and n_1 are constants -> it doesn't need to be operated by Basic Operators */
    359 
    360     offsetA = offsetB = j;
    361 
    362     if (((index >> (6*N - 5)) & 1L) == 0)
    363     {                                      /* if (((index >> ((6*N)-5)) & 1) == 0) */
    364         offsetA = offset;
    365     }
    366     else
    367     {
    368         offsetB = offset;
    369     }
    370 
    371 
    372     switch ((index >> (6*N - 4)) & 3)
    373     {                                      /* (index >> ((6*N)-4)) & 3 */
    374         case 0:
    375             dec_5p_5N(index >> N, n_1, offsetA, pos);  /* dec_5p_5N(index>>N, n_1, offsetA, pos); */
    376             dec_1p_N1(index, n_1, offsetA, pos + 5);
    377             break;
    378         case 1:
    379             dec_5p_5N(index >> N, n_1, offsetA, pos);  /* dec_5p_5N(index>>N, n_1, offsetA, pos); */
    380             dec_1p_N1(index, n_1, offsetB, pos + 5);
    381             break;
    382         case 2:
    383             dec_4p_4N(index >> (2*n_1 + 1), n_1, offsetA, pos); /* dec_4p_4N(index>>((2*n_1)+1 ), n_1, offsetA, pos); */
    384             dec_2p_2N1(index, n_1, offsetB, pos + 4);
    385             break;
    386         case 3:
    387             dec_3p_3N1(index >> (3*n_1 + 1), n_1, offset, pos); /* dec_3p_3N1(index>>((3*n_1)+ 1), n_1, offset, pos); */
    388             dec_3p_3N1(index, n_1, j, pos + 3);
    389             break;
    390     }
    391     return;
    392 }
    393