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 <assert.h>
38 : #include <stdint.h>
39 : #include "options.h"
40 : #include "cnst.h"
41 : #include "prot.h"
42 : #include "stat_com.h"
43 : #include "basop_util.h"
44 : #include "wmc_auto.h"
45 :
46 :
47 : /*---------------------------------------------------------------
48 : * Ari decode 14 bits routines
49 : -------------------------------------------------------------*/
50 :
51 : /*---------------------------------------------------------------
52 : * ari_start_decoding_14bits()
53 : *
54 : * Start ArCo decoding
55 : *-------------------------------------------------------------*/
56 :
57 1153792 : void ari_start_decoding_14bits(
58 : Decoder_State *st,
59 : Tastat *s )
60 : {
61 : uint32_t val;
62 :
63 1153792 : val = get_next_indice( st, cbitsnew );
64 :
65 1153792 : s->low = 0;
66 1153792 : s->high = ari_q4new;
67 1153792 : s->value = val;
68 :
69 1153792 : return;
70 : }
71 :
72 : /*---------------------------------------------------------------
73 : * ari_start_decoding_14bits_prm()
74 : *
75 : * Start ArCo decoding
76 : *-------------------------------------------------------------*/
77 :
78 17703 : int16_t ari_start_decoding_14bits_prm(
79 : const int16_t *ptr,
80 : int16_t bp,
81 : Tastat *s )
82 : {
83 : int32_t val;
84 : int16_t i;
85 : const int16_t *p;
86 :
87 17703 : val = 0;
88 17703 : p = ptr + bp;
89 300951 : for ( i = 0; i < cbitsnew; i++ )
90 : {
91 283248 : val = ( val << 1 ) | *( p + i );
92 : }
93 :
94 17703 : s->low = 0;
95 17703 : s->high = ari_q4new;
96 17703 : s->value = val;
97 :
98 17703 : bp = bp + i;
99 :
100 17703 : return bp;
101 : }
102 :
103 : /*---------------------------------------------------------------
104 : * ari_decode_14bits_s17_ext()
105 : *
106 : * Only for 17 symbols with new extended Tables:
107 : * based on tri's optimization
108 : * based on extended Tables which need less branches for coding
109 : *-------------------------------------------------------------*/
110 :
111 142470 : void ari_decode_14bits_s17_ext(
112 : Decoder_State *st,
113 : uint16_t *res,
114 : Tastat *s,
115 : const uint16_t *cum_freq )
116 : {
117 : uint16_t symbol;
118 : int32_t low, high;
119 : uint32_t range, value, cum;
120 : const uint16_t *p;
121 :
122 : /* read s->low,high,value sequentially */
123 142470 : low = s->low;
124 142470 : high = s->high;
125 142470 : value = s->value;
126 :
127 142470 : range = high - low + 1; /* keep: tmp=low-1 */
128 142470 : cum = ( ( ( (uint32_t) ( value - low + 1 ) ) << stat_bitsnew ) - ( (uint32_t) 1 ) );
129 :
130 142470 : p = cum_freq;
131 :
132 : /* Note: For each indirect addressing p[i], we assume a tmp pointer init followed by a costfree reading the value */
133 : /* If the value multiplied by range is greater than cum, the pointer p is set to the tmp pointer */
134 : /* tmp_p = p+8; if (tmp_p[0]*range>cum) p = tmp_p; */
135 :
136 142470 : if ( p[8] * range > cum )
137 : {
138 15863 : p += 8;
139 : }
140 : /* */
141 142470 : if ( p[4] * range > cum )
142 : {
143 35278 : p += 4;
144 : }
145 : /* */
146 142470 : if ( p[2] * range > cum )
147 : {
148 16342 : p += 2;
149 : }
150 : /* */
151 142470 : if ( p[1] * range > cum )
152 : {
153 35671 : p += 1;
154 35671 : if ( ( p == cum_freq + 15 ) && ( p[1] * range > cum ) )
155 : {
156 7893 : p += 1;
157 : }
158 : }
159 :
160 142470 : symbol = (uint16_t) ( p - cum_freq );
161 :
162 142470 : high = low + mul_sbc_14bits( range, cum_freq[symbol] ) - 1;
163 142470 : low += mul_sbc_14bits( range, cum_freq[symbol + 1] );
164 :
165 : for ( ;; )
166 : {
167 416835 : if ( high >= ari_q2new )
168 : {
169 316044 : if ( low >= ari_q2new )
170 : {
171 96332 : value -= ari_q2new;
172 96332 : low -= ari_q2new;
173 96332 : high -= ari_q2new;
174 : }
175 : else
176 : {
177 219712 : if ( low >= ari_q1new && high < ari_q3new )
178 : {
179 77242 : value -= ari_q1new;
180 77242 : low -= ari_q1new;
181 77242 : high -= ari_q1new;
182 : }
183 : else
184 : {
185 : break;
186 : }
187 : }
188 : }
189 274365 : low += low;
190 274365 : high += high + 1;
191 :
192 274365 : value = ( value << 1 ) | get_next_indice_1( st );
193 : }
194 :
195 142470 : s->low = low;
196 142470 : s->high = high;
197 142470 : s->value = value;
198 :
199 142470 : *res = symbol;
200 :
201 142470 : return;
202 : }
203 :
204 :
205 : /*---------------------------------------------------------------
206 : * ari_decode_14bits_s27_ext()
207 : *
208 : * Only for 17 symbols with new extended Tables:
209 : * based on tri's optimization
210 : * based on extended Tables which need less branches for coding
211 : * copied from ari_decode_14bits_s17_ext, with changes marked
212 : *-------------------------------------------------------------*/
213 :
214 5893058 : void ari_decode_14bits_s27_ext(
215 : Decoder_State *st,
216 : uint16_t *res,
217 : Tastat *s,
218 : const uint16_t *cum_freq )
219 : {
220 : uint16_t symbol;
221 : int32_t low, high;
222 : uint32_t range, value, cum;
223 : uint16_t il, ih, im;
224 :
225 : /* read s->low,high,value sequentially */
226 5893058 : low = s->low;
227 5893058 : high = s->high;
228 5893058 : value = s->value;
229 :
230 5893058 : range = high - low + 1; /* keep: tmp=low-1 */
231 5893058 : cum = ( ( ( (uint32_t) ( value - low + 1 ) ) << stat_bitsnew ) - ( (uint32_t) 1 ) );
232 :
233 :
234 : /* Note: For each indirect addressing p[i], we assume a tmp pointer init followed by a costfree reading the value */
235 : /* If the value multiplied by range is greater than cum, the pointer p is set to the tmp pointer */
236 : /* tmp_p = p+8; if (tmp_p[0]*range>cum) p = tmp_p; */
237 :
238 : /* begin change when compared with ari_decode_14bits_s17_ext,
239 : starting with line: if (p[8] * range > cum) { */
240 5893058 : il = 0;
241 5893058 : ih = 27;
242 :
243 : /* do a five step binary search, using the interval [il, ih) */
244 5893058 : im = 13; /* (il + ih) >> 1 */
245 5893058 : if ( cum_freq[im] * range > cum )
246 : {
247 3532032 : il = im;
248 : }
249 : else
250 : {
251 2361026 : ih = im;
252 : }
253 :
254 5893058 : im = ( il + ih ) >> 1;
255 5893058 : if ( cum_freq[im] * range > cum )
256 : {
257 2164619 : il = im;
258 : }
259 : else
260 : {
261 3728439 : ih = im;
262 : }
263 :
264 5893058 : im = ( il + ih ) >> 1;
265 5893058 : if ( cum_freq[im] * range > cum )
266 : {
267 2865679 : il = im;
268 : }
269 : else
270 : {
271 3027379 : ih = im;
272 : }
273 :
274 5893058 : im = ( il + ih ) >> 1;
275 5893058 : if ( cum_freq[im] * range > cum )
276 : {
277 3445147 : il = im;
278 : }
279 : else
280 : {
281 2447911 : ih = im;
282 : }
283 :
284 5893058 : if ( ih - il > 1 ) /* if the interval has more than one symbol */
285 : {
286 : /* here, only ih == il + 2 is possible, which means two symbols in the interval */
287 4992306 : im = il + 1; /* (il + ih) >> 1 */
288 4992306 : if ( cum_freq[im] * range > cum )
289 : {
290 2442845 : il = im;
291 : }
292 : }
293 :
294 5893058 : symbol = il;
295 : /* end change when compared with ari_decode_14bits_s17_ext,
296 : ending with line: symbol = p - cum_freq; */
297 :
298 5893058 : high = low + mul_sbc_14bits( range, cum_freq[symbol] ) - 1;
299 5893058 : low += mul_sbc_14bits( range, cum_freq[symbol + 1] );
300 :
301 : for ( ;; )
302 : {
303 25058090 : if ( high >= ari_q2new )
304 : {
305 17848381 : if ( low >= ari_q2new )
306 : {
307 7108735 : value -= ari_q2new;
308 7108735 : low -= ari_q2new;
309 7108735 : high -= ari_q2new;
310 : }
311 : else
312 : {
313 10739646 : if ( low >= ari_q1new && high < ari_q3new )
314 : {
315 4846588 : value -= ari_q1new;
316 4846588 : low -= ari_q1new;
317 4846588 : high -= ari_q1new;
318 : }
319 : else
320 : {
321 : break;
322 : }
323 : }
324 : }
325 19165032 : low += low;
326 19165032 : high += high + 1;
327 :
328 19165032 : value = ( value << 1 ) | get_next_indice_1( st );
329 : }
330 :
331 5893058 : s->low = low;
332 5893058 : s->high = high;
333 5893058 : s->value = value;
334 :
335 5893058 : *res = symbol;
336 :
337 5893058 : return;
338 : }
339 :
340 :
341 : /*---------------------------------------------------------------
342 : * ari_decode_14bits_bit_ext()
343 : *
344 : * Only for decoding one bit with uniform probability:
345 : * based on tri's optimization
346 : * copied from ari_decode_14bits_s17_ext, with changes marked
347 : * the equivalent cum_freq table used is {16384, 8192, 0}
348 : *-------------------------------------------------------------*/
349 :
350 2301634 : void ari_decode_14bits_bit_ext(
351 : Decoder_State *st,
352 : uint16_t *res,
353 : Tastat *s )
354 : {
355 : uint16_t symbol;
356 : int32_t low, high;
357 : uint32_t range, value, cum;
358 :
359 : /* read s->low,high,value sequentially */
360 2301634 : low = s->low;
361 2301634 : high = s->high;
362 2301634 : value = s->value;
363 :
364 2301634 : range = high - low + 1; /* keep: tmp=low-1 */
365 2301634 : cum = ( ( ( (uint32_t) ( value - low + 1 ) ) << stat_bitsnew ) - ( (uint32_t) 1 ) );
366 :
367 :
368 : /* Note: For each indirect addressing p[i], we assume a tmp pointer init followed by a costfree reading the value */
369 : /* If the value multiplied by range is greater than cum, the pointer p is set to the tmp pointer */
370 : /* tmp_p = p+8; if (tmp_p[0]*range>cum) p = tmp_p; */
371 :
372 : /* begin change when compared with ari_decode_14bits_s17_ext,
373 : starting with line: if (p[8] * range > cum) { */
374 2301634 : symbol = 0;
375 :
376 2301634 : if ( ( range << 13 ) > cum )
377 : {
378 1152736 : symbol = 1;
379 : }
380 :
381 2301634 : if ( symbol == 0 )
382 : {
383 : /* high is unchanged */
384 1148898 : low = low + ( range >> 1 );
385 : }
386 : else
387 : {
388 1152736 : high = low + ( range >> 1 ) - 1;
389 : /* low is unchanged */
390 : }
391 : /* end change when compared with ari_decode_14bits_s17_ext,
392 : ending with line: low += mul_sbc_14bits(range, cum_freq[symbol + 1]); */
393 :
394 :
395 : for ( ;; )
396 : {
397 4618675 : if ( high >= ari_q2new )
398 : {
399 4200326 : if ( low >= ari_q2new )
400 : {
401 740134 : value -= ari_q2new;
402 740134 : low -= ari_q2new;
403 740134 : high -= ari_q2new;
404 : }
405 : else
406 : {
407 3460192 : if ( low >= ari_q1new && high < ari_q3new )
408 : {
409 1158558 : value -= ari_q1new;
410 1158558 : low -= ari_q1new;
411 1158558 : high -= ari_q1new;
412 : }
413 : else
414 : {
415 : break;
416 : }
417 : }
418 : }
419 2317041 : low += low;
420 2317041 : high += high + 1;
421 :
422 2317041 : value = ( value << 1 ) | get_next_indice_1( st );
423 : }
424 :
425 2301634 : s->low = low;
426 2301634 : s->high = high;
427 2301634 : s->value = value;
428 :
429 2301634 : *res = symbol;
430 :
431 2301634 : return;
432 : }
433 :
434 : /*------------------------------------------------------------------------
435 : * Function: ari_decode_14bits_pow()
436 : *
437 : * Decode a symbol which follows the exponential distribution. That is,
438 : * symbols are in the following intervals
439 : *
440 : * p(x = 0) = 1 - exp(- 0.5 * base * 2)
441 : * p(x = q>0) = exp(- (q-0.5)*base* 2) - exp(- (q+0.5)*base*2 )
442 : *
443 : *-------------------------------------------------------------------------*/
444 :
445 11010623 : int16_t ari_decode_14bits_pow(
446 : const int16_t *ptr,
447 : int16_t bp,
448 : int16_t bits,
449 : int16_t *res,
450 : Tastat *s,
451 : uint16_t base )
452 : {
453 : uint16_t symbol;
454 : int32_t low, high;
455 : uint32_t range, value, cum;
456 : Word16 pows[12]; /* "base" to the power of 2, 4, 8,... 2^12 */
457 11010623 : Word16 lowlim, highlim = 0, testval;
458 : int16_t k;
459 :
460 11010623 : low = s->low;
461 11010623 : high = s->high + 1;
462 11010623 : value = s->value;
463 11010623 : lowlim = 0;
464 11010623 : symbol = 0;
465 :
466 11010623 : range = high - low;
467 :
468 : /* the value read from bitstream */
469 11010623 : assert( value >= (uint32_t) low );
470 11010623 : cum = ( ( ( (uint32_t) ( value - low ) ) << stat_bitsnew ) + ( 1 << stat_bitsnew ) - 1 );
471 :
472 : /* search for the interval where "cum" fits */
473 11010623 : if ( ( ( base >> 1 ) * range ) > cum ) /* below pow-1 */
474 : {
475 517290 : pows[0] = testval = base;
476 : /* increase exponent until it is smaller than "cum" */
477 822211 : for ( k = 1; k < 12; k++ )
478 : {
479 822211 : highlim = testval;
480 822211 : pows[k] = mult_r( pows[k - 1], pows[k - 1] );
481 822211 : move16();
482 822211 : testval = mult_r( pows[k], base );
483 822211 : if ( ( ( testval >> 1 ) * range ) <= cum ) /* found! big range is [lowlim,testval], (now narrow it down) */
484 : {
485 517290 : lowlim = testval;
486 517290 : k--;
487 517290 : symbol = 1 << k;
488 517290 : break;
489 : }
490 : }
491 517290 : assert( k < 12 ); /* maximum 2^10-1*/
492 : /* narrow the range down */
493 653292 : for ( k--; k > 0; k-- )
494 : {
495 136002 : testval = mult_r( highlim, pows[k] );
496 136002 : if ( ( ( testval >> 1 ) * range ) <= cum )
497 : {
498 88852 : lowlim = testval;
499 88852 : symbol -= 1 << ( k - 1 );
500 : }
501 : else
502 : {
503 47150 : highlim = testval;
504 : }
505 : }
506 517290 : highlim >>= 1;
507 517290 : lowlim >>= 1;
508 : }
509 : else /* trivial case, above pow-1, that is, first symbol */
510 : {
511 10493333 : symbol = 0;
512 10493333 : lowlim = base >> 1;
513 10493333 : highlim = 16384;
514 : }
515 :
516 :
517 11010623 : high = low + mul_sbc_14bits( range, highlim );
518 :
519 11010623 : low += mul_sbc_14bits( range, lowlim );
520 :
521 : /*ptr init for ptr*/
522 13057606 : for ( ; bp < bits; )
523 : {
524 5164481 : if ( high > ari_q2new )
525 : {
526 4494919 : if ( low >= ari_q2new )
527 : {
528 739175 : value -= ari_q2new;
529 739175 : low -= ari_q2new;
530 739175 : high -= ari_q2new;
531 : }
532 : else
533 : {
534 3755744 : if ( low >= ari_q1new && high <= ari_q3new )
535 : {
536 638246 : value -= ari_q1new;
537 638246 : low -= ari_q1new;
538 638246 : high -= ari_q1new;
539 : }
540 : else
541 : {
542 : break;
543 : }
544 : }
545 : }
546 2046983 : low += low;
547 2046983 : high += high;
548 :
549 2046983 : assert( abs( ptr[bp] ) <= 1 && "AC expects reading binary values!!!" );
550 :
551 2046983 : value = ( value << 1 ) | ptr[bp++];
552 : }
553 :
554 11010623 : if ( !( bp != bits || !( ( s->low == low ) && ( s->high == high ) && ( s->value == value ) ) ) )
555 : {
556 : /* This should not happen except of bit errors. */
557 0 : s->high = s->low = 0;
558 0 : *res = 0;
559 0 : return -1;
560 : }
561 :
562 11010623 : s->low = low;
563 11010623 : s->high = high - 1;
564 11010623 : s->value = value;
565 :
566 11010623 : *res = symbol;
567 :
568 11010623 : return bp;
569 : }
570 :
571 :
572 : /*------------------------------------------------------------------------
573 : * Function: ari_decode_14bits_sign()
574 : *
575 : * Decode a sign with equal probabilities.
576 : *-------------------------------------------------------------------------*/
577 :
578 517290 : int16_t ari_decode_14bits_sign(
579 : const int16_t *ptr,
580 : int16_t bp,
581 : int16_t bits,
582 : uint16_t *res,
583 : Tastat *s )
584 : {
585 : uint16_t symbol;
586 : int32_t low, high;
587 : uint32_t range, value, cum;
588 :
589 517290 : low = s->low;
590 517290 : high = s->high + 1;
591 517290 : value = s->value;
592 :
593 517290 : range = high - low;
594 :
595 517290 : if ( bp < bits )
596 : {
597 482455 : assert( value >= (uint32_t) low );
598 482455 : cum = ( ( ( (uint32_t) ( value - low ) ) << stat_bitsnew ) + ( 1 << stat_bitsnew ) - 1 );
599 482455 : if ( 8192 * range > cum )
600 : {
601 241412 : symbol = 2;
602 241412 : high = low + ( range >> 1 );
603 : }
604 : else
605 : {
606 241043 : symbol = 1;
607 241043 : low += range >> 1;
608 : }
609 :
610 : /*ptr init for ptr*/
611 965980 : for ( ; bp < bits; )
612 : {
613 963437 : if ( high > ari_q2new )
614 : {
615 840698 : if ( low >= ari_q2new )
616 : {
617 119779 : value -= ari_q2new;
618 119779 : low -= ari_q2new;
619 119779 : high -= ari_q2new;
620 : }
621 : else
622 : {
623 720919 : if ( low >= ari_q1new && high <= ari_q3new )
624 : {
625 241007 : value -= ari_q1new;
626 241007 : low -= ari_q1new;
627 241007 : high -= ari_q1new;
628 : }
629 : else
630 : {
631 : break;
632 : }
633 : }
634 : }
635 483525 : low += low;
636 483525 : high += high;
637 :
638 483525 : assert( abs( ptr[bp] ) <= 1 && "AC expects reading binary values!!!" );
639 :
640 483525 : value = ( value << 1 ) | ptr[bp++];
641 : }
642 : }
643 : else
644 : {
645 34835 : cum = value - low;
646 34835 : range >>= 1;
647 34835 : if ( range > cum )
648 : {
649 17430 : symbol = 2;
650 17430 : high = low + range;
651 : }
652 : else
653 : {
654 17405 : symbol = 1;
655 17405 : low += range;
656 : }
657 : }
658 :
659 517290 : s->low = low;
660 517290 : s->high = high - 1;
661 517290 : s->value = value;
662 :
663 517290 : *res = symbol;
664 :
665 517290 : return bp;
666 : }
|