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: dec_gain.cpp
37 ------------------------------------------------------------------------------
41 /*----------------------------------------------------------------------------
43 ----------------------------------------------------------------------------*/
53 #include "qua_gain_tbl.h"
54 #include "qgain475_tab.h"
56 /*----------------------------------------------------------------------------
58 ; Define module specific macros here
59 ----------------------------------------------------------------------------*/
62 /*----------------------------------------------------------------------------
64 ; Include all pre-processor statements here. Include conditional
65 ; compile variables also.
66 ----------------------------------------------------------------------------*/
68 /*----------------------------------------------------------------------------
69 ; LOCAL FUNCTION DEFINITIONS
70 ; Function Prototype declaration
71 ----------------------------------------------------------------------------*/
73 /*----------------------------------------------------------------------------
74 ; LOCAL VARIABLE DEFINITIONS
75 ; Variable declaration - defined here and used outside this module
76 ----------------------------------------------------------------------------*/
79 ------------------------------------------------------------------------------
80 FUNCTION NAME: dec_gain
81 ------------------------------------------------------------------------------
82 INPUT AND OUTPUT DEFINITIONS
85 pred_state = pointer to MA predictor state of type gc_predState
86 index = AMR mode of type enum Mode
87 code[] = pointer to innovative vector of type Word16
88 evenSubfr = Flag for even subframes of type Word16
89 pOverflow = pointer to overflow flag
93 pred_state = pointer to MA predictor state of type gc_predState
94 gain_pit = pointer to pitch gain of type Word16
95 gain_cod = pointer to code gain of type Word16
100 Global Variables Used:
103 Local Variables Needed:
106 ------------------------------------------------------------------------------
110 Purpose : Decode the pitch and codebook gains
112 ------------------------------------------------------------------------------
117 ------------------------------------------------------------------------------
120 agc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
122 ------------------------------------------------------------------------------
132 ------------------------------------------------------------------------------
134 [State any special notes, constraints or cautions for users of this function]
136 ------------------------------------------------------------------------------
141 gc_predState *pred_state, /* i/o: MA predictor state */
142 enum Mode mode, /* i : AMR mode */
143 Word16 index, /* i : index of quantization. */
144 Word16 code[], /* i : Innovative vector. */
145 Word16 evenSubfr, /* i : Flag for even subframes */
146 Word16 * gain_pit, /* o : Pitch gain. */
147 Word16 * gain_cod, /* o : Code gain. */
148 CommonAmrTbls* common_amr_tbls, /* i : ptr to struct of tbls ptrs */
157 Word16 qua_ener_MR122;
163 /* Read the quantized gains (table depends on mode) */
164 index = shl(index, 2, pOverflow);
166 if (mode == MR102 || mode == MR74 || mode == MR67)
168 p = &(common_amr_tbls->table_gain_highrates_ptr[index]);
172 qua_ener_MR122 = *p++;
179 index += (1 ^ evenSubfr) << 1; /* evenSubfr is 0 or 1 */
181 if (index > (MR475_VQ_SIZE*4 - 2))
183 index = (MR475_VQ_SIZE * 4 - 2); /* avoid possible buffer overflow */
186 p = &table_gain_MR475[index];
191 /*---------------------------------------------------------*
192 * calculate predictor update values (not stored in 4.75 *
193 * quantizer table to save space): *
194 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
196 * qua_ener = log2(g) *
197 * qua_ener_MR122 = 20*log10(g) *
198 *---------------------------------------------------------*/
200 /* Log2(x Q12) = log2(x) + 12 */
202 Log2(temp1, &exp, &frac, pOverflow);
205 temp1 = shr_r(frac, 5, pOverflow);
206 temp2 = shl(exp, 10, pOverflow);
207 qua_ener_MR122 = add_16(temp1, temp2, pOverflow);
209 /* 24660 Q12 ~= 6.0206 = 20*log10(2) */
210 L_tmp = Mpy_32_16(exp, frac, 24660, pOverflow);
211 L_tmp = L_shl(L_tmp, 13, pOverflow);
212 qua_ener = pv_round(L_tmp, pOverflow);
213 /* Q12 * Q0 = Q13 -> Q10 */
217 p = &(common_amr_tbls->table_gain_lowrates_ptr[index]);
221 qua_ener_MR122 = *p++;
226 /*-------------------------------------------------------------------*
227 * predict codebook gain *
228 * ~~~~~~~~~~~~~~~~~~~~~ *
229 * gc0 = Pow2(int(d)+frac(d)) *
232 * gcode0 (Q14) = 2^14*2^frac = gc0 * 2^(14-exp) *
233 *-------------------------------------------------------------------*/
235 gc_pred(pred_state, mode, code, &exp, &frac, NULL, NULL, pOverflow);
237 gcode0 = (Word16) Pow2(14, frac, pOverflow);
239 /*------------------------------------------------------------------*
240 * read quantized gains, update table of past quantized energies *
241 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
242 * st->past_qua_en(Q10) = 20 * Log10(g_fac) / constant *
245 * constant = 20*Log10(2) *
246 *------------------------------------------------------------------*/
248 L_tmp = L_mult(g_code, gcode0, pOverflow);
250 L_tmp = L_shr(L_tmp, temp1, pOverflow);
251 *gain_cod = (Word16)(L_tmp >> 16);
253 /* update table of past quantized energies */
255 gc_pred_update(pred_state, qua_ener_MR122, qua_ener);