1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
30 ------------------------------------------------------------------------------
40 ------------------------------------------------------------------------------
43 This file contains functions that perform closed-loop fractional pitch
46 ------------------------------------------------------------------------------
50 /*----------------------------------------------------------------------------
52 ----------------------------------------------------------------------------*/
54 #include "basicop_malloc.h"
66 /*----------------------------------------------------------------------------
68 ; Define module specific macros here
69 ----------------------------------------------------------------------------*/
72 /*----------------------------------------------------------------------------
74 ; Include all pre-processor statements here. Include conditional
75 ; compile variables also.
76 ----------------------------------------------------------------------------*/
79 /*----------------------------------------------------------------------------
80 ; LOCAL FUNCTION DEFINITIONS
81 ; Function Prototype declaration
82 ----------------------------------------------------------------------------*/
84 /*----------------------------------------------------------------------------
85 ; LOCAL VARIABLE DEFINITIONS
86 ; Variable declaration - defined here and used outside this module
87 ----------------------------------------------------------------------------*/
91 ------------------------------------------------------------------------------
92 FUNCTION NAME: cl_ltp_init
93 ------------------------------------------------------------------------------
94 INPUT AND OUTPUT DEFINITIONS
97 state = Pointer to a pointer to a clLtpState structure
100 state points to the newly created clLtpState structure.
103 This function returns 0 upon success and -1 upon failure.
105 Global Variables Used:
108 Local Variables Needed:
111 ------------------------------------------------------------------------------
114 Allocates state memory and initializes state memory
116 ------------------------------------------------------------------------------
121 ------------------------------------------------------------------------------
124 cl_ltp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
126 ------------------------------------------------------------------------------
129 int cl_ltp_init (clLtpState **state)
133 if (state == (clLtpState **) NULL){
134 fprintf(stderr, "cl_ltp_init: invalid parameter\n");
140 if ((s= (clLtpState *) malloc(sizeof(clLtpState))) == NULL){
141 fprintf(stderr, "cl_ltp_init: can not malloc state structure\n");
145 // init the sub state
146 if (Pitch_fr_init(&s->pitchSt)) {
159 ------------------------------------------------------------------------------
161 [State any special notes, constraints or cautions for users of this function]
163 ------------------------------------------------------------------------------
166 Word16 cl_ltp_init(clLtpState **state)
170 if (state == (clLtpState **) NULL)
172 /*fprint(stderr, "cl_ltp_init: invalid parameter\n");*/
177 /* allocate memory */
178 if ((s = (clLtpState *) oscl_malloc(sizeof(clLtpState))) == NULL)
180 /*fprint(stderr, "cl_ltp_init: can not malloc state structure\n");*/
184 /* init the sub state */
185 if (Pitch_fr_init(&s->pitchSt))
198 /****************************************************************************/
201 ------------------------------------------------------------------------------
202 FUNCTION NAME: cl_ltp_reset
203 ------------------------------------------------------------------------------
204 INPUT AND OUTPUT DEFINITIONS
207 state = pointer to the clLtpState structure to be reset
210 The state structure pointed to by clLtpState *state is reset.
213 The function returns int 0 if successful, -1 otherwise.
215 Global Variables Used:
218 Local Variables Needed:
221 ------------------------------------------------------------------------------
224 Initializes state memory to zero.
226 ------------------------------------------------------------------------------
229 ------------------------------------------------------------------------------
232 cl_ltp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
234 ------------------------------------------------------------------------------
237 int cl_ltp_reset (clLtpState *state)
239 if (state == (clLtpState *) NULL){
240 fprintf(stderr, "cl_ltp_reset: invalid parameter\n");
244 // Reset pitch search states
245 Pitch_fr_reset (state->pitchSt);
250 ------------------------------------------------------------------------------
252 [State any special notes, constraints or cautions for users of this function]
254 ------------------------------------------------------------------------------
257 Word16 cl_ltp_reset(clLtpState *state)
259 if (state == (clLtpState *) NULL)
261 /*fprint(stderr, "cl_ltp_reset: invalid parameter\n"); */
265 /* Reset pitch search states */
266 Pitch_fr_reset(state->pitchSt);
271 /****************************************************************************/
274 ------------------------------------------------------------------------------
275 FUNCTION NAME: cl_ltp_exit
276 ------------------------------------------------------------------------------
277 INPUT AND OUTPUT DEFINITIONS
280 clLtpState **state = Reference to the state object to be freed.
283 The memory used by the structure which is pointed to by 'state'
289 Global Variables Used:
292 Local Variables Needed:
295 ------------------------------------------------------------------------------
298 The memory used for state memory is freed
301 ------------------------------------------------------------------------------
306 ------------------------------------------------------------------------------
309 cl_ltp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
311 ------------------------------------------------------------------------------
314 void cl_ltp_exit (clLtpState **state)
316 if (state == NULL || *state == NULL)
320 Pitch_fr_exit(&(*state)->pitchSt);
329 ------------------------------------------------------------------------------
331 [State any special notes, constraints or cautions for users of this function]
333 ------------------------------------------------------------------------------
336 void cl_ltp_exit(clLtpState **state)
338 if (state == NULL || *state == NULL)
343 /* dealloc members */
344 Pitch_fr_exit(&(*state)->pitchSt);
346 /* deallocate memory */
353 /****************************************************************************/
356 ------------------------------------------------------------------------------
357 FUNCTION NAME: cl_ltp
358 ------------------------------------------------------------------------------
359 INPUT AND OUTPUT DEFINITIONS
362 clSt = pointer to the clLtpState struct
363 tonSt = pointer to the tonStabState structure
364 mode = codec mode value, of type enum Mode
365 frameOffset = offset to subframe (Word16)
366 T_op = pointer to buffer of open loop pitch lags (Word16)
367 h1 = pointer to impulse response vector (Word16)
368 exc = pointer to excitation vector (Word16)
369 res2 = pointer to long term prediction residual (Word16)
370 xn = pointer to target vector for pitch search (Word16)
371 lsp_flag = LSP resonance flag (Word16)
374 clSt = pointer to the clLtpState struct
375 tonSt = pointer to the tonStabState structure
376 exc = pointer to excitation vector (Word16)
377 res2 = pointer to long term prediction residual (Word16)
378 xn2 = pointer to target vector for codebook search (Word16)
379 yl = pointer to buffer of filtered adaptive excitation (Word16)
380 T0 = pointer to pitch delay (integer part) (Word16)
381 T0_frac = pointer to pitch delay (fractional part) (Word16)
382 gain_pit = pointer to pitch gain (Word16)
383 g_coeff = pointer to array of correlations between xn, y1, & y2 (Word16)
384 anap = pointer to pointer to analysis parameters (Word16)
385 gp_limit = pointer to the pitch gain limit (Word16)
386 pOverflow = pointer to overflow indicator (Flag)
389 return_value = 0 (int)
391 Global Variables Used:
394 Local Variables Needed:
397 ------------------------------------------------------------------------------
400 This function performs closed-loop fractional pitch search.
402 ------------------------------------------------------------------------------
407 ------------------------------------------------------------------------------
410 cl_ltp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
412 ------------------------------------------------------------------------------
413 PSEUDO-CODE FOR cl_ltp
416 clLtpState *clSt, // i/o : State struct
417 tonStabState *tonSt, // i/o : State struct
418 enum Mode mode, // i : coder mode
419 Word16 frameOffset, // i : Offset to subframe
420 Word16 T_op[], // i : Open loop pitch lags
421 Word16 *h1, // i : Impulse response vector Q12
422 Word16 *exc, // i/o : Excitation vector Q0
423 Word16 res2[], // i/o : Long term prediction residual Q0
424 Word16 xn[], // i : Target vector for pitch search Q0
425 Word16 lsp_flag, // i : LSP resonance flag
426 Word16 xn2[], // o : Target vector for codebook search Q0
427 Word16 y1[], // o : Filtered adaptive excitation Q0
428 Word16 *T0, // o : Pitch delay (integer part)
429 Word16 *T0_frac, // o : Pitch delay (fractional part)
430 Word16 *gain_pit, // o : Pitch gain Q14
431 Word16 g_coeff[], // o : Correlations between xn, y1, & y2
432 Word16 **anap, // o : Analysis parameters
433 Word16 *gp_limit // o : pitch gain limit
438 Word32 L_temp; // temporarily variable
439 Word16 resu3; // flag for upsample resolution
442 *----------------------------------------------------------------------*
443 * Closed-loop fractional pitch search *
444 *----------------------------------------------------------------------*
445 *T0 = Pitch_fr(clSt->pitchSt,
446 mode, T_op, exc, xn, h1,
447 L_SUBFR, frameOffset,
448 T0_frac, &resu3, &index);
452 *-----------------------------------------------------------------*
453 * - find unity gain pitch excitation (adapitve codebook entry) *
454 * with fractional interpolation. *
455 * - find filtered pitch exc. y1[]=exc[] convolve with h1[]) *
456 * - compute pitch gain and limit between 0 and 1.2 *
457 * - update target vector for codebook search *
458 * - find LTP residual. *
459 *-----------------------------------------------------------------*
461 Pred_lt_3or6(exc, *T0, *T0_frac, L_SUBFR, resu3);
463 Convolve(exc, h1, y1, L_SUBFR);
465 // gain_pit is Q14 for all modes
466 *gain_pit = G_pitch(mode, xn, y1, g_coeff, L_SUBFR);
469 // check if the pitch gain should be limit due to resonance in LPC filter
472 if ((lsp_flag != 0) &&
473 (sub(*gain_pit, GP_CLIP) > 0))
475 gpc_flag = check_gp_clipping(tonSt, *gain_pit);
478 // special for the MR475, MR515 mode; limit the gain to 0.85 to
479 // cope with bit errors in the decoder in a better way.
480 if ((sub (mode, MR475) == 0) || (sub (mode, MR515) == 0)) {
481 if ( sub (*gain_pit, 13926) > 0) {
482 *gain_pit = 13926; // 0.85 in Q14
496 // For MR122, gain_pit is quantized here and not in gainQuant
497 if (sub(mode, MR122)==0)
499 *(*anap)++ = q_gain_pitch(MR122, *gp_limit, gain_pit,
504 // update target vector und evaluate LTP residual
505 for (i = 0; i < L_SUBFR; i++) {
506 L_temp = L_mult(y1[i], *gain_pit);
507 L_temp = L_shl(L_temp, 1);
508 xn2[i] = sub(xn[i], extract_h(L_temp));
510 L_temp = L_mult(exc[i], *gain_pit);
511 L_temp = L_shl(L_temp, 1);
512 res2[i] = sub(res2[i], extract_h(L_temp));
518 ------------------------------------------------------------------------------
520 [State any special notes, constraints or cautions for users of this function]
522 ------------------------------------------------------------------------------
526 clLtpState *clSt, /* i/o : State struct */
527 tonStabState *tonSt, /* i/o : State struct */
528 enum Mode mode, /* i : coder mode */
529 Word16 frameOffset, /* i : Offset to subframe */
530 Word16 T_op[], /* i : Open loop pitch lags */
531 Word16 *h1, /* i : Impulse response vector Q12 */
532 Word16 *exc, /* i/o : Excitation vector Q0 */
533 Word16 res2[], /* i/o : Long term prediction residual Q0 */
534 Word16 xn[], /* i : Target vector for pitch search Q0 */
535 Word16 lsp_flag, /* i : LSP resonance flag */
536 Word16 xn2[], /* o : Target vector for codebook search Q0 */
537 Word16 yl[], /* o : Filtered adaptive excitation Q0 */
538 Word16 *T0, /* o : Pitch delay (integer part) */
539 Word16 *T0_frac, /* o : Pitch delay (fractional part) */
540 Word16 *gain_pit, /* o : Pitch gain Q14 */
541 Word16 g_coeff[], /* o : Correlations between xn, y1, & y2 */
542 Word16 **anap, /* o : Analysis parameters */
543 Word16 *gp_limit, /* o : pitch gain limit */
544 const Word16* qua_gain_pitch_ptr, /* i : ptr to read-only table */
545 Flag *pOverflow /* o : overflow indicator */
550 Word32 L_temp; /* temporarily variable */
551 Word16 resu3; /* flag for upsample resolution */
560 /*----------------------------------------------------------------------*
561 * Closed-loop fractional pitch search *
562 *----------------------------------------------------------------------*/
580 /*-----------------------------------------------------------------*
581 * - find unity gain pitch excitation (adapitve codebook entry) *
582 * with fractional interpolation. *
583 * - find filtered pitch exc. y1[]=exc[] convolve with h1[]) *
584 * - compute pitch gain and limit between 0 and 1.2 *
585 * - update target vector for codebook search *
586 * - find LTP residual. *
587 *-----------------------------------------------------------------*/
597 Convolve(exc, h1, yl, L_SUBFR);
599 /* gain_pit is Q14 for all modes */
610 /* check if the pitch gain should be limit due to resonance in LPC filter */
614 if ((lsp_flag != 0) && ((Word32)(*gain_pit) > GP_CLIP))
616 gpc_flag = check_gp_clipping(tonSt, *gain_pit, pOverflow);
619 /* special for the MR475, MR515 mode; limit the gain to 0.85 to */
620 /* cope with bit errors in the decoder in a better way. */
622 if ((mode == MR475) || (mode == MR515))
624 *gain_pit = ((Word32) * gain_pit > 13926) ? 13926 : *gain_pit;
638 /* For MR122, gain_pit is quantized here and not in gainQuant */
661 /* update target vector und evaluate LTP residual */
662 for (i = 0; i < L_SUBFR; i++)
664 L_temp = ((Word32) * (p_yl++) * temp) >> 14;
665 *(p_xn2++) = *(p_xn++) - (Word16)L_temp;
667 L_temp = ((Word32) * (p_exc++) * temp) >> 14;
668 res2[i] -= (Word16)L_temp;