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 15292160 : 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 15292160 : assert( x > 0 );
24 :
25 15292160 : L_tmp = L_shl( L_deposit_h( x ), x_e ); /* Q31 */
26 :
27 : /* split into integer and fractional parts */
28 15292160 : xi = round_fx( L_tmp ); /* Q15 */
29 15292160 : xf = extract_l( L_tmp ); /* Q31 */
30 :
31 : BASOP_SATURATE_WARNING_OFF_EVS;
32 15292160 : 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 15292160 : y = L_mac0( 65536, xf, 1 ); /* Q16 */
42 15292160 : tmp = shr( mult( xf, xf ), 2 );
43 15292160 : y = L_mac0( y, tmp, 1 ); /* Q16 */
44 15292160 : tmp = shr( mult( shr( mult( tmp, xf ), 1 ), 65536 / 3 ), 1 );
45 15292160 : y = L_mac0( y, tmp, 1 ); /* Q16 */
46 15292160 : tmp = shr( mult( tmp, xf ), 3 );
47 15292160 : y = L_mac0( y, tmp, 1 ); /* Q16 */
48 :
49 : /* Integer part */
50 15292160 : b0 = s_and( xi, 1 );
51 15292160 : b1 = s_and( xi, 2 );
52 15292160 : b2 = s_and( xi, 4 );
53 15292160 : b3 = s_and( xi, 8 );
54 :
55 15292160 : IF( b0 != 0 )
56 : {
57 7363130 : y = Mpy_32_16_1( y, 24109 ); /* exp(-1) in -1Q16 */
58 : }
59 15292160 : IF( b1 != 0 )
60 : {
61 7150461 : y = Mpy_32_16_1( y, 17739 ); /* exp(-2) in -2Q17 */
62 : }
63 15292160 : IF( b2 != 0 )
64 : {
65 8483950 : y = Mpy_32_16_1( y, 19205 ); /* exp(-4) in -5Q20 */
66 : }
67 15292160 : IF( b3 != 0 )
68 : {
69 898292 : y = Mpy_32_16_1( y, 22513 ); /* exp(-8) in -11Q26 */
70 : }
71 :
72 : /* scaling: -1*b0 - 2*b1 -5*b2 -11*b3 */
73 15292160 : y = L_shr( y, add( add( xi, shr( xi, 2 ) ), shr( b3, 3 ) ) );
74 :
75 : /* zero for xi >= 16 */
76 15292160 : if ( shr( xi, 4 ) > 0 )
77 : {
78 0 : y = L_deposit_l( 0 );
79 : }
80 :
81 :
82 15292160 : 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 1261436 : 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 1261436 : assert( exp >= 0 );
107 :
108 1261436 : out = base; /* Q15 */
109 1261436 : move16();
110 1261436 : out2 = 0x7FFF; /* 1 in Q15 */
111 1261436 : move16();
112 1261436 : IF( exp != 0 )
113 : {
114 1261436 : exp2 = sub( exp, 1 );
115 1261436 : maxk = sub( 15, norm_s( exp ) );
116 1261436 : assert( maxk < 12 );
117 :
118 1261436 : pows[0] = base; /* Q15 */
119 1261436 : move16();
120 3676218 : FOR( k = 0; k < maxk; k++ )
121 : {
122 2414782 : pows[k + 1] = mult_r( pows[k], pows[k] ); /* Q15 */
123 2414782 : move16();
124 : }
125 1261436 : k = sub( k, 1 );
126 1261436 : h = shl( 1, k ); /* highest bit of exp2 */
127 1261436 : out2 = base;
128 1261436 : move16();
129 1261436 : 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 1261436 : IF( s_and( exp2, h ) != 0 )
132 : {
133 344881 : out2 = mult_r( out2, pows[k + 1] ); /* Q15 */
134 : }
135 :
136 1261436 : h = shr( h, 1 );
137 2414782 : FOR( k = sub( k, 1 ); k >= 0; k-- )
138 : {
139 1153346 : IF( s_and( exp, h ) != 0 )
140 : {
141 433627 : out = mult_r( out, pows[k + 1] ); /* Q15 */
142 : }
143 :
144 1153346 : IF( s_and( exp2, h ) != 0 )
145 : {
146 654440 : out2 = mult_r( out2, pows[k + 1] ); /* Q15 */
147 : }
148 :
149 1153346 : h = shr( h, 1 );
150 : }
151 : }
152 :
153 1261436 : *pout1 = out2; /* Q15 */
154 1261436 : move16();
155 1261436 : *pout2 = out; /* Q15 */
156 1261436 : move16();
157 1261436 : }
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 47891 : 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 :
198 47891 : lob_bits = 0;
199 47891 : move16();
200 47891 : hib_bits = 0;
201 47891 : move16();
202 :
203 : /* Boosting to account for expected spectrum truncation (kMax) */
204 : /* target_bits = (int)(target_bits * (1.2f - 0.00045f * target_bits + 0.00000025f * target_bits * target_bits)); */
205 47891 : L_tmp = L_shr( Mpy_32_16_1( L_mult0( target_bits, target_bits ), 17180 ), 6 ); /* Q15; 17180 -> 0.00000025f (Q36) */
206 47891 : L_tmp = L_sub( L_tmp, L_shr( L_mult0( target_bits, 30199 ), 11 ) ); /* Q15; 30199 -> 0.00045f (Q26) */
207 47891 : L_tmp = L_add( L_tmp, 39322 ); /* Q15; 39322 -> 1.2f (Q15) */
208 47891 : L_tmp = Mpy_32_16_1( L_tmp, target_bits ); /* Q0 */
209 47891 : assert( L_tmp < 32768 );
210 47891 : target_bits = extract_l( L_tmp );
211 :
212 : /* Calculate inverse envelope and find initial scale guess based on mean */
213 47891 : mean = L_deposit_l( 0 );
214 9978845 : FOR( k = 0; k < L_frame; k++ )
215 : {
216 : /* ienv[k] = 1.0f / env[k];
217 : mean += ienv[k]; */
218 :
219 9930954 : tmp = norm_l( env[k] );
220 9930954 : tmp2 = sub( 15, tmp );
221 9930954 : tmp = Inv16( round_fx_sat( L_shl( env[k], tmp ) ), &tmp2 ); /* exp(tmp2) */
222 9930954 : ienv[k] = L_shl( L_deposit_h( tmp ), sub( tmp2, 15 ) ); /* Q16 */
223 9930954 : move32();
224 9930954 : mean = L_add( mean, ienv[k] ); /* Q16 */
225 : }
226 47891 : tmp = norm_s( L_frame );
227 47891 : tmp = shl( div_s( 8192, shl( L_frame, tmp ) ), sub( tmp, 7 ) );
228 47891 : mean = L_shr( Mpy_32_16_1( mean, tmp ), 6 ); /* Q16 */
229 :
230 : /* Rate dependent compensation to get closer to the target on average */
231 : /* mean = (float)pow(mean, (float)L_frame / (float)target_bits * 0.357f); */
232 47891 : tmp = BASOP_Util_Divide1616_Scale( L_frame, target_bits, &tmp2 ); /* exp(tmp2) */
233 47891 : tmp = mult_r( tmp, 11698 /*0.357f Q15*/ );
234 47891 : mean = BASOP_Util_fPow( mean, 15, L_deposit_h( tmp ), tmp2, &mean_e ); /* exp(mean_e) */
235 :
236 : /* Find first-guess scaling coefficient "scale" such that if "mean" is the
237 : * mean of the envelope, then the mean bit-consumption is approximately
238 : *
239 : * log2(2*e*mean*scale + 0.15 + 0.035/(mean*scale)) * L_frame = target_bits
240 : */
241 : /* a = 2*2.71828183f*mean*mean; */
242 47891 : tmp = round_fx( mean ); /* Q15 - mean_e */
243 47891 : a = L_mult( mult_r( tmp, 22268 /*2.71828183f Q13*/ ), tmp ); /* 2 * mean_e + 3 */
244 47891 : a_e = add( shl( mean_e, 1 ), 3 );
245 :
246 : /* b = (0.15f - (float)pow(2.0f, target_bits/(float)L_frame)) * mean; */
247 47891 : tmp = BASOP_Util_Divide1616_Scale( target_bits, L_frame, &tmp2 ); /* exp(tmp2) */
248 47891 : tmp = round_fx( BASOP_util_Pow2( L_deposit_h( tmp ), tmp2, &tmp2 ) );
249 47891 : b_e = BASOP_Util_Add_MantExp( 4915 /*0.15f Q15*/, 0, negate( tmp ), tmp2, &b );
250 47891 : b = mult_r( b, round_fx( mean ) ); /* exp(b_e + mean_e) */
251 47891 : b_e = add( b_e, mean_e );
252 :
253 : /* scale = (-b + (float)sqrt(b*b - 4.0f*a*0.035f)) / (2.0f * a); */
254 47891 : tmp = round_fx_sat( 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 ) );
255 :
256 47891 : IF( tmp <= 0 )
257 : {
258 0 : tmp = 0;
259 0 : move16();
260 0 : set16_fx( s_env, 0, L_frame );
261 : }
262 : ELSE
263 : {
264 47891 : tmp = Sqrt16( tmp, &tmp2 );
265 : }
266 :
267 47891 : tmp2 = BASOP_Util_Add_MantExp( negate( b ), b_e, tmp, tmp2, &scale ); /* exp(scale) */
268 47891 : scale = BASOP_Util_Divide1616_Scale( scale, round_fx( a ), &tmp );
269 47891 : scale = shl_sat( scale, sub( sub( add( tmp, tmp2 ), a_e ), 1 ) ); /* Q15 */
270 :
271 : /* iscale = 1.0f / scale; */
272 47891 : iscale_e = 0;
273 47891 : move16();
274 47891 : iscale = Inv16( s_max( 1, scale ), &iscale_e ); /* exp(isacle_e) */
275 :
276 47891 : lob = 0;
277 47891 : move16();
278 47891 : hib = 0;
279 47891 : move16();
280 :
281 47891 : max_iter = 2;
282 47891 : move16();
283 47891 : if ( low_complexity )
284 : {
285 44721 : max_iter = 1;
286 44721 : move16();
287 : }
288 :
289 98952 : FOR( iter = 0; iter < max_iter; iter++ )
290 : {
291 51061 : statesi = 0x7FFF; /* 1 in Q15 */
292 51061 : move16();
293 51061 : bits = 0;
294 51061 : move16();
295 :
296 10624951 : FOR( k = 0; k < L_frame; k++ )
297 : {
298 10573890 : s = Mpy_32_16_1( ienv[k], scale ); /* Q16 */
299 :
300 10573890 : IF( LE_32( s, 5243l /*0.08f Q16*/ ) )
301 : {
302 : /* If s = 0.08, the expected bit-consumption is log2(1.0224). Below 0.08, the bit-consumption
303 : estimate function becomes inaccurate, so use log2(1.0224) for all values below 0.08. */
304 : /* round(state * 1.0224 * 32768) */
305 31445 : statesi = mult_r( statesi, 16751 /*1.0224 Q14*/ ); /* Q14 */
306 31445 : tmp = norm_s( statesi );
307 31445 : statesi = shl( statesi, tmp ); /* Q15 */
308 31445 : bits = add( bits, sub( 1, tmp ) ); /* Q0 */
309 : }
310 10542445 : ELSE IF( LE_32( s, 16711680l /*255.0 Q16*/ ) )
311 : {
312 : /* a = 5.436564f * s + 0.15f + 0.035f * env[k] * iscale; */
313 10542445 : L_tmp = L_shl( Mpy_32_16_1( s, 22268 /*5.436564f Q12*/ ), 3 ); /* Q16 */
314 10542445 : L_tmp = L_add( L_tmp, 9830l /*0.15f Q16*/ ); /* Q16 */
315 10542445 : L_tmp = L_add( L_tmp, L_shl( Mpy_32_16_1( env[k], mult_r( 1147 /*0.035f Q15*/, iscale ) ), iscale_e ) ); /* Q16 */
316 :
317 10542445 : tmp = norm_l( L_tmp );
318 10542445 : statesi = mult_r( statesi, round_fx_sat( L_shl( L_tmp, tmp ) ) );
319 10542445 : bits = add( bits, sub( 15, tmp ) );
320 :
321 10542445 : tmp = norm_s( statesi );
322 10542445 : statesi = shl( statesi, tmp );
323 10542445 : bits = sub( bits, tmp ); /* Q0 */
324 : }
325 : ELSE
326 : {
327 : /* for large envelope values, s > 255, bit consumption is approx log2(2*e*s)
328 : * further, we use round(log2(x)) = floor(log2(x)+0.5) = floor(log2(x*sqrt(2))) */
329 : /* a = 5.436564f * s; */
330 0 : L_tmp = Mpy_32_16_1( s, 31492 /*5.436564f * 1.4142f Q12*/ ); /* Q13 */
331 0 : bits = add( bits, sub( 17, norm_l( L_tmp ) ) ); /* Q0 */
332 : }
333 : }
334 :
335 51061 : IF( LE_16( bits, target_bits ) ) /* Bits leftover => scale is too small */
336 : {
337 3841 : lob = scale; /* Q0 */
338 3841 : move16();
339 3841 : lob_bits = bits; /* Q0 */
340 3841 : move16();
341 :
342 3841 : IF( hib > 0 ) /* Bisection search */
343 : {
344 2839 : adjust = div_s( sub( hib_bits, target_bits ), sub( hib_bits, lob_bits ) ); /* Q15 */
345 2839 : scale = add( mult_r( sub( lob, hib ), adjust ), hib );
346 : }
347 : ELSE /* Initial scale adaptation */
348 : {
349 : /* adjust = 1.05f * target_bits / (float)bits;
350 : scale *= adjust; */
351 1002 : adjust = mult_r( 17203 /*1.05f Q14*/, target_bits );
352 1002 : adjust = BASOP_Util_Divide1616_Scale( adjust, bits, &tmp ); /* exp(tmp) */
353 1002 : scale = shl( mult_r( scale, adjust ), add( 1, tmp ) );
354 : }
355 : }
356 : ELSE /* Ran out of bits => scale is too large */
357 : {
358 47220 : hib = scale;
359 47220 : move16();
360 47220 : hib_bits = bits;
361 47220 : move16();
362 :
363 47220 : IF( lob > 0 ) /* Bisection search */
364 : {
365 117 : adjust = div_s( sub( hib_bits, target_bits ), sub( hib_bits, lob_bits ) ); /* Q15 */
366 117 : scale = add( mult_r( sub( lob, hib ), adjust ), hib );
367 : }
368 : ELSE
369 : { /* Initial scale adaptation */
370 47103 : test();
371 47103 : IF( target_bits <= 0 || bits <= 0 ) /* safety check in case of bit errors */
372 : {
373 0 : adjust = 0;
374 0 : move16();
375 0 : set16_fx( s_env, 0, L_frame );
376 : }
377 : ELSE
378 : {
379 47103 : adjust = div_s( mult_r( 31130 /*0.95f Q15*/, target_bits ), bits ); /* Q15 */
380 : }
381 47103 : scale = mult_r( scale, adjust );
382 : }
383 : }
384 51061 : iscale_e = 0;
385 51061 : move16();
386 :
387 51061 : IF( scale == 0 ) /* safety check in case of bit errors */
388 : {
389 0 : iscale = 0;
390 0 : move16();
391 0 : set16_fx( s_env, 0, L_frame );
392 : }
393 : ELSE
394 : {
395 51061 : iscale = Inv16( scale, &iscale_e );
396 : }
397 : }
398 47891 : L_frame = L_spec_core; /* Q0 */
399 47891 : move16();
400 :
401 47891 : tmp = getScaleFactor32( env, L_frame );
402 47891 : *s_env_e = sub( add( 15, iscale_e ), tmp );
403 47891 : move16();
404 : BASOP_SATURATE_WARNING_OFF_EVS;
405 47891 : a = L_shl_sat( 1265000, sub( 15, *s_env_e ) );
406 : BASOP_SATURATE_WARNING_ON_EVS;
407 :
408 30269491 : FOR( k = 0; k < L_frame; k++ )
409 : {
410 30221600 : L_tmp = Mpy_32_16_1( L_shl( env[k], tmp ), iscale ); /* Q31 - e */
411 30221600 : L_tmp = L_min( L_tmp, a ); /* Q31 - e */
412 30221600 : s_env[k] = round_fx( L_tmp ); /* Q15 - e */
413 30221600 : move16();
414 : }
415 47891 : }
416 :
417 : /*------------------------------------------------------------------------
418 : * Function: tcx_arith_render_envelope
419 : *
420 : * Calculate the envelope of the spectrum based on the LPC shape. The
421 : * envelope is used in a perceptual domain, whereby the LPC shape has to
422 : * be multiplied by the perceptual model.
423 : * Operations that are performed on the spectrum, which change the magnitude
424 : * expectation of lines, such as low-frequency emphasis, are included in the
425 : * envelope shape.
426 : * NOTE: This function must be bit-exact on all platforms such that encoder
427 : * and decoder remain synchronized.
428 : *-------------------------------------------------------------------------*/
429 24233 : void tcx_arith_render_envelope(
430 : const Word16 A_ind[], /* i: LPC coefficients of signal envelope Q12*/
431 : const Word16 L_frame, /* i: number of spectral lines Q0*/
432 : const Word16 L_spec, /* Q0 */
433 : const Word16 preemph_fac, /* i: pre-emphasis factor Q15*/
434 : const Word16 gamma_w, /* i: A_ind -> weighted envelope factor Q15*/
435 : const Word16 gamma_uw, /* i: A_ind -> non-weighted envelope factor Q14*/
436 : Word32 env[] /* o: shaped signal envelope Q16*/
437 : )
438 : {
439 : Word16 k;
440 : Word16 tmpA[M + 2];
441 : Word16 signal_env[FDNS_NPTS], signal_env_e[FDNS_NPTS];
442 : Word16 gainlpc[FDNS_NPTS], gainlpc_e[FDNS_NPTS];
443 :
444 :
445 : /* Compute perceptual LPC envelope, transform it into freq.-domain gains */
446 24233 : weight_a_fx( A_ind, tmpA, gamma_w, M );
447 24233 : lpc2mdct( tmpA, M, NULL, NULL, gainlpc, gainlpc_e, FDNS_NPTS, 0 );
448 :
449 : /* Add pre-emphasis tilt to LPC envelope, transform LPC into MDCT gains */
450 24233 : E_LPC_a_weight_inv( A_ind, signal_env, gamma_uw, M );
451 24233 : E_LPC_a_add_tilt( signal_env, tmpA, preemph_fac, M );
452 24233 : lpc2mdct( tmpA, M + 1, signal_env, signal_env_e, NULL, NULL, FDNS_NPTS, 0 );
453 :
454 : /* Compute weighted signal envelope in perceptual domain */
455 1575145 : FOR( k = 0; k < FDNS_NPTS; k++ )
456 : {
457 1550912 : signal_env[k] = mult_r( signal_env[k], gainlpc[k] ); /* exp(signal_env_e + gainlpc_e) */
458 1550912 : move16();
459 1550912 : signal_env_e[k] = add( signal_env_e[k], gainlpc_e[k] );
460 1550912 : move16();
461 : }
462 :
463 : /* Adaptive low frequency emphasis */
464 24233 : set32_fx( env, 0x10000 /* 1 in Q16 */, L_frame );
465 :
466 24233 : AdaptLowFreqDeemph( env, 15,
467 : 1,
468 : gainlpc, gainlpc_e,
469 : L_frame, NULL );
470 :
471 : /* Scale from FDNS_NPTS to L_frame and multiply LFE gains */
472 24233 : mdct_noiseShaping_interp( env, L_frame, signal_env, signal_env_e );
473 :
474 8991529 : FOR( k = L_frame; k < L_spec; ++k )
475 : {
476 8967296 : env[k] = env[k - 1]; /* Q16 */
477 8967296 : move32();
478 : }
479 24233 : }
480 :
481 : #define WMC_TOOL_SKIP
482 :
483 : /*-------------------------------------------------------*
484 : * expfp_evs()
485 : *
486 : * Fixed point implementation of exp()
487 : *-------------------------------------------------------*/
488 :
489 : /*! r: Q15 */
490 14718918 : Word16 expfp_evs_fx(
491 : const Word16 x, /* i : mantissa Q15-e */
492 : const Word16 x_e /* i : exponent Q0 */
493 : )
494 : {
495 : Word16 xi, xf, tmp;
496 : Word16 b0, b1, b2, b3;
497 : Word32 y, L_tmp;
498 :
499 14718918 : assert( x <= 0 );
500 :
501 14718918 : L_tmp = L_negate( L_shl( L_deposit_h( x ), sub( x_e, 15 ) ) ); /* Q16 */
502 :
503 : /* split into integer and fractional parts */
504 14718918 : xi = round_fx( L_tmp ); /* Q0 */
505 14718918 : xf = extract_l( L_tmp ); /* Q16 */
506 :
507 : BASOP_SATURATE_WARNING_OFF;
508 14718918 : xf = negate( xf );
509 : BASOP_SATURATE_WARNING_ON;
510 :
511 : /* Fractional part */
512 : /* y = 65536
513 : + xf
514 : + ((xf*xf) / (2*65536))
515 : + ((((((xf*xf) / (2*65536))*xf) / 65536)*65536/3) / 65536)
516 : + ((((((((xf*xf) / (2*65536))*xf) / 65536)*65536/3) / 65536)*xf) / (4*65536)); */
517 14718918 : y = L_mac0( 65536, xf, 1 ); /* Q16 */
518 14718918 : tmp = shr( mult( xf, xf ), 2 ); /* Q15 */
519 14718918 : y = L_mac0( y, tmp, 1 ); /* Q16 */
520 14718918 : tmp = shr( mult( shr( mult( tmp, xf ), 1 ), 65536 / 3 ), 1 );
521 14718918 : y = L_mac0( y, tmp, 1 ); /* Q16 */
522 14718918 : tmp = shr( mult( tmp, xf ), 3 );
523 14718918 : y = L_mac0( y, tmp, 1 ); /* Q16 */
524 :
525 : /* Integer part */
526 14718918 : b0 = s_and( xi, 1 );
527 14718918 : b1 = s_and( xi, 2 );
528 14718918 : b2 = s_and( xi, 4 );
529 14718918 : b3 = s_and( xi, 8 );
530 :
531 14718918 : if ( b0 != 0 )
532 7102318 : y = Mpy_32_16_1( y, 24109 ); /* exp(-1) in -1Q16 */
533 14718918 : if ( b1 != 0 )
534 6866925 : y = Mpy_32_16_1( y, 17739 ); /* exp(-2) in -2Q17 */
535 14718918 : if ( b2 != 0 )
536 8234143 : y = Mpy_32_16_1( y, 19205 ); /* exp(-4) in -5Q20 */
537 14718918 : if ( b3 != 0 )
538 773551 : y = Mpy_32_16_1( y, 22513 ); /* exp(-8) in -11Q26 */
539 :
540 : /* scaling: -1*b0 - 2*b1 -5*b2 -11*b3 */
541 14718918 : y = L_shr( y, add( add( xi, shr( xi, 2 ) ), shr( b3, 3 ) ) ); /* Q16 */
542 :
543 : /* zero for xi >= 16 */
544 14718918 : if ( shr( xi, 4 ) > 0 )
545 : {
546 0 : y = L_deposit_l( 0 );
547 0 : move16();
548 : }
549 :
550 14718918 : return round_fx( L_shl( y, 15 ) );
551 : }
552 :
553 : /*-------------------------------------------------------*
554 : * powfp_odd2_evs()
555 : *
556 : * Fixed point implementation of pow(), where base is fixed point (16/16) and exponent a small *odd* integer
557 : *-------------------------------------------------------*/
558 : /*
559 : *
560 : * Returns: *pout1 = ( (base/65536)^(2*exp - 1) ) * 65536
561 : * *pout2 = ( (base/65536)^(2*exp + 1) ) * 65536
562 : *
563 : * NOTE: This function must be in sync with ari_decode_14bits_pow_ivas() */
564 :
565 :
566 : /*------------------------------------------------------------------------
567 : * Function: tcx_arith_render_envelope_flt
568 : *
569 : * Calculate the envelope of the spectrum based on the LPC shape. The
570 : * envelope is used in a perceptual domain, whereby the LPC shape has to
571 : * be multiplied by the perceptual model.
572 : * Operations that are performed on the spectrum, which change the magnitude
573 : * expectation of lines, such as low-frequency emphasis, are included in the
574 : * envelope shape.
575 : * NOTE: This function must be bit-exact on all platforms such that encoder
576 : * and decoder remain synchronized.
577 : *-------------------------------------------------------------------------*/
578 :
579 23658 : void tcx_arith_render_envelope_ivas_fx(
580 : const Word16 A_ind[], /* i : LPC coefficients of signal envelope Q12*/
581 : const Word16 L_frame, /* i : number of spectral lines Q0*/
582 : const Word16 L_spec, /* i : length of the coded spectrum Q0*/
583 : const Word16 preemph_fac, /* i : pre-emphasis factor Q15*/
584 : const Word16 gamma_w, /* i : A_ind -> weighted envelope factor Q15*/
585 : const Word16 gamma_uw, /* i : A_ind -> non-weighted envelope factor Q14*/
586 : Word32 env[] /* o : shaped signal envelope Q16*/
587 : )
588 : {
589 : Word16 k;
590 : Word16 tmpA[M + 2];
591 : Word16 signal_env[FDNS_NPTS], signal_env_e[FDNS_NPTS];
592 : Word16 gainlpc[FDNS_NPTS], gainlpc_e[FDNS_NPTS];
593 :
594 : /* Compute perceptual LPC envelope, transform it into freq.-domain gains */
595 23658 : basop_weight_a( A_ind, tmpA, gamma_w );
596 23658 : basop_lpc2mdct_fx( tmpA, M, NULL, NULL, gainlpc, gainlpc_e );
597 :
598 : /* Add pre-emphasis tilt to LPC envelope, transform LPC into MDCT gains */
599 23658 : basop_weight_a_inv( A_ind, signal_env, gamma_uw );
600 23658 : basop_E_LPC_a_add_tilt( signal_env, tmpA, preemph_fac );
601 23658 : basop_lpc2mdct_fx( tmpA, M + 1, signal_env, signal_env_e, NULL, NULL );
602 :
603 : /* Compute weighted signal envelope in perceptual domain */
604 1537770 : FOR( k = 0; k < FDNS_NPTS; k++ )
605 : {
606 1514112 : signal_env[k] = mult_r( signal_env[k], gainlpc[k] ); /* exp(signal_env_e + gainlpc_e) */
607 1514112 : move16();
608 1514112 : signal_env_e[k] = add( signal_env_e[k], gainlpc_e[k] );
609 1514112 : move16();
610 : }
611 :
612 : /* Adaptive low frequency emphasis */
613 6199082 : FOR( k = 0; k < L_frame; k++ )
614 : {
615 6175424 : env[k] = 0x10000; /* 1 in Q16 */
616 6175424 : move32();
617 : }
618 :
619 23658 : basop_PsychAdaptLowFreqDeemph_fx( env, gainlpc, gainlpc_e, NULL );
620 :
621 : /* Scale from FDNS_NPTS to L_frame and multiply LFE gains */
622 23658 : basop_mdct_noiseShaping_interp_fx( env, L_frame, signal_env, signal_env_e );
623 :
624 8777674 : FOR( k = L_frame; k < L_spec; ++k )
625 : {
626 8754016 : env[k] = env[k - 1]; /* Q16 */
627 8754016 : move32();
628 : }
629 :
630 23658 : return;
631 : }
632 :
633 : #undef WMC_TOOL_SKIP
|