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 "wmc_auto.h"
45 :
46 : /*--------------------------------------------------------------------------*
47 : * hq2_noise_inject()
48 : *
49 : * HQ2 noise injection for WB signals
50 : *--------------------------------------------------------------------------*/
51 :
52 4020 : void hq2_noise_inject(
53 : float y2hat[],
54 : const int16_t band_start[],
55 : const int16_t band_end[],
56 : const int16_t band_width[],
57 : float Ep[],
58 : float Rk[],
59 : const int16_t npulses[],
60 : int16_t ni_seed,
61 : const int16_t bands,
62 : const int16_t ni_start_band,
63 : const int16_t bw_low,
64 : const int16_t bw_high,
65 : const float enerL,
66 : const float enerH,
67 : float last_ni_gain[],
68 : float last_env[],
69 : int16_t *last_max_pos_pulse,
70 : int16_t *p2a_flags,
71 : int16_t p2a_bands,
72 : const int16_t hqswb_clas,
73 : const int16_t bwidth,
74 : const int32_t bwe_br )
75 : {
76 4020 : int16_t i, j, k, ni_end_band, satur = 0, count[BANDS_MAX], max_pos_pulse, pos;
77 : float ni_gain[BANDS_MAX], pd[BANDS_MAX], rand, peak[BANDS_MAX], fac, env[BANDS_MAX];
78 4020 : int16_t sb = bands;
79 :
80 4020 : if ( ( hqswb_clas == HQ_HARMONIC || hqswb_clas == HQ_NORMAL ) && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) && bwidth == SWB )
81 : {
82 3053 : sb = ( bwe_br == HQ_16k40 ) ? 19 : 17;
83 : }
84 :
85 : /* calculate the envelopes/ the decoded peak coeff./number of the decoded coeff./ the last subbands of the bit-allocated/saturation of bit-allocation */
86 4020 : ni_end_band = bands;
87 4020 : max_pos_pulse = bands;
88 87882 : for ( k = ni_start_band; k < ni_end_band; k++ )
89 : {
90 83862 : pd[k] = (float) Rk[k] / band_width[k];
91 83862 : env[k] = (float) sqrt( Ep[k] / band_width[k] );
92 :
93 83862 : peak[k] = 0.0f;
94 83862 : count[k] = 0;
95 83862 : if ( npulses[k] != 0 )
96 : {
97 785759 : for ( i = band_start[k]; i <= band_end[k]; i++ )
98 : {
99 725251 : Ep[k] -= y2hat[i] * y2hat[i];
100 725251 : if ( fabs( y2hat[i] ) > peak[k] )
101 : {
102 75810 : peak[k] = (float) fabs( y2hat[i] );
103 : }
104 :
105 725251 : if ( y2hat[i] != 0 )
106 : {
107 145929 : count[k]++;
108 : }
109 : }
110 :
111 60508 : max_pos_pulse = k;
112 60508 : Ep[k] = Ep[k] > 0 ? (float) sqrt( Ep[k] / band_width[k] ) : 0.0f;
113 : }
114 : else
115 : {
116 23354 : Ep[k] = env[k];
117 : }
118 : }
119 :
120 87882 : for ( k = ni_start_band; k < ni_end_band; k++ )
121 : {
122 : /* calculate the noise gain */
123 83862 : satur = ( pd[k] >= 0.8f ) ? 1 : 0;
124 83862 : if ( satur == 0 && Ep[k] > 0 )
125 : {
126 48104 : if ( npulses[k] != 0 )
127 : {
128 25194 : if ( bwidth == SWB )
129 : {
130 22540 : if ( hqswb_clas != HQ_TRANSIENT )
131 : {
132 21708 : if ( hqswb_clas == HQ_HARMONIC )
133 : {
134 752 : fac = ( k <= sb ) ? 6.0f * ( 1.5f - pd[k] ) * env[k] * Ep[k] / ( peak[k] * peak[k] ) : 1.5f * Ep[k] / peak[k];
135 : }
136 : else
137 : {
138 20956 : fac = ( k <= sb ) ? 5.0f * ( 1.5f - pd[k] ) * Ep[k] / ( peak[k] ) : 4.0f * Ep[k] / peak[k];
139 : }
140 : }
141 : else
142 : {
143 832 : fac = 1.1f;
144 : }
145 : }
146 : else
147 : {
148 2654 : fac = 20.0f * min( 1.0f, ( 1.5f - pd[k] ) ) * env[k] * Ep[k] / ( peak[k] * peak[k] );
149 2654 : if ( k > 1 && k < ni_end_band - 1 )
150 : {
151 2357 : if ( count[k + 1] == 0 && env[k] > 0.5f * env[k + 1] && env[k] < 2.0f * env[k - 1] )
152 : {
153 635 : fac = 1.5f * env[k + 1] / env[k];
154 : }
155 1722 : else if ( count[k - 1] == 0 && peak[k] > 2.0f * env[k] )
156 : {
157 573 : fac = env[k - 1] / env[k];
158 : }
159 : }
160 :
161 2654 : if ( k >= ni_end_band - p2a_bands && bwidth == WB )
162 : {
163 54 : if ( bw_low * enerH > bw_high * enerL && peak[k] < 2.0f * env[k] )
164 : {
165 0 : fac *= ( 2.0f - Ep[k] / peak[k] );
166 : }
167 :
168 54 : if ( p2a_flags[k] == 0 && fac > 0 )
169 : {
170 50 : fac *= min( 1.25f * env[k] / ( Ep[k] * fac ), 1.5f );
171 : }
172 : }
173 : }
174 : }
175 : else
176 : {
177 22910 : fac = ( hqswb_clas == HQ_HARMONIC && bwidth == SWB ) ? 0.8f : 1.1f;
178 : }
179 :
180 48104 : ni_gain[k] = fac * Ep[k];
181 : }
182 : else
183 : {
184 35758 : ni_gain[k] = 0.0;
185 : }
186 :
187 : /* smooth the noise gain between the current frame and the previous frame */
188 83862 : pos = bwidth == SWB ? ni_end_band - 1 : max( max_pos_pulse, *last_max_pos_pulse );
189 :
190 83862 : if ( k <= pos )
191 : {
192 82172 : if ( k > 0 && k < ni_end_band - 1 )
193 : {
194 74700 : if ( ( env[k] > 0.5f * last_env[k] && env[k] < 2.0f * last_env[k] ) ||
195 20591 : ( ( ( env[k] + env[k - 1] + env[k + 1] ) > 0.5f * ( last_env[k] + last_env[k - 1] + last_env[k + 1] ) ) &&
196 18343 : ( ( env[k] + env[k - 1] + env[k + 1] ) < 2.0f * ( last_env[k] + last_env[k - 1] + last_env[k + 1] ) ) ) )
197 : {
198 59233 : if ( ni_gain[k] > last_ni_gain[k] )
199 : {
200 18969 : ni_gain[k] = 0.2f * ni_gain[k] + 0.8f * last_ni_gain[k];
201 : }
202 : else
203 : {
204 40264 : ni_gain[k] = 0.6f * ni_gain[k] + 0.4f * last_ni_gain[k];
205 : }
206 : }
207 : }
208 7472 : else if ( k == ni_end_band - 1 )
209 : {
210 3452 : if ( ( env[k] > 0.5f * last_env[k] && env[k] < 2.0f * last_env[k] ) ||
211 850 : ( ( env[k] + env[k - 1] ) > 0.5f * ( last_env[k] + last_env[k - 1] ) &&
212 674 : ( env[k] + env[k - 1] ) < 2.0f * ( last_env[k] + last_env[k - 1] ) ) )
213 : {
214 2668 : if ( ni_gain[k] > last_ni_gain[k] )
215 : {
216 1360 : ni_gain[k] = 0.2f * ni_gain[k] + 0.8f * last_ni_gain[k];
217 : }
218 : else
219 : {
220 1308 : ni_gain[k] = 0.6f * ni_gain[k] + 0.4f * last_ni_gain[k];
221 : }
222 : }
223 : }
224 : }
225 :
226 : /* inject noise into the non-decoded coeffs */
227 83862 : if ( k >= ni_end_band - p2a_bands && p2a_flags[k] == 0 && bwidth != SWB )
228 : {
229 15880 : for ( i = band_start[k]; i <= band_end[k]; i++ )
230 : {
231 15381 : if ( y2hat[i] != 0 )
232 : {
233 281 : y2hat[i] *= 0.8f;
234 : }
235 : }
236 : }
237 :
238 83862 : if ( k == max_pos_pulse && k < bands - p2a_bands && satur != 1 && bwidth != SWB )
239 : {
240 562 : j = 0;
241 562 : if ( ni_gain[k] < 0.01f )
242 : {
243 10 : fac = 0.0f;
244 : }
245 : else
246 : {
247 552 : fac = max( Ep[k] / ni_gain[k], 1.0f );
248 : }
249 12504 : for ( i = band_start[k]; i <= band_end[k]; i++ )
250 : {
251 11942 : if ( y2hat[i] == 0 )
252 : {
253 11070 : rand = own_random( &ni_seed ) / PCM16_TO_FLT_FAC;
254 11070 : y2hat[i] += ( fac - ( ( fac - 1.0f ) * j ) / band_width[k] ) * ni_gain[k] * rand;
255 : }
256 :
257 11942 : j++;
258 : }
259 : }
260 : else
261 : {
262 2031990 : for ( i = band_start[k]; i <= band_end[k]; i++ )
263 : {
264 1948690 : if ( y2hat[i] == 0 )
265 : {
266 1803633 : rand = own_random( &ni_seed ) / PCM16_TO_FLT_FAC;
267 1803633 : y2hat[i] += ni_gain[k] * rand;
268 : }
269 : }
270 : }
271 : }
272 :
273 4020 : mvr2r( env, last_env, ni_end_band );
274 4020 : mvr2r( ni_gain, last_ni_gain, ni_end_band );
275 4020 : *last_max_pos_pulse = max_pos_pulse;
276 :
277 4020 : return;
278 : }
|