ex_ctrl.cpp revision 4f1efc098cb5791c3e9f483f2af84aef70d2d0a0
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/****************************************************************************************
19Portions 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)
26Permission to distribute, modify and use this file under the standard license
27terms listed above has been obtained from the copyright holder.
28****************************************************************************************/
29/*
30------------------------------------------------------------------------------
31
32
33
34 Pathname: ./audio/gsm-amr/c/src/ex_ctrl.c
35 Funtions: ex_ctrl
36
37     Date: 02/08/2002
38
39------------------------------------------------------------------------------
40 REVISION HISTORY
41
42 Description:  Replaced "int" and/or "char" with OSCL defined types.
43
44 Description:
45
46------------------------------------------------------------------------------
47*/
48
49/*----------------------------------------------------------------------------
50; INCLUDES
51----------------------------------------------------------------------------*/
52#include "ex_ctrl.h"
53#include "typedef.h"
54#include "cnst.h"
55#include "copy.h"
56#include "set_zero.h"
57#include "gmed_n.h"
58#include "sqrt_l.h"
59#include "basic_op.h"
60/*----------------------------------------------------------------------------
61; MACROS
62; Define module specific macros here
63----------------------------------------------------------------------------*/
64
65/*----------------------------------------------------------------------------
66; DEFINES
67; Include all pre-processor statements here. Include conditional
68; compile variables also.
69----------------------------------------------------------------------------*/
70
71/*----------------------------------------------------------------------------
72; LOCAL FUNCTION DEFINITIONS
73; Function Prototype declaration
74----------------------------------------------------------------------------*/
75
76/*----------------------------------------------------------------------------
77; LOCAL VARIABLE DEFINITIONS
78; Variable declaration - defined here and used outside this module
79----------------------------------------------------------------------------*/
80
81/*
82------------------------------------------------------------------------------
83 FUNCTION NAME: ex_ctrl
84------------------------------------------------------------------------------
85 INPUT AND OUTPUT DEFINITIONS
86
87 Inputs:
88 excitation = pointer to current subframe excitation of type Word16
89 excEnergy = Exc. Energy, sqrt(totEx*totEx) of type Word16
90 exEnergyHist = pointer to history of subframe energies of type Word16
91 voicedHangover = # of fr. after last voiced fr  of type Word16
92 carefulFlag = restrict dynamic in scaling of type Word16
93 pOverflow = pointer to overflow indicator
94
95 Outputs:
96 pOverflow = 1 if overflow exists in the math functions called by this function.
97
98 Returns:
99    None
100
101 Global Variables Used:
102    None
103
104 Local Variables Needed:
105    None
106
107------------------------------------------------------------------------------
108 FUNCTION DESCRIPTION
109
110 Function    : Ex_ctrl
111 Purpose     : Charaterice synthesis speech and detect background noise
112 Returns     : background noise decision; 0 = no bgn, 1 = bgn
113
114------------------------------------------------------------------------------
115 REQUIREMENTS
116
117 None
118
119------------------------------------------------------------------------------
120 REFERENCES
121
122 ex_ctrl.c, 3GPP TS 26.101 version 4.1.0 Release 4, June 2001
123
124------------------------------------------------------------------------------
125 PSEUDO-CODE
126
127
128
129------------------------------------------------------------------------------
130 RESOURCES USED [optional]
131
132 When the code is written for a specific target processor the
133 the resources used should be documented below.
134
135 HEAP MEMORY USED: x bytes
136
137 STACK MEMORY USED: x bytes
138
139 CLOCK CYCLES: (cycle count equation for this function) + (variable
140                used to represent cycle count for each subroutine
141                called)
142     where: (cycle count variable) = cycle count for [subroutine
143                                     name]
144
145------------------------------------------------------------------------------
146 CAUTION [optional]
147 [State any special notes, constraints or cautions for users of this function]
148
149------------------------------------------------------------------------------
150*/
151Word16 Ex_ctrl(Word16 excitation[],    /*i/o: Current subframe excitation   */
152               Word16 excEnergy,      /* i : Exc. Energy, sqrt(totEx*totEx)*/
153               Word16 exEnergyHist[], /* i : History of subframe energies  */
154               Word16 voicedHangover, /* i : # of fr. after last voiced fr.*/
155               Word16 prevBFI,        /* i : Set i previous BFI            */
156               Word16 carefulFlag,    /* i : Restrict dymamic in scaling   */
157               Flag   *pOverflow
158              )
159{
160    Word16 i, exp;
161    Word16 testEnergy, scaleFactor, avgEnergy, prevEnergy;
162    Word32 t0;
163
164    /* get target level */
165    avgEnergy = gmed_n(exEnergyHist, 9);
166
167    prevEnergy = shr(add(exEnergyHist[7], exEnergyHist[8], pOverflow) , 1, pOverflow);
168
169    if (exEnergyHist[8] < prevEnergy)
170    {
171        prevEnergy = exEnergyHist[8];
172    }
173
174    /* upscaling to avoid too rapid energy rises  for some cases */
175    if ((excEnergy < avgEnergy) && (excEnergy > 5))
176    {
177        testEnergy = shl(prevEnergy, 2, pOverflow);  /* testEnergy = 4*prevEnergy; */
178
179        if ((voicedHangover < 7) || prevBFI != 0)
180        {
181            /* testEnergy = 3*prevEnergy */
182            testEnergy = sub(testEnergy, prevEnergy, pOverflow);
183        }
184
185        if (avgEnergy > testEnergy)
186        {
187            avgEnergy = testEnergy;
188        }
189
190        /* scaleFactor=avgEnergy/excEnergy in Q0 (const 29 below)*/
191        exp = norm_s(excEnergy);
192        excEnergy = shl(excEnergy, exp, pOverflow);
193        excEnergy = div_s((Word16) 16383, excEnergy);
194        t0 = L_mult(avgEnergy, excEnergy, pOverflow);
195        t0 = L_shr(t0, sub(20, exp, pOverflow), pOverflow);
196        /* const=30 for t0 in Q0, 20 for Q10 */
197        if (t0 > 32767)
198        {
199            t0 = 32767; /* saturate  */
200        }
201        scaleFactor = extract_l(t0);
202
203        /* test if scaleFactor > 3.0 */
204        if (carefulFlag != 0 && (scaleFactor > 3072))
205        {
206            scaleFactor = 3072;
207        }
208
209        /* scale the excitation by scaleFactor */
210        for (i = 0; i < L_SUBFR; i++)
211        {
212            t0 = L_mult(scaleFactor, excitation[i], pOverflow);
213            t0 = L_shr(t0, 11, pOverflow);
214            excitation[i] = extract_l(t0);
215        }
216    }
217
218    return 0;
219}
220