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 6964 : 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 6964 : p1 = perm[0];
76 6964 : p2 = perm[1];
77 6964 : tmp = pTmp1[p1];
78 6964 : pTmp1[p1] = pTmp1[p2];
79 6964 : pTmp1[p2] = tmp;
80 6964 : p1 = perm[2];
81 :
82 6964 : if ( p1 > -1 )
83 : {
84 1279 : p2 = perm[3];
85 1279 : tmp = pTmp1[p1];
86 1279 : pTmp1[p1] = pTmp1[p2];
87 1279 : pTmp1[p2] = tmp;
88 : }
89 :
90 6964 : return;
91 : }
92 :
93 : /*-----------------------------------------------------------------*
94 : * make_offset_scale()
95 : *
96 : *-----------------------------------------------------------------*/
97 :
98 27506488 : 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 27506488 : offset_scale[0] = 1;
107 110025952 : for ( i = 1; i <= no_scl; i++ )
108 : {
109 82519464 : offset_scale[i] = offset_scale[i - 1] + tab_no_cv[(int16_t) no_ld[i - 1]];
110 : }
111 :
112 27506488 : return;
113 : }
114 :
115 : /*-----------------------------------------------------------------*
116 : * decode_indexes()
117 : *
118 : *-----------------------------------------------------------------*/
119 :
120 9769063 : 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 9769063 : UWord32 index1 = 0, index2, idx_scale;
129 : uint16_t i;
130 : float scale;
131 :
132 9769063 : int16_t len_scales = MAX_NO_SCALES * 2;
133 : UWord32 offset_scale1[MAX_NO_SCALES + 1], offset_scale2[MAX_NO_SCALES + 1];
134 :
135 9769063 : if ( no_bits <= 2 * LEN_INDICE ) /* the third short is not used */
136 : {
137 6311834 : index[2] = 0;
138 6311834 : if ( no_bits <= LEN_INDICE )
139 : {
140 15590 : index[1] = 0;
141 : }
142 : }
143 :
144 : /* safety check in case of bit errors */
145 39076252 : for ( i = 0; i < 3; i++ )
146 : {
147 29307189 : 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 9769063 : create_offset( offset_scale1, offset_scale2, mode_glb, prediction_flag );
156 : /* first subvector */
157 9769063 : if ( offset_scale2[MAX_NO_SCALES - 1] > 0 )
158 : {
159 9769063 : 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 9769063 : if ( index1 == 0 )
168 : {
169 18117 : for ( i = 0; i < LATTICE_DIM; i++ )
170 : {
171 16104 : x_lvq[i] = 0.0;
172 : }
173 : }
174 : else
175 : {
176 9767050 : 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 9767050 : i = 1;
185 16256506 : while ( (int16_t) i <= MAX_NO_SCALES && index1 >= offset_scale1[i] )
186 : {
187 6489456 : i++;
188 : }
189 :
190 9767050 : idx_scale = i - 1;
191 9767050 : index1 -= offset_scale1[idx_scale];
192 :
193 : /* find idx_leader */
194 9767050 : i = 1;
195 108523842 : while ( index1 >= table_no_cv[i] )
196 : {
197 98756792 : i++;
198 : }
199 9767050 : decode_comb( (Word32) ( index1 - table_no_cv[i - 1] ), x_lvq, i - 1 );
200 9767050 : scale = p_scales[mode_glb * len_scales + idx_scale];
201 87903450 : for ( i = 0; i < LATTICE_DIM; i++ )
202 : {
203 78136400 : x_lvq[i] *= scale;
204 : }
205 : }
206 :
207 : /* second subvector */
208 9769063 : if ( index2 == 0 )
209 : {
210 6520707 : for ( i = LATTICE_DIM; i < 2 * LATTICE_DIM; i++ )
211 : {
212 5796184 : x_lvq[i] = 0.0;
213 : }
214 : }
215 : else
216 : {
217 :
218 : /* find the index for the scale/truncation */
219 9044540 : i = 1;
220 13481354 : while ( index2 >= offset_scale2[i] )
221 : {
222 4436814 : i++;
223 : }
224 :
225 9044540 : idx_scale = i - 1;
226 9044540 : index2 -= offset_scale2[idx_scale];
227 : /* find the index of the leader vector */
228 9044540 : i = 1;
229 36784624 : while ( index2 >= table_no_cv[i] )
230 : {
231 27740084 : i++;
232 : }
233 9044540 : decode_comb( (Word32) ( index2 - table_no_cv[i - 1] ), &x_lvq[LATTICE_DIM], i - 1 );
234 :
235 9044540 : scale = p_scales[mode_glb * len_scales + MAX_NO_SCALES + idx_scale];
236 81400860 : for ( i = LATTICE_DIM; i < 2 * LATTICE_DIM; i++ )
237 : {
238 72356320 : x_lvq[i] *= scale;
239 : }
240 : }
241 :
242 9769063 : return 0;
243 : }
244 :
245 : /*-----------------------------------------------------------------*
246 : * deindex_lvq()
247 : *
248 : *-----------------------------------------------------------------*/
249 :
250 9766424 : 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 9766424 : if ( sf_flag == 1 )
264 : {
265 1107609 : if ( mode < 6 ) /* for NB */
266 : {
267 4753 : 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 1102856 : mode_glb = offset_lvq_modes_SN[mode] + no_bits - min_lat_bits_SN[mode]; /* there is granularity of 1 bit */
272 : }
273 1107609 : p_scales = &scales[0][0];
274 : }
275 : else
276 : {
277 8658815 : if ( ( mode < 6 ) || ( mode == 12 ) ) /* for NB */
278 : {
279 82504 : 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 8576311 : mode_glb = offset_lvq_modes_pred[mode] + no_bits - min_lat_bits_pred[mode];
284 : }
285 8658815 : p_scales = &scales_p[0][0];
286 : }
287 :
288 : /* decode the lattice index into the lattice codevectors for the two subvectors */
289 9766424 : ber_flag = decode_indexes( index, no_bits, p_scales, 1 - sf_flag, x_lvq, mode_glb );
290 :
291 9766424 : if ( sf_flag == 1 )
292 : {
293 18829353 : for ( i = 0; i < 2 * LATTICE_DIM; i++ )
294 : {
295 17721744 : x_lvq[i] *= sigma_MSLVQ[mode][i];
296 : }
297 : }
298 : else
299 : {
300 147199855 : for ( i = 0; i < 2 * LATTICE_DIM; i++ )
301 : {
302 138541040 : x_lvq[i] *= sigma_p[mode][i];
303 : }
304 : }
305 :
306 9766424 : 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 2639 : 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 2639 : mode_glb = START_CNG + idx_cv;
331 2639 : mode = LVQ_COD_MODES + idx_cv;
332 :
333 2639 : p_scales = &scales[0][0];
334 2639 : ber_flag = decode_indexes( index, no_bits, p_scales, 0, x_lvq, mode_glb );
335 :
336 44863 : for ( i = 0; i < 2 * LATTICE_DIM; i++ )
337 : {
338 42224 : x_lvq[i] *= sigma_MSLVQ[mode][i];
339 : }
340 :
341 : /* check if permutting needed */
342 2639 : if ( cng_sort[idx_cv] )
343 : {
344 2392 : permute( x_lvq, perm_MSLVQ[idx_cv] );
345 : }
346 :
347 2639 : return ber_flag;
348 : }
349 :
350 :
351 : /*-----------------------------------------------------------------*
352 : * decode_comb()
353 : *
354 : * combinatorial deindexing of a codevector including the signs
355 : *
356 : *-----------------------------------------------------------------*/
357 :
358 18834954 : 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 18834954 : idx_sign = (int16_t) ( index / pi0[idx_lead] );
367 18834954 : index -= idx_sign * pi0[idx_lead];
368 18834954 : decode_leaders( index, idx_lead, cv );
369 18834954 : decode_sign_pc1( cv, idx_sign, pl_par[idx_lead] );
370 :
371 18834954 : return;
372 : }
373 :
374 : /*-----------------------------------------------------------------*
375 : * decode_leaders()
376 : *
377 : * decode index of a codevector from the leader class idx_lead
378 : *-----------------------------------------------------------------*/
379 :
380 18834954 : 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 18834954 : no_vals_loc = no_vals[idx_lead];
391 18834954 : val_crt = vals[idx_lead][no_vals_loc - 1];
392 18834954 : no_vals_last = no_vals_ind[idx_lead][no_vals_loc - 1];
393 :
394 110317116 : for ( i = 0; i < no_vals_last; i++ )
395 : {
396 91482162 : cv[i] = val_crt;
397 : }
398 :
399 18834954 : val_crt = 1;
400 18834954 : dim_loc = no_vals_last;
401 :
402 18834954 : switch ( no_vals_loc )
403 : {
404 2313094 : case 1:
405 2313094 : break;
406 10983647 : case 2:
407 10983647 : idx2c( LATTICE_DIM, p, no_vals_ind[idx_lead][0], index );
408 10983647 : put_value( cv, p, vals[idx_lead][0], no_vals_last, no_vals_ind[idx_lead][0] );
409 10983647 : break;
410 46777 : case 4:
411 46777 : dim_loc += no_vals_ind[idx_lead][2];
412 46777 : n_crt = no_vals_ind[idx_lead][2];
413 46777 : index1 = (Word32) index / C_VQ[dim_loc][n_crt];
414 46777 : index -= index1 * C_VQ[dim_loc][n_crt];
415 46777 : idx2c( dim_loc, p, n_crt, index );
416 46777 : put_value( cv, p, vals[idx_lead][2], no_vals_last, no_vals_ind[idx_lead][2] );
417 46777 : index = index1;
418 : /* FALLTHRU */
419 5538213 : case 3:
420 5538213 : dim_loc += no_vals_ind[idx_lead][1];
421 5538213 : n_crt = no_vals_ind[idx_lead][1];
422 5538213 : index1 = (Word32) index / C_VQ[dim_loc][n_crt];
423 5538213 : index -= index1 * C_VQ[dim_loc][n_crt];
424 5538213 : idx2c( dim_loc, p, n_crt, index );
425 5538213 : put_value( cv, p, vals[idx_lead][1], dim_loc - n_crt, n_crt );
426 5538213 : idx2c( LATTICE_DIM, p, no_vals_ind[idx_lead][0], index1 );
427 5538213 : put_value( cv, p, vals[idx_lead][0], dim_loc, no_vals_ind[idx_lead][0] );
428 5538213 : break;
429 : }
430 :
431 18834954 : 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 22106850 : 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 192193012 : for ( i = 0; i < dim + no_new_val; i++ )
452 : {
453 170086162 : occ[i] = 0;
454 : }
455 :
456 81304320 : for ( i = 0; i < no_new_val; i++ )
457 : {
458 59197470 : cv_out[p[i]] = val;
459 59197470 : occ[p[i]] = 1;
460 : }
461 :
462 22106850 : cnt = 0;
463 192193012 : for ( i = 0; i < dim + no_new_val; i++ )
464 : {
465 170086162 : if ( occ[i] == 0 )
466 : {
467 110888692 : cv_out[i] = cv[cnt++];
468 : }
469 : }
470 :
471 192193012 : for ( i = 0; i < dim + no_new_val; i++ )
472 : {
473 170086162 : cv[i] = cv_out[i];
474 : }
475 :
476 22106850 : 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 59197470 : 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 59197470 : skip = 0;
495 59197470 : pos = 0;
496 59197470 : k1 = k - 1;
497 123875193 : while ( skip + C_VQ[n - pos - 1][k1] - 1 < val )
498 : {
499 64677723 : skip += C_VQ[n - pos - 1][k1];
500 64677723 : pos++;
501 : }
502 :
503 59197470 : p[0] = pos;
504 59197470 : n -= pos + 1;
505 59197470 : val -= skip;
506 59197470 : if ( k == 1 )
507 : {
508 22106850 : return;
509 : }
510 :
511 37090620 : idx2c( n, p + 1, k1, val );
512 :
513 : /* pos+1 */
514 111957334 : for ( i = 1; i < k; i++ )
515 : {
516 74866714 : p[i] += pos + 1;
517 : }
518 :
519 37090620 : return;
520 : }
521 :
522 : /*-----------------------------------------------------------------*
523 : * decode_sign_pc1()
524 : *
525 : *-----------------------------------------------------------------*/
526 :
527 18834954 : 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 18834954 : int16_t i, len = LATTICE_DIM, cnt_neg = 1;
534 :
535 18834954 : if ( parity )
536 : {
537 7567341 : len -= 1;
538 : }
539 :
540 161947245 : for ( i = 0; i < len; i++ )
541 : {
542 143112291 : if ( c[i] > 0 )
543 : {
544 102115532 : if ( idx_sign % 2 )
545 : {
546 48833731 : c[i] = -c[i];
547 48833731 : cnt_neg = -cnt_neg;
548 : }
549 102115532 : idx_sign >>= 1;
550 : }
551 : }
552 :
553 18834954 : if ( len < LATTICE_DIM )
554 : {
555 7567341 : if ( cnt_neg != parity )
556 : {
557 3502781 : c[len] = -c[len];
558 : }
559 : }
560 :
561 18834954 : return;
562 : }
563 :
564 : /*-----------------------------------------------------------------*
565 : * extract_low()
566 : *
567 : * (!!!!! function for int64 !!!!)
568 : *-----------------------------------------------------------------*/
569 :
570 15936724 : static UWord32 extract_low(
571 : UWord32 x )
572 : {
573 15936724 : return ( x & ( 0xffff ) );
574 : }
575 :
576 : /*-----------------------------------------------------------------*
577 : * extract_high()
578 : *
579 : * (!!!!! function for int64 !!!!)
580 : *-----------------------------------------------------------------*/
581 :
582 15936724 : static UWord32 extract_high(
583 : UWord32 x )
584 : {
585 15936724 : return ( x >> 16 );
586 : }
587 :
588 : /*-----------------------------------------------------------------*
589 : * multiply32_32_64()
590 : *
591 : * (!!!!! function for int64 !!!!)
592 : *-----------------------------------------------------------------*/
593 :
594 3984181 : void multiply32_32_64(
595 : UWord32 x,
596 : UWord32 y,
597 : UWord32 *res )
598 : {
599 : UWord32 tmp, x_tmp[2], y_tmp[2];
600 3984181 : UWord32 high = 0;
601 :
602 3984181 : x_tmp[0] = extract_low( x ); /* lowest 16 bits */
603 3984181 : x_tmp[1] = extract_high( x );
604 3984181 : y_tmp[0] = extract_low( y );
605 3984181 : y_tmp[1] = extract_high( y );
606 3984181 : tmp = x_tmp[0] * y_tmp[0];
607 3984181 : high = extract_high( tmp );
608 3984181 : res[0] = extract_low( tmp );
609 3984181 : tmp = x_tmp[1] * y_tmp[0] + x_tmp[0] * y_tmp[1] + high; /* x and y are not using all 32 bits */
610 3984181 : high = extract_high( tmp );
611 3984181 : res[0] += ( extract_low( tmp ) << 16 );
612 3984181 : tmp = x_tmp[1] * y_tmp[1] + high;
613 3984181 : res[1] = tmp;
614 :
615 3984181 : return;
616 : }
617 :
618 : /*-----------------------------------------------------------------*
619 : * get_no_bits()
620 : *
621 : * (!!!!! function for int64 !!!!)
622 : *-----------------------------------------------------------------*/
623 :
624 9769063 : static int16_t get_no_bits(
625 : UWord32 x )
626 : {
627 9769063 : int16_t nb = 0;
628 :
629 9769063 : if ( x == 0 )
630 : {
631 8433306 : return 1;
632 : }
633 :
634 4441330 : while ( x > 0 )
635 : {
636 3105573 : x >>= 1;
637 3105573 : nb++;
638 : }
639 :
640 1335757 : return nb;
641 : }
642 :
643 : /*-----------------------------------------------------------------*
644 : * divide_64_32()
645 : *
646 : * (!!!!! function for int64 !!!!)
647 : *-----------------------------------------------------------------*/
648 :
649 9769063 : 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 9769063 : x[0] = ( ( (UWord32) xs[2] & ( ( 1 << 2 ) - 1 ) ) << ( LEN_INDICE * 2 ) ) + ( xs[1] << LEN_INDICE ) + xs[0];
660 9769063 : x[1] = xs[2] >> 2;
661 :
662 : /* find number of bits of x[0] and x[1] */
663 9769063 : nb_x1 = get_no_bits( x[1] );
664 :
665 : /* take the first 32 bits */
666 9769063 : if ( nb_x1 > 0 )
667 : {
668 9769063 : x_tmp = ( x[1] << ( 32 - nb_x1 ) ) + ( x[0] >> nb_x1 );
669 9769063 : q = (UWord32) ( x_tmp / y + 0.5 );
670 9769063 : r = x_tmp - q * y; /* this is the first reminder */
671 9769063 : r = ( r << nb_x1 ) + ( x[0] & ( ( 1 << nb_x1 ) - 1 ) );
672 :
673 9769063 : q1 = (UWord32) ( r / y + 0.5 );
674 9769063 : *result = ( q << nb_x1 ) + q1;
675 9769063 : *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 9769063 : return;
686 : }
687 :
688 :
689 : /*-----------------------------------------------------------------*
690 : * create_offset()
691 : *
692 : *
693 : *-----------------------------------------------------------------*/
694 :
695 13753244 : 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 13753244 : if ( prediction_flag == 0 )
704 : {
705 : /* safety_net */
706 1992940 : tmp = no_lead_idx[mode][0];
707 1992940 : if ( ( tmp <= LIMIT_LEADER ) && ( tmp < no_lead_idx[mode][1] - 2 ) )
708 : {
709 0 : tmp += DELTA_LEADER;
710 : }
711 1992940 : make_offset_scale( table_no_cv, leaders_short[tmp], MAX_NO_SCALES, offset_scale1 );
712 1992940 : make_offset_scale( table_no_cv, leaders_short[no_lead_idx[mode][1]], MAX_NO_SCALES, offset_scale2 );
713 : }
714 : else
715 : {
716 11760304 : tmp = no_lead_p_idx[mode][0];
717 11760304 : tmp1 = no_lead_p_idx[mode][1];
718 11760304 : if ( ( tmp <= LIMIT_LEADER ) && ( tmp < tmp1 - 2 ) )
719 : {
720 308205 : tmp += DELTA_LEADER;
721 : }
722 :
723 11760304 : if ( ( tmp == LIMIT_LEADER ) && ( tmp1 == 0 ) )
724 : {
725 0 : tmp += DELTA_LEADER;
726 0 : tmp1 = DELTA_LEADER;
727 : }
728 :
729 11760304 : make_offset_scale( table_no_cv, leaders_short[tmp], MAX_NO_SCALES, offset_scale1 );
730 11760304 : make_offset_scale( table_no_cv, leaders_short[tmp1], MAX_NO_SCALES, offset_scale2 );
731 : }
732 :
733 13753244 : return;
734 : }
735 :
736 : /*-----------------------------------------------------------------*
737 : * sort_desc_ind()
738 : *
739 : * sorts in descending order and computes indices in the sorted vector
740 : *-----------------------------------------------------------------*/
741 15777921 : 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 140969018 : for ( i = 0; i < len; i++ )
751 : {
752 125191097 : ind[i] = i;
753 : }
754 15777921 : sorted = 0;
755 102591239 : for ( k = len - 1; k && !sorted; k-- )
756 : {
757 86813318 : sorted = 1;
758 481769001 : for ( i = 0; i < k; i++ )
759 : {
760 394955683 : if ( s[i] < s[i + 1] )
761 : {
762 214889106 : sorted = 0;
763 214889106 : t = s[i];
764 214889106 : s[i] = s[i + 1];
765 214889106 : s[i + 1] = t;
766 214889106 : a = ind[i];
767 214889106 : ind[i] = ind[i + 1];
768 214889106 : ind[i + 1] = a;
769 : }
770 : }
771 : }
772 :
773 15777921 : return;
774 : }
775 :
776 : /*-----------------------------------------------------------------*
777 : * deindex_lvq_SHB()
778 : *
779 : *
780 : *-----------------------------------------------------------------*/
781 :
782 23365 : 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 23365 : if ( mode == 0 )
796 : {
797 23365 : p_no_lead = &no_lead_BWE[( nbits - mslvq_SHB_min_bits[0] ) * 3];
798 23365 : 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 23365 : if ( index == 0 )
808 : {
809 1 : set_zero( out, LATTICE_DIM );
810 : }
811 : else
812 : {
813 : /* create offsets */
814 23364 : offsets[0] = 0;
815 93456 : for ( i = 0; i < MAX_NO_SCALES; i++ )
816 : {
817 70092 : offsets[i + 1] = table_no_cv[p_no_lead[i]] + offsets[i];
818 : }
819 :
820 : /* find idx_scale */
821 23364 : idx_scale = 0;
822 53190 : while ( (int16_t) i <= MAX_NO_SCALES && index >= offsets[idx_scale] )
823 : {
824 29826 : idx_scale++;
825 : }
826 23364 : idx_scale--;
827 23364 : index -= offsets[idx_scale];
828 :
829 : /* find idx_leader */
830 23364 : i = 1;
831 157450 : while ( index > table_no_cv[i] )
832 : {
833 134086 : i++;
834 : }
835 23364 : i = i - 1;
836 :
837 23364 : decode_comb( (Word32) ( index - table_no_cv[i] - 1 ), out, i );
838 :
839 23364 : scale = p_scales[idx_scale];
840 210276 : for ( i = 0; i < LATTICE_DIM; i++ )
841 : {
842 186912 : out[i] *= scale * sigma_BWE[mode * LATTICE_DIM + i];
843 : }
844 : }
845 :
846 23365 : return;
847 : }
|