Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <assert.h>
6 : #include <stdint.h>
7 : #include "options.h"
8 : #include "cnst.h"
9 : #include "rom_com.h"
10 : #include "prot_fx.h"
11 : #include "basop_util.h"
12 : #include "basop_proto_func.h"
13 :
14 : /* Returns: number of bits consumed */
15 0 : static Word16 tcx_arith_decode_fx(
16 : Word16 L_frame, /* i: number of spectral lines Q0 */
17 : const Word16 envelope[], /* i: scaled envelope Q15-e */
18 : Word16 envelope_e, /* i: scaled envelope exponent Q0 */
19 : Word16 target_bits, /* i: target bit budget Q0 */
20 : Word16 prm[], /* i: bit-stream Q0 */
21 : Word32 q_spectrum[], /* o: scalar quantized spectrum Q31-e */
22 : Word16 *q_spectrum_e, /* o: spectrum exponent Q0 */
23 : Word16 *nf_seed /* o: noise filling seed Q0 */
24 : )
25 : {
26 : Word16 bp, k, q, s;
27 : Tastat as;
28 : Word16 exp_k;
29 : Word16 tmp;
30 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
31 : Word32 Q;
32 : Word64 W_tmp;
33 0 : move32();
34 : #endif
35 :
36 0 : bp = ari_start_decoding_14bits_prm_fx( prm, 0, &as );
37 :
38 0 : tmp = sub( envelope_e, 1 + 15 );
39 0 : W_tmp = 0;
40 :
41 0 : FOR( k = 0; k < L_frame; k++ )
42 : {
43 0 : IF( envelope[k] == 0 ) /* safety check in case of bit errors */
44 : {
45 0 : set32_fx( q_spectrum, 0, L_frame );
46 0 : return -1;
47 : }
48 : ELSE
49 : {
50 0 : exp_k = round_fx( expfp( envelope[k], tmp ) );
51 : }
52 : /* decode line magnitude */
53 0 : bp = ari_decode_14bits_pow_fx( prm, bp, target_bits, &q, &as, exp_k );
54 :
55 0 : IF( q == 0 )
56 : {
57 0 : q_spectrum[k] = L_deposit_l( 0 );
58 0 : move32();
59 : }
60 0 : IF( q != 0 ) /* line is non-zero, decode sign */
61 : {
62 0 : bp = ari_decode_14bits_sign_fx( prm, bp, target_bits, &s, &as );
63 :
64 0 : W_tmp = W_mac_16_16( W_tmp, q, k );
65 :
66 0 : Q = L_mult( q, negate( shl( 1, sub( 30, SPEC_EXP_DEC ) ) ) );
67 0 : IF( EQ_16( s, 0 ) )
68 0 : Q = L_mult( q, shl( 1, sub( 30, SPEC_EXP_DEC ) ) );
69 0 : q_spectrum[k] = Q;
70 0 : move32();
71 : }
72 :
73 0 : IF( ari_decode_overflow_fx( &as ) )
74 : {
75 0 : if ( LT_16( bp, target_bits ) ) /* safety check in case of bit errors */
76 : {
77 0 : bp = -1;
78 0 : move16();
79 : }
80 0 : BREAK; /* no bits left, so exit loop */
81 : }
82 : }
83 0 : *q_spectrum_e = SPEC_EXP_DEC;
84 0 : move16();
85 :
86 0 : set32_fx( q_spectrum + k, 0, sub( L_frame, k ) );
87 :
88 : /* noise filling seed */
89 0 : *nf_seed = extract_l( W_extract_l( W_tmp ) );
90 0 : move16();
91 :
92 0 : return bp;
93 : }
94 :
95 0 : void tcx_arith_decode_envelope_fx(
96 : Word32 q_spectrum[], /* o: quantised MDCT coefficients Q31-e */
97 : Word16 *q_spectrum_e, /* o: MDCT exponent Q0 */
98 : Word16 L_frame, /* i: frame or MDCT length Q0 */
99 : Word16 L_spec, /* i: length w/o BW limitation Q0 */
100 : Decoder_State *st,
101 : const Word16 A_ind[], /* i: quantised LPC coefficients Q12 */
102 : Word16 target_bits, /* i: number of available bits Q0 */
103 : Word16 prm[], /* i: bitstream parameters Q0 */
104 : Word8 use_hm, /* i: use HM in current frame? */
105 : Word16 prm_hm[], /* i: HM parameter area Q0 */
106 : Word16 tcxltp_pitch, /* i: TCX LTP pitch in FD, -1 if n/a Q0*/
107 : Word16 *arith_bits, /* o: bits used for ari. coding Q0 */
108 : Word16 *signaling_bits, /* o: bits used for signaling Q0 */
109 : Word16 *nf_seed, /* o: noise filling seed Q0 */
110 : Word16 low_complexity /* i: low-complexity flag Q0 */
111 : )
112 : {
113 : Word32 env[N_MAX_ARI]; /* unscaled envelope */
114 : Word16 *envelope; /* scaled envelope */
115 : Word16 envelope_e;
116 : Word16 L_spec_core;
117 : TCX_CONFIG_HANDLE tcx_cfg;
118 : Word16 gamma_w, gamma_uw;
119 : Word16 hm_bits;
120 0 : tcx_cfg = st->hTcxCfg;
121 :
122 0 : test();
123 0 : test();
124 0 : test();
125 0 : test();
126 0 : test();
127 0 : test();
128 0 : test();
129 0 : IF( GT_16( L_spec, N_MAX_ARI ) || ( EQ_16( st->element_mode, EVS_MONO ) && GT_16( target_bits, ( ACELP_13k20 / FRAMES_PER_SEC ) ) ) ||
130 : ( EQ_16( st->element_mode, IVAS_SCE ) && ( GT_16( st->bits_frame_nominal, ( LPC_SHAPED_ARI_MAX_RATE / FRAMES_PER_SEC ) ) ) ) ||
131 : ( GT_16( st->element_mode, IVAS_SCE ) && ( GT_16( st->bits_frame_nominal, ( LPC_SHAPED_ARI_MAX_RATE_CPE / FRAMES_PER_SEC ) ) ) ) ||
132 : ( target_bits <= 0 ) )
133 : {
134 : /* this could happen in case of bit errors */
135 0 : st->BER_detect = 1;
136 0 : move16();
137 0 : L_spec = N_MAX_ARI;
138 0 : move16();
139 0 : *signaling_bits = 0;
140 0 : move16();
141 0 : *arith_bits = 0;
142 0 : move16();
143 0 : set32_fx( q_spectrum, 0, L_frame );
144 :
145 0 : return;
146 : }
147 :
148 0 : *signaling_bits = 0;
149 0 : move16();
150 0 : assert( st->hTcxDec->enableTcxLpc );
151 :
152 0 : gamma_w = 32767 /*1.0f Q15*/;
153 0 : move16();
154 0 : gamma_uw = st->inv_gamma;
155 0 : move16();
156 :
157 0 : tcx_arith_render_envelope( A_ind, L_frame, L_spec, tcx_cfg->preemph_fac, gamma_w, gamma_uw, env );
158 :
159 0 : IF( use_hm )
160 : {
161 0 : IF( prm_hm[0] )
162 : {
163 0 : tcx_hm_decode( L_spec, env, target_bits, tcx_cfg->coder_type, prm_hm, tcxltp_pitch, &hm_bits );
164 :
165 0 : IF( hm_bits < 0 )
166 : {
167 0 : st->BER_detect = 1;
168 0 : move16();
169 0 : *signaling_bits = 0;
170 0 : move16();
171 0 : *arith_bits = 0;
172 0 : move16();
173 0 : set32_fx( q_spectrum, 0, L_frame );
174 0 : return;
175 : }
176 : }
177 : ELSE
178 : {
179 0 : hm_bits = 1;
180 0 : move16();
181 : }
182 0 : *signaling_bits = add( *signaling_bits, hm_bits );
183 0 : move16();
184 : }
185 : ELSE
186 : {
187 0 : prm_hm[0] = 0; /* just to be sure */
188 0 : move16();
189 : }
190 :
191 0 : L_spec_core = L_spec;
192 0 : move16();
193 0 : IF( st->igf )
194 : {
195 0 : L_spec_core = s_min( L_spec_core, st->hIGFDec->infoIGFStartLine );
196 : }
197 0 : envelope = (Word16 *) env;
198 :
199 0 : tcx_arith_scale_envelope( L_spec, L_spec_core, env, target_bits, low_complexity, envelope, &envelope_e );
200 :
201 0 : *arith_bits = tcx_arith_decode_fx( L_spec, envelope, envelope_e, target_bits, prm, q_spectrum, q_spectrum_e, nf_seed );
202 0 : move16();
203 :
204 : /* safety check in case of bit errors */
205 0 : IF( *arith_bits < 0 )
206 : {
207 0 : st->BER_detect = 1;
208 0 : move16();
209 0 : set32_fx( q_spectrum, 0, L_frame );
210 : }
211 :
212 0 : set32_fx( q_spectrum + L_spec, 0, sub( L_frame, L_spec ) );
213 : }
214 :
215 : /*-------------------------------------------------------*
216 : * tcx_arith_decode()
217 : *
218 : *
219 : *-------------------------------------------------------*/
220 :
221 : /*! r: number of bits consumed */
222 :
223 16968 : static Word16 tcx_arith_decode_ivas_fx(
224 : const Word16 L_frame, /* i : number of spectral lines */
225 : const Word16 envelope[], /* i : scaled envelope (Q15-envelope_e) */
226 : Word16 envelope_e, /* i : scaled envelope exponent (Q0) */
227 : const Word16 target_bits, /* i : target bit budget */
228 : Word16 prm[], /* i : bitstream parameters */
229 : Word32 q_spectrum[], /* o : scalar quantized spectrum (Q31-q_spectrum_e) */
230 : Word16 *q_spectrum_e /* o : spectrum exponent */
231 : )
232 : {
233 : Word16 bp, k, q;
234 : Word16 s;
235 : Tastat as;
236 : UWord16 exp_k;
237 : Word16 tmp;
238 :
239 16968 : bp = ari_start_decoding_14bits_prm_ivas_fx( prm, 0, &as );
240 :
241 16968 : tmp = sub( envelope_e, 1 );
242 :
243 10553704 : FOR( k = 0; k < L_frame; k++ )
244 : {
245 10537161 : IF( EQ_16( envelope[k], 0 ) ) /* safety check in case of bit errors */
246 : {
247 0 : set32_fx( q_spectrum, 0, L_frame );
248 0 : return -1;
249 : }
250 : ELSE
251 : {
252 10537161 : exp_k = expfp_evs_fx( negate( envelope[k] ), tmp );
253 : }
254 :
255 : /* decode line magnitude */
256 10537161 : bp = ari_decode_14bits_pow_ivas( prm, bp, target_bits, &q, &as, exp_k );
257 :
258 10537161 : IF( q )
259 : {
260 : /* line is non-zero, decode sign */
261 491051 : bp = ari_decode_14bits_sign_ivas( prm, bp, target_bits, &s, &as );
262 491051 : q_spectrum[k] = L_mult( q, sub( 3, shl( s, 1 ) ) );
263 491051 : move32();
264 491051 : q_spectrum[k] = L_shl( q_spectrum[k], 30 - SPEC_EXP_DEC ); // Q(31-20)
265 491051 : move32();
266 : }
267 : ELSE
268 : {
269 : /* line is zero, no sign needed */
270 10046110 : q_spectrum[k] = 0;
271 10046110 : move32();
272 : }
273 :
274 10537161 : IF( LE_32( as.high, as.low ) )
275 : {
276 425 : if ( LT_16( bp, target_bits ) ) /* safety check in case of bit errors */
277 : {
278 0 : bp = -1;
279 0 : move16();
280 : }
281 425 : BREAK; /* no bits left, so exit loop */
282 : }
283 : }
284 16968 : *q_spectrum_e = SPEC_EXP_DEC;
285 16968 : move16();
286 :
287 16968 : set32_fx( q_spectrum + k, 0, sub( L_frame, k ) );
288 :
289 16968 : return bp;
290 : }
291 :
292 : /*-------------------------------------------------------*
293 : * tcx_arith_decode_envelope()
294 : *
295 : *
296 : *-------------------------------------------------------*/
297 :
298 16968 : void tcx_arith_decode_envelope_ivas_fx(
299 : Decoder_State *st, /* i/o: coder state */
300 : Word32 q_spectrum[], /* o : quantised MDCT coefficients Q(31-q_spectrum_e) */
301 : Word16 *q_spectrum_e, /* o : MDCT exponent */
302 : const Word16 L_frame, /* i : frame or MDCT length */
303 : Word16 L_spec, /* i : length w/o BW limitation */
304 : const Word16 A_ind[], /* i : quantised LPC coefficients */
305 : const Word16 target_bits, /* i : number of available bits */
306 : Word16 prm[], /* i : bitstream parameters */
307 : const Word16 use_hm, /* i : use HM in current frame? */
308 : const Word16 prm_hm[], /* i : HM parameter area */
309 : Word16 tcxltp_pitch, /* i : TCX LTP pitch in FD, -1 if n/a*/
310 : Word16 *arith_bits, /* o : bits used for ari. coding */
311 : Word16 *signaling_bits, /* o : bits used for signaling */
312 : const Word16 low_complexity /* i : low-complexity flag */
313 : )
314 : {
315 : Word32 env[N_MAX_ARI]; /* unscaled envelope (Q16) */
316 : Word16 *envelope; /* scaled envelope (Q15-e) */
317 : Word16 envelope_e;
318 : Word16 L_spec_core;
319 : TCX_CONFIG_HANDLE hTcxCfg;
320 : TCX_DEC_HANDLE hTcxDec;
321 : Word16 gamma_w, gamma_uw;
322 : Word16 hm_bits;
323 :
324 16968 : test();
325 16968 : test();
326 16968 : test();
327 16968 : test();
328 16968 : test();
329 16968 : test();
330 16968 : test();
331 16968 : IF( GT_16( L_spec, N_MAX_ARI ) || ( ( st->element_mode == EVS_MONO ) && GT_16( target_bits, ( ACELP_13k20 / FRAMES_PER_SEC ) ) ) ||
332 : ( EQ_16( st->element_mode, IVAS_SCE ) && ( GT_16( st->bits_frame_nominal, ( LPC_SHAPED_ARI_MAX_RATE / FRAMES_PER_SEC ) ) ) ) ||
333 : ( GT_16( st->element_mode, IVAS_SCE ) && ( GT_16( st->bits_frame_nominal, ( LPC_SHAPED_ARI_MAX_RATE_CPE / FRAMES_PER_SEC ) ) ) ) ||
334 : ( target_bits <= 0 ) )
335 : {
336 : /* this could happen in case of bit errors */
337 0 : st->BER_detect = 1;
338 0 : move16();
339 0 : L_spec = N_MAX_ARI;
340 0 : move16();
341 0 : *signaling_bits = 0;
342 0 : move16();
343 0 : *arith_bits = 0;
344 0 : move16();
345 0 : set32_fx( q_spectrum, 0, L_frame );
346 :
347 0 : return;
348 : }
349 :
350 16968 : hTcxCfg = st->hTcxCfg;
351 16968 : hTcxDec = st->hTcxDec;
352 16968 : *signaling_bits = 0;
353 16968 : move16();
354 :
355 16968 : assert( hTcxDec->enableTcxLpc );
356 16968 : gamma_w = MAX16B;
357 16968 : move16();
358 16968 : gamma_uw = st->inv_gamma;
359 16968 : move16();
360 :
361 : #define WMC_TOOL_SKIP
362 16968 : tcx_arith_render_envelope_ivas_fx( A_ind, L_frame, L_spec, hTcxCfg->preemph_fac, gamma_w, gamma_uw, env );
363 : #undef WMC_TOOL_SKIP
364 :
365 16968 : IF( use_hm != 0 )
366 : {
367 15353 : IF( prm_hm[0] != 0 )
368 : {
369 6810 : tcx_hm_decode( L_spec, env, target_bits, st->coder_type, prm_hm, tcxltp_pitch, &hm_bits );
370 :
371 6810 : IF( hm_bits < 0 )
372 : {
373 0 : st->BER_detect = 1;
374 0 : move16();
375 0 : *signaling_bits = 0;
376 0 : move16();
377 0 : *arith_bits = 0;
378 0 : move16();
379 0 : set32_fx( q_spectrum, 0, L_frame );
380 :
381 0 : return;
382 : }
383 : }
384 : ELSE
385 : {
386 8543 : hm_bits = 1;
387 8543 : move16();
388 : }
389 15353 : *signaling_bits = add( *signaling_bits, hm_bits );
390 15353 : move16();
391 : }
392 :
393 16968 : L_spec_core = L_spec;
394 16968 : move16();
395 16968 : IF( st->igf )
396 : {
397 16968 : L_spec_core = s_min( L_spec_core, st->hIGFDec->infoIGFStartLine );
398 : }
399 :
400 16968 : envelope = (Word16 *) env;
401 16968 : tcx_arith_scale_envelope( L_spec, L_spec_core, env, target_bits, low_complexity, envelope, &envelope_e );
402 :
403 16968 : *arith_bits = tcx_arith_decode_ivas_fx( L_spec, envelope, envelope_e, target_bits, prm, q_spectrum, q_spectrum_e );
404 16968 : move16();
405 :
406 : /* safety check in case of bit errors */
407 16968 : IF( *arith_bits < 0 )
408 : {
409 0 : st->BER_detect = 1;
410 0 : move16();
411 0 : set32_fx( q_spectrum, 0, L_frame );
412 : }
413 :
414 16968 : set32_fx( q_spectrum + L_spec, 0, sub( L_frame, L_spec ) );
415 :
416 16968 : return;
417 : }
|