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