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: d_gain_c.cpp
37 ------------------------------------------------------------------------------
38 INPUT AND OUTPUT DEFINITIONS
41 pred_state = pointer to sturcture type gc_predState. MA predictor state
42 mode = AMR mode (MR795 or MR122) of type enum Mode
43 index = received quantization index of type Word16
44 code[] = pointer to innovation codevector of type Word16
45 pOverflow= pointer to value indicating existence of overflow (Flag)
48 pred_state = pointer to sturcture type gc_predState. MA predictor state
49 gain_code = pointer to decoded innovation gain of type Word16
50 pOverflow = 1 if there is an overflow else it is zero.
55 Global Variables Used:
58 Local Variables Needed:
61 ------------------------------------------------------------------------------
64 Function : d_gain_code
65 Purpose : Decode the fixed codebook gain using the received index.
67 ------------------------------------------------------------------------------
72 ------------------------------------------------------------------------------
75 d_gain_c.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
77 ------------------------------------------------------------------------------
82 ------------------------------------------------------------------------------
86 /*----------------------------------------------------------------------------
88 ----------------------------------------------------------------------------*/
101 /*--------------------------------------------------------------------------*/
107 /*----------------------------------------------------------------------------
109 ; Define module specific macros here
110 ----------------------------------------------------------------------------*/
113 /*----------------------------------------------------------------------------
115 ; Include all pre-processor statements here. Include conditional
116 ; compile variables also.
117 ----------------------------------------------------------------------------*/
119 /*----------------------------------------------------------------------------
120 ; LOCAL FUNCTION DEFINITIONS
121 ; Function Prototype declaration
122 ----------------------------------------------------------------------------*/
125 /*----------------------------------------------------------------------------
126 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
127 ; Variable declaration - defined here and used outside this module
128 ----------------------------------------------------------------------------*/
131 /*----------------------------------------------------------------------------
132 ; EXTERNAL FUNCTION REFERENCES
133 ; Declare functions defined elsewhere and referenced in this module
134 ----------------------------------------------------------------------------*/
136 /*----------------------------------------------------------------------------
137 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
138 ; Declare variables used in this module but defined elsewhere
139 ----------------------------------------------------------------------------*/
141 /*--------------------------------------------------------------------------*/
146 /*----------------------------------------------------------------------------
148 ----------------------------------------------------------------------------*/
150 gc_predState *pred_state, /* i/o : MA predictor state */
151 enum Mode mode, /* i : AMR mode (MR795 or MR122) */
152 Word16 index, /* i : received quantization index */
153 Word16 code[], /* i : innovation codevector */
154 const Word16* qua_gain_code_ptr, /* i : Pointer to read-only table */
155 Word16 *gain_code, /* o : decoded innovation gain */
159 Word16 gcode0, exp, frac;
161 Word16 qua_ener_MR122, qua_ener;
167 /*-------------- Decode codebook gain ---------------*/
169 /*-------------------------------------------------------------------*
170 * predict codebook gain *
171 * ~~~~~~~~~~~~~~~~~~~~~ *
172 * gc0 = Pow2(int(d)+frac(d)) *
175 *-------------------------------------------------------------------*/
177 gc_pred(pred_state, mode, code, &exp, &frac,
178 &exp_inn_en, &frac_inn_en, pOverflow);
180 index &= 31; /* index < 32, to avoid buffer overflow */
181 tbl_tmp = index + (index << 1);
183 p = &qua_gain_code_ptr[tbl_tmp];
185 /* Different scalings between MR122 and the other modes */
186 temp = sub((Word16)mode, (Word16)MR122, pOverflow);
189 gcode0 = (Word16)(Pow2(exp, frac, pOverflow)); /* predicted gain */
190 gcode0 = shl(gcode0, 4, pOverflow);
191 *gain_code = shl(mult(gcode0, *p++, pOverflow), 1, pOverflow);
195 gcode0 = (Word16)(Pow2(14, frac, pOverflow));
196 L_tmp = L_mult(*p++, gcode0, pOverflow);
197 L_tmp = L_shr(L_tmp, sub(9, exp, pOverflow), pOverflow);
198 *gain_code = (Word16)(L_tmp >> 16); /* Q1 */
201 /*-------------------------------------------------------------------*
202 * update table of past quantized energies *
203 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
204 *-------------------------------------------------------------------*/
205 qua_ener_MR122 = *p++;
207 gc_pred_update(pred_state, qua_ener_MR122, qua_ener);