Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_wb / dec / src / phase_dispersion.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: phase_dispersion.cpp
35
36 ------------------------------------------------------------------------------
37  INPUT AND OUTPUT DEFINITIONS
38
39      int16 gain_code,               (i) Q0  : gain of code
40      int16 gain_pit,                (i) Q14 : gain of pitch
41      int16 code[],                  (i/o)   : code vector
42      int16 mode,                    (i)     : level, 0=hi, 1=lo, 2=off
43      int16 disp_mem[],              (i/o)   : static memory (size = 8)
44      int16 ScratchMem[]
45
46 ------------------------------------------------------------------------------
47  FUNCTION DESCRIPTION
48
49     post-processing to enhance noise in low bit rate.
50
51 ------------------------------------------------------------------------------
52  REQUIREMENTS
53
54
55 ------------------------------------------------------------------------------
56  REFERENCES
57
58 ------------------------------------------------------------------------------
59  PSEUDO-CODE
60
61 ------------------------------------------------------------------------------
62 */
63
64
65 /*----------------------------------------------------------------------------
66 ; INCLUDES
67 ----------------------------------------------------------------------------*/
68
69 #include "pv_amr_wb_type_defs.h"
70 #include "pvamrwbdecoder_basic_op.h"
71 #include "pvamrwbdecoder_cnst.h"
72 #include "pvamrwbdecoder_mem_funcs.h"
73 #include "pvamrwbdecoder_acelp.h"
74 #include "pvamrwb_math_op.h"
75
76
77 /*----------------------------------------------------------------------------
78 ; MACROS
79 ; Define module specific macros here
80 ----------------------------------------------------------------------------*/
81
82
83 /*----------------------------------------------------------------------------
84 ; DEFINES
85 ; Include all pre-processor statements here. Include conditional
86 ; compile variables also.
87 ----------------------------------------------------------------------------*/
88 #define pitch_0_9  14746                   /* 0.9 in Q14 */
89 #define pitch_0_6  9830                    /* 0.6 in Q14 */
90 #define L_SUBFR 64
91
92 /*----------------------------------------------------------------------------
93 ; LOCAL FUNCTION DEFINITIONS
94 ; Function Prototype declaration
95 ----------------------------------------------------------------------------*/
96
97 /*----------------------------------------------------------------------------
98 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
99 ; Variable declaration - defined here and used outside this module
100 ----------------------------------------------------------------------------*/
101 /* impulse response with phase dispersion */
102
103 /* 2.0 - 6.4 kHz phase dispersion */
104 static const int16 ph_imp_low[L_SUBFR] =
105 {
106     20182,  9693,  3270, -3437, 2864, -5240,  1589, -1357,
107     600,  3893, -1497,  -698, 1203, -5249,  1199,  5371,
108     -1488,  -705, -2887,  1976,  898,   721, -3876,  4227,
109     -5112,  6400, -1032, -4725, 4093, -4352,  3205,  2130,
110     -1996, -1835,  2648, -1786, -406,   573,  2484, -3608,
111     3139, -1363, -2566,  3808, -639, -2051,  -541,  2376,
112     3932, -6262,  1432, -3601, 4889,   370,   567, -1163,
113     -2854,  1914,    39, -2418, 3454,  2975, -4021,  3431
114 };
115
116 /* 3.2 - 6.4 kHz phase dispersion */
117 static const int16 ph_imp_mid[L_SUBFR] =
118 {
119     24098, 10460, -5263,  -763,  2048,  -927,  1753, -3323,
120     2212,   652, -2146,  2487, -3539,  4109, -2107,  -374,
121     -626,  4270, -5485,  2235,  1858, -2769,   744,  1140,
122     -763, -1615,  4060, -4574,  2982, -1163,   731, -1098,
123     803,   167,  -714,   606,  -560,   639,    43, -1766,
124     3228, -2782,   665,   763,   233, -2002,  1291,  1871,
125     -3470,  1032,  2710, -4040,  3624, -4214,  5292, -4270,
126     1563,   108,  -580,  1642, -2458,   957,  544,   2540
127 };
128
129 /*----------------------------------------------------------------------------
130 ; EXTERNAL FUNCTION REFERENCES
131 ; Declare functions defined elsewhere and referenced in this module
132 ----------------------------------------------------------------------------*/
133
134 /*----------------------------------------------------------------------------
135 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
136 ; Declare variables used in this module but defined elsewhere
137 ----------------------------------------------------------------------------*/
138
139 /*----------------------------------------------------------------------------
140 ; FUNCTION CODE
141 ----------------------------------------------------------------------------*/
142
143 void phase_dispersion(
144     int16 gain_code,             /* (i) Q0  : gain of code             */
145     int16 gain_pit,              /* (i) Q14 : gain of pitch            */
146     int16 code[],                /* (i/o)   : code vector              */
147     int16 mode,                  /* (i)     : level, 0=hi, 1=lo, 2=off */
148     int16 disp_mem[],            /* (i/o)   : static memory (size = 8) */
149     int16 ScratchMem[]
150 )
151 {
152     int16 i, j, state;
153     int16 *prev_gain_pit, *prev_gain_code, *prev_state;
154     int16 *code2 = ScratchMem;
155
156     prev_state = disp_mem;
157     prev_gain_code = disp_mem + 1;
158     prev_gain_pit = disp_mem + 2;
159
160     pv_memset((void *)code2, 0, (2*L_SUBFR)*sizeof(*code2));
161
162
163     if (gain_pit < pitch_0_6)
164     {
165         state = 0;
166     }
167     else if (gain_pit < pitch_0_9)
168     {
169         state = 1;
170     }
171     else
172     {
173         state = 2;
174     }
175
176     for (i = 5; i > 0; i--)
177     {
178         prev_gain_pit[i] = prev_gain_pit[i - 1];
179     }
180     prev_gain_pit[0] = gain_pit;
181
182     if (sub_int16(gain_code, *prev_gain_code) > shl_int16(*prev_gain_code, 1))
183     {
184         /* onset */
185         if (state < 2)
186         {
187             state++;
188         }
189     }
190     else
191     {
192         j = 0;
193         for (i = 0; i < 6; i++)
194         {
195             if (prev_gain_pit[i] < pitch_0_6)
196             {
197                 j++;
198             }
199         }
200
201         if (j > 2)
202         {
203             state = 0;
204         }
205         if (state > *prev_state + 1)
206         {
207             state--;
208         }
209     }
210
211     *prev_gain_code = gain_code;
212     *prev_state = state;
213
214     /* circular convolution */
215
216     state += mode;              /* level of dispersion */
217
218     if (state == 0)
219     {
220         for (i = 0; i < L_SUBFR; i++)
221         {
222             if (code[i] != 0)
223             {
224                 for (j = 0; j < L_SUBFR; j++)
225                 {
226                     code2[i + j] = add_int16(code2[i + j], mult_int16_r(code[i], ph_imp_low[j]));
227                 }
228             }
229         }
230     }
231     else if (state == 1)
232     {
233         for (i = 0; i < L_SUBFR; i++)
234         {
235             if (code[i] != 0)
236             {
237                 for (j = 0; j < L_SUBFR; j++)
238                 {
239                     code2[i + j] = add_int16(code2[i + j], mult_int16_r(code[i], ph_imp_mid[j]));
240                 }
241             }
242         }
243     }
244     if (state < 2)
245     {
246         for (i = 0; i < L_SUBFR; i++)
247         {
248             code[i] = add_int16(code2[i], code2[i + L_SUBFR]);
249         }
250     }
251     return;
252 }
253