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 10906423 : void ari_copy_states_fx( TastatEnc *source, TastatEnc *dest )
24 : {
25 10906423 : dest->low = source->low;
26 10906423 : move32();
27 10906423 : dest->high = source->high;
28 10906423 : move32();
29 10906423 : dest->value = source->value;
30 10906423 : move16();
31 10906423 : }
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 19327 : void ari_start_encoding_14bits_fx( TastatEnc *s )
46 : {
47 19327 : s->low = L_deposit_l( 0 );
48 19327 : s->high = ari_q4new + 1;
49 19327 : move32();
50 19327 : s->value = 0;
51 19327 : move16();
52 19327 : }
53 :
54 : /*---------------------------------------------------------------
55 : * Ari encoder 14 bits routines
56 : * Returns: new bit-stream position
57 : *-------------------------------------------------------------*/
58 19864825 : 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 19864825 : assert( bit == 0 || bit == 1 );
66 19864825 : ptr[bp++] = bit; /* send initially a zero or one Q0*/
67 19864825 : move16();
68 19864825 : bit = s_xor( bit, 1 ); /* invert bit to send */
69 28338276 : FOR( ; bits_to_follow > 0; bits_to_follow-- )
70 : {
71 8473451 : ptr[bp++] = bit; /* send inverted bit Q0*/
72 8473451 : move16();
73 : }
74 19864825 : 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 1902 : Word16 ari_done_encoding_14bits_fx(
88 : Word16 *ptr, /* Q0 */
89 : Word16 bp, /* Q0 */
90 : TastatEnc *s )
91 : {
92 : Word16 bit;
93 :
94 1902 : bit = 0;
95 1902 : move16();
96 1902 : if ( GE_32( s->low, ari_q1new ) )
97 : {
98 756 : bit = s_xor( bit, 1 );
99 : }
100 1902 : 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 77894 : 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 77894 : high = L_add( s->high, 0 );
135 77894 : low = L_add( s->low, 0 );
136 77894 : range = L_sub( high, low ); /* Q0 */
137 :
138 77894 : L_temp1 = L_shl( range, 15 - stat_bitsnew /*both are constants*/ );
139 77894 : Mpy_32_16_ss( L_temp1, cum_freq[symbol + 1], &L_temp2, &temp );
140 77894 : if ( symbol != 0 ) /* when symbol is 0, range remains unchanged */
141 : {
142 38776 : Mpy_32_16_ss( L_temp1, cum_freq[symbol], &range, &temp );
143 : }
144 77894 : high = L_add( low, range ); /* Q0 */
145 77894 : low = L_add( low, L_temp2 ); /* Q0 */
146 :
147 77894 : bits_to_follow = s->value; /* Q0 */
148 77894 : move16();
149 :
150 241139 : FOR( i = 0; i < 0x7FFF; i++ )
151 : {
152 241139 : IF( LE_32( high, ari_q2new ) )
153 : {
154 60342 : bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 0 ); /* Q0 */
155 60342 : bits_to_follow = 0;
156 60342 : move16();
157 : }
158 180797 : ELSE IF( GE_32( low, ari_q2new ) )
159 : {
160 57815 : bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 1 ); /* Q0 */
161 57815 : bits_to_follow = 0;
162 57815 : move16();
163 57815 : low = L_sub( low, ari_q2new ); /* Q0 */
164 57815 : high = L_sub( high, ari_q2new ); /* Subtract offset to top. Q0*/
165 : }
166 : ELSE
167 : {
168 122982 : test();
169 122982 : IF( GE_32( low, ari_q1new ) && LE_32( high, ari_q3new ) )
170 : {
171 : /* Output an opposite bit */
172 : /* later if in middle half. */
173 45088 : bits_to_follow = add( bits_to_follow, 1 ); /* Q0 */
174 45088 : low = L_sub( low, ari_q1new ); /* Subtract offset to middle Q0*/
175 45088 : high = L_sub( high, ari_q1new ); /* Q0 */
176 : }
177 : ELSE
178 : {
179 : BREAK; /* Otherwise exit loop. */
180 : }
181 : }
182 :
183 163245 : low = L_add( low, low ); /* Q0 */
184 163245 : high = L_add( high, high ); /* Scale up code range. Q0*/
185 : }
186 :
187 77894 : s->low = low; /* Q0 */
188 77894 : move32();
189 77894 : s->high = high; /* Q0 */
190 77894 : move32();
191 77894 : s->value = bits_to_follow; /* Q0 */
192 77894 : move16();
193 :
194 77894 : return bp;
195 : }
196 : /*------------------------------------------------------------------------
197 : * Function: ari_encode_overflow_fx()
198 : *
199 : *
200 : *-------------------------------------------------------------------------*/
201 10824046 : Word16 ari_encode_overflow_fx( TastatEnc *s )
202 : {
203 10824046 : 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 11331031 : 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 11331031 : bits_to_follow = s->value;
223 11331031 : move16();
224 :
225 : /* while there are more than 16 bits left */
226 11331031 : tmp = sub( 16, bits ); /* Q0 */
227 13795727 : WHILE( add( add( bp, bits_to_follow ), tmp ) < 0 )
228 : {
229 5968175 : IF( LE_32( high, ari_q2new ) )
230 : {
231 771814 : bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 0 ); /* Q0 */
232 771814 : bits_to_follow = 0;
233 771814 : move16();
234 : }
235 5196361 : ELSE IF( GE_32( low, ari_q2new ) )
236 : {
237 836107 : bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 1 ); /* Q0 */
238 836107 : bits_to_follow = 0;
239 836107 : move16();
240 836107 : low = L_sub( low, ari_q2new ); /* Q0 */
241 836107 : high = L_sub( high, ari_q2new ); /* Subtract offset to top. Q0*/
242 : }
243 : ELSE
244 : {
245 4360254 : test();
246 4360254 : IF( GE_32( low, ari_q1new ) && LE_32( high, ari_q3new ) )
247 : {
248 : /* Output an opposite bit */
249 : /* later if in middle half. */
250 856775 : bits_to_follow = add( bits_to_follow, 1 ); /* Q0 */
251 856775 : low = L_sub( low, ari_q1new ); /* Subtract offset to middle Q0*/
252 856775 : high = L_sub( high, ari_q1new ); /* Q0 */
253 : }
254 : ELSE
255 : {
256 : BREAK; /* Otherwise exit loop. */
257 : }
258 : }
259 :
260 2464696 : low = L_add( low, low ); /* Q0 */
261 2464696 : high = L_add( high, high ); /* Scale up code range. Q0*/
262 : }
263 :
264 11331031 : s->low = low; /* Q0 */
265 11331031 : move32();
266 11331031 : s->high = high; /* Q0 */
267 11331031 : move32();
268 11331031 : s->value = bits_to_follow; /* Q0 */
269 11331031 : move16();
270 :
271 11331031 : return bp;
272 : }
273 :
274 : /*------------------------------------------------------------------------
275 : * Function: ari_encode_14bits_range_fx
276 : *
277 : * Encode an cumulative frequency interval.
278 : *-------------------------------------------------------------------------*/
279 :
280 10824046 : 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 10824046 : range = L_sub( s->high, s->low ); /* Q0 */
292 :
293 10824046 : high = L_add( s->low, mul_sbc_14bits( range, cum_freq_high ) ); /* Q0 */
294 10824046 : low = L_add( s->low, mul_sbc_14bits( range, cum_freq_low ) ); /* Q0 */
295 :
296 10824046 : 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 506985 : 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 506985 : high = L_add( s->high, 0 );
317 506985 : low = L_add( s->low, 0 );
318 506985 : range = L_sub( high, low ); /* Q0 */
319 :
320 506985 : L_tmp = L_shr( range, 1 );
321 506985 : if ( sign != 0 )
322 : {
323 253787 : high = L_add( low, L_tmp ); /* Q0 */
324 : }
325 506985 : if ( sign == 0 )
326 : {
327 253198 : low = L_add( low, L_tmp ); /* Q0 */
328 : }
329 :
330 506985 : 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 17425 : 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 17425 : tmp = sub( bits, 16 ); /* Q0 */
347 17425 : 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 17425 : high = extract_l( L_sub( s->high, 1 ) ); /* Q0 */
353 :
354 17425 : bp = ari_put_bit_plus_follow( ptr, bp, s->value, lshr( high, 15 ) ); /* Q0 */
355 17425 : high = lshl( high, 1 );
356 :
357 17425 : tmp = s_min( 15, sub( bits, bp ) );
358 278800 : FOR( k = 0; k < tmp; ++k )
359 : {
360 261375 : ptr[bp++] = lshr( high, 15 ); /* Q0 */
361 261375 : move16();
362 261375 : high = lshl( high, 1 );
363 : }
364 :
365 17425 : return bp;
366 : }
367 :
368 2097192 : void ari_start_encoding_14bits_ivas_fx(
369 : Tastat *s )
370 : {
371 : /* : addressing is made with walking pointer s */
372 2097192 : s->low = 0;
373 2097192 : s->high = ari_q4new;
374 2097192 : s->bits_to_follow = 0;
375 2097192 : move32();
376 2097192 : move32();
377 2097192 : move32();
378 :
379 2097192 : return;
380 : }
381 :
382 : /*---------------------------------------------------------------
383 : * ari_done_encoding_14bits_ivas_fx()
384 : *
385 : * Finish ArCo encoding
386 : *-------------------------------------------------------------*/
387 :
388 1201947 : Word16 ari_done_encoding_14bits_ivas_fx(
389 : Word16 *ptr, /* Q0 */
390 : Word16 bp, /* Q0 */
391 : Tastat *s )
392 : {
393 : Word16 bit;
394 :
395 1201947 : bit = 0;
396 1201947 : move16();
397 1201947 : IF( GE_32( s->low, ari_q1new ) )
398 : {
399 436505 : bit = s_xor( bit, 1 );
400 : }
401 1201947 : 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 6266094 : 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 6266094 : high = L_add( s->high, 0 );
428 6266094 : low = L_add( s->low, 0 );
429 6266094 : range = L_add( L_sub( high, low ), 1 ); /* Q0 */
430 :
431 6266094 : L_temp1 = L_shl( range, 15 - stat_bitsnew /*both are constants*/ );
432 6266094 : Mpy_32_16_ss( L_temp1, cum_freq[symbol + 1], &L_temp2, &temp );
433 6266094 : IF( symbol != 0 ) /* when symbol is 0, range remains unchanged */
434 : {
435 6245456 : Mpy_32_16_ss( L_temp1, cum_freq[symbol], &range, &temp );
436 : }
437 6266094 : high = L_sub( L_add( low, range ), 1 ); /* Q0 */
438 6266094 : low = L_add( low, L_temp2 ); /* Q0 */
439 :
440 6266094 : assert( s->bits_to_follow <= MAX_16 );
441 6266094 : bits_to_follow = extract_l( s->bits_to_follow ); /* Q0 */
442 :
443 27085493 : FOR( i = 0; i < 0x7FFF; i++ )
444 : {
445 27085493 : IF( LT_32( high, ari_q2new ) )
446 : {
447 7760003 : bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 0 ); /* Q0 */
448 7760003 : bits_to_follow = 0;
449 7760003 : move16();
450 : }
451 19325490 : ELSE IF( GE_32( low, ari_q2new ) )
452 : {
453 7932222 : bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 1 ); /* Q0 */
454 7932222 : bits_to_follow = 0;
455 7932222 : move16();
456 7932222 : low = L_sub( low, ari_q2new );
457 7932222 : high = L_sub( high, ari_q2new ); /* Subtract offset to top. */
458 : }
459 : ELSE
460 : {
461 11393268 : test();
462 11393268 : IF( GE_32( low, ari_q1new ) && LT_32( high, ari_q3new ) )
463 : {
464 : /* Output an opposite bit */
465 : /* later if in middle half. */
466 5127174 : bits_to_follow = add( bits_to_follow, 1 ); /* Q0 */
467 5127174 : low = L_sub( low, ari_q1new ); /* Subtract offset to middle*/
468 5127174 : high = L_sub( high, ari_q1new );
469 : }
470 : ELSE
471 : {
472 : BREAK; /* Otherwise exit loop. */
473 : }
474 : }
475 20819399 : low = L_add( low, low ); /* Q0 */
476 20819399 : high = L_add( L_add( high, high ), 1 ); /* Scale up code range. Q0*/
477 : }
478 :
479 6266094 : s->low = low; /* Q0 */
480 6266094 : move32();
481 6266094 : s->high = high; /* Q0 */
482 6266094 : move32();
483 6266094 : s->bits_to_follow = bits_to_follow; /* Q0 */
484 6266094 : move32();
485 :
486 6266094 : return bp;
487 : }
488 :
489 :
490 : /*------------------------------------------------------------------------
491 : * Function: ari_encode_14bits_high_low_fx()
492 : *
493 : *-------------------------------------------------------------------------*/
494 :
495 2435816 : 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 2435816 : bits_to_follow = extract_l( s->bits_to_follow ); /* Q0 */
507 2435816 : move16();
508 :
509 : /* while there are more than 16 bits left */
510 2435816 : tmp = sub( 16, bits ); /* Q0 */
511 4901625 : WHILE( add( add( bp, bits_to_follow ), tmp ) < 0 )
512 : {
513 4901625 : IF( LE_32( high, ari_q2new ) )
514 : {
515 449104 : bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 0 ); /* Q0 */
516 449104 : bits_to_follow = 0;
517 449104 : move16();
518 : }
519 4452521 : ELSE IF( GE_32( low, ari_q2new ) )
520 : {
521 776144 : bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 1 ); /* Q0 */
522 776144 : bits_to_follow = 0;
523 776144 : move16();
524 776144 : low = L_sub( low, ari_q2new ); /* Q0 */
525 776144 : high = L_sub( high, ari_q2new ); /* Subtract offset to top. Q0*/
526 : }
527 : ELSE
528 : {
529 3676377 : test();
530 3676377 : IF( GE_32( low, ari_q1new ) && LE_32( high, ari_q3new ) )
531 : {
532 : /* Output an opposite bit */
533 : /* later if in middle half. */
534 1240561 : bits_to_follow = add( bits_to_follow, 1 ); /* Q0 */
535 1240561 : low = L_sub( low, ari_q1new ); /* Subtract offset to middle Q0*/
536 1240561 : high = L_sub( high, ari_q1new ); /* Q0 */
537 : }
538 : ELSE
539 : {
540 : BREAK; /* Otherwise exit loop. */
541 : }
542 : }
543 :
544 2465809 : low = L_add( low, low );
545 2465809 : high = L_add( high, high ); /* Scale up code range. */
546 : }
547 :
548 2435816 : s->low = low; /* Q0 */
549 2435816 : move32();
550 2435816 : s->high = L_sub( high, 1 ); /* Q0 */
551 2435816 : move32();
552 2435816 : s->bits_to_follow = bits_to_follow; /* Q0 */
553 2435816 : move16();
554 :
555 2435816 : return bp;
556 : }
557 :
558 : /*------------------------------------------------------------------------
559 : * Function: ari_encode_14bits_sign_ivas_fx()
560 : *
561 : * Encode a sign with equal probabilities.
562 : *-------------------------------------------------------------------------*/
563 2435816 : 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 2435816 : high = L_add( s->high, 1 );
575 2435816 : low = L_add( s->low, 0 );
576 2435816 : range = L_sub( high, low ); /* Q0 */
577 :
578 2435816 : L_tmp = L_shr( range, 1 );
579 2435816 : if ( sign != 0 )
580 : {
581 1222058 : high = L_add( low, L_tmp ); /* Q0 */
582 : }
583 2435816 : if ( sign == 0 )
584 : {
585 1213758 : low = L_add( low, L_tmp ); /* Q0 */
586 : }
587 :
588 2435816 : return ari_encode_14bits_high_low_ivas_fx( ptr, bp, extract_l( bits ), s, high, low );
589 : }
|