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 Filename: ton_stab.cpp
36 ------------------------------------------------------------------------------
40 /*----------------------------------------------------------------------------
42 ----------------------------------------------------------------------------*/
50 /*----------------------------------------------------------------------------
52 ; Define module specific macros here
53 ----------------------------------------------------------------------------*/
56 /*----------------------------------------------------------------------------
58 ; Include all pre-processor statements here. Include conditional
59 ; compile variables also.
60 ----------------------------------------------------------------------------*/
63 /*----------------------------------------------------------------------------
64 ; LOCAL FUNCTION DEFINITIONS
65 ; Function Prototype declaration
66 ----------------------------------------------------------------------------*/
68 /*----------------------------------------------------------------------------
69 ; LOCAL VARIABLE DEFINITIONS
70 ; Variable declaration - defined here and used outside this module
71 ----------------------------------------------------------------------------*/
75 ------------------------------------------------------------------------------
76 FUNCTION NAME: ton_stab_init
77 ------------------------------------------------------------------------------
78 INPUT AND OUTPUT DEFINITIONS
81 state = pointer to pointer to structure type tonStabState.
89 Global Variables Used:
92 Local Variables Needed:
95 ------------------------------------------------------------------------------
98 Function: ton_stab_init
99 Purpose: Allocates state memory and initializes state memory
101 ------------------------------------------------------------------------------
106 ------------------------------------------------------------------------------
109 ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
111 ------------------------------------------------------------------------------
114 int ton_stab_init (tonStabState **state)
118 if (state == (tonStabState **) NULL){
119 // fprintf(stderr, "ton_stab_init: invalid parameter\n");
125 if ((s= (tonStabState *) malloc(sizeof(tonStabState))) == NULL){
126 // fprintf(stderr, "ton_stab_init: can not malloc state structure\n");
138 ------------------------------------------------------------------------------
140 [State any special notes, constraints or cautions for users of this function]
142 ------------------------------------------------------------------------------
145 Word16 ton_stab_init(tonStabState **state)
149 if (state == (tonStabState **) NULL)
151 /* fprintf(stderr, "ton_stab_init: invalid parameter\n"); */
156 /* allocate memory */
157 if ((s = (tonStabState *) oscl_malloc(sizeof(tonStabState))) == NULL)
159 /* fprintf(stderr, "ton_stab_init: can not malloc state structure\n"); */
170 /****************************************************************************/
174 ------------------------------------------------------------------------------
175 FUNCTION NAME: ton_stab_reset
176 ------------------------------------------------------------------------------
177 INPUT AND OUTPUT DEFINITIONS
180 st = pointer to pointer to structure type tonStabState.
188 Global Variables Used:
191 Local Variables Needed:
194 ------------------------------------------------------------------------------
197 Function: ton_stab_reset
198 Purpose: Initializes state memory to zero
200 ------------------------------------------------------------------------------
205 ------------------------------------------------------------------------------
208 ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
210 ------------------------------------------------------------------------------
213 int ton_stab_reset (tonStabState *st)
215 if (st == (tonStabState *) NULL){
216 // fprintf(stderr, "ton_stab_init: invalid parameter\n");
220 // initialize tone stabilizer state
222 Set_zero(st->gp, N_FRAME); // Init Gp_Clipping
227 ------------------------------------------------------------------------------
229 [State any special notes, constraints or cautions for users of this function]
231 ------------------------------------------------------------------------------
234 Word16 ton_stab_reset(tonStabState *st)
236 if (st == (tonStabState *) NULL)
238 /* fprintf(stderr, "ton_stab_init: invalid parameter\n"); */
242 /* initialize tone stabilizer state */
244 /* Init Gp_Clipping */
245 oscl_memset((void *)st->gp, 0, N_FRAME*sizeof(*st->gp));
251 /****************************************************************************/
254 ------------------------------------------------------------------------------
255 FUNCTION NAME: ton_stab_exit
256 ------------------------------------------------------------------------------
257 INPUT AND OUTPUT DEFINITIONS
260 state = pointer to pointer to structure type tonStabState.
268 Global Variables Used:
271 Local Variables Needed:
274 ------------------------------------------------------------------------------
277 Function: ton_stab_exit
278 Purpose: The memory used for state memory is freed
280 ------------------------------------------------------------------------------
285 ------------------------------------------------------------------------------
288 ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
290 ------------------------------------------------------------------------------
293 void ton_stab_exit (tonStabState **state)
295 if (state == NULL || *state == NULL)
305 ------------------------------------------------------------------------------
307 [State any special notes, constraints or cautions for users of this function]
309 ------------------------------------------------------------------------------
312 void ton_stab_exit(tonStabState **state)
314 if (state == NULL || *state == NULL)
317 /* deallocate memory */
324 /****************************************************************************/
327 ------------------------------------------------------------------------------
328 FUNCTION NAME: check_lsp
329 ------------------------------------------------------------------------------
330 INPUT AND OUTPUT DEFINITIONS
333 state = pointer to pointer to structure type tonStabState.
334 lsp = pointer to unquantized LSPs of type Word16
337 pOverflow = 1 if there is an overflow else it is zero.
342 Global Variables Used:
345 Local Variables Needed:
348 ------------------------------------------------------------------------------
351 Function: check_lsp()
352 Purpose: Check the LSP's to detect resonances
354 ------------------------------------------------------------------------------
359 ------------------------------------------------------------------------------
362 ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
364 ------------------------------------------------------------------------------
367 Word16 check_lsp(tonStabState *st, // i/o : State struct
368 Word16 *lsp // i : unquantized LSP's
371 Word16 i, dist, dist_min1, dist_min2, dist_th;
373 // Check for a resonance:
374 // Find minimum distance between lsp[i] and lsp[i+1]
377 for (i = 3; i < M-2; i++)
379 dist = sub(lsp[i], lsp[i+1]);
381 if (sub(dist, dist_min1) < 0)
388 for (i = 1; i < 3; i++)
390 dist = sub(lsp[i], lsp[i+1]);
392 if (sub(dist, dist_min2) < 0)
398 if (sub(lsp[1], 32000) > 0)
402 else if (sub(lsp[1], 30500) > 0)
411 if (sub(dist_min1, 1500) < 0 ||
412 sub(dist_min2, dist_th) < 0)
414 st->count = add(st->count, 1);
421 // Need 12 consecutive frames to set the flag
422 if (sub(st->count, 12) >= 0)
433 ------------------------------------------------------------------------------
435 [State any special notes, constraints or cautions for users of this function]
437 ------------------------------------------------------------------------------
440 Word16 check_lsp(tonStabState *st, /* i/o : State struct */
441 Word16 *lsp, /* i : unquantized LSP's */
450 Word16 *p_lsp = &lsp[3];
451 Word16 *p_lsp_1 = &lsp[4];
453 OSCL_UNUSED_ARG(pOverflow);
454 /* Check for a resonance: */
455 /* Find minimum distance between lsp[i] and lsp[i+1] */
458 for (i = 3; i < M - 2; i++)
460 dist = *(p_lsp++) - *(p_lsp_1++);
462 if (dist < dist_min1)
472 for (i = 1; i < 3; i++)
474 dist = *(p_lsp++) - *(p_lsp_1++);
476 if (dist < dist_min2)
486 else if (lsp[1] > 30500)
495 if ((dist_min1 < 1500) || (dist_min2 < dist_th))
504 /* Need 12 consecutive frames to set the flag */
516 /****************************************************************************/
519 ------------------------------------------------------------------------------
520 FUNCTION NAME: check_gp_clipping
521 ------------------------------------------------------------------------------
522 INPUT AND OUTPUT DEFINITIONS
525 state = pointer to pointer to structure type tonStabState.
526 g_pitch = pitch gain of type Word16
529 pOverflow = 1 if there is an overflow else it is zero.
534 Global Variables Used:
537 Local Variables Needed:
540 ------------------------------------------------------------------------------
543 Function: Check_Gp_Clipping()
544 Purpose: Verify that the sum of the last (N_FRAME+1) pitch
545 gains is under a certain threshold.
547 ------------------------------------------------------------------------------
552 ------------------------------------------------------------------------------
555 ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
557 ------------------------------------------------------------------------------
560 Word16 check_gp_clipping(tonStabState *st, // i/o : State struct
561 Word16 g_pitch // i : pitch gain
566 sum = shr(g_pitch, 3); // Division by 8
567 for (i = 0; i < N_FRAME; i++)
569 sum = add(sum, st->gp[i]);
572 if (sub(sum, GP_CLIP) > 0)
582 ------------------------------------------------------------------------------
584 [State any special notes, constraints or cautions for users of this function]
586 ------------------------------------------------------------------------------
589 Word16 check_gp_clipping(tonStabState *st, /* i/o : State struct */
590 Word16 g_pitch, /* i : pitch gain */
597 sum = shr(g_pitch, 3, pOverflow); /* Division by 8 */
598 for (i = 0; i < N_FRAME; i++)
600 sum = add_16(sum, st->gp[i], pOverflow);
613 /****************************************************************************/
616 ------------------------------------------------------------------------------
617 FUNCTION NAME: update_gp_clipping
618 ------------------------------------------------------------------------------
619 INPUT AND OUTPUT DEFINITIONS
622 state = pointer to pointer to structure type tonStabState.
623 g_pitch = pitch gain of type Word16
626 pOverflow = 1 if there is an overflow else it is zero.
631 Global Variables Used:
634 Local Variables Needed:
637 ------------------------------------------------------------------------------
640 Function: Update_Gp_Clipping()
641 Purpose: Update past pitch gain memory
643 ------------------------------------------------------------------------------
648 ------------------------------------------------------------------------------
651 ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
653 ------------------------------------------------------------------------------
656 void update_gp_clipping(tonStabState *st, // i/o : State struct
657 Word16 g_pitch // i : pitch gain
660 Copy(&st->gp[1], &st->gp[0], N_FRAME-1);
661 st->gp[N_FRAME-1] = shr(g_pitch, 3);
664 ------------------------------------------------------------------------------
666 [State any special notes, constraints or cautions for users of this function]
668 ------------------------------------------------------------------------------
671 void update_gp_clipping(tonStabState *st, /* i/o : State struct */
672 Word16 g_pitch, /* i : pitch gain */
676 OSCL_UNUSED_ARG(pOverflow);
677 for (int i = 0; i < N_FRAME - 1; i++)
679 st->gp[i] = st->gp[i+1];
681 st->gp[N_FRAME-1] = g_pitch >> 3;