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: qua_gain.cpp
36 ------------------------------------------------------------------------------
39 Quantization of pitch and codebook gains.
40 ------------------------------------------------------------------------------
43 /*----------------------------------------------------------------------------
45 ----------------------------------------------------------------------------*/
55 /*--------------------------------------------------------------------------*/
61 /*----------------------------------------------------------------------------
63 ; Define module specific macros here
64 ----------------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------------
68 ; Include all pre-processor statements here. Include conditional
69 ; compile variables also.
70 ----------------------------------------------------------------------------*/
72 /*----------------------------------------------------------------------------
73 ; LOCAL FUNCTION DEFINITIONS
74 ; Function Prototype declaration
75 ----------------------------------------------------------------------------*/
77 /*----------------------------------------------------------------------------
78 ; LOCAL VARIABLE DEFINITIONS
79 ; Variable declaration - defined here and used outside this module
80 ----------------------------------------------------------------------------*/
82 /*----------------------------------------------------------------------------
83 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
84 ; Declare variables used in this module but defined elsewhere
85 ----------------------------------------------------------------------------*/
87 /*--------------------------------------------------------------------------*/
93 ------------------------------------------------------------------------------
95 ------------------------------------------------------------------------------
96 INPUT AND OUTPUT DEFINITIONS
100 mode -- enum Mode -- AMR mode
101 Word16 exp_gcode0 -- Word16 -- predicted CB gain (exponent), Q0
102 Word16 frac_gcode0 -- Word16 -- predicted CB gain (fraction), Q15
103 Word16 frac_coeff -- Word16 Array -- energy coeff. (5), fraction part, Q15
104 Word16 exp_coeff -- Word16 Array -- energy coeff. (5), exponent part, Q0
105 (frac_coeff and exp_coeff computed in
106 calc_filt_energies())
108 Word16 gp_limit -- Word16 -- pitch gain limit
111 Word16 *gain_pit -- Pointer to Word16 -- Pitch gain, Q14
112 Word16 *gain_cod -- Pointer to Word16 -- Code gain, Q1
113 Word16 *qua_ener_MR122 -- Pointer to Word16 -- quantized energy error, Q10
114 (for MR122 MA predictor update)
115 Word16 *qua_ener -- Pointer to Word16 -- quantized energy error, Q10
116 (for other MA predictor update)
117 Flag *pOverflow -- Pointer to Flag -- overflow indicator
120 Word16 -- index of quantization.
122 Global Variables Used:
125 Local Variables Needed:
128 ------------------------------------------------------------------------------
131 Quantization of pitch and codebook gains.
132 ------------------------------------------------------------------------------
137 ------------------------------------------------------------------------------
140 qua_gain.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
142 ------------------------------------------------------------------------------
146 ------------------------------------------------------------------------------
148 [State any special notes, constraints or cautions for users of this function]
150 ------------------------------------------------------------------------------
155 Qua_gain( /* o : index of quantization. */
156 enum Mode mode, /* i : AMR mode */
157 Word16 exp_gcode0, /* i : predicted CB gain (exponent), Q0 */
158 Word16 frac_gcode0, /* i : predicted CB gain (fraction), Q15 */
159 Word16 frac_coeff[], /* i : energy coeff. (5), fraction part, Q15 */
160 Word16 exp_coeff[], /* i : energy coeff. (5), exponent part, Q0 */
161 /* (frac_coeff and exp_coeff computed in */
162 /* calc_filt_energies()) */
163 Word16 gp_limit, /* i : pitch gain limit */
164 Word16 *gain_pit, /* o : Pitch gain, Q14 */
165 Word16 *gain_cod, /* o : Code gain, Q1 */
166 Word16 *qua_ener_MR122, /* o : quantized energy error, Q10 */
167 /* (for MR122 MA predictor update) */
168 Word16 *qua_ener, /* o : quantized energy error, Q10 */
169 /* (for other MA predictor update) */
170 CommonAmrTbls* common_amr_tbls, /* i : ptr to struct of tables ptrs */
171 Flag *pOverflow /* o : overflow indicator */
193 const Word16 *table_gain;
196 if (mode == MR102 || mode == MR74 || mode == MR67)
198 table_len = VQ_SIZE_HIGHRATES;
199 table_gain = common_amr_tbls->table_gain_highrates_ptr;
203 table_len = VQ_SIZE_LOWRATES;
204 table_gain = common_amr_tbls->table_gain_lowrates_ptr;
207 /*-------------------------------------------------------------------*
208 * predicted codebook gain *
209 * ~~~~~~~~~~~~~~~~~~~~~~~ *
210 * gc0 = 2^exp_gcode0 + 2^frac_gcode0 *
212 * gcode0 (Q14) = 2^14*2^frac_gcode0 = gc0 * 2^(14-exp_gcode0) *
213 *-------------------------------------------------------------------*/
215 gcode0 = (Word16)(Pow2(14, frac_gcode0, pOverflow));
217 /*-------------------------------------------------------------------*
218 * Scaling considerations: *
219 * ~~~~~~~~~~~~~~~~~~~~~~~ *
220 *-------------------------------------------------------------------*/
223 * The error energy (sum) to be minimized consists of five terms, t[0..4].
225 * t[0] = gp^2 * <y1 y1>
226 * t[1] = -2*gp * <xn y1>
227 * t[2] = gc^2 * <y2 y2>
228 * t[3] = -2*gc * <xn y2>
229 * t[4] = 2*gp*gc * <y1 y2>
233 /* determine the scaling exponent for g_code: ec = ec0 - 11 */
234 exp_code = exp_gcode0 - 11;
236 /* calculate exp_max[i] = s[i]-1 */
237 exp_max[0] = exp_coeff[0] - 13;
238 exp_max[1] = exp_coeff[1] - 14;
240 temp = shl(exp_code, 1, pOverflow);
242 exp_max[2] = add_16(exp_coeff[2], temp, pOverflow);
244 exp_max[3] = add_16(exp_coeff[3], exp_code, pOverflow);
247 exp_max[4] = add_16(exp_coeff[4], temp, pOverflow);
250 /*-------------------------------------------------------------------*
251 * Find maximum exponent: *
252 * ~~~~~~~~~~~~~~~~~~~~~~ *
254 * For the sum operation, all terms must have the same scaling; *
255 * that scaling should be low enough to prevent overflow. There- *
256 * fore, the maximum scale is determined and all coefficients are *
259 * e_max = max(exp_max[i]) + 1; *
260 * e = exp_max[i]-e_max; e <= 0! *
262 *-------------------------------------------------------------------*/
265 for (i = 1; i < 5; i++)
267 if (exp_max[i] > e_max)
275 for (i = 0; i < 5; i++)
277 j = e_max - exp_max[i];
278 L_tmp = ((Word32)frac_coeff[i] << 16);
279 L_tmp = L_shr(L_tmp, j, pOverflow);
280 L_Extract(L_tmp, &coeff[i], &coeff_lo[i], pOverflow);
284 /*-------------------------------------------------------------------*
288 * For each pair (g_pitch, g_fac) in the table calculate the *
289 * terms t[0..4] and sum them up; the result is the mean squared *
290 * error for the quantized gains from the table. The index for the *
291 * minimum MSE is stored and finally used to retrieve the quantized *
293 *-------------------------------------------------------------------*/
295 /* start with "infinite" MSE */
300 for (i = 0; i < table_len; i++)
303 g_code = *p++; /* this is g_fac */
304 p++; /* skip log2(g_fac) */
305 p++; /* skip 20*log10(g_fac) */
307 if (g_pitch <= gp_limit)
309 g_code = mult(g_code, gcode0, pOverflow);
310 g2_pitch = mult(g_pitch, g_pitch, pOverflow);
311 g2_code = mult(g_code, g_code, pOverflow);
312 g_pit_cod = mult(g_code, g_pitch, pOverflow);
314 L_tmp = Mpy_32_16(coeff[0], coeff_lo[0], g2_pitch, pOverflow);
315 L_tmp2 = Mpy_32_16(coeff[1], coeff_lo[1], g_pitch, pOverflow);
316 L_tmp = L_add(L_tmp, L_tmp2, pOverflow);
318 L_tmp2 = Mpy_32_16(coeff[2], coeff_lo[2], g2_code, pOverflow);
319 L_tmp = L_add(L_tmp, L_tmp2, pOverflow);
321 L_tmp2 = Mpy_32_16(coeff[3], coeff_lo[3], g_code, pOverflow);
322 L_tmp = L_add(L_tmp, L_tmp2, pOverflow);
324 L_tmp2 = Mpy_32_16(coeff[4], coeff_lo[4], g_pit_cod, pOverflow);
325 L_tmp = L_add(L_tmp, L_tmp2, pOverflow);
327 /* store table index if MSE for this index is lower
328 than the minimum MSE seen so far */
329 if (L_tmp < dist_min)
337 /*------------------------------------------------------------------*
338 * read quantized gains and new values for MA predictor memories *
339 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
340 *------------------------------------------------------------------*/
342 /* Read the quantized gains */
343 p = &table_gain[shl(index, 2, pOverflow)];
346 *qua_ener_MR122 = *p++;
349 /*------------------------------------------------------------------*
350 * calculate final fixed codebook gain: *
351 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
354 *------------------------------------------------------------------*/
356 L_tmp = L_mult(g_code, gcode0, pOverflow);
357 temp = 10 - exp_gcode0;
358 L_tmp = L_shr(L_tmp, temp, pOverflow);
360 *gain_cod = (Word16)(L_tmp >> 16);