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 ------------------------------------------------------------------------------
34 Pathname: ./audio/gsm-amr/c/src/ton_stab.c
39 ------------------------------------------------------------------------------
42 Description: For check_lsp()
43 1. Eliminated unused include files.
44 2. Replaced array addressing by pointers
45 3. Eliminated math operations that unnecessary checked for
46 saturation this by evaluating the operands
47 For update_gp_clipping()
48 1. Replaced copy() with more efficient memcpy()
49 2. Replaced right shift function with right shift
51 Description: Replaced OSCL mem type functions and eliminated include
52 files that now are chosen by OSCL definitions
54 Description: Replaced "int" and/or "char" with OSCL defined types.
58 ------------------------------------------------------------------------------
62 /*----------------------------------------------------------------------------
64 ----------------------------------------------------------------------------*/
73 /*----------------------------------------------------------------------------
75 ; Define module specific macros here
76 ----------------------------------------------------------------------------*/
79 /*----------------------------------------------------------------------------
81 ; Include all pre-processor statements here. Include conditional
82 ; compile variables also.
83 ----------------------------------------------------------------------------*/
86 /*----------------------------------------------------------------------------
87 ; LOCAL FUNCTION DEFINITIONS
88 ; Function Prototype declaration
89 ----------------------------------------------------------------------------*/
91 /*----------------------------------------------------------------------------
92 ; LOCAL VARIABLE DEFINITIONS
93 ; Variable declaration - defined here and used outside this module
94 ----------------------------------------------------------------------------*/
98 ------------------------------------------------------------------------------
99 FUNCTION NAME: ton_stab_init
100 ------------------------------------------------------------------------------
101 INPUT AND OUTPUT DEFINITIONS
104 state = pointer to pointer to structure type tonStabState.
112 Global Variables Used:
115 Local Variables Needed:
118 ------------------------------------------------------------------------------
121 Function: ton_stab_init
122 Purpose: Allocates state memory and initializes state memory
124 ------------------------------------------------------------------------------
129 ------------------------------------------------------------------------------
132 ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
134 ------------------------------------------------------------------------------
137 int ton_stab_init (tonStabState **state)
141 if (state == (tonStabState **) NULL){
142 // fprintf(stderr, "ton_stab_init: invalid parameter\n");
148 if ((s= (tonStabState *) malloc(sizeof(tonStabState))) == NULL){
149 // fprintf(stderr, "ton_stab_init: can not malloc state structure\n");
161 ------------------------------------------------------------------------------
162 RESOURCES USED [optional]
164 When the code is written for a specific target processor the
165 the resources used should be documented below.
167 HEAP MEMORY USED: x bytes
169 STACK MEMORY USED: x bytes
171 CLOCK CYCLES: (cycle count equation for this function) + (variable
172 used to represent cycle count for each subroutine
174 where: (cycle count variable) = cycle count for [subroutine
177 ------------------------------------------------------------------------------
179 [State any special notes, constraints or cautions for users of this function]
181 ------------------------------------------------------------------------------
184 Word16 ton_stab_init(tonStabState **state)
188 if (state == (tonStabState **) NULL)
190 /* fprintf(stderr, "ton_stab_init: invalid parameter\n"); */
195 /* allocate memory */
196 if ((s = (tonStabState *) malloc(sizeof(tonStabState))) == NULL)
198 /* fprintf(stderr, "ton_stab_init: can not malloc state structure\n"); */
209 /****************************************************************************/
213 ------------------------------------------------------------------------------
214 FUNCTION NAME: ton_stab_reset
215 ------------------------------------------------------------------------------
216 INPUT AND OUTPUT DEFINITIONS
219 st = pointer to pointer to structure type tonStabState.
227 Global Variables Used:
230 Local Variables Needed:
233 ------------------------------------------------------------------------------
236 Function: ton_stab_reset
237 Purpose: Initializes state memory to zero
239 ------------------------------------------------------------------------------
244 ------------------------------------------------------------------------------
247 ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
249 ------------------------------------------------------------------------------
252 int ton_stab_reset (tonStabState *st)
254 if (st == (tonStabState *) NULL){
255 // fprintf(stderr, "ton_stab_init: invalid parameter\n");
259 // initialize tone stabilizer state
261 Set_zero(st->gp, N_FRAME); // Init Gp_Clipping
266 ------------------------------------------------------------------------------
267 RESOURCES USED [optional]
269 When the code is written for a specific target processor the
270 the resources used should be documented below.
272 HEAP MEMORY USED: x bytes
274 STACK MEMORY USED: x bytes
276 CLOCK CYCLES: (cycle count equation for this function) + (variable
277 used to represent cycle count for each subroutine
279 where: (cycle count variable) = cycle count for [subroutine
282 ------------------------------------------------------------------------------
284 [State any special notes, constraints or cautions for users of this function]
286 ------------------------------------------------------------------------------
289 Word16 ton_stab_reset(tonStabState *st)
291 if (st == (tonStabState *) NULL)
293 /* fprintf(stderr, "ton_stab_init: invalid parameter\n"); */
297 /* initialize tone stabilizer state */
299 Set_zero(st->gp, N_FRAME); /* Init Gp_Clipping */
304 /****************************************************************************/
307 ------------------------------------------------------------------------------
308 FUNCTION NAME: ton_stab_exit
309 ------------------------------------------------------------------------------
310 INPUT AND OUTPUT DEFINITIONS
313 state = pointer to pointer to structure type tonStabState.
321 Global Variables Used:
324 Local Variables Needed:
327 ------------------------------------------------------------------------------
330 Function: ton_stab_exit
331 Purpose: The memory used for state memory is freed
333 ------------------------------------------------------------------------------
338 ------------------------------------------------------------------------------
341 ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
343 ------------------------------------------------------------------------------
346 void ton_stab_exit (tonStabState **state)
348 if (state == NULL || *state == NULL)
358 ------------------------------------------------------------------------------
359 RESOURCES USED [optional]
361 When the code is written for a specific target processor the
362 the resources used should be documented below.
364 HEAP MEMORY USED: x bytes
366 STACK MEMORY USED: x bytes
368 CLOCK CYCLES: (cycle count equation for this function) + (variable
369 used to represent cycle count for each subroutine
371 where: (cycle count variable) = cycle count for [subroutine
374 ------------------------------------------------------------------------------
376 [State any special notes, constraints or cautions for users of this function]
378 ------------------------------------------------------------------------------
381 void ton_stab_exit(tonStabState **state)
383 if (state == NULL || *state == NULL)
386 /* deallocate memory */
393 /****************************************************************************/
396 ------------------------------------------------------------------------------
397 FUNCTION NAME: check_lsp
398 ------------------------------------------------------------------------------
399 INPUT AND OUTPUT DEFINITIONS
402 state = pointer to pointer to structure type tonStabState.
403 lsp = pointer to unquantized LSPs of type Word16
406 pOverflow = 1 if there is an overflow else it is zero.
411 Global Variables Used:
414 Local Variables Needed:
417 ------------------------------------------------------------------------------
420 Function: check_lsp()
421 Purpose: Check the LSP's to detect resonances
423 ------------------------------------------------------------------------------
428 ------------------------------------------------------------------------------
431 ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
433 ------------------------------------------------------------------------------
436 Word16 check_lsp(tonStabState *st, // i/o : State struct
437 Word16 *lsp // i : unquantized LSP's
440 Word16 i, dist, dist_min1, dist_min2, dist_th;
442 // Check for a resonance:
443 // Find minimum distance between lsp[i] and lsp[i+1]
446 for (i = 3; i < M-2; i++)
448 dist = sub(lsp[i], lsp[i+1]);
450 if (sub(dist, dist_min1) < 0)
457 for (i = 1; i < 3; i++)
459 dist = sub(lsp[i], lsp[i+1]);
461 if (sub(dist, dist_min2) < 0)
467 if (sub(lsp[1], 32000) > 0)
471 else if (sub(lsp[1], 30500) > 0)
480 if (sub(dist_min1, 1500) < 0 ||
481 sub(dist_min2, dist_th) < 0)
483 st->count = add(st->count, 1);
490 // Need 12 consecutive frames to set the flag
491 if (sub(st->count, 12) >= 0)
502 ------------------------------------------------------------------------------
503 RESOURCES USED [optional]
505 When the code is written for a specific target processor the
506 the resources used should be documented below.
508 HEAP MEMORY USED: x bytes
510 STACK MEMORY USED: x bytes
512 CLOCK CYCLES: (cycle count equation for this function) + (variable
513 used to represent cycle count for each subroutine
515 where: (cycle count variable) = cycle count for [subroutine
518 ------------------------------------------------------------------------------
520 [State any special notes, constraints or cautions for users of this function]
522 ------------------------------------------------------------------------------
525 Word16 check_lsp(tonStabState *st, /* i/o : State struct */
526 Word16 *lsp, /* i : unquantized LSP's */
535 Word16 *p_lsp = &lsp[3];
536 Word16 *p_lsp_1 = &lsp[4];
538 OSCL_UNUSED_ARG(pOverflow);
539 /* Check for a resonance: */
540 /* Find minimum distance between lsp[i] and lsp[i+1] */
543 for (i = 3; i < M - 2; i++)
545 dist = *(p_lsp++) - *(p_lsp_1++);
547 if (dist < dist_min1)
557 for (i = 1; i < 3; i++)
559 dist = *(p_lsp++) - *(p_lsp_1++);
561 if (dist < dist_min2)
571 else if (lsp[1] > 30500)
580 if ((dist_min1 < 1500) || (dist_min2 < dist_th))
589 /* Need 12 consecutive frames to set the flag */
601 /****************************************************************************/
604 ------------------------------------------------------------------------------
605 FUNCTION NAME: check_gp_clipping
606 ------------------------------------------------------------------------------
607 INPUT AND OUTPUT DEFINITIONS
610 state = pointer to pointer to structure type tonStabState.
611 g_pitch = pitch gain of type Word16
614 pOverflow = 1 if there is an overflow else it is zero.
619 Global Variables Used:
622 Local Variables Needed:
625 ------------------------------------------------------------------------------
628 Function: Check_Gp_Clipping()
629 Purpose: Verify that the sum of the last (N_FRAME+1) pitch
630 gains is under a certain threshold.
632 ------------------------------------------------------------------------------
637 ------------------------------------------------------------------------------
640 ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
642 ------------------------------------------------------------------------------
645 Word16 check_gp_clipping(tonStabState *st, // i/o : State struct
646 Word16 g_pitch // i : pitch gain
651 sum = shr(g_pitch, 3); // Division by 8
652 for (i = 0; i < N_FRAME; i++)
654 sum = add(sum, st->gp[i]);
657 if (sub(sum, GP_CLIP) > 0)
667 ------------------------------------------------------------------------------
668 RESOURCES USED [optional]
670 When the code is written for a specific target processor the
671 the resources used should be documented below.
673 HEAP MEMORY USED: x bytes
675 STACK MEMORY USED: x bytes
677 CLOCK CYCLES: (cycle count equation for this function) + (variable
678 used to represent cycle count for each subroutine
680 where: (cycle count variable) = cycle count for [subroutine
683 ------------------------------------------------------------------------------
685 [State any special notes, constraints or cautions for users of this function]
687 ------------------------------------------------------------------------------
690 Word16 check_gp_clipping(tonStabState *st, /* i/o : State struct */
691 Word16 g_pitch, /* i : pitch gain */
698 sum = shr(g_pitch, 3, pOverflow); /* Division by 8 */
699 for (i = 0; i < N_FRAME; i++)
701 sum = add(sum, st->gp[i], pOverflow);
714 /****************************************************************************/
717 ------------------------------------------------------------------------------
718 FUNCTION NAME: update_gp_clipping
719 ------------------------------------------------------------------------------
720 INPUT AND OUTPUT DEFINITIONS
723 state = pointer to pointer to structure type tonStabState.
724 g_pitch = pitch gain of type Word16
727 pOverflow = 1 if there is an overflow else it is zero.
732 Global Variables Used:
735 Local Variables Needed:
738 ------------------------------------------------------------------------------
741 Function: Update_Gp_Clipping()
742 Purpose: Update past pitch gain memory
744 ------------------------------------------------------------------------------
749 ------------------------------------------------------------------------------
752 ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
754 ------------------------------------------------------------------------------
757 void update_gp_clipping(tonStabState *st, // i/o : State struct
758 Word16 g_pitch // i : pitch gain
761 Copy(&st->gp[1], &st->gp[0], N_FRAME-1);
762 st->gp[N_FRAME-1] = shr(g_pitch, 3);
765 ------------------------------------------------------------------------------
766 RESOURCES USED [optional]
768 When the code is written for a specific target processor the
769 the resources used should be documented below.
771 HEAP MEMORY USED: x bytes
773 STACK MEMORY USED: x bytes
775 CLOCK CYCLES: (cycle count equation for this function) + (variable
776 used to represent cycle count for each subroutine
778 where: (cycle count variable) = cycle count for [subroutine
781 ------------------------------------------------------------------------------
783 [State any special notes, constraints or cautions for users of this function]
785 ------------------------------------------------------------------------------
788 void update_gp_clipping(tonStabState *st, /* i/o : State struct */
789 Word16 g_pitch, /* i : pitch gain */
793 OSCL_UNUSED_ARG(pOverflow);
794 for (int i = 0; i < N_FRAME - 1; i++)
796 st->gp[i] = st->gp[i+1];
798 st->gp[N_FRAME-1] = g_pitch >> 3;