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 *) calloc(sizeof(lspState), 1)) == 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 lsp_exit(&s);
186 return -1;
187 }
188
189 if (0 != lsp_reset(s))
190 {
191 lsp_exit(&s);
192 return -1;
193 }
194
195 *st = s;
196
197 return 0;
198 }
199
200
201
202
203
204 /*
205 ------------------------------------------------------------------------------
206 FUNCTION NAME: lsp_reset
207 ------------------------------------------------------------------------------
208 INPUT AND OUTPUT DEFINITIONS
209
210 Inputs:
211 st = Pointer to type lspState
212
213 Outputs:
214 st = Pointer to type lspState -- values are reset.
215
216 Returns:
217 None
218
219 Global Variables Used:
220 None
221
222 Local Variables Needed:
223 None
224
225 ------------------------------------------------------------------------------
226 FUNCTION DESCRIPTION
227
228 resets lsp_state data
229 ------------------------------------------------------------------------------
230 REQUIREMENTS
231
232 None
233
234 ------------------------------------------------------------------------------
235 REFERENCES
236
237 lsp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
238
239 ------------------------------------------------------------------------------
240 PSEUDO-CODE
241
242
243 ------------------------------------------------------------------------------
244 RESOURCES USED [optional]
245
246 When the code is written for a specific target processor the
247 the resources used should be documented below.
248
249 HEAP MEMORY USED: x bytes
250
251 STACK MEMORY USED: x bytes
252
253 CLOCK CYCLES: (cycle count equation for this function) + (variable
254 used to represent cycle count for each subroutine
255 called)
256 where: (cycle count variable) = cycle count for [subroutine
257 name]
258
259 ------------------------------------------------------------------------------
260 CAUTION [optional]
261 [State any special notes, constraints or cautions for users of this function]
262
263 ------------------------------------------------------------------------------
264 */
lsp_reset(lspState * st)265 Word16 lsp_reset(lspState *st)
266 {
267
268 if (st == (lspState *) NULL)
269 {
270 /* fprintf(stderr, "lsp_reset: invalid parameter\n"); */
271 return -1;
272 }
273
274 /* Init lsp_old[] */
275 memcpy(st->lsp_old, lsp_init_data, M*sizeof(Word16));
276
277 /* Initialize lsp_old_q[] */
278 memcpy(st->lsp_old_q, st->lsp_old, M*sizeof(Word16));
279
280 /* Reset quantization state */
281 Q_plsf_reset(st->qSt);
282
283 return 0;
284 }
285
286
287
288
289
290
291
292 /*
293 ------------------------------------------------------------------------------
294 FUNCTION NAME: lsp_exit
295 ------------------------------------------------------------------------------
296 INPUT AND OUTPUT DEFINITIONS
297
298 Inputs:
299 st = Pointer to type lspState
300
301 Outputs:
302 None
303
304 Returns:
305 None
306
307 Global Variables Used:
308 None
309
310 Local Variables Needed:
311 None
312
313 ------------------------------------------------------------------------------
314 FUNCTION DESCRIPTION
315
316 Frees memory used by lspState.
317
318 ------------------------------------------------------------------------------
319 REQUIREMENTS
320
321 None
322
323 ------------------------------------------------------------------------------
324 REFERENCES
325
326 lsp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
327
328 ------------------------------------------------------------------------------
329 PSEUDO-CODE
330
331
332 ------------------------------------------------------------------------------
333 RESOURCES USED [optional]
334
335 When the code is written for a specific target processor the
336 the resources used should be documented below.
337
338 HEAP MEMORY USED: x bytes
339
340 STACK MEMORY USED: x bytes
341
342 CLOCK CYCLES: (cycle count equation for this function) + (variable
343 used to represent cycle count for each subroutine
344 called)
345 where: (cycle count variable) = cycle count for [subroutine
346 name]
347
348 ------------------------------------------------------------------------------
349 CAUTION [optional]
350 [State any special notes, constraints or cautions for users of this function]
351
352 ------------------------------------------------------------------------------
353 */
lsp_exit(lspState ** st)354 void lsp_exit(lspState **st)
355 {
356 if (st == NULL || *st == NULL)
357 return;
358
359 /* Deallocate members */
360 Q_plsf_exit(&(*st)->qSt);
361
362 /* deallocate memory */
363 free(*st);
364 *st = NULL;
365
366 return;
367 }
368
369
370
371 /*
372 ------------------------------------------------------------------------------
373 FUNCTION NAME: lsp
374 ------------------------------------------------------------------------------
375 INPUT AND OUTPUT DEFINITIONS
376
377
378
379 Inputs:
380 st = Pointer to type lspState -- State struct
381 req_mode = enum Mode -- requested coder mode
382 used_mode = enum Mode -- used coder mode
383 az = array of type Word16 -- interpolated LP parameters Q12
384
385 Outputs:
386 azQ = array of type Word16 -- quantization interpol. LP parameters Q12
387 lsp_new = array of type Word16 -- new lsp vector
388 anap = Double pointer of type Word16 -- analysis parameters
389 pOverflow = Pointer to type Flag -- Flag set when overflow occurs
390 st = Pointer to type lspState -- State struct
391 az = array of type Word16 -- interpolated LP parameters Q12
392
393 Returns:
394 None
395
396 Global Variables Used:
397 None
398
399 Local Variables Needed:
400 None
401
402 ------------------------------------------------------------------------------
403 FUNCTION DESCRIPTION
404
405
406 ------------------------------------------------------------------------------
407 REQUIREMENTS
408
409 None
410
411 ------------------------------------------------------------------------------
412 REFERENCES
413
414 lsp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
415
416 ------------------------------------------------------------------------------
417 PSEUDO-CODE
418
419
420 ------------------------------------------------------------------------------
421 RESOURCES USED [optional]
422
423 When the code is written for a specific target processor the
424 the resources used should be documented below.
425
426 HEAP MEMORY USED: x bytes
427
428 STACK MEMORY USED: x bytes
429
430 CLOCK CYCLES: (cycle count equation for this function) + (variable
431 used to represent cycle count for each subroutine
432 called)
433 where: (cycle count variable) = cycle count for [subroutine
434 name]
435
436 ------------------------------------------------------------------------------
437 CAUTION [optional]
438 [State any special notes, constraints or cautions for users of this function]
439
440 ------------------------------------------------------------------------------
441 */
lsp(lspState * st,enum Mode req_mode,enum Mode used_mode,Word16 az[],Word16 azQ[],Word16 lsp_new[],Word16 ** anap,Flag * pOverflow)442 void lsp(lspState *st, /* i/o : State struct */
443 enum Mode req_mode, /* i : requested coder mode */
444 enum Mode used_mode,/* i : used coder mode */
445 Word16 az[], /* i/o : interpolated LP parameters Q12 */
446 Word16 azQ[], /* o : quantization interpol. LP parameters Q12*/
447 Word16 lsp_new[], /* o : new lsp vector */
448 Word16 **anap, /* o : analysis parameters */
449 Flag *pOverflow) /* o : Flag set when overflow occurs */
450
451 {
452 Word16 lsp_new_q[M]; /* LSPs at 4th subframe */
453 Word16 lsp_mid[M], lsp_mid_q[M]; /* LSPs at 2nd subframe */
454
455 Word16 pred_init_i; /* init index for MA prediction in DTX mode */
456
457 if (req_mode == MR122)
458 {
459 Az_lsp(&az[MP1], lsp_mid, st->lsp_old, pOverflow);
460 Az_lsp(&az[MP1 * 3], lsp_new, lsp_mid, pOverflow);
461
462 /*--------------------------------------------------------------------*
463 * Find interpolated LPC parameters in all subframes (both quantized *
464 * and unquantized). *
465 * The interpolated parameters are in array A_t[] of size (M+1)*4 *
466 * and the quantized interpolated parameters are in array Aq_t[] *
467 *--------------------------------------------------------------------*/
468 Int_lpc_1and3_2(st->lsp_old, lsp_mid, lsp_new, az, pOverflow);
469
470 if (used_mode != MRDTX)
471 {
472 /* LSP quantization (lsp_mid[] and lsp_new[] jointly quantized) */
473 Q_plsf_5(
474 st->qSt,
475 lsp_mid,
476 lsp_new,
477 lsp_mid_q,
478 lsp_new_q,
479 *anap,
480 pOverflow);
481
482 Int_lpc_1and3(st->lsp_old_q, lsp_mid_q, lsp_new_q, azQ, pOverflow);
483
484 /* Advance analysis parameters pointer */
485 (*anap) += 5;
486 }
487 }
488 else
489 {
490 Az_lsp(&az[MP1 * 3], lsp_new, st->lsp_old, pOverflow); /* From A(z) to lsp */
491
492 /*--------------------------------------------------------------------*
493 * Find interpolated LPC parameters in all subframes (both quantized *
494 * and unquantized). *
495 * The interpolated parameters are in array A_t[] of size (M+1)*4 *
496 * and the quantized interpolated parameters are in array Aq_t[] *
497 *--------------------------------------------------------------------*/
498
499 Int_lpc_1to3_2(st->lsp_old, lsp_new, az, pOverflow);
500
501 if (used_mode != MRDTX)
502 {
503 /* LSP quantization */
504 Q_plsf_3(
505 st->qSt,
506 req_mode,
507 lsp_new,
508 lsp_new_q,
509 *anap,
510 &pred_init_i,
511 pOverflow);
512
513 Int_lpc_1to3(
514 st->lsp_old_q,
515 lsp_new_q,
516 azQ,
517 pOverflow);
518
519 /* Advance analysis parameters pointer */
520 (*anap) += 3;
521 }
522 }
523
524 /* update the LSPs for the next frame */
525 memcpy(st->lsp_old, lsp_new, M*sizeof(Word16));
526
527 if (used_mode != MRDTX)
528 {
529 memcpy(st->lsp_old_q, lsp_new_q, M*sizeof(Word16));
530 }
531 }
532
533