Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / dec / src / lsp_avg.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: lsp_avg.cpp
35
36 ------------------------------------------------------------------------------
37  MODULE DESCRIPTION
38
39     LSP averaging and history
40 ------------------------------------------------------------------------------
41 */
42
43 /*----------------------------------------------------------------------------
44 ; INCLUDES
45 ----------------------------------------------------------------------------*/
46 #include "lsp_avg.h"
47 #include "basic_op.h"
48 #include "oper_32b.h"
49 #include "oscl_mem.h"
50 #include "q_plsf_5_tbl.h"
51
52 /*----------------------------------------------------------------------------
53 ; MACROS
54 ; Define module specific macros here
55 ----------------------------------------------------------------------------*/
56
57 /*----------------------------------------------------------------------------
58 ; DEFINES
59 ; Include all pre-processor statements here. Include conditional
60 ; compile variables also.
61 ----------------------------------------------------------------------------*/
62
63 /*----------------------------------------------------------------------------
64 ; LOCAL FUNCTION DEFINITIONS
65 ; Function Prototype declaration
66 ----------------------------------------------------------------------------*/
67
68 /*----------------------------------------------------------------------------
69 ; LOCAL VARIABLE DEFINITIONS
70 ; Variable declaration - defined here and used outside this module
71 ----------------------------------------------------------------------------*/
72
73 /*----------------------------------------------------------------------------
74 ; EXTERNAL FUNCTION REFERENCES
75 ; Declare functions defined elsewhere and referenced in this module
76 ----------------------------------------------------------------------------*/
77
78 /*----------------------------------------------------------------------------
79 ; EXTERNAL VARIABLES REFERENCES
80 ; Declare variables used in this module but defined elsewhere
81 ----------------------------------------------------------------------------*/
82
83 /*
84 ------------------------------------------------------------------------------
85  FUNCTION NAME: lsp_avg_reset
86 ------------------------------------------------------------------------------
87  INPUT AND OUTPUT DEFINITIONS
88
89  Inputs:
90     st = pointer to structure of type lsp_avgState
91
92  Outputs:
93     fields of the structure pointed to by state are initialized.
94
95  Returns:
96     return_value = 0, if reset was successful; -1, otherwise (int)
97
98  Global Variables Used:
99     None
100
101  Local Variables Needed:
102     None
103
104 ------------------------------------------------------------------------------
105  FUNCTION DESCRIPTION
106
107
108 ------------------------------------------------------------------------------
109  REQUIREMENTS
110
111  None
112
113 ------------------------------------------------------------------------------
114  REFERENCES
115
116 lsp_avg.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
117
118 ------------------------------------------------------------------------------
119  PSEUDO-CODE
120
121 int lsp_avg_reset (lsp_avgState *st)
122 {
123   if (st == (lsp_avgState *) NULL){
124       // fprintf(stderr, "lsp_avg_reset: invalid parameter\n");
125       return -1;
126   }
127
128   Copy(mean_lsf, &st->lsp_meanSave[0], M);
129
130   return 0;
131 }
132
133 ------------------------------------------------------------------------------
134  CAUTION [optional]
135  [State any special notes, constraints or cautions for users of this function]
136
137 ------------------------------------------------------------------------------
138 */
139
140 Word16 lsp_avg_reset(lsp_avgState *st, const Word16* mean_lsf_5_ptr)
141 {
142     if (st == (lsp_avgState *) NULL)
143     {
144         /* fprintf(stderr, "lsp_avg_reset: invalid parameter\n"); */
145         return -1;
146     }
147
148     oscl_memmove((void *)&st->lsp_meanSave[0], mean_lsf_5_ptr, M*sizeof(*mean_lsf_5_ptr));
149
150     return 0;
151 }
152
153
154 /*
155 ------------------------------------------------------------------------------
156  FUNCTION NAME: lsp_avg
157 ------------------------------------------------------------------------------
158  INPUT AND OUTPUT DEFINITIONS
159
160  Inputs:
161     st  = pointer to structure of type lsp_avgState
162     lsp = pointer to Word16, which reflects the state of the state machine
163
164  Outputs:
165     st = pointer to structure of type lsp_avgState
166     pOverflow = pointer to type Flag -- overflow indicator
167
168  Returns:
169     None
170
171  Global Variables Used:
172     None
173
174  Local Variables Needed:
175     None
176
177 ------------------------------------------------------------------------------
178  FUNCTION DESCRIPTION
179
180
181 ------------------------------------------------------------------------------
182  REQUIREMENTS
183
184  None
185
186 ------------------------------------------------------------------------------
187  REFERENCES
188
189 lsp_avg.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
190
191 ------------------------------------------------------------------------------
192  PSEUDO-CODE
193
194
195 void lsp_avg (
196     lsp_avgState *st,         // i/o : State struct                 Q15
197     Word16 *lsp               // i   : state of the state machine   Q15
198 )
199 {
200     Word16 i;
201     Word32 L_tmp;            // Q31
202
203     for (i = 0; i < M; i++) {
204
205        // mean = 0.84*mean
206        L_tmp = L_deposit_h(st->lsp_meanSave[i]);
207        L_tmp = L_msu(L_tmp, EXPCONST, st->lsp_meanSave[i]);
208
209        // Add 0.16 of newest LSPs to mean
210        L_tmp = L_mac(L_tmp, EXPCONST, lsp[i]);
211
212        // Save means
213        st->lsp_meanSave[i] = pv_round(L_tmp);   // Q15
214     }
215
216     return;
217 }
218
219 ------------------------------------------------------------------------------
220  CAUTION [optional]
221  [State any special notes, constraints or cautions for users of this function]
222
223 ------------------------------------------------------------------------------
224 */
225
226 void lsp_avg(
227     lsp_avgState *st,         /* i/o : State struct                 Q15 */
228     Word16 *lsp,              /* i   : state of the state machine   Q15 */
229     Flag   *pOverflow         /* o   : Flag set when overflow occurs    */
230 )
231 {
232     Word16 i;
233     Word32 L_tmp;            /* Q31 */
234
235     for (i = 0; i < M; i++)
236     {
237
238         /* mean = 0.84*mean */
239         L_tmp = ((Word32)st->lsp_meanSave[i] << 16);
240         L_tmp = L_msu(L_tmp, EXPCONST, st->lsp_meanSave[i], pOverflow);
241
242         /* Add 0.16 of newest LSPs to mean */
243         L_tmp = L_mac(L_tmp, EXPCONST, lsp[i], pOverflow);
244
245         /* Save means */
246         st->lsp_meanSave[i] = pv_round(L_tmp, pOverflow);   /* Q15 */
247     }
248
249     return;
250 }