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  Pathname: ./audio/gsm-amr/c/src/lsp.c
31  Functions:
32 
33 ------------------------------------------------------------------------------
34  REVISION HISTORY
35 
36  Description: Updated template used to PV coding template.
37  Changed to accept the pOverflow flag for EPOC compatibility.
38 
39  Description: Per review comments, added pOverflow flag to a few forgotten
40  functions.  Removed unnecessary include files.
41 
42  Description:  For lsp_reset() and lsp()
43               1. Replaced copy() with more efficient memcpy().
44               2. Eliminated unused include file copy.h.
45 
46  Description:  For lsp_reset()
47               1. Modified memcpy() operands order.
48 
49  Description:  Replaced OSCL mem type functions and eliminated include
50                files that now are chosen by OSCL definitions
51 
52  Description:  Replaced "int" and/or "char" with OSCL defined types.
53 
54  Who:                           Date:
55  Description:
56 
57 ------------------------------------------------------------------------------
58  MODULE DESCRIPTION
59 
60 
61 ------------------------------------------------------------------------------
62 */
63 
64 /*----------------------------------------------------------------------------
65 ; INCLUDES
66 ----------------------------------------------------------------------------*/
67 #include <stdlib.h>
68 #include <string.h>
69 
70 #include "lsp.h"
71 #include "typedef.h"
72 #include "q_plsf.h"
73 #include "az_lsp.h"
74 #include "int_lpc.h"
75 #include "lsp_tab.h"
76 
77 /*----------------------------------------------------------------------------
78 ; MACROS
79 ; Define module specific macros here
80 ----------------------------------------------------------------------------*/
81 
82 /*----------------------------------------------------------------------------
83 ; DEFINES
84 ; Include all pre-processor statements here. Include conditional
85 ; compile variables also.
86 ----------------------------------------------------------------------------*/
87 
88 /*----------------------------------------------------------------------------
89 ; LOCAL FUNCTION DEFINITIONS
90 ; Function Prototype declaration
91 ----------------------------------------------------------------------------*/
92 
93 /*----------------------------------------------------------------------------
94 ; LOCAL VARIABLE DEFINITIONS
95 ; Variable declaration - defined here and used outside this module
96 ----------------------------------------------------------------------------*/
97 
98 
99 /*
100 ------------------------------------------------------------------------------
101  FUNCTION NAME: lsp_init (lspState **st)
102 ------------------------------------------------------------------------------
103  INPUT AND OUTPUT DEFINITIONS
104 
105  Inputs:
106     st = Pointer to type lspState
107 
108  Outputs:
109     st = Pointer to type lspState -- values are initialized.
110 
111  Returns:
112     None
113 
114  Global Variables Used:
115     lsp_init_data = Word16 array.
116 
117 
118  Local Variables Needed:
119     None
120 
121 ------------------------------------------------------------------------------
122  FUNCTION DESCRIPTION
123 
124     Initializes lsp state data.
125 
126 ------------------------------------------------------------------------------
127  REQUIREMENTS
128 
129  None
130 
131 ------------------------------------------------------------------------------
132  REFERENCES
133 
134  lsp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
135 
136 ------------------------------------------------------------------------------
137  PSEUDO-CODE
138 
139 
140 ------------------------------------------------------------------------------
141  RESOURCES USED [optional]
142 
143  When the code is written for a specific target processor the
144  the resources used should be documented below.
145 
146  HEAP MEMORY USED: x bytes
147 
148  STACK MEMORY USED: x bytes
149 
150  CLOCK CYCLES: (cycle count equation for this function) + (variable
151                 used to represent cycle count for each subroutine
152                 called)
153      where: (cycle count variable) = cycle count for [subroutine
154                                      name]
155 
156 ------------------------------------------------------------------------------
157  CAUTION [optional]
158  [State any special notes, constraints or cautions for users of this function]
159 
160 ------------------------------------------------------------------------------
161 */
162 
lsp_init(lspState ** st)163 Word16 lsp_init(lspState **st)
164 {
165     lspState* s;
166 
167     if (st == (lspState **) NULL)
168     {
169         /* fprintf(stderr, "lsp_init: invalid parameter\n"); */
170         return -1;
171     }
172 
173     *st = NULL;
174 
175     /* allocate memory */
176     if ((s = (lspState *) malloc(sizeof(lspState))) == NULL)
177     {
178         /* fprintf(stderr, "lsp_init: can not malloc state structure\n"); */
179         return -1;
180     }
181 
182     /* Initialize quantization state */
183     if (0 != Q_plsf_init(&s->qSt))
184     {
185         return -1;
186     }
187 
188     if (0 != lsp_reset(s))
189     {
190         return -1;
191     }
192 
193     *st = s;
194 
195     return 0;
196 }
197 
198 
199 
200 
201 
202 /*
203 ------------------------------------------------------------------------------
204  FUNCTION NAME: lsp_reset
205 ------------------------------------------------------------------------------
206  INPUT AND OUTPUT DEFINITIONS
207 
208  Inputs:
209     st = Pointer to type lspState
210 
211  Outputs:
212     st = Pointer to type lspState -- values are reset.
213 
214  Returns:
215     None
216 
217  Global Variables Used:
218     None
219 
220  Local Variables Needed:
221     None
222 
223 ------------------------------------------------------------------------------
224  FUNCTION DESCRIPTION
225 
226     resets lsp_state data
227 ------------------------------------------------------------------------------
228  REQUIREMENTS
229 
230  None
231 
232 ------------------------------------------------------------------------------
233  REFERENCES
234 
235  lsp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
236 
237 ------------------------------------------------------------------------------
238  PSEUDO-CODE
239 
240 
241 ------------------------------------------------------------------------------
242  RESOURCES USED [optional]
243 
244  When the code is written for a specific target processor the
245  the resources used should be documented below.
246 
247  HEAP MEMORY USED: x bytes
248 
249  STACK MEMORY USED: x bytes
250 
251  CLOCK CYCLES: (cycle count equation for this function) + (variable
252                 used to represent cycle count for each subroutine
253                 called)
254      where: (cycle count variable) = cycle count for [subroutine
255                                      name]
256 
257 ------------------------------------------------------------------------------
258  CAUTION [optional]
259  [State any special notes, constraints or cautions for users of this function]
260 
261 ------------------------------------------------------------------------------
262 */
lsp_reset(lspState * st)263 Word16 lsp_reset(lspState *st)
264 {
265 
266     if (st == (lspState *) NULL)
267     {
268         /* fprintf(stderr, "lsp_reset: invalid parameter\n"); */
269         return -1;
270     }
271 
272     /* Init lsp_old[] */
273     memcpy(st->lsp_old,   lsp_init_data,   M*sizeof(Word16));
274 
275     /* Initialize lsp_old_q[] */
276     memcpy(st->lsp_old_q,   st->lsp_old,  M*sizeof(Word16));
277 
278     /* Reset quantization state */
279     Q_plsf_reset(st->qSt);
280 
281     return 0;
282 }
283 
284 
285 
286 
287 
288 
289 
290 /*
291 ------------------------------------------------------------------------------
292  FUNCTION NAME: lsp_exit
293 ------------------------------------------------------------------------------
294  INPUT AND OUTPUT DEFINITIONS
295 
296  Inputs:
297     st = Pointer to type lspState
298 
299  Outputs:
300     None
301 
302  Returns:
303     None
304 
305  Global Variables Used:
306     None
307 
308  Local Variables Needed:
309     None
310 
311 ------------------------------------------------------------------------------
312  FUNCTION DESCRIPTION
313 
314     Frees memory used by lspState.
315 
316 ------------------------------------------------------------------------------
317  REQUIREMENTS
318 
319  None
320 
321 ------------------------------------------------------------------------------
322  REFERENCES
323 
324  lsp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
325 
326 ------------------------------------------------------------------------------
327  PSEUDO-CODE
328 
329 
330 ------------------------------------------------------------------------------
331  RESOURCES USED [optional]
332 
333  When the code is written for a specific target processor the
334  the resources used should be documented below.
335 
336  HEAP MEMORY USED: x bytes
337 
338  STACK MEMORY USED: x bytes
339 
340  CLOCK CYCLES: (cycle count equation for this function) + (variable
341                 used to represent cycle count for each subroutine
342                 called)
343      where: (cycle count variable) = cycle count for [subroutine
344                                      name]
345 
346 ------------------------------------------------------------------------------
347  CAUTION [optional]
348  [State any special notes, constraints or cautions for users of this function]
349 
350 ------------------------------------------------------------------------------
351 */
lsp_exit(lspState ** st)352 void lsp_exit(lspState **st)
353 {
354     if (st == NULL || *st == NULL)
355         return;
356 
357     /* Deallocate members */
358     Q_plsf_exit(&(*st)->qSt);
359 
360     /* deallocate memory */
361     free(*st);
362     *st = NULL;
363 
364     return;
365 }
366 
367 
368 
369 /*
370 ------------------------------------------------------------------------------
371  FUNCTION NAME: lsp
372 ------------------------------------------------------------------------------
373  INPUT AND OUTPUT DEFINITIONS
374 
375 
376 
377  Inputs:
378     st = Pointer to type lspState -- State struct
379     req_mode = enum Mode -- requested coder mode
380     used_mode = enum Mode -- used coder mode
381     az = array of type Word16 -- interpolated LP parameters Q12
382 
383  Outputs:
384     azQ = array of type Word16 -- quantization interpol. LP parameters Q12
385     lsp_new = array of type Word16 -- new lsp vector
386     anap = Double pointer of type Word16 -- analysis parameters
387     pOverflow = Pointer to type Flag -- Flag set when overflow occurs
388     st = Pointer to type lspState -- State struct
389     az = array of type Word16 -- interpolated LP parameters Q12
390 
391  Returns:
392     None
393 
394  Global Variables Used:
395     None
396 
397  Local Variables Needed:
398     None
399 
400 ------------------------------------------------------------------------------
401  FUNCTION DESCRIPTION
402 
403 
404 ------------------------------------------------------------------------------
405  REQUIREMENTS
406 
407  None
408 
409 ------------------------------------------------------------------------------
410  REFERENCES
411 
412  lsp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
413 
414 ------------------------------------------------------------------------------
415  PSEUDO-CODE
416 
417 
418 ------------------------------------------------------------------------------
419  RESOURCES USED [optional]
420 
421  When the code is written for a specific target processor the
422  the resources used should be documented below.
423 
424  HEAP MEMORY USED: x bytes
425 
426  STACK MEMORY USED: x bytes
427 
428  CLOCK CYCLES: (cycle count equation for this function) + (variable
429                 used to represent cycle count for each subroutine
430                 called)
431      where: (cycle count variable) = cycle count for [subroutine
432                                      name]
433 
434 ------------------------------------------------------------------------------
435  CAUTION [optional]
436  [State any special notes, constraints or cautions for users of this function]
437 
438 ------------------------------------------------------------------------------
439 */
lsp(lspState * st,enum Mode req_mode,enum Mode used_mode,Word16 az[],Word16 azQ[],Word16 lsp_new[],Word16 ** anap,Flag * pOverflow)440 void lsp(lspState *st,       /* i/o : State struct                            */
441          enum Mode req_mode, /* i   : requested coder mode                    */
442          enum Mode used_mode,/* i   : used coder mode                         */
443          Word16 az[],        /* i/o : interpolated LP parameters Q12          */
444          Word16 azQ[],       /* o   : quantization interpol. LP parameters Q12*/
445          Word16 lsp_new[],   /* o   : new lsp vector                          */
446          Word16 **anap,      /* o   : analysis parameters                     */
447          Flag   *pOverflow)  /* o   : Flag set when overflow occurs           */
448 
449 {
450     Word16 lsp_new_q[M];    /* LSPs at 4th subframe           */
451     Word16 lsp_mid[M], lsp_mid_q[M];    /* LSPs at 2nd subframe           */
452 
453     Word16 pred_init_i; /* init index for MA prediction in DTX mode */
454 
455     if (req_mode == MR122)
456     {
457         Az_lsp(&az[MP1], lsp_mid, st->lsp_old, pOverflow);
458         Az_lsp(&az[MP1 * 3], lsp_new, lsp_mid, pOverflow);
459 
460         /*--------------------------------------------------------------------*
461          * Find interpolated LPC parameters in all subframes (both quantized  *
462          * and unquantized).                                                  *
463          * The interpolated parameters are in array A_t[] of size (M+1)*4     *
464          * and the quantized interpolated parameters are in array Aq_t[]      *
465          *--------------------------------------------------------------------*/
466         Int_lpc_1and3_2(st->lsp_old, lsp_mid, lsp_new, az, pOverflow);
467 
468         if (used_mode != MRDTX)
469         {
470             /* LSP quantization (lsp_mid[] and lsp_new[] jointly quantized) */
471             Q_plsf_5(
472                 st->qSt,
473                 lsp_mid,
474                 lsp_new,
475                 lsp_mid_q,
476                 lsp_new_q,
477                 *anap,
478                 pOverflow);
479 
480             Int_lpc_1and3(st->lsp_old_q, lsp_mid_q, lsp_new_q, azQ, pOverflow);
481 
482             /* Advance analysis parameters pointer */
483             (*anap) += 5;
484         }
485     }
486     else
487     {
488         Az_lsp(&az[MP1 * 3], lsp_new, st->lsp_old, pOverflow);  /* From A(z) to lsp  */
489 
490         /*--------------------------------------------------------------------*
491          * Find interpolated LPC parameters in all subframes (both quantized  *
492          * and unquantized).                                                  *
493          * The interpolated parameters are in array A_t[] of size (M+1)*4     *
494          * and the quantized interpolated parameters are in array Aq_t[]      *
495          *--------------------------------------------------------------------*/
496 
497         Int_lpc_1to3_2(st->lsp_old, lsp_new, az, pOverflow);
498 
499         if (used_mode != MRDTX)
500         {
501             /* LSP quantization */
502             Q_plsf_3(
503                 st->qSt,
504                 req_mode,
505                 lsp_new,
506                 lsp_new_q,
507                 *anap,
508                 &pred_init_i,
509                 pOverflow);
510 
511             Int_lpc_1to3(
512                 st->lsp_old_q,
513                 lsp_new_q,
514                 azQ,
515                 pOverflow);
516 
517             /* Advance analysis parameters pointer */
518             (*anap) += 3;
519         }
520     }
521 
522     /* update the LSPs for the next frame */
523     memcpy(st->lsp_old,   lsp_new,   M*sizeof(Word16));
524 
525     if (used_mode != MRDTX)
526     {
527         memcpy(st->lsp_old_q, lsp_new_q, M*sizeof(Word16));
528     }
529 }
530 
531