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.073
     22     ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
     23     Available from http://www.3gpp.org
     24 
     25 (C) 2004, 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  Pathname: ./audio/gsm-amr/c/src/enc_lag3.c
     35  Functions:
     36 
     37      Date: 01/28/2002
     38 
     39 ------------------------------------------------------------------------------
     40  REVISION HISTORY
     41 
     42  Description:  Replaced "int" and/or "char" with OSCL defined types.
     43 
     44  Description:
     45 
     46 ------------------------------------------------------------------------------
     47 */
     48 
     49 /*----------------------------------------------------------------------------
     50 ; INCLUDES
     51 ----------------------------------------------------------------------------*/
     52 #include "enc_lag3.h"
     53 #include "typedef.h"
     54 #include "basic_op.h"
     55 #include "cnst.h"
     56 
     57 /*----------------------------------------------------------------------------
     58 ; MACROS
     59 ; Define module specific macros here
     60 ----------------------------------------------------------------------------*/
     61 
     62 /*----------------------------------------------------------------------------
     63 ; DEFINES
     64 ; Include all pre-processor statements here. Include conditional
     65 ; compile variables also.
     66 ----------------------------------------------------------------------------*/
     67 
     68 
     69 /*----------------------------------------------------------------------------
     70 ; LOCAL FUNCTION DEFINITIONS
     71 ; Function Prototype declaration
     72 ----------------------------------------------------------------------------*/
     73 
     74 
     75 /*----------------------------------------------------------------------------
     76 ; LOCAL VARIABLE DEFINITIONS
     77 ; Variable declaration - defined here and used outside this module
     78 ----------------------------------------------------------------------------*/
     79 
     80 /*
     81 ------------------------------------------------------------------------------
     82  FUNCTION NAME: enc_lag3
     83 ------------------------------------------------------------------------------
     84  INPUT AND OUTPUT DEFINITIONS
     85 
     86  Inputs:
     87   T0 = Pitch delay of type Word16
     88   T0_frac = Fractional pitch delay of type Word16
     89   T0_prev = Integer pitch delay of last subframe of type Word16
     90   T0_min  = minimum of search range of type Word16
     91   T0_max  = maximum of search range of type Word16
     92   delta_flag = Flag for 1st (or 3rd) subframe of type Word16
     93   flag4   = Flag for encoding with 4 bits of type Word16
     94   pOverflow = pointer indicating overflow of type Flag
     95 
     96  Outputs:
     97   pOverflow = 1 if there is an overflow else it is zero.
     98 
     99  Returns:
    100   None
    101 
    102  Global Variables Used:
    103   None
    104 
    105  Local Variables Needed:
    106   None
    107 
    108 ------------------------------------------------------------------------------
    109  FUNCTION DESCRIPTION
    110 
    111  This function implements the encoding of fractional pitch lag with
    112  1/3 resolution.
    113 
    114  *   FUNCTION:  Enc_lag3
    115  *
    116  *   PURPOSE:  Encoding of fractional pitch lag with 1/3 resolution.
    117  *
    118  *   DESCRIPTION:
    119  *                    First and third subframes:
    120  *                    --------------------------
    121  *   The pitch range is divided as follows:
    122  *           19 1/3  to   84 2/3   resolution 1/3
    123  *           85      to   143      resolution 1
    124  *
    125  *   The period is encoded with 8 bits.
    126  *   For the range with fractions:
    127  *     index = (T-19)*3 + frac - 1;
    128  *                         where T=[19..85] and frac=[-1,0,1]
    129  *   and for the integer only range
    130  *     index = (T - 85) + 197;        where T=[86..143]
    131  *
    132  *                    Second and fourth subframes:
    133  *                    ----------------------------
    134  *   For the 2nd and 4th subframes a resolution of 1/3 is always used,
    135  *   and the search range is relative to the lag in previous subframe.
    136  *   If t0 is the lag in the previous subframe then
    137  *   t_min=t0-5   and  t_max=t0+4   and  the range is given by
    138  *        t_min - 2/3   to  t_max + 2/3
    139  *
    140  *   The period in the 2nd (and 4th) subframe is encoded with 5 bits:
    141  *     index = (T-(t_min-1))*3 + frac - 1;
    142  *                 where T=[t_min-1..t_max+1]
    143 
    144 ------------------------------------------------------------------------------
    145  REQUIREMENTS
    146 
    147  None
    148 
    149 ------------------------------------------------------------------------------
    150  REFERENCES
    151 
    152  enc_lag3.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
    153 
    154 ------------------------------------------------------------------------------
    155  PSEUDO-CODE
    156 
    157    Word16 index, i, tmp_ind, uplag;
    158    Word16 tmp_lag;
    159 
    160    if (delta_flag == 0)
    161    {  // if 1st or 3rd subframe
    162 
    163       // encode pitch delay (with fraction)
    164 
    165       if (sub (T0, 85) <= 0)
    166       {
    167          // index = T0*3 - 58 + T0_frac
    168          i = add (add (T0, T0), T0);
    169          index = add (sub (i, 58), T0_frac);
    170       }
    171       else
    172       {
    173          index = add (T0, 112);
    174       }
    175    }
    176    else
    177    {   // if second or fourth subframe
    178       if (flag4 == 0) {
    179 
    180          // 'normal' encoding: either with 5 or 6 bit resolution
    181 
    182          // index = 3*(T0 - T0_min) + 2 + T0_frac
    183          i = sub (T0, T0_min);
    184          i = add (add (i, i), i);
    185          index = add (add (i, 2), T0_frac);
    186       }
    187       else {
    188 
    189          // encoding with 4 bit resolution
    190 
    191          tmp_lag = T0_prev;
    192 
    193          if ( sub( sub(tmp_lag, T0_min), 5) > 0)
    194             tmp_lag = add (T0_min, 5);
    195          if ( sub( sub(T0_max, tmp_lag), 4) > 0)
    196             tmp_lag = sub (T0_max, 4);
    197 
    198          uplag = add (add (add (T0, T0), T0), T0_frac);
    199 
    200          i = sub (tmp_lag, 2);
    201          tmp_ind = add (add (i, i), i);
    202 
    203          if (sub (tmp_ind, uplag) >= 0) {
    204             index = add (sub (T0, tmp_lag), 5);
    205          }
    206          else {
    207 
    208             i = add (tmp_lag, 1);
    209             i = add (add (i, i), i);
    210 
    211             if (sub (i, uplag) > 0) {
    212 
    213                 index = add ( sub (uplag, tmp_ind), 3);
    214             }
    215             else {
    216 
    217                index = add (sub (T0, tmp_lag), 11);
    218             }
    219          }
    220 
    221       } // end if (encoding with 4 bit resolution)
    222    }   // end if (second of fourth subframe)
    223 
    224    return index;
    225 }
    226 
    227 ------------------------------------------------------------------------------
    228  RESOURCES USED [optional]
    229 
    230  When the code is written for a specific target processor the
    231  the resources used should be documented below.
    232 
    233  HEAP MEMORY USED: x bytes
    234 
    235  STACK MEMORY USED: x bytes
    236 
    237  CLOCK CYCLES: (cycle count equation for this function) + (variable
    238                 used to represent cycle count for each subroutine
    239                 called)
    240      where: (cycle count variable) = cycle count for [subroutine
    241                                      name]
    242 
    243 ------------------------------------------------------------------------------
    244  CAUTION [optional]
    245  [State any special notes, constraints or cautions for users of this function]
    246 
    247 ------------------------------------------------------------------------------
    248 */
    249 
    250 
    251 Word16 Enc_lag3(         /* o  : Return index of encoding             */
    252     Word16 T0,           /* i  : Pitch delay                          */
    253     Word16 T0_frac,      /* i  : Fractional pitch delay               */
    254     Word16 T0_prev,      /* i  : Integer pitch delay of last subframe */
    255     Word16 T0_min,       /* i  : minimum of search range              */
    256     Word16 T0_max,       /* i  : maximum of search range              */
    257     Word16 delta_flag,   /* i  : Flag for 1st (or 3rd) subframe       */
    258     Word16 flag4,        /* i  : Flag for encoding with 4 bits        */
    259     Flag   *pOverflow
    260 )
    261 {
    262     Word16 index, i, tmp_ind, uplag;
    263     Word16 tmp_lag;
    264     Word16 temp1;
    265     Word16 temp2;
    266 
    267 
    268 
    269     if (delta_flag == 0)
    270     {  /* if 1st or 3rd subframe */
    271 
    272         /* encode pitch delay (with fraction) */
    273         temp1 = sub(T0, 85, pOverflow);
    274         if (temp1 <= 0)
    275         {
    276             /* index = T0*3 - 58 + T0_frac   */
    277             temp2 = add(T0, T0, pOverflow);
    278             i = add(temp2, T0, pOverflow);
    279             temp2 = sub(i, 58, pOverflow);
    280             index = add(temp2, T0_frac, pOverflow);
    281         }
    282         else
    283         {
    284             index = add(T0, 112, pOverflow);
    285         }
    286     }
    287     else
    288     {   /* if second or fourth subframe */
    289         if (flag4 == 0)
    290         {
    291 
    292             /* 'normal' encoding: either with 5 or 6 bit resolution */
    293 
    294             /* index = 3*(T0 - T0_min) + 2 + T0_frac */
    295             i = sub(T0, T0_min, pOverflow);
    296             temp2 = add(i, i, pOverflow);
    297             i = add(temp2, i, pOverflow);
    298             temp2 = add(i, 2, pOverflow);
    299             index = add(temp2, T0_frac, pOverflow);
    300         }
    301         else
    302         {
    303 
    304             /* encoding with 4 bit resolution */
    305 
    306             tmp_lag = T0_prev;
    307             temp1 = sub(tmp_lag, T0_min, pOverflow);
    308             temp2 = sub(temp1, 5, pOverflow);
    309             if (temp2 > 0)
    310                 tmp_lag = add(T0_min, 5, pOverflow);
    311             temp1 = sub(T0_max, tmp_lag, pOverflow);
    312             temp2 = sub(temp1, 4, pOverflow);
    313             if (temp2 > 0)
    314                 tmp_lag = sub(T0_max, 4, pOverflow);
    315 
    316             temp1 = add(T0, T0, pOverflow);
    317             temp2 = add(temp1, T0, pOverflow);
    318             uplag = add(temp2, T0_frac, pOverflow);
    319 
    320             i = sub(tmp_lag, 2, pOverflow);
    321             temp1 = add(i, i, pOverflow);
    322             tmp_ind = add(temp1, i, pOverflow);
    323 
    324             temp1 = sub(tmp_ind, uplag, pOverflow);
    325             if (temp1 >= 0)
    326             {
    327                 temp1 = sub(T0, tmp_lag, pOverflow);
    328                 index = add(temp1, 5, pOverflow);
    329             }
    330             else
    331             {
    332 
    333                 i = add(tmp_lag, 1, pOverflow);
    334                 temp1 = add(i, i, pOverflow);
    335                 i = add(temp1, i, pOverflow);
    336 
    337                 if (sub(i, uplag, pOverflow) > 0)
    338                 {
    339                     temp1 = sub(uplag, tmp_ind, pOverflow);
    340                     index = add(temp1, 3, pOverflow);
    341                 }
    342                 else
    343                 {
    344                     temp1 = sub(T0, tmp_lag, pOverflow);
    345                     index = add(temp1, 11, pOverflow);
    346                 }
    347             }
    348 
    349         } /* end if (encoding with 4 bit resolution) */
    350     }   /* end if (second of fourth subframe) */
    351 
    352     return index;
    353 }
    354 
    355 
    356 
    357