📄 qgain795.cpp
字号:
/* ------------------------------------------------------------------ * Copyright (C) 2008 PacketVideo * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. * See the License for the specific language governing permissions * and limitations under the License. * ------------------------------------------------------------------- *//****************************************************************************************Portions of this file are derived from the following 3GPP standard: 3GPP TS 26.073 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec Available from http://www.3gpp.org(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)Permission to distribute, modify and use this file under the standard licenseterms listed above has been obtained from the copyright holder.****************************************************************************************//*------------------------------------------------------------------------------ Pathname: ./audio/gsm-amr/c/src/qgain795.c Functions: MR795_gain_code_quant3 MR795_gain_code_quant_mod MR795_gain_quant Date: 02/04/2002------------------------------------------------------------------------------ REVISION HISTORY Description: Updated template used to PV coding template. Changed to accept the pOverflow flag for EPOC compatibility. Description: (1) Removed optimization -- mult(i, 3, pOverflow) is NOT the same as adding i to itself 3 times. The reason is because the mult function does a right shift by 15, which will obliterate smaller numbers. Description: Replaced OSCL mem type functions and eliminated include files that now are chosen by OSCL definitions Description: Replaced "int" and/or "char" with OSCL defined types. Description: Changed round function name to pv_round to avoid conflict with round function in C standard library. Description: Added #ifdef __cplusplus around extern'ed table. Description:------------------------------------------------------------------------------ MODULE DESCRIPTION------------------------------------------------------------------------------*//*----------------------------------------------------------------------------; INCLUDES----------------------------------------------------------------------------*/#include "qgain795.h"#include "typedef.h"#include "basic_op.h"#include "cnst.h"#include "log2.h"#include "pow2.h"#include "sqrt_l.h"#include "g_adapt.h"#include "calc_en.h"#include "q_gain_p.h"/*--------------------------------------------------------------------------*/#ifdef __cplusplusextern "C"{#endif /*---------------------------------------------------------------------------- ; MACROS ; Define module specific macros here ----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------- ; DEFINES ; Include all pre-processor statements here. Include conditional ; compile variables also. ----------------------------------------------------------------------------*/#define NB_QUA_CODE 32 /*---------------------------------------------------------------------------- ; LOCAL FUNCTION DEFINITIONS ; Function Prototype declaration ----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------- ; LOCAL VARIABLE DEFINITIONS ; Variable declaration - defined here and used outside this module ----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------- ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES ; Declare variables used in this module but defined elsewhere ----------------------------------------------------------------------------*/ extern const Word16 qua_gain_code[NB_QUA_CODE*3]; /*--------------------------------------------------------------------------*/#ifdef __cplusplus}#endif/*------------------------------------------------------------------------------ FUNCTION NAME: MR795_gain_code_quant3------------------------------------------------------------------------------ INPUT AND OUTPUT DEFINITIONS Inputs: exp_gcode0 -- Word16 -- predicted CB gain (exponent), Q0 gcode0 -- Word16 -- predicted CB gain (norm.) g_pitch_cand[] -- Word16 array -- Pitch gain candidates (3), Q14 g_pitch_cind[] -- Word16 array -- Pitch gain cand. indices (3), Q0 frac_coeff[] -- Word16 array -- coefficients (5), Q15 exp_coeff[] -- Word16 array -- energy coefficients (5), Q0 coefficients from calc_filt_ener() Outputs: gain_pit -- Pointer to Word16 -- Pitch gain, Q14 gain_pit_ind -- Pointer to Word16 -- Pitch gain index, Q0 gain_cod -- Pointer to Word16 -- Code gain, Q1 gain_cod_ind -- Pointer to Word16 -- Code gain index, Q0 qua_ener_MR122 -- Pointer to Word16 -- quantized energy error, Q10 (for MR122 MA predictor update) qua_ener -- Pointer to Word16 -- quantized energy error, Q10 (for other MA predictor update) pOverflow -- Pointer to Flag -- overflow indicator Returns: None Global Variables Used: None Local Variables Needed: None------------------------------------------------------------------------------ FUNCTION DESCRIPTION PURPOSE: Pre-quantization of codebook gains, given three possible LTP gains (using predicted codebook gain)------------------------------------------------------------------------------ REQUIREMENTS None------------------------------------------------------------------------------ REFERENCES qgain795.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001------------------------------------------------------------------------------ PSEUDO-CODE------------------------------------------------------------------------------ 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 voidMR795_gain_code_quant3( Word16 exp_gcode0, /* i : predicted CB gain (exponent), Q0 */ Word16 gcode0, /* i : predicted CB gain (norm.), Q14 */ Word16 g_pitch_cand[], /* i : Pitch gain candidates (3), Q14 */ Word16 g_pitch_cind[], /* i : Pitch gain cand. indices (3), Q0 */ Word16 frac_coeff[], /* i : coefficients (5), Q15 */ Word16 exp_coeff[], /* i : energy coefficients (5), Q0 */ /* coefficients from calc_filt_ener()*/ Word16 *gain_pit, /* o : Pitch gain, Q14 */ Word16 *gain_pit_ind, /* o : Pitch gain index, Q0 */ Word16 *gain_cod, /* o : Code gain, Q1 */ Word16 *gain_cod_ind, /* o : Code gain index, Q0 */ Word16 *qua_ener_MR122, /* o : quantized energy error, Q10 */ /* (for MR122 MA predictor update) */ Word16 *qua_ener, /* o : quantized energy error, Q10 */ /* (for other MA predictor update) */ Flag *pOverflow /* o : overflow indicator */){ const Word16 *p; Word16 i; Word16 j; Word16 cod_ind; Word16 pit_ind; Word16 e_max; Word16 exp_code; Word16 g_pitch; Word16 g2_pitch; Word16 g_code; Word16 g2_code_h; Word16 g2_code_l; Word16 g_pit_cod_h; Word16 g_pit_cod_l; Word16 coeff[5]; Word16 coeff_lo[5]; Word16 exp_max[5]; Word32 L_tmp; Word32 L_tmp0; Word32 dist_min; /* * The error energy (sum) to be minimized consists of five terms, t[0..4]. * * t[0] = gp^2 * <y1 y1> * t[1] = -2*gp * <xn y1> * t[2] = gc^2 * <y2 y2> * t[3] = -2*gc * <xn y2> * t[4] = 2*gp*gc * <y1 y2> * */ /* determine the scaling exponent for g_code: ec = ec0 - 10 */ exp_code = sub(exp_gcode0, 10, pOverflow); /* calculate exp_max[i] = s[i]-1 */ exp_max[0] = sub(exp_coeff[0], 13, pOverflow); exp_max[1] = sub(exp_coeff[1], 14, pOverflow); exp_max[2] = add(exp_coeff[2], add(15, shl(exp_code, 1, pOverflow), pOverflow), pOverflow); exp_max[3] = add(exp_coeff[3], exp_code, pOverflow); exp_max[4] = add(exp_coeff[4], add(exp_code, 1, pOverflow), pOverflow); /*-------------------------------------------------------------------* * Find maximum exponent: * * ~~~~~~~~~~~~~~~~~~~~~~ * * * * For the sum operation, all terms must have the same scaling; * * that scaling should be low enough to prevent overflow. There- * * fore, the maximum scale is determined and all coefficients are * * re-scaled: * * * * e_max = max(exp_max[i]) + 1; * * e = exp_max[i]-e_max; e <= 0! * * c[i] = c[i]*2^e * *-------------------------------------------------------------------*/ e_max = exp_max[0]; for (i = 1; i < 5; i++) /* implemented flattened */ { if (exp_max[i] > e_max) { e_max = exp_max[i]; } } e_max = add(e_max, 1, pOverflow); /* To avoid overflow */ for (i = 0; i < 5; i++) { j = sub(e_max, exp_max[i], pOverflow); L_tmp = L_deposit_h(frac_coeff[i]); L_tmp = L_shr(L_tmp, j, pOverflow); L_Extract(L_tmp, &coeff[i], &coeff_lo[i], pOverflow); } /*-------------------------------------------------------------------*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -