Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 : #include <stdint.h>
5 : #include <stdlib.h>
6 : #include "options.h" /* Compilation switches */
7 : #include "cnst.h" /* Common constants */
8 : #include "rom_com.h" /* Static table prototypes */
9 : #include "prot_fx.h"
10 : /*---------------------------------------------------------------------*
11 : * tcq_core_LR_dec()
12 : *
13 : * Main Generic Audio Decoder Routine for LR-MDCT
14 : *---------------------------------------------------------------------*/
15 :
16 32 : void tcq_core_LR_dec_fx(
17 : Decoder_State *st_fx,
18 : Word16 *inp_vector_fx, /*Qx */
19 : const Word16 bit_budget, /*Q0 */
20 : const Word16 BANDS, /*Q0 */
21 : const Word16 *band_start, /*Q0 */
22 : const Word16 *band_width, /*Q0 */
23 : Word32 *Rk_fx, /*Q16*/
24 : Word16 *npulses, /*Q0 */
25 : Word16 *k_sort, /*Q0 */
26 : const Word16 *p2a_flags, /*Q0 */
27 : const Word16 p2a_bands, /*Q0 */
28 : const Word16 *last_bitalloc, /*Q0 */
29 : const Word16 input_frame, /*Q0 */
30 : const Word16 adjustFlag, /*Q0 */
31 : const Word16 *is_transient /*Q0 */
32 : )
33 : {
34 : Word16 i, j, k;
35 : Word32 Rk_sort_fx[NB_SFM];
36 32 : Word16 flag_wbnb = 0;
37 32 : move16();
38 : Word16 USQ_TCQ[NB_SFM]; /* TCQ is selected by default*/
39 : Word16 nb_bytes, pulsesnum, nz;
40 :
41 : Word16 positions_fx[L_FRAME48k];
42 : Word32 surplus_fx, delta_fx;
43 : Word16 k_num[2];
44 : Word32 bit_surplus_fx[2];
45 : ARCODEC ardec_fx, *pardec_fx;
46 : TCQ_BITSTREAM bs_fx, *pbs_fx;
47 :
48 32 : Word16 nzb = 0;
49 32 : move16();
50 32 : Word16 nzbands = 0;
51 32 : move16();
52 32 : Word16 lsbtcq_bits = TCQ_AMP;
53 32 : move16();
54 32 : Word16 tcq_arbits = 2;
55 32 : move16();
56 :
57 : /* LSB TCQ variables*/
58 : Word16 dpath[280];
59 32 : Word16 bcount = 0;
60 32 : move16();
61 32 : Word32 bsub_fx = 0;
62 32 : move32();
63 : Word16 mbuffer_fx[560];
64 :
65 32 : Word32 leftbits = 0;
66 32 : move32();
67 32 : Word32 sepbits = 0;
68 32 : move32();
69 32 : Word32 divider = 0;
70 32 : move32();
71 :
72 : #ifndef BASOP_NOGLOB_DECLARE_LOCAL
73 : if ( Overflow == 1 )
74 : {
75 : Overflow = 0; /* set overflow flag to zero before entering TCQ functions without any message */
76 : }
77 : #endif
78 :
79 : /* initialization */
80 32 : set16_fx( dpath, 0, 280 );
81 32 : set16_fx( USQ_TCQ, 0, NB_SFM );
82 32 : set16_fx( positions_fx, 0, L_FRAME32k );
83 32 : set16_fx( mbuffer_fx, 0, 560 );
84 :
85 32 : test();
86 32 : test();
87 32 : IF( LE_16( input_frame, L_FRAME16k ) && ( adjustFlag == 0 ) && ( *is_transient == 0 ) )
88 : {
89 0 : flag_wbnb = 1;
90 0 : move16();
91 0 : lsbtcq_bits = 0;
92 0 : move16();
93 0 : tcq_arbits = 0;
94 0 : move16();
95 : }
96 :
97 32 : pardec_fx = &ardec_fx;
98 32 : pbs_fx = &bs_fx;
99 32 : pbs_fx->curPos = 7;
100 32 : move16();
101 32 : pbs_fx->numbits = L_deposit_l( 0 );
102 32 : move32();
103 32 : pbs_fx->numByte = L_deposit_l( 0 );
104 32 : move32();
105 :
106 : /* Bits distribution analysis*/
107 756 : FOR( i = 0; i < BANDS; i++ )
108 : {
109 724 : IF( GE_32( ar_div( Rk_fx[i], band_width[i] ), 49152 ) )
110 : {
111 : /* USQ used for high importance bands*/
112 270 : USQ_TCQ[i] = 1;
113 270 : move16();
114 : }
115 : ELSE
116 : {
117 : /* TCQ used for usual bands*/
118 454 : USQ_TCQ[i] = 0;
119 454 : move16();
120 : }
121 724 : if ( Rk_fx[i] > 0 )
122 : {
123 515 : nzbands = add( nzbands, 1 );
124 : }
125 : }
126 :
127 756 : FOR( j = 0; j < BANDS; j++ )
128 : {
129 724 : if ( Rk_fx[j] > 0 )
130 : {
131 515 : nzb = add( nzb, 1 );
132 : }
133 : }
134 :
135 32 : bsub_fx = L_shl( L_add( tcq_arbits, lsbtcq_bits ), 16 );
136 32 : if ( bsub_fx > 0 )
137 : {
138 32 : bsub_fx = L_add( bsub_fx, 2048 );
139 : }
140 756 : FOR( j = sub( BANDS, 1 ); j >= 0; j-- )
141 : {
142 724 : IF( Rk_fx[j] > 0 )
143 : {
144 515 : Rk_fx[j] = L_sub( Rk_fx[j], ar_div( bsub_fx, nzb ) ); /*Q16*/
145 515 : move32();
146 515 : IF( Rk_fx[j] < 0 )
147 : {
148 0 : bsub_fx = L_sub( bsub_fx, L_add( ar_div( bsub_fx, nzb ), Rk_fx[j] ) );
149 0 : Rk_fx[j] = L_deposit_l( 0 );
150 0 : move32();
151 : }
152 : ELSE
153 : {
154 515 : bsub_fx = L_sub( bsub_fx, ar_div( bsub_fx, nzb ) );
155 : }
156 515 : nzb = sub( nzb, 1 );
157 : }
158 : }
159 :
160 32 : srt_vec_ind_fx( Rk_fx, Rk_sort_fx, k_sort, BANDS );
161 :
162 : /*read the bits*/
163 32 : nb_bytes = shr( bit_budget, 3 );
164 32 : k = sub( bit_budget, shl( nb_bytes, 3 ) );
165 567 : FOR( i = 0; i < nb_bytes; i++ )
166 : {
167 535 : pbs_fx->buf[i] = (UWord8) get_next_indice_fx( st_fx, 8 );
168 535 : move16();
169 : }
170 :
171 32 : IF( k > 0 )
172 : {
173 : Word16 tmp;
174 30 : tmp = (Word16) get_next_indice_fx( st_fx, k );
175 30 : pbs_fx->buf[nb_bytes] = (UWord8) shl( tmp, sub( 8, k ) );
176 30 : move16();
177 : /* i++;
178 : nb_bytes++; */
179 30 : i = add( i, 1 );
180 30 : nb_bytes = add( nb_bytes, 1 );
181 : }
182 : /* set two more bytes, which are used to flush the arithmetic coder, to 0
183 : -> this avoids reading of uninitialized memory */
184 32 : nb_bytes = s_min( add( nb_bytes, 2 ), MAX_SIZEBUF_PBITSTREAM );
185 96 : FOR( ; i < nb_bytes; i++ )
186 : {
187 64 : pbs_fx->buf[i] = 0;
188 64 : move16();
189 : }
190 :
191 32 : pbs_fx->maxBytes = nb_bytes;
192 32 : move16();
193 :
194 32 : ar_decoder_start_fx( pardec_fx, pbs_fx );
195 :
196 32 : delta_fx = L_deposit_l( 0 );
197 32 : surplus_fx = L_deposit_l( 0 );
198 :
199 32 : test();
200 32 : test();
201 32 : IF( LE_16( input_frame, L_FRAME16k ) && ( adjustFlag == 0 ) && ( *is_transient == 0 ) )
202 : {
203 0 : surplus_fx = -131072;
204 0 : move32(); /*2 in Q16 */
205 :
206 0 : bit_allocation_second_fx( Rk_fx, Rk_sort_fx, BANDS, band_width, k_sort, k_num, p2a_flags, p2a_bands, last_bitalloc, input_frame );
207 :
208 0 : nzbands = 0;
209 0 : move16();
210 0 : FOR( j = 0; j < BANDS; j++ )
211 : {
212 0 : test();
213 0 : IF( EQ_16( j, k_num[0] ) || EQ_16( j, k_num[1] ) )
214 : {
215 0 : sepbits = L_add( sepbits, Rk_fx[k_sort[j]] );
216 : }
217 : ELSE
218 : {
219 0 : leftbits = L_add( leftbits, Rk_fx[k_sort[j]] );
220 0 : if ( Rk_fx[k_sort[j]] > 0 )
221 : {
222 0 : nzbands = add( nzbands, 1 );
223 : }
224 : }
225 : }
226 :
227 0 : FOR( k = 0; k < BANDS; k++ )
228 : {
229 0 : test();
230 0 : IF( NE_16( k, k_num[0] ) && NE_16( k, k_num[1] ) )
231 : {
232 0 : test();
233 0 : test();
234 0 : IF( ( Rk_fx[k_sort[k]] > 0 ) && ( USQ_TCQ[k_sort[k]] == 0 ) )
235 : {
236 : /* When number of bits per band is less than
237 : arithmetic bits overhead, this band is not encoded.
238 : It may happens when the actual number of bits per
239 : band is near same to estimated number of bits, for
240 : most bands (very unprobable but possible) */
241 0 : IF( L_add( Rk_fx[k_sort[k]], delta_fx ) < 0 )
242 : {
243 0 : pulsesnum = 0;
244 0 : move16();
245 0 : FOR( i = 0; i < band_width[k_sort[k]]; i++ )
246 : {
247 0 : inp_vector_fx[band_start[k_sort[k]] + i] = 0;
248 0 : move16();
249 : }
250 0 : IF( surplus_fx != 0 )
251 : {
252 : /* surplus_fx += (Rk[k_sort[k]] + delta);*/
253 0 : surplus_fx = L_add( Rk_fx[k_sort[k]], surplus_fx );
254 0 : surplus_fx = L_add( delta_fx, surplus_fx );
255 : }
256 : }
257 : ELSE
258 : {
259 : /*get number of pulses */
260 0 : pulsesnum = GetScale_fx( band_width[k_sort[k]],
261 0 : L_add( Rk_fx[k_sort[k]], delta_fx ),
262 : &surplus_fx );
263 0 : leftbits = L_sub( leftbits, L_add( Rk_fx[k_sort[k]], delta_fx ) );
264 0 : decode_position_ari_fx( pardec_fx, band_width[k_sort[k]], pulsesnum, &nz, &positions_fx[band_start[k_sort[k]]] );
265 : /*decode tcq magniitude and update the surplus bits.*/
266 0 : decode_mangitude_tcq_fx( pardec_fx, band_width[k_sort[k]], pulsesnum, nz, &positions_fx[band_start[k_sort[k]]], &inp_vector_fx[band_start[k_sort[k]]], &surplus_fx );
267 0 : decode_signs_fx( pardec_fx, band_width[k_sort[k]], &inp_vector_fx[band_start[k_sort[k]]] );
268 : }
269 0 : nzbands = sub( nzbands, 1 );
270 0 : move16();
271 : }
272 0 : ELSE IF( ( Rk_fx[k_sort[k]] > 0 ) && EQ_16( USQ_TCQ[k_sort[k]], 1 ) )
273 : {
274 : /* When number of bits per band is less than
275 : arithmetic bits overhead, this band is not encoded.
276 : It may happens when the actual number of bits per
277 : band is near same to estimated number of bits, for
278 : most bands (very unprobable but possible) */
279 0 : IF( L_add( Rk_fx[k_sort[k]], delta_fx ) < 0 )
280 : {
281 0 : pulsesnum = 0;
282 0 : move16();
283 0 : FOR( i = 0; i < band_width[k_sort[k]]; i++ )
284 : {
285 0 : inp_vector_fx[band_start[k_sort[k]] + i] = 0;
286 0 : move16();
287 : }
288 0 : IF( surplus_fx != 0 )
289 : {
290 0 : surplus_fx = L_add( Rk_fx[k_sort[k]], surplus_fx );
291 0 : surplus_fx = L_add( delta_fx, surplus_fx );
292 : }
293 : }
294 : ELSE
295 : {
296 0 : pulsesnum = GetScale_fx( band_width[k_sort[k]],
297 0 : L_add( Rk_fx[k_sort[k]], delta_fx ),
298 : &surplus_fx );
299 0 : leftbits = L_sub( leftbits, L_add( Rk_fx[k_sort[k]], delta_fx ) );
300 0 : decode_position_ari_fx( pardec_fx, band_width[k_sort[k]], pulsesnum, &nz, &positions_fx[band_start[k_sort[k]]] );
301 : /*decode usq magnitude and don't need to update surplus bits*/
302 0 : decode_magnitude_usq_fx( pardec_fx, band_width[k_sort[k]], pulsesnum, nz, &positions_fx[band_start[k_sort[k]]], &inp_vector_fx[band_start[k_sort[k]]] );
303 0 : decode_signs_fx( pardec_fx, band_width[k_sort[k]], &inp_vector_fx[band_start[k_sort[k]]] );
304 : }
305 0 : nzbands = sub( nzbands, 1 );
306 0 : move16();
307 : }
308 : ELSE
309 : {
310 0 : pulsesnum = 0;
311 0 : move16();
312 0 : FOR( i = 0; i < band_width[k_sort[k]]; i++ )
313 : {
314 0 : inp_vector_fx[band_start[k_sort[k]] + i] = 0;
315 0 : move16();
316 : }
317 : }
318 :
319 0 : npulses[k_sort[k]] = pulsesnum;
320 0 : move16();
321 :
322 0 : delta_fx = L_deposit_l( 0 );
323 0 : test();
324 0 : IF( ( Rk_fx[k_sort[k]] > 0 ) && ( surplus_fx < 0 ) )
325 : {
326 0 : IF( LE_16( nzbands, 1 ) )
327 : {
328 0 : divider = 0;
329 0 : move32();
330 : }
331 : ELSE
332 : {
333 0 : divider = 2;
334 0 : move32();
335 : }
336 :
337 0 : IF( L_add( L_add( surplus_fx, sepbits ), ar_div( leftbits, divider ) ) < 0 )
338 : {
339 : /* Overflow possible => start to distribute negative surplus */
340 0 : delta_fx = ar_div( L_add( surplus_fx, sepbits ), nzbands );
341 : }
342 : ELSE
343 : {
344 0 : delta_fx = 0;
345 0 : move32();
346 : }
347 0 : surplus_fx = L_sub( surplus_fx, delta_fx );
348 : }
349 : }
350 : }
351 :
352 0 : test();
353 0 : test();
354 0 : test();
355 0 : IF( ( GT_32( surplus_fx, 524288 ) && EQ_16( input_frame, L_FRAME8k ) ) || ( GT_32( surplus_fx, 786432 ) && EQ_16( input_frame, L_FRAME16k ) ) )
356 : {
357 0 : bit_surplus_fx[0] = Mult_32_16( surplus_fx, 24576 ); /* Q16 */
358 0 : move32();
359 0 : bit_surplus_fx[1] = Mult_32_16( surplus_fx, 8192 ); /* Q16 */
360 0 : move32();
361 : }
362 : ELSE
363 : {
364 0 : bit_surplus_fx[0] = surplus_fx;
365 0 : move32();
366 0 : bit_surplus_fx[1] = L_deposit_l( 0 );
367 0 : move32();
368 : }
369 :
370 0 : FOR( k = 0; k < BANDS; k++ )
371 : {
372 0 : FOR( j = 0; j < 2; j++ )
373 : {
374 0 : IF( EQ_16( k, k_num[j] ) )
375 : {
376 0 : Rk_fx[k_sort[k]] = L_add( Rk_fx[k_sort[k]], bit_surplus_fx[j] );
377 0 : move32();
378 :
379 0 : test();
380 0 : test();
381 0 : IF( ( Rk_fx[k_sort[k]] > 0 ) && ( USQ_TCQ[k_sort[k]] == 0 ) )
382 : {
383 : /* get number of pulses */
384 0 : pulsesnum = GetScale_fx( band_width[k_sort[k]], Rk_fx[k_sort[k]], &surplus_fx );
385 :
386 0 : decode_position_ari_fx( pardec_fx, band_width[k_sort[k]], pulsesnum, &nz, &positions_fx[band_start[k_sort[k]]] );
387 : /* decode tcq magniitude and update the surplus bits. */
388 0 : decode_mangitude_tcq_fx( pardec_fx, band_width[k_sort[k]], pulsesnum, nz, &positions_fx[band_start[k_sort[k]]], &inp_vector_fx[band_start[k_sort[k]]], &surplus_fx );
389 0 : decode_signs_fx( pardec_fx, band_width[k_sort[k]], &inp_vector_fx[band_start[k_sort[k]]] );
390 : }
391 0 : ELSE IF( ( Rk_fx[k_sort[k]] > 0 ) && EQ_16( USQ_TCQ[k_sort[k]], 1 ) )
392 : {
393 0 : pulsesnum = GetScale_fx( band_width[k_sort[k]], Rk_fx[k_sort[k]], &surplus_fx );
394 :
395 0 : decode_position_ari_fx( pardec_fx, band_width[k_sort[k]], pulsesnum, &nz, &positions_fx[band_start[k_sort[k]]] );
396 : /* decode usq magnitude and don't need to update surplus bits */
397 0 : decode_magnitude_usq_fx( pardec_fx, band_width[k_sort[k]], pulsesnum, nz, &positions_fx[band_start[k_sort[k]]], &inp_vector_fx[band_start[k_sort[k]]] );
398 0 : decode_signs_fx( pardec_fx, band_width[k_sort[k]], &inp_vector_fx[band_start[k_sort[k]]] );
399 : }
400 : ELSE
401 : {
402 0 : pulsesnum = 0;
403 0 : move16();
404 0 : FOR( i = 0; i < band_width[k_sort[k]]; i++ )
405 : {
406 0 : inp_vector_fx[band_start[k_sort[k]] + i] = 0;
407 0 : move16();
408 : }
409 : }
410 :
411 0 : npulses[k_sort[k]] = pulsesnum;
412 0 : move16();
413 : }
414 : }
415 : }
416 : }
417 : ELSE
418 : {
419 756 : FOR( k = 0; k < BANDS; k++ )
420 : {
421 724 : IF( Rk_fx[k_sort[k]] > 0 )
422 : {
423 515 : pulsesnum = GetScale_fx( band_width[k_sort[k]], L_add( Rk_fx[k_sort[k]], delta_fx ), &surplus_fx );
424 :
425 515 : decode_position_ari_fx( pardec_fx, band_width[k_sort[k]], pulsesnum, &nz, &positions_fx[band_start[k_sort[k]]] );
426 :
427 : /*decode usq magnitude and don't need to update surplus bits*/
428 515 : decode_magnitude_usq_fx( pardec_fx, band_width[k_sort[k]], pulsesnum, nz, &positions_fx[band_start[k_sort[k]]], &inp_vector_fx[band_start[k_sort[k]]] );
429 515 : decode_signs_fx( pardec_fx, band_width[k_sort[k]], &inp_vector_fx[band_start[k_sort[k]]] );
430 :
431 515 : nzbands = sub( nzbands, 1 );
432 : }
433 : ELSE
434 : {
435 209 : pulsesnum = 0;
436 209 : move16();
437 11409 : FOR( i = 0; i < band_width[k_sort[k]]; i++ )
438 : {
439 11200 : inp_vector_fx[band_start[k_sort[k]] + i] = 0;
440 11200 : move16();
441 : }
442 : }
443 :
444 724 : npulses[k_sort[k]] = pulsesnum;
445 724 : move16();
446 :
447 : /* surplus distribution */
448 724 : test();
449 724 : IF( ( surplus_fx > 0 ) && ( nzbands > 0 ) )
450 : {
451 483 : delta_fx = ar_div( surplus_fx, nzbands );
452 483 : surplus_fx = L_sub( surplus_fx, delta_fx );
453 : }
454 : }
455 : }
456 :
457 : /* Load TCQ path from bitstream */
458 32 : LoadTCQdata_fx( pardec_fx, dpath, lsbtcq_bits );
459 :
460 32 : TCQLSBdec_fx( dpath, mbuffer_fx, i_mult( 2, lsbtcq_bits ) );
461 :
462 32 : ar_decoder_done_fx( pardec_fx );
463 :
464 : /* Restore TCQ */
465 32 : IF( !flag_wbnb )
466 : {
467 756 : FOR( k = 0; k < BANDS; k++ )
468 : {
469 724 : IF( Rk_fx[k_sort[k]] > 0 )
470 : {
471 515 : RestoreTCQdec_fx( &inp_vector_fx[band_start[k_sort[k]]], band_width[k_sort[k]], &bcount, mbuffer_fx );
472 : }
473 : }
474 : }
475 : ELSE
476 : {
477 0 : FOR( k = 0; k < BANDS; k++ )
478 : {
479 0 : test();
480 0 : test();
481 0 : IF( ( Rk_fx[k_sort[k]] > 0 ) && NE_16( k, k_num[0] ) && NE_16( k, k_num[1] ) )
482 : {
483 0 : RestoreTCQdec_fx( &inp_vector_fx[band_start[k_sort[k]]], band_width[k_sort[k]], &bcount, mbuffer_fx );
484 : }
485 : }
486 :
487 0 : FOR( k = 0; k < BANDS; k++ )
488 : {
489 0 : test();
490 0 : test();
491 0 : IF( ( Rk_fx[k_sort[k]] > 0 ) && ( EQ_16( k, k_num[0] ) || EQ_16( k, k_num[1] ) ) )
492 : {
493 0 : RestoreTCQdec_fx( &inp_vector_fx[band_start[k_sort[k]]], band_width[k_sort[k]], &bcount, mbuffer_fx );
494 : }
495 : }
496 : }
497 :
498 :
499 32 : return;
500 : }
|