Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / dec / src / ex_ctrl.cpp
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  Filename: ex_ctrl.cpp
35
36 ------------------------------------------------------------------------------
37 */
38
39 /*----------------------------------------------------------------------------
40 ; INCLUDES
41 ----------------------------------------------------------------------------*/
42 #include "ex_ctrl.h"
43 #include "typedef.h"
44 #include "cnst.h"
45 #include "set_zero.h"
46 #include "gmed_n.h"
47 #include "sqrt_l.h"
48 #include "basic_op.h"
49 /*----------------------------------------------------------------------------
50 ; MACROS
51 ; Define module specific macros here
52 ----------------------------------------------------------------------------*/
53
54 /*----------------------------------------------------------------------------
55 ; DEFINES
56 ; Include all pre-processor statements here. Include conditional
57 ; compile variables also.
58 ----------------------------------------------------------------------------*/
59
60 /*----------------------------------------------------------------------------
61 ; LOCAL FUNCTION DEFINITIONS
62 ; Function Prototype declaration
63 ----------------------------------------------------------------------------*/
64
65 /*----------------------------------------------------------------------------
66 ; LOCAL VARIABLE DEFINITIONS
67 ; Variable declaration - defined here and used outside this module
68 ----------------------------------------------------------------------------*/
69
70 /*
71 ------------------------------------------------------------------------------
72  FUNCTION NAME: ex_ctrl
73 ------------------------------------------------------------------------------
74  INPUT AND OUTPUT DEFINITIONS
75
76  Inputs:
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
83
84  Outputs:
85  pOverflow = 1 if overflow exists in the math functions called by this function.
86
87  Returns:
88     None
89
90  Global Variables Used:
91     None
92
93  Local Variables Needed:
94     None
95
96 ------------------------------------------------------------------------------
97  FUNCTION DESCRIPTION
98
99  Function    : Ex_ctrl
100  Purpose     : Charaterice synthesis speech and detect background noise
101  Returns     : background noise decision; 0 = no bgn, 1 = bgn
102
103 ------------------------------------------------------------------------------
104  REQUIREMENTS
105
106  None
107
108 ------------------------------------------------------------------------------
109  REFERENCES
110
111  ex_ctrl.c, 3GPP TS 26.101 version 4.1.0 Release 4, June 2001
112
113 ------------------------------------------------------------------------------
114  PSEUDO-CODE
115
116
117
118 ------------------------------------------------------------------------------
119  CAUTION [optional]
120  [State any special notes, constraints or cautions for users of this function]
121
122 ------------------------------------------------------------------------------
123 */
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   */
130                Flag   *pOverflow
131               )
132 {
133     Word16 i, exp;
134     Word16 testEnergy, scaleFactor, avgEnergy, prevEnergy;
135     Word32 t0;
136
137     /* get target level */
138     avgEnergy = gmed_n(exEnergyHist, 9);
139
140     prevEnergy = (exEnergyHist[7] + exEnergyHist[8]) >> 1;
141     if (exEnergyHist[8] < prevEnergy)
142     {
143         prevEnergy = exEnergyHist[8];
144     }
145
146     /* upscaling to avoid too rapid energy rises  for some cases */
147     if ((excEnergy < avgEnergy) && (excEnergy > 5))
148     {
149         testEnergy = shl(prevEnergy, 2, pOverflow);  /* testEnergy = 4*prevEnergy; */
150
151         if ((voicedHangover < 7) || prevBFI != 0)
152         {
153             /* testEnergy = 3*prevEnergy */
154             testEnergy = sub(testEnergy, prevEnergy, pOverflow);
155         }
156
157         if (avgEnergy > testEnergy)
158         {
159             avgEnergy = testEnergy;
160         }
161
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 */
169         if (t0 > 32767)
170         {
171             t0 = 32767; /* saturate  */
172         }
173         scaleFactor = (Word16)(t0);
174
175         /* test if scaleFactor > 3.0 */
176         if (carefulFlag != 0 && (scaleFactor > 3072))
177         {
178             scaleFactor = 3072;
179         }
180
181         /* scale the excitation by scaleFactor */
182         for (i = 0; i < L_SUBFR; i++)
183         {
184             t0 = L_mult(scaleFactor, excitation[i], pOverflow);
185             t0 = L_shr(t0, 11, pOverflow);
186             excitation[i] = (Word16)(t0);
187         }
188     }
189
190     return 0;
191 }