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