1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
30 ------------------------------------------------------------------------------
34 Filename: q_gain_c.cpp
35 Functions: q_gain_code
37 ------------------------------------------------------------------------------
40 Scalar quantization of the innovative codebook gain.
42 ------------------------------------------------------------------------------
45 /*----------------------------------------------------------------------------
47 ----------------------------------------------------------------------------*/
55 /*--------------------------------------------------------------------------*/
61 /*----------------------------------------------------------------------------
63 ; Define module specific macros here
64 ----------------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------------
68 ; Include all pre-processor statements here. Include conditional
69 ; compile variables also.
70 ----------------------------------------------------------------------------*/
71 #define NB_QUA_CODE 32
73 /*----------------------------------------------------------------------------
74 ; LOCAL FUNCTION DEFINITIONS
75 ; Function Prototype declaration
76 ----------------------------------------------------------------------------*/
78 /*----------------------------------------------------------------------------
79 ; LOCAL VARIABLE DEFINITIONS
80 ; Variable declaration - defined here and used outside this module
81 ----------------------------------------------------------------------------*/
83 /*----------------------------------------------------------------------------
84 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
85 ; Declare variables used in this module but defined elsewhere
86 ----------------------------------------------------------------------------*/
88 /*--------------------------------------------------------------------------*/
94 ------------------------------------------------------------------------------
95 FUNCTION NAME: q_gain_code
96 ------------------------------------------------------------------------------
97 INPUT AND OUTPUT DEFINITIONS
100 mode -- enum Mode -- AMR mode
101 exp_gcode0 -- Word16 -- predicted CB gain (exponent), Q0
102 frac_gcode0 -- Word16 -- predicted CB gain (fraction), Q15
103 gain -- Pointer to Word16 -- quantized fixed codebook gain, Q1
106 gain -- Pointer to Word16 -- quantized fixed codebook gain, Q1
108 qua_ener_MR122 -- Pointer to Word16 -- quantized energy error, Q10
109 (for MR122 MA predictor update)
111 qua_ener -- Pointer to Word16 -- quantized energy error, Q10
112 (for other MA predictor update)
114 pOverflow -- Pointer to Flag -- overflow indicator
116 quantization index -- Word16 -- Q0
118 Global Variables Used:
121 Local Variables Needed:
124 ------------------------------------------------------------------------------
127 Scalar quantization of the innovative codebook gain.
129 ------------------------------------------------------------------------------
134 ------------------------------------------------------------------------------
137 q_gain_c.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
139 ------------------------------------------------------------------------------
143 ------------------------------------------------------------------------------
145 [State any special notes, constraints or cautions for users of this function]
147 ------------------------------------------------------------------------------
150 Word16 q_gain_code( /* o : quantization index, Q0 */
151 enum Mode mode, /* i : AMR mode */
152 Word16 exp_gcode0, /* i : predicted CB gain (exponent), Q0 */
153 Word16 frac_gcode0, /* i : predicted CB gain (fraction), Q15 */
154 Word16 *gain, /* i/o: quantized fixed codebook gain, Q1 */
155 Word16 *qua_ener_MR122, /* o : quantized energy error, Q10 */
156 /* (for MR122 MA predictor update) */
157 Word16 *qua_ener, /* o : quantized energy error, Q10 */
158 /* (for other MA predictor update) */
159 const Word16* qua_gain_code_ptr, /* i : ptr to read-only table */
174 g_q0 = *gain >> 1; /* Q1 -> Q0 */
181 /*-------------------------------------------------------------------*
182 * predicted codebook gain *
183 * ~~~~~~~~~~~~~~~~~~~~~~~ *
184 * gc0 = Pow2(int(d)+frac(d)) *
187 *-------------------------------------------------------------------*/
189 gcode0 = (Word16) Pow2(exp_gcode0, frac_gcode0, pOverflow); /* predicted gain */
193 gcode0 = shl(gcode0, 4, pOverflow);
197 gcode0 = shl(gcode0, 5, pOverflow);
200 /*-------------------------------------------------------------------*
201 * Search for best quantizer *
202 *-------------------------------------------------------------------*/
204 p = &qua_gain_code_ptr[0];
205 err_min = ((Word32)gcode0 * *(p++)) >> 15;
206 err_min = g_q0 - err_min;
212 p += 2; /* skip quantized energy errors */
215 for (i = 1; i < NB_QUA_CODE; i++)
217 err = ((Word32)gcode0 * *(p++)) >> 15;
225 p += 2; /* skip quantized energy error */
234 temp = index + (index << 1);
236 p = &qua_gain_code_ptr[temp];
238 temp = (gcode0 * *(p++)) >> 15;
248 /* quantized error energies (for MA predictor update) */
249 *qua_ener_MR122 = *p++;