Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / dec / src / d_gain_c.cpp
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
20
21     3GPP TS 26.073
22     ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23     Available from http://www.3gpp.org
24
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 ****************************************************************************************/
29 /*
30 ------------------------------------------------------------------------------
31
32
33
34  Filename: d_gain_c.cpp
35  Functions: d_gain_c
36
37  ------------------------------------------------------------------------------
38  INPUT AND OUTPUT DEFINITIONS
39
40  Inputs:
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)
46
47  Outputs:
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.
51
52  Returns:
53     None.
54
55  Global Variables Used:
56     None
57
58  Local Variables Needed:
59     None
60
61 ------------------------------------------------------------------------------
62  FUNCTION DESCRIPTION
63
64   Function    : d_gain_code
65   Purpose     : Decode the fixed codebook gain using the received index.
66
67 ------------------------------------------------------------------------------
68  REQUIREMENTS
69
70
71
72 ------------------------------------------------------------------------------
73  REFERENCES
74
75  d_gain_c.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
76
77 ------------------------------------------------------------------------------
78  PSEUDO-CODE
79
80
81
82 ------------------------------------------------------------------------------
83 */
84
85
86 /*----------------------------------------------------------------------------
87 ; INCLUDES
88 ----------------------------------------------------------------------------*/
89 #include "d_gain_c.h"
90 #include "typedef.h"
91 #include "mode.h"
92
93 #include "oper_32b.h"
94 #include "cnst.h"
95 #include "log2.h"
96 #include "pow2.h"
97 #include "gc_pred.h"
98
99 #include "basic_op.h"
100
101 /*--------------------------------------------------------------------------*/
102 #ifdef __cplusplus
103 extern "C"
104 {
105 #endif
106
107     /*----------------------------------------------------------------------------
108     ; MACROS
109     ; Define module specific macros here
110     ----------------------------------------------------------------------------*/
111
112
113     /*----------------------------------------------------------------------------
114     ; DEFINES
115     ; Include all pre-processor statements here. Include conditional
116     ; compile variables also.
117     ----------------------------------------------------------------------------*/
118
119     /*----------------------------------------------------------------------------
120     ; LOCAL FUNCTION DEFINITIONS
121     ; Function Prototype declaration
122     ----------------------------------------------------------------------------*/
123
124
125     /*----------------------------------------------------------------------------
126     ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
127     ; Variable declaration - defined here and used outside this module
128     ----------------------------------------------------------------------------*/
129
130
131     /*----------------------------------------------------------------------------
132     ; EXTERNAL FUNCTION REFERENCES
133     ; Declare functions defined elsewhere and referenced in this module
134     ----------------------------------------------------------------------------*/
135
136     /*----------------------------------------------------------------------------
137     ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
138     ; Declare variables used in this module but defined elsewhere
139     ----------------------------------------------------------------------------*/
140
141     /*--------------------------------------------------------------------------*/
142 #ifdef __cplusplus
143 }
144 #endif
145
146 /*----------------------------------------------------------------------------
147 ; FUNCTION CODE
148 ----------------------------------------------------------------------------*/
149 void d_gain_code(
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          */
156     Flag   *pOverflow
157 )
158 {
159     Word16 gcode0, exp, frac;
160     const Word16 *p;
161     Word16 qua_ener_MR122, qua_ener;
162     Word16 exp_inn_en;
163     Word16 frac_inn_en;
164     Word32 L_tmp;
165     Word16 tbl_tmp;
166     Word16 temp;
167     /*-------------- Decode codebook gain ---------------*/
168
169     /*-------------------------------------------------------------------*
170      *  predict codebook gain                                            *
171      *  ~~~~~~~~~~~~~~~~~~~~~                                            *
172      *  gc0     = Pow2(int(d)+frac(d))                                   *
173      *          = 2^exp + 2^frac                                         *
174      *                                                                   *
175      *-------------------------------------------------------------------*/
176
177     gc_pred(pred_state, mode, code, &exp, &frac,
178             &exp_inn_en, &frac_inn_en, pOverflow);
179
180     index &= 31;                    /* index < 32, to avoid buffer overflow */
181     tbl_tmp = index + (index << 1);
182
183     p = &qua_gain_code_ptr[tbl_tmp];
184
185     /* Different scalings between MR122 and the other modes */
186     temp = sub((Word16)mode, (Word16)MR122, pOverflow);
187     if (temp == 0)
188     {
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);
192     }
193     else
194     {
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 */
199     }
200
201     /*-------------------------------------------------------------------*
202      *  update table of past quantized energies                          *
203      *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                          *
204      *-------------------------------------------------------------------*/
205     qua_ener_MR122 = *p++;
206     qua_ener = *p++;
207     gc_pred_update(pred_state, qua_ener_MR122, qua_ener);
208
209     return;
210 }
211
212
213
214