Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_wb / dec / src / agc2_amr_wb.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.173
22     ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
23     Available from http://www.3gpp.org
24
25 (C) 2007, 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: agc2_amr_wb.cpp
35
36 ------------------------------------------------------------------------------
37  INPUT AND OUTPUT DEFINITIONS
38
39      int16 * sig_in,            (i)     : postfilter input signal
40      int16 * sig_out,           (i/o)   : postfilter output signal
41      int16 l_trm                (i)     : subframe size
42
43
44 ------------------------------------------------------------------------------
45  FUNCTION DESCRIPTION
46
47     Performs adaptive gain control
48
49 ------------------------------------------------------------------------------
50  REQUIREMENTS
51
52
53 ------------------------------------------------------------------------------
54  REFERENCES
55
56 ------------------------------------------------------------------------------
57  PSEUDO-CODE
58
59 ------------------------------------------------------------------------------
60 */
61
62
63 /*----------------------------------------------------------------------------
64 ; INCLUDES
65 ----------------------------------------------------------------------------*/
66
67 #include "pvamrwbdecoder_cnst.h"
68 #include "pvamrwbdecoder_acelp.h"
69 #include "pv_amr_wb_type_defs.h"
70 #include "pvamrwbdecoder_basic_op.h"
71 #include "pvamrwb_math_op.h"
72
73 /*----------------------------------------------------------------------------
74 ; MACROS
75 ; Define module specific macros here
76 ----------------------------------------------------------------------------*/
77
78
79 /*----------------------------------------------------------------------------
80 ; DEFINES
81 ; Include all pre-processor statements here. Include conditional
82 ; compile variables also.
83 ----------------------------------------------------------------------------*/
84
85 /*----------------------------------------------------------------------------
86 ; LOCAL FUNCTION DEFINITIONS
87 ; Function Prototype declaration
88 ----------------------------------------------------------------------------*/
89
90 /*----------------------------------------------------------------------------
91 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
92 ; Variable declaration - defined here and used outside this module
93 ----------------------------------------------------------------------------*/
94
95 /*----------------------------------------------------------------------------
96 ; EXTERNAL FUNCTION REFERENCES
97 ; Declare functions defined elsewhere and referenced in this module
98 ----------------------------------------------------------------------------*/
99
100 /*----------------------------------------------------------------------------
101 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
102 ; Declare variables used in this module but defined elsewhere
103 ----------------------------------------------------------------------------*/
104
105 /*----------------------------------------------------------------------------
106 ; FUNCTION CODE
107 ----------------------------------------------------------------------------*/
108
109 void agc2_amr_wb(
110     int16 * sig_in,          /* (i)     : postfilter input signal  */
111     int16 * sig_out,         /* (i/o)   : postfilter output signal */
112     int16 l_trm              /* (i)     : subframe size            */
113 )
114 {
115
116     int16 i, exp;
117     int16 gain_in, gain_out, g0;
118     int32 s;
119
120     int16 temp;
121
122     /* calculate gain_out with exponent */
123
124     temp = sig_out[0] >> 2;
125     s = fxp_mul_16by16(temp, temp) << 1;
126     for (i = 1; i < l_trm; i++)
127     {
128         temp = sig_out[i] >> 2;
129         s = mac_16by16_to_int32(s, temp, temp);
130     }
131
132
133     if (s == 0)
134     {
135         return;
136     }
137     exp = normalize_amr_wb(s) - 1;
138     gain_out = amr_wb_round(s << exp);
139
140     /* calculate gain_in with exponent */
141
142     temp = sig_in[0] >> 2;
143     s = mul_16by16_to_int32(temp, temp);
144     for (i = 1; i < l_trm; i++)
145     {
146         temp = sig_in[i] >> 2;
147         s = mac_16by16_to_int32(s, temp, temp);
148     }
149
150
151     if (s == 0)
152     {
153         g0 = 0;
154     }
155     else
156     {
157         i = normalize_amr_wb(s);
158         gain_in = amr_wb_round(s << i);
159         exp -= i;
160
161         /*
162          *  g0 = sqrt(gain_in/gain_out)
163          */
164
165         s = div_16by16(gain_out, gain_in);
166         s = shl_int32(s, 7);                   /* s = gain_out / gain_in */
167         s = shr_int32(s, exp);                 /* add exponent */
168
169         s = one_ov_sqrt(s);
170         g0 = amr_wb_round(shl_int32(s, 9));
171     }
172     /* sig_out(n) = gain(n) sig_out(n) */
173
174     for (i = 0; i < l_trm; i++)
175     {
176         sig_out[i] = extract_h(shl_int32(fxp_mul_16by16(sig_out[i], g0), 3));
177
178     }
179
180     return;
181 }
182