Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / src / lsp.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  Filename: lsp.cpp
31  Functions:
32
33 ------------------------------------------------------------------------------
34  MODULE DESCRIPTION
35
36
37 ------------------------------------------------------------------------------
38 */
39
40 /*----------------------------------------------------------------------------
41 ; INCLUDES
42 ----------------------------------------------------------------------------*/
43 #include "lsp.h"
44 #include "typedef.h"
45 #include "q_plsf.h"
46 #include "az_lsp.h"
47 #include "int_lpc.h"
48 #include "lsp_tab.h"
49 #include "oscl_mem.h"
50
51 /*----------------------------------------------------------------------------
52 ; MACROS
53 ; Define module specific macros here
54 ----------------------------------------------------------------------------*/
55
56 /*----------------------------------------------------------------------------
57 ; DEFINES
58 ; Include all pre-processor statements here. Include conditional
59 ; compile variables also.
60 ----------------------------------------------------------------------------*/
61
62 /*----------------------------------------------------------------------------
63 ; LOCAL FUNCTION DEFINITIONS
64 ; Function Prototype declaration
65 ----------------------------------------------------------------------------*/
66
67 /*----------------------------------------------------------------------------
68 ; LOCAL VARIABLE DEFINITIONS
69 ; Variable declaration - defined here and used outside this module
70 ----------------------------------------------------------------------------*/
71
72
73 /*
74 ------------------------------------------------------------------------------
75  FUNCTION NAME: lsp_init (lspState **st)
76 ------------------------------------------------------------------------------
77  INPUT AND OUTPUT DEFINITIONS
78
79  Inputs:
80     st = Pointer to type lspState
81
82  Outputs:
83     st = Pointer to type lspState -- values are initialized.
84
85  Returns:
86     None
87
88  Global Variables Used:
89     lsp_init_data = Word16 array.
90
91
92  Local Variables Needed:
93     None
94
95 ------------------------------------------------------------------------------
96  FUNCTION DESCRIPTION
97
98     Initializes lsp state data.
99
100 ------------------------------------------------------------------------------
101  REQUIREMENTS
102
103  None
104
105 ------------------------------------------------------------------------------
106  REFERENCES
107
108  lsp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
109
110 ------------------------------------------------------------------------------
111  PSEUDO-CODE
112
113
114 ------------------------------------------------------------------------------
115  CAUTION [optional]
116  [State any special notes, constraints or cautions for users of this function]
117
118 ------------------------------------------------------------------------------
119 */
120
121 OSCL_EXPORT_REF Word16 lsp_init(lspState **st)
122 {
123     lspState* s;
124
125     if (st == (lspState **) NULL)
126     {
127         /* fprintf(stderr, "lsp_init: invalid parameter\n"); */
128         return -1;
129     }
130
131     *st = NULL;
132
133     /* allocate memory */
134     if ((s = (lspState *) oscl_malloc(sizeof(lspState))) == NULL)
135     {
136         /* fprintf(stderr, "lsp_init: can not malloc state structure\n"); */
137         return -1;
138     }
139
140     /* Initialize quantization state */
141     if (0 != Q_plsf_init(&s->qSt))
142     {
143         return -1;
144     }
145
146     if (0 != lsp_reset(s))
147     {
148         return -1;
149     }
150
151     *st = s;
152
153     return 0;
154 }
155
156
157
158
159
160 /*
161 ------------------------------------------------------------------------------
162  FUNCTION NAME: lsp_reset
163 ------------------------------------------------------------------------------
164  INPUT AND OUTPUT DEFINITIONS
165
166  Inputs:
167     st = Pointer to type lspState
168
169  Outputs:
170     st = Pointer to type lspState -- values are reset.
171
172  Returns:
173     None
174
175  Global Variables Used:
176     None
177
178  Local Variables Needed:
179     None
180
181 ------------------------------------------------------------------------------
182  FUNCTION DESCRIPTION
183
184     resets lsp_state data
185 ------------------------------------------------------------------------------
186  REQUIREMENTS
187
188  None
189
190 ------------------------------------------------------------------------------
191  REFERENCES
192
193  lsp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
194
195 ------------------------------------------------------------------------------
196  PSEUDO-CODE
197
198
199 ------------------------------------------------------------------------------
200  CAUTION [optional]
201  [State any special notes, constraints or cautions for users of this function]
202
203 ------------------------------------------------------------------------------
204 */
205 OSCL_EXPORT_REF Word16 lsp_reset(lspState *st)
206 {
207
208     if (st == (lspState *) NULL)
209     {
210         /* fprintf(stderr, "lsp_reset: invalid parameter\n"); */
211         return -1;
212     }
213
214     /* Init lsp_old[] */
215     oscl_memcpy(st->lsp_old,   lsp_init_data,   M*sizeof(Word16));
216
217     /* Initialize lsp_old_q[] */
218     oscl_memcpy(st->lsp_old_q,   st->lsp_old,  M*sizeof(Word16));
219
220     /* Reset quantization state */
221     Q_plsf_reset(st->qSt);
222
223     return 0;
224 }
225
226
227
228
229
230
231
232 /*
233 ------------------------------------------------------------------------------
234  FUNCTION NAME: lsp_exit
235 ------------------------------------------------------------------------------
236  INPUT AND OUTPUT DEFINITIONS
237
238  Inputs:
239     st = Pointer to type lspState
240
241  Outputs:
242     None
243
244  Returns:
245     None
246
247  Global Variables Used:
248     None
249
250  Local Variables Needed:
251     None
252
253 ------------------------------------------------------------------------------
254  FUNCTION DESCRIPTION
255
256     Frees memory used by lspState.
257
258 ------------------------------------------------------------------------------
259  REQUIREMENTS
260
261  None
262
263 ------------------------------------------------------------------------------
264  REFERENCES
265
266  lsp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
267
268 ------------------------------------------------------------------------------
269  PSEUDO-CODE
270
271
272 ------------------------------------------------------------------------------
273  CAUTION [optional]
274  [State any special notes, constraints or cautions for users of this function]
275
276 ------------------------------------------------------------------------------
277 */
278 OSCL_EXPORT_REF void lsp_exit(lspState **st)
279 {
280     if (st == NULL || *st == NULL)
281         return;
282
283     /* Deallocate members */
284     Q_plsf_exit(&(*st)->qSt);
285
286     /* deallocate memory */
287     oscl_free(*st);
288     *st = NULL;
289
290     return;
291 }
292
293
294
295 /*
296 ------------------------------------------------------------------------------
297  FUNCTION NAME: lsp
298 ------------------------------------------------------------------------------
299  INPUT AND OUTPUT DEFINITIONS
300
301
302
303  Inputs:
304     st = Pointer to type lspState -- State struct
305     req_mode = enum Mode -- requested coder mode
306     used_mode = enum Mode -- used coder mode
307     az = array of type Word16 -- interpolated LP parameters Q12
308
309  Outputs:
310     azQ = array of type Word16 -- quantization interpol. LP parameters Q12
311     lsp_new = array of type Word16 -- new lsp vector
312     anap = Double pointer of type Word16 -- analysis parameters
313     pOverflow = Pointer to type Flag -- Flag set when overflow occurs
314     st = Pointer to type lspState -- State struct
315     az = array of type Word16 -- interpolated LP parameters Q12
316
317  Returns:
318     None
319
320  Global Variables Used:
321     None
322
323  Local Variables Needed:
324     None
325
326 ------------------------------------------------------------------------------
327  FUNCTION DESCRIPTION
328
329
330 ------------------------------------------------------------------------------
331  REQUIREMENTS
332
333  None
334
335 ------------------------------------------------------------------------------
336  REFERENCES
337
338  lsp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
339
340 ------------------------------------------------------------------------------
341  PSEUDO-CODE
342
343
344 ------------------------------------------------------------------------------
345  CAUTION [optional]
346  [State any special notes, constraints or cautions for users of this function]
347
348 ------------------------------------------------------------------------------
349 */
350 OSCL_EXPORT_REF void lsp(lspState *st,       /* i/o : State struct            */
351                          enum Mode req_mode, /* i   : requested coder mode                    */
352                          enum Mode used_mode,/* i   : used coder mode                         */
353                          Word16 az[],        /* i/o : interpolated LP parameters Q12          */
354                          Word16 azQ[],       /* o   : quantization interpol. LP parameters Q12*/
355                          Word16 lsp_new[],   /* o   : new lsp vector                          */
356                          Word16 **anap,      /* o   : analysis parameters                     */
357                          Flag   *pOverflow)  /* o   : Flag set when overflow occurs           */
358
359 {
360     Word16 lsp_new_q[M];    /* LSPs at 4th subframe           */
361     Word16 lsp_mid[M], lsp_mid_q[M];    /* LSPs at 2nd subframe           */
362
363     Word16 pred_init_i; /* init index for MA prediction in DTX mode */
364
365     if (req_mode == MR122)
366     {
367         Az_lsp(&az[MP1], lsp_mid, st->lsp_old, pOverflow);
368         Az_lsp(&az[MP1 * 3], lsp_new, lsp_mid, pOverflow);
369
370         /*--------------------------------------------------------------------*
371          * Find interpolated LPC parameters in all subframes (both quantized  *
372          * and unquantized).                                                  *
373          * The interpolated parameters are in array A_t[] of size (M+1)*4     *
374          * and the quantized interpolated parameters are in array Aq_t[]      *
375          *--------------------------------------------------------------------*/
376         Int_lpc_1and3_2(st->lsp_old, lsp_mid, lsp_new, az, pOverflow);
377
378         if (used_mode != MRDTX)
379         {
380             /* LSP quantization (lsp_mid[] and lsp_new[] jointly quantized) */
381             Q_plsf_5(
382                 st->qSt,
383                 lsp_mid,
384                 lsp_new,
385                 lsp_mid_q,
386                 lsp_new_q,
387                 *anap,
388                 pOverflow);
389
390             Int_lpc_1and3(st->lsp_old_q, lsp_mid_q, lsp_new_q, azQ, pOverflow);
391
392             /* Advance analysis parameters pointer */
393             (*anap) += 5;
394         }
395     }
396     else
397     {
398         Az_lsp(&az[MP1 * 3], lsp_new, st->lsp_old, pOverflow);  /* From A(z) to lsp  */
399
400         /*--------------------------------------------------------------------*
401          * Find interpolated LPC parameters in all subframes (both quantized  *
402          * and unquantized).                                                  *
403          * The interpolated parameters are in array A_t[] of size (M+1)*4     *
404          * and the quantized interpolated parameters are in array Aq_t[]      *
405          *--------------------------------------------------------------------*/
406
407         Int_lpc_1to3_2(st->lsp_old, lsp_new, az, pOverflow);
408
409         if (used_mode != MRDTX)
410         {
411             /* LSP quantization */
412             Q_plsf_3(
413                 st->qSt,
414                 req_mode,
415                 lsp_new,
416                 lsp_new_q,
417                 *anap,
418                 &pred_init_i,
419                 pOverflow);
420
421             Int_lpc_1to3(
422                 st->lsp_old_q,
423                 lsp_new_q,
424                 azQ,
425                 pOverflow);
426
427             /* Advance analysis parameters pointer */
428             (*anap) += 3;
429         }
430     }
431
432     /* update the LSPs for the next frame */
433     oscl_memcpy(st->lsp_old,   lsp_new,   M*sizeof(Word16));
434
435     if (used_mode != MRDTX)
436     {
437         oscl_memcpy(st->lsp_old_q, lsp_new_q, M*sizeof(Word16));
438     }
439 }
440