Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include <math.h>
43 : #include "prot.h"
44 : #include "cnst.h"
45 : #include "wmc_auto.h"
46 :
47 : /*-------------------------------------------------------------------*
48 : * Local constants
49 : *-------------------------------------------------------------------*/
50 : #define DIST_ISF_MAX_IO 150.0f
51 :
52 : #define DIST_ISF_MAX 120.0f
53 : #define DIST_ISF_THRES 60
54 : #define GAIN_PIT_THRES 0.9f
55 : #define GAIN_PIT_MIN 0.6f
56 :
57 : #define ALPHA1 0.98f
58 : #define ALPHA4 0.99f
59 : #define WINDOW_SIZE 50
60 : #define THRESH_TYPE 0.85f
61 : #define THRESH_VOICING 0.86f
62 :
63 : /*-------------------------------------------------------------------*
64 : * init_gp_clip
65 : *
66 : * Pitch Gain clipping initializations
67 : *-------------------------------------------------------------------*/
68 33737 : void init_gp_clip(
69 : float mem[] /* o : memory of gain of pitch clipping algorithm */
70 : )
71 : {
72 33737 : mem[0] = DIST_ISF_MAX;
73 33737 : mem[1] = GAIN_PIT_MIN;
74 33737 : mem[2] = 0.0f; /* old energy of target (dB) */
75 33737 : mem[3] = 0.0f;
76 33737 : mem[4] = 0.0f;
77 33737 : mem[5] = 0.8f;
78 :
79 33737 : return;
80 : }
81 :
82 : /*-------------------------------------------------------------------*
83 : * Function gp_clip
84 : *
85 : * The gain needs to be limited (gain pitch < 1.0) when one of the
86 : * following cases occurs:
87 : * - a resonance on LPC filter (lp_disp < 60 Hz) AND a good pitch
88 : * prediction (lp_gp > 0.9)
89 : * - target energy drops by 6 dB AND a good pitch prediction (lp_gp>1.0)
90 : *-------------------------------------------------------------------*/
91 :
92 623292 : int16_t gp_clip(
93 : const int16_t element_mode, /* i : element mode */
94 : const int32_t core_brate, /* i : core bitrate */
95 : const float *voicing, /* i : normalized correlations (from OL pitch) */
96 : const int16_t i_subfr, /* i : subframe index */
97 : const int16_t coder_type, /* i : type of coder */
98 : const float xn[], /* i : target vector */
99 : float mem[] /* i/o: memory of gain of pitch clipping algorithm */
100 : )
101 : {
102 : int16_t clip;
103 : int16_t i;
104 : float wener, tmp;
105 :
106 623292 : clip = 0;
107 :
108 623292 : if ( ( core_brate == ACELP_6k60 ) || ( core_brate == ACELP_8k85 ) || ( element_mode > EVS_MONO ) )
109 : {
110 614050 : tmp = 0.9f + ( 0.1f * mem[0] / DIST_ISF_MAX_IO ); /* clipping is activated when filtered pitch gain > threshold (0.94 to 1) */
111 614050 : if ( mem[1] > tmp )
112 : {
113 0 : clip = 1;
114 : }
115 : }
116 9242 : else if ( ( mem[0] < DIST_ISF_THRES ) && ( mem[1] > GAIN_PIT_THRES ) )
117 : {
118 20 : clip = 1;
119 : }
120 :
121 623292 : wener = 0.01f;
122 40513980 : for ( i = 0; i < L_SUBFR; i++ )
123 : {
124 39890688 : wener += xn[i] * xn[i];
125 : }
126 623292 : wener = 10.0f * (float) log10( wener );
127 623292 : if ( ( wener < ( mem[2] - 6.0f ) ) && ( mem[1] > 1.0f ) )
128 : {
129 0 : clip = 1;
130 : }
131 :
132 623292 : mem[2] = wener;
133 623292 : tmp = ALPHA1 * mem[4];
134 :
135 623292 : if ( coder_type == GENERIC || coder_type == TRANSITION || coder_type == INACTIVE )
136 : {
137 548678 : tmp += ( 1 - ALPHA1 );
138 : }
139 :
140 623292 : mem[4] = tmp;
141 623292 : tmp = ALPHA4 * mem[5];
142 623292 : if ( i_subfr == 0 )
143 : {
144 143309 : mem[5] = ( 1 - ALPHA4 ) * voicing[0] + tmp;
145 : }
146 479983 : else if ( i_subfr == 2 * L_SUBFR )
147 : {
148 138200 : mem[5] = ( 1 - ALPHA4 ) * voicing[1] + tmp;
149 : }
150 623292 : if ( mem[3] > WINDOW_SIZE )
151 : {
152 430918 : if ( ( mem[4] > THRESH_TYPE ) && ( mem[5] > THRESH_VOICING ) )
153 : {
154 6371 : clip = 1;
155 : }
156 : }
157 : else
158 : {
159 192374 : mem[3]++;
160 : }
161 :
162 623292 : return ( clip );
163 : }
164 :
165 : /*-------------------------------------------------------------------*
166 : * gp_clip_test_lsf()
167 : *
168 : * check the minimum distance of LSFs for pitch gain clipping flag
169 : *-------------------------------------------------------------------*/
170 :
171 175546 : void gp_clip_test_lsf(
172 : const int16_t element_mode, /* i : element mode */
173 : const int32_t core_brate, /* i : core bitrate */
174 : const float lsf[], /* i : LSF vector */
175 : float mem[], /* i/o: memory of gain of pitch clipping algorithm */
176 : const int16_t Opt_AMR_WB /* i : flag indicating AMR-WB IO mode */
177 : )
178 : {
179 : int16_t i;
180 : int16_t m;
181 : float dist, dist_min;
182 :
183 175546 : dist_min = lsf[1] - lsf[0];
184 :
185 175546 : if ( Opt_AMR_WB )
186 : {
187 0 : m = M - 1;
188 : }
189 : else
190 : {
191 175546 : m = M;
192 : }
193 :
194 2633190 : for ( i = 2; i < m; i++ )
195 : {
196 2457644 : dist = lsf[i] - lsf[i - 1];
197 2457644 : if ( dist < dist_min )
198 : {
199 211170 : dist_min = dist;
200 : }
201 : }
202 :
203 175546 : dist = 0.8f * mem[0] + 0.2f * dist_min;
204 :
205 175546 : if ( ( core_brate == ACELP_6k60 ) || ( core_brate == ACELP_8k85 ) || ( element_mode > EVS_MONO ) )
206 : {
207 174136 : if ( dist > DIST_ISF_MAX_IO )
208 : {
209 73830 : dist = DIST_ISF_MAX_IO;
210 : }
211 : }
212 1410 : else if ( dist > DIST_ISF_MAX )
213 : {
214 619 : dist = DIST_ISF_MAX;
215 : }
216 :
217 175546 : mem[0] = dist;
218 :
219 175546 : return;
220 : }
221 :
222 : /*-------------------------------------------------------------------*
223 : * gp_clip_test_gain_pit()
224 : *
225 : * low-pass filtering of the pitch gain for pitch gain clipping flag
226 : *-------------------------------------------------------------------*/
227 :
228 644046 : void gp_clip_test_gain_pit(
229 : const int16_t element_mode, /* i : element mode */
230 : const int32_t core_brate, /* i : core bitrate */
231 : const float gain_pit, /* i : gain of quantized pitch */
232 : float mem[] /* i/o: memory of gain of pitch clipping algorithm */
233 : )
234 : {
235 : float gain;
236 :
237 644046 : if ( ( core_brate == ACELP_6k60 ) || ( core_brate == ACELP_8k85 ) || ( element_mode > EVS_MONO ) )
238 : {
239 634704 : gain = 0.98f * mem[1] + 0.02f * gain_pit; /* long term LTP gain average (>250ms) */
240 : }
241 : else
242 : {
243 9342 : gain = 0.9f * mem[1] + 0.1f * gain_pit;
244 : }
245 :
246 644046 : if ( gain < GAIN_PIT_MIN )
247 : {
248 103483 : gain = GAIN_PIT_MIN;
249 : }
250 644046 : mem[1] = gain;
251 :
252 644046 : return;
253 : }
|