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 <math.h>
43 : #include "cnst.h"
44 : #include "rom_com.h"
45 : #include "prot.h"
46 : #include "stl.h"
47 : #include "wmc_auto.h"
48 :
49 : /*-------------------------------------------------------------------*
50 : * Local prototypes
51 : *
52 : *-------------------------------------------------------------------*/
53 :
54 : static int16_t get_pvq_splits( Decoder_State *st, PVQ_DEC_HANDLE hPVQ, const int16_t band_bits, const int16_t sfmsize, int16_t *bits );
55 :
56 : static void densitySymbolIndexDecode( Decoder_State *st, PVQ_DEC_HANDLE hPVQ, int16_t density, int16_t opp_sz, int16_t near_sz, int16_t *index_phi );
57 :
58 : static void decode_energies( Decoder_State *st, PVQ_DEC_HANDLE hPVQ, int16_t Np, int16_t *dim_part, int16_t *bits_part, int16_t *g_part, int16_t qband, int16_t *bits_left, int16_t n, const int16_t strict_bits );
59 :
60 : /*-------------------------------------------------------------------*
61 : * pvq_decode_band()
62 : *
63 : *-------------------------------------------------------------------*/
64 :
65 880647 : static void pvq_decode_band(
66 : Decoder_State *st, /* i/o: Decoder state */
67 : PVQ_DEC_HANDLE hPVQ, /* i/o: PVQ decoder handle */
68 : int16_t *pulse_vector, /* i/o: decoded integer shape vector */
69 : int16_t *npulses, /* i/o: number of pulses */
70 : float *coefs_quant, /* i/o: decoded coefficients buffer */
71 : const int16_t sfmsize, /* i : band length */
72 : const int16_t band_bits, /* i : assigned bit budget */
73 : int16_t *bits_left, /* i/o: number of bits / bits remaining */
74 : const int16_t strict_bits /* i : Conservative rounding flag */
75 : )
76 : {
77 : int16_t K_val;
78 : int16_t j, Np;
79 : int16_t part_start[MAX_SPLITS + 1], dim_part[MAX_SPLITS + 1], bits_part[MAX_SPLITS + 1];
80 : int16_t pool_tot, pool_part, dim_parts;
81 : float g_part[MAX_SPLITS];
82 : int16_t g_part_s[MAX_SPLITS];
83 : int16_t sg_part[MAX_SPLITS + 1];
84 : int16_t idx_sort[MAX_SPLITS + 1];
85 : int16_t js, band_bits_tot, split_bit;
86 :
87 880647 : Np = get_pvq_splits( st, hPVQ, band_bits, sfmsize, &split_bit );
88 880647 : band_bits_tot = band_bits - split_bit;
89 :
90 880647 : dim_parts = (int16_t) intLimCDivPos( sfmsize, Np );
91 880647 : set_s( dim_part, dim_parts, Np - 1 );
92 880647 : dim_part[Np - 1] = sfmsize - dim_parts * ( Np - 1 );
93 :
94 880647 : part_start[0] = 0;
95 975756 : for ( j = 1; j < Np; j++ )
96 : {
97 95109 : part_start[j] = part_start[j - 1] + dim_part[j - 1];
98 : }
99 :
100 880647 : set_s( g_part_s, -32768, Np );
101 :
102 880647 : if ( Np > 1 )
103 : {
104 85056 : decode_energies( st, hPVQ, Np, dim_part, bits_part, g_part_s, band_bits_tot, bits_left, sfmsize, strict_bits );
105 : }
106 : else
107 : {
108 795591 : bits_part[0] = band_bits_tot;
109 : }
110 :
111 880647 : pool_tot = 0;
112 880647 : pool_part = 0;
113 :
114 1856403 : for ( j = 0; j < Np; j++ )
115 : {
116 975756 : g_part[j] = -( (float) g_part_s[j] ) / 32768;
117 : /* note: here g_part needs to be become exactly 1.0(float) thus in BASOP Word16 g_part_s is in the negative Q15 domain */
118 :
119 :
120 : /* aligned to BASOP to avoid USAN undefined negation warning for -(-32768) */
121 975756 : g_part_s[j] = negate( g_part_s[j] );
122 : }
123 :
124 :
125 880647 : srt_vec_ind( g_part_s, sg_part, idx_sort, Np );
126 :
127 :
128 1856403 : for ( j = 0; j < Np; j++ )
129 : {
130 975756 : js = idx_sort[Np - 1 - j];
131 975756 : pool_part = shrtCDivSignedApprox( pool_tot, Np - j );
132 975756 : bits_part[js] = max( 0, min( bits_part[js] + pool_part, 256 ) );
133 :
134 975756 : conservativeL1Norm( dim_part[js], bits_part[js], strict_bits, *bits_left, pool_tot, *npulses, /* inputs */
135 : &K_val, bits_left, &pool_tot, npulses ); /* outputs */
136 975756 : if ( K_val >= 1 )
137 : {
138 885486 : pvq_decode( st, hPVQ, coefs_quant + part_start[js], pulse_vector + part_start[js], K_val, dim_part[js], g_part[js] );
139 : }
140 : else
141 : {
142 90270 : set_f( coefs_quant + part_start[js], 0.0f, dim_part[js] );
143 90270 : set_s( pulse_vector + part_start[js], 0, dim_part[js] );
144 : }
145 : }
146 :
147 880647 : return;
148 : }
149 :
150 : /*-------------------------------------------------------------------*
151 : * pvq_decode_frame()
152 : *
153 : *-------------------------------------------------------------------*/
154 :
155 92424 : void pvq_decode_frame(
156 : Decoder_State *st, /* i/o: Decoder state */
157 : float *coefs_quant, /* o : quantized coefficients */
158 : int16_t *npulses, /* o : number of pulses per band */
159 : int16_t *pulse_vector, /* o : non-normalized pulse shapes */
160 : const int16_t *sfm_start, /* i : indices of first coeffs in the bands */
161 : const int16_t *sfm_end, /* i : indices of last coeffs in the bands */
162 : const int16_t *sfmsize, /* i : band sizes */
163 : const int16_t nb_sfm, /* i : total number of bands */
164 : const int16_t *R, /* i : bitallocation per band (Q3) */
165 : const int16_t pvq_bits, /* i : number of bits avaiable */
166 : const int16_t core /* i : core */
167 : )
168 : {
169 : int16_t i, j;
170 : int16_t band_bits, bits_left;
171 92424 : int16_t bit_pool = 0;
172 : int16_t coded_bands, bands_to_code;
173 : int16_t curr_bits;
174 : int16_t R_sort[NB_SFM]; /*Q3*/
175 : int16_t is, i_sort[NB_SFM];
176 : int16_t strict_bits;
177 :
178 : PVQ_DEC_DATA pvq_dec;
179 92424 : PVQ_DEC_HANDLE hPVQ = &pvq_dec;
180 :
181 92424 : rc_dec_init( st, hPVQ, pvq_bits );
182 92424 : curr_bits = ( pvq_bits - RC_BITS_RESERVED ) << 3;
183 92424 : bands_to_code = 0;
184 1423926 : for ( i = 0; i < nb_sfm; i++ )
185 : {
186 1331502 : if ( R[i] > 0 )
187 : {
188 880647 : bands_to_code++;
189 : }
190 : }
191 :
192 92424 : if ( core == ACELP_CORE )
193 : {
194 67719 : strict_bits = PVQ_CONS;
195 67719 : srt_vec_ind( R, R_sort, i_sort, nb_sfm );
196 : }
197 : else
198 : {
199 24705 : strict_bits = PVQ_NEAREST;
200 869883 : for ( i = 0; i < nb_sfm; i++ )
201 : {
202 845178 : i_sort[i] = i;
203 : }
204 : }
205 :
206 92424 : coded_bands = 0;
207 1423926 : for ( i = 0; i < nb_sfm; i++ )
208 : {
209 1331502 : is = i_sort[i];
210 1331502 : if ( R[is] > 0 )
211 : {
212 880647 : bandBitsAdjustment( hPVQ->rc_num_bits, hPVQ->rc_range, curr_bits, bands_to_code, bands_to_code - coded_bands, sfmsize[is], R[is], bit_pool, /* inputs */
213 : &band_bits, &bits_left, &bit_pool ); /* outputs */
214 :
215 880647 : pvq_decode_band( st, hPVQ, &pulse_vector[sfm_start[is]], &npulses[is], &coefs_quant[sfm_start[is]], sfmsize[is], band_bits, &bits_left, strict_bits );
216 :
217 : /* Updates */
218 880647 : coded_bands++;
219 : }
220 : else
221 : {
222 10838895 : for ( j = sfm_start[is]; j < sfm_end[is]; j++ )
223 : {
224 10388040 : coefs_quant[j] = 0.0f;
225 10388040 : pulse_vector[j] = 0;
226 : }
227 : }
228 : }
229 :
230 92424 : rc_dec_finish( st, hPVQ );
231 :
232 92424 : return;
233 : }
234 :
235 : /*-------------------------------------------------------------------*
236 : * pvq_core_dec()
237 : *
238 : *-------------------------------------------------------------------*/
239 :
240 : /*! r: number of bits decoded */
241 88557 : int16_t pvq_core_dec(
242 : Decoder_State *st, /* i/o: Decoder state */
243 : const int16_t *sfm_start, /* i : indices of first coeffs in the bands */
244 : const int16_t *sfm_end, /* i : indices of last coeffs in the bands */
245 : const int16_t *sfmsize, /* i : band sizes */
246 : float coefs_quant[], /* o : output MDCT */
247 : const int16_t bits_tot, /* i : bit budget */
248 : const int16_t nb_sfm, /* i : number of bands */
249 : int16_t *R, /* i/o: Bit allocation/Adjusted bit alloc.(Q3) */
250 : int16_t *Rs, /* i/o: Integer bit allocation */
251 : int16_t *npulses, /* o : number of pulses per band */
252 : int16_t *maxpulse, /* o : maximum pulse per band */
253 : const int16_t core /* i : core */
254 : )
255 : {
256 : int16_t i;
257 : int16_t R_upd;
258 : int16_t ord[NB_SFM_MAX];
259 : int16_t pulse_vector[L_SPEC48k_EXT];
260 : int16_t pvq_bits;
261 : int16_t gain_bits_array[NB_SFM];
262 : float fg_pred[NB_SFM_MAX];
263 :
264 88557 : if ( st->hHQ_core != NULL )
265 : {
266 88476 : st->hHQ_core->ber_occured_in_pvq = 0;
267 : }
268 :
269 88557 : R_upd = bits_tot * 8;
270 88557 : assign_gain_bits( core, nb_sfm, sfmsize, R, gain_bits_array, &R_upd );
271 :
272 88557 : pvq_bits = R_upd >> 3;
273 :
274 88557 : pvq_decode_frame( st, coefs_quant, npulses, pulse_vector, sfm_start, sfm_end, sfmsize, nb_sfm, R, pvq_bits, core );
275 :
276 88557 : if ( Rs != NULL )
277 : {
278 858429 : for ( i = 0; i < nb_sfm; i++ )
279 : {
280 837591 : Rs[i] = Rs[i] * ( npulses[i] > 0 ); /* Update Rs in case no pulses were assigned */
281 : }
282 : }
283 :
284 1412472 : for ( i = 0; i < nb_sfm; i++ )
285 : {
286 1323915 : ord[i] = i;
287 1323915 : R[i] = R[i] * ( npulses[i] > 0 ); /* Update in case no pulses were assigned */
288 : }
289 :
290 88557 : get_max_pulses( sfm_start, sfm_end, ord, npulses, nb_sfm, pulse_vector, maxpulse );
291 :
292 88557 : fine_gain_pred( sfm_start, sfm_end, sfmsize, ord, npulses, maxpulse, R, nb_sfm, coefs_quant, pulse_vector, fg_pred, core );
293 :
294 88557 : fine_gain_dec( st, ord, nb_sfm, gain_bits_array, fg_pred );
295 :
296 88557 : if ( st->hHQ_core != NULL )
297 : {
298 88476 : if ( st->hHQ_core->ber_occured_in_pvq != 0 )
299 : {
300 0 : set_f( fg_pred, ( 1.0f / 8192.0f ), nb_sfm ); /* low complex ECU action in case of detetected BER in PVQ decoding */
301 : }
302 : }
303 :
304 88557 : apply_gain( ord, sfm_start, sfm_end, nb_sfm, fg_pred, coefs_quant );
305 :
306 88557 : return (int16_t) bits_tot;
307 : }
308 :
309 :
310 : /*-------------------------------------------------------------------*
311 : * decode_energies()
312 : *
313 : *-------------------------------------------------------------------*/
314 :
315 95109 : static void decode_energies(
316 : Decoder_State *st,
317 : PVQ_DEC_HANDLE hPVQ,
318 : int16_t Np,
319 : int16_t *dim_part,
320 : int16_t *bits_part,
321 : int16_t *g_part,
322 : int16_t qband,
323 : int16_t *bits_left,
324 : int16_t dim,
325 : const int16_t strict_bits )
326 : {
327 : int16_t res;
328 : int16_t i, l_Np, r_Np;
329 : int16_t l_bits, r_bits, l_dim, r_dim;
330 : int16_t il, ir;
331 : int16_t oppRQ3, qzero;
332 95109 : int16_t index_phi = -1;
333 95109 : l_Np = Np >> 1;
334 95109 : r_Np = Np - l_Np;
335 :
336 95109 : l_bits = 0;
337 95109 : l_dim = 0;
338 190977 : for ( i = 0; i < l_Np; i++ )
339 : {
340 95868 : l_dim += dim_part[i];
341 : }
342 95109 : r_dim = dim - l_dim;
343 :
344 95109 : obtainEnergyQuantizerDensity( dim, qband, &res );
345 95109 : rangeCoderFinalizationFBits( hPVQ->rc_num_bits, hPVQ->rc_range, &qzero );
346 95109 : densitySymbolIndexDecode( st, hPVQ, res, r_dim, l_dim, &index_phi );
347 95109 : densityAngle2RmsProjDec( res, index_phi, &ir, &il, &oppRQ3 );
348 190977 : for ( i = 0; i < l_Np; i++ )
349 : {
350 95868 : g_part[i] = ( (int16_t) g_part[i] * il + 16384 ) >> 15;
351 : }
352 :
353 199695 : for ( i = l_Np; i < Np; i++ )
354 : {
355 104586 : g_part[i] = ( (int16_t) g_part[i] * ir + 16384 ) >> 15;
356 : }
357 :
358 :
359 95109 : NearOppSplitAdjustment( qband, qzero, hPVQ->rc_num_bits, hPVQ->rc_range, *bits_left, strict_bits, Np, dim_part[0], dim_part[Np - 1], l_dim, r_dim, oppRQ3, &l_bits, &r_bits, bits_left );
360 95109 : if ( l_Np > 1 )
361 : {
362 738 : decode_energies( st, hPVQ, l_Np, dim_part, bits_part, g_part, l_bits, bits_left, l_dim, strict_bits );
363 : }
364 : else
365 : {
366 94371 : bits_part[0] = l_bits;
367 : }
368 :
369 95109 : if ( r_Np > 1 )
370 : {
371 9315 : decode_energies( st, hPVQ, r_Np, &dim_part[l_Np], &bits_part[l_Np], &g_part[l_Np], r_bits, bits_left, r_dim, strict_bits );
372 : }
373 : else
374 : {
375 85794 : bits_part[1] = r_bits;
376 : }
377 :
378 95109 : return;
379 : }
380 :
381 : /*-------------------------------------------------------------------*
382 : * densitySymbolIndexDecode()
383 : *
384 : *-------------------------------------------------------------------*/
385 :
386 95109 : static void densitySymbolIndexDecode(
387 : Decoder_State *st,
388 : PVQ_DEC_HANDLE hPVQ,
389 : int16_t density,
390 : int16_t opp_sz,
391 : int16_t near_sz,
392 : int16_t *index_phi )
393 : {
394 :
395 : int32_t tmp1;
396 : int16_t tmp2;
397 95109 : int32_t cum_freq = 0, dec_freq, tot;
398 95109 : uint32_t sym_freq = 1;
399 : int16_t angle, c;
400 : int16_t res1, res2, res_c, res_alpha;
401 95109 : int16_t res = density;
402 95109 : int16_t r_dim = opp_sz;
403 95109 : int16_t l_dim = near_sz;
404 : int16_t alpha;
405 : #ifdef BASOP_NOGLOB
406 : Flag Overflow;
407 : #endif /* BASOP_NOGLOB */
408 :
409 95109 : if ( ( 0xFFFE & density ) == 0 )
410 : { /* odd density exit */
411 531 : *index_phi = -1;
412 531 : return;
413 : }
414 : #define WMC_TOOL_SKIP
415 94578 : angle = atan2_fx( SQRT_DIM_fx[r_dim], SQRT_DIM_fx[l_dim] );
416 : #ifndef BASOP_NOGLOB
417 : angle = shl( angle, 1 );
418 : #else /* BASOP_NOGLOB */
419 94578 : angle = shl_o( angle, 1, &Overflow );
420 : #endif /* BASOP_NOGLOB */
421 94578 : angle = mult_r( angle, 20861 );
422 94578 : c = mult_r( res, angle );
423 : #undef WMC_TOOL_SKIP
424 :
425 94578 : res_c = res - c;
426 :
427 94578 : if ( c == 0 )
428 : {
429 0 : tot = res * ( res + 1 ) + 1;
430 0 : dec_freq = rc_decode( &st->BER_detect, hPVQ, tot );
431 0 : alpha = (int16_t) floor_sqrt_exact( (uint32_t) ( ( res + 1 ) * ( res + 1 ) - dec_freq ) ) + res + 1;
432 0 : sym_freq = 2 * ( res - alpha ) + 1;
433 0 : cum_freq = alpha * ( 2 * ( res + 1 ) - alpha );
434 : }
435 94578 : else if ( c == res )
436 : {
437 0 : tot = res * ( res + 1 ) + 1;
438 0 : dec_freq = rc_decode( &st->BER_detect, hPVQ, tot );
439 0 : alpha = (int16_t) floor_sqrt_exact( (uint32_t) dec_freq );
440 0 : sym_freq = 2 * alpha + 1;
441 0 : cum_freq = alpha * alpha;
442 : }
443 : else
444 : {
445 94578 : tot = res * c * ( res - c ) + res + 1;
446 94578 : dec_freq = rc_decode( &st->BER_detect, hPVQ, tot );
447 94578 : if ( dec_freq < tot - ( res + 1 ) - ( res - ( c + 1 ) ) * ( res - c ) * c + c + 1 )
448 : {
449 53952 : alpha = ( res_c - 1 + (int16_t) floor_sqrt_exact( (uint32_t) ( res_c * ( res_c + 4 * dec_freq - 2 ) + 1 ) ) ) / ( 2 * res_c );
450 :
451 53952 : sym_freq = 2 * alpha * res_c + 1;
452 53952 : cum_freq = alpha * ( ( alpha - 1 ) * res_c + 1 );
453 : }
454 : else
455 : {
456 40626 : res1 = res + 1;
457 40626 : res2 = 2 * res + 1;
458 40626 : tmp1 = ( c * res2 + 1 ) * ( c * res2 + 1 ) + 4 * c * ( ( tot - dec_freq - res1 ) - c * ( res * res1 ) );
459 40626 : tmp2 = (int16_t) floor_sqrt_exact( (uint32_t) tmp1 );
460 40626 : if ( tmp2 * tmp2 != tmp1 )
461 : {
462 39081 : tmp2++; /* convert to ceil */
463 : }
464 :
465 40626 : alpha = ( c * ( 2 * res + 1 ) + 1 - tmp2 ) / ( 2 * c );
466 :
467 40626 : res_alpha = res - alpha;
468 40626 : sym_freq = 2 * res_alpha * c + 1;
469 40626 : cum_freq = tot - ( res + 1 ) - res_alpha * ( res_alpha + 1 ) * c + alpha;
470 : }
471 : }
472 94578 : rc_dec_update( st, hPVQ, cum_freq, sym_freq );
473 :
474 94578 : *index_phi = alpha;
475 :
476 94578 : return;
477 : }
478 :
479 :
480 : /*--------------------------------------------------------------------------*
481 : * get_pvq_splits()
482 : *
483 : * Retrieve the number of segments
484 : *--------------------------------------------------------------------------*/
485 :
486 : /*! r: Number of segments */
487 880647 : static int16_t get_pvq_splits(
488 : Decoder_State *st, /* i/o: Decoder state */
489 : PVQ_DEC_HANDLE hPVQ, /* i/o: PVQ decoder handle */
490 : const int16_t band_bits, /* i : Band bit-budget */
491 : const int16_t sfmsize, /* i : Band width */
492 : int16_t *bits /* o : Used bits */
493 : )
494 : {
495 : int16_t Np;
496 : uint32_t flag;
497 :
498 880647 : Np = (int16_t) ( intLimCDivPos( band_bits, 67 ) >> 2 );
499 880647 : if ( band_bits - 268 * Np != 0 || Np == 0 ) /* L_msu */
500 : {
501 879912 : Np++; /* ceil */
502 : }
503 880647 : *bits = 0;
504 880647 : if ( Np < MAX_SPLITS && ( band_bits - ( 8 * sfmsize * THR_ADD_SPLIT ) > 0 ) )
505 : {
506 219 : flag = rc_dec_bits( st, hPVQ, 1 );
507 219 : *bits = 8;
508 219 : if ( flag )
509 : {
510 48 : Np += 1;
511 : }
512 : }
513 :
514 880647 : Np = max( Np, (int16_t) ( ceil( (float) sfmsize / PVQ_MAX_BAND_SIZE ) ) );
515 880647 : Np = min( MAX_SPLITS, Np );
516 880647 : Np = min( (int16_t) floor( (float) sfmsize / MIN_BAND_SIZE ), Np );
517 :
518 880647 : return Np;
519 : }
|