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 : #ifdef DEBUGGING
45 : #include "debug.h"
46 : #endif
47 : #include "wmc_auto.h"
48 :
49 :
50 : /*---------------------------------------------------------------
51 : * ari_copy_states()
52 : *
53 : * Copy state
54 : *-------------------------------------------------------------*/
55 :
56 152237354 : void ari_copy_states(
57 : Tastat *source,
58 : Tastat *dest )
59 : {
60 152237354 : dest->low = source->low;
61 152237354 : dest->high = source->high;
62 152237354 : dest->bits_to_follow = source->bits_to_follow;
63 :
64 152237354 : return;
65 : }
66 :
67 : /*---------------------------------------------------------------
68 : Ari encoder 14 bits routines
69 : -------------------------------------------------------------*/
70 :
71 : /*---------------------------------------------------------------
72 : * ari_start_encoding_14bits()
73 : *
74 : * Start ArCo encoding
75 : *-------------------------------------------------------------*/
76 :
77 32317182 : void ari_start_encoding_14bits(
78 : Tastat *s )
79 : {
80 : /* : addressing is made with walking pointer s */
81 32317182 : s->low = 0;
82 32317182 : s->high = ari_q4new;
83 32317182 : s->bits_to_follow = 0;
84 :
85 32317182 : return;
86 : }
87 :
88 :
89 : /*---------------------------------------------------------------
90 : * ari_done_encoding_14bits()
91 : *
92 : * Finish ArCo encoding
93 : *-------------------------------------------------------------*/
94 :
95 16781528 : int16_t ari_done_encoding_14bits(
96 : int16_t *ptr,
97 : int16_t bp,
98 : Tastat *s )
99 : {
100 : int32_t low;
101 : int32_t bits_to_follow;
102 :
103 : /* not needed, s points to s->low */
104 16781528 : low = s->low;
105 16781528 : bits_to_follow = s->bits_to_follow + 1;
106 :
107 16781528 : if ( low < ari_q1new )
108 : {
109 10461016 : ptr[bp++] = 0; /*send a zero*/
110 31160685 : for ( ; bits_to_follow > 0; bits_to_follow-- )
111 : {
112 20699669 : ptr[bp++] = 1; /*send a one*/
113 : }
114 : }
115 : else
116 : {
117 6320512 : ptr[bp++] = 1; /*send a one*/
118 18942521 : for ( ; bits_to_follow > 0; bits_to_follow-- )
119 : {
120 12622009 : ptr[bp++] = 0; /*send a zero*/
121 : }
122 : }
123 :
124 : /*It is done so no need to save values-> no counting*/
125 : /*s->low = low;
126 : s->bits_to_follow = bits_to_follow;*/
127 :
128 16781528 : return bp;
129 : }
130 :
131 :
132 : /*---------------------------------------------------------------
133 : * ari_encode_14bits_ext()
134 : *
135 : * encode function for extended proba tables: less branches needed for coding
136 : *
137 : *-------------------------------------------------------------*/
138 :
139 99996171 : int16_t ari_encode_14bits_ext(
140 : int16_t *ptr,
141 : int16_t bp,
142 : Tastat *s,
143 : int32_t symbol,
144 : const uint16_t *cum_freq )
145 : {
146 : int32_t low, high, range;
147 : int32_t bits_to_follow;
148 :
149 : /*for all operation using bit_ptr=&ptr[bp] */
150 : /* for reading s->high,low,bits_to_follow sequentially */
151 99996171 : high = s->high;
152 99996171 : low = s->low;
153 99996171 : range = high - low + 1;
154 :
155 99996171 : high = low + mul_sbc_14bits( range, cum_freq[symbol] ) - 1;
156 99996171 : low += mul_sbc_14bits( range, cum_freq[symbol + 1] );
157 :
158 99996171 : bits_to_follow = s->bits_to_follow;
159 :
160 : for ( ;; )
161 : {
162 454048331 : if ( high < ari_q2new )
163 : {
164 137014624 : ptr[bp++] = 0; /*send a zero*/
165 183445655 : for ( ; bits_to_follow > 0; bits_to_follow-- )
166 : {
167 46431031 : ptr[bp++] = 1; /*send a one*/
168 : }
169 : }
170 : else
171 : {
172 317033707 : if ( low >= ari_q2new )
173 : {
174 136168131 : ptr[bp++] = 1; /*send a one*/
175 159974338 : for ( ; bits_to_follow > 0; bits_to_follow-- )
176 : {
177 23806207 : ptr[bp++] = 0; /*send a zero*/
178 : }
179 136168131 : low -= ari_q2new;
180 136168131 : high -= ari_q2new; /* Subtract offset to top. */
181 : }
182 : else
183 : {
184 : /* Output an opposite bit */
185 180865576 : if ( low >= ari_q1new && high < ari_q3new ) /* Output an opposite bit */
186 : {
187 : /* later if in middle half. */
188 80869405 : bits_to_follow += 1;
189 80869405 : low -= ari_q1new; /* Subtract offset to middle*/
190 80869405 : high -= ari_q1new;
191 : }
192 : else
193 : {
194 : break; /* Otherwise exit loop. */
195 : }
196 : }
197 : }
198 354052160 : low += low;
199 354052160 : high += high + 1; /* Scale up code range. */
200 : }
201 :
202 99996171 : s->low = low;
203 99996171 : s->high = high;
204 99996171 : s->bits_to_follow = bits_to_follow;
205 :
206 99996171 : return bp;
207 : }
208 :
209 :
210 : /*------------------------------------------------------------------------
211 : * Function: ari_encode_14bits_range()
212 : *
213 : * Encode an cumulative frequency interval.
214 : *-------------------------------------------------------------------------*/
215 :
216 147597205 : int16_t ari_encode_14bits_range(
217 : int16_t *ptr,
218 : int16_t bp,
219 : int32_t bits,
220 : Tastat *s,
221 : uint16_t cum_freq_low,
222 : uint16_t cum_freq_high )
223 : {
224 : int32_t low, high, range;
225 : int32_t bits_to_follow;
226 :
227 : /* not needed, s points to s->low */
228 147597205 : high = s->high;
229 147597205 : high++;
230 147597205 : low = s->low;
231 147597205 : range = high - low;
232 :
233 147597205 : high = low + mul_sbc_14bits( range, cum_freq_high );
234 147597205 : low += mul_sbc_14bits( range, cum_freq_low );
235 :
236 147597205 : bits_to_follow = s->bits_to_follow;
237 :
238 : /* while there are more than 16 bits left */
239 182061187 : for ( ; bp + 16 + bits_to_follow - bits < 0; )
240 : {
241 96299880 : if ( high <= ari_q2new )
242 : {
243 9857116 : ptr[bp++] = 0; /*send a zero*/
244 14317843 : for ( ; bits_to_follow > 0; bits_to_follow-- )
245 : {
246 4460727 : ptr[bp++] = 1; /*send a one*/
247 : }
248 : }
249 86442764 : else if ( low >= ari_q2new )
250 : {
251 : /* to reach this branch */
252 15381390 : ptr[bp++] = 1; /*send a one*/
253 20082933 : for ( ; bits_to_follow > 0; bits_to_follow-- )
254 : {
255 4701543 : ptr[bp++] = 0; /*send a zero*/
256 : }
257 15381390 : low -= ari_q2new;
258 15381390 : high -= ari_q2new; /* Subtract offset to top. */
259 : }
260 71061374 : else if ( low >= ari_q1new && high <= ari_q3new )
261 : {
262 : /* to reach this branch */
263 : /* Output an opposite bit */
264 : /* later if in middle half. */
265 9225476 : bits_to_follow += 1;
266 9225476 : low -= ari_q1new; /* Subtract offset to middle*/
267 9225476 : high -= ari_q1new;
268 : }
269 : else
270 : {
271 : /* to reach this branch */
272 : break; /* Otherwise exit loop. */
273 : }
274 :
275 34463982 : low += low;
276 34463982 : high += high; /* Scale up code range. */
277 : }
278 : /* if there are <= 16 bits left */
279 147597205 : if ( bp + 16 + bits_to_follow - bits >= 0 )
280 : {
281 : /* No need to do anyhing, but let's keep a place for a breakpoint */
282 85761307 : s->bits_to_follow = -1;
283 : }
284 :
285 147597205 : s->low = low;
286 147597205 : s->high = high - 1;
287 147597205 : s->bits_to_follow = bits_to_follow;
288 :
289 147597205 : return bp;
290 : }
291 :
292 : /*------------------------------------------------------------------------
293 : * Function: ari_encode_14bits_sign()
294 : *
295 : * Encode a sign with equal probabilities.
296 : *-------------------------------------------------------------------------*/
297 :
298 41358517 : int16_t ari_encode_14bits_sign(
299 : int16_t *ptr,
300 : int16_t bp,
301 : int32_t bits,
302 : Tastat *s,
303 : int32_t sign )
304 : {
305 : int32_t low, high, range;
306 : int32_t bits_to_follow;
307 :
308 : /* not needed, s points to s->low */
309 41358517 : high = s->high;
310 41358517 : high++;
311 41358517 : low = s->low;
312 41358517 : range = high - low;
313 :
314 41358517 : if ( sign )
315 : {
316 20254515 : high = low + ( range >> 1 );
317 : }
318 : else
319 : {
320 21104002 : low += range >> 1;
321 : }
322 :
323 41358517 : bits_to_follow = s->bits_to_follow;
324 :
325 : /* while there are more than 16 bits left */
326 82836705 : for ( ; bp + 16 + bits_to_follow - bits < 0; )
327 : {
328 82378089 : if ( high <= ari_q2new )
329 : {
330 7867579 : ptr[bp++] = 0; /*send a zero*/
331 14633361 : for ( ; bits_to_follow > 0; bits_to_follow-- )
332 : {
333 6765782 : ptr[bp++] = 1; /*send a one*/
334 : }
335 : }
336 74510510 : else if ( low >= ari_q2new )
337 : {
338 : /* to reach this branch */
339 12843049 : ptr[bp++] = 1; /*send a one*/
340 20865404 : for ( ; bits_to_follow > 0; bits_to_follow-- )
341 : {
342 8022355 : ptr[bp++] = 0; /*send a zero*/
343 : }
344 12843049 : low -= ari_q2new;
345 12843049 : high -= ari_q2new; /* Subtract offset to top. */
346 : }
347 61667461 : else if ( low >= ari_q1new && high <= ari_q3new )
348 : {
349 : /* to reach this branch */
350 : /* Output an opposite bit */
351 : /* later if in middle half. */
352 20767560 : bits_to_follow += 1;
353 20767560 : low -= ari_q1new; /* Subtract offset to middle*/
354 20767560 : high -= ari_q1new;
355 : }
356 : else
357 : {
358 : /* to reach this branch */
359 : break; /* Otherwise exit loop. */
360 : }
361 :
362 41478188 : low += low;
363 41478188 : high += high; /* Scale up code range. */
364 : }
365 :
366 41358517 : s->low = low;
367 41358517 : s->high = high - 1;
368 41358517 : s->bits_to_follow = bits_to_follow;
369 :
370 41358517 : return bp;
371 : }
372 :
373 : /*------------------------------------------------------------------------
374 : * Function: ari_done_cbr_encoding_14bits()
375 : *
376 : * Finish up encoding in CBR mode.
377 : *-------------------------------------------------------------------------*/
378 :
379 263941 : int16_t ari_done_cbr_encoding_14bits(
380 : int16_t *ptr,
381 : int16_t bp,
382 : int32_t bits,
383 : Tastat *s )
384 : {
385 : int32_t high;
386 : int32_t bits_to_follow;
387 : uint16_t k;
388 :
389 263941 : while ( bits - bp - 16 - s->bits_to_follow > 0 )
390 : {
391 0 : bp = ari_encode_14bits_sign( ptr, bp, bits, s, 0 );
392 : }
393 :
394 : /* not needed, s points to s->low */
395 263941 : high = s->high;
396 263941 : bits_to_follow = s->bits_to_follow;
397 :
398 263941 : if ( bits_to_follow )
399 : {
400 : /* If in upper half, then output a one, bits_to_follow zeros, and the remaining bits, except the first one */
401 67632 : if ( high < 0x8000 )
402 : {
403 33109 : ptr[bp++] = 0; /*send a zero*/
404 98537 : for ( ; bits_to_follow > 0; bits_to_follow-- )
405 : {
406 65428 : ptr[bp++] = 1; /*send a one*/
407 : }
408 : }
409 : else
410 : {
411 34523 : ptr[bp++] = 1; /*send a one*/
412 103768 : for ( ; bits_to_follow > 0; bits_to_follow-- )
413 : {
414 69245 : ptr[bp++] = 0; /*send a zero*/
415 : }
416 : }
417 : /* write remaining bits */
418 1014480 : for ( k = 0x4000; k > 0; k >>= 1 )
419 : {
420 1014480 : if ( k & high )
421 : {
422 558133 : ptr[bp++] = 1; /*send a one*/
423 : }
424 : else
425 : {
426 456347 : ptr[bp++] = 0; /*send a zero*/
427 : }
428 :
429 1014480 : if ( bp >= bits )
430 : {
431 67632 : break;
432 : }
433 : }
434 : }
435 : else
436 : {
437 : /* no carry-bits, just write all bits */
438 3140944 : for ( k = 0x8000; k > 0; k >>= 1 )
439 : {
440 3140944 : if ( k & high )
441 : {
442 2189448 : ptr[bp++] = 1; /*send a one*/
443 : }
444 : else
445 : {
446 951496 : ptr[bp++] = 0; /*send a zero*/
447 : }
448 :
449 3140944 : if ( bp >= bits )
450 : {
451 196309 : break;
452 : }
453 : }
454 : }
455 :
456 263941 : return bp;
457 : }
|