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/ton_stab.c
     35  Funtions:
     36 
     37      Date: 02/06/2002
     38 
     39 ------------------------------------------------------------------------------
     40  REVISION HISTORY
     41 
     42  Description:  For check_lsp()
     43               1. Eliminated unused include files.
     44               2. Replaced array addressing by pointers
     45               3. Eliminated math operations that unnecessary checked for
     46                  saturation this by evaluating the operands
     47                For update_gp_clipping()
     48               1. Replaced copy() with more efficient memcpy()
     49               2. Replaced right shift function with right shift
     50 
     51  Description:  Replaced OSCL mem type functions and eliminated include
     52                files that now are chosen by OSCL definitions
     53 
     54  Description:  Replaced "int" and/or "char" with OSCL defined types.
     55 
     56  Description:
     57 
     58 ------------------------------------------------------------------------------
     59 */
     60 
     61 
     62 /*----------------------------------------------------------------------------
     63 ; INCLUDES
     64 ----------------------------------------------------------------------------*/
     65 #include <stdlib.h>
     66 
     67 #include "ton_stab.h"
     68 #include "oper_32b.h"
     69 #include "cnst.h"
     70 #include "set_zero.h"
     71 #include "basic_op.h"
     72 
     73 /*----------------------------------------------------------------------------
     74 ; MACROS
     75 ; Define module specific macros here
     76 ----------------------------------------------------------------------------*/
     77 
     78 
     79 /*----------------------------------------------------------------------------
     80 ; DEFINES
     81 ; Include all pre-processor statements here. Include conditional
     82 ; compile variables also.
     83 ----------------------------------------------------------------------------*/
     84 
     85 
     86 /*----------------------------------------------------------------------------
     87 ; LOCAL FUNCTION DEFINITIONS
     88 ; Function Prototype declaration
     89 ----------------------------------------------------------------------------*/
     90 
     91 /*----------------------------------------------------------------------------
     92 ; LOCAL VARIABLE DEFINITIONS
     93 ; Variable declaration - defined here and used outside this module
     94 ----------------------------------------------------------------------------*/
     95 
     96 
     97 /*
     98 ------------------------------------------------------------------------------
     99  FUNCTION NAME: ton_stab_init
    100 ------------------------------------------------------------------------------
    101  INPUT AND OUTPUT DEFINITIONS
    102 
    103  Inputs:
    104     state = pointer to pointer to structure type tonStabState.
    105 
    106  Outputs:
    107     None
    108 
    109  Returns:
    110     None.
    111 
    112  Global Variables Used:
    113     None
    114 
    115  Local Variables Needed:
    116     None
    117 
    118 ------------------------------------------------------------------------------
    119  FUNCTION DESCRIPTION
    120 
    121   Function:   ton_stab_init
    122   Purpose:    Allocates state memory and initializes state memory
    123 
    124 ------------------------------------------------------------------------------
    125  REQUIREMENTS
    126 
    127   None.
    128 
    129 ------------------------------------------------------------------------------
    130  REFERENCES
    131 
    132  ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
    133 
    134 ------------------------------------------------------------------------------
    135  PSEUDO-CODE
    136 
    137 int ton_stab_init (tonStabState **state)
    138 {
    139     tonStabState* s;
    140 
    141     if (state == (tonStabState **) NULL){
    142         // fprintf(stderr, "ton_stab_init: invalid parameter\n");
    143         return -1;
    144     }
    145     *state = NULL;
    146 
    147     // allocate memory
    148     if ((s= (tonStabState *) malloc(sizeof(tonStabState))) == NULL){
    149         // fprintf(stderr, "ton_stab_init: can not malloc state structure\n");
    150         return -1;
    151     }
    152 
    153     ton_stab_reset(s);
    154 
    155     *state = s;
    156 
    157     return 0;
    158 }
    159 
    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 
    184 Word16 ton_stab_init(tonStabState **state)
    185 {
    186     tonStabState* s;
    187 
    188     if (state == (tonStabState **) NULL)
    189     {
    190         /* fprintf(stderr, "ton_stab_init: invalid parameter\n"); */
    191         return -1;
    192     }
    193     *state = NULL;
    194 
    195     /* allocate memory */
    196     if ((s = (tonStabState *) malloc(sizeof(tonStabState))) == NULL)
    197     {
    198         /* fprintf(stderr, "ton_stab_init: can not malloc state structure\n"); */
    199         return -1;
    200     }
    201 
    202     ton_stab_reset(s);
    203 
    204     *state = s;
    205 
    206     return 0;
    207 }
    208 
    209 /****************************************************************************/
    210 
    211 
    212 /*
    213 ------------------------------------------------------------------------------
    214  FUNCTION NAME: ton_stab_reset
    215 ------------------------------------------------------------------------------
    216  INPUT AND OUTPUT DEFINITIONS
    217 
    218  Inputs:
    219     st = pointer to pointer to structure type tonStabState.
    220 
    221  Outputs:
    222     None
    223 
    224  Returns:
    225     None.
    226 
    227  Global Variables Used:
    228     None
    229 
    230  Local Variables Needed:
    231     None
    232 
    233 ------------------------------------------------------------------------------
    234  FUNCTION DESCRIPTION
    235 
    236   Function:   ton_stab_reset
    237   Purpose:    Initializes state memory to zero
    238 
    239 ------------------------------------------------------------------------------
    240  REQUIREMENTS
    241 
    242  None.
    243 
    244 ------------------------------------------------------------------------------
    245  REFERENCES
    246 
    247  ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
    248 
    249 ------------------------------------------------------------------------------
    250  PSEUDO-CODE
    251 
    252 int ton_stab_reset (tonStabState *st)
    253 {
    254     if (st == (tonStabState *) NULL){
    255         // fprintf(stderr, "ton_stab_init: invalid parameter\n");
    256         return -1;
    257     }
    258 
    259     // initialize tone stabilizer state
    260     st->count = 0;
    261     Set_zero(st->gp, N_FRAME);    // Init Gp_Clipping
    262 
    263     return 0;
    264 }
    265 
    266 ------------------------------------------------------------------------------
    267  RESOURCES USED [optional]
    268 
    269  When the code is written for a specific target processor the
    270  the resources used should be documented below.
    271 
    272  HEAP MEMORY USED: x bytes
    273 
    274  STACK MEMORY USED: x bytes
    275 
    276  CLOCK CYCLES: (cycle count equation for this function) + (variable
    277                 used to represent cycle count for each subroutine
    278                 called)
    279      where: (cycle count variable) = cycle count for [subroutine
    280                                      name]
    281 
    282 ------------------------------------------------------------------------------
    283  CAUTION [optional]
    284  [State any special notes, constraints or cautions for users of this function]
    285 
    286 ------------------------------------------------------------------------------
    287 */
    288 
    289 Word16 ton_stab_reset(tonStabState *st)
    290 {
    291     if (st == (tonStabState *) NULL)
    292     {
    293         /* fprintf(stderr, "ton_stab_init: invalid parameter\n"); */
    294         return -1;
    295     }
    296 
    297     /* initialize tone stabilizer state */
    298     st->count = 0;
    299     Set_zero(st->gp, N_FRAME);    /* Init Gp_Clipping */
    300 
    301     return 0;
    302 }
    303 
    304 /****************************************************************************/
    305 
    306 /*
    307 ------------------------------------------------------------------------------
    308  FUNCTION NAME: ton_stab_exit
    309 ------------------------------------------------------------------------------
    310  INPUT AND OUTPUT DEFINITIONS
    311 
    312  Inputs:
    313     state = pointer to pointer to structure type tonStabState.
    314 
    315  Outputs:
    316     None
    317 
    318  Returns:
    319     None.
    320 
    321  Global Variables Used:
    322     None
    323 
    324  Local Variables Needed:
    325     None
    326 
    327 ------------------------------------------------------------------------------
    328  FUNCTION DESCRIPTION
    329 
    330   Function:   ton_stab_exit
    331   Purpose:    The memory used for state memory is freed
    332 
    333 ------------------------------------------------------------------------------
    334  REQUIREMENTS
    335 
    336  None.
    337 
    338 ------------------------------------------------------------------------------
    339  REFERENCES
    340 
    341  ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
    342 
    343 ------------------------------------------------------------------------------
    344  PSEUDO-CODE
    345 
    346 void ton_stab_exit (tonStabState **state)
    347 {
    348     if (state == NULL || *state == NULL)
    349         return;
    350 
    351     // deallocate memory
    352     free(*state);
    353     *state = NULL;
    354 
    355     return;
    356 }
    357 
    358 ------------------------------------------------------------------------------
    359  RESOURCES USED [optional]
    360 
    361  When the code is written for a specific target processor the
    362  the resources used should be documented below.
    363 
    364  HEAP MEMORY USED: x bytes
    365 
    366  STACK MEMORY USED: x bytes
    367 
    368  CLOCK CYCLES: (cycle count equation for this function) + (variable
    369                 used to represent cycle count for each subroutine
    370                 called)
    371      where: (cycle count variable) = cycle count for [subroutine
    372                                      name]
    373 
    374 ------------------------------------------------------------------------------
    375  CAUTION [optional]
    376  [State any special notes, constraints or cautions for users of this function]
    377 
    378 ------------------------------------------------------------------------------
    379 */
    380 
    381 void ton_stab_exit(tonStabState **state)
    382 {
    383     if (state == NULL || *state == NULL)
    384         return;
    385 
    386     /* deallocate memory */
    387     free(*state);
    388     *state = NULL;
    389 
    390     return;
    391 }
    392 
    393 /****************************************************************************/
    394 
    395 /*
    396 ------------------------------------------------------------------------------
    397  FUNCTION NAME: check_lsp
    398 ------------------------------------------------------------------------------
    399  INPUT AND OUTPUT DEFINITIONS
    400 
    401  Inputs:
    402     state = pointer to pointer to structure type tonStabState.
    403     lsp   = pointer to unquantized LSPs of type Word16
    404 
    405  Outputs:
    406     pOverflow = 1 if there is an overflow else it is zero.
    407 
    408  Returns:
    409     None.
    410 
    411  Global Variables Used:
    412     None
    413 
    414  Local Variables Needed:
    415     None
    416 
    417 ------------------------------------------------------------------------------
    418  FUNCTION DESCRIPTION
    419 
    420   Function:  check_lsp()
    421   Purpose:   Check the LSP's to detect resonances
    422 
    423 ------------------------------------------------------------------------------
    424  REQUIREMENTS
    425 
    426  None.
    427 
    428 ------------------------------------------------------------------------------
    429  REFERENCES
    430 
    431  ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
    432 
    433 ------------------------------------------------------------------------------
    434  PSEUDO-CODE
    435 
    436 Word16 check_lsp(tonStabState *st, // i/o : State struct
    437                  Word16 *lsp       // i   : unquantized LSP's
    438 )
    439 {
    440    Word16 i, dist, dist_min1, dist_min2, dist_th;
    441 
    442    // Check for a resonance:
    443    // Find minimum distance between lsp[i] and lsp[i+1]
    444 
    445    dist_min1 = MAX_16;
    446    for (i = 3; i < M-2; i++)
    447    {
    448       dist = sub(lsp[i], lsp[i+1]);
    449 
    450       if (sub(dist, dist_min1) < 0)
    451       {
    452          dist_min1 = dist;
    453       }
    454    }
    455 
    456    dist_min2 = MAX_16;
    457    for (i = 1; i < 3; i++)
    458    {
    459       dist = sub(lsp[i], lsp[i+1]);
    460 
    461       if (sub(dist, dist_min2) < 0)
    462       {
    463          dist_min2 = dist;
    464       }
    465    }
    466 
    467    if (sub(lsp[1], 32000) > 0)
    468    {
    469       dist_th = 600;
    470    }
    471    else if (sub(lsp[1], 30500) > 0)
    472    {
    473       dist_th = 800;
    474    }
    475    else
    476    {
    477       dist_th = 1100;
    478    }
    479 
    480    if (sub(dist_min1, 1500) < 0 ||
    481        sub(dist_min2, dist_th) < 0)
    482    {
    483       st->count = add(st->count, 1);
    484    }
    485    else
    486    {
    487       st->count = 0;
    488    }
    489 
    490    // Need 12 consecutive frames to set the flag
    491    if (sub(st->count, 12) >= 0)
    492    {
    493       st->count = 12;
    494       return 1;
    495    }
    496    else
    497    {
    498       return 0;
    499    }
    500 }
    501 
    502 ------------------------------------------------------------------------------
    503  RESOURCES USED [optional]
    504 
    505  When the code is written for a specific target processor the
    506  the resources used should be documented below.
    507 
    508  HEAP MEMORY USED: x bytes
    509 
    510  STACK MEMORY USED: x bytes
    511 
    512  CLOCK CYCLES: (cycle count equation for this function) + (variable
    513                 used to represent cycle count for each subroutine
    514                 called)
    515      where: (cycle count variable) = cycle count for [subroutine
    516                                      name]
    517 
    518 ------------------------------------------------------------------------------
    519  CAUTION [optional]
    520  [State any special notes, constraints or cautions for users of this function]
    521 
    522 ------------------------------------------------------------------------------
    523 */
    524 
    525 Word16 check_lsp(tonStabState *st, /* i/o : State struct            */
    526                  Word16 *lsp,      /* i   : unquantized LSP's       */
    527                  Flag  *pOverflow
    528                 )
    529 {
    530     Word16 i;
    531     Word16 dist;
    532     Word16 dist_min1;
    533     Word16 dist_min2;
    534     Word16 dist_th;
    535     Word16 *p_lsp   = &lsp[3];
    536     Word16 *p_lsp_1 = &lsp[4];
    537 
    538     OSCL_UNUSED_ARG(pOverflow);
    539     /* Check for a resonance:                             */
    540     /* Find minimum distance between lsp[i] and lsp[i+1]  */
    541 
    542     dist_min1 = MAX_16;
    543     for (i = 3; i < M - 2; i++)
    544     {
    545         dist = *(p_lsp++) - *(p_lsp_1++);
    546 
    547         if (dist < dist_min1)
    548         {
    549             dist_min1 = dist;
    550         }
    551     }
    552 
    553     dist_min2 = MAX_16;
    554     p_lsp   = &lsp[1];
    555     p_lsp_1 = &lsp[2];
    556 
    557     for (i = 1; i < 3; i++)
    558     {
    559         dist = *(p_lsp++) - *(p_lsp_1++);
    560 
    561         if (dist < dist_min2)
    562         {
    563             dist_min2 = dist;
    564         }
    565     }
    566 
    567     if (lsp[1] > 32000)
    568     {
    569         dist_th = 600;
    570     }
    571     else if (lsp[1] > 30500)
    572     {
    573         dist_th = 800;
    574     }
    575     else
    576     {
    577         dist_th = 1100;
    578     }
    579 
    580     if ((dist_min1 < 1500) || (dist_min2 < dist_th))
    581     {
    582         st->count++;
    583     }
    584     else
    585     {
    586         st->count = 0;
    587     }
    588 
    589     /* Need 12 consecutive frames to set the flag */
    590     if (st->count >= 12)
    591     {
    592         st->count = 12;
    593         return 1;
    594     }
    595     else
    596     {
    597         return 0;
    598     }
    599 }
    600 
    601 /****************************************************************************/
    602 
    603 /*
    604 ------------------------------------------------------------------------------
    605  FUNCTION NAME: check_gp_clipping
    606 ------------------------------------------------------------------------------
    607  INPUT AND OUTPUT DEFINITIONS
    608 
    609  Inputs:
    610     state = pointer to pointer to structure type tonStabState.
    611     g_pitch = pitch gain of type Word16
    612 
    613  Outputs:
    614     pOverflow = 1 if there is an overflow else it is zero.
    615 
    616  Returns:
    617     None.
    618 
    619  Global Variables Used:
    620     None
    621 
    622  Local Variables Needed:
    623     None
    624 
    625 ------------------------------------------------------------------------------
    626  FUNCTION DESCRIPTION
    627 
    628   Function:   Check_Gp_Clipping()
    629   Purpose:    Verify that the sum of the last (N_FRAME+1) pitch
    630               gains is under a certain threshold.
    631 
    632 ------------------------------------------------------------------------------
    633  REQUIREMENTS
    634 
    635  None.
    636 
    637 ------------------------------------------------------------------------------
    638  REFERENCES
    639 
    640  ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
    641 
    642 ------------------------------------------------------------------------------
    643  PSEUDO-CODE
    644 
    645 Word16 check_gp_clipping(tonStabState *st, // i/o : State struct
    646                          Word16 g_pitch    // i   : pitch gain
    647 )
    648 {
    649    Word16 i, sum;
    650 
    651    sum = shr(g_pitch, 3);          // Division by 8
    652    for (i = 0; i < N_FRAME; i++)
    653    {
    654       sum = add(sum, st->gp[i]);
    655    }
    656 
    657    if (sub(sum, GP_CLIP) > 0)
    658    {
    659       return 1;
    660    }
    661    else
    662    {
    663       return 0;
    664    }
    665 }
    666 
    667 ------------------------------------------------------------------------------
    668  RESOURCES USED [optional]
    669 
    670  When the code is written for a specific target processor the
    671  the resources used should be documented below.
    672 
    673  HEAP MEMORY USED: x bytes
    674 
    675  STACK MEMORY USED: x bytes
    676 
    677  CLOCK CYCLES: (cycle count equation for this function) + (variable
    678                 used to represent cycle count for each subroutine
    679                 called)
    680      where: (cycle count variable) = cycle count for [subroutine
    681                                      name]
    682 
    683 ------------------------------------------------------------------------------
    684  CAUTION [optional]
    685  [State any special notes, constraints or cautions for users of this function]
    686 
    687 ------------------------------------------------------------------------------
    688 */
    689 
    690 Word16 check_gp_clipping(tonStabState *st, /* i/o : State struct            */
    691                          Word16 g_pitch,   /* i   : pitch gain              */
    692                          Flag   *pOverflow
    693                         )
    694 {
    695     Word16 i;
    696     Word16 sum;
    697 
    698     sum = shr(g_pitch, 3, pOverflow);        /* Division by 8 */
    699     for (i = 0; i < N_FRAME; i++)
    700     {
    701         sum = add(sum, st->gp[i], pOverflow);
    702     }
    703 
    704     if (sum > GP_CLIP)
    705     {
    706         return 1;
    707     }
    708     else
    709     {
    710         return 0;
    711     }
    712 }
    713 
    714 /****************************************************************************/
    715 
    716 /*
    717 ------------------------------------------------------------------------------
    718  FUNCTION NAME: update_gp_clipping
    719 ------------------------------------------------------------------------------
    720  INPUT AND OUTPUT DEFINITIONS
    721 
    722  Inputs:
    723     state = pointer to pointer to structure type tonStabState.
    724     g_pitch = pitch gain of type Word16
    725 
    726  Outputs:
    727     pOverflow = 1 if there is an overflow else it is zero.
    728 
    729  Returns:
    730     None.
    731 
    732  Global Variables Used:
    733     None
    734 
    735  Local Variables Needed:
    736     None
    737 
    738 ------------------------------------------------------------------------------
    739  FUNCTION DESCRIPTION
    740 
    741   Function:  Update_Gp_Clipping()
    742   Purpose:   Update past pitch gain memory
    743 
    744 ------------------------------------------------------------------------------
    745  REQUIREMENTS
    746 
    747  None.
    748 
    749 ------------------------------------------------------------------------------
    750  REFERENCES
    751 
    752  ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
    753 
    754 ------------------------------------------------------------------------------
    755  PSEUDO-CODE
    756 
    757 void update_gp_clipping(tonStabState *st, // i/o : State struct
    758                         Word16 g_pitch    // i   : pitch gain
    759 )
    760 {
    761    Copy(&st->gp[1], &st->gp[0], N_FRAME-1);
    762    st->gp[N_FRAME-1] = shr(g_pitch, 3);
    763 }
    764 
    765 ------------------------------------------------------------------------------
    766  RESOURCES USED [optional]
    767 
    768  When the code is written for a specific target processor the
    769  the resources used should be documented below.
    770 
    771  HEAP MEMORY USED: x bytes
    772 
    773  STACK MEMORY USED: x bytes
    774 
    775  CLOCK CYCLES: (cycle count equation for this function) + (variable
    776                 used to represent cycle count for each subroutine
    777                 called)
    778      where: (cycle count variable) = cycle count for [subroutine
    779                                      name]
    780 
    781 ------------------------------------------------------------------------------
    782  CAUTION [optional]
    783  [State any special notes, constraints or cautions for users of this function]
    784 
    785 ------------------------------------------------------------------------------
    786 */
    787 
    788 void update_gp_clipping(tonStabState *st, /* i/o : State struct            */
    789                         Word16 g_pitch,   /* i   : pitch gain              */
    790                         Flag   *pOverflow
    791                        )
    792 {
    793     OSCL_UNUSED_ARG(pOverflow);
    794     for (int i = 0; i < N_FRAME - 1; i++)
    795     {
    796         st->gp[i] = st->gp[i+1];
    797     }
    798     st->gp[N_FRAME-1] =  g_pitch >> 3;
    799 }
    800 
    801