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 15162043 : void ari_start_decoding_14bits(
58 : Decoder_State *st,
59 : Tastat *s )
60 : {
61 : uint32_t val;
62 :
63 15162043 : val = get_next_indice( st, cbitsnew );
64 :
65 15162043 : s->low = 0;
66 15162043 : s->high = ari_q4new;
67 15162043 : s->value = val;
68 :
69 15162043 : return;
70 : }
71 :
72 : /*---------------------------------------------------------------
73 : * ari_start_decoding_14bits_prm()
74 : *
75 : * Start ArCo decoding
76 : *-------------------------------------------------------------*/
77 :
78 371149 : 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 371149 : val = 0;
88 371149 : p = ptr + bp;
89 6309533 : for ( i = 0; i < cbitsnew; i++ )
90 : {
91 5938384 : val = ( val << 1 ) | *( p + i );
92 : }
93 :
94 371149 : s->low = 0;
95 371149 : s->high = ari_q4new;
96 371149 : s->value = val;
97 :
98 371149 : bp = bp + i;
99 :
100 371149 : 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 6559358 : 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 6559358 : low = s->low;
124 6559358 : high = s->high;
125 6559358 : value = s->value;
126 :
127 6559358 : range = high - low + 1; /* keep: tmp=low-1 */
128 6559358 : cum = ( ( ( (uint32_t) ( value - low + 1 ) ) << stat_bitsnew ) - ( (uint32_t) 1 ) );
129 :
130 6559358 : 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 6559358 : if ( p[8] * range > cum )
137 : {
138 2535444 : p += 8;
139 : }
140 : /* */
141 6559358 : if ( p[4] * range > cum )
142 : {
143 3115931 : p += 4;
144 : }
145 : /* */
146 6559358 : if ( p[2] * range > cum )
147 : {
148 2526803 : p += 2;
149 : }
150 : /* */
151 6559358 : if ( p[1] * range > cum )
152 : {
153 3118412 : p += 1;
154 3118412 : if ( ( p == cum_freq + 15 ) && ( p[1] * range > cum ) )
155 : {
156 1590910 : p += 1;
157 : }
158 : }
159 :
160 6559358 : symbol = (uint16_t) ( p - cum_freq );
161 :
162 6559358 : high = low + mul_sbc_14bits( range, cum_freq[symbol] ) - 1;
163 6559358 : low += mul_sbc_14bits( range, cum_freq[symbol + 1] );
164 :
165 : for ( ;; )
166 : {
167 22533021 : if ( high >= ari_q2new )
168 : {
169 16711400 : if ( low >= ari_q2new )
170 : {
171 5849229 : value -= ari_q2new;
172 5849229 : low -= ari_q2new;
173 5849229 : high -= ari_q2new;
174 : }
175 : else
176 : {
177 10862171 : if ( low >= ari_q1new && high < ari_q3new )
178 : {
179 4302813 : value -= ari_q1new;
180 4302813 : low -= ari_q1new;
181 4302813 : high -= ari_q1new;
182 : }
183 : else
184 : {
185 : break;
186 : }
187 : }
188 : }
189 15973663 : low += low;
190 15973663 : high += high + 1;
191 :
192 15973663 : value = ( value << 1 ) | get_next_indice_1( st );
193 : }
194 :
195 6559358 : s->low = low;
196 6559358 : s->high = high;
197 6559358 : s->value = value;
198 :
199 6559358 : *res = symbol;
200 :
201 6559358 : 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 84479364 : 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 84479364 : low = s->low;
227 84479364 : high = s->high;
228 84479364 : value = s->value;
229 :
230 84479364 : range = high - low + 1; /* keep: tmp=low-1 */
231 84479364 : 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 84479364 : il = 0;
241 84479364 : ih = 27;
242 :
243 : /* do a five step binary search, using the interval [il, ih) */
244 84479364 : im = 13; /* (il + ih) >> 1 */
245 84479364 : if ( cum_freq[im] * range > cum )
246 : {
247 52565933 : il = im;
248 : }
249 : else
250 : {
251 31913431 : ih = im;
252 : }
253 :
254 84479364 : im = ( il + ih ) >> 1;
255 84479364 : if ( cum_freq[im] * range > cum )
256 : {
257 31797287 : il = im;
258 : }
259 : else
260 : {
261 52682077 : ih = im;
262 : }
263 :
264 84479364 : im = ( il + ih ) >> 1;
265 84479364 : if ( cum_freq[im] * range > cum )
266 : {
267 42343432 : il = im;
268 : }
269 : else
270 : {
271 42135932 : ih = im;
272 : }
273 :
274 84479364 : im = ( il + ih ) >> 1;
275 84479364 : if ( cum_freq[im] * range > cum )
276 : {
277 49053754 : il = im;
278 : }
279 : else
280 : {
281 35425610 : ih = im;
282 : }
283 :
284 84479364 : 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 72125480 : im = il + 1; /* (il + ih) >> 1 */
288 72125480 : if ( cum_freq[im] * range > cum )
289 : {
290 35172855 : il = im;
291 : }
292 : }
293 :
294 84479364 : symbol = il;
295 : /* end change when compared with ari_decode_14bits_s17_ext,
296 : ending with line: symbol = p - cum_freq; */
297 :
298 84479364 : high = low + mul_sbc_14bits( range, cum_freq[symbol] ) - 1;
299 84479364 : low += mul_sbc_14bits( range, cum_freq[symbol + 1] );
300 :
301 : for ( ;; )
302 : {
303 375717736 : if ( high >= ari_q2new )
304 : {
305 262245311 : if ( low >= ari_q2new )
306 : {
307 107827004 : value -= ari_q2new;
308 107827004 : low -= ari_q2new;
309 107827004 : high -= ari_q2new;
310 : }
311 : else
312 : {
313 154418307 : if ( low >= ari_q1new && high < ari_q3new )
314 : {
315 69938943 : value -= ari_q1new;
316 69938943 : low -= ari_q1new;
317 69938943 : high -= ari_q1new;
318 : }
319 : else
320 : {
321 : break;
322 : }
323 : }
324 : }
325 291238372 : low += low;
326 291238372 : high += high + 1;
327 :
328 291238372 : value = ( value << 1 ) | get_next_indice_1( st );
329 : }
330 :
331 84479364 : s->low = low;
332 84479364 : s->high = high;
333 84479364 : s->value = value;
334 :
335 84479364 : *res = symbol;
336 :
337 84479364 : 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 30406270 : 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 30406270 : low = s->low;
361 30406270 : high = s->high;
362 30406270 : value = s->value;
363 :
364 30406270 : range = high - low + 1; /* keep: tmp=low-1 */
365 30406270 : 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 30406270 : symbol = 0;
375 :
376 30406270 : if ( ( range << 13 ) > cum )
377 : {
378 14898473 : symbol = 1;
379 : }
380 :
381 30406270 : if ( symbol == 0 )
382 : {
383 : /* high is unchanged */
384 15507797 : low = low + ( range >> 1 );
385 : }
386 : else
387 : {
388 14898473 : 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 61018509 : if ( high >= ari_q2new )
398 : {
399 55629272 : if ( low >= ari_q2new )
400 : {
401 9899054 : value -= ari_q2new;
402 9899054 : low -= ari_q2new;
403 9899054 : high -= ari_q2new;
404 : }
405 : else
406 : {
407 45730218 : if ( low >= ari_q1new && high < ari_q3new )
408 : {
409 15323948 : value -= ari_q1new;
410 15323948 : low -= ari_q1new;
411 15323948 : high -= ari_q1new;
412 : }
413 : else
414 : {
415 : break;
416 : }
417 : }
418 : }
419 30612239 : low += low;
420 30612239 : high += high + 1;
421 :
422 30612239 : value = ( value << 1 ) | get_next_indice_1( st );
423 : }
424 :
425 30406270 : s->low = low;
426 30406270 : s->high = high;
427 30406270 : s->value = value;
428 :
429 30406270 : *res = symbol;
430 :
431 30406270 : 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 207156323 : 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 207156323 : Word16 lowlim, highlim = 0, testval;
458 : int16_t k;
459 :
460 207156323 : low = s->low;
461 207156323 : high = s->high + 1;
462 207156323 : value = s->value;
463 207156323 : lowlim = 0;
464 207156323 : symbol = 0;
465 :
466 207156323 : range = high - low;
467 :
468 : /* the value read from bitstream */
469 207156323 : assert( value >= (uint32_t) low );
470 207156323 : cum = ( ( ( (uint32_t) ( value - low ) ) << stat_bitsnew ) + ( 1 << stat_bitsnew ) - 1 );
471 :
472 : /* search for the interval where "cum" fits */
473 207156323 : if ( ( ( base >> 1 ) * range ) > cum ) /* below pow-1 */
474 : {
475 10693430 : pows[0] = testval = base;
476 : /* increase exponent until it is smaller than "cum" */
477 17284487 : for ( k = 1; k < 12; k++ )
478 : {
479 17284487 : highlim = testval;
480 17284487 : pows[k] = mult_r( pows[k - 1], pows[k - 1] );
481 17284487 : move16();
482 17284487 : testval = mult_r( pows[k], base );
483 17284487 : if ( ( ( testval >> 1 ) * range ) <= cum ) /* found! big range is [lowlim,testval], (now narrow it down) */
484 : {
485 10693430 : lowlim = testval;
486 10693430 : k--;
487 10693430 : symbol = 1 << k;
488 10693430 : break;
489 : }
490 : }
491 10693430 : assert( k < 12 ); /* maximum 2^10-1*/
492 : /* narrow the range down */
493 13734565 : for ( k--; k > 0; k-- )
494 : {
495 3041135 : testval = mult_r( highlim, pows[k] );
496 3041135 : if ( ( ( testval >> 1 ) * range ) <= cum )
497 : {
498 1959544 : lowlim = testval;
499 1959544 : symbol -= 1 << ( k - 1 );
500 : }
501 : else
502 : {
503 1081591 : highlim = testval;
504 : }
505 : }
506 10693430 : highlim >>= 1;
507 10693430 : lowlim >>= 1;
508 : }
509 : else /* trivial case, above pow-1, that is, first symbol */
510 : {
511 196462893 : symbol = 0;
512 196462893 : lowlim = base >> 1;
513 196462893 : highlim = 16384;
514 : }
515 :
516 :
517 207156323 : high = low + mul_sbc_14bits( range, highlim );
518 :
519 207156323 : low += mul_sbc_14bits( range, lowlim );
520 :
521 : /*ptr init for ptr*/
522 254704708 : for ( ; bp < bits; )
523 : {
524 123991606 : if ( high > ari_q2new )
525 : {
526 109824669 : if ( low >= ari_q2new )
527 : {
528 19790123 : value -= ari_q2new;
529 19790123 : low -= ari_q2new;
530 19790123 : high -= ari_q2new;
531 : }
532 : else
533 : {
534 90034546 : if ( low >= ari_q1new && high <= ari_q3new )
535 : {
536 13591325 : value -= ari_q1new;
537 13591325 : low -= ari_q1new;
538 13591325 : high -= ari_q1new;
539 : }
540 : else
541 : {
542 : break;
543 : }
544 : }
545 : }
546 47548385 : low += low;
547 47548385 : high += high;
548 :
549 47548385 : assert( abs( ptr[bp] ) <= 1 && "AC expects reading binary values!!!" );
550 :
551 47548385 : value = ( value << 1 ) | ptr[bp++];
552 : }
553 :
554 207156323 : 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 207156323 : s->low = low;
563 207156323 : s->high = high - 1;
564 207156323 : s->value = value;
565 :
566 207156323 : *res = symbol;
567 :
568 207156323 : return bp;
569 : }
570 :
571 :
572 : /*------------------------------------------------------------------------
573 : * Function: ari_decode_14bits_sign()
574 : *
575 : * Decode a sign with equal probabilities.
576 : *-------------------------------------------------------------------------*/
577 :
578 10693430 : 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 10693430 : low = s->low;
590 10693430 : high = s->high + 1;
591 10693430 : value = s->value;
592 :
593 10693430 : range = high - low;
594 :
595 10693430 : if ( bp < bits )
596 : {
597 10046002 : assert( value >= (uint32_t) low );
598 10046002 : cum = ( ( ( (uint32_t) ( value - low ) ) << stat_bitsnew ) + ( 1 << stat_bitsnew ) - 1 );
599 10046002 : if ( 8192 * range > cum )
600 : {
601 4997002 : symbol = 2;
602 4997002 : high = low + ( range >> 1 );
603 : }
604 : else
605 : {
606 5049000 : symbol = 1;
607 5049000 : low += range >> 1;
608 : }
609 :
610 : /*ptr init for ptr*/
611 20094308 : for ( ; bp < bits; )
612 : {
613 20047944 : if ( high > ari_q2new )
614 : {
615 17520214 : if ( low >= ari_q2new )
616 : {
617 2525785 : value -= ari_q2new;
618 2525785 : low -= ari_q2new;
619 2525785 : high -= ari_q2new;
620 : }
621 : else
622 : {
623 14994429 : if ( low >= ari_q1new && high <= ari_q3new )
624 : {
625 4994791 : value -= ari_q1new;
626 4994791 : low -= ari_q1new;
627 4994791 : high -= ari_q1new;
628 : }
629 : else
630 : {
631 : break;
632 : }
633 : }
634 : }
635 10048306 : low += low;
636 10048306 : high += high;
637 :
638 10048306 : assert( abs( ptr[bp] ) <= 1 && "AC expects reading binary values!!!" );
639 :
640 10048306 : value = ( value << 1 ) | ptr[bp++];
641 : }
642 : }
643 : else
644 : {
645 647428 : cum = value - low;
646 647428 : range >>= 1;
647 647428 : if ( range > cum )
648 : {
649 328546 : symbol = 2;
650 328546 : high = low + range;
651 : }
652 : else
653 : {
654 318882 : symbol = 1;
655 318882 : low += range;
656 : }
657 : }
658 :
659 10693430 : s->low = low;
660 10693430 : s->high = high - 1;
661 10693430 : s->value = value;
662 :
663 10693430 : *res = symbol;
664 :
665 10693430 : return bp;
666 : }
|