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.452 Aug 12, 2021. Version 16.3.0
35 : ====================================================================================*/
36 :
37 : #include "options.h" /* Common prototypes */
38 : #include "prot_fx.h" /* Common prototypes */
39 : #include "ivas_cnst.h"
40 : #include "stl.h"
41 :
42 : /*-------------------------------------------------------------------*
43 : * Rescale_exc:
44 : *
45 : * Find absolute maximum of excitation
46 : * Fin scaling factor to apply the excitation and its related memory
47 : * Scale excitation and total excitation (exc2)
48 : *-------------------------------------------------------------------*/
49 605422 : Word16 Rescale_exc(
50 : Word16 dct_post_old_exc_fx[], /* i/o: Music post processing memory Q_exc*/
51 : Word16 exc[], /* i/o: excitation to rescale Q_exc */
52 : Word16 bwe_exc[], /* output Q = Q_exc */
53 : Word16 *last_exc_dct_in, /* output Q = Q_exc */
54 : Word16 lg, /* i : frame size */
55 : Word16 lg32,
56 : Word32 L_gain_code, /* i : decoded codebook gain Q16 */
57 : Word16 *sQ_exc, /* i/o: Excitation scaling factor */
58 : Word16 *sQsubfr, /* i/o: Past excitation scaling factors */
59 : Word16 exc2[], /* o : local excitation vector output Q = Q_exc */
60 : Word16 i_subfr, /* i : subframe number */
61 : const Word16 coder_type )
62 : {
63 : Word16 i, tmp, max, new_Q;
64 :
65 : /*-------------------------------------------
66 : * find maximum of absolute excitation
67 : *-------------------------------------------*/
68 605422 : max = s_max( abs_s( exc[0] ), 1 );
69 45709120 : FOR( i = 1; i < lg; i++ )
70 : {
71 45103698 : tmp = abs_s( exc[i] );
72 45103698 : max = s_max( max, tmp );
73 : }
74 :
75 : /*----------------------------------------------
76 : * find scaling (tmp) to set max = [2048..4096[
77 : *----------------------------------------------*/
78 605422 : tmp = sub( add( norm_s( max ), *sQ_exc ), 3 );
79 605422 : tmp = s_min( tmp, 12 );
80 :
81 : /*----------------------------------------------
82 : * find scaling (new_Q) to keep gain_code < 2048
83 : *----------------------------------------------*/
84 :
85 605422 : new_Q = add( tmp, 1 );
86 605422 : tmp = sub( norm_l( L_or( L_gain_code, 1 ) ), 3 ); /* to get to 0x08000000L (L_or with 1 to avoid norm_l(0)) */
87 605422 : tmp = s_min( tmp, new_Q );
88 605422 : tmp = s_max( tmp, 0 );
89 605422 : tmp = sub( tmp, 1 );
90 :
91 : /*#define REMOVE_EXCITATION_PER_FRAME_SCALING */
92 :
93 : /*----------------------------------------------
94 : * new_Q = smallest Q since 4 subframes (20ms)
95 : *----------------------------------------------*/
96 605422 : IF( EQ_16( coder_type, TRANSITION ) )
97 : {
98 59123 : tmp = s_min( tmp, 7 );
99 : }
100 546299 : ELSE IF( EQ_16( coder_type, INACTIVE ) )
101 : {
102 31726 : tmp = s_min( tmp, 13 );
103 : }
104 514573 : ELSE IF( GT_16( lg, L_SUBFR ) ) /* --> can only happen in AUDIO mode */
105 : {
106 14350 : tmp = s_min( tmp, 4 ); /* Limitation of the scaling gain because the frequency domain will add much more energy to the excitation*/
107 14350 : if ( GE_32( L_abs( L_gain_code ), 3276800 ) ) /*(1-gain_pit)*past gain code*4 > 50 */
108 : {
109 6690 : tmp = s_min( tmp, 2 ); /* Limitation of the scaling gain because the frequency domain might add much more energy to the excitation*/
110 : }
111 : }
112 :
113 605422 : new_Q = s_min( tmp, sQsubfr[0] );
114 605422 : IF( EQ_16( lg, L_SUBFR ) )
115 : {
116 2845855 : FOR( i = L_Q_MEM - 1; i >= 1; i-- )
117 : {
118 2276684 : new_Q = s_min( new_Q, sQsubfr[i] );
119 2276684 : sQsubfr[i] = sQsubfr[i - 1];
120 2276684 : move16();
121 : }
122 : }
123 : ELSE
124 : {
125 36251 : IF( EQ_16( lg, 2 * L_SUBFR ) )
126 : {
127 5220 : new_Q = s_min( new_Q, sQsubfr[L_Q_MEM - 1] );
128 20880 : FOR( i = L_Q_MEM - 1; i >= 2; i-- )
129 : {
130 15660 : sQsubfr[i] = sQsubfr[1];
131 15660 : move16();
132 : }
133 5220 : sQsubfr[1] = tmp;
134 5220 : move16();
135 5220 : sQsubfr[0] = tmp;
136 5220 : move16();
137 : }
138 : ELSE
139 : {
140 31031 : set16_fx( sQsubfr, tmp, L_Q_MEM );
141 : }
142 : }
143 605422 : sQsubfr[0] = tmp;
144 605422 : move16();
145 :
146 : /*----------------------------------------------
147 : * rescale excitation and associated memories
148 : *----------------------------------------------*/
149 :
150 605422 : tmp = sub( new_Q, *sQ_exc );
151 :
152 605422 : IF( tmp != 0 )
153 : {
154 :
155 91589 : Scale_sig( exc - L_EXC_MEM_DEC, add( L_EXC_MEM_DEC, lg ), tmp );
156 91589 : IF( last_exc_dct_in != NULL )
157 : {
158 91164 : Scale_sig( last_exc_dct_in, L_FRAME, tmp );
159 : }
160 91589 : IF( bwe_exc != NULL )
161 : {
162 91563 : Scale_sig( bwe_exc - PIT16k_MAX * 2, add( PIT16k_MAX * 2, lg32 ), tmp );
163 : }
164 91589 : IF( exc2 != NULL )
165 : {
166 76969 : Scale_sig( exc2, i_subfr, tmp );
167 : }
168 91589 : IF( dct_post_old_exc_fx != NULL )
169 : {
170 89446 : Scale_sig( dct_post_old_exc_fx, DCT_L_POST - OFFSET2, tmp );
171 : }
172 : }
173 :
174 : /* scaling factor of excitation (-1..12) */
175 605422 : *sQ_exc = new_Q;
176 605422 : move16();
177 :
178 605422 : return tmp;
179 : }
180 :
181 : /*-------------------------------------------------------------------*
182 : * Rescale_mem:
183 : *
184 : * this function should be called after excitation update (4 subfr) and before frame synthesis
185 : * Rescale excitation related memories
186 : *-------------------------------------------------------------------*/
187 167951 : void Rescale_mem(
188 : const Word16 Q_exc, /* i : current excitation scaling (>=0) */
189 : Word16 *prev_Q_syn, /* i/o : scaling factor of previous frame */
190 : Word16 *Q_syn, /* i/o : scaling factor of frame */
191 : Word16 *mem_syn2, /* i/o : modified synthesis memory Qexp_scale */
192 : Word16 *mem_syn_clas_estim_fx, /* i/o : old 12k8 core memory for classification Qexp_scale*/
193 : const Word16 MaxScaling, /* i: Minimal difference between excitation scaling and synthesis scaling */
194 : Word16 *mem_deemph, /* i/o: speech deemph filter memory Qexp_scale */
195 : Word16 *pst_old_syn, /* i/o: psfiler Qexp_scale*/
196 : Word16 *pst_mem_deemp_err, /* i/o: psfiler Qexp_scale */
197 : Word16 *mem_agc, /*Qexp_scale*/
198 : PFSTAT_HANDLE hPFstat, /* i/o: All memories related to NB post filter */
199 : const Word16 Vad_flag,
200 : const Word16 Cna_flag,
201 : const Word16 *tmp_buffer /* tmp_buffer in Q-1 */
202 : )
203 : {
204 : Word16 exp_scale, new_Q, tmp, i;
205 :
206 : /*-------------------------------------------------------------------*
207 : * find scaling of synthesis (based on min of current frame and last frame)
208 : * scaling factor of synthesis (-1..6)
209 : *-------------------------------------------------------------------*/
210 167951 : new_Q = sub( Q_exc, MaxScaling );
211 167951 : tmp = 1;
212 167951 : move16();
213 167951 : IF( tmp_buffer != NULL )
214 : {
215 : /* use the temporary synthesis in Q-1 to estimate the scaling */
216 37393757 : FOR( i = 0; i < L_FRAME; i++ )
217 : {
218 37248256 : tmp = s_max( abs_s( tmp_buffer[i] ), tmp );
219 : }
220 : /* we add Q_syn which represents the actual scaling of the memories prev_Q_syn represents the last potential scaling */
221 145501 : tmp = sub( add( norm_s( tmp ), -1 ), 3 ); /* -2 ... 12 */
222 : }
223 22450 : ELSE IF( hPFstat != NULL )
224 : {
225 258332 : FOR( i = 0; i < M; i++ )
226 : {
227 243136 : tmp = s_max( abs_s( mem_syn2[i] ), tmp );
228 243136 : tmp = s_max( abs_s( pst_old_syn[i] ), tmp );
229 243136 : tmp = s_max( abs_s( mem_syn_clas_estim_fx[i] ), tmp );
230 : }
231 744604 : FOR( ; i < L_SUBFR; i++ )
232 : {
233 729408 : tmp = s_max( abs_s( pst_old_syn[i] ), tmp );
234 729408 : tmp = s_max( abs_s( mem_syn_clas_estim_fx[i] ), tmp );
235 : }
236 2963220 : FOR( ; i < L_SYN_MEM_CLAS_ESTIM; i++ )
237 : {
238 2948024 : tmp = s_max( abs_s( mem_syn_clas_estim_fx[i] ), tmp );
239 2948024 : tmp = s_max( abs_s( pst_old_syn[i] ), tmp );
240 : }
241 501468 : FOR( ; i < NBPSF_PIT_MAX; i++ )
242 : {
243 486272 : tmp = s_max( abs_s( pst_old_syn[i] ), tmp );
244 : }
245 : /* we add Q_syn which represents the actual scaling of the memories prev_Q_syn represents the last potential scaling */
246 15196 : tmp = sub( add( norm_s( tmp ), *Q_syn ), 2 ); /* -2 ... 12 */
247 : }
248 :
249 :
250 167951 : IF( Vad_flag != 0 )
251 : {
252 148423 : new_Q = s_min( sub( Q_exc, 2 ), tmp );
253 : }
254 19528 : ELSE IF( Cna_flag != 0 )
255 : {
256 5 : new_Q = s_min( Q_exc, sub( tmp, 2 ) );
257 : }
258 : ELSE
259 : {
260 19523 : new_Q = s_min( Q_exc, tmp );
261 : }
262 167951 : new_Q = s_min( new_Q, 12 ); /* */
263 167951 : new_Q = s_max( new_Q, -1 ); /* */
264 :
265 : /*#define REMOVE_SYNTHESIS_PER_FRAME_SCALING */
266 167951 : tmp = s_min( new_Q, *prev_Q_syn );
267 167951 : *prev_Q_syn = new_Q;
268 167951 : move16();
269 :
270 167951 : exp_scale = sub( tmp, *Q_syn );
271 167951 : *Q_syn = tmp;
272 167951 : move16();
273 :
274 : /* rescale synthesis memory (mem_syn2) */
275 167951 : Scale_sig( mem_syn2, M, exp_scale );
276 167951 : Scale_sig( mem_syn_clas_estim_fx, L_SYN_MEM_CLAS_ESTIM, exp_scale );
277 : /*Scale_sig(core_old_syn, L_SYN_MEM, exp_scale);*/
278 167951 : Scale_sig( mem_deemph, 1, exp_scale );
279 167951 : IF( hPFstat != NULL )
280 : {
281 160697 : Scale_sig( pst_old_syn, NBPSF_PIT_MAX, exp_scale );
282 160697 : Scale_sig( pst_mem_deemp_err, 1, exp_scale );
283 160697 : Scale_sig( hPFstat->mem_pf_in, L_SUBFR, exp_scale ); /* NB post_filter mem */
284 160697 : Scale_sig( hPFstat->mem_res2, DECMEM_RES2, exp_scale ); /* NB post_filter mem */
285 160697 : Scale_sig( hPFstat->mem_stp, L_SUBFR, exp_scale ); /* NB post_filter mem */
286 : }
287 167951 : Scale_sig( mem_agc, 1, exp_scale ); /* NB post_filter mem */
288 167951 : return;
289 : }
290 :
291 : // note_ : also present in tools_fx.c which one to take is an issue?
292 : /*-------------------------------------------------------------------*
293 : * Scale_sig32
294 : * Note: In order to save complexity, call function only, if exp0 != 0
295 : * Up/down scale a 32 bits vector
296 : *-------------------------------------------------------------------*/
297 627851173 : void scale_sig32(
298 : Word32 x[], /* i/o: signal to scale Qx */
299 : const Word16 lg, /* i : size of x[] Q0 */
300 : const Word16 exp0 /* i : exponent: x = round(x << exp) Qx exp */
301 : )
302 : {
303 : Word16 i;
304 :
305 23184329092 : FOR( i = 0; i < lg; i++ )
306 : {
307 : /* saturation can occur here */
308 22556477919 : x[i] = L_shl( x[i], exp0 );
309 22556477919 : move32();
310 22556477919 : if ( 0 == exp0 )
311 : {
312 140433805 : i = lg;
313 : }
314 : }
315 627851173 : }
316 :
317 0 : void scale_sig32_r(
318 : Word32 x[], /* i/o: signal to scale Qx */
319 : const Word16 lg, /* i : size of x[] Q0 */
320 : const Word16 exp0 /* i : exponent: x = round(x << exp) Qx exp */
321 : )
322 : {
323 : Word16 i;
324 :
325 0 : FOR( i = 0; i < lg; i++ )
326 : {
327 : /* saturation can occur here */
328 0 : x[i] = L_shl_r( x[i], exp0 );
329 0 : move32();
330 0 : if ( 0 == exp0 )
331 : {
332 0 : i = lg;
333 : }
334 : }
335 0 : }
336 :
337 : /*-------------------------------------------------------------------*
338 : * Rescale_mem:
339 : *
340 : * this function should be called after excitation update (4 subfr) and before frame synthesis
341 : * Rescale excitation related memories
342 : *-------------------------------------------------------------------*/
343 3020 : Word16 rescale_mem(
344 : const Word16 *Q_exc, /* i : current excitation scaling (>=0) */
345 : Word16 *prev_Q_syn, /* i/o : scaling factor of previous frame */
346 : Word16 *Q_syn, /* i/o : scaling factor of frame */
347 : Word16 *mem_syn2, /* i/o : modified synthesis memory Q_syn */
348 : Word16 *syn, /* i/o : synthesis to rescale Q_syn */
349 : Word16 mem_len, /* i : lenght of modified synthesis memory */
350 : Word16 i_subfr /* i : subframe number */
351 : )
352 : {
353 : Word16 exp_scale, new_Q, tmp;
354 : Word16 i, max16, max_scale;
355 :
356 3020 : max16 = 0;
357 3020 : move16();
358 51340 : FOR( i = 0; i < mem_len; i++ )
359 : {
360 48320 : max16 = s_max( max16, abs_s( mem_syn2[i] ) );
361 : }
362 3020 : IF( syn != NULL )
363 : {
364 389580 : FOR( i = 0; i < i_subfr; i++ )
365 : {
366 386560 : max16 = s_max( max16, abs_s( syn[i] ) );
367 : }
368 : }
369 3020 : max_scale = 15;
370 3020 : move16();
371 3020 : IF( max16 > 0 )
372 : {
373 3012 : max_scale = add( norm_s( max16 ), -3 );
374 : }
375 :
376 : /*-------------------------------------------------------------------*
377 : * find scaling of synthesis (based on min of current frame and last frame)
378 : * scaling factor of synthesis (-1..6)
379 : *-------------------------------------------------------------------*/
380 3020 : new_Q = sub( *Q_exc, 3 );
381 3020 : new_Q = s_max( new_Q, -1 );
382 :
383 3020 : tmp = add( max_scale, *Q_syn );
384 3020 : if ( GT_16( s_min( new_Q, *prev_Q_syn ), tmp ) )
385 : {
386 68 : new_Q = s_max( tmp, -1 );
387 : }
388 :
389 3020 : tmp = s_min( new_Q, *prev_Q_syn );
390 3020 : *prev_Q_syn = new_Q;
391 3020 : move16();
392 :
393 3020 : exp_scale = sub( tmp, *Q_syn );
394 3020 : *Q_syn = tmp;
395 3020 : move16();
396 : /* rescale synthesis memory (mem_syn2) */
397 3020 : Scale_sig( mem_syn2, mem_len, exp_scale );
398 3020 : IF( syn != NULL )
399 : {
400 3020 : Scale_sig( syn, i_subfr, exp_scale );
401 : }
402 :
403 3020 : return exp_scale;
404 : }
|