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 ------------------------------------------------------------------------------
36 ------------------------------------------------------------------------------
39 /*----------------------------------------------------------------------------
41 ----------------------------------------------------------------------------*/
49 /*----------------------------------------------------------------------------
51 ; Define module specific macros here
52 ----------------------------------------------------------------------------*/
54 /*----------------------------------------------------------------------------
56 ; Include all pre-processor statements here. Include conditional
57 ; compile variables also.
58 ----------------------------------------------------------------------------*/
60 /*----------------------------------------------------------------------------
61 ; LOCAL FUNCTION DEFINITIONS
62 ; Function Prototype declaration
63 ----------------------------------------------------------------------------*/
65 /*----------------------------------------------------------------------------
66 ; LOCAL VARIABLE DEFINITIONS
67 ; Variable declaration - defined here and used outside this module
68 ----------------------------------------------------------------------------*/
71 ------------------------------------------------------------------------------
72 FUNCTION NAME: ex_ctrl
73 ------------------------------------------------------------------------------
74 INPUT AND OUTPUT DEFINITIONS
77 excitation = pointer to current subframe excitation of type Word16
78 excEnergy = Exc. Energy, sqrt(totEx*totEx) of type Word16
79 exEnergyHist = pointer to history of subframe energies of type Word16
80 voicedHangover = # of fr. after last voiced fr of type Word16
81 carefulFlag = restrict dynamic in scaling of type Word16
82 pOverflow = pointer to overflow indicator
85 pOverflow = 1 if overflow exists in the math functions called by this function.
90 Global Variables Used:
93 Local Variables Needed:
96 ------------------------------------------------------------------------------
100 Purpose : Charaterice synthesis speech and detect background noise
101 Returns : background noise decision; 0 = no bgn, 1 = bgn
103 ------------------------------------------------------------------------------
108 ------------------------------------------------------------------------------
111 ex_ctrl.c, 3GPP TS 26.101 version 4.1.0 Release 4, June 2001
113 ------------------------------------------------------------------------------
118 ------------------------------------------------------------------------------
120 [State any special notes, constraints or cautions for users of this function]
122 ------------------------------------------------------------------------------
124 Word16 Ex_ctrl(Word16 excitation[], /*i/o: Current subframe excitation */
125 Word16 excEnergy, /* i : Exc. Energy, sqrt(totEx*totEx)*/
126 Word16 exEnergyHist[], /* i : History of subframe energies */
127 Word16 voicedHangover, /* i : # of fr. after last voiced fr.*/
128 Word16 prevBFI, /* i : Set i previous BFI */
129 Word16 carefulFlag, /* i : Restrict dymamic in scaling */
134 Word16 testEnergy, scaleFactor, avgEnergy, prevEnergy;
137 /* get target level */
138 avgEnergy = gmed_n(exEnergyHist, 9);
140 prevEnergy = (exEnergyHist[7] + exEnergyHist[8]) >> 1;
141 if (exEnergyHist[8] < prevEnergy)
143 prevEnergy = exEnergyHist[8];
146 /* upscaling to avoid too rapid energy rises for some cases */
147 if ((excEnergy < avgEnergy) && (excEnergy > 5))
149 testEnergy = shl(prevEnergy, 2, pOverflow); /* testEnergy = 4*prevEnergy; */
151 if ((voicedHangover < 7) || prevBFI != 0)
153 /* testEnergy = 3*prevEnergy */
154 testEnergy = sub(testEnergy, prevEnergy, pOverflow);
157 if (avgEnergy > testEnergy)
159 avgEnergy = testEnergy;
162 /* scaleFactor=avgEnergy/excEnergy in Q0 (const 29 below)*/
163 exp = norm_s(excEnergy);
164 excEnergy = shl(excEnergy, exp, pOverflow);
165 excEnergy = div_s((Word16) 16383, excEnergy);
166 t0 = L_mult(avgEnergy, excEnergy, pOverflow);
167 t0 = L_shr(t0, sub(20, exp, pOverflow), pOverflow);
168 /* const=30 for t0 in Q0, 20 for Q10 */
171 t0 = 32767; /* saturate */
173 scaleFactor = (Word16)(t0);
175 /* test if scaleFactor > 3.0 */
176 if (carefulFlag != 0 && (scaleFactor > 3072))
181 /* scale the excitation by scaleFactor */
182 for (i = 0; i < L_SUBFR; i++)
184 t0 = L_mult(scaleFactor, excitation[i], pOverflow);
185 t0 = L_shr(t0, 11, pOverflow);
186 excitation[i] = (Word16)(t0);