qgain475.cpp

来自「实现3GPP的GSM中AMR语音的CODECS。」· C++ 代码 · 共 1,476 行 · 第 1/4 页

CPP
1,476
字号
    18072,        10754,         16625,     6845,    3134,         2298,         10869,     2437,    15580,         6913,         12597,     3381,    11116,         3297,         16762,     2424,    18853,         6715,         17171,     9887,    12743,         2605,          8937,     3140,    19033,         7764,         18347,     3880,    20475,         3682,         19602,     3380,    13044,        19373,         10526,    23124};/*------------------------------------------------------------------------------ FUNCTION NAME: MR475_quant_store_results------------------------------------------------------------------------------ INPUT AND OUTPUT DEFINITIONS Inputs:    pred_st = pointer to structure of type gc_predState    p = pointer to selected quantizer table entry (const Word16)    gcode0 = predicted CB gain (Word16)    exp_gcode0 = exponent of predicted CB gain (Word16)    gain_pit = pointer to Pitch gain (Word16)    gain_cod = pointer to Code gain (Word16) Outputs:    pred_st points to the updated structure of type gc_predState    gain_pit points to Pitch gain    gain_cod points to Code gain    pOverflow points to overflow indicator (Flag) Returns:    None. Global Variables Used:    None. Local Variables Needed:    None.------------------------------------------------------------------------------ FUNCTION DESCRIPTION This function calculates the final fixed codebook gain and the predictor update values, and updates the gain predictor.------------------------------------------------------------------------------ REQUIREMENTS None.------------------------------------------------------------------------------ REFERENCES qgain475.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001------------------------------------------------------------------------------ PSEUDO-CODEstatic void MR475_quant_store_results(    gc_predState *pred_st, // i/o: gain predictor state struct    const Word16 *p,       // i  : pointer to selected quantizer table entry    Word16 gcode0,         // i  : predicted CB gain,     Q(14 - exp_gcode0)    Word16 exp_gcode0,     // i  : exponent of predicted CB gain,        Q0    Word16 *gain_pit,      // o  : Pitch gain,                           Q14    Word16 *gain_cod       // o  : Code gain,                            Q1){    Word16 g_code, exp, frac, tmp;    Word32 L_tmp;    Word16 qua_ener_MR122; // o  : quantized energy error, MR122 version Q10    Word16 qua_ener;       // o  : quantized energy error,               Q10    // Read the quantized gains    *gain_pit = *p++;    g_code = *p++;    //------------------------------------------------------------------*     *  calculate final fixed codebook gain:                            *     *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                            *     *                                                                  *     *   gc = gc0 * g                                                   *     *------------------------------------------------------------------    L_tmp = L_mult(g_code, gcode0);    L_tmp = L_shr(L_tmp, sub(10, exp_gcode0));    *gain_cod = extract_h(L_tmp);    //------------------------------------------------------------------*     *  calculate predictor update values and update gain predictor:    *     *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    *     *                                                                  *     *   qua_ener       = log2(g)                                       *     *   qua_ener_MR122 = 20*log10(g)                                   *     *------------------------------------------------------------------    Log2 (L_deposit_l (g_code), &exp, &frac); // Log2(x Q12) = log2(x) + 12    exp = sub(exp, 12);    tmp = shr_r (frac, 5);    qua_ener_MR122 = add (tmp, shl (exp, 10));    L_tmp = Mpy_32_16(exp, frac, 24660); // 24660 Q12 ~= 6.0206 = 20*log10(2)    qua_ener = pv_round (L_shl (L_tmp, 13)); // Q12 * Q0 = Q13 -> Q10    gc_pred_update(pred_st, qua_ener_MR122, qua_ener);}------------------------------------------------------------------------------ RESOURCES USED [optional] When the code is written for a specific target processor the the resources used should be documented below. HEAP MEMORY USED: x bytes STACK MEMORY USED: x bytes CLOCK CYCLES: (cycle count equation for this function) + (variable                used to represent cycle count for each subroutine                called)     where: (cycle count variable) = cycle count for [subroutine                                     name]------------------------------------------------------------------------------ CAUTION [optional] [State any special notes, constraints or cautions for users of this function]------------------------------------------------------------------------------*/static void MR475_quant_store_results(    gc_predState *pred_st, /* i/o: gain predictor state struct               */    const Word16 *p,       /* i  : pointer to selected quantizer table entry */    Word16 gcode0,         /* i  : predicted CB gain,     Q(14 - exp_gcode0) */    Word16 exp_gcode0,     /* i  : exponent of predicted CB gain,        Q0  */    Word16 *gain_pit,      /* o  : Pitch gain,                           Q14 */    Word16 *gain_cod,      /* o  : Code gain,                            Q1  */    Flag   *pOverflow      /* o  : overflow indicator                        */){    Word16 g_code;    Word16 exp;    Word16 frac;    Word16 tmp;    Word32 L_tmp;    Word16 qua_ener_MR122; /* o  : quantized energy error, MR122 version Q10 */    Word16 qua_ener;       /* o  : quantized energy error,               Q10 */    /* Read the quantized gains */    *gain_pit = *p++;    g_code = *p++;    /*------------------------------------------------------------------*     *  calculate final fixed codebook gain:                            *     *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                            *     *                                                                  *     *   gc = gc0 * g                                                   *     *------------------------------------------------------------------*/    L_tmp = ((Word32) g_code * gcode0) << 1;    tmp   = 10 - exp_gcode0;    L_tmp = L_shr(L_tmp, tmp, pOverflow);    *gain_cod = (Word16)(L_tmp >> 16);    /*------------------------------------------------------------------*     *  calculate predictor update values and update gain predictor:    *     *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    *     *                                                                  *     *   qua_ener       = log2(g)                                       *     *   qua_ener_MR122 = 20*log10(g)                                   *     *------------------------------------------------------------------*/    /* Log2(x Q12) = log2(x) + 12 */    Log2((Word32) g_code, &exp, &frac, pOverflow);    exp -= 12;    tmp = shr_r(frac, 5, pOverflow);    qua_ener_MR122 = exp << 10;    qua_ener_MR122 = tmp + qua_ener_MR122;    /* 24660 Q12 ~= 6.0206 = 20*log10(2) */    L_tmp = Mpy_32_16(exp, frac, 24660, pOverflow);    L_tmp = L_tmp << 13;    /* Q12 * Q0 = Q13 -> Q10 */    qua_ener = (Word16)((L_tmp + (Word32) 0x00008000L) >> 16);    gc_pred_update(pred_st, qua_ener_MR122, qua_ener);    return;}/****************************************************************************//*------------------------------------------------------------------------------ FUNCTION NAME: MR475_update_unq_pred------------------------------------------------------------------------------ INPUT AND OUTPUT DEFINITIONS Inputs:    pred_st = pointer to structure of type gc_predState    exp_gcode0 = predicted CB gain (exponent MSW) (Word16)    frac_gcode0 = predicted CB gain (exponent LSW) (Word16)    cod_gain_exp = optimum codebook gain (exponent)(Word16)    cod_gain_frac = optimum codebook gain (fraction) (Word16) Outputs:    pred_st points to the updated structure of type gc_predState    pOverflow points to overflow indicator (Flag) Returns:    None. Global Variables Used:    None. Local Variables Needed:    None.------------------------------------------------------------------------------ FUNCTION DESCRIPTION This module uses the optimum codebook gain and updates the "unquantized" gain predictor with the (bounded) prediction error.------------------------------------------------------------------------------ REQUIREMENTS None.------------------------------------------------------------------------------ REFERENCES qgain475.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001------------------------------------------------------------------------------ PSEUDO-CODEvoidMR475_update_unq_pred(    gc_predState *pred_st, // i/o: gain predictor state struct    Word16 exp_gcode0,     // i  : predicted CB gain (exponent MSW),  Q0    Word16 frac_gcode0,    // i  : predicted CB gain (exponent LSW),  Q15    Word16 cod_gain_exp,   // i  : optimum codebook gain (exponent),  Q0    Word16 cod_gain_frac   // i  : optimum codebook gain (fraction),  Q15){    Word16 tmp, exp, frac;    Word16 qua_ener, qua_ener_MR122;    Word32 L_tmp;    // calculate prediction error factor (given optimum CB gain gcu):    //   predErrFact = gcu / gcode0    //   (limit to MIN_PRED_ERR_FACT <= predErrFact <= MAX_PRED_ERR_FACT    //    -> limit qua_ener*)    //    // calculate prediction error (log):    //    //   qua_ener_MR122 = log2(predErrFact)    //   qua_ener       = 20*log10(predErrFact)    if (cod_gain_frac <= 0)    {        // if gcu <= 0 -> predErrFact = 0 < MIN_PRED_ERR_FACT        // -> set qua_ener(_MR122) directly        qua_ener = MIN_QUA_ENER;        qua_ener_MR122 = MIN_QUA_ENER_MR122;    }    else    {        // convert gcode0 from DPF to standard fraction/exponent format        // with normalized frac, i.e. 16384 <= frac <= 32767        // Note: exponent correction (exp=exp-14) is done after div_s        frac_gcode0 = extract_l (Pow2 (14, frac_gcode0));        // make sure cod_gain_frac < frac_gcode0  for div_s        if (sub(cod_gain_frac, frac_gcode0) >= 0)        {            cod_gain_frac = shr (cod_gain_frac, 1);            cod_gain_exp = add (cod_gain_exp, 1);        }        // predErrFact        //   = gcu / gcode0        //   = cod_gain_frac/frac_gcode0 * 2^(cod_gain_exp-(exp_gcode0-14))        //   = div_s (c_g_f, frac_gcode0)*2^-15 * 2^(c_g_e-exp_gcode0+14)        //   = div_s * 2^(cod_gain_exp-exp_gcode0 - 1)        frac = div_s (cod_gain_frac, frac_gcode0);        tmp = sub (sub (cod_gain_exp, exp_gcode0), 1);        Log2 (L_deposit_l (frac), &exp, &frac);        exp = add (exp, tmp);        // calculate prediction error (log2, Q10)        qua_ener_MR122 = shr_r (frac, 5);        qua_ener_MR122 = add (qua_ener_MR122, shl (exp, 10));        if (sub(qua_ener_MR122, MIN_QUA_ENER_MR122) < 0)        {            qua_ener = MIN_QUA_ENER;            qua_ener_MR122 = MIN_QUA_ENER_MR122;        }        else if (sub(qua_ener_MR122, MAX_QUA_ENER_MR122) > 0)        {            qua_ener = MAX_QUA_ENER;            qua_ener_MR122 = MAX_QUA_ENER_MR122;        }        else        {            // calculate prediction error (20*log10, Q10)            L_tmp = Mpy_32_16(exp, frac, 24660);            // 24660 Q12 ~= 6.0206 = 20*log10(2)            qua_ener = pv_round (L_shl (L_tmp, 13));            // Q12 * Q0 = Q13 -> Q26 -> Q10        }    }    // update MA predictor memory    gc_pred_update(pred_st, qua_ener_MR122, qua_ener);}------------------------------------------------------------------------------ RESOURCES USED [optional] When the code is written for a specific target processor the the resources used should be documented below. HEAP MEMORY USED: x bytes STACK MEMORY USED: x bytes CLOCK CYCLES: (cycle count equation for this function) + (variable                used to represent cycle count for each subroutine                called)     where: (cycle count variable) = cycle count for [subroutine                                     name]------------------------------------------------------------------------------ CAUTION [optional] [State any special notes, constraints or cautions for users of this function]------------------------------------------------------------------------------*/void MR475_update_unq_pred(    gc_predState *pred_st, /* i/o: gain predictor state struct            */    Word16 exp_gcode0,     /* i  : predicted CB gain (exponent MSW),  Q0  */    Word16 frac_gcode0,    /* i  : predicted CB gain (exponent LSW),  Q15 */    Word16 cod_gain_exp,   /* i  : optimum codebook gain (exponent),  Q0  */    Word16 cod_gain_frac,  /* i  : optimum codebook gain (fraction),  Q15 */    Flag   *pOverflow      /* o  : overflow indicator                     */){    Word16 tmp;    Word16 exp;    Word16 frac;    Word16 qua_ener;    Word16 qua_ener_MR122;    Word32 L_tmp;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?