Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / lpc.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: lpc.cpp
35
36 ------------------------------------------------------------------------------
37 */
38
39 /*----------------------------------------------------------------------------
40 ; INCLUDES
41 ----------------------------------------------------------------------------*/
42 #include "lpc.h"
43 #include "typedef.h"
44 #include "oper_32b.h"
45 #include "autocorr.h"
46 #include "lag_wind.h"
47 #include "levinson.h"
48 #include "cnst.h"
49 #include "mode.h"
50 #include "sub.h"
51 #include "oscl_mem.h"
52
53 /*----------------------------------------------------------------------------
54 ; MACROS
55 ; Define module specific macros here
56 ----------------------------------------------------------------------------*/
57
58
59 /*----------------------------------------------------------------------------
60 ; DEFINES
61 ; Include all pre-processor statements here. Include conditional
62 ; compile variables also.
63 ----------------------------------------------------------------------------*/
64
65
66 /*----------------------------------------------------------------------------
67 ; LOCAL FUNCTION DEFINITIONS
68 ; Function Prototype declaration
69 ----------------------------------------------------------------------------*/
70
71
72 /*----------------------------------------------------------------------------
73 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
74 ; Variable declaration - defined here and used outside this module
75 ----------------------------------------------------------------------------*/
76
77 /*
78 ------------------------------------------------------------------------------
79  FUNCTION NAME: lpc_init
80 ------------------------------------------------------------------------------
81  INPUT AND OUTPUT DEFINITIONS
82
83  Inputs:
84     state = pointer to pointer of state data of type lpcState
85
86  Outputs:
87     None
88
89  Returns:
90     None
91
92  Global Variables Used:
93     None.
94
95  Local Variables Needed:
96     None.
97
98 ------------------------------------------------------------------------------
99  FUNCTION DESCRIPTION
100
101  This function initializes the state data for the LPC module.
102
103 ------------------------------------------------------------------------------
104  REQUIREMENTS
105
106  None.
107
108 ------------------------------------------------------------------------------
109  REFERENCES
110
111  lpc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
112
113 ------------------------------------------------------------------------------
114  PSEUDO-CODE
115
116
117   lpcState* s;
118
119   if (state == (lpcState **) NULL){
120       // fprintf(stderr, "lpc_init: invalid parameter\n");
121       return -1;
122   }
123   *state = NULL;
124
125   // allocate memory
126   if ((s= (lpcState *) malloc(sizeof(lpcState))) == NULL){
127       // fprintf(stderr, "lpc_init: can not malloc state structure\n");
128       return -1;
129   }
130
131   s->levinsonSt = NULL;
132
133   // Init sub states
134   if (Levinson_init(&s->levinsonSt)) {
135      lpc_exit(&s);
136      return -1;
137   }
138
139
140   lpc_reset(s);
141   *state = s;
142
143   return 0;
144
145 ------------------------------------------------------------------------------
146  CAUTION [optional]
147  [State any special notes, constraints or cautions for users of this function]
148
149 ------------------------------------------------------------------------------
150 */
151 Word16 lpc_init(lpcState **state)
152 {
153     lpcState* s;
154
155     if (state == (lpcState **) NULL)
156     {
157         /* fprintf(stderr, "lpc_init: invalid parameter\n"); */
158         return -1;
159     }
160     *state = NULL;
161
162     /* allocate memory */
163     if ((s = (lpcState *) oscl_malloc(sizeof(lpcState))) == NULL)
164     {
165         /* fprintf(stderr, "lpc_init: can not malloc state structure\n"); */
166         return -1;
167     }
168
169     s->levinsonSt = NULL;
170
171     /* Init sub states */
172     if (Levinson_init(&s->levinsonSt))
173     {
174         lpc_exit(&s);
175         return -1;
176     }
177
178     lpc_reset(s);
179     *state = s;
180
181     return 0;
182 }
183
184 /*
185 ------------------------------------------------------------------------------
186  FUNCTION NAME: lpc_reset
187 ------------------------------------------------------------------------------
188  INPUT AND OUTPUT DEFINITIONS
189
190  Inputs:
191     state = pointer to pointer of state data of type lpcState
192
193  Outputs:
194     None
195
196  Returns:
197     None
198
199  Global Variables Used:
200     None.
201
202  Local Variables Needed:
203     None.
204
205 ------------------------------------------------------------------------------
206  FUNCTION DESCRIPTION
207
208  This function resets the state data for the LPC module.
209
210 ------------------------------------------------------------------------------
211  REQUIREMENTS
212
213  None.
214
215 ------------------------------------------------------------------------------
216  REFERENCES
217
218  lpc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
219
220 ------------------------------------------------------------------------------
221  PSEUDO-CODE
222
223   if (state == (lpcState *) NULL){
224       // fprintf(stderr, "lpc_reset: invalid parameter\n");
225       return -1;
226   }
227
228   Levinson_reset(state->levinsonSt);
229
230   return 0;
231
232 ------------------------------------------------------------------------------
233  CAUTION [optional]
234  [State any special notes, constraints or cautions for users of this function]
235
236 ------------------------------------------------------------------------------
237 */
238 Word16 lpc_reset(lpcState *state)
239 {
240
241     if (state == (lpcState *) NULL)
242     {
243         /* fprintf(stderr, "lpc_reset: invalid parameter\n"); */
244         return -1;
245     }
246
247     Levinson_reset(state->levinsonSt);
248
249     return 0;
250 }
251
252 /*
253 ------------------------------------------------------------------------------
254  FUNCTION NAME: lpc_exit
255 ------------------------------------------------------------------------------
256  INPUT AND OUTPUT DEFINITIONS
257
258  Inputs:
259     state = pointer to pointer of state data of type lpcState
260
261  Outputs:
262     None
263
264  Returns:
265     None
266
267  Global Variables Used:
268     None.
269
270  Local Variables Needed:
271     None.
272
273 ------------------------------------------------------------------------------
274  FUNCTION DESCRIPTION
275
276  This function frees the state data for the LPC module.
277
278 ------------------------------------------------------------------------------
279  REQUIREMENTS
280
281  None.
282
283 ------------------------------------------------------------------------------
284  REFERENCES
285
286  lpc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
287
288 ------------------------------------------------------------------------------
289  PSEUDO-CODE
290
291
292   if (state == NULL || *state == NULL)
293       return;
294
295   Levinson_exit(&(*state)->levinsonSt);
296
297   // deallocate memory
298   free(*state);
299   *state = NULL;
300
301   return;
302
303
304 ------------------------------------------------------------------------------
305  CAUTION [optional]
306  [State any special notes, constraints or cautions for users of this function]
307
308 ------------------------------------------------------------------------------
309 */
310 void lpc_exit(lpcState **state)
311 {
312     if (state == NULL || *state == NULL)
313         return;
314
315     Levinson_exit(&(*state)->levinsonSt);
316
317     /* deallocate memory */
318     oscl_free(*state);
319     *state = NULL;
320
321     return;
322 }
323
324
325 /*
326 ------------------------------------------------------------------------------
327  FUNCTION NAME: lpc
328 ------------------------------------------------------------------------------
329  INPUT AND OUTPUT DEFINITIONS
330
331  Inputs:
332     state = pointer to state data of type lpcState
333     mode  = coder mode of type enum Mode
334     x[]   = pointer to input signal (Q15) of type Word16
335     x_12k2[] = pointer to input signal (EFR) (Q15) of type Word16
336     pOverflow = pointer to overflow indicator of type Flag
337
338  Outputs:
339     a[]   = pointer to predictor coefficients (Q12) of type Word16
340
341  Returns:
342     None
343
344  Global Variables Used:
345     None.
346
347  Local Variables Needed:
348     None.
349
350 ------------------------------------------------------------------------------
351  FUNCTION DESCRIPTION
352
353  This function executes the LPC functionality for GSM AMR.
354
355 ------------------------------------------------------------------------------
356  REQUIREMENTS
357
358  None.
359
360 ------------------------------------------------------------------------------
361  REFERENCES
362
363  lpc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
364
365 ------------------------------------------------------------------------------
366  PSEUDO-CODE
367
368    Word16 rc[4];                  // First 4 reflection coefficients Q15
369    Word16 rLow[MP1], rHigh[MP1];  // Autocorrelations low and hi
370                                   // No fixed Q value but normalized
371                                   // so that overflow is avoided
372
373    if ( sub ((Word16)mode, (Word16)MR122) == 0)
374    {
375        // Autocorrelations
376        Autocorr(x_12k2, M, rHigh, rLow, window_160_80);
377        // Lag windowing
378        Lag_window(M, rHigh, rLow);
379        // Levinson Durbin
380        Levinson(st->levinsonSt, rHigh, rLow, &a[MP1], rc);
381
382        // Autocorrelations
383        Autocorr(x_12k2, M, rHigh, rLow, window_232_8);
384        // Lag windowing
385        Lag_window(M, rHigh, rLow);
386        // Levinson Durbin
387        Levinson(st->levinsonSt, rHigh, rLow, &a[MP1 * 3], rc);
388    }
389    else
390    {
391        // Autocorrelations
392        Autocorr(x, M, rHigh, rLow, window_200_40);
393        // Lag windowing
394        Lag_window(M, rHigh, rLow);
395        // Levinson Durbin
396        Levinson(st->levinsonSt, rHigh, rLow, &a[MP1 * 3], rc);
397    }
398
399    return 0;
400
401 ------------------------------------------------------------------------------
402  CAUTION [optional]
403  [State any special notes, constraints or cautions for users of this function]
404
405 ------------------------------------------------------------------------------
406 */
407 void lpc(
408     lpcState *st,     /* i/o: State struct                */
409     enum Mode mode,   /* i  : coder mode                  */
410     Word16 x[],       /* i  : Input signal           Q15  */
411     Word16 x_12k2[],  /* i  : Input signal (EFR)     Q15  */
412     Word16 a[],       /* o  : predictor coefficients Q12  */
413     CommonAmrTbls* common_amr_tbls, /* i : ptr to struct with table ptrs */
414     Flag   *pOverflow
415 )
416 {
417     Word16 rc[4];                  /* First 4 reflection coefficients Q15 */
418     Word16 rLow[MP1], rHigh[MP1];  /* Autocorrelations low and hi      */
419     /* No fixed Q value but normalized  */
420     /* so that overflow is avoided      */
421
422     const Word16* window_160_80_ptr = common_amr_tbls->window_160_80_ptr;
423     const Word16* window_232_8_ptr = common_amr_tbls->window_232_8_ptr;
424     const Word16* window_200_40_ptr = common_amr_tbls->window_200_40_ptr;
425
426     if (mode == MR122)
427     {
428         /* Autocorrelations */
429         Autocorr(x_12k2, M, rHigh, rLow, window_160_80_ptr, pOverflow);
430         /* Lag windowing    */
431         Lag_window(M, rHigh, rLow, pOverflow);
432         /* Levinson Durbin  */
433         Levinson(st->levinsonSt, rHigh, rLow, &a[MP1], rc, pOverflow);
434
435         /* Autocorrelations */
436         Autocorr(x_12k2, M, rHigh, rLow, window_232_8_ptr, pOverflow);
437         /* Lag windowing    */
438         Lag_window(M, rHigh, rLow, pOverflow);
439         /* Levinson Durbin  */
440         Levinson(st->levinsonSt, rHigh, rLow, &a[MP1 * 3], rc, pOverflow);
441     }
442     else
443     {
444         /* Autocorrelations */
445         Autocorr(x, M, rHigh, rLow, window_200_40_ptr, pOverflow);
446         /* Lag windowing    */
447         Lag_window(M, rHigh, rLow, pOverflow);
448         /* Levinson Durbin  */
449         Levinson(st->levinsonSt, rHigh, rLow, &a[MP1 * 3], rc, pOverflow);
450     }
451
452 }
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467