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_gain2_amr_wb.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      int16 index,                 (i)     : index of quantization.
     48      int16 nbits,                 (i)     : number of bits (6 or 7)
     49      int16 code[],                (i) Q9  : Innovative vector.
     50      int16 L_subfr,               (i)     : Subframe lenght.
     51      int16 * gain_pit,            (o) Q14 : Pitch gain.
     52      int32 * gain_cod,            (o) Q16 : Code gain.
     53      int16 bfi,                   (i)     : bad frame indicator
     54      int16 prev_bfi,              (i)     : Previous BF indicator
     55      int16 state,                 (i)     : State of BFH
     56      int16 unusable_frame,        (i)     : UF indicator
     57      int16 vad_hist,              (i)     : number of non-speech frames
     58      int16 * mem                  (i/o)   : static memory (4 words)
     59 
     60 ------------------------------------------------------------------------------
     61  FUNCTION DESCRIPTION
     62 
     63     Decode the pitch and codebook gains
     64 
     65 ------------------------------------------------------------------------------
     66  REQUIREMENTS
     67 
     68 
     69 ------------------------------------------------------------------------------
     70  REFERENCES
     71 
     72 ------------------------------------------------------------------------------
     73  PSEUDO-CODE
     74 
     75 ------------------------------------------------------------------------------
     76 */
     77 
     78 
     79 /*----------------------------------------------------------------------------
     80 ; INCLUDES
     81 ----------------------------------------------------------------------------*/
     82 
     83 
     84 #include "pv_amr_wb_type_defs.h"
     85 #include "pvamrwbdecoder_basic_op.h"
     86 #include "pvamrwb_math_op.h"
     87 #include "pvamrwbdecoder_cnst.h"
     88 #include "pvamrwbdecoder_acelp.h"
     89 
     90 #include "qisf_ns.h"
     91 
     92 /*----------------------------------------------------------------------------
     93 ; MACROS
     94 ; Define module specific macros here
     95 ----------------------------------------------------------------------------*/
     96 
     97 
     98 /*----------------------------------------------------------------------------
     99 ; DEFINES
    100 ; Include all pre-processor statements here. Include conditional
    101 ; compile variables also.
    102 ----------------------------------------------------------------------------*/
    103 
    104 #define MEAN_ENER    30
    105 #define PRED_ORDER   4
    106 
    107 #define L_LTPHIST 5
    108 
    109 /*----------------------------------------------------------------------------
    110 ; LOCAL FUNCTION DEFINITIONS
    111 ; Function Prototype declaration
    112 ----------------------------------------------------------------------------*/
    113 
    114 /*----------------------------------------------------------------------------
    115 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
    116 ; Variable declaration - defined here and used outside this module
    117 ----------------------------------------------------------------------------*/
    118 
    119 const int16 pdown_unusable[7] = {32767, 31130, 29491, 24576, 7537, 1638, 328};
    120 const int16 cdown_unusable[7] = {32767, 16384, 8192, 8192, 8192, 4915, 3277};
    121 
    122 const int16 pdown_usable[7] = {32767, 32113, 31457, 24576, 7537, 1638, 328};
    123 const int16 cdown_usable[7] = {32767, 32113, 32113, 32113, 32113, 32113, 22938};
    124 
    125 
    126 /* MA prediction coeff ={0.5, 0.4, 0.3, 0.2} in Q13 */
    127 const int16 pred[PRED_ORDER] = {4096, 3277, 2458, 1638};
    128 
    129 /*----------------------------------------------------------------------------
    130 ; EXTERNAL FUNCTION REFERENCES
    131 ; Declare functions defined elsewhere and referenced in this module
    132 ----------------------------------------------------------------------------*/
    133 
    134 /*----------------------------------------------------------------------------
    135 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
    136 ; Declare variables used in this module but defined elsewhere
    137 ----------------------------------------------------------------------------*/
    138 
    139 /*----------------------------------------------------------------------------
    140 ; FUNCTION CODE
    141 ----------------------------------------------------------------------------*/
    142 
    143 
    144 /* output  :static memory (4 words)      */
    145 void dec_gain2_amr_wb_init(int16 * mem)
    146 {
    147 
    148     /* 4nd order quantizer energy predictor (init to -14.0 in Q10) */
    149     mem[0] = -14336;                          /* past_qua_en[0] */
    150     mem[1] = -14336;                          /* past_qua_en[1] */
    151     mem[2] = -14336;                          /* past_qua_en[2] */
    152     mem[3] = -14336;                          /* past_qua_en[3] */
    153     /* 4  *past_gain_pit  */
    154     /* 5  *past_gain_code  */
    155     /* 6  *prev_gc  */
    156     /* next 5  pbuf[]  */
    157     /* next 5  gbuf[]  */
    158     /* next 5  pbuf2[]  */
    159     pv_memset((void *)&mem[4], 0, 18*sizeof(*mem));
    160 
    161     mem[22] = 21845;
    162 
    163 }
    164 
    165 /*----------------------------------------------------------------------------
    166 ; FUNCTION CODE
    167 ----------------------------------------------------------------------------*/
    168 
    169 void dec_gain2_amr_wb(
    170     int16 index,               /* (i)     : index of quantization.      */
    171     int16 nbits,               /* (i)     : number of bits (6 or 7)     */
    172     int16 code[],              /* (i) Q9  : Innovative vector.          */
    173     int16 L_subfr,             /* (i)     : Subframe lenght.            */
    174     int16 * gain_pit,          /* (o) Q14 : Pitch gain.                 */
    175     int32 * gain_cod,          /* (o) Q16 : Code gain.                  */
    176     int16 bfi,                 /* (i)     : bad frame indicator         */
    177     int16 prev_bfi,            /* (i)     : Previous BF indicator       */
    178     int16 state,               /* (i)     : State of BFH                */
    179     int16 unusable_frame,      /* (i)     : UF indicator                */
    180     int16 vad_hist,            /* (i)     : number of non-speech frames */
    181     int16 * mem                /* (i/o)   : static memory (4 words)     */
    182 )
    183 {
    184     const int16 *p;
    185     int16 *past_gain_pit, *past_gain_code, *past_qua_en, *gbuf, *pbuf, *prev_gc;
    186     int16 *pbuf2;
    187     int16 i, tmp, exp, frac, gcode0, exp_gcode0, qua_ener, gcode_inov;
    188     int16 tmp1, g_code;
    189     int16 tmp2;
    190     int32 L_tmp;
    191 
    192     past_qua_en = mem;
    193     past_gain_pit = mem + 4;
    194     past_gain_code = mem + 5;
    195     prev_gc = mem + 6;
    196     pbuf = mem + 7;
    197     gbuf = mem + 12;
    198     pbuf2 = mem + 17;
    199 
    200     /*
    201      *  Find energy of code and compute:
    202      *
    203      *    L_tmp = 1.0 / sqrt(energy of code/ L_subfr)
    204      */
    205 
    206     L_tmp = Dot_product12(code, code, L_subfr, &exp);
    207     exp -= 24;                /* exp: -18 (code in Q9), -6 (/L_subfr) */
    208 
    209     one_ov_sqrt_norm(&L_tmp, &exp);
    210 
    211     gcode_inov = extract_h(shl_int32(L_tmp, exp - 3));  /* g_code_inov in Q12 */
    212 
    213     /*
    214      * Case of erasure.
    215      */
    216 
    217     if (bfi != 0)
    218     {
    219         tmp = median5(&pbuf[2]);
    220         *past_gain_pit = tmp;
    221 
    222         if (*past_gain_pit > 15565)
    223         {
    224             *past_gain_pit = 15565;        /* 0.95 in Q14 */
    225 
    226         }
    227 
    228         if (unusable_frame != 0)
    229         {
    230             *gain_pit = mult_int16(pdown_unusable[state], *past_gain_pit);
    231         }
    232         else
    233         {
    234             *gain_pit = mult_int16(pdown_usable[state], *past_gain_pit);
    235         }
    236         tmp = median5(&gbuf[2]);
    237 
    238         if (vad_hist > 2)
    239         {
    240             *past_gain_code = tmp;
    241         }
    242         else
    243         {
    244 
    245             if (unusable_frame != 0)
    246             {
    247                 *past_gain_code = mult_int16(cdown_unusable[state], tmp);
    248             }
    249             else
    250             {
    251                 *past_gain_code = mult_int16(cdown_usable[state], tmp);
    252             }
    253         }
    254 
    255         /* update table of past quantized energies */
    256 
    257         tmp  = past_qua_en[3];
    258         tmp1 = past_qua_en[2];
    259         L_tmp  = tmp;
    260         L_tmp += tmp1;
    261         past_qua_en[3] = tmp;
    262         tmp  = past_qua_en[1];
    263         tmp1 = past_qua_en[0];
    264         L_tmp += tmp;
    265         L_tmp += tmp1;
    266         past_qua_en[2] = tmp;
    267         qua_ener = (int16)(L_tmp >> 3);
    268         past_qua_en[1] = tmp1;
    269 
    270 
    271         qua_ener -= 3072;    /* -3 in Q10 */
    272 
    273         if (qua_ener < -14336)
    274         {
    275             qua_ener = -14336;                /* -14 in Q10 */
    276         }
    277 
    278         past_qua_en[0] = qua_ener;
    279 
    280 
    281         for (i = 1; i < 5; i++)
    282         {
    283             gbuf[i - 1] = gbuf[i];
    284             pbuf[i - 1] = pbuf[i];
    285         }
    286         gbuf[4] = *past_gain_code;
    287         pbuf[4] = *past_gain_pit;
    288 
    289 
    290         /* adjust gain according to energy of code */
    291         /* past_gain_code(Q3) * gcode_inov(Q12) => Q16 */
    292         *gain_cod = mul_16by16_to_int32(*past_gain_code, gcode_inov);
    293 
    294         return;
    295     }
    296     /*
    297      * Compute gcode0
    298      *  = Sum(i=0,1) pred[i]*past_qua_en[i] + mean_ener - ener_code
    299      */
    300 
    301     L_tmp = L_deposit_h(MEAN_ENER);        /* MEAN_ENER in Q16 */
    302     L_tmp = shl_int32(L_tmp, 8);               /* From Q16 to Q24 */
    303     L_tmp = mac_16by16_to_int32(L_tmp, pred[0], past_qua_en[0]);      /* Q13*Q10 -> Q24 */
    304     L_tmp = mac_16by16_to_int32(L_tmp, pred[1], past_qua_en[1]);      /* Q13*Q10 -> Q24 */
    305     L_tmp = mac_16by16_to_int32(L_tmp, pred[2], past_qua_en[2]);      /* Q13*Q10 -> Q24 */
    306     L_tmp = mac_16by16_to_int32(L_tmp, pred[3], past_qua_en[3]);      /* Q13*Q10 -> Q24 */
    307 
    308     gcode0 = extract_h(L_tmp);             /* From Q24 to Q8  */
    309 
    310     /*
    311      * gcode0 = pow(10.0, gcode0/20)
    312      *        = pow(2, 3.321928*gcode0/20)
    313      *        = pow(2, 0.166096*gcode0)
    314      */
    315 
    316     L_tmp = ((int32)gcode0 * 5443) >> 7;      /* *0.166096 in Q15 -> Q24     */
    317 
    318     int32_to_dpf(L_tmp, &exp_gcode0, &frac);  /* Extract exponant of gcode0  */
    319 
    320     gcode0 = (int16)(power_of_2(14, frac));    /* Put 14 as exponant so that  */
    321     /* output of Pow2() will be:   */
    322     /* 16384 < Pow2() <= 32767     */
    323     exp_gcode0 -= 14;
    324 
    325     /* Read the quantized gains */
    326 
    327     if (nbits == 6)
    328     {
    329         p = &t_qua_gain6b[index<<1];
    330     }
    331     else
    332     {
    333         p = &t_qua_gain7b[index<<1];
    334     }
    335     *gain_pit = *p++;                         /* selected pitch gain in Q14 */
    336     g_code = *p++;                            /* selected code gain in Q11  */
    337 
    338     L_tmp = mul_16by16_to_int32(g_code, gcode0);        /* Q11*Q0 -> Q12 */
    339     L_tmp = shl_int32(L_tmp, exp_gcode0 + 4);   /* Q12 -> Q16 */
    340 
    341     *gain_cod = L_tmp;                        /* gain of code in Q16 */
    342 
    343     if (prev_bfi == 1)
    344     {
    345         L_tmp = mul_16by16_to_int32(*prev_gc, 5120);    /* prev_gc(Q3) * 1.25(Q12) = Q16 */
    346         /* if((*gain_cod > ((*prev_gc) * 1.25)) && (*gain_cod > 100.0)) */
    347 
    348         if ((*gain_cod > L_tmp) && (*gain_cod > 6553600))
    349         {
    350             *gain_cod = L_tmp;
    351         }
    352     }
    353     /* keep past gain code in Q3 for frame erasure (can saturate) */
    354     *past_gain_code = amr_wb_round(shl_int32(*gain_cod, 3));
    355     *past_gain_pit = *gain_pit;
    356 
    357 
    358     *prev_gc = *past_gain_code;
    359     tmp  = gbuf[1];
    360     tmp1 = pbuf[1];
    361     tmp2 = pbuf2[1];
    362     for (i = 1; i < 5; i++)
    363     {
    364         gbuf[i - 1]  = tmp;
    365         pbuf[i - 1]  = tmp1;
    366         pbuf2[i - 1] = tmp2;
    367         tmp  = gbuf[i];
    368         tmp1 = pbuf[i];
    369         tmp2 = pbuf2[i];
    370     }
    371     gbuf[4] = *past_gain_code;
    372     pbuf[4] = *past_gain_pit;
    373     pbuf2[4] = *past_gain_pit;
    374 
    375 
    376     /* adjust gain according to energy of code */
    377     int32_to_dpf(*gain_cod, &exp, &frac);
    378     L_tmp = mul_32by16(exp, frac, gcode_inov);
    379 
    380     *gain_cod = shl_int32(L_tmp, 3);              /* gcode_inov in Q12 */
    381 
    382 
    383     past_qua_en[3] = past_qua_en[2];
    384     past_qua_en[2] = past_qua_en[1];
    385     past_qua_en[1] = past_qua_en[0];
    386 
    387     /*
    388      * qua_ener = 20*log10(g_code)
    389      *          = 6.0206*log2(g_code)
    390      *          = 6.0206*(log2(g_codeQ11) - 11)
    391      */
    392     L_tmp = (int32)g_code;
    393     amrwb_log_2(L_tmp, &exp, &frac);
    394     exp -= 11;
    395     L_tmp = mul_32by16(exp, frac, 24660);   /* x 6.0206 in Q12 */
    396 
    397     /* update table of past quantized energies */
    398 
    399     past_qua_en[0] = (int16)(L_tmp >> 3); /* result in Q10 */
    400 
    401     return;
    402 }
    403 
    404 
    405