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