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.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include "cnst.h"
43 : #include "rom_com.h"
44 : #include "prot.h"
45 : #include "basop_util.h"
46 : #include "basop_proto_func.h"
47 : #include "wmc_auto.h"
48 :
49 : /*---------------------------------------------------------------------*
50 : * tcq_core_LR_enc()
51 : *
52 : * Main Generic Audio Decoder Routine for LR-MDCT
53 : *---------------------------------------------------------------------*/
54 :
55 4234 : void tcq_core_LR_dec(
56 : Decoder_State *st,
57 : int32_t *inp_vector,
58 : const int16_t bit_budget,
59 : const int16_t BANDS,
60 : const int16_t *band_start,
61 : const int16_t *band_width,
62 : Word32 *Rk_fx,
63 : int16_t *npulses,
64 : int16_t *k_sort,
65 : const int16_t *p2a_flags,
66 : const int16_t p2a_bands,
67 : const int16_t *last_bitalloc,
68 : const int16_t input_frame,
69 : const int16_t adjustFlag,
70 : const int16_t *is_transient )
71 : {
72 : int16_t i, j, k;
73 : float Rk_sort[NB_SFM];
74 4234 : int16_t flag_wbnb = 0;
75 : int16_t USQ_TCQ[NB_SFM]; /* TCQ is selected by default*/
76 : int16_t nb_bytes, pulsesnum, nz;
77 : int32_t positions[L_FRAME32k];
78 : int16_t k_num[2];
79 : ARCODEC ardec, *pardec;
80 : TCQ_BITSTREAM bs, *pbs;
81 :
82 4234 : int16_t nzbands = 0;
83 4234 : int16_t lsbtcq_bits = TCQ_AMP;
84 4234 : int16_t tcq_arbits = 2;
85 :
86 : /* LSB TCQ variables*/
87 : int16_t dpath[280];
88 4234 : int16_t bcount = 0;
89 : float mbuffer[560];
90 :
91 4234 : Word32 leftbits = 0;
92 4234 : Word32 sepbits = 0;
93 4234 : Word32 divider = 0;
94 :
95 : /*Word32 Rk_fx[NB_SFM];*/ /* Q16 */
96 : Word32 Rk_sort_fx[NB_SFM]; /* Q16 */
97 4234 : Word32 bsub_fx = 0;
98 :
99 4234 : Word16 nzb = 0;
100 : Word32 delta_fx;
101 : Word32 surplus_fx;
102 : Word32 bit_surplus_fx[2];
103 :
104 : /* initialization */
105 4234 : set_s( dpath, 0, 280 );
106 4234 : set_f( mbuffer, 0.f, 560 );
107 4234 : set_f( Rk_sort, 0.f, NB_SFM );
108 4234 : set_s( USQ_TCQ, 0, NB_SFM );
109 4234 : set_l( positions, 0, L_FRAME32k );
110 :
111 4234 : if ( input_frame <= L_FRAME16k && adjustFlag == 0 && *is_transient == 0 )
112 : {
113 317 : flag_wbnb = 1;
114 317 : lsbtcq_bits = 0;
115 317 : tcq_arbits = 0;
116 : }
117 :
118 4234 : pardec = &ardec;
119 4234 : pbs = &bs;
120 4234 : pbs->curPos = 7;
121 4234 : pbs->numbits = 0;
122 4234 : pbs->numByte = 0;
123 :
124 : /* Bits distribution analysis*/
125 95349 : for ( i = 0; i < BANDS; i++ )
126 : {
127 91115 : if ( L_sub( ar_div( Rk_fx[i], band_width[i] ), 49152 ) >= 0 )
128 : {
129 : /* USQ used for high importance bands*/
130 42646 : USQ_TCQ[i] = 1;
131 : }
132 : else
133 : {
134 : /* TCQ used for usual bands*/
135 48469 : USQ_TCQ[i] = 0;
136 : }
137 91115 : if ( Rk_fx[i] > 0.0f )
138 : {
139 69159 : nzbands++;
140 : }
141 : }
142 :
143 95349 : for ( j = 0; j < BANDS; j++ )
144 : {
145 91115 : if ( Rk_fx[j] > 0.0f )
146 : {
147 69159 : nzb++;
148 : }
149 : }
150 :
151 : #define WMC_TOOL_SKIP
152 4234 : bsub_fx = L_shl( L_add( tcq_arbits, lsbtcq_bits ), 16 );
153 4234 : IF( bsub_fx > 0 )
154 : {
155 3917 : bsub_fx = L_add( bsub_fx, 2048 );
156 : }
157 95349 : for ( j = BANDS - 1; j >= 0; j-- )
158 : {
159 91115 : if ( Rk_fx[j] > 0 )
160 : {
161 69159 : Rk_fx[j] = L_sub( Rk_fx[j], ar_div( bsub_fx, nzb ) );
162 69159 : if ( Rk_fx[j] < 0 )
163 : {
164 105 : bsub_fx = L_sub( bsub_fx, L_add( ar_div( bsub_fx, nzb ), Rk_fx[j] ) );
165 105 : Rk_fx[j] = 0;
166 : }
167 : else
168 : {
169 69054 : bsub_fx = L_sub( bsub_fx, ar_div( bsub_fx, nzb ) );
170 : }
171 69159 : nzb = sub( nzb, 1 );
172 : }
173 : }
174 :
175 4234 : srt_vec_ind_fx( Rk_fx, Rk_sort_fx, k_sort, BANDS );
176 : #undef WMC_TOOL_SKIP
177 :
178 : /*read the bits*/
179 4234 : nb_bytes = bit_budget >> 3;
180 4234 : k = bit_budget - ( nb_bytes << 3 );
181 83834 : for ( i = 0; i < nb_bytes; i++ )
182 : {
183 79600 : pbs->buf[i] = (uint8_t) get_next_indice( st, 8 );
184 : }
185 :
186 4234 : if ( k > 0 )
187 : {
188 3721 : pbs->buf[nb_bytes] = (uint8_t) get_next_indice( st, k );
189 3721 : pbs->buf[nb_bytes] <<= ( 8 - k );
190 3721 : i++;
191 3721 : nb_bytes++;
192 : }
193 : /* set two more bytes, which are used to flush the arithmetic coder, to 0
194 : -> this avoids reading of uninitialized memory */
195 4234 : nb_bytes = min( nb_bytes + 2, MAX_SIZEBUF_PBITSTREAM );
196 12702 : for ( ; i < nb_bytes; i++ )
197 : {
198 8468 : pbs->buf[i] = 0;
199 : }
200 :
201 4234 : pbs->maxBytes = nb_bytes;
202 :
203 4234 : ar_decoder_start( pardec, pbs );
204 :
205 4234 : delta_fx = 0;
206 4234 : surplus_fx = 0;
207 :
208 4234 : if ( input_frame <= L_FRAME16k && adjustFlag == 0 && *is_transient == 0 )
209 : {
210 317 : surplus_fx = -131072;
211 :
212 317 : bit_allocation_second_fx( Rk_fx, Rk_sort_fx, BANDS, band_width, k_sort, k_num, p2a_flags, p2a_bands, last_bitalloc, input_frame );
213 :
214 317 : nzbands = 0;
215 4945 : for ( j = 0; j < BANDS; j++ )
216 : {
217 4628 : if ( sub( j, k_num[0] ) == 0 || sub( j, k_num[1] ) == 0 )
218 : {
219 634 : sepbits = L_add( sepbits, Rk_fx[k_sort[j]] );
220 : }
221 : else
222 : {
223 3994 : leftbits = L_add( leftbits, Rk_fx[k_sort[j]] );
224 3994 : if ( Rk_fx[k_sort[j]] > 0 )
225 : {
226 3242 : nzbands = add( nzbands, 1 );
227 : }
228 : }
229 : }
230 :
231 4945 : for ( k = 0; k < BANDS; k++ )
232 : {
233 4628 : if ( k != k_num[0] && k != k_num[1] )
234 : {
235 3994 : if ( Rk_fx[k_sort[k]] > 0 && USQ_TCQ[k_sort[k]] == 0 )
236 : {
237 : /* When number of bits per band is less than
238 : arithmetic bits overhead, this band is not encoded.
239 : It may happens when the actual number of bits per
240 : band is near same to estimated number of bits, for
241 : most bands (very unprobable but possible) */
242 937 : if ( L_add( Rk_fx[k_sort[k]], delta_fx ) < 0 )
243 : {
244 0 : pulsesnum = 0;
245 0 : for ( i = 0; i < band_width[k_sort[k]]; i++ )
246 : {
247 0 : inp_vector[band_start[k_sort[k]] + i] = 0;
248 : }
249 0 : if ( surplus_fx != 0 )
250 : {
251 : #define WMC_TOOL_SKIP
252 0 : surplus_fx = L_add( Rk_fx[k_sort[k]], surplus_fx );
253 0 : surplus_fx = L_add( delta_fx, surplus_fx );
254 : #undef WMC_TOOL_SKIP
255 : }
256 : }
257 : else
258 : {
259 : /*get number of pulses */
260 937 : pulsesnum = GetScale_fx( band_width[k_sort[k]], L_add( Rk_fx[k_sort[k]], delta_fx ), &surplus_fx );
261 :
262 937 : leftbits = L_sub( leftbits, L_add( Rk_fx[k_sort[k]], delta_fx ) );
263 :
264 937 : decode_position_ari_fx( pardec, band_width[k_sort[k]], pulsesnum, &nz, &positions[band_start[k_sort[k]]] );
265 937 : decode_mangitude_tcq_fx( pardec, band_width[k_sort[k]], pulsesnum, nz, &positions[band_start[k_sort[k]]], &inp_vector[band_start[k_sort[k]]], &surplus_fx );
266 937 : decode_signs_fx( pardec, band_width[k_sort[k]], &inp_vector[band_start[k_sort[k]]] );
267 : }
268 937 : nzbands--;
269 : }
270 3057 : else if ( Rk_fx[k_sort[k]] > 0 && USQ_TCQ[k_sort[k]] == 1 )
271 : {
272 : /* When number of bits per band is less than
273 : arithmetic bits overhead, this band is not encoded.
274 : It may happens when the actual number of bits per
275 : band is near same to estimated number of bits, for
276 : most bands (very unprobable but possible) */
277 2305 : if ( L_add( Rk_fx[k_sort[k]], delta_fx ) < 0 )
278 : {
279 0 : pulsesnum = 0;
280 0 : for ( i = 0; i < band_width[k_sort[k]]; i++ )
281 : {
282 0 : inp_vector[band_start[k_sort[k]] + i] = 0;
283 : }
284 0 : if ( surplus_fx != 0 )
285 : {
286 : #define WMC_TOOL_SKIP
287 0 : surplus_fx = L_add( Rk_fx[k_sort[k]], surplus_fx );
288 0 : surplus_fx = L_add( delta_fx, surplus_fx );
289 : #undef WMC_TOOL_SKIP
290 : }
291 : }
292 : else
293 : {
294 :
295 2305 : pulsesnum = GetScale_fx( band_width[k_sort[k]], L_add( Rk_fx[k_sort[k]], delta_fx ), &surplus_fx );
296 :
297 2305 : leftbits = L_sub( leftbits, L_add( Rk_fx[k_sort[k]], delta_fx ) );
298 :
299 2305 : decode_position_ari_fx( pardec, band_width[k_sort[k]], pulsesnum, &nz, &positions[band_start[k_sort[k]]] );
300 2305 : decode_magnitude_usq_fx( pardec, band_width[k_sort[k]], pulsesnum, nz, &positions[band_start[k_sort[k]]], &inp_vector[band_start[k_sort[k]]] );
301 2305 : decode_signs_fx( pardec, band_width[k_sort[k]], &inp_vector[band_start[k_sort[k]]] );
302 : }
303 2305 : nzbands--;
304 : }
305 : else
306 : {
307 752 : pulsesnum = 0;
308 17231 : for ( i = 0; i < band_width[k_sort[k]]; i++ )
309 : {
310 16479 : inp_vector[band_start[k_sort[k]] + i] = 0;
311 : }
312 : }
313 :
314 3994 : npulses[k_sort[k]] = pulsesnum;
315 :
316 3994 : if ( Rk_fx[k_sort[k]] > 0 && surplus_fx < 0 )
317 : {
318 : #define WMC_TOOL_SKIP
319 258 : IF( nzbands <= 1 )
320 : {
321 6 : divider = 0;
322 : }
323 : ELSE
324 : {
325 252 : divider = 2;
326 : }
327 :
328 258 : IF( L_add( L_add( surplus_fx, sepbits ), ar_div( leftbits, divider ) ) < 0 )
329 : {
330 : /* Overflow possible => start to distribute negative surplus */
331 0 : delta_fx = ar_div( surplus_fx + sepbits, nzbands );
332 : }
333 : else
334 : {
335 258 : delta_fx = 0;
336 : }
337 258 : surplus_fx = L_sub( surplus_fx, delta_fx );
338 : #undef WMC_TOOL_SKIP
339 : }
340 : else
341 : {
342 3736 : delta_fx = 0;
343 : }
344 : }
345 : }
346 :
347 : #define WMC_TOOL_SKIP
348 317 : if ( ( L_sub( surplus_fx, 524288 ) > 0 && sub( input_frame, L_FRAME8k ) == 0 ) || ( L_sub( surplus_fx, 786432 ) > 0 && sub( input_frame, L_FRAME16k ) == 0 ) )
349 : {
350 195 : bit_surplus_fx[0] = Mult_32_16( surplus_fx, 24576 ); /* Q16 */
351 195 : bit_surplus_fx[1] = Mult_32_16( surplus_fx, 8192 ); /* Q16 */
352 : }
353 : #undef WMC_TOOL_SKIP
354 : else
355 : {
356 122 : bit_surplus_fx[0] = surplus_fx;
357 122 : bit_surplus_fx[1] = 0;
358 : }
359 :
360 4945 : for ( k = 0; k < BANDS; k++ )
361 : {
362 13884 : for ( j = 0; j < 2; j++ )
363 : {
364 9256 : if ( k == k_num[j] )
365 : {
366 : #define WMC_TOOL_SKIP
367 634 : Rk_fx[k_sort[k]] = L_add( Rk_fx[k_sort[k]], bit_surplus_fx[j] );
368 : #undef WMC_TOOL_SKIP
369 634 : if ( Rk_fx[k_sort[k]] > 0 && USQ_TCQ[k_sort[k]] == 0 )
370 : {
371 : /* get number of pulses */
372 456 : pulsesnum = GetScale_fx( band_width[k_sort[k]], Rk_fx[k_sort[k]], &surplus_fx );
373 :
374 456 : decode_position_ari_fx( pardec, band_width[k_sort[k]], pulsesnum, &nz, &positions[band_start[k_sort[k]]] );
375 : /* decode tcq magniitude and update the surplus bits. */
376 456 : decode_mangitude_tcq_fx( pardec, band_width[k_sort[k]], pulsesnum, nz, &positions[band_start[k_sort[k]]], &inp_vector[band_start[k_sort[k]]], &surplus_fx );
377 456 : decode_signs_fx( pardec, band_width[k_sort[k]], &inp_vector[band_start[k_sort[k]]] );
378 : }
379 178 : else if ( Rk_fx[k_sort[k]] > 0 && USQ_TCQ[k_sort[k]] == 1 )
380 : {
381 99 : pulsesnum = GetScale_fx( band_width[k_sort[k]], Rk_fx[k_sort[k]], &surplus_fx );
382 :
383 99 : decode_position_ari_fx( pardec, band_width[k_sort[k]], pulsesnum, &nz, &positions[band_start[k_sort[k]]] );
384 : /* decode usq magnitude and don't need to update surplus bits */
385 99 : decode_magnitude_usq_fx( pardec, band_width[k_sort[k]], pulsesnum, nz, &positions[band_start[k_sort[k]]], &inp_vector[band_start[k_sort[k]]] );
386 99 : decode_signs_fx( pardec, band_width[k_sort[k]], &inp_vector[band_start[k_sort[k]]] );
387 : }
388 : else
389 : {
390 79 : pulsesnum = 0;
391 1556 : for ( i = 0; i < band_width[k_sort[k]]; i++ )
392 : {
393 1477 : inp_vector[band_start[k_sort[k]] + i] = 0;
394 : }
395 : }
396 634 : npulses[k_sort[k]] = pulsesnum;
397 : }
398 : }
399 : }
400 : }
401 : else
402 : {
403 90404 : for ( k = 0; k < BANDS; k++ )
404 : {
405 86487 : if ( Rk_fx[k_sort[k]] > 0 )
406 : {
407 65343 : pulsesnum = GetScale_fx( band_width[k_sort[k]], Rk_fx[k_sort[k]] + delta_fx, &surplus_fx );
408 :
409 65343 : decode_position_ari_fx( pardec, band_width[k_sort[k]], pulsesnum, &nz, &positions[band_start[k_sort[k]]] );
410 :
411 : /*decode usq magnitude and don't need to update surplus bits*/
412 65343 : decode_magnitude_usq_fx( pardec, band_width[k_sort[k]], pulsesnum, nz, &positions[band_start[k_sort[k]]], &inp_vector[band_start[k_sort[k]]] );
413 65343 : decode_signs_fx( pardec, band_width[k_sort[k]], &inp_vector[band_start[k_sort[k]]] );
414 :
415 : #define WMC_TOOL_SKIP
416 65343 : nzbands = sub( nzbands, 1 );
417 : #undef WMC_TOOL_SKIP
418 : }
419 : else
420 : {
421 21144 : pulsesnum = 0;
422 1378812 : for ( i = 0; i < band_width[k_sort[k]]; i++ )
423 : {
424 1357668 : inp_vector[band_start[k_sort[k]] + i] = 0;
425 : }
426 : }
427 :
428 86487 : npulses[k_sort[k]] = pulsesnum;
429 :
430 : /* surplus distribution */
431 86487 : if ( surplus_fx > 0 && nzbands > 0 )
432 : {
433 61481 : delta_fx = ar_div( surplus_fx, nzbands );
434 61481 : surplus_fx = L_sub( surplus_fx, delta_fx );
435 : }
436 : }
437 : }
438 : /* Load TCQ path from bitstream */
439 4234 : LoadTCQdata( pardec, dpath, lsbtcq_bits );
440 :
441 4234 : TCQLSBdec( dpath, mbuffer, 2 * lsbtcq_bits );
442 :
443 4234 : ar_decoder_done( pardec );
444 :
445 : /* Restore TCQ */
446 4234 : if ( !flag_wbnb )
447 : {
448 90404 : for ( k = 0; k < BANDS; k++ )
449 : {
450 86487 : if ( Rk_fx[k_sort[k]] > 0 )
451 : {
452 65343 : RestoreTCQdec( &inp_vector[band_start[k_sort[k]]], band_width[k_sort[k]], &bcount, mbuffer );
453 : }
454 : }
455 : }
456 : else
457 : {
458 4945 : for ( k = 0; k < BANDS; k++ )
459 : {
460 4628 : if ( Rk_fx[k_sort[k]] > 0 && k != k_num[0] && k != k_num[1] )
461 : {
462 3242 : RestoreTCQdec( &inp_vector[band_start[k_sort[k]]], band_width[k_sort[k]], &bcount, mbuffer );
463 : }
464 : }
465 4945 : for ( k = 0; k < BANDS; k++ )
466 : {
467 4628 : if ( Rk_fx[k_sort[k]] > 0 && ( k == k_num[0] || k == k_num[1] ) )
468 : {
469 555 : RestoreTCQdec( &inp_vector[band_start[k_sort[k]]], band_width[k_sort[k]], &bcount, mbuffer );
470 : }
471 : }
472 : }
473 :
474 :
475 4234 : return;
476 : }
|