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  Filename:  /audio/gsm-amr/c/src/a_refl.c
     35  Functions: a_refl
     36 
     37      Date: 02/05/2002
     38 
     39 ------------------------------------------------------------------------------
     40  REVISION HISTORY
     41 
     42  Description: Removing unneeded include files and the goto statement.
     43 
     44 
     45  Description: Changed function name to pv_round to avoid conflict with
     46               round function in C standard library.
     47 
     48  Description:  Replaced "int" and/or "char" with OSCL defined types.
     49 
     50  Description:  Using inline functions from basic_op.h .
     51                Removing unneeded include files.
     52 
     53  Description:
     54 
     55 ------------------------------------------------------------------------------
     56 */
     57 
     58 
     59 /*----------------------------------------------------------------------------
     60 ; INCLUDES
     61 ----------------------------------------------------------------------------*/
     62 #include "a_refl.h"
     63 #include "typedef.h"
     64 #include "cnst.h"
     65 #include "basic_op.h"
     66 
     67 /*----------------------------------------------------------------------------
     68 ; MACROS [optional]
     69 ; [Define module specific macros here]
     70 ----------------------------------------------------------------------------*/
     71 
     72 /*----------------------------------------------------------------------------
     73 ; DEFINES [optional]
     74 ; [Include all pre-processor statements here. Include conditional
     75 ; compile variables also.]
     76 ----------------------------------------------------------------------------*/
     77 
     78 /*----------------------------------------------------------------------------
     79 ; LOCAL FUNCTION DEFINITIONS
     80 ; [List function prototypes here]
     81 ----------------------------------------------------------------------------*/
     82 
     83 /*----------------------------------------------------------------------------
     84 ; LOCAL VARIABLE DEFINITIONS
     85 ; [Variable declaration - defined here and used outside this module]
     86 ----------------------------------------------------------------------------*/
     87 
     88 /*
     89 ------------------------------------------------------------------------------
     90  FUNCTION NAME: AMREncode
     91 ------------------------------------------------------------------------------
     92  INPUT AND OUTPUT DEFINITIONS
     93 
     94  Inputs:
     95     a[] = pointer to directform coefficients of type Word16
     96     refl[] = pointer to reflection coefficients of type Word16
     97 
     98  Outputs:
     99     pOverflow = 1 if overflow exists in the math operations else zero.
    100 
    101  Returns:
    102     None
    103 
    104  Global Variables Used:
    105     None
    106 
    107  Local Variables Needed:
    108     None
    109 
    110 ------------------------------------------------------------------------------
    111  FUNCTION DESCRIPTION
    112 
    113      File             : a_refl.c
    114      Purpose          : Convert from direct form coefficients to
    115                         reflection coefficients
    116 
    117 ------------------------------------------------------------------------------
    118  REQUIREMENTS
    119 
    120  None
    121 
    122 ------------------------------------------------------------------------------
    123  REFERENCES
    124 
    125  [1] a_refl.c , 3GPP TS 26.101 version 4.1.0 Release 4, June 2001
    126 
    127 ------------------------------------------------------------------------------
    128  PSEUDO-CODE
    129 
    130 
    131 void A_Refl(
    132    Word16 a[],        // i   : Directform coefficients
    133    Word16 refl[]      // o   : Reflection coefficients
    134 )
    135 {
    136    // local variables
    137    Word16 i,j;
    138    Word16 aState[M];
    139    Word16 bState[M];
    140    Word16 normShift;
    141    Word16 normProd;
    142    Word32 L_acc;
    143    Word16 scale;
    144    Word32 L_temp;
    145    Word16 temp;
    146    Word16 mult;
    147 
    148    // initialize states
    149    for (i = 0; i < M; i++)
    150    {
    151       aState[i] = a[i];
    152    }
    153 
    154    // backward Levinson recursion
    155    for (i = M-1; i >= 0; i--)
    156    {
    157       if (sub(abs_s(aState[i]), 4096) >= 0)
    158       {
    159          goto ExitRefl;
    160       }
    161 
    162       refl[i] = shl(aState[i], 3);
    163 
    164       L_temp = L_mult(refl[i], refl[i]);
    165       L_acc = L_sub(MAX_32, L_temp);
    166 
    167       normShift = norm_l(L_acc);
    168       scale = sub(15, normShift);
    169 
    170       L_acc = L_shl(L_acc, normShift);
    171       normProd = pv_round(L_acc);
    172 
    173       mult = div_s(16384, normProd);
    174 
    175       for (j = 0; j < i; j++)
    176       {
    177          L_acc = L_deposit_h(aState[j]);
    178          L_acc = L_msu(L_acc, refl[i], aState[i-j-1]);
    179 
    180          temp = pv_round(L_acc);
    181          L_temp = L_mult(mult, temp);
    182          L_temp = L_shr_r(L_temp, scale);
    183 
    184          if (L_sub(L_abs(L_temp), 32767) > 0)
    185          {
    186             goto ExitRefl;
    187          }
    188 
    189          bState[j] = extract_l(L_temp);
    190       }
    191 
    192       for (j = 0; j < i; j++)
    193       {
    194          aState[j] = bState[j];
    195       }
    196    }
    197    return;
    198 
    199 ExitRefl:
    200    for (i = 0; i < M; i++)
    201    {
    202       refl[i] = 0;
    203    }
    204 }
    205 
    206 ------------------------------------------------------------------------------
    207  RESOURCES USED [optional]
    208 
    209  When the code is written for a specific target processor the
    210  the resources used should be documented below.
    211 
    212  HEAP MEMORY USED: x bytes
    213 
    214  STACK MEMORY USED: x bytes
    215 
    216  CLOCK CYCLES: (cycle count equation for this function) + (variable
    217                 used to represent cycle count for each subroutine
    218                 called)
    219      where: (cycle count variable) = cycle count for [subroutine
    220                                      name]
    221 
    222 ------------------------------------------------------------------------------
    223  CAUTION [optional]
    224  [State any special notes, constraints or cautions for users of this function]
    225 
    226 ------------------------------------------------------------------------------
    227 */
    228 
    229 void A_Refl(
    230     Word16 a[],        /* i   : Directform coefficients */
    231     Word16 refl[],     /* o   : Reflection coefficients */
    232     Flag   *pOverflow
    233 )
    234 {
    235     /* local variables */
    236     Word16 i;
    237     Word16 j;
    238     Word16 aState[M];
    239     Word16 bState[M];
    240     Word16 normShift;
    241     Word16 normProd;
    242     Word32 L_acc;
    243     Word16 scale;
    244     Word32 L_temp;
    245     Word16 temp;
    246     Word16 mult;
    247 
    248     /* initialize states */
    249     for (i = 0; i < M; i++)
    250     {
    251         aState[i] = a[i];
    252     }
    253 
    254     /* backward Levinson recursion */
    255     for (i = M - 1; i >= 0; i--)
    256     {
    257         if (abs_s(aState[i]) >= 4096)
    258         {
    259             for (i = 0; i < M; i++)
    260             {
    261                 refl[i] = 0;
    262             }
    263             break;
    264         }
    265 
    266         refl[i] = shl(aState[i], 3, pOverflow);
    267 
    268         L_temp = L_mult(refl[i], refl[i], pOverflow);
    269         L_acc = L_sub(MAX_32, L_temp, pOverflow);
    270 
    271         normShift = norm_l(L_acc);
    272         scale = sub(15, normShift, pOverflow);
    273 
    274         L_acc = L_shl(L_acc, normShift, pOverflow);
    275         normProd = pv_round(L_acc, pOverflow);
    276 
    277         mult = div_s(16384, normProd);
    278 
    279         for (j = 0; j < i; j++)
    280         {
    281             L_acc = L_deposit_h(aState[j]);
    282             L_acc = L_msu(L_acc, refl[i], aState[i-j-1], pOverflow);
    283 
    284             temp = pv_round(L_acc, pOverflow);
    285             L_temp = L_mult(mult, temp, pOverflow);
    286             L_temp = L_shr_r(L_temp, scale, pOverflow);
    287 
    288             if (L_abs(L_temp) > 32767)
    289             {
    290                 for (i = 0; i < M; i++)
    291                 {
    292                     refl[i] = 0;
    293                 }
    294                 break;
    295             }
    296 
    297             bState[j] = extract_l(L_temp);
    298         }
    299 
    300         for (j = 0; j < i; j++)
    301         {
    302             aState[j] = bState[j];
    303         }
    304     }
    305     return;
    306 }
    307 
    308 
    309 
    310 
    311 
    312 
    313 
    314