Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdint.h>
6 : #include <assert.h>
7 : #include "options.h"
8 : #include "prot_fx.h"
9 : #include "basop_util.h"
10 : #include "basop_proto_func.h"
11 : #include "cnst.h"
12 :
13 : /* Fixed point implementation of exp(negate()) */
14 11037360 : Word32 expfp( /* o: Q31 */
15 : const Word16 x, /* i: mantissa Q-e */
16 : const Word16 x_e ) /* i: exponent Q0 */
17 : {
18 : Word16 xi, xf, tmp;
19 : Word16 b0, b1, b2, b3;
20 : Word32 y, L_tmp;
21 :
22 :
23 11037360 : assert( x > 0 );
24 :
25 11037360 : L_tmp = L_shl( L_deposit_h( x ), x_e ); /* Q31 */
26 :
27 : /* split into integer and fractional parts */
28 11037360 : xi = round_fx( L_tmp ); /* Q15 */
29 11037360 : xf = extract_l( L_tmp ); /* Q31 */
30 :
31 : BASOP_SATURATE_WARNING_OFF_EVS;
32 11037360 : xf = negate( xf );
33 : BASOP_SATURATE_WARNING_ON_EVS;
34 :
35 : /* Fractional part */
36 : /* y = 65536
37 : + xf
38 : + ((xf*xf) / (2*65536))
39 : + ((((((xf*xf) / (2*65536))*xf) / 65536)*65536/3) / 65536)
40 : + ((((((((xf*xf) / (2*65536))*xf) / 65536)*65536/3) / 65536)*xf) / (4*65536)); */
41 11037360 : y = L_mac0( 65536, xf, 1 ); /* Q16 */
42 11037360 : tmp = shr( mult( xf, xf ), 2 );
43 11037360 : y = L_mac0( y, tmp, 1 ); /* Q16 */
44 11037360 : tmp = shr( mult( shr( mult( tmp, xf ), 1 ), 65536 / 3 ), 1 );
45 11037360 : y = L_mac0( y, tmp, 1 ); /* Q16 */
46 11037360 : tmp = shr( mult( tmp, xf ), 3 );
47 11037360 : y = L_mac0( y, tmp, 1 ); /* Q16 */
48 :
49 : /* Integer part */
50 11037360 : b0 = s_and( xi, 1 );
51 11037360 : b1 = s_and( xi, 2 );
52 11037360 : b2 = s_and( xi, 4 );
53 11037360 : b3 = s_and( xi, 8 );
54 :
55 11037360 : IF( b0 != 0 )
56 : {
57 5348344 : y = Mpy_32_16_1( y, 24109 ); /* exp(-1) in -1Q16 */
58 : }
59 11037360 : IF( b1 != 0 )
60 : {
61 5172785 : y = Mpy_32_16_1( y, 17739 ); /* exp(-2) in -2Q17 */
62 : }
63 11037360 : IF( b2 != 0 )
64 : {
65 6349002 : y = Mpy_32_16_1( y, 19205 ); /* exp(-4) in -5Q20 */
66 : }
67 11037360 : IF( b3 != 0 )
68 : {
69 879590 : y = Mpy_32_16_1( y, 22513 ); /* exp(-8) in -11Q26 */
70 : }
71 :
72 : /* scaling: -1*b0 - 2*b1 -5*b2 -11*b3 */
73 11037360 : y = L_shr( y, add( add( xi, shr( xi, 2 ) ), shr( b3, 3 ) ) );
74 :
75 : /* zero for xi >= 16 */
76 11037360 : if ( shr( xi, 4 ) > 0 )
77 : {
78 0 : y = L_deposit_l( 0 );
79 : }
80 :
81 :
82 11037360 : return L_shl( y, 15 );
83 : }
84 :
85 : /* Fixed point implementation of pow(), where base is fixed point (16/16) and exponent a small *odd* integer
86 : *
87 : * Returns: *pout1 = ( (base/65536)^(2*exp - 1) ) * 65536
88 : * *pout2 = ( (base/65536)^(2*exp + 1) ) * 65536
89 : *
90 : * NOTE: This function must be in sync with ari_decode_14bits_pow_fx() */
91 880185 : void powfp_odd2(
92 : const Word16 base, /* Q15 */
93 : const Word16 exp, /* Q0 */
94 : Word16 *pout1, /* Q15 */
95 : Word16 *pout2 /* Q15 */
96 : )
97 : {
98 : /* this version is in sync with ari_enc_14bits_pow()
99 : * that is, we have to start multiplication from the largest power-of-two, in order to
100 : * get the rounding errors to appear at the same places */
101 : Word16 pows[12]; /* powers of two exponents*/
102 : Word16 exp2;
103 : Word16 out, out2;
104 : Word16 k, h, maxk;
105 :
106 880185 : assert( exp >= 0 );
107 :
108 880185 : out = base; /* Q15 */
109 880185 : move16();
110 880185 : out2 = 0x7FFF; /* 1 in Q15 */
111 880185 : move16();
112 880185 : IF( exp != 0 )
113 : {
114 880185 : exp2 = sub( exp, 1 );
115 880185 : maxk = sub( 15, norm_s( exp ) );
116 880185 : assert( maxk < 12 );
117 :
118 880185 : pows[0] = base; /* Q15 */
119 880185 : move16();
120 2539129 : FOR( k = 0; k < maxk; k++ )
121 : {
122 1658944 : pows[k + 1] = mult_r( pows[k], pows[k] ); /* Q15 */
123 1658944 : move16();
124 : }
125 880185 : k = sub( k, 1 );
126 880185 : h = shl( 1, k ); /* highest bit of exp2 */
127 880185 : out2 = base;
128 880185 : move16();
129 880185 : out = mult_r( out, pows[k + 1] ); /* we already know that "exp" has the highest bit set to one since we calculated .. */
130 : /* .. the effective length of "exp" earlier on, thus we omit the branch for out2 */
131 880185 : IF( s_and( exp2, h ) != 0 )
132 : {
133 232352 : out2 = mult_r( out2, pows[k + 1] ); /* Q15 */
134 : }
135 :
136 880185 : h = shr( h, 1 );
137 1658944 : FOR( k = sub( k, 1 ); k >= 0; k-- )
138 : {
139 778759 : IF( s_and( exp, h ) != 0 )
140 : {
141 284157 : out = mult_r( out, pows[k + 1] ); /* Q15 */
142 : }
143 :
144 778759 : IF( s_and( exp2, h ) != 0 )
145 : {
146 445874 : out2 = mult_r( out2, pows[k + 1] ); /* Q15 */
147 : }
148 :
149 778759 : h = shr( h, 1 );
150 : }
151 : }
152 :
153 880185 : *pout1 = out2; /* Q15 */
154 880185 : move16();
155 880185 : *pout2 = out; /* Q15 */
156 880185 : move16();
157 880185 : }
158 :
159 : /*------------------------------------------------------------------------
160 : * Function: tcx_arith_scale_envelope
161 : *
162 : * For optimal performance of the arithmetic coder, the envelope shape must
163 : * be scaled such that the expected bit-consumption of a signal that
164 : * follows the scaled shape coincides with the target bitrate.
165 : * This function calculates a first-guess scaling and then uses the bi-section
166 : * search to find the optimal scaling.
167 : *
168 : * We assume that lines follow the Laplacian distribution, whereby the expected
169 : * bit-consumption would be log2(2*e*s[k]), where s[k] is the envelope value
170 : * for the line in question. However, this theoretical formula assumes that
171 : * all lines are encoded with magnitude+sign. Since the sign is unnecessary
172 : * for 0-values, that estimate of bit-consumption is biased when s[k] is small.
173 : * Analytical solution of the expectation for small s[k] is difficult, whereby
174 : * we use the approximation log2(2*e*s[k] + 0.15 + 0.035 / s[k]) which is accurate
175 : * on the range 0.08 to 1.0.
176 : *
177 : * NOTE: This function must be bit-exact on all platforms such that encoder
178 : * and decoder remain synchronized.
179 : *-------------------------------------------------------------------------*/
180 31484 : void tcx_arith_scale_envelope(
181 : const Word16 L_spec_core, /* i: number of lines to scale Q0 */
182 : Word16 L_frame, /* i: number of lines Q0 */
183 : const Word32 env[], /* i: unscaled envelope Q16 */
184 : Word16 target_bits, /* i: number of available bits Q0 */
185 : const Word16 low_complexity, /* i: low-complexity flag Q0 */
186 : Word16 s_env[], /* o: scaled envelope Q15-e */
187 : Word16 *s_env_e /* o: scaled envelope exponent Q0 */
188 : )
189 : {
190 : Word32 ienv[N_MAX_ARI];
191 : Word16 scale, iscale, iscale_e, a_e, b, b_e;
192 : Word16 lob, hib, adjust;
193 : Word16 k, iter, max_iter, lob_bits, hib_bits;
194 : Word16 statesi, bits;
195 : Word32 mean, a, s, L_tmp;
196 : Word16 mean_e, tmp, tmp2;
197 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
198 31484 : Flag Overflow = 0;
199 31484 : move32();
200 : #endif
201 :
202 :
203 31484 : lob_bits = 0;
204 31484 : move16();
205 31484 : hib_bits = 0;
206 31484 : move16();
207 :
208 : /* Boosting to account for expected spectrum truncation (kMax) */
209 : /* target_bits = (int)(target_bits * (1.2f - 0.00045f * target_bits + 0.00000025f * target_bits * target_bits)); */
210 31484 : L_tmp = L_shr( Mpy_32_16_1( L_mult0( target_bits, target_bits ), 17180 ), 6 ); /* Q15; 17180 -> 0.00000025f (Q36) */
211 31484 : L_tmp = L_sub( L_tmp, L_shr( L_mult0( target_bits, 30199 ), 11 ) ); /* Q15; 30199 -> 0.00045f (Q26) */
212 31484 : L_tmp = L_add( L_tmp, 39322 ); /* Q15; 39322 -> 1.2f (Q15) */
213 31484 : L_tmp = Mpy_32_16_1( L_tmp, target_bits ); /* Q0 */
214 31484 : assert( L_tmp < 32768 );
215 31484 : target_bits = extract_l( L_tmp );
216 :
217 : /* Calculate inverse envelope and find initial scale guess based on mean */
218 31484 : mean = L_deposit_l( 0 );
219 6594008 : FOR( k = 0; k < L_frame; k++ )
220 : {
221 : /* ienv[k] = 1.0f / env[k];
222 : mean += ienv[k]; */
223 :
224 6562524 : tmp = norm_l( env[k] );
225 6562524 : tmp2 = sub( 15, tmp );
226 6562524 : tmp = Inv16( round_fx_o( L_shl_o( env[k], tmp, &Overflow ), &Overflow ), &tmp2 ); /* exp(tmp2) */
227 6562524 : ienv[k] = L_shl( L_deposit_h( tmp ), sub( tmp2, 15 ) ); /* Q16 */
228 6562524 : move32();
229 6562524 : mean = L_add( mean, ienv[k] ); /* Q16 */
230 : }
231 31484 : tmp = norm_s( L_frame );
232 31484 : tmp = shl( div_s( 8192, shl( L_frame, tmp ) ), sub( tmp, 7 ) );
233 31484 : mean = L_shr( Mpy_32_16_1( mean, tmp ), 6 ); /* Q16 */
234 :
235 : /* Rate dependent compensation to get closer to the target on average */
236 : /* mean = (float)pow(mean, (float)L_frame / (float)target_bits * 0.357f); */
237 31484 : tmp = BASOP_Util_Divide1616_Scale( L_frame, target_bits, &tmp2 ); /* exp(tmp2) */
238 31484 : tmp = mult_r( tmp, 11698 /*0.357f Q15*/ );
239 31484 : mean = BASOP_Util_fPow( mean, 15, L_deposit_h( tmp ), tmp2, &mean_e ); /* exp(mean_e) */
240 :
241 : /* Find first-guess scaling coefficient "scale" such that if "mean" is the
242 : * mean of the envelope, then the mean bit-consumption is approximately
243 : *
244 : * log2(2*e*mean*scale + 0.15 + 0.035/(mean*scale)) * L_frame = target_bits
245 : */
246 : /* a = 2*2.71828183f*mean*mean; */
247 31484 : tmp = round_fx( mean ); /* Q15 - mean_e */
248 31484 : a = L_mult( mult_r( tmp, 22268 /*2.71828183f Q13*/ ), tmp ); /* 2 * mean_e + 3 */
249 31484 : a_e = add( shl( mean_e, 1 ), 3 );
250 :
251 : /* b = (0.15f - (float)pow(2.0f, target_bits/(float)L_frame)) * mean; */
252 31484 : tmp = BASOP_Util_Divide1616_Scale( target_bits, L_frame, &tmp2 ); /* exp(tmp2) */
253 31484 : tmp = round_fx( BASOP_util_Pow2( L_deposit_h( tmp ), tmp2, &tmp2 ) );
254 31484 : b_e = BASOP_Util_Add_MantExp( 4915 /*0.15f Q15*/, 0, negate( tmp ), tmp2, &b );
255 31484 : b = mult_r( b, round_fx( mean ) ); /* exp(b_e + mean_e) */
256 31484 : b_e = add( b_e, mean_e );
257 :
258 : /* scale = (-b + (float)sqrt(b*b - 4.0f*a*0.035f)) / (2.0f * a); */
259 31484 : tmp = round_fx_o( BASOP_Util_Add_Mant32Exp( L_mult( b, b ), shl( b_e, 1 ), Mpy_32_16_1( a, -4588 /*-4.0f*0.035f Q15*/ ), a_e, &tmp2 ), &Overflow );
260 :
261 31484 : IF( tmp <= 0 )
262 : {
263 0 : tmp = 0;
264 0 : move16();
265 0 : set16_fx( s_env, 0, L_frame );
266 : }
267 : ELSE
268 : {
269 31484 : tmp = Sqrt16( tmp, &tmp2 );
270 : }
271 :
272 31484 : tmp2 = BASOP_Util_Add_MantExp( negate( b ), b_e, tmp, tmp2, &scale ); /* exp(scale) */
273 31484 : scale = BASOP_Util_Divide1616_Scale( scale, round_fx( a ), &tmp );
274 31484 : scale = shl_o( scale, sub( sub( add( tmp, tmp2 ), a_e ), 1 ), &Overflow ); /* Q15 */
275 :
276 : /* iscale = 1.0f / scale; */
277 31484 : iscale_e = 0;
278 31484 : move16();
279 31484 : iscale = Inv16( s_max( 1, scale ), &iscale_e ); /* exp(isacle_e) */
280 :
281 31484 : lob = 0;
282 31484 : move16();
283 31484 : hib = 0;
284 31484 : move16();
285 :
286 31484 : max_iter = 2;
287 31484 : move16();
288 31484 : if ( low_complexity )
289 : {
290 28808 : max_iter = 1;
291 28808 : move16();
292 : }
293 :
294 65644 : FOR( iter = 0; iter < max_iter; iter++ )
295 : {
296 34160 : statesi = 0x7FFF; /* 1 in Q15 */
297 34160 : move16();
298 34160 : bits = 0;
299 34160 : move16();
300 :
301 7155642 : FOR( k = 0; k < L_frame; k++ )
302 : {
303 7121482 : s = Mpy_32_16_1( ienv[k], scale ); /* Q16 */
304 :
305 7121482 : IF( LE_32( s, 5243l /*0.08f Q16*/ ) )
306 : {
307 : /* If s = 0.08, the expected bit-consumption is log2(1.0224). Below 0.08, the bit-consumption
308 : estimate function becomes inaccurate, so use log2(1.0224) for all values below 0.08. */
309 : /* round(state * 1.0224 * 32768) */
310 28186 : statesi = mult_r( statesi, 16751 /*1.0224 Q14*/ ); /* Q14 */
311 28186 : tmp = norm_s( statesi );
312 28186 : statesi = shl( statesi, tmp ); /* Q15 */
313 28186 : bits = add( bits, sub( 1, tmp ) ); /* Q0 */
314 : }
315 7093296 : ELSE IF( LE_32( s, 16711680l /*255.0 Q16*/ ) )
316 : {
317 : /* a = 5.436564f * s + 0.15f + 0.035f * env[k] * iscale; */
318 7093296 : L_tmp = L_shl( Mpy_32_16_1( s, 22268 /*5.436564f Q12*/ ), 3 ); /* Q16 */
319 7093296 : L_tmp = L_add( L_tmp, 9830l /*0.15f Q16*/ ); /* Q16 */
320 7093296 : L_tmp = L_add( L_tmp, L_shl( Mpy_32_16_1( env[k], mult_r( 1147 /*0.035f Q15*/, iscale ) ), iscale_e ) ); /* Q16 */
321 :
322 7093296 : tmp = norm_l( L_tmp );
323 7093296 : statesi = mult_r( statesi, round_fx_o( L_shl_o( L_tmp, tmp, &Overflow ), &Overflow ) );
324 7093296 : bits = add( bits, sub( 15, tmp ) );
325 :
326 7093296 : tmp = norm_s( statesi );
327 7093296 : statesi = shl( statesi, tmp );
328 7093296 : bits = sub( bits, tmp ); /* Q0 */
329 : }
330 : ELSE
331 : {
332 : /* for large envelope values, s > 255, bit consumption is approx log2(2*e*s)
333 : * further, we use round(log2(x)) = floor(log2(x)+0.5) = floor(log2(x*sqrt(2))) */
334 : /* a = 5.436564f * s; */
335 0 : L_tmp = Mpy_32_16_1( s, 31492 /*5.436564f * 1.4142f Q12*/ ); /* Q13 */
336 0 : bits = add( bits, sub( 17, norm_l( L_tmp ) ) ); /* Q0 */
337 : }
338 : }
339 :
340 34160 : IF( LE_16( bits, target_bits ) ) /* Bits leftover => scale is too small */
341 : {
342 3369 : lob = scale; /* Q0 */
343 3369 : move16();
344 3369 : lob_bits = bits; /* Q0 */
345 3369 : move16();
346 :
347 3369 : IF( hib > 0 ) /* Bisection search */
348 : {
349 2397 : adjust = div_s( sub( hib_bits, target_bits ), sub( hib_bits, lob_bits ) ); /* Q15 */
350 2397 : scale = add( mult_r( sub( lob, hib ), adjust ), hib );
351 : }
352 : ELSE /* Initial scale adaptation */
353 : {
354 : /* adjust = 1.05f * target_bits / (float)bits;
355 : scale *= adjust; */
356 972 : adjust = mult_r( 17203 /*1.05f Q14*/, target_bits );
357 972 : adjust = BASOP_Util_Divide1616_Scale( adjust, bits, &tmp ); /* exp(tmp) */
358 972 : scale = shl( mult_r( scale, adjust ), add( 1, tmp ) );
359 : }
360 : }
361 : ELSE /* Ran out of bits => scale is too large */
362 : {
363 30791 : hib = scale;
364 30791 : move16();
365 30791 : hib_bits = bits;
366 30791 : move16();
367 :
368 30791 : IF( lob > 0 ) /* Bisection search */
369 : {
370 113 : adjust = div_s( sub( hib_bits, target_bits ), sub( hib_bits, lob_bits ) ); /* Q15 */
371 113 : scale = add( mult_r( sub( lob, hib ), adjust ), hib );
372 : }
373 : ELSE
374 : { /* Initial scale adaptation */
375 30678 : test();
376 30678 : IF( target_bits <= 0 || bits <= 0 ) /* safety check in case of bit errors */
377 : {
378 0 : adjust = 0;
379 0 : move16();
380 0 : set16_fx( s_env, 0, L_frame );
381 : }
382 : ELSE
383 : {
384 30678 : adjust = div_s( mult_r( 31130 /*0.95f Q15*/, target_bits ), bits ); /* Q15 */
385 : }
386 30678 : scale = mult_r( scale, adjust );
387 : }
388 : }
389 34160 : iscale_e = 0;
390 34160 : move16();
391 :
392 34160 : IF( scale == 0 ) /* safety check in case of bit errors */
393 : {
394 0 : iscale = 0;
395 0 : move16();
396 0 : set16_fx( s_env, 0, L_frame );
397 : }
398 : ELSE
399 : {
400 34160 : iscale = Inv16( scale, &iscale_e );
401 : }
402 : }
403 31484 : L_frame = L_spec_core; /* Q0 */
404 31484 : move16();
405 :
406 31484 : tmp = getScaleFactor32( env, L_frame );
407 31484 : *s_env_e = sub( add( 15, iscale_e ), tmp );
408 31484 : move16();
409 : BASOP_SATURATE_WARNING_OFF_EVS;
410 31484 : a = L_shl_o( 1265000, sub( 15, *s_env_e ), &Overflow );
411 : BASOP_SATURATE_WARNING_ON_EVS;
412 :
413 19781644 : FOR( k = 0; k < L_frame; k++ )
414 : {
415 19750160 : L_tmp = Mpy_32_16_1( L_shl( env[k], tmp ), iscale ); /* Q31 - e */
416 19750160 : L_tmp = L_min( L_tmp, a ); /* Q31 - e */
417 19750160 : s_env[k] = round_fx( L_tmp ); /* Q15 - e */
418 19750160 : move16();
419 : }
420 31484 : }
421 :
422 : /*------------------------------------------------------------------------
423 : * Function: tcx_arith_render_envelope
424 : *
425 : * Calculate the envelope of the spectrum based on the LPC shape. The
426 : * envelope is used in a perceptual domain, whereby the LPC shape has to
427 : * be multiplied by the perceptual model.
428 : * Operations that are performed on the spectrum, which change the magnitude
429 : * expectation of lines, such as low-frequency emphasis, are included in the
430 : * envelope shape.
431 : * NOTE: This function must be bit-exact on all platforms such that encoder
432 : * and decoder remain synchronized.
433 : *-------------------------------------------------------------------------*/
434 17495 : void tcx_arith_render_envelope(
435 : const Word16 A_ind[], /* i: LPC coefficients of signal envelope Q12*/
436 : const Word16 L_frame, /* i: number of spectral lines Q0*/
437 : const Word16 L_spec, /* Q0 */
438 : const Word16 preemph_fac, /* i: pre-emphasis factor Q15*/
439 : const Word16 gamma_w, /* i: A_ind -> weighted envelope factor Q15*/
440 : const Word16 gamma_uw, /* i: A_ind -> non-weighted envelope factor Q14*/
441 : Word32 env[] /* o: shaped signal envelope Q16*/
442 : )
443 : {
444 : Word16 k;
445 : Word16 tmpA[M + 2];
446 : Word16 signal_env[FDNS_NPTS], signal_env_e[FDNS_NPTS];
447 : Word16 gainlpc[FDNS_NPTS], gainlpc_e[FDNS_NPTS];
448 :
449 :
450 : /* Compute perceptual LPC envelope, transform it into freq.-domain gains */
451 17495 : weight_a_fx( A_ind, tmpA, gamma_w, M );
452 17495 : lpc2mdct( tmpA, M, NULL, NULL, gainlpc, gainlpc_e, FDNS_NPTS, 0 );
453 :
454 : /* Add pre-emphasis tilt to LPC envelope, transform LPC into MDCT gains */
455 17495 : E_LPC_a_weight_inv( A_ind, signal_env, gamma_uw, M );
456 17495 : E_LPC_a_add_tilt( signal_env, tmpA, preemph_fac, M );
457 17495 : lpc2mdct( tmpA, M + 1, signal_env, signal_env_e, NULL, NULL, FDNS_NPTS, 0 );
458 :
459 : /* Compute weighted signal envelope in perceptual domain */
460 1137175 : FOR( k = 0; k < FDNS_NPTS; k++ )
461 : {
462 1119680 : signal_env[k] = mult_r( signal_env[k], gainlpc[k] ); /* exp(signal_env_e + gainlpc_e) */
463 1119680 : move16();
464 1119680 : signal_env_e[k] = add( signal_env_e[k], gainlpc_e[k] );
465 1119680 : move16();
466 : }
467 :
468 : /* Adaptive low frequency emphasis */
469 17495 : set32_fx( env, 0x10000 /* 1 in Q16 */, L_frame );
470 :
471 17495 : AdaptLowFreqDeemph( env, 15,
472 : 1,
473 : gainlpc, gainlpc_e,
474 : L_frame, NULL );
475 :
476 : /* Scale from FDNS_NPTS to L_frame and multiply LFE gains */
477 17495 : mdct_noiseShaping_interp( env, L_frame, signal_env, signal_env_e );
478 :
479 6470791 : FOR( k = L_frame; k < L_spec; ++k )
480 : {
481 6453296 : env[k] = env[k - 1]; /* Q16 */
482 6453296 : move32();
483 : }
484 17495 : }
485 :
486 : #define WMC_TOOL_SKIP
487 :
488 : /*-------------------------------------------------------*
489 : * expfp_evs()
490 : *
491 : * Fixed point implementation of exp()
492 : *-------------------------------------------------------*/
493 :
494 : /*! r: Q15 */
495 8590021 : Word16 expfp_evs_fx(
496 : const Word16 x, /* i : mantissa Q15-e */
497 : const Word16 x_e /* i : exponent Q0 */
498 : )
499 : {
500 : Word16 xi, xf, tmp;
501 : Word16 b0, b1, b2, b3;
502 : Word32 y, L_tmp;
503 :
504 8590021 : assert( x <= 0 );
505 :
506 8590021 : L_tmp = L_negate( L_shl( L_deposit_h( x ), sub( x_e, 15 ) ) ); /* Q16 */
507 :
508 : /* split into integer and fractional parts */
509 8590021 : xi = round_fx( L_tmp ); /* Q0 */
510 8590021 : xf = extract_l( L_tmp ); /* Q16 */
511 :
512 : BASOP_SATURATE_WARNING_OFF;
513 8590021 : xf = negate( xf );
514 : BASOP_SATURATE_WARNING_ON;
515 :
516 : /* Fractional part */
517 : /* y = 65536
518 : + xf
519 : + ((xf*xf) / (2*65536))
520 : + ((((((xf*xf) / (2*65536))*xf) / 65536)*65536/3) / 65536)
521 : + ((((((((xf*xf) / (2*65536))*xf) / 65536)*65536/3) / 65536)*xf) / (4*65536)); */
522 8590021 : y = L_mac0( 65536, xf, 1 ); /* Q16 */
523 8590021 : tmp = shr( mult( xf, xf ), 2 ); /* Q15 */
524 8590021 : y = L_mac0( y, tmp, 1 ); /* Q16 */
525 8590021 : tmp = shr( mult( shr( mult( tmp, xf ), 1 ), 65536 / 3 ), 1 );
526 8590021 : y = L_mac0( y, tmp, 1 ); /* Q16 */
527 8590021 : tmp = shr( mult( tmp, xf ), 3 );
528 8590021 : y = L_mac0( y, tmp, 1 ); /* Q16 */
529 :
530 : /* Integer part */
531 8590021 : b0 = s_and( xi, 1 );
532 8590021 : b1 = s_and( xi, 2 );
533 8590021 : b2 = s_and( xi, 4 );
534 8590021 : b3 = s_and( xi, 8 );
535 :
536 8590021 : if ( b0 != 0 )
537 4161711 : y = Mpy_32_16_1( y, 24109 ); /* exp(-1) in -1Q16 */
538 8590021 : if ( b1 != 0 )
539 3983094 : y = Mpy_32_16_1( y, 17739 ); /* exp(-2) in -2Q17 */
540 8590021 : if ( b2 != 0 )
541 4991692 : y = Mpy_32_16_1( y, 19205 ); /* exp(-4) in -5Q20 */
542 8590021 : if ( b3 != 0 )
543 583897 : y = Mpy_32_16_1( y, 22513 ); /* exp(-8) in -11Q26 */
544 :
545 : /* scaling: -1*b0 - 2*b1 -5*b2 -11*b3 */
546 8590021 : y = L_shr( y, add( add( xi, shr( xi, 2 ) ), shr( b3, 3 ) ) ); /* Q16 */
547 :
548 : /* zero for xi >= 16 */
549 8590021 : if ( shr( xi, 4 ) > 0 )
550 : {
551 0 : y = L_deposit_l( 0 );
552 0 : move16();
553 : }
554 :
555 8590021 : return round_fx( L_shl( y, 15 ) );
556 : }
557 :
558 : /*-------------------------------------------------------*
559 : * powfp_odd2_evs()
560 : *
561 : * Fixed point implementation of pow(), where base is fixed point (16/16) and exponent a small *odd* integer
562 : *-------------------------------------------------------*/
563 : /*
564 : *
565 : * Returns: *pout1 = ( (base/65536)^(2*exp - 1) ) * 65536
566 : * *pout2 = ( (base/65536)^(2*exp + 1) ) * 65536
567 : *
568 : * NOTE: This function must be in sync with ari_decode_14bits_pow_ivas() */
569 :
570 :
571 : /*------------------------------------------------------------------------
572 : * Function: tcx_arith_render_envelope_flt
573 : *
574 : * Calculate the envelope of the spectrum based on the LPC shape. The
575 : * envelope is used in a perceptual domain, whereby the LPC shape has to
576 : * be multiplied by the perceptual model.
577 : * Operations that are performed on the spectrum, which change the magnitude
578 : * expectation of lines, such as low-frequency emphasis, are included in the
579 : * envelope shape.
580 : * NOTE: This function must be bit-exact on all platforms such that encoder
581 : * and decoder remain synchronized.
582 : *-------------------------------------------------------------------------*/
583 :
584 13989 : void tcx_arith_render_envelope_ivas_fx(
585 : const Word16 A_ind[], /* i : LPC coefficients of signal envelope Q12*/
586 : const Word16 L_frame, /* i : number of spectral lines Q0*/
587 : const Word16 L_spec, /* i : length of the coded spectrum Q0*/
588 : const Word16 preemph_fac, /* i : pre-emphasis factor Q15*/
589 : const Word16 gamma_w, /* i : A_ind -> weighted envelope factor Q15*/
590 : const Word16 gamma_uw, /* i : A_ind -> non-weighted envelope factor Q14*/
591 : Word32 env[] /* o : shaped signal envelope Q16*/
592 : )
593 : {
594 : Word16 k;
595 : Word16 tmpA[M + 2];
596 : Word16 signal_env[FDNS_NPTS], signal_env_e[FDNS_NPTS];
597 : Word16 gainlpc[FDNS_NPTS], gainlpc_e[FDNS_NPTS];
598 :
599 : /* Compute perceptual LPC envelope, transform it into freq.-domain gains */
600 13989 : basop_weight_a( A_ind, tmpA, gamma_w );
601 13989 : basop_lpc2mdct_fx( tmpA, M, NULL, NULL, gainlpc, gainlpc_e );
602 :
603 : /* Add pre-emphasis tilt to LPC envelope, transform LPC into MDCT gains */
604 13989 : basop_weight_a_inv( A_ind, signal_env, gamma_uw );
605 13989 : basop_E_LPC_a_add_tilt( signal_env, tmpA, preemph_fac );
606 13989 : basop_lpc2mdct_fx( tmpA, M + 1, signal_env, signal_env_e, NULL, NULL );
607 :
608 : /* Compute weighted signal envelope in perceptual domain */
609 909285 : FOR( k = 0; k < FDNS_NPTS; k++ )
610 : {
611 895296 : signal_env[k] = mult_r( signal_env[k], gainlpc[k] ); /* exp(signal_env_e + gainlpc_e) */
612 895296 : move16();
613 895296 : signal_env_e[k] = add( signal_env_e[k], gainlpc_e[k] );
614 895296 : move16();
615 : }
616 :
617 : /* Adaptive low frequency emphasis */
618 3677989 : FOR( k = 0; k < L_frame; k++ )
619 : {
620 3664000 : env[k] = 0x10000; /* 1 in Q16 */
621 3664000 : move32();
622 : }
623 :
624 13989 : basop_PsychAdaptLowFreqDeemph_fx( env, gainlpc, gainlpc_e, NULL );
625 :
626 : /* Scale from FDNS_NPTS to L_frame and multiply LFE gains */
627 13989 : basop_mdct_noiseShaping_interp_fx( env, L_frame, signal_env, signal_env_e );
628 :
629 5062789 : FOR( k = L_frame; k < L_spec; ++k )
630 : {
631 5048800 : env[k] = env[k - 1]; /* Q16 */
632 5048800 : move32();
633 : }
634 :
635 13989 : return;
636 : }
637 :
638 : #undef WMC_TOOL_SKIP
|