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 : #include "cnst.h"
40 : #include "rom_com.h"
41 : #include "prot.h"
42 : #include "wmc_auto.h"
43 : #include "ivas_prot.h"
44 :
45 : /*-----------------------------------------------------------------*
46 : * Local function prototypes
47 : *-----------------------------------------------------------------*/
48 :
49 : static void decode_comb( Word32 index, float *cv, int16_t idx_lead );
50 :
51 : static void decode_sign_pc1( float *c, int16_t idx_sign, const int16_t parity );
52 :
53 : static void put_value( float *cv, int16_t *p, float val, const int16_t dim, const int16_t no_new_val );
54 :
55 : static void decode_leaders( Word32 index, int16_t idx_lead, float *cv );
56 :
57 : static void idx2c( int16_t n, int16_t *p, const int16_t k, Word32 val );
58 :
59 : static void divide_64_32( int16_t *x, UWord32 y, UWord32 *result, UWord32 *rem );
60 :
61 :
62 : /*-----------------------------------------------------------------*
63 : * permute()
64 : * used in CNG-LP coding
65 : *-----------------------------------------------------------------*/
66 :
67 2556 : void permute(
68 : float *pTmp1, /* i/o: vector whose components are to be permuted */
69 : const int16_t *perm /* i : permutation info (indexes that should be interchanged), max two perms */
70 : )
71 : {
72 : int16_t p1, p2;
73 : float tmp;
74 :
75 2556 : p1 = perm[0];
76 2556 : p2 = perm[1];
77 2556 : tmp = pTmp1[p1];
78 2556 : pTmp1[p1] = pTmp1[p2];
79 2556 : pTmp1[p2] = tmp;
80 2556 : p1 = perm[2];
81 :
82 2556 : if ( p1 > -1 )
83 : {
84 444 : p2 = perm[3];
85 444 : tmp = pTmp1[p1];
86 444 : pTmp1[p1] = pTmp1[p2];
87 444 : pTmp1[p2] = tmp;
88 : }
89 :
90 2556 : return;
91 : }
92 :
93 : /*-----------------------------------------------------------------*
94 : * make_offset_scale()
95 : *
96 : *-----------------------------------------------------------------*/
97 :
98 2804624 : static void make_offset_scale(
99 : const UWord32 tab_no_cv[],
100 : const Word8 *no_ld,
101 : const int16_t no_scl,
102 : UWord32 *offset_scale )
103 : {
104 : int16_t i;
105 :
106 2804624 : offset_scale[0] = 1;
107 11218496 : for ( i = 1; i <= no_scl; i++ )
108 : {
109 8413872 : offset_scale[i] = offset_scale[i - 1] + tab_no_cv[(int16_t) no_ld[i - 1]];
110 : }
111 :
112 2804624 : return;
113 : }
114 :
115 : /*-----------------------------------------------------------------*
116 : * decode_indexes()
117 : *
118 : *-----------------------------------------------------------------*/
119 :
120 1055504 : static int16_t decode_indexes(
121 : int16_t *index,
122 : const int16_t no_bits,
123 : const float *p_scales,
124 : const int16_t prediction_flag,
125 : float *x_lvq,
126 : const int16_t mode_glb )
127 : {
128 1055504 : UWord32 index1 = 0, index2, idx_scale;
129 : uint16_t i;
130 : float scale;
131 :
132 1055504 : int16_t len_scales = MAX_NO_SCALES * 2;
133 : UWord32 offset_scale1[MAX_NO_SCALES + 1], offset_scale2[MAX_NO_SCALES + 1];
134 :
135 1055504 : if ( no_bits <= 2 * LEN_INDICE ) /* the third short is not used */
136 : {
137 582603 : index[2] = 0;
138 582603 : if ( no_bits <= LEN_INDICE )
139 : {
140 2550 : index[1] = 0;
141 : }
142 : }
143 :
144 : /* safety check in case of bit errors */
145 4222016 : for ( i = 0; i < 3; i++ )
146 : {
147 3166512 : if ( index[i] < 0 )
148 : {
149 0 : set_f( x_lvq, 0.0f, 2 * LATTICE_DIM );
150 0 : index[i] = 0;
151 0 : return 1;
152 : }
153 : }
154 :
155 1055504 : create_offset( offset_scale1, offset_scale2, mode_glb, prediction_flag );
156 : /* first subvector */
157 1055504 : if ( offset_scale2[MAX_NO_SCALES - 1] > 0 )
158 : {
159 1055504 : divide_64_32( index, offset_scale2[MAX_NO_SCALES], &index1, &index2 );
160 : }
161 : else
162 : {
163 0 : index1 = (UWord32) ( index[0] ); /* this is for very low bitrates, so there is no loss in truncation */
164 0 : index2 = 0;
165 : }
166 :
167 1055504 : if ( index1 == 0 )
168 : {
169 3492 : for ( i = 0; i < LATTICE_DIM; i++ )
170 : {
171 3104 : x_lvq[i] = 0.0;
172 : }
173 : }
174 : else
175 : {
176 1055116 : if ( index1 >= offset_scale1[MAX_NO_SCALES] )
177 : {
178 : /* safety check in case of bit errors */
179 0 : set_f( x_lvq, 0.0f, 2 * LATTICE_DIM );
180 0 : return 1;
181 : }
182 :
183 : /* find idx_scale */
184 1055116 : i = 1;
185 1634512 : while ( (int16_t) i <= MAX_NO_SCALES && index1 >= offset_scale1[i] )
186 : {
187 579396 : i++;
188 : }
189 :
190 1055116 : idx_scale = i - 1;
191 1055116 : index1 -= offset_scale1[idx_scale];
192 :
193 : /* find idx_leader */
194 1055116 : i = 1;
195 12758160 : while ( index1 >= table_no_cv[i] )
196 : {
197 11703044 : i++;
198 : }
199 1055116 : decode_comb( (Word32) ( index1 - table_no_cv[i - 1] ), x_lvq, i - 1 );
200 1055116 : scale = p_scales[mode_glb * len_scales + idx_scale];
201 9496044 : for ( i = 0; i < LATTICE_DIM; i++ )
202 : {
203 8440928 : x_lvq[i] *= scale;
204 : }
205 : }
206 :
207 : /* second subvector */
208 1055504 : if ( index2 == 0 )
209 : {
210 395703 : for ( i = LATTICE_DIM; i < 2 * LATTICE_DIM; i++ )
211 : {
212 351736 : x_lvq[i] = 0.0;
213 : }
214 : }
215 : else
216 : {
217 :
218 : /* find the index for the scale/truncation */
219 1011537 : i = 1;
220 1512998 : while ( index2 >= offset_scale2[i] )
221 : {
222 501461 : i++;
223 : }
224 :
225 1011537 : idx_scale = i - 1;
226 1011537 : index2 -= offset_scale2[idx_scale];
227 : /* find the index of the leader vector */
228 1011537 : i = 1;
229 4488891 : while ( index2 >= table_no_cv[i] )
230 : {
231 3477354 : i++;
232 : }
233 1011537 : decode_comb( (Word32) ( index2 - table_no_cv[i - 1] ), &x_lvq[LATTICE_DIM], i - 1 );
234 :
235 1011537 : scale = p_scales[mode_glb * len_scales + MAX_NO_SCALES + idx_scale];
236 9103833 : for ( i = LATTICE_DIM; i < 2 * LATTICE_DIM; i++ )
237 : {
238 8092296 : x_lvq[i] *= scale;
239 : }
240 : }
241 :
242 1055504 : return 0;
243 : }
244 :
245 : /*-----------------------------------------------------------------*
246 : * deindex_lvq()
247 : *
248 : *-----------------------------------------------------------------*/
249 :
250 1054205 : int16_t deindex_lvq(
251 : int16_t *index, /* i : index to be decoded, as an array of 3 shorts */
252 : float *x_lvq, /* o : decoded codevector */
253 : const int16_t mode, /* i : LVQ coding mode (select scales & no_lead ), or idx_cv */
254 : const int16_t sf_flag, /* i : safety net flag */
255 : const int16_t no_bits /* i : number of bits for lattice */
256 : )
257 : {
258 : int16_t i;
259 : const float *p_scales;
260 : int16_t mode_glb;
261 : int16_t ber_flag;
262 :
263 1054205 : if ( sf_flag == 1 )
264 : {
265 150641 : if ( mode < 6 ) /* for NB */
266 : {
267 0 : mode_glb = offset_lvq_modes_SN[mode] + offset_in_lvq_mode_SN[mode][no_bits - min_lat_bits_SN[mode]];
268 : }
269 : else
270 : {
271 150641 : mode_glb = offset_lvq_modes_SN[mode] + no_bits - min_lat_bits_SN[mode]; /* there is granularity of 1 bit */
272 : }
273 150641 : p_scales = &scales[0][0];
274 : }
275 : else
276 : {
277 903564 : if ( ( mode < 6 ) || ( mode == 12 ) ) /* for NB */
278 : {
279 20837 : mode_glb = offset_lvq_modes_pred[mode] + offset_in_lvq_mode_pred[mode][no_bits - min_lat_bits_pred[mode]];
280 : }
281 : else
282 : {
283 882727 : mode_glb = offset_lvq_modes_pred[mode] + no_bits - min_lat_bits_pred[mode];
284 : }
285 903564 : p_scales = &scales_p[0][0];
286 : }
287 :
288 : /* decode the lattice index into the lattice codevectors for the two subvectors */
289 1054205 : ber_flag = decode_indexes( index, no_bits, p_scales, 1 - sf_flag, x_lvq, mode_glb );
290 :
291 1054205 : if ( sf_flag == 1 )
292 : {
293 2560897 : for ( i = 0; i < 2 * LATTICE_DIM; i++ )
294 : {
295 2410256 : x_lvq[i] *= sigma_MSLVQ[mode][i];
296 : }
297 : }
298 : else
299 : {
300 15360588 : for ( i = 0; i < 2 * LATTICE_DIM; i++ )
301 : {
302 14457024 : x_lvq[i] *= sigma_p[mode][i];
303 : }
304 : }
305 :
306 1054205 : return ber_flag;
307 : }
308 :
309 : /*------------------------------------------------------------------------------------------------------------*
310 : * deindex_lvq_cng()
311 : *
312 : * Note:
313 : * The sampling frequency for the LVQ CNG decoder frame can be determined by checking the fully decoded
314 : * value of the highest order LSF coefficient. Thus sampling rate information, nor extra codebooks are
315 : * not needed for deindex_lvq_cng(), since it is embedded inside the LSF codebooks.
316 : *------------------------------------------------------------------------------------------------------------*/
317 :
318 1299 : int16_t deindex_lvq_cng(
319 : int16_t *index, /* i : index to be decoded, as an array of 3 shorts */
320 : float *x_lvq, /* o : decoded codevector */
321 : const int16_t idx_cv, /* i : relative mode_lvq, wrt START_CNG */
322 : const int16_t no_bits /* i : number of bits for lattice */
323 : )
324 : {
325 : int16_t i;
326 : const float *p_scales;
327 : int16_t mode_glb, mode;
328 : int16_t ber_flag;
329 :
330 1299 : mode_glb = START_CNG + idx_cv;
331 1299 : mode = LVQ_COD_MODES + idx_cv;
332 :
333 1299 : p_scales = &scales[0][0];
334 1299 : ber_flag = decode_indexes( index, no_bits, p_scales, 0, x_lvq, mode_glb );
335 :
336 22083 : for ( i = 0; i < 2 * LATTICE_DIM; i++ )
337 : {
338 20784 : x_lvq[i] *= sigma_MSLVQ[mode][i];
339 : }
340 :
341 : /* check if permutting needed */
342 1299 : if ( cng_sort[idx_cv] )
343 : {
344 1278 : permute( x_lvq, perm_MSLVQ[idx_cv] );
345 : }
346 :
347 1299 : return ber_flag;
348 : }
349 :
350 :
351 : /*-----------------------------------------------------------------*
352 : * decode_comb()
353 : *
354 : * combinatorial deindexing of a codevector including the signs
355 : *
356 : *-----------------------------------------------------------------*/
357 :
358 2076037 : static void decode_comb(
359 : Word32 index, /* i : index to be decoded */
360 : float *cv, /* o : decoded codevector */
361 : int16_t idx_lead /* i : leader class index */
362 : )
363 : {
364 : int16_t idx_sign;
365 :
366 2076037 : idx_sign = (int16_t) ( index / pi0[idx_lead] );
367 2076037 : index -= idx_sign * pi0[idx_lead];
368 2076037 : decode_leaders( index, idx_lead, cv );
369 2076037 : decode_sign_pc1( cv, idx_sign, pl_par[idx_lead] );
370 :
371 2076037 : return;
372 : }
373 :
374 : /*-----------------------------------------------------------------*
375 : * decode_leaders()
376 : *
377 : * decode index of a codevector from the leader class idx_lead
378 : *-----------------------------------------------------------------*/
379 :
380 2076037 : static void decode_leaders(
381 : Word32 index, /* i : index to be decoded */
382 : int16_t idx_lead, /* i : leader class index */
383 : float *cv /* o : decoded codevector */
384 : )
385 : {
386 : int16_t i, no_vals_loc, no_vals_last, p[LATTICE_DIM], dim_loc, n_crt;
387 : Word32 index1;
388 : float val_crt;
389 :
390 2076037 : no_vals_loc = no_vals[idx_lead];
391 2076037 : val_crt = vals[idx_lead][no_vals_loc - 1];
392 2076037 : no_vals_last = no_vals_ind[idx_lead][no_vals_loc - 1];
393 :
394 12042446 : for ( i = 0; i < no_vals_last; i++ )
395 : {
396 9966409 : cv[i] = val_crt;
397 : }
398 :
399 2076037 : val_crt = 1;
400 2076037 : dim_loc = no_vals_last;
401 :
402 2076037 : switch ( no_vals_loc )
403 : {
404 209261 : case 1:
405 209261 : break;
406 1248729 : case 2:
407 1248729 : idx2c( LATTICE_DIM, p, no_vals_ind[idx_lead][0], index );
408 1248729 : put_value( cv, p, vals[idx_lead][0], no_vals_last, no_vals_ind[idx_lead][0] );
409 1248729 : break;
410 5045 : case 4:
411 5045 : dim_loc += no_vals_ind[idx_lead][2];
412 5045 : n_crt = no_vals_ind[idx_lead][2];
413 5045 : index1 = (Word32) index / C_VQ[dim_loc][n_crt];
414 5045 : index -= index1 * C_VQ[dim_loc][n_crt];
415 5045 : idx2c( dim_loc, p, n_crt, index );
416 5045 : put_value( cv, p, vals[idx_lead][2], no_vals_last, no_vals_ind[idx_lead][2] );
417 5045 : index = index1;
418 : /* FALLTHRU */
419 618047 : case 3:
420 618047 : dim_loc += no_vals_ind[idx_lead][1];
421 618047 : n_crt = no_vals_ind[idx_lead][1];
422 618047 : index1 = (Word32) index / C_VQ[dim_loc][n_crt];
423 618047 : index -= index1 * C_VQ[dim_loc][n_crt];
424 618047 : idx2c( dim_loc, p, n_crt, index );
425 618047 : put_value( cv, p, vals[idx_lead][1], dim_loc - n_crt, n_crt );
426 618047 : idx2c( LATTICE_DIM, p, no_vals_ind[idx_lead][0], index1 );
427 618047 : put_value( cv, p, vals[idx_lead][0], dim_loc, no_vals_ind[idx_lead][0] );
428 618047 : break;
429 : }
430 :
431 2076037 : return;
432 : }
433 :
434 : /*-----------------------------------------------------------------*
435 : * put_value()
436 : *
437 : * inserts no_new_val values of val in the codevector cv at the positions given by the array p
438 : *-----------------------------------------------------------------*/
439 :
440 2489868 : static void put_value(
441 : float *cv, /* i : input codevector */
442 : int16_t *p, /* i : array with positions */
443 : float val, /* i : value to be inserted */
444 : const int16_t dim, /* i : vector dimension */
445 : const int16_t no_new_val /* i : number of values to be inserted */
446 : )
447 : {
448 : float cv_out[LATTICE_DIM];
449 : int16_t i, occ[LATTICE_DIM], cnt;
450 :
451 21623399 : for ( i = 0; i < dim + no_new_val; i++ )
452 : {
453 19133531 : occ[i] = 0;
454 : }
455 :
456 9131755 : for ( i = 0; i < no_new_val; i++ )
457 : {
458 6641887 : cv_out[p[i]] = val;
459 6641887 : occ[p[i]] = 1;
460 : }
461 :
462 2489868 : cnt = 0;
463 21623399 : for ( i = 0; i < dim + no_new_val; i++ )
464 : {
465 19133531 : if ( occ[i] == 0 )
466 : {
467 12491644 : cv_out[i] = cv[cnt++];
468 : }
469 : }
470 :
471 21623399 : for ( i = 0; i < dim + no_new_val; i++ )
472 : {
473 19133531 : cv[i] = cv_out[i];
474 : }
475 :
476 2489868 : return;
477 : }
478 :
479 : /*-----------------------------------------------------------------*
480 : * idx2c()
481 : *
482 : * decode index of binomial combinations, find the positions of k components out of n total components
483 : *-----------------------------------------------------------------*/
484 :
485 6641887 : static void idx2c(
486 : int16_t n, /* i : total number of positions (components) */
487 : int16_t *p, /* o : array with positions of the k components */
488 : const int16_t k, /* i : number of components whose position is to be determined */
489 : Word32 val /* i : index to be decoded */
490 : )
491 : {
492 : int16_t i, skip, pos, k1;
493 :
494 6641887 : skip = 0;
495 6641887 : pos = 0;
496 6641887 : k1 = k - 1;
497 14054599 : while ( skip + C_VQ[n - pos - 1][k1] - 1 < val )
498 : {
499 7412712 : skip += C_VQ[n - pos - 1][k1];
500 7412712 : pos++;
501 : }
502 :
503 6641887 : p[0] = pos;
504 6641887 : n -= pos + 1;
505 6641887 : val -= skip;
506 6641887 : if ( k == 1 )
507 : {
508 2489868 : return;
509 : }
510 :
511 4152019 : idx2c( n, p + 1, k1, val );
512 :
513 : /* pos+1 */
514 12589751 : for ( i = 1; i < k; i++ )
515 : {
516 8437732 : p[i] += pos + 1;
517 : }
518 :
519 4152019 : return;
520 : }
521 :
522 : /*-----------------------------------------------------------------*
523 : * decode_sign_pc1()
524 : *
525 : *-----------------------------------------------------------------*/
526 :
527 2076037 : static void decode_sign_pc1(
528 : float *c, /* o : decoded codevector */
529 : int16_t idx_sign, /* i : sign index */
530 : const int16_t parity /* i : parity flag (+1/-1/0) */
531 : )
532 : {
533 2076037 : int16_t i, len = LATTICE_DIM, cnt_neg = 1;
534 :
535 2076037 : if ( parity )
536 : {
537 874600 : len -= 1;
538 : }
539 :
540 17809733 : for ( i = 0; i < len; i++ )
541 : {
542 15733696 : if ( c[i] > 0 )
543 : {
544 11419699 : if ( idx_sign % 2 )
545 : {
546 5522185 : c[i] = -c[i];
547 5522185 : cnt_neg = -cnt_neg;
548 : }
549 11419699 : idx_sign >>= 1;
550 : }
551 : }
552 :
553 2076037 : if ( len < LATTICE_DIM )
554 : {
555 874600 : if ( cnt_neg != parity )
556 : {
557 398034 : c[len] = -c[len];
558 : }
559 : }
560 :
561 2076037 : return;
562 : }
563 :
564 : /*-----------------------------------------------------------------*
565 : * extract_low()
566 : *
567 : * (!!!!! function for int64 !!!!)
568 : *-----------------------------------------------------------------*/
569 :
570 1387232 : static UWord32 extract_low(
571 : UWord32 x )
572 : {
573 1387232 : return ( x & ( 0xffff ) );
574 : }
575 :
576 : /*-----------------------------------------------------------------*
577 : * extract_high()
578 : *
579 : * (!!!!! function for int64 !!!!)
580 : *-----------------------------------------------------------------*/
581 :
582 1387232 : static UWord32 extract_high(
583 : UWord32 x )
584 : {
585 1387232 : return ( x >> 16 );
586 : }
587 :
588 : /*-----------------------------------------------------------------*
589 : * multiply32_32_64()
590 : *
591 : * (!!!!! function for int64 !!!!)
592 : *-----------------------------------------------------------------*/
593 :
594 346808 : void multiply32_32_64(
595 : UWord32 x,
596 : UWord32 y,
597 : UWord32 *res )
598 : {
599 : UWord32 tmp, x_tmp[2], y_tmp[2];
600 346808 : UWord32 high = 0;
601 :
602 346808 : x_tmp[0] = extract_low( x ); /* lowest 16 bits */
603 346808 : x_tmp[1] = extract_high( x );
604 346808 : y_tmp[0] = extract_low( y );
605 346808 : y_tmp[1] = extract_high( y );
606 346808 : tmp = x_tmp[0] * y_tmp[0];
607 346808 : high = extract_high( tmp );
608 346808 : res[0] = extract_low( tmp );
609 346808 : tmp = x_tmp[1] * y_tmp[0] + x_tmp[0] * y_tmp[1] + high; /* x and y are not using all 32 bits */
610 346808 : high = extract_high( tmp );
611 346808 : res[0] += ( extract_low( tmp ) << 16 );
612 346808 : tmp = x_tmp[1] * y_tmp[1] + high;
613 346808 : res[1] = tmp;
614 :
615 346808 : return;
616 : }
617 :
618 : /*-----------------------------------------------------------------*
619 : * get_no_bits()
620 : *
621 : * (!!!!! function for int64 !!!!)
622 : *-----------------------------------------------------------------*/
623 :
624 1055504 : static int16_t get_no_bits(
625 : UWord32 x )
626 : {
627 1055504 : int16_t nb = 0;
628 :
629 1055504 : if ( x == 0 )
630 : {
631 869660 : return 1;
632 : }
633 :
634 611476 : while ( x > 0 )
635 : {
636 425632 : x >>= 1;
637 425632 : nb++;
638 : }
639 :
640 185844 : return nb;
641 : }
642 :
643 : /*-----------------------------------------------------------------*
644 : * divide_64_32()
645 : *
646 : * (!!!!! function for int64 !!!!)
647 : *-----------------------------------------------------------------*/
648 :
649 1055504 : static void divide_64_32(
650 : int16_t *xs, /* i : denominator as array of two int32 */
651 : UWord32 y, /* i : nominator on 32 bits */
652 : UWord32 *result, /* o : integer division result on 32 bits */
653 : UWord32 *rem /* o : integer division reminder on 32 bits */
654 : )
655 : {
656 : int16_t nb_x1;
657 : UWord32 r, q, q1, x_tmp, x[2];
658 :
659 1055504 : x[0] = ( ( (UWord32) xs[2] & ( ( 1 << 2 ) - 1 ) ) << ( LEN_INDICE * 2 ) ) + ( xs[1] << LEN_INDICE ) + xs[0];
660 1055504 : x[1] = xs[2] >> 2;
661 :
662 : /* find number of bits of x[0] and x[1] */
663 1055504 : nb_x1 = get_no_bits( x[1] );
664 :
665 : /* take the first 32 bits */
666 1055504 : if ( nb_x1 > 0 )
667 : {
668 1055504 : x_tmp = ( x[1] << ( 32 - nb_x1 ) ) + ( x[0] >> nb_x1 );
669 1055504 : q = (UWord32) ( x_tmp / y + 0.5 );
670 1055504 : r = x_tmp - q * y; /* this is the first reminder */
671 1055504 : r = ( r << nb_x1 ) + ( x[0] & ( ( 1 << nb_x1 ) - 1 ) );
672 :
673 1055504 : q1 = (UWord32) ( r / y + 0.5 );
674 1055504 : *result = ( q << nb_x1 ) + q1;
675 1055504 : *rem = r - q1 * y;
676 : }
677 : else
678 : {
679 0 : x_tmp = x[0];
680 0 : q = ( (UWord32) ( x_tmp / y + 0.5 ) );
681 0 : *result = q;
682 0 : *rem = x_tmp - q * y;
683 : }
684 :
685 1055504 : return;
686 : }
687 :
688 :
689 : /*-----------------------------------------------------------------*
690 : * create_offset()
691 : *
692 : *
693 : *-----------------------------------------------------------------*/
694 :
695 1402312 : void create_offset(
696 : UWord32 *offset_scale1,
697 : UWord32 *offset_scale2,
698 : const int16_t mode,
699 : const int16_t prediction_flag )
700 : {
701 : int16_t tmp, tmp1;
702 :
703 1402312 : if ( prediction_flag == 0 )
704 : {
705 : /* safety_net */
706 243846 : tmp = no_lead_idx[mode][0];
707 243846 : if ( ( tmp <= LIMIT_LEADER ) && ( tmp < no_lead_idx[mode][1] - 2 ) )
708 : {
709 0 : tmp += DELTA_LEADER;
710 : }
711 243846 : make_offset_scale( table_no_cv, leaders_short[tmp], MAX_NO_SCALES, offset_scale1 );
712 243846 : make_offset_scale( table_no_cv, leaders_short[no_lead_idx[mode][1]], MAX_NO_SCALES, offset_scale2 );
713 : }
714 : else
715 : {
716 1158466 : tmp = no_lead_p_idx[mode][0];
717 1158466 : tmp1 = no_lead_p_idx[mode][1];
718 1158466 : if ( ( tmp <= LIMIT_LEADER ) && ( tmp < tmp1 - 2 ) )
719 : {
720 27572 : tmp += DELTA_LEADER;
721 : }
722 :
723 1158466 : if ( ( tmp == LIMIT_LEADER ) && ( tmp1 == 0 ) )
724 : {
725 0 : tmp += DELTA_LEADER;
726 0 : tmp1 = DELTA_LEADER;
727 : }
728 :
729 1158466 : make_offset_scale( table_no_cv, leaders_short[tmp], MAX_NO_SCALES, offset_scale1 );
730 1158466 : make_offset_scale( table_no_cv, leaders_short[tmp1], MAX_NO_SCALES, offset_scale2 );
731 : }
732 :
733 1402312 : return;
734 : }
735 :
736 : /*-----------------------------------------------------------------*
737 : * sort_desc_ind()
738 : *
739 : * sorts in descending order and computes indices in the sorted vector
740 : *-----------------------------------------------------------------*/
741 1400476 : void sort_desc_ind(
742 : float *s, /* i/o: vector to be sorted */
743 : const int16_t len, /* i : vector length */
744 : int16_t *ind /* o : array of indices */
745 : )
746 : {
747 : int16_t i, k, sorted, a;
748 : float t;
749 :
750 12531803 : for ( i = 0; i < len; i++ )
751 : {
752 11131327 : ind[i] = i;
753 : }
754 1400476 : sorted = 0;
755 9207456 : for ( k = len - 1; k && !sorted; k-- )
756 : {
757 7806980 : sorted = 1;
758 43232633 : for ( i = 0; i < k; i++ )
759 : {
760 35425653 : if ( s[i] < s[i + 1] )
761 : {
762 19109310 : sorted = 0;
763 19109310 : t = s[i];
764 19109310 : s[i] = s[i + 1];
765 19109310 : s[i + 1] = t;
766 19109310 : a = ind[i];
767 19109310 : ind[i] = ind[i + 1];
768 19109310 : ind[i + 1] = a;
769 : }
770 : }
771 : }
772 :
773 1400476 : return;
774 : }
775 :
776 : /*-----------------------------------------------------------------*
777 : * deindex_lvq_SHB()
778 : *
779 : *
780 : *-----------------------------------------------------------------*/
781 :
782 9384 : void deindex_lvq_SHB(
783 : UWord32 index,
784 : float *out,
785 : const int16_t nbits,
786 : const int16_t mode )
787 : {
788 : uint16_t i;
789 : const Word8 *p_no_lead;
790 : const float *p_scales;
791 : float scale;
792 : int16_t idx_scale;
793 : UWord32 offsets[MAX_NO_SCALES + 1];
794 :
795 9384 : if ( mode == 0 )
796 : {
797 9384 : p_no_lead = &no_lead_BWE[( nbits - mslvq_SHB_min_bits[0] ) * 3];
798 9384 : p_scales = &scales_BWE[( nbits - mslvq_SHB_min_bits[0] ) * 3];
799 : }
800 : else
801 : {
802 0 : p_no_lead = &no_lead_BWE_3b[( nbits - mslvq_SHB_min_bits[1] ) * 3];
803 0 : p_scales = &scales_BWE_3b[( nbits - mslvq_SHB_min_bits[1] ) * 3];
804 : }
805 :
806 :
807 9384 : if ( index == 0 )
808 : {
809 0 : set_zero( out, LATTICE_DIM );
810 : }
811 : else
812 : {
813 : /* create offsets */
814 9384 : offsets[0] = 0;
815 37536 : for ( i = 0; i < MAX_NO_SCALES; i++ )
816 : {
817 28152 : offsets[i + 1] = table_no_cv[p_no_lead[i]] + offsets[i];
818 : }
819 :
820 : /* find idx_scale */
821 9384 : idx_scale = 0;
822 21741 : while ( (int16_t) i <= MAX_NO_SCALES && index >= offsets[idx_scale] )
823 : {
824 12357 : idx_scale++;
825 : }
826 9384 : idx_scale--;
827 9384 : index -= offsets[idx_scale];
828 :
829 : /* find idx_leader */
830 9384 : i = 1;
831 64107 : while ( index > table_no_cv[i] )
832 : {
833 54723 : i++;
834 : }
835 9384 : i = i - 1;
836 :
837 9384 : decode_comb( (Word32) ( index - table_no_cv[i] - 1 ), out, i );
838 :
839 9384 : scale = p_scales[idx_scale];
840 84456 : for ( i = 0; i < LATTICE_DIM; i++ )
841 : {
842 75072 : out[i] *= scale * sigma_BWE[mode * LATTICE_DIM + i];
843 : }
844 : }
845 :
846 9384 : return;
847 : }
|