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 Pathname: ./audio/gsm-amr/c/src/cl_ltp.c
35 Funtions: cl_ltp_init
36 cl_ltp_reset
37 cl_ltp_exit
38 cl_ltp
39
40 Date: 06/07/2000
41
42 ------------------------------------------------------------------------------
43 REVISION HISTORY
44
45 Description: Placed into PV template and optimized.
46
47 Description: Synchronized file with UMTS version 3.2.0. Updated coding
48 template. Removed unnecessary include files.
49
50 Description: Removed basic_op.h and oper_32b.h in the include section, and
51 added basicop_malloc.h.
52
53 Description: Fixed typecasting issue in TI C compiler.
54
55 Description: Added pOverflow parameter -- fixed minor template problem.
56
57 Description:
58 1. Eliminated unused include file typedef.h.
59 2. Replaced array addressing by pointers
60 3. Eliminated if-else checks for saturation
61
62 Description: Replaced OSCL mem type functions and eliminated include
63 files that now are chosen by OSCL definitions
64
65 Description: Replaced "int" and/or "char" with OSCL defined types.
66
67 Description:
68
69 ------------------------------------------------------------------------------
70 MODULE DESCRIPTION
71
72 This file contains functions that perform closed-loop fractional pitch
73 search.
74
75 ------------------------------------------------------------------------------
76 */
77
78
79 /*----------------------------------------------------------------------------
80 ; INCLUDES
81 ----------------------------------------------------------------------------*/
82 #include <stdlib.h>
83
84 #include "cl_ltp.h"
85 #include "basicop_malloc.h"
86 #include "cnst.h"
87 #include "convolve.h"
88 #include "g_pitch.h"
89 #include "pred_lt.h"
90 #include "pitch_fr.h"
91 #include "enc_lag3.h"
92 #include "enc_lag6.h"
93 #include "q_gain_p.h"
94 #include "ton_stab.h"
95
96 /*----------------------------------------------------------------------------
97 ; MACROS
98 ; Define module specific macros here
99 ----------------------------------------------------------------------------*/
100
101
102 /*----------------------------------------------------------------------------
103 ; DEFINES
104 ; Include all pre-processor statements here. Include conditional
105 ; compile variables also.
106 ----------------------------------------------------------------------------*/
107
108
109 /*----------------------------------------------------------------------------
110 ; LOCAL FUNCTION DEFINITIONS
111 ; Function Prototype declaration
112 ----------------------------------------------------------------------------*/
113
114 /*----------------------------------------------------------------------------
115 ; LOCAL VARIABLE DEFINITIONS
116 ; Variable declaration - defined here and used outside this module
117 ----------------------------------------------------------------------------*/
118
119
120 /*
121 ------------------------------------------------------------------------------
122 FUNCTION NAME: cl_ltp_init
123 ------------------------------------------------------------------------------
124 INPUT AND OUTPUT DEFINITIONS
125
126 Inputs:
127 state = Pointer to a pointer to a clLtpState structure
128
129 Outputs:
130 state points to the newly created clLtpState structure.
131
132 Returns:
133 This function returns 0 upon success and -1 upon failure.
134
135 Global Variables Used:
136 None
137
138 Local Variables Needed:
139 None
140
141 ------------------------------------------------------------------------------
142 FUNCTION DESCRIPTION
143
144 Allocates state memory and initializes state memory
145
146 ------------------------------------------------------------------------------
147 REQUIREMENTS
148
149 None.
150
151 ------------------------------------------------------------------------------
152 REFERENCES
153
154 cl_ltp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
155
156 ------------------------------------------------------------------------------
157 PSEUDO-CODE
158
159 int cl_ltp_init (clLtpState **state)
160 {
161 clLtpState* s;
162
163 if (state == (clLtpState **) NULL){
164 fprintf(stderr, "cl_ltp_init: invalid parameter\n");
165 return -1;
166 }
167 *state = NULL;
168
169 // allocate memory
170 if ((s= (clLtpState *) malloc(sizeof(clLtpState))) == NULL){
171 fprintf(stderr, "cl_ltp_init: can not malloc state structure\n");
172 return -1;
173 }
174
175 // init the sub state
176 if (Pitch_fr_init(&s->pitchSt)) {
177 cl_ltp_exit(&s);
178 return -1;
179 }
180
181 cl_ltp_reset(s);
182
183 *state = s;
184
185 return 0;
186 }
187
188
189 ------------------------------------------------------------------------------
190 RESOURCES USED [optional]
191
192 When the code is written for a specific target processor the
193 the resources used should be documented below.
194
195 HEAP MEMORY USED: x bytes
196
197 STACK MEMORY USED: x bytes
198
199 CLOCK CYCLES: (cycle count equation for this function) + (variable
200 used to represent cycle count for each subroutine
201 called)
202 where: (cycle count variable) = cycle count for [subroutine
203 name]
204
205 ------------------------------------------------------------------------------
206 CAUTION [optional]
207 [State any special notes, constraints or cautions for users of this function]
208
209 ------------------------------------------------------------------------------
210 */
211
cl_ltp_init(clLtpState ** state)212 Word16 cl_ltp_init(clLtpState **state)
213 {
214 clLtpState* s;
215
216 if (state == (clLtpState **) NULL)
217 {
218 /*fprint(stderr, "cl_ltp_init: invalid parameter\n");*/
219 return(-1);
220 }
221 *state = NULL;
222
223 /* allocate memory */
224 if ((s = (clLtpState *) malloc(sizeof(clLtpState))) == NULL)
225 {
226 /*fprint(stderr, "cl_ltp_init: can not malloc state structure\n");*/
227 return(-1);
228 }
229
230 /* init the sub state */
231 if (Pitch_fr_init(&s->pitchSt))
232 {
233 cl_ltp_exit(&s);
234 return(-1);
235 }
236
237 cl_ltp_reset(s);
238
239 *state = s;
240
241 return(0);
242 }
243
244 /****************************************************************************/
245
246 /*
247 ------------------------------------------------------------------------------
248 FUNCTION NAME: cl_ltp_reset
249 ------------------------------------------------------------------------------
250 INPUT AND OUTPUT DEFINITIONS
251
252 Inputs:
253 state = pointer to the clLtpState structure to be reset
254
255 Outputs:
256 The state structure pointed to by clLtpState *state is reset.
257
258 Returns:
259 The function returns int 0 if successful, -1 otherwise.
260
261 Global Variables Used:
262 None
263
264 Local Variables Needed:
265 None
266
267 ------------------------------------------------------------------------------
268 FUNCTION DESCRIPTION
269
270 Initializes state memory to zero.
271
272 ------------------------------------------------------------------------------
273 REQUIREMENTS
274
275 ------------------------------------------------------------------------------
276 REFERENCES
277
278 cl_ltp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
279
280 ------------------------------------------------------------------------------
281 PSEUDO-CODE
282
283 int cl_ltp_reset (clLtpState *state)
284 {
285 if (state == (clLtpState *) NULL){
286 fprintf(stderr, "cl_ltp_reset: invalid parameter\n");
287 return -1;
288 }
289
290 // Reset pitch search states
291 Pitch_fr_reset (state->pitchSt);
292
293 return 0;
294 }
295
296 ------------------------------------------------------------------------------
297 RESOURCES USED [optional]
298
299 When the code is written for a specific target processor the
300 the resources used should be documented below.
301
302 HEAP MEMORY USED: x bytes
303
304 STACK MEMORY USED: x bytes
305
306 CLOCK CYCLES: (cycle count equation for this function) + (variable
307 used to represent cycle count for each subroutine
308 called)
309 where: (cycle count variable) = cycle count for [subroutine
310 name]
311
312 ------------------------------------------------------------------------------
313 CAUTION [optional]
314 [State any special notes, constraints or cautions for users of this function]
315
316 ------------------------------------------------------------------------------
317 */
318
cl_ltp_reset(clLtpState * state)319 Word16 cl_ltp_reset(clLtpState *state)
320 {
321 if (state == (clLtpState *) NULL)
322 {
323 /*fprint(stderr, "cl_ltp_reset: invalid parameter\n"); */
324 return(-1);
325 }
326
327 /* Reset pitch search states */
328 Pitch_fr_reset(state->pitchSt);
329
330 return(0);
331 }
332
333 /****************************************************************************/
334
335 /*
336 ------------------------------------------------------------------------------
337 FUNCTION NAME: cl_ltp_exit
338 ------------------------------------------------------------------------------
339 INPUT AND OUTPUT DEFINITIONS
340
341 Inputs:
342 clLtpState **state = Reference to the state object to be freed.
343
344 Outputs:
345 The memory used by the structure which is pointed to by 'state'
346 is freed.
347
348 Returns:
349 None
350
351 Global Variables Used:
352 None
353
354 Local Variables Needed:
355 None
356
357 ------------------------------------------------------------------------------
358 FUNCTION DESCRIPTION
359
360 The memory used for state memory is freed
361
362
363 ------------------------------------------------------------------------------
364 REQUIREMENTS
365
366 None
367
368 ------------------------------------------------------------------------------
369 REFERENCES
370
371 cl_ltp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
372
373 ------------------------------------------------------------------------------
374 PSEUDO-CODE
375
376 void cl_ltp_exit (clLtpState **state)
377 {
378 if (state == NULL || *state == NULL)
379 return;
380
381 // dealloc members
382 Pitch_fr_exit(&(*state)->pitchSt);
383
384 // deallocate memory
385 free(*state);
386 *state = NULL;
387
388 return;
389 }
390
391 ------------------------------------------------------------------------------
392 RESOURCES USED [optional]
393
394 When the code is written for a specific target processor the
395 the resources used should be documented below.
396
397 HEAP MEMORY USED: x bytes
398
399 STACK MEMORY USED: x bytes
400
401 CLOCK CYCLES: (cycle count equation for this function) + (variable
402 used to represent cycle count for each subroutine
403 called)
404 where: (cycle count variable) = cycle count for [subroutine
405 name]
406
407 ------------------------------------------------------------------------------
408 CAUTION [optional]
409 [State any special notes, constraints or cautions for users of this function]
410
411 ------------------------------------------------------------------------------
412 */
413
cl_ltp_exit(clLtpState ** state)414 void cl_ltp_exit(clLtpState **state)
415 {
416 if (state == NULL || *state == NULL)
417 {
418 return;
419 }
420
421 /* dealloc members */
422 Pitch_fr_exit(&(*state)->pitchSt);
423
424 /* deallocate memory */
425 free(*state);
426 *state = NULL;
427
428 return;
429 }
430
431 /****************************************************************************/
432
433 /*
434 ------------------------------------------------------------------------------
435 FUNCTION NAME: cl_ltp
436 ------------------------------------------------------------------------------
437 INPUT AND OUTPUT DEFINITIONS
438
439 Inputs:
440 clSt = pointer to the clLtpState struct
441 tonSt = pointer to the tonStabState structure
442 mode = codec mode value, of type enum Mode
443 frameOffset = offset to subframe (Word16)
444 T_op = pointer to buffer of open loop pitch lags (Word16)
445 h1 = pointer to impulse response vector (Word16)
446 exc = pointer to excitation vector (Word16)
447 res2 = pointer to long term prediction residual (Word16)
448 xn = pointer to target vector for pitch search (Word16)
449 lsp_flag = LSP resonance flag (Word16)
450
451 Outputs:
452 clSt = pointer to the clLtpState struct
453 tonSt = pointer to the tonStabState structure
454 exc = pointer to excitation vector (Word16)
455 res2 = pointer to long term prediction residual (Word16)
456 xn2 = pointer to target vector for codebook search (Word16)
457 yl = pointer to buffer of filtered adaptive excitation (Word16)
458 T0 = pointer to pitch delay (integer part) (Word16)
459 T0_frac = pointer to pitch delay (fractional part) (Word16)
460 gain_pit = pointer to pitch gain (Word16)
461 g_coeff = pointer to array of correlations between xn, y1, & y2 (Word16)
462 anap = pointer to pointer to analysis parameters (Word16)
463 gp_limit = pointer to the pitch gain limit (Word16)
464 pOverflow = pointer to overflow indicator (Flag)
465
466 Returns:
467 return_value = 0 (int)
468
469 Global Variables Used:
470 None
471
472 Local Variables Needed:
473 None
474
475 ------------------------------------------------------------------------------
476 FUNCTION DESCRIPTION
477
478 This function performs closed-loop fractional pitch search.
479
480 ------------------------------------------------------------------------------
481 REQUIREMENTS
482
483 None.
484
485 ------------------------------------------------------------------------------
486 REFERENCES
487
488 cl_ltp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
489
490 ------------------------------------------------------------------------------
491 PSEUDO-CODE FOR cl_ltp
492
493 int cl_ltp (
494 clLtpState *clSt, // i/o : State struct
495 tonStabState *tonSt, // i/o : State struct
496 enum Mode mode, // i : coder mode
497 Word16 frameOffset, // i : Offset to subframe
498 Word16 T_op[], // i : Open loop pitch lags
499 Word16 *h1, // i : Impulse response vector Q12
500 Word16 *exc, // i/o : Excitation vector Q0
501 Word16 res2[], // i/o : Long term prediction residual Q0
502 Word16 xn[], // i : Target vector for pitch search Q0
503 Word16 lsp_flag, // i : LSP resonance flag
504 Word16 xn2[], // o : Target vector for codebook search Q0
505 Word16 y1[], // o : Filtered adaptive excitation Q0
506 Word16 *T0, // o : Pitch delay (integer part)
507 Word16 *T0_frac, // o : Pitch delay (fractional part)
508 Word16 *gain_pit, // o : Pitch gain Q14
509 Word16 g_coeff[], // o : Correlations between xn, y1, & y2
510 Word16 **anap, // o : Analysis parameters
511 Word16 *gp_limit // o : pitch gain limit
512 )
513 {
514 Word16 i;
515 Word16 index;
516 Word32 L_temp; // temporarily variable
517 Word16 resu3; // flag for upsample resolution
518 Word16 gpc_flag;
519
520 *----------------------------------------------------------------------*
521 * Closed-loop fractional pitch search *
522 *----------------------------------------------------------------------*
523 *T0 = Pitch_fr(clSt->pitchSt,
524 mode, T_op, exc, xn, h1,
525 L_SUBFR, frameOffset,
526 T0_frac, &resu3, &index);
527
528 *(*anap)++ = index;
529
530 *-----------------------------------------------------------------*
531 * - find unity gain pitch excitation (adapitve codebook entry) *
532 * with fractional interpolation. *
533 * - find filtered pitch exc. y1[]=exc[] convolve with h1[]) *
534 * - compute pitch gain and limit between 0 and 1.2 *
535 * - update target vector for codebook search *
536 * - find LTP residual. *
537 *-----------------------------------------------------------------*
538
539 Pred_lt_3or6(exc, *T0, *T0_frac, L_SUBFR, resu3);
540
541 Convolve(exc, h1, y1, L_SUBFR);
542
543 // gain_pit is Q14 for all modes
544 *gain_pit = G_pitch(mode, xn, y1, g_coeff, L_SUBFR);
545
546
547 // check if the pitch gain should be limit due to resonance in LPC filter
548 gpc_flag = 0;
549 *gp_limit = MAX_16;
550 if ((lsp_flag != 0) &&
551 (sub(*gain_pit, GP_CLIP) > 0))
552 {
553 gpc_flag = check_gp_clipping(tonSt, *gain_pit);
554 }
555
556 // special for the MR475, MR515 mode; limit the gain to 0.85 to
557 // cope with bit errors in the decoder in a better way.
558 if ((sub (mode, MR475) == 0) || (sub (mode, MR515) == 0)) {
559 if ( sub (*gain_pit, 13926) > 0) {
560 *gain_pit = 13926; // 0.85 in Q14
561 }
562
563 if (gpc_flag != 0) {
564 *gp_limit = GP_CLIP;
565 }
566 }
567 else
568 {
569 if (gpc_flag != 0)
570 {
571 *gp_limit = GP_CLIP;
572 *gain_pit = GP_CLIP;
573 }
574 // For MR122, gain_pit is quantized here and not in gainQuant
575 if (sub(mode, MR122)==0)
576 {
577 *(*anap)++ = q_gain_pitch(MR122, *gp_limit, gain_pit,
578 NULL, NULL);
579 }
580 }
581
582 // update target vector und evaluate LTP residual
583 for (i = 0; i < L_SUBFR; i++) {
584 L_temp = L_mult(y1[i], *gain_pit);
585 L_temp = L_shl(L_temp, 1);
586 xn2[i] = sub(xn[i], extract_h(L_temp));
587
588 L_temp = L_mult(exc[i], *gain_pit);
589 L_temp = L_shl(L_temp, 1);
590 res2[i] = sub(res2[i], extract_h(L_temp));
591 }
592
593 return 0;
594 }
595
596 ------------------------------------------------------------------------------
597 RESOURCES USED [optional]
598
599 When the code is written for a specific target processor the
600 the resources used should be documented below.
601
602 HEAP MEMORY USED: x bytes
603
604 STACK MEMORY USED: x bytes
605
606 CLOCK CYCLES: (cycle count equation for this function) + (variable
607 used to represent cycle count for each subroutine
608 called)
609 where: (cycle count variable) = cycle count for [subroutine
610 name]
611
612 ------------------------------------------------------------------------------
613 CAUTION [optional]
614 [State any special notes, constraints or cautions for users of this function]
615
616 ------------------------------------------------------------------------------
617 */
618
cl_ltp(clLtpState * clSt,tonStabState * tonSt,enum Mode mode,Word16 frameOffset,Word16 T_op[],Word16 * h1,Word16 * exc,Word16 res2[],Word16 xn[],Word16 lsp_flag,Word16 xn2[],Word16 yl[],Word16 * T0,Word16 * T0_frac,Word16 * gain_pit,Word16 g_coeff[],Word16 ** anap,Word16 * gp_limit,Flag * pOverflow)619 void cl_ltp(
620 clLtpState *clSt, /* i/o : State struct */
621 tonStabState *tonSt, /* i/o : State struct */
622 enum Mode mode, /* i : coder mode */
623 Word16 frameOffset, /* i : Offset to subframe */
624 Word16 T_op[], /* i : Open loop pitch lags */
625 Word16 *h1, /* i : Impulse response vector Q12 */
626 Word16 *exc, /* i/o : Excitation vector Q0 */
627 Word16 res2[], /* i/o : Long term prediction residual Q0 */
628 Word16 xn[], /* i : Target vector for pitch search Q0 */
629 Word16 lsp_flag, /* i : LSP resonance flag */
630 Word16 xn2[], /* o : Target vector for codebook search Q0 */
631 Word16 yl[], /* o : Filtered adaptive excitation Q0 */
632 Word16 *T0, /* o : Pitch delay (integer part) */
633 Word16 *T0_frac, /* o : Pitch delay (fractional part) */
634 Word16 *gain_pit, /* o : Pitch gain Q14 */
635 Word16 g_coeff[], /* o : Correlations between xn, y1, & y2 */
636 Word16 **anap, /* o : Analysis parameters */
637 Word16 *gp_limit, /* o : pitch gain limit */
638 Flag *pOverflow /* o : overflow indicator */
639 )
640 {
641 register Word16 i;
642 Word16 index;
643 Word32 L_temp; /* temporarily variable */
644 Word16 resu3; /* flag for upsample resolution */
645 Word16 gpc_flag;
646
647 Word16 temp;
648 Word16 *p_exc;
649 Word16 *p_xn;
650 Word16 *p_xn2;
651 Word16 *p_yl;
652
653 /*----------------------------------------------------------------------*
654 * Closed-loop fractional pitch search *
655 *----------------------------------------------------------------------*/
656 *T0 =
657 Pitch_fr(
658 clSt->pitchSt,
659 mode,
660 T_op,
661 exc,
662 xn,
663 h1,
664 L_SUBFR,
665 frameOffset,
666 T0_frac,
667 &resu3,
668 &index,
669 pOverflow);
670
671 *(*anap)++ = index;
672
673 /*-----------------------------------------------------------------*
674 * - find unity gain pitch excitation (adapitve codebook entry) *
675 * with fractional interpolation. *
676 * - find filtered pitch exc. y1[]=exc[] convolve with h1[]) *
677 * - compute pitch gain and limit between 0 and 1.2 *
678 * - update target vector for codebook search *
679 * - find LTP residual. *
680 *-----------------------------------------------------------------*/
681
682 Pred_lt_3or6(
683 exc,
684 *T0,
685 *T0_frac,
686 L_SUBFR,
687 resu3,
688 pOverflow);
689
690 Convolve(exc, h1, yl, L_SUBFR);
691
692 /* gain_pit is Q14 for all modes */
693 *gain_pit =
694 G_pitch(
695 mode,
696 xn,
697 yl,
698 g_coeff,
699 L_SUBFR,
700 pOverflow);
701
702
703 /* check if the pitch gain should be limit due to resonance in LPC filter */
704 gpc_flag = 0;
705 *gp_limit = MAX_16;
706
707 if ((lsp_flag != 0) && ((Word32)(*gain_pit) > GP_CLIP))
708 {
709 gpc_flag = check_gp_clipping(tonSt, *gain_pit, pOverflow);
710 }
711
712 /* special for the MR475, MR515 mode; limit the gain to 0.85 to */
713 /* cope with bit errors in the decoder in a better way. */
714
715 if ((mode == MR475) || (mode == MR515))
716 {
717 *gain_pit = ((Word32) * gain_pit > 13926) ? 13926 : *gain_pit;
718
719 if (gpc_flag != 0)
720 {
721 *gp_limit = GP_CLIP;
722 }
723 }
724 else
725 {
726 if (gpc_flag != 0)
727 {
728 *gp_limit = GP_CLIP;
729 *gain_pit = GP_CLIP;
730 }
731 /* For MR122, gain_pit is quantized here and not in gainQuant */
732 if (mode == MR122)
733 {
734 *(*anap)++ =
735 q_gain_pitch(
736 MR122,
737 *gp_limit,
738 gain_pit,
739 NULL,
740 NULL,
741 pOverflow);
742 }
743 }
744
745
746 p_exc = &exc[0];
747 p_xn = &xn[0];
748 p_xn2 = &xn2[0];
749 p_yl = &yl[0];
750
751 temp = *gain_pit;
752
753 /* update target vector und evaluate LTP residual */
754 for (i = 0; i < L_SUBFR; i++)
755 {
756 L_temp = ((Word32) * (p_yl++) * temp) >> 14;
757 *(p_xn2++) = *(p_xn++) - (Word16)L_temp;
758
759 L_temp = ((Word32) * (p_exc++) * temp) >> 14;
760 res2[i] -= (Word16)L_temp;
761 }
762
763 }
764