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 5172800 : 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 5172800 : Np = get_pvq_splits( st, hPVQ, band_bits, sfmsize, &split_bit );
88 5172800 : band_bits_tot = band_bits - split_bit;
89 :
90 5172800 : dim_parts = (int16_t) intLimCDivPos( sfmsize, Np );
91 5172800 : set_s( dim_part, dim_parts, Np - 1 );
92 5172800 : dim_part[Np - 1] = sfmsize - dim_parts * ( Np - 1 );
93 :
94 5172800 : part_start[0] = 0;
95 5593735 : for ( j = 1; j < Np; j++ )
96 : {
97 420935 : part_start[j] = part_start[j - 1] + dim_part[j - 1];
98 : }
99 :
100 5172800 : set_s( g_part_s, -32768, Np );
101 :
102 5172800 : if ( Np > 1 )
103 : {
104 376704 : 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 4796096 : bits_part[0] = band_bits_tot;
109 : }
110 :
111 5172800 : pool_tot = 0;
112 5172800 : pool_part = 0;
113 :
114 10766535 : for ( j = 0; j < Np; j++ )
115 : {
116 5593735 : 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 5593735 : g_part_s[j] = negate( g_part_s[j] );
122 : }
123 :
124 :
125 5172800 : srt_vec_ind( g_part_s, sg_part, idx_sort, Np );
126 :
127 :
128 10766535 : for ( j = 0; j < Np; j++ )
129 : {
130 5593735 : js = idx_sort[Np - 1 - j];
131 5593735 : pool_part = shrtCDivSignedApprox( pool_tot, Np - j );
132 5593735 : bits_part[js] = max( 0, min( bits_part[js] + pool_part, 256 ) );
133 :
134 5593735 : 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 5593735 : if ( K_val >= 1 )
137 : {
138 4609688 : 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 984047 : set_f( coefs_quant + part_start[js], 0.0f, dim_part[js] );
143 984047 : set_s( pulse_vector + part_start[js], 0, dim_part[js] );
144 : }
145 : }
146 :
147 5172800 : return;
148 : }
149 :
150 : /*-------------------------------------------------------------------*
151 : * pvq_decode_frame()
152 : *
153 : *-------------------------------------------------------------------*/
154 :
155 616299 : 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 616299 : 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 616299 : PVQ_DEC_HANDLE hPVQ = &pvq_dec;
180 :
181 616299 : rc_dec_init( st, hPVQ, pvq_bits );
182 616299 : curr_bits = ( pvq_bits - RC_BITS_RESERVED ) << 3;
183 616299 : bands_to_code = 0;
184 8252072 : for ( i = 0; i < nb_sfm; i++ )
185 : {
186 7635773 : if ( R[i] > 0 )
187 : {
188 5172800 : bands_to_code++;
189 : }
190 : }
191 :
192 616299 : if ( core == ACELP_CORE )
193 : {
194 491030 : strict_bits = PVQ_CONS;
195 491030 : srt_vec_ind( R, R_sort, i_sort, nb_sfm );
196 : }
197 : else
198 : {
199 125269 : strict_bits = PVQ_NEAREST;
200 4093125 : for ( i = 0; i < nb_sfm; i++ )
201 : {
202 3967856 : i_sort[i] = i;
203 : }
204 : }
205 :
206 616299 : coded_bands = 0;
207 8252072 : for ( i = 0; i < nb_sfm; i++ )
208 : {
209 7635773 : is = i_sort[i];
210 7635773 : if ( R[is] > 0 )
211 : {
212 5172800 : 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 5172800 : 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 5172800 : coded_bands++;
219 : }
220 : else
221 : {
222 54778209 : for ( j = sfm_start[is]; j < sfm_end[is]; j++ )
223 : {
224 52315236 : coefs_quant[j] = 0.0f;
225 52315236 : pulse_vector[j] = 0;
226 : }
227 : }
228 : }
229 :
230 616299 : rc_dec_finish( st, hPVQ );
231 :
232 616299 : return;
233 : }
234 :
235 : /*-------------------------------------------------------------------*
236 : * pvq_core_dec()
237 : *
238 : *-------------------------------------------------------------------*/
239 :
240 : /*! r: number of bits decoded */
241 597203 : 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 597203 : if ( st->hHQ_core != NULL )
265 : {
266 590982 : st->hHQ_core->ber_occured_in_pvq = 0;
267 : }
268 :
269 597203 : R_upd = bits_tot * 8;
270 597203 : assign_gain_bits( core, nb_sfm, sfmsize, R, gain_bits_array, &R_upd );
271 :
272 597203 : pvq_bits = R_upd >> 3;
273 :
274 597203 : pvq_decode_frame( st, coefs_quant, npulses, pulse_vector, sfm_start, sfm_end, sfmsize, nb_sfm, R, pvq_bits, core );
275 :
276 597203 : if ( Rs != NULL )
277 : {
278 4045429 : for ( i = 0; i < nb_sfm; i++ )
279 : {
280 3939256 : Rs[i] = Rs[i] * ( npulses[i] > 0 ); /* Update Rs in case no pulses were assigned */
281 : }
282 : }
283 :
284 8204376 : for ( i = 0; i < nb_sfm; i++ )
285 : {
286 7607173 : ord[i] = i;
287 7607173 : R[i] = R[i] * ( npulses[i] > 0 ); /* Update in case no pulses were assigned */
288 : }
289 :
290 597203 : get_max_pulses( sfm_start, sfm_end, ord, npulses, nb_sfm, pulse_vector, maxpulse );
291 :
292 597203 : fine_gain_pred( sfm_start, sfm_end, sfmsize, ord, npulses, maxpulse, R, nb_sfm, coefs_quant, pulse_vector, fg_pred, core );
293 :
294 597203 : fine_gain_dec( st, ord, nb_sfm, gain_bits_array, fg_pred );
295 :
296 597203 : if ( st->hHQ_core != NULL )
297 : {
298 590982 : 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 597203 : apply_gain( ord, sfm_start, sfm_end, nb_sfm, fg_pred, coefs_quant );
305 :
306 597203 : return (int16_t) bits_tot;
307 : }
308 :
309 :
310 : /*-------------------------------------------------------------------*
311 : * decode_energies()
312 : *
313 : *-------------------------------------------------------------------*/
314 :
315 420935 : 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 420935 : int16_t index_phi = -1;
333 420935 : l_Np = Np >> 1;
334 420935 : r_Np = Np - l_Np;
335 :
336 420935 : l_bits = 0;
337 420935 : l_dim = 0;
338 846390 : for ( i = 0; i < l_Np; i++ )
339 : {
340 425455 : l_dim += dim_part[i];
341 : }
342 420935 : r_dim = dim - l_dim;
343 :
344 420935 : obtainEnergyQuantizerDensity( dim, qband, &res );
345 420935 : rangeCoderFinalizationFBits( hPVQ->rc_num_bits, hPVQ->rc_range, &qzero );
346 420935 : densitySymbolIndexDecode( st, hPVQ, res, r_dim, l_dim, &index_phi );
347 420935 : densityAngle2RmsProjDec( res, index_phi, &ir, &il, &oppRQ3 );
348 846390 : for ( i = 0; i < l_Np; i++ )
349 : {
350 425455 : g_part[i] = ( (int16_t) g_part[i] * il + 16384 ) >> 15;
351 : }
352 :
353 882772 : for ( i = l_Np; i < Np; i++ )
354 : {
355 461837 : g_part[i] = ( (int16_t) g_part[i] * ir + 16384 ) >> 15;
356 : }
357 :
358 :
359 420935 : 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 420935 : if ( l_Np > 1 )
361 : {
362 4351 : 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 416584 : bits_part[0] = l_bits;
367 : }
368 :
369 420935 : if ( r_Np > 1 )
370 : {
371 39880 : 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 381055 : bits_part[1] = r_bits;
376 : }
377 :
378 420935 : return;
379 : }
380 :
381 : /*-------------------------------------------------------------------*
382 : * densitySymbolIndexDecode()
383 : *
384 : *-------------------------------------------------------------------*/
385 :
386 420935 : 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 420935 : int32_t cum_freq = 0, dec_freq, tot;
398 420935 : uint32_t sym_freq = 1;
399 : int16_t angle, c;
400 : int16_t res1, res2, res_c, res_alpha;
401 420935 : int16_t res = density;
402 420935 : int16_t r_dim = opp_sz;
403 420935 : int16_t l_dim = near_sz;
404 : int16_t alpha;
405 : #ifdef BASOP_NOGLOB
406 : Flag Overflow;
407 : #endif /* BASOP_NOGLOB */
408 :
409 420935 : if ( ( 0xFFFE & density ) == 0 )
410 : { /* odd density exit */
411 1375 : *index_phi = -1;
412 1375 : return;
413 : }
414 : #define WMC_TOOL_SKIP
415 419560 : 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 419560 : angle = shl_o( angle, 1, &Overflow );
420 : #endif /* BASOP_NOGLOB */
421 419560 : angle = mult_r( angle, 20861 );
422 419560 : c = mult_r( res, angle );
423 : #undef WMC_TOOL_SKIP
424 :
425 419560 : res_c = res - c;
426 :
427 419560 : 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 419560 : 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 419560 : tot = res * c * ( res - c ) + res + 1;
446 419560 : dec_freq = rc_decode( &st->BER_detect, hPVQ, tot );
447 419560 : if ( dec_freq < tot - ( res + 1 ) - ( res - ( c + 1 ) ) * ( res - c ) * c + c + 1 )
448 : {
449 237750 : 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 237750 : sym_freq = 2 * alpha * res_c + 1;
452 237750 : cum_freq = alpha * ( ( alpha - 1 ) * res_c + 1 );
453 : }
454 : else
455 : {
456 181810 : res1 = res + 1;
457 181810 : res2 = 2 * res + 1;
458 181810 : tmp1 = ( c * res2 + 1 ) * ( c * res2 + 1 ) + 4 * c * ( ( tot - dec_freq - res1 ) - c * ( res * res1 ) );
459 181810 : tmp2 = (int16_t) floor_sqrt_exact( (uint32_t) tmp1 );
460 181810 : if ( tmp2 * tmp2 != tmp1 )
461 : {
462 176682 : tmp2++; /* convert to ceil */
463 : }
464 :
465 181810 : alpha = ( c * ( 2 * res + 1 ) + 1 - tmp2 ) / ( 2 * c );
466 :
467 181810 : res_alpha = res - alpha;
468 181810 : sym_freq = 2 * res_alpha * c + 1;
469 181810 : cum_freq = tot - ( res + 1 ) - res_alpha * ( res_alpha + 1 ) * c + alpha;
470 : }
471 : }
472 419560 : rc_dec_update( st, hPVQ, cum_freq, sym_freq );
473 :
474 419560 : *index_phi = alpha;
475 :
476 419560 : return;
477 : }
478 :
479 :
480 : /*--------------------------------------------------------------------------*
481 : * get_pvq_splits()
482 : *
483 : * Retrieve the number of segments
484 : *--------------------------------------------------------------------------*/
485 :
486 : /*! r: Number of segments */
487 5172800 : 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 5172800 : Np = (int16_t) ( intLimCDivPos( band_bits, 67 ) >> 2 );
499 5172800 : if ( band_bits - 268 * Np != 0 || Np == 0 ) /* L_msu */
500 : {
501 5167640 : Np++; /* ceil */
502 : }
503 5172800 : *bits = 0;
504 5172800 : if ( Np < MAX_SPLITS && ( band_bits - ( 8 * sfmsize * THR_ADD_SPLIT ) > 0 ) )
505 : {
506 2250 : flag = rc_dec_bits( st, hPVQ, 1 );
507 2250 : *bits = 8;
508 2250 : if ( flag )
509 : {
510 392 : Np += 1;
511 : }
512 : }
513 :
514 5172800 : Np = max( Np, (int16_t) ( ceil( (float) sfmsize / PVQ_MAX_BAND_SIZE ) ) );
515 5172800 : Np = min( MAX_SPLITS, Np );
516 5172800 : Np = min( (int16_t) floor( (float) sfmsize / MIN_BAND_SIZE ), Np );
517 :
518 5172800 : return Np;
519 : }
|