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/lpc.c
     35 
     36      Date: 01/31/2002
     37 
     38 ------------------------------------------------------------------------------
     39  REVISION HISTORY
     40 
     41  Description: Updating includes and making code more simple as per comments.
     42 
     43  Description:  Replaced OSCL mem type functions and eliminated include
     44                files that now are chosen by OSCL definitions
     45 
     46  Description:  Replaced "int" and/or "char" with OSCL defined types.
     47 
     48  Description:
     49 
     50 ------------------------------------------------------------------------------
     51 */
     52 
     53 /*----------------------------------------------------------------------------
     54 ; INCLUDES
     55 ----------------------------------------------------------------------------*/
     56 #include <stdlib.h>
     57 
     58 #include "lpc.h"
     59 #include "typedef.h"
     60 #include "oper_32b.h"
     61 #include "autocorr.h"
     62 #include "lag_wind.h"
     63 #include "levinson.h"
     64 #include "cnst.h"
     65 #include "mode.h"
     66 #include "window_tab.h"
     67 #include "sub.h"
     68 
     69 /*----------------------------------------------------------------------------
     70 ; MACROS
     71 ; Define module specific macros here
     72 ----------------------------------------------------------------------------*/
     73 
     74 
     75 /*----------------------------------------------------------------------------
     76 ; DEFINES
     77 ; Include all pre-processor statements here. Include conditional
     78 ; compile variables also.
     79 ----------------------------------------------------------------------------*/
     80 
     81 
     82 /*----------------------------------------------------------------------------
     83 ; LOCAL FUNCTION DEFINITIONS
     84 ; Function Prototype declaration
     85 ----------------------------------------------------------------------------*/
     86 
     87 
     88 /*----------------------------------------------------------------------------
     89 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
     90 ; Variable declaration - defined here and used outside this module
     91 ----------------------------------------------------------------------------*/
     92 
     93 /*
     94 ------------------------------------------------------------------------------
     95  FUNCTION NAME: lpc_init
     96 ------------------------------------------------------------------------------
     97  INPUT AND OUTPUT DEFINITIONS
     98 
     99  Inputs:
    100     state = pointer to pointer of state data of type lpcState
    101 
    102  Outputs:
    103     None
    104 
    105  Returns:
    106     None
    107 
    108  Global Variables Used:
    109     None.
    110 
    111  Local Variables Needed:
    112     None.
    113 
    114 ------------------------------------------------------------------------------
    115  FUNCTION DESCRIPTION
    116 
    117  This function initializes the state data for the LPC module.
    118 
    119 ------------------------------------------------------------------------------
    120  REQUIREMENTS
    121 
    122  None.
    123 
    124 ------------------------------------------------------------------------------
    125  REFERENCES
    126 
    127  lpc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
    128 
    129 ------------------------------------------------------------------------------
    130  PSEUDO-CODE
    131 
    132 
    133   lpcState* s;
    134 
    135   if (state == (lpcState **) NULL){
    136       // fprintf(stderr, "lpc_init: invalid parameter\n");
    137       return -1;
    138   }
    139   *state = NULL;
    140 
    141   // allocate memory
    142   if ((s= (lpcState *) malloc(sizeof(lpcState))) == NULL){
    143       // fprintf(stderr, "lpc_init: can not malloc state structure\n");
    144       return -1;
    145   }
    146 
    147   s->levinsonSt = NULL;
    148 
    149   // Init sub states
    150   if (Levinson_init(&s->levinsonSt)) {
    151      lpc_exit(&s);
    152      return -1;
    153   }
    154 
    155 
    156   lpc_reset(s);
    157   *state = s;
    158 
    159   return 0;
    160 
    161 ------------------------------------------------------------------------------
    162  RESOURCES USED [optional]
    163 
    164  When the code is written for a specific target processor the
    165  the resources used should be documented below.
    166 
    167  HEAP MEMORY USED: x bytes
    168 
    169  STACK MEMORY USED: x bytes
    170 
    171  CLOCK CYCLES: (cycle count equation for this function) + (variable
    172                 used to represent cycle count for each subroutine
    173                 called)
    174      where: (cycle count variable) = cycle count for [subroutine
    175                                      name]
    176 
    177 ------------------------------------------------------------------------------
    178  CAUTION [optional]
    179  [State any special notes, constraints or cautions for users of this function]
    180 
    181 ------------------------------------------------------------------------------
    182 */
    183 Word16 lpc_init(lpcState **state)
    184 {
    185     lpcState* s;
    186 
    187     if (state == (lpcState **) NULL)
    188     {
    189         /* fprintf(stderr, "lpc_init: invalid parameter\n"); */
    190         return -1;
    191     }
    192     *state = NULL;
    193 
    194     /* allocate memory */
    195     if ((s = (lpcState *) malloc(sizeof(lpcState))) == NULL)
    196     {
    197         /* fprintf(stderr, "lpc_init: can not malloc state structure\n"); */
    198         return -1;
    199     }
    200 
    201     s->levinsonSt = NULL;
    202 
    203     /* Init sub states */
    204     if (Levinson_init(&s->levinsonSt))
    205     {
    206         lpc_exit(&s);
    207         return -1;
    208     }
    209 
    210     lpc_reset(s);
    211     *state = s;
    212 
    213     return 0;
    214 }
    215 
    216 /*
    217 ------------------------------------------------------------------------------
    218  FUNCTION NAME: lpc_reset
    219 ------------------------------------------------------------------------------
    220  INPUT AND OUTPUT DEFINITIONS
    221 
    222  Inputs:
    223     state = pointer to pointer of state data of type lpcState
    224 
    225  Outputs:
    226     None
    227 
    228  Returns:
    229     None
    230 
    231  Global Variables Used:
    232     None.
    233 
    234  Local Variables Needed:
    235     None.
    236 
    237 ------------------------------------------------------------------------------
    238  FUNCTION DESCRIPTION
    239 
    240  This function resets the state data for the LPC module.
    241 
    242 ------------------------------------------------------------------------------
    243  REQUIREMENTS
    244 
    245  None.
    246 
    247 ------------------------------------------------------------------------------
    248  REFERENCES
    249 
    250  lpc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
    251 
    252 ------------------------------------------------------------------------------
    253  PSEUDO-CODE
    254 
    255   if (state == (lpcState *) NULL){
    256       // fprintf(stderr, "lpc_reset: invalid parameter\n");
    257       return -1;
    258   }
    259 
    260   Levinson_reset(state->levinsonSt);
    261 
    262   return 0;
    263 
    264 ------------------------------------------------------------------------------
    265  RESOURCES USED [optional]
    266 
    267  When the code is written for a specific target processor the
    268  the resources used should be documented below.
    269 
    270  HEAP MEMORY USED: x bytes
    271 
    272  STACK MEMORY USED: x bytes
    273 
    274  CLOCK CYCLES: (cycle count equation for this function) + (variable
    275                 used to represent cycle count for each subroutine
    276                 called)
    277      where: (cycle count variable) = cycle count for [subroutine
    278                                      name]
    279 
    280 ------------------------------------------------------------------------------
    281  CAUTION [optional]
    282  [State any special notes, constraints or cautions for users of this function]
    283 
    284 ------------------------------------------------------------------------------
    285 */
    286 Word16 lpc_reset(lpcState *state)
    287 {
    288 
    289     if (state == (lpcState *) NULL)
    290     {
    291         /* fprintf(stderr, "lpc_reset: invalid parameter\n"); */
    292         return -1;
    293     }
    294 
    295     Levinson_reset(state->levinsonSt);
    296 
    297     return 0;
    298 }
    299 
    300 /*
    301 ------------------------------------------------------------------------------
    302  FUNCTION NAME: lpc_exit
    303 ------------------------------------------------------------------------------
    304  INPUT AND OUTPUT DEFINITIONS
    305 
    306  Inputs:
    307     state = pointer to pointer of state data of type lpcState
    308 
    309  Outputs:
    310     None
    311 
    312  Returns:
    313     None
    314 
    315  Global Variables Used:
    316     None.
    317 
    318  Local Variables Needed:
    319     None.
    320 
    321 ------------------------------------------------------------------------------
    322  FUNCTION DESCRIPTION
    323 
    324  This function frees the state data for the LPC module.
    325 
    326 ------------------------------------------------------------------------------
    327  REQUIREMENTS
    328 
    329  None.
    330 
    331 ------------------------------------------------------------------------------
    332  REFERENCES
    333 
    334  lpc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
    335 
    336 ------------------------------------------------------------------------------
    337  PSEUDO-CODE
    338 
    339 
    340   if (state == NULL || *state == NULL)
    341       return;
    342 
    343   Levinson_exit(&(*state)->levinsonSt);
    344 
    345   // deallocate memory
    346   free(*state);
    347   *state = NULL;
    348 
    349   return;
    350 
    351 
    352 ------------------------------------------------------------------------------
    353  RESOURCES USED [optional]
    354 
    355  When the code is written for a specific target processor the
    356  the resources used should be documented below.
    357 
    358  HEAP MEMORY USED: x bytes
    359 
    360  STACK MEMORY USED: x bytes
    361 
    362  CLOCK CYCLES: (cycle count equation for this function) + (variable
    363                 used to represent cycle count for each subroutine
    364                 called)
    365      where: (cycle count variable) = cycle count for [subroutine
    366                                      name]
    367 
    368 ------------------------------------------------------------------------------
    369  CAUTION [optional]
    370  [State any special notes, constraints or cautions for users of this function]
    371 
    372 ------------------------------------------------------------------------------
    373 */
    374 void lpc_exit(lpcState **state)
    375 {
    376     if (state == NULL || *state == NULL)
    377         return;
    378 
    379     Levinson_exit(&(*state)->levinsonSt);
    380 
    381     /* deallocate memory */
    382     free(*state);
    383     *state = NULL;
    384 
    385     return;
    386 }
    387 
    388 
    389 /*
    390 ------------------------------------------------------------------------------
    391  FUNCTION NAME: lpc
    392 ------------------------------------------------------------------------------
    393  INPUT AND OUTPUT DEFINITIONS
    394 
    395  Inputs:
    396     state = pointer to state data of type lpcState
    397     mode  = coder mode of type enum Mode
    398     x[]   = pointer to input signal (Q15) of type Word16
    399     x_12k2[] = pointer to input signal (EFR) (Q15) of type Word16
    400     pOverflow = pointer to overflow indicator of type Flag
    401 
    402  Outputs:
    403     a[]   = pointer to predictor coefficients (Q12) of type Word16
    404 
    405  Returns:
    406     None
    407 
    408  Global Variables Used:
    409     None.
    410 
    411  Local Variables Needed:
    412     None.
    413 
    414 ------------------------------------------------------------------------------
    415  FUNCTION DESCRIPTION
    416 
    417  This function executes the LPC functionality for GSM AMR.
    418 
    419 ------------------------------------------------------------------------------
    420  REQUIREMENTS
    421 
    422  None.
    423 
    424 ------------------------------------------------------------------------------
    425  REFERENCES
    426 
    427  lpc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
    428 
    429 ------------------------------------------------------------------------------
    430  PSEUDO-CODE
    431 
    432    Word16 rc[4];                  // First 4 reflection coefficients Q15
    433    Word16 rLow[MP1], rHigh[MP1];  // Autocorrelations low and hi
    434                                   // No fixed Q value but normalized
    435                                   // so that overflow is avoided
    436 
    437    if ( sub ((Word16)mode, (Word16)MR122) == 0)
    438    {
    439        // Autocorrelations
    440        Autocorr(x_12k2, M, rHigh, rLow, window_160_80);
    441        // Lag windowing
    442        Lag_window(M, rHigh, rLow);
    443        // Levinson Durbin
    444        Levinson(st->levinsonSt, rHigh, rLow, &a[MP1], rc);
    445 
    446        // Autocorrelations
    447        Autocorr(x_12k2, M, rHigh, rLow, window_232_8);
    448        // Lag windowing
    449        Lag_window(M, rHigh, rLow);
    450        // Levinson Durbin
    451        Levinson(st->levinsonSt, rHigh, rLow, &a[MP1 * 3], rc);
    452    }
    453    else
    454    {
    455        // Autocorrelations
    456        Autocorr(x, M, rHigh, rLow, window_200_40);
    457        // Lag windowing
    458        Lag_window(M, rHigh, rLow);
    459        // Levinson Durbin
    460        Levinson(st->levinsonSt, rHigh, rLow, &a[MP1 * 3], rc);
    461    }
    462 
    463    return 0;
    464 
    465 ------------------------------------------------------------------------------
    466  RESOURCES USED [optional]
    467 
    468  When the code is written for a specific target processor the
    469  the resources used should be documented below.
    470 
    471  HEAP MEMORY USED: x bytes
    472 
    473  STACK MEMORY USED: x bytes
    474 
    475  CLOCK CYCLES: (cycle count equation for this function) + (variable
    476                 used to represent cycle count for each subroutine
    477                 called)
    478      where: (cycle count variable) = cycle count for [subroutine
    479                                      name]
    480 
    481 ------------------------------------------------------------------------------
    482  CAUTION [optional]
    483  [State any special notes, constraints or cautions for users of this function]
    484 
    485 ------------------------------------------------------------------------------
    486 */
    487 void lpc(
    488     lpcState *st,     /* i/o: State struct                */
    489     enum Mode mode,   /* i  : coder mode                  */
    490     Word16 x[],       /* i  : Input signal           Q15  */
    491     Word16 x_12k2[],  /* i  : Input signal (EFR)     Q15  */
    492     Word16 a[],       /* o  : predictor coefficients Q12  */
    493     Flag   *pOverflow
    494 )
    495 {
    496     Word16 rc[4];                  /* First 4 reflection coefficients Q15 */
    497     Word16 rLow[MP1], rHigh[MP1];  /* Autocorrelations low and hi      */
    498     /* No fixed Q value but normalized  */
    499     /* so that overflow is avoided      */
    500 
    501     if (mode == MR122)
    502     {
    503         /* Autocorrelations */
    504         Autocorr(x_12k2, M, rHigh, rLow, window_160_80, pOverflow);
    505         /* Lag windowing    */
    506         Lag_window(M, rHigh, rLow, pOverflow);
    507         /* Levinson Durbin  */
    508         Levinson(st->levinsonSt, rHigh, rLow, &a[MP1], rc, pOverflow);
    509 
    510         /* Autocorrelations */
    511         Autocorr(x_12k2, M, rHigh, rLow, window_232_8, pOverflow);
    512         /* Lag windowing    */
    513         Lag_window(M, rHigh, rLow, pOverflow);
    514         /* Levinson Durbin  */
    515         Levinson(st->levinsonSt, rHigh, rLow, &a[MP1 * 3], rc, pOverflow);
    516     }
    517     else
    518     {
    519         /* Autocorrelations */
    520         Autocorr(x, M, rHigh, rLow, window_200_40, pOverflow);
    521         /* Lag windowing    */
    522         Lag_window(M, rHigh, rLow, pOverflow);
    523         /* Levinson Durbin  */
    524         Levinson(st->levinsonSt, rHigh, rLow, &a[MP1 * 3], rc, pOverflow);
    525     }
    526 
    527 }
    528 
    529 
    530 
    531 
    532 
    533 
    534 
    535 
    536 
    537 
    538 
    539 
    540 
    541 
    542 
    543