Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdint.h>
6 : #include "assert.h"
7 : #include "options.h"
8 : //#include "basop_mpy.h"
9 : #include "cnst.h"
10 : #include "rom_com.h"
11 : //#include "prot_fx.h"
12 : #include "prot_fx.h" /* Function prototypes */
13 : #include "prot_fx_enc.h" /* Function prototypes */
14 :
15 : /*---------------------------------------------------------------
16 : * ari_copy_states_fx()
17 : *
18 : * \param[i] source
19 : * \param[o] dest
20 : *
21 : * \return none
22 : *-------------------------------------------------------------*/
23 10954944 : void ari_copy_states_fx( TastatEnc *source, TastatEnc *dest )
24 : {
25 10954944 : dest->low = source->low;
26 10954944 : move32();
27 10954944 : dest->high = source->high;
28 10954944 : move32();
29 10954944 : dest->value = source->value;
30 10954944 : move16();
31 10954944 : }
32 :
33 : /*---------------------------------------------------------------
34 : Ari encoder 14 bits routines
35 : -------------------------------------------------------------*/
36 :
37 :
38 : /*---------------------------------------------------------------
39 : * ari_start_encoding_14bits_fx()
40 : *
41 : * \param[o] s
42 : *
43 : * \return none
44 : *-------------------------------------------------------------*/
45 19481 : void ari_start_encoding_14bits_fx( TastatEnc *s )
46 : {
47 19481 : s->low = L_deposit_l( 0 );
48 19481 : s->high = ari_q4new + 1;
49 19481 : move32();
50 19481 : s->value = 0;
51 19481 : move16();
52 19481 : }
53 :
54 : /*---------------------------------------------------------------
55 : * Ari encoder 14 bits routines
56 : * Returns: new bit-stream position
57 : *-------------------------------------------------------------*/
58 19258676 : Word16 ari_put_bit_plus_follow(
59 : Word16 ptr[], /* o: bit-stream Q0*/
60 : Word16 bp, /* i: bit-stream position Q0*/
61 : Word16 bits_to_follow, /* i: number of opposite bits to follow 'bit' Q0*/
62 : Word16 bit /* i: bit to send Q0*/
63 : )
64 : {
65 19258676 : assert( bit == 0 || bit == 1 );
66 19258676 : ptr[bp++] = bit; /* send initially a zero or one Q0*/
67 19258676 : move16();
68 19258676 : bit = s_xor( bit, 1 ); /* invert bit to send */
69 27502266 : FOR( ; bits_to_follow > 0; bits_to_follow-- )
70 : {
71 8243590 : ptr[bp++] = bit; /* send inverted bit Q0*/
72 8243590 : move16();
73 : }
74 19258676 : return bp;
75 : }
76 :
77 : /*-------------------------------------------------------------- -
78 : *ari_done_encoding_14bits_fx()
79 : *
80 : *
81 : * \param[o] ptr
82 : * \param[i] bp
83 : * \param[i] s
84 : *
85 : * \return bit consumption
86 : * ------------------------------------------------------------ - */
87 1986 : Word16 ari_done_encoding_14bits_fx(
88 : Word16 *ptr, /* Q0 */
89 : Word16 bp, /* Q0 */
90 : TastatEnc *s )
91 : {
92 : Word16 bit;
93 :
94 1986 : bit = 0;
95 1986 : move16();
96 1986 : if ( GE_32( s->low, ari_q1new ) )
97 : {
98 762 : bit = s_xor( bit, 1 );
99 : }
100 1986 : return ari_put_bit_plus_follow( ptr, bp, add( s->value, 1 ), bit );
101 : }
102 :
103 :
104 : /*---------------------------------------------------------------
105 : *ari_encode_14bits_ext_fx()
106 : *
107 : * encode function for extended proba tables : less branches needed for coding
108 : *
109 : *
110 : * \param[o] ptr
111 : * \param[i] bp
112 : * \param[i/o] s
113 : * \param[i] symbol
114 : * \param[i] cum_freq
115 : *
116 : * \return bit consumption
117 : * ------------------------------------------------------------ - */
118 82353 : Word16 ari_encode_14bits_ext_fx(
119 : Word16 *ptr, /* Q0 */
120 : Word16 bp, /* Q0 */
121 : TastatEnc *s,
122 : Word32 symbol, /* Q0 */
123 : UWord16 const *cum_freq /* Q0 */
124 : )
125 : {
126 : Word32 low;
127 : Word32 high;
128 : Word32 range;
129 : Word16 bits_to_follow;
130 : Word16 i;
131 : UWord16 temp;
132 : Word32 L_temp1, L_temp2;
133 :
134 82353 : high = L_add( s->high, 0 );
135 82353 : low = L_add( s->low, 0 );
136 82353 : range = L_sub( high, low ); /* Q0 */
137 :
138 82353 : L_temp1 = L_shl( range, 15 - stat_bitsnew /*both are constants*/ );
139 82353 : Mpy_32_16_ss( L_temp1, cum_freq[symbol + 1], &L_temp2, &temp );
140 82353 : if ( symbol != 0 ) /* when symbol is 0, range remains unchanged */
141 : {
142 41049 : Mpy_32_16_ss( L_temp1, cum_freq[symbol], &range, &temp );
143 : }
144 82353 : high = L_add( low, range ); /* Q0 */
145 82353 : low = L_add( low, L_temp2 ); /* Q0 */
146 :
147 82353 : bits_to_follow = s->value; /* Q0 */
148 82353 : move16();
149 :
150 255056 : FOR( i = 0; i < 0x7FFF; i++ )
151 : {
152 255056 : IF( LE_32( high, ari_q2new ) )
153 : {
154 63984 : bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 0 ); /* Q0 */
155 63984 : bits_to_follow = 0;
156 63984 : move16();
157 : }
158 191072 : ELSE IF( GE_32( low, ari_q2new ) )
159 : {
160 61243 : bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 1 ); /* Q0 */
161 61243 : bits_to_follow = 0;
162 61243 : move16();
163 61243 : low = L_sub( low, ari_q2new ); /* Q0 */
164 61243 : high = L_sub( high, ari_q2new ); /* Subtract offset to top. Q0*/
165 : }
166 : ELSE
167 : {
168 129829 : test();
169 129829 : IF( GE_32( low, ari_q1new ) && LE_32( high, ari_q3new ) )
170 : {
171 : /* Output an opposite bit */
172 : /* later if in middle half. */
173 47476 : bits_to_follow = add( bits_to_follow, 1 ); /* Q0 */
174 47476 : low = L_sub( low, ari_q1new ); /* Subtract offset to middle Q0*/
175 47476 : high = L_sub( high, ari_q1new ); /* Q0 */
176 : }
177 : ELSE
178 : {
179 : BREAK; /* Otherwise exit loop. */
180 : }
181 : }
182 :
183 172703 : low = L_add( low, low ); /* Q0 */
184 172703 : high = L_add( high, high ); /* Scale up code range. Q0*/
185 : }
186 :
187 82353 : s->low = low; /* Q0 */
188 82353 : move32();
189 82353 : s->high = high; /* Q0 */
190 82353 : move32();
191 82353 : s->value = bits_to_follow; /* Q0 */
192 82353 : move16();
193 :
194 82353 : return bp;
195 : }
196 : /*------------------------------------------------------------------------
197 : * Function: ari_encode_overflow_fx()
198 : *
199 : *
200 : *-------------------------------------------------------------------------*/
201 10868688 : Word16 ari_encode_overflow_fx( TastatEnc *s )
202 : {
203 10868688 : return L_sub( L_sub( s->high, 1 ), s->low ) <= 0;
204 : }
205 :
206 : /*------------------------------------------------------------------------
207 : * Function: ari_encode_14bits_high_low()
208 : *
209 : *
210 : *-------------------------------------------------------------------------*/
211 11378411 : static Word16 ari_encode_14bits_high_low(
212 : Word16 *ptr, /* Q0 */
213 : Word16 bp, /* Q0 */
214 : Word16 bits, /* Q0 */
215 : TastatEnc *s,
216 : Word32 high, /* Q0 */
217 : Word32 low /* Q0 */
218 : )
219 : {
220 : Word16 bits_to_follow, tmp;
221 :
222 11378411 : bits_to_follow = s->value;
223 11378411 : move16();
224 :
225 : /* while there are more than 16 bits left */
226 11378411 : tmp = sub( 16, bits ); /* Q0 */
227 13856307 : WHILE( add( add( bp, bits_to_follow ), tmp ) < 0 )
228 : {
229 6008409 : IF( LE_32( high, ari_q2new ) )
230 : {
231 774777 : bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 0 ); /* Q0 */
232 774777 : bits_to_follow = 0;
233 774777 : move16();
234 : }
235 5233632 : ELSE IF( GE_32( low, ari_q2new ) )
236 : {
237 842076 : bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 1 ); /* Q0 */
238 842076 : bits_to_follow = 0;
239 842076 : move16();
240 842076 : low = L_sub( low, ari_q2new ); /* Q0 */
241 842076 : high = L_sub( high, ari_q2new ); /* Subtract offset to top. Q0*/
242 : }
243 : ELSE
244 : {
245 4391556 : test();
246 4391556 : IF( GE_32( low, ari_q1new ) && LE_32( high, ari_q3new ) )
247 : {
248 : /* Output an opposite bit */
249 : /* later if in middle half. */
250 861043 : bits_to_follow = add( bits_to_follow, 1 ); /* Q0 */
251 861043 : low = L_sub( low, ari_q1new ); /* Subtract offset to middle Q0*/
252 861043 : high = L_sub( high, ari_q1new ); /* Q0 */
253 : }
254 : ELSE
255 : {
256 : BREAK; /* Otherwise exit loop. */
257 : }
258 : }
259 :
260 2477896 : low = L_add( low, low ); /* Q0 */
261 2477896 : high = L_add( high, high ); /* Scale up code range. Q0*/
262 : }
263 :
264 11378411 : s->low = low; /* Q0 */
265 11378411 : move32();
266 11378411 : s->high = high; /* Q0 */
267 11378411 : move32();
268 11378411 : s->value = bits_to_follow; /* Q0 */
269 11378411 : move16();
270 :
271 11378411 : return bp;
272 : }
273 :
274 : /*------------------------------------------------------------------------
275 : * Function: ari_encode_14bits_range_fx
276 : *
277 : * Encode an cumulative frequency interval.
278 : *-------------------------------------------------------------------------*/
279 :
280 10868688 : Word16 ari_encode_14bits_range_fx(
281 : Word16 *ptr, /* Q0 */
282 : Word16 bp, /* Q0 */
283 : Word16 bits, /* Q0 */
284 : TastatEnc *s,
285 : Word16 cum_freq_low, /* Q0 */
286 : Word16 cum_freq_high /* Q0 */
287 : )
288 : {
289 : Word32 low, high, range;
290 :
291 10868688 : range = L_sub( s->high, s->low ); /* Q0 */
292 :
293 10868688 : high = L_add( s->low, mul_sbc_14bits( range, cum_freq_high ) ); /* Q0 */
294 10868688 : low = L_add( s->low, mul_sbc_14bits( range, cum_freq_low ) ); /* Q0 */
295 :
296 10868688 : return ari_encode_14bits_high_low( ptr, bp, bits, s, high, low );
297 : }
298 :
299 :
300 : /*------------------------------------------------------------------------
301 : * Function: ari_encode_14bits_sign_fx
302 : *
303 : * Encode a sign with equal probabilities.
304 : *-------------------------------------------------------------------------*/
305 509723 : Word16 ari_encode_14bits_sign_fx(
306 : Word16 *ptr, /* Q0 */
307 : Word16 bp, /* Q0 */
308 : Word16 bits, /* Q0 */
309 : TastatEnc *s,
310 : Word16 sign /* Q0 */
311 : )
312 : {
313 : Word32 low, high, range;
314 : Word32 L_tmp;
315 :
316 509723 : high = L_add( s->high, 0 );
317 509723 : low = L_add( s->low, 0 );
318 509723 : range = L_sub( high, low ); /* Q0 */
319 :
320 509723 : L_tmp = L_shr( range, 1 );
321 509723 : if ( sign != 0 )
322 : {
323 255330 : high = L_add( low, L_tmp ); /* Q0 */
324 : }
325 509723 : if ( sign == 0 )
326 : {
327 254393 : low = L_add( low, L_tmp ); /* Q0 */
328 : }
329 :
330 509723 : return ari_encode_14bits_high_low( ptr, bp, bits, s, high, low );
331 : }
332 :
333 : /*------------------------------------------------------------------------
334 : * Function: ari_done_cbr_encoding_14bits_fx
335 : *
336 : * Finish up encoding in CBR mode.
337 : *-------------------------------------------------------------------------*/
338 17495 : Word16 ari_done_cbr_encoding_14bits_fx(
339 : Word16 *ptr, /* Q0 */
340 : Word16 bp, /* Q0 */
341 : Word16 bits, /* Q0 */
342 : TastatEnc *s )
343 : {
344 : Word16 high, tmp, k;
345 :
346 17495 : tmp = sub( bits, 16 ); /* Q0 */
347 17495 : WHILE( GT_16( sub( tmp, bp ), s->value ) )
348 : {
349 0 : bp = ari_encode_14bits_sign_fx( ptr, bp, bits, s, 0 ); /* Q0 */
350 : }
351 :
352 17495 : high = extract_l( L_sub( s->high, 1 ) ); /* Q0 */
353 :
354 17495 : bp = ari_put_bit_plus_follow( ptr, bp, s->value, lshr( high, 15 ) ); /* Q0 */
355 17495 : high = lshl( high, 1 );
356 :
357 17495 : tmp = s_min( 15, sub( bits, bp ) );
358 279920 : FOR( k = 0; k < tmp; ++k )
359 : {
360 262425 : ptr[bp++] = lshr( high, 15 ); /* Q0 */
361 262425 : move16();
362 262425 : high = lshl( high, 1 );
363 : }
364 :
365 17495 : return bp;
366 : }
367 :
368 2039676 : void ari_start_encoding_14bits_ivas_fx(
369 : Tastat *s )
370 : {
371 : /* : addressing is made with walking pointer s */
372 2039676 : s->low = 0;
373 2039676 : s->high = ari_q4new;
374 2039676 : s->bits_to_follow = 0;
375 2039676 : move32();
376 2039676 : move32();
377 2039676 : move32();
378 :
379 2039676 : return;
380 : }
381 :
382 : /*---------------------------------------------------------------
383 : * ari_done_encoding_14bits_ivas_fx()
384 : *
385 : * Finish ArCo encoding
386 : *-------------------------------------------------------------*/
387 :
388 1161041 : Word16 ari_done_encoding_14bits_ivas_fx(
389 : Word16 *ptr, /* Q0 */
390 : Word16 bp, /* Q0 */
391 : Tastat *s )
392 : {
393 : Word16 bit;
394 :
395 1161041 : bit = 0;
396 1161041 : move16();
397 1161041 : IF( GE_32( s->low, ari_q1new ) )
398 : {
399 422639 : bit = s_xor( bit, 1 );
400 : }
401 1161041 : return ari_put_bit_plus_follow( ptr, bp, add( extract_l( s->bits_to_follow ), 1 ), bit );
402 : }
403 :
404 :
405 : /*---------------------------------------------------------------
406 : * ari_encode_14bits_ext_ivas_fx()
407 : *
408 : * encode function for extended proba tables: less branches needed for coding
409 : *
410 : *-------------------------------------------------------------*/
411 6077740 : Word16 ari_encode_14bits_ext_ivas_fx(
412 : Word16 *ptr, /* Q0 */
413 : Word16 bp, /* Q0 */
414 : Tastat *s,
415 : Word32 symbol, /* Q0 */
416 : UWord16 const *cum_freq /* Q0 */
417 : )
418 : {
419 : Word32 low;
420 : Word32 high;
421 : Word32 range;
422 : Word16 bits_to_follow;
423 : Word16 i;
424 : UWord16 temp;
425 : Word32 L_temp1, L_temp2;
426 :
427 6077740 : high = L_add( s->high, 0 );
428 6077740 : low = L_add( s->low, 0 );
429 6077740 : range = L_add( L_sub( high, low ), 1 ); /* Q0 */
430 :
431 6077740 : L_temp1 = L_shl( range, 15 - stat_bitsnew /*both are constants*/ );
432 6077740 : Mpy_32_16_ss( L_temp1, cum_freq[symbol + 1], &L_temp2, &temp );
433 6077740 : IF( symbol != 0 ) /* when symbol is 0, range remains unchanged */
434 : {
435 6061319 : Mpy_32_16_ss( L_temp1, cum_freq[symbol], &range, &temp );
436 : }
437 6077740 : high = L_sub( L_add( low, range ), 1 ); /* Q0 */
438 6077740 : low = L_add( low, L_temp2 ); /* Q0 */
439 :
440 6077740 : assert( s->bits_to_follow <= MAX_16 );
441 6077740 : bits_to_follow = extract_l( s->bits_to_follow ); /* Q0 */
442 :
443 26198571 : FOR( i = 0; i < 0x7FFF; i++ )
444 : {
445 26198571 : IF( LT_32( high, ari_q2new ) )
446 : {
447 7495923 : bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 0 ); /* Q0 */
448 7495923 : bits_to_follow = 0;
449 7495923 : move16();
450 : }
451 18702648 : ELSE IF( GE_32( low, ari_q2new ) )
452 : {
453 7656347 : bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 1 ); /* Q0 */
454 7656347 : bits_to_follow = 0;
455 7656347 : move16();
456 7656347 : low = L_sub( low, ari_q2new );
457 7656347 : high = L_sub( high, ari_q2new ); /* Subtract offset to top. */
458 : }
459 : ELSE
460 : {
461 11046301 : test();
462 11046301 : IF( GE_32( low, ari_q1new ) && LT_32( high, ari_q3new ) )
463 : {
464 : /* Output an opposite bit */
465 : /* later if in middle half. */
466 4968561 : bits_to_follow = add( bits_to_follow, 1 ); /* Q0 */
467 4968561 : low = L_sub( low, ari_q1new ); /* Subtract offset to middle*/
468 4968561 : high = L_sub( high, ari_q1new );
469 : }
470 : ELSE
471 : {
472 : BREAK; /* Otherwise exit loop. */
473 : }
474 : }
475 20120831 : low = L_add( low, low ); /* Q0 */
476 20120831 : high = L_add( L_add( high, high ), 1 ); /* Scale up code range. Q0*/
477 : }
478 :
479 6077740 : s->low = low; /* Q0 */
480 6077740 : move32();
481 6077740 : s->high = high; /* Q0 */
482 6077740 : move32();
483 6077740 : s->bits_to_follow = bits_to_follow; /* Q0 */
484 6077740 : move32();
485 :
486 6077740 : return bp;
487 : }
488 :
489 :
490 : /*------------------------------------------------------------------------
491 : * Function: ari_encode_14bits_high_low_fx()
492 : *
493 : *-------------------------------------------------------------------------*/
494 :
495 2353908 : static Word16 ari_encode_14bits_high_low_ivas_fx(
496 : Word16 *ptr, /* Q0 */
497 : Word16 bp, /* Q0 */
498 : Word16 bits, /* Q0 */
499 : Tastat *s,
500 : Word32 high, /* Q0 */
501 : Word32 low /* Q0 */
502 : )
503 : {
504 : Word16 bits_to_follow, tmp;
505 :
506 2353908 : bits_to_follow = extract_l( s->bits_to_follow ); /* Q0 */
507 2353908 : move16();
508 :
509 : /* while there are more than 16 bits left */
510 2353908 : tmp = sub( 16, bits ); /* Q0 */
511 4741191 : WHILE( add( add( bp, bits_to_follow ), tmp ) < 0 )
512 : {
513 4741191 : IF( LE_32( high, ari_q2new ) )
514 : {
515 436289 : bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 0 ); /* Q0 */
516 436289 : bits_to_follow = 0;
517 436289 : move16();
518 : }
519 4304902 : ELSE IF( GE_32( low, ari_q2new ) )
520 : {
521 747515 : bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 1 ); /* Q0 */
522 747515 : bits_to_follow = 0;
523 747515 : move16();
524 747515 : low = L_sub( low, ari_q2new ); /* Q0 */
525 747515 : high = L_sub( high, ari_q2new ); /* Subtract offset to top. Q0*/
526 : }
527 : ELSE
528 : {
529 3557387 : test();
530 3557387 : IF( GE_32( low, ari_q1new ) && LE_32( high, ari_q3new ) )
531 : {
532 : /* Output an opposite bit */
533 : /* later if in middle half. */
534 1203479 : bits_to_follow = add( bits_to_follow, 1 ); /* Q0 */
535 1203479 : low = L_sub( low, ari_q1new ); /* Subtract offset to middle Q0*/
536 1203479 : high = L_sub( high, ari_q1new ); /* Q0 */
537 : }
538 : ELSE
539 : {
540 : BREAK; /* Otherwise exit loop. */
541 : }
542 : }
543 :
544 2387283 : low = L_add( low, low );
545 2387283 : high = L_add( high, high ); /* Scale up code range. */
546 : }
547 :
548 2353908 : s->low = low; /* Q0 */
549 2353908 : move32();
550 2353908 : s->high = L_sub( high, 1 ); /* Q0 */
551 2353908 : move32();
552 2353908 : s->bits_to_follow = bits_to_follow; /* Q0 */
553 2353908 : move16();
554 :
555 2353908 : return bp;
556 : }
557 :
558 : /*------------------------------------------------------------------------
559 : * Function: ari_encode_14bits_sign_ivas_fx()
560 : *
561 : * Encode a sign with equal probabilities.
562 : *-------------------------------------------------------------------------*/
563 2353908 : Word16 ari_encode_14bits_sign_ivas_fx(
564 : Word16 *ptr, /* Q0 */
565 : Word16 bp, /* Q0 */
566 : Word32 bits, /* Q0 */
567 : Tastat *s,
568 : Word16 sign /* Q0 */
569 : )
570 : {
571 : Word32 low, high, range;
572 : Word32 L_tmp;
573 :
574 2353908 : high = L_add( s->high, 1 );
575 2353908 : low = L_add( s->low, 0 );
576 2353908 : range = L_sub( high, low ); /* Q0 */
577 :
578 2353908 : L_tmp = L_shr( range, 1 );
579 2353908 : if ( sign != 0 )
580 : {
581 1182394 : high = L_add( low, L_tmp ); /* Q0 */
582 : }
583 2353908 : if ( sign == 0 )
584 : {
585 1171514 : low = L_add( low, L_tmp ); /* Q0 */
586 : }
587 :
588 2353908 : return ari_encode_14bits_high_low_ivas_fx( ptr, bp, extract_l( bits ), s, high, low );
589 : }
|