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 <stdint.h>
38 : #include "options.h"
39 : #include <assert.h>
40 : #include "cnst.h"
41 : #include "rom_com.h"
42 : #include "prot.h"
43 : #include "ivas_prot.h" /* Range coder header file */
44 : #include "ivas_rom_com.h"
45 : #include "ivas_rom_enc.h"
46 : #ifdef DEBUGGING
47 : #include "debug.h"
48 : #endif
49 : #include "wmc_auto.h"
50 :
51 :
52 : /*-------------------------------------------------------------------*
53 : * ACcontextMapping_encode2_no_mem_s17_LC()
54 : *
55 : * Arithmetic encoder
56 : *-------------------------------------------------------------------*/
57 :
58 1325 : void ACcontextMapping_encode2_no_mem_s17_LC(
59 : BSTR_ENC_HANDLE hBstr, /* i/o: bitstream handle */
60 : int16_t *x,
61 : int16_t nt,
62 : int16_t lastnz,
63 : int16_t nbbits,
64 : int16_t resQMaxBits,
65 : CONTEXT_HM_CONFIG *hm_cfg )
66 : {
67 : int16_t ptr[BITBUFSIZE];
68 : Tastat as, as_overflow;
69 : int16_t bp, bp_overflow;
70 : int16_t a1, b1, a1_i, b1_i, k;
71 : uint16_t t;
72 : int16_t pki, lev1;
73 : int16_t rateFlag;
74 : int16_t value;
75 : int16_t nbbits_ntuples, nbbits_lsbs, nbbits_signs, nbbits_signs_overflow, nbbits_lsbs_overflow, flag_overflow;
76 : int16_t *lsbs_bits;
77 : int16_t nt_half;
78 : int32_t c[2], *ctx;
79 : int32_t p1, p2;
80 : int16_t ii[2];
81 : int16_t idx1, idx2, idx;
82 : int16_t numPeakIndicesOrig, numHoleIndices;
83 : uint16_t signs[N_MAX];
84 : int16_t nbbits_m2;
85 :
86 1325 : a1 = 0; /* to avoid compilation warnings */
87 1325 : b1 = 0; /* to avoid compilation warnings */
88 :
89 : /* Rate flag */
90 1325 : if ( nbbits > 400 )
91 : {
92 76 : rateFlag = 2 << NBITS_CONTEXT;
93 : }
94 : else
95 : {
96 1249 : rateFlag = 0;
97 : }
98 :
99 : /* Init */
100 1325 : nt_half = nt >> 1;
101 1325 : c[0] = c[1] = 0;
102 :
103 : /* Bits for encoding the number of encoded tuples */
104 1325 : nbbits_ntuples = 0;
105 1325 : k = 1;
106 :
107 13250 : while ( k < nt / 2 )
108 : {
109 11925 : nbbits_ntuples++;
110 11925 : k = k << 1;
111 : }
112 :
113 1325 : t = 0;
114 1325 : bp = nbbits_ntuples;
115 1325 : nbbits_signs = 0;
116 1325 : nbbits_lsbs = 0;
117 1325 : nbbits_m2 = nbbits - 2;
118 1325 : flag_overflow = 0;
119 :
120 1325 : if ( hm_cfg )
121 : {
122 : /* mapped domain */
123 112 : numPeakIndicesOrig = hm_cfg->numPeakIndices;
124 112 : hm_cfg->numPeakIndices = min( hm_cfg->numPeakIndices, lastnz );
125 112 : numHoleIndices = lastnz - hm_cfg->numPeakIndices;
126 :
127 : /* Mark hole indices beyond lastnz as pruned */
128 48702 : for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
129 : {
130 48590 : hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] + nt;
131 : }
132 :
133 112 : ii[0] = numPeakIndicesOrig;
134 112 : ii[1] = 0;
135 :
136 112 : p1 = p2 = 0; /* to avoid compilation warnings */
137 : }
138 : else
139 : {
140 : /* unmapped domain */
141 1213 : ii[0] = 0;
142 :
143 1213 : p1 = p2 = 0;
144 :
145 : /* Find last non-zero tuple */
146 : /* ensure termination of while loop by dummy value */
147 1213 : a1 = x[0];
148 1213 : x[0] = 1;
149 :
150 5483 : while ( x[lastnz - 1] == 0 && x[lastnz - 2] == 0 )
151 : {
152 4270 : lastnz -= 2;
153 : }
154 1213 : x[0] = a1;
155 : }
156 :
157 1325 : lsbs_bits = (int16_t *) ptr + nbbits - 1;
158 :
159 : /*Start Encoding*/
160 1325 : ari_start_encoding_14bits( &as );
161 :
162 : /*Main Loop through the 2-tuples*/
163 1325 : b1_i = -1;
164 :
165 135901 : for ( k = 0; k < lastnz; k += 2 )
166 : {
167 :
168 134577 : if ( hm_cfg )
169 : {
170 11480 : a1_i = get_next_coeff_mapped( ii, &p1, &idx1, hm_cfg );
171 11480 : b1_i = get_next_coeff_mapped( ii, &p2, &idx2, hm_cfg );
172 : }
173 : else
174 : {
175 123097 : a1_i = get_next_coeff_unmapped( ii, &idx1 );
176 123097 : b1_i = get_next_coeff_unmapped( ii, &idx2 );
177 : }
178 :
179 134577 : idx = min( idx1, idx2 );
180 :
181 : /* Get context */
182 134577 : ctx = &c[p1 | p2];
183 :
184 134577 : t = (uint16_t) ( *ctx + rateFlag );
185 134577 : t += ( nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
186 :
187 : /* Init current 2-tuple encoding */
188 :
189 134577 : if ( flag_overflow != 0 )
190 : {
191 0 : x[a1_i] = 0;
192 0 : x[b1_i] = 0;
193 : }
194 :
195 134577 : a1 = (int16_t) abs( x[a1_i] );
196 134577 : b1 = (int16_t) abs( x[b1_i] );
197 :
198 134577 : lev1 = -1;
199 :
200 : /*Copy states*/
201 134577 : ari_copy_states( &as, &as_overflow );
202 134577 : bp_overflow = bp;
203 134577 : nbbits_signs_overflow = nbbits_signs;
204 134577 : nbbits_lsbs_overflow = nbbits_lsbs;
205 :
206 : /*Signs encoding*/
207 :
208 134577 : if ( a1 > 0 )
209 : {
210 34938 : signs[nbbits_signs++] = ( (uint16_t) x[a1_i] >> ( sizeof( uint16_t ) * 8 - 1 ) );
211 : }
212 :
213 134577 : if ( b1 > 0 )
214 : {
215 34264 : signs[nbbits_signs++] = ( (uint16_t) x[b1_i] >> ( sizeof( uint16_t ) * 8 - 1 ) );
216 : }
217 :
218 : /* MSBs coding */
219 142470 : while ( max( a1, b1 ) >= A_THRES )
220 : {
221 7893 : pki = ari_lookup_s17_LC[t + ( ( lev1 + 1 ) << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
222 7893 : bp = ari_encode_14bits_ext( ptr, bp, &as, VAL_ESC, ari_pk_s17_LC_ext[pki] );
223 :
224 7893 : *lsbs_bits-- = a1 & 1;
225 7893 : *lsbs_bits-- = b1 & 1;
226 :
227 : /* LSBs bit counting */
228 7893 : nbbits_lsbs += 2;
229 :
230 7893 : a1 >>= 1;
231 7893 : b1 >>= 1;
232 :
233 7893 : lev1 = min( lev1 + 1, 2 );
234 : }
235 :
236 134577 : pki = ari_lookup_s17_LC[t + ( ( lev1 + 1 ) << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
237 134577 : bp = ari_encode_14bits_ext( ptr, bp, &as, a1 + A_THRES * b1, ari_pk_s17_LC_ext[pki] );
238 :
239 :
240 : /* Check bit budget */
241 134577 : if ( bp + as.bits_to_follow + nbbits_signs + nbbits_lsbs > nbbits_m2 )
242 : {
243 1 : ari_copy_states( &as_overflow, &as );
244 1 : bp = bp_overflow;
245 :
246 1 : if ( !flag_overflow )
247 : {
248 1 : nbbits_signs = nbbits_signs_overflow;
249 1 : nbbits_lsbs = nbbits_lsbs_overflow;
250 :
251 1 : if ( hm_cfg )
252 : {
253 1 : flag_overflow = 1;
254 :
255 : /* Code from now only zeros */
256 1 : x[a1_i] = 0;
257 1 : x[b1_i] = 0;
258 1 : lev1 = -1;
259 :
260 1 : pki = ari_lookup_s17_LC[t];
261 1 : bp = ari_encode_14bits_ext( ptr, bp, &as, 0, ari_pk_s17_LC_ext[pki] );
262 :
263 1 : if ( bp + as.bits_to_follow + nbbits_signs + nbbits_lsbs > nbbits_m2 )
264 : {
265 1 : ari_copy_states( &as_overflow, &as );
266 1 : bp = bp_overflow;
267 1 : break;
268 : }
269 : }
270 : else
271 : {
272 0 : break;
273 : }
274 : }
275 : else
276 : {
277 0 : break;
278 : }
279 : }
280 :
281 : /* Update context for next 2-tuple */
282 134576 : if ( p1 == p2 )
283 : {
284 : /* peak-peak or hole-hole context */
285 :
286 134017 : if ( lev1 <= 0 )
287 : {
288 132199 : t = 1 + ( a1 + b1 ) * ( lev1 + 2 );
289 : }
290 : else
291 : {
292 1818 : t = 13 + lev1;
293 : }
294 :
295 134017 : *ctx = ( *ctx & 0xf ) * 16 + t;
296 : }
297 : else
298 : {
299 : /* mixed context */
300 :
301 559 : if ( idx1 & 1 )
302 : {
303 : /* update first context */
304 274 : c[p1] = update_mixed_context( c[p1], (int16_t) abs( x[a1_i] ) );
305 : }
306 :
307 559 : if ( idx2 & 1 )
308 : {
309 : /* update second context */
310 285 : c[p2] = update_mixed_context( c[p2], (int16_t) abs( x[b1_i] ) );
311 : }
312 : }
313 : } /*end of the 2-tuples loop*/
314 :
315 :
316 : /* End arithmetic coder, overflow management */
317 1325 : bp = ari_done_encoding_14bits( ptr, bp, &as );
318 :
319 : /*Overflow is detected*/
320 :
321 1325 : if ( k != lastnz )
322 : {
323 :
324 1 : if ( hm_cfg )
325 : {
326 : /*Fill with zero to be sure that decoder finish at the same position the MSB decoding*/
327 :
328 1 : for ( ; bp < nbbits - ( nbbits_signs + nbbits_lsbs ); )
329 : {
330 0 : ptr[bp++] = 0;
331 : }
332 : }
333 : else
334 : {
335 0 : lastnz = k;
336 : }
337 : }
338 :
339 : /* Push number of encoded tuples */
340 1325 : value = ( lastnz >> 1 ) - 1;
341 1325 : push_next_indice( hBstr, value, nbbits_ntuples );
342 :
343 : /* Push arithmetic coded bits */
344 1325 : push_next_bits( hBstr, (uint16_t *) &ptr[nbbits_ntuples], bp - nbbits_ntuples );
345 :
346 : /* Push sign bits */
347 1325 : push_next_bits( hBstr, (uint16_t *) signs, nbbits_signs );
348 1325 : bp += nbbits_signs;
349 :
350 : /* write residual Quantization bits */
351 15026 : for ( k = 0; k < min( nbbits - bp - nbbits_lsbs, resQMaxBits ); k++ )
352 : {
353 13701 : ptr[nbbits - 1 - nbbits_lsbs - k] = x[nt + k];
354 : }
355 :
356 : /* Write filler bits */
357 4057 : for ( ; k < nbbits - bp - nbbits_lsbs; ++k )
358 : {
359 2732 : ptr[nbbits - 1 - nbbits_lsbs - k] = 0;
360 : }
361 :
362 : /* Check for debugging */
363 1325 : assert( bp + k <= nbbits );
364 :
365 : /* Push the rest of the buffer */
366 1325 : push_next_bits( hBstr, (uint16_t *) &ptr[bp], nbbits - bp );
367 :
368 : #ifdef DEBUGGING
369 : /* return (bp+nbbits_lsbs);*/ /*return only for debug plot*/
370 : #endif
371 1325 : return;
372 : }
373 :
374 : /*-------------------------------------------------------------------*
375 : * find_last_nz_pair()
376 : *
377 : *
378 : *-------------------------------------------------------------------*/
379 :
380 288365 : static int16_t find_last_nz_pair(
381 : const int16_t x[],
382 : const int16_t length,
383 : const CONTEXT_HM_CONFIG *hm_cfg )
384 : {
385 : int16_t last_nz, i;
386 : const int16_t *tmp;
387 :
388 288365 : last_nz = 2;
389 :
390 288365 : if ( hm_cfg )
391 : {
392 : /* mapped kernel */
393 282043 : tmp = hm_cfg->indexBuffer;
394 :
395 56637689 : for ( i = length; i >= 4; i -= 2 )
396 : {
397 :
398 56619255 : if ( x[tmp[i - 2]] != 0 || x[tmp[i - 1]] != 0 )
399 : {
400 263609 : last_nz = i;
401 263609 : break;
402 : }
403 : }
404 : }
405 : else
406 : {
407 : /* unmapped kernel */
408 :
409 1424523 : for ( i = length; i >= 4; i -= 2 )
410 : {
411 :
412 1424523 : if ( x[i - 2] != 0 || x[i - 1] != 0 )
413 : {
414 6322 : last_nz = i;
415 6322 : break;
416 : }
417 : }
418 : }
419 :
420 288365 : return last_nz;
421 : }
422 :
423 :
424 : /*-------------------------------------------------------------------*
425 : * ACcontextMapping_encode2_estimate_no_mem_s17_LC()
426 : *
427 : *
428 : *-------------------------------------------------------------------*/
429 :
430 9137 : int16_t ACcontextMapping_encode2_estimate_no_mem_s17_LC(
431 : const int16_t *x,
432 : const int16_t nt,
433 : int16_t *lastnz_out,
434 : int16_t *nEncoded,
435 : const int16_t target,
436 : int16_t *stop,
437 : CONTEXT_HM_CONFIG *hm_cfg )
438 : {
439 : int16_t a1, b1, a1_i, b1_i;
440 : int16_t k, pki, lev1;
441 : uint16_t t;
442 : int16_t lastnz, lastnz2;
443 : int16_t rateFlag;
444 : int16_t nbits_old, nbits;
445 : int16_t stop2;
446 : int32_t proba;
447 : int16_t nlz;
448 : const uint16_t *cum_freq;
449 : int16_t symbol;
450 : const uint8_t *lookup;
451 : int16_t nt_half;
452 : int32_t c[2], *ctx;
453 : int32_t p1, p2;
454 : int16_t ii[2];
455 : int16_t idx1, idx2, idx;
456 9137 : int16_t numPeakIndicesOrig = 0, numHoleIndices = 0; /* only to avoid compiler warning */
457 :
458 : /* Rate flag */
459 9137 : if ( target > 400 )
460 : {
461 380 : rateFlag = 2 << NBITS_CONTEXT;
462 : }
463 : else
464 : {
465 8757 : rateFlag = 0;
466 : }
467 :
468 : /* 2 bits = arithmetic coder initialization interval = 1 bits for rounding last proba + 1 bit?*/
469 9137 : nbits = 2;
470 : /*proba coded on 14bits -> proba=1*/
471 9137 : proba = 16384;
472 :
473 : /* Init */
474 9137 : nt_half = nt >> 1;
475 9137 : stop2 = 0;
476 9137 : c[0] = c[1] = 0;
477 :
478 : /* bits to encode lastnz */
479 9137 : k = 1;
480 :
481 91370 : while ( k < nt / 2 )
482 : {
483 82233 : nbits++;
484 82233 : k = k << 1;
485 : /* check while condition */
486 : }
487 9137 : nbits_old = nbits;
488 :
489 9137 : nbits -= target;
490 :
491 : /* Find last non-zero tuple in the mapped domain signal */
492 9137 : lastnz = find_last_nz_pair( x, nt, hm_cfg );
493 :
494 : /* At least one tuple is coded */
495 9137 : lastnz2 = 2;
496 :
497 9137 : if ( hm_cfg )
498 : {
499 : /* mapped domain */
500 2815 : numPeakIndicesOrig = hm_cfg->numPeakIndices;
501 2815 : hm_cfg->numPeakIndices = min( hm_cfg->numPeakIndices, lastnz );
502 2815 : numHoleIndices = lastnz - hm_cfg->numPeakIndices;
503 :
504 : /* Mark hole indices beyond lastnz as pruned */
505 896105 : for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
506 : {
507 893290 : hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] + nt;
508 : }
509 :
510 2815 : ii[0] = numPeakIndicesOrig;
511 2815 : ii[1] = 0;
512 :
513 2815 : p1 = p2 = 0; /* to avoid compilation warnings */
514 : }
515 : else
516 : {
517 : /* unmapped domain */
518 6322 : ii[0] = 0;
519 :
520 6322 : p1 = p2 = 0;
521 : }
522 :
523 : /* Main Loop through the 2-tuples */
524 1016604 : for ( k = 0; k < lastnz; k += 2 )
525 : {
526 1008584 : if ( hm_cfg )
527 : {
528 377706 : a1_i = get_next_coeff_mapped( ii, &p1, &idx1, hm_cfg );
529 377706 : b1_i = get_next_coeff_mapped( ii, &p2, &idx2, hm_cfg );
530 : }
531 : else
532 : {
533 630878 : a1_i = get_next_coeff_unmapped( ii, &idx1 );
534 630878 : b1_i = get_next_coeff_unmapped( ii, &idx2 );
535 : }
536 :
537 1008584 : idx = min( idx1, idx2 );
538 :
539 : /* Get context */
540 1008584 : ctx = &c[p1 | p2];
541 :
542 1008584 : t = (uint16_t) ( *ctx + rateFlag );
543 1008584 : t += ( nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
544 :
545 : /* Init current 2-tuple encoding */
546 1008584 : a1 = (int16_t) abs( x[a1_i] );
547 1008584 : b1 = (int16_t) abs( x[b1_i] );
548 1008584 : lev1 = -( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
549 :
550 : /* Signs Bits */
551 1008584 : nbits += min( a1, 1 );
552 1008584 : nbits += min( b1, 1 );
553 :
554 : /* pre-compute address of ari_pk_s17_LC_ext[0][Val_esc] to avoid doing it multiple times inside the loop */
555 1008584 : lookup = &ari_lookup_s17_LC[t] + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
556 :
557 : /* check while condition */
558 : /* MSBs coding */
559 1062669 : while ( max( a1, b1 ) >= A_THRES )
560 : {
561 54085 : pki = lookup[lev1];
562 54085 : cum_freq = ari_pk_s17_LC_ext[pki] + VAL_ESC;
563 : /*p1*p2=proba on 28 bits: p=0.5->power(2,27)*/
564 54085 : proba *= *( cum_freq );
565 : /*Number of leading zero computed in one cycle=norm_l() in BASOP*/
566 54085 : nlz = 2;
567 219285 : while ( proba < 134217728 ) /*power(2,27)*/
568 : {
569 165200 : nlz++;
570 165200 : proba = proba << 1;
571 : }
572 54085 : nbits += nlz;
573 : /*addition added as shift not done in norm_l(): real shift = 14-nlz*/
574 54085 : proba >>= 14; /*proba is rounded down on 14 bits ->automatic over-estimation of bit consumption*/
575 :
576 54085 : ( a1 ) >>= 1;
577 54085 : ( b1 ) >>= 1;
578 :
579 54085 : lev1 = min( lev1 + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) ), 2 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
580 : /* check while condition */
581 : }
582 1008584 : pki = lookup[lev1];
583 1008584 : symbol = a1 + A_THRES * b1;
584 1008584 : cum_freq = ari_pk_s17_LC_ext[pki] + symbol;
585 : /*p1*p2=proba on 28 bits: p=0.5->power(2,27)*/
586 1008584 : proba *= ( cum_freq[0] - cum_freq[1] );
587 : /*Number of leading zero computed in one cycle=norm_l() in BASOP*/
588 1008584 : nlz = 0;
589 2754046 : while ( proba < 134217728 ) /*power(2,27)*/
590 : {
591 1745462 : nlz++;
592 1745462 : proba = proba << 1;
593 : }
594 :
595 1008584 : nbits += nlz;
596 1008584 : proba >>= 14; /*proba is rounded down on 14 bits ->automatic over-estimation of bit consumption*/
597 :
598 : /* Should we truncate? */
599 1008584 : if ( nbits > 0 )
600 : {
601 101520 : stop2 = 1;
602 :
603 101520 : if ( *stop )
604 : {
605 1117 : break;
606 : }
607 : }
608 : else
609 : {
610 907064 : if ( hm_cfg || k == 0 || x[a1_i] || x[b1_i] )
611 : {
612 551458 : nbits_old = nbits + target;
613 551458 : lastnz2 = b1_i + 1;
614 : }
615 : }
616 :
617 : /* Update context for next 2-tuple */
618 1007467 : if ( p1 == p2 ) /* peak-peak or hole-hole context */
619 : {
620 955632 : lev1 >>= NBITS_CONTEXT + NBITS_RATEQ;
621 :
622 955632 : if ( lev1 <= 0 )
623 : {
624 943671 : t = 1 + ( a1 + b1 ) * ( lev1 + 2 );
625 : }
626 : else
627 : {
628 11961 : t = 13 + lev1;
629 : }
630 :
631 955632 : *ctx = ( *ctx & 0xf ) * 16 + t;
632 : }
633 : else
634 : {
635 : /* mixed context */
636 :
637 51835 : if ( idx1 & 1 )
638 : {
639 : /* update first context */
640 25715 : c[p1] = update_mixed_context( c[p1], (int16_t) abs( x[a1_i] ) );
641 : }
642 :
643 51835 : if ( idx2 & 1 )
644 : {
645 : /* update second context */
646 25944 : c[p2] = update_mixed_context( c[p2], (int16_t) abs( x[b1_i] ) );
647 : }
648 : }
649 : } /*end of the 2-tuples loop*/
650 :
651 9137 : nbits += target;
652 :
653 : /* Output */
654 :
655 9137 : if ( *stop )
656 : {
657 1256 : nbits = nbits_old;
658 : }
659 :
660 9137 : if ( stop2 )
661 : {
662 4955 : stop2 = nbits;
663 : }
664 9137 : *nEncoded = lastnz2;
665 9137 : *stop = stop2;
666 9137 : *lastnz_out = lastnz;
667 :
668 9137 : if ( hm_cfg )
669 : {
670 : /* Restore hole indices beyond lastnz */
671 896105 : for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
672 : {
673 893290 : hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] - nt;
674 : }
675 2815 : hm_cfg->numPeakIndices = numPeakIndicesOrig;
676 : }
677 :
678 9137 : return nbits_old;
679 : }
680 :
681 :
682 : /* Range Coder Functions */
683 :
684 : /*-------------------------------------------------------------------*
685 : * RCcontextMapping_encode2_no_mem_s17_LCS()
686 : *
687 : * Range encoder
688 : *-------------------------------------------------------------------*/
689 :
690 643706 : void RCcontextMapping_encode2_no_mem_s17_LCS(
691 : BSTR_ENC_HANDLE hBstr, /* i/o: bitstream handle */
692 : int16_t *x,
693 : const int16_t nt,
694 : int16_t lastnz,
695 : const int16_t nbbits,
696 : const int16_t resQMaxBits,
697 : CONTEXT_HM_CONFIG *hm_cfg )
698 : {
699 : int16_t ptr[BITBUFSIZE];
700 : RangeUniEncState rc_st_enc;
701 : int16_t bp;
702 : int16_t rc_tot_bits; /* No. of bits returned by range coder */
703 : int16_t pki, lev1;
704 : uint16_t t;
705 : int16_t rateFlag;
706 : int16_t value;
707 : int16_t nbbits_ntuples, nbbits_lsbs, nbbits_signs;
708 : int16_t *lsbs_bits;
709 : int16_t nt_half;
710 : int16_t signs[N_MAX];
711 : int16_t a1, b1, k;
712 :
713 643706 : a1 = 0; /* to avoid compilation warnings */
714 643706 : b1 = 0; /* to avoid compilation warnings */
715 :
716 : /* Init */
717 643706 : nt_half = nt >> 1;
718 :
719 : /* Bits for encoding the number of encoded tuples */
720 643706 : nbbits_ntuples = 0;
721 643706 : k = 1;
722 6357832 : while ( k < nt / 2 )
723 : {
724 5714126 : nbbits_ntuples++;
725 5714126 : k = k << 1;
726 : }
727 :
728 643706 : t = 0;
729 643706 : nbbits_signs = 0;
730 643706 : nbbits_lsbs = 0;
731 :
732 643706 : if ( hm_cfg )
733 : {
734 : int16_t a1_i, b1_i;
735 : int32_t c[2], *ctx;
736 : int32_t p1, p2;
737 : int16_t ii[2];
738 : int16_t idx1, idx2, idx;
739 : int16_t numPeakIndicesOrig, numHoleIndices;
740 :
741 : /* Rate flag */
742 8628 : if ( nbbits > 400 )
743 : {
744 3793 : rateFlag = 2 << NBITS_CONTEXT;
745 : }
746 : else
747 : {
748 4835 : rateFlag = 0;
749 : }
750 :
751 8628 : c[0] = c[1] = 0;
752 :
753 : /* mapped domain */
754 8628 : numPeakIndicesOrig = hm_cfg->numPeakIndices;
755 8628 : hm_cfg->numPeakIndices = min( hm_cfg->numPeakIndices, lastnz );
756 8628 : numHoleIndices = lastnz - hm_cfg->numPeakIndices;
757 :
758 : /* Mark hole indices beyond lastnz as pruned */
759 3986386 : for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
760 : {
761 3977758 : hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] + nt;
762 : }
763 :
764 8628 : ii[0] = numPeakIndicesOrig;
765 8628 : ii[1] = 0;
766 :
767 8628 : p1 = p2 = 0; /* to avoid compilation warnings */
768 :
769 8628 : lsbs_bits = ptr + nbbits - 1;
770 :
771 : /*Start Encoding*/
772 : /* Initialize range encoder */
773 8628 : rc_uni_enc_init( &rc_st_enc );
774 :
775 : /*Main Loop through the 2-tuples*/
776 8628 : b1_i = -1;
777 :
778 1498134 : for ( k = 0; k < lastnz; k += 2 )
779 : {
780 1489506 : a1_i = get_next_coeff_mapped( ii, &p1, &idx1, hm_cfg );
781 1489506 : b1_i = get_next_coeff_mapped( ii, &p2, &idx2, hm_cfg );
782 :
783 1489506 : idx = min( idx1, idx2 );
784 :
785 : /* Get context */
786 1489506 : ctx = &c[p1 | p2];
787 :
788 1489506 : t = (uint16_t) ( *ctx + rateFlag );
789 1489506 : t += ( nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
790 :
791 : /* Init current 2-tuple encoding */
792 1489506 : a1 = (int16_t) abs( x[a1_i] );
793 1489506 : b1 = (int16_t) abs( x[b1_i] );
794 :
795 1489506 : lev1 = -1;
796 :
797 : /*Signs encoding*/
798 1489506 : if ( a1 > 0 )
799 : {
800 377579 : signs[nbbits_signs++] = ( (uint16_t) x[a1_i] >> ( sizeof( uint16_t ) * 8 - 1 ) );
801 : }
802 :
803 1489506 : if ( b1 > 0 )
804 : {
805 374294 : signs[nbbits_signs++] = ( (uint16_t) x[b1_i] >> ( sizeof( uint16_t ) * 8 - 1 ) );
806 : }
807 :
808 : /* MSBs coding */
809 1677983 : while ( max( a1, b1 ) >= A_THRES )
810 : {
811 188477 : pki = ari_lookup_s17_LC[t + ( ( lev1 + 1 ) << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
812 188477 : rc_uni_enc_encode_symbol_fastS( &rc_st_enc, VAL_ESC, cum_freq_ari_pk_s17_LC_ext[pki], sym_freq_ari_pk_s17_LC_ext[pki], 14 ); /* Encode ESC symbol */
813 :
814 188477 : *lsbs_bits-- = a1 & 1;
815 188477 : *lsbs_bits-- = b1 & 1;
816 :
817 : /* LSBs bit counting */
818 188477 : nbbits_lsbs += 2;
819 :
820 188477 : a1 >>= 1;
821 188477 : b1 >>= 1;
822 :
823 188477 : lev1 = min( lev1 + 1, 2 );
824 : }
825 :
826 1489506 : pki = ari_lookup_s17_LC[t + ( ( lev1 + 1 ) << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
827 1489506 : rc_uni_enc_encode_symbol_fastS( &rc_st_enc, a1 + A_THRES * b1, cum_freq_ari_pk_s17_LC_ext[pki], sym_freq_ari_pk_s17_LC_ext[pki], 14 ); /* Encode MSB symbol */
828 :
829 : /*Confirm that there is no overflow, i.e. bit-budget has not exceeded */
830 : #ifdef DEBUGGING
831 : assert( rc_uni_enc_virtual_finish( &rc_st_enc ) <= nbbits - nbbits_signs - nbbits_lsbs - nbbits_ntuples );
832 : #endif
833 :
834 : /* Update context for next 2-tuple */
835 1489506 : if ( p1 == p2 )
836 : {
837 : /* peak-peak or hole-hole context */
838 :
839 1359661 : if ( lev1 <= 0 )
840 : {
841 1323446 : t = 1 + ( a1 + b1 ) * ( lev1 + 2 );
842 : }
843 : else
844 : {
845 36215 : t = 13 + lev1;
846 : }
847 :
848 1359661 : *ctx = ( *ctx & 0xf ) * 16 + t;
849 : }
850 : else
851 : {
852 : /* mixed context */
853 :
854 129845 : if ( idx1 & 1 )
855 : {
856 : /* update first context */
857 61509 : c[p1] = update_mixed_context( c[p1], (int16_t) abs( x[a1_i] ) );
858 : }
859 :
860 129845 : if ( idx2 & 1 )
861 : {
862 : /* update second context */
863 68336 : c[p2] = update_mixed_context( c[p2], (int16_t) abs( x[b1_i] ) );
864 : }
865 : }
866 :
867 : } /*end of the 2-tuples loop*/
868 : }
869 : else /* if (!hm_cfg) */
870 : {
871 : int16_t cp;
872 : int16_t esc_nb, rateQ;
873 : uint16_t s;
874 :
875 : /* Rate flag */
876 635078 : if ( nbbits > 400 )
877 : {
878 447909 : rateFlag = 2;
879 : }
880 : else
881 : {
882 187169 : rateFlag = 0;
883 : }
884 :
885 635078 : s = 0;
886 :
887 : /* Find last non-zero tuple */
888 : /* ensure termination of while loop by dummy value */
889 635078 : a1 = x[0];
890 635078 : x[0] = 1; /* ensure first tuple is non-zero */
891 :
892 5582236 : while ( x[lastnz - 1] == 0 && x[lastnz - 2] == 0 )
893 : {
894 4947158 : lastnz -= 2;
895 : }
896 635078 : x[0] = a1;
897 :
898 635078 : lsbs_bits = ptr + nbbits - 1;
899 :
900 : /*Start Encoding*/
901 : /* Initialize range encoder */
902 635078 : rc_uni_enc_init( &rc_st_enc );
903 :
904 : /*Main Loop through the 2-tuples*/
905 131696874 : for ( k = 0; k < lastnz; k += 2 )
906 : {
907 :
908 : /* Init current 2-tuple encoding */
909 131061796 : a1 = (int16_t) abs( x[k + 0] );
910 131061796 : b1 = (int16_t) abs( x[k + 1] );
911 :
912 131061796 : lev1 = 0;
913 131061796 : esc_nb = 0;
914 :
915 : /*Signs encoding*/
916 131061796 : if ( a1 > 0 )
917 : {
918 54228330 : signs[nbbits_signs++] = ( (uint16_t) x[k + 0] >> ( sizeof( uint16_t ) * 8 - 1 ) );
919 : }
920 :
921 131061796 : if ( b1 > 0 )
922 : {
923 54135010 : signs[nbbits_signs++] = ( (uint16_t) x[k + 1] >> ( sizeof( uint16_t ) * 8 - 1 ) );
924 : }
925 :
926 131061796 : rateQ = rateFlag + ( k > ( nt >> 1 ) );
927 :
928 : /* MSBs coding */
929 159614214 : while ( max( a1, b1 ) >= A_THRES )
930 : {
931 28552418 : pki = ari_lookup_s17_LC[t + ( ( rateQ ) << NBITS_CONTEXT ) + ( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
932 28552418 : rc_uni_enc_encode_symbol_fastS( &rc_st_enc, VAL_ESC, cum_freq_ari_pk_s17_LC_ext[pki], sym_freq_ari_pk_s17_LC_ext[pki], 14 ); /* Encode ESC symbol */
933 :
934 28552418 : *lsbs_bits-- = a1 & 1;
935 28552418 : *lsbs_bits-- = b1 & 1;
936 :
937 : /* LSBs bit counting */
938 28552418 : nbbits_lsbs += 2;
939 :
940 28552418 : a1 >>= 1;
941 28552418 : b1 >>= 1;
942 :
943 28552418 : lev1++;
944 28552418 : esc_nb = min( lev1, 3 );
945 : }
946 :
947 131061796 : pki = ari_lookup_s17_LC[t + ( ( rateQ ) << NBITS_CONTEXT ) + ( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
948 131061796 : rc_uni_enc_encode_symbol_fastS( &rc_st_enc, a1 + A_THRES * b1, cum_freq_ari_pk_s17_LC_ext[pki], sym_freq_ari_pk_s17_LC_ext[pki], 14 ); /* Encode MSB symbol */
949 :
950 : /*Confirm that there is no overflow, i.e. bit-budget has not exceeded */
951 : #ifdef DEBUGGING
952 : assert( rc_uni_enc_virtual_finish( &rc_st_enc ) <= nbbits - nbbits_signs - nbbits_lsbs - nbbits_ntuples );
953 : #endif
954 :
955 : /* Update context for next 2-tuple */
956 131061796 : if ( esc_nb < 2 )
957 : {
958 123914157 : cp = 1 + ( ( a1 + b1 ) * ( esc_nb + 1 ) );
959 : }
960 : else
961 : {
962 7147639 : cp = 12 + esc_nb;
963 : }
964 :
965 : /*Shift old 4 bits, replace last 4 bits*/
966 : #ifdef FIX_1464_UBSAN_RC_CONTEXT_MAP
967 131061796 : s = s & 0x0F;
968 : #endif
969 131061796 : s = ( s << 4 ) + cp;
970 131061796 : t = s & 0xFF;
971 :
972 : } /*end of the 2-tuples loop*/
973 : }
974 :
975 : /* Finish range encoder */
976 643706 : rc_tot_bits = rc_uni_enc_finish( &rc_st_enc ); /* No. of bits consumed by range coder */
977 643706 : bp = rc_tot_bits + nbbits_ntuples; /* Update bitstream pointer */
978 :
979 : /* Cross-check that there is no overflow */
980 : #ifdef DEBUGGING
981 : assert( k == lastnz );
982 : #endif
983 :
984 : /* Push number of encoded tuples */
985 643706 : value = ( lastnz >> 1 ) - 1;
986 643706 : push_next_indice( hBstr, value, nbbits_ntuples );
987 :
988 : /* Push range coded bits from byte_buffer to bitstream */
989 : /* 1) Push all complete bytes, one byte at a time */
990 48578271 : for ( k = 0; k < ( rc_tot_bits >> 3 ); k++ )
991 : {
992 47934565 : push_next_indice( hBstr, rc_st_enc.byte_buffer[k], 8 );
993 : }
994 : /* 2) Push remaining bits */
995 643706 : if ( ( rc_tot_bits & 7 ) != 0 )
996 : {
997 564945 : push_next_indice( hBstr, rc_st_enc.byte_buffer[k] >> ( 8 - ( rc_tot_bits & 7 ) ), rc_tot_bits & 7 );
998 : }
999 :
1000 : /* Push sign bits */
1001 643706 : push_next_bits( hBstr, (uint16_t *) signs, nbbits_signs );
1002 643706 : bp += nbbits_signs;
1003 :
1004 : /*write residual Quantization bits*/
1005 :
1006 3967476 : for ( k = 0; k < min( nbbits - bp - nbbits_lsbs, resQMaxBits ); k++ )
1007 : {
1008 3323770 : ptr[nbbits - 1 - nbbits_lsbs - k] = x[nt + k];
1009 : }
1010 : /* Write filler bits */
1011 22266232 : for ( ; k < nbbits - bp - nbbits_lsbs; ++k )
1012 : {
1013 21622526 : ptr[nbbits - 1 - nbbits_lsbs - k] = 0;
1014 : }
1015 :
1016 : /* Check for debugging */
1017 643706 : assert( bp + k <= nbbits );
1018 :
1019 : /* Push the rest of the buffer */
1020 643706 : push_next_bits( hBstr, (uint16_t *) &ptr[bp], nbbits - bp );
1021 :
1022 : /* return (bp+nbbits_lsbs);*/ /*return only for debug plot*/
1023 643706 : return;
1024 : }
1025 :
1026 : /*-------------------------------------------------------------------*
1027 : * RCcontextMapping_encode2_estimate_no_mem_s17_LCS()
1028 : *
1029 : * Range coder bit-estimation
1030 : *-------------------------------------------------------------------*/
1031 :
1032 2924359 : int16_t RCcontextMapping_encode2_estimate_no_mem_s17_LCS(
1033 : int16_t *x, /* Spectral coefficients */
1034 : const int16_t nt, /* L - size of spectrum (no. of spectral coefficients) */
1035 : int16_t *lastnz_out,
1036 : int16_t *nEncoded, /* No. of spectral coefficients that can be coded without an overflow occuring */
1037 : const int16_t target, /* Target bits */
1038 : int16_t *stop,
1039 : int16_t mode,
1040 : CONTEXT_HM_CONFIG *hm_cfg /* context-based harmonic model configuration */
1041 : )
1042 : {
1043 : /* Common variables */
1044 : int16_t a1, b1;
1045 : int16_t k, pki, lev1;
1046 : uint16_t t;
1047 : int16_t lastnz, lastnz2;
1048 : int16_t rateFlag;
1049 : float bit_estimate;
1050 : int16_t symbol;
1051 : const uint8_t *lookup;
1052 : float nbits2;
1053 :
1054 : /* Initialization */
1055 2924359 : bit_estimate = 2.0f;
1056 2924359 : nbits2 = 0.f;
1057 :
1058 : /* bits to encode lastnz */
1059 2924359 : k = 1;
1060 :
1061 28729246 : while ( k < nt / 2 )
1062 : {
1063 25804887 : bit_estimate++;
1064 25804887 : k = k << 1;
1065 : /* check while condition */
1066 : }
1067 :
1068 2924359 : nbits2 = bit_estimate;
1069 :
1070 2924359 : if ( hm_cfg )
1071 : {
1072 : int16_t a1_i, b1_i;
1073 : int16_t stop2;
1074 : int16_t total_output_bits;
1075 : int16_t nt_half;
1076 : int32_t c[2], *ctx;
1077 : int32_t p1, p2;
1078 : int16_t ii[2];
1079 : int16_t idx1, idx2, idx;
1080 279228 : int16_t numPeakIndicesOrig = 0, numHoleIndices = 0; /* only to avoid compiler warning */
1081 :
1082 : /* Rate flag */
1083 279228 : if ( target > 400 )
1084 : {
1085 162866 : rateFlag = 2 << NBITS_CONTEXT; /* Select context-A for higher bitrates */
1086 : }
1087 : else
1088 : {
1089 116362 : rateFlag = 0; /* Select context-B for lower bitrates */
1090 : }
1091 :
1092 279228 : nt_half = nt >> 1;
1093 279228 : stop2 = 0;
1094 279228 : c[0] = c[1] = 0;
1095 :
1096 : /* Find last non-zero tuple in the mapped domain signal */
1097 279228 : lastnz = find_last_nz_pair( x, nt, hm_cfg );
1098 :
1099 279228 : lastnz2 = 2;
1100 :
1101 : /* mapped domain */
1102 279228 : numPeakIndicesOrig = hm_cfg->numPeakIndices;
1103 279228 : hm_cfg->numPeakIndices = min( hm_cfg->numPeakIndices, lastnz );
1104 279228 : numHoleIndices = lastnz - hm_cfg->numPeakIndices;
1105 :
1106 : /* Mark hole indices beyond lastnz as pruned */
1107 103880574 : for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
1108 : {
1109 103601346 : hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] + nt;
1110 : }
1111 :
1112 279228 : ii[0] = numPeakIndicesOrig;
1113 279228 : ii[1] = 0;
1114 :
1115 279228 : p1 = p2 = 0; /* to avoid compilation warnings */
1116 :
1117 : /* Main Loop through the 2-tuples */
1118 52497842 : for ( k = 0; k < lastnz; k += 2 )
1119 : {
1120 52322965 : a1_i = get_next_coeff_mapped( ii, &p1, &idx1, hm_cfg );
1121 52322965 : b1_i = get_next_coeff_mapped( ii, &p2, &idx2, hm_cfg );
1122 :
1123 52322965 : idx = min( idx1, idx2 );
1124 :
1125 : /* Get context */
1126 52322965 : ctx = &c[p1 | p2];
1127 :
1128 52322965 : t = (uint16_t) ( *ctx + rateFlag );
1129 52322965 : t += ( nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
1130 :
1131 : /* Init current 2-tuple encoding */
1132 52322965 : a1 = (int16_t) abs( x[a1_i] );
1133 52322965 : b1 = (int16_t) abs( x[b1_i] );
1134 52322965 : lev1 = -( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
1135 :
1136 : /* Signs Bits */
1137 52322965 : bit_estimate += min( a1, 1 );
1138 52322965 : bit_estimate += min( b1, 1 );
1139 :
1140 : /* pre-compute address of ari_pk_s17_LC_ext[0][Val_esc] to avoid doing it multiple times inside the loop */
1141 52322965 : lookup = &ari_lookup_s17_LC[t] + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
1142 :
1143 : /* check while condition */
1144 : /* MSBs coding */
1145 57613968 : while ( max( a1, b1 ) >= A_THRES )
1146 : {
1147 5291003 : pki = lookup[lev1]; /* ESC symbol */
1148 :
1149 5291003 : bit_estimate = bit_estimate + ari_bit_estimate_s17_LC[pki][VAL_ESC];
1150 5291003 : bit_estimate += 2; /* Add 2 LSB bits corresponding to the bit-plane */
1151 :
1152 5291003 : ( a1 ) >>= 1;
1153 5291003 : ( b1 ) >>= 1;
1154 :
1155 5291003 : lev1 = min( lev1 + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) ), 2 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
1156 :
1157 : /* check while condition */
1158 : }
1159 :
1160 52322965 : pki = lookup[lev1];
1161 :
1162 52322965 : symbol = a1 + A_THRES * b1;
1163 52322965 : bit_estimate = bit_estimate + ari_bit_estimate_s17_LC[pki][symbol];
1164 :
1165 : /* Should we truncate? */
1166 52322965 : if ( bit_estimate > target )
1167 : {
1168 8851344 : stop2 = 1;
1169 :
1170 8851344 : if ( *stop )
1171 : {
1172 104351 : break;
1173 : }
1174 : }
1175 : else
1176 : {
1177 43471621 : lastnz2 = b1_i + 1;
1178 43471621 : nbits2 = bit_estimate;
1179 : }
1180 :
1181 : /* Update context for next 2-tuple */
1182 52218614 : if ( p1 == p2 ) /* peak-peak or hole-hole context */
1183 : {
1184 43406672 : lev1 >>= NBITS_CONTEXT + NBITS_RATEQ;
1185 :
1186 43406672 : if ( lev1 <= 0 )
1187 : {
1188 42478086 : t = 1 + ( a1 + b1 ) * ( lev1 + 2 );
1189 : }
1190 : else
1191 : {
1192 928586 : t = 13 + lev1;
1193 : }
1194 :
1195 43406672 : *ctx = ( *ctx & 0xf ) * 16 + t;
1196 : }
1197 : else
1198 : {
1199 : /* mixed context */
1200 :
1201 8811942 : if ( idx1 & 1 )
1202 : {
1203 : /* update first context */
1204 4399603 : c[p1] = update_mixed_context( c[p1], (int16_t) abs( x[a1_i] ) );
1205 : }
1206 :
1207 8811942 : if ( idx2 & 1 )
1208 : {
1209 : /* update second context */
1210 4398640 : c[p2] = update_mixed_context( c[p2], (int16_t) abs( x[b1_i] ) );
1211 : }
1212 : }
1213 :
1214 : } /*end of the 2-tuples loop*/
1215 :
1216 279228 : total_output_bits = (int16_t) ( bit_estimate + 0.5f );
1217 279228 : if ( *stop )
1218 : {
1219 118754 : total_output_bits = (int16_t) ( nbits2 + 0.5f );
1220 : }
1221 :
1222 279228 : if ( stop2 )
1223 : {
1224 214715 : stop2 = total_output_bits;
1225 : }
1226 279228 : *nEncoded = lastnz2;
1227 279228 : *stop = stop2; /* If zero, it means no overflow occured during bit-estimation */
1228 279228 : *lastnz_out = lastnz;
1229 :
1230 : /* Restore hole indices beyond lastnz */
1231 103880574 : for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
1232 : {
1233 103601346 : hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] - nt;
1234 : }
1235 279228 : hm_cfg->numPeakIndices = numPeakIndicesOrig;
1236 :
1237 279228 : return (int16_t) ( nbits2 + 0.5f );
1238 : }
1239 : else /* if (!hm_cfg) */
1240 : {
1241 : int16_t esc_nb, cp, rateQ;
1242 : uint16_t s;
1243 : int16_t tot_bits2;
1244 2645131 : int16_t overflow_flag = 0;
1245 :
1246 : /* Rate flag */
1247 2645131 : if ( target > 400 )
1248 : {
1249 1962574 : rateFlag = 2;
1250 : }
1251 : else
1252 : {
1253 682557 : rateFlag = 0; /* Select context-B for lower bitrates */
1254 : }
1255 :
1256 2645131 : t = 0;
1257 2645131 : s = 0;
1258 2645131 : cp = 0;
1259 2645131 : lastnz = 1;
1260 2645131 : lastnz2 = 0;
1261 2645131 : tot_bits2 = 0;
1262 :
1263 : /* Find last non-zero tuple in the mapped domain signal */
1264 476600100 : for ( lastnz = ( nt - 2 ); lastnz >= 0; lastnz -= 2 )
1265 : {
1266 476540306 : if ( ( x[lastnz] != 0 ) || ( x[lastnz + 1] != 0 ) )
1267 : {
1268 : break;
1269 : }
1270 : }
1271 2645131 : lastnz += 2;
1272 2645131 : if ( lastnz < 2 )
1273 : {
1274 59794 : lastnz = 2; /* At least one tuple is coded */
1275 : }
1276 :
1277 2645131 : lastnz2 = 2;
1278 :
1279 : /* Main Loop through the 2-tuples */
1280 566297900 : for ( k = 0; k < lastnz; k += 2 )
1281 : {
1282 : /* Init current 2-tuple encoding */
1283 563652769 : a1 = (int16_t) abs( x[k] );
1284 563652769 : b1 = (int16_t) abs( x[k + 1] );
1285 563652769 : lev1 = 0;
1286 563652769 : esc_nb = 0;
1287 563652769 : rateQ = rateFlag + ( k > ( nt >> 1 ) );
1288 :
1289 : /* Signs Bits */
1290 563652769 : bit_estimate += min( a1, 1 );
1291 563652769 : bit_estimate += min( b1, 1 );
1292 :
1293 : /* pre-compute address of ari_pk_s17_LC_ext[0][Val_esc] to avoid doing it multiple times inside the loop */
1294 563652769 : lookup = &ari_lookup_s17_LC[t + ( rateQ << NBITS_CONTEXT )];
1295 :
1296 : /* check while condition */
1297 : /* MSBs coding */
1298 692618964 : while ( max( a1, b1 ) >= A_THRES )
1299 : {
1300 128966195 : pki = lookup[( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
1301 :
1302 128966195 : bit_estimate = bit_estimate + ari_bit_estimate_s17_LC[pki][VAL_ESC];
1303 128966195 : bit_estimate += 2; /* Add 2 LSB bits corresponding to the bit-plane */
1304 :
1305 128966195 : ( a1 ) >>= 1;
1306 128966195 : ( b1 ) >>= 1;
1307 :
1308 128966195 : lev1++;
1309 128966195 : esc_nb = min( lev1, 3 );
1310 :
1311 : /* check while condition */
1312 : }
1313 :
1314 563652769 : pki = lookup[( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
1315 :
1316 563652769 : symbol = a1 + A_THRES * b1;
1317 563652769 : bit_estimate = bit_estimate + ari_bit_estimate_s17_LC[pki][symbol];
1318 :
1319 : /* Should we truncate? */
1320 563652769 : if ( bit_estimate > target ) /* Overflow occured */
1321 : {
1322 21440155 : overflow_flag = 1;
1323 : }
1324 : else
1325 : {
1326 542212614 : if ( abs( x[k] ) || abs( x[k + 1] ) ) /* No overflow & non-zero tuple */
1327 : {
1328 329446159 : nbits2 = bit_estimate;
1329 329446159 : lastnz2 = k + 2;
1330 : }
1331 : }
1332 :
1333 : /* Update context for next 2-tuple */
1334 563652769 : if ( esc_nb < 2 )
1335 : {
1336 531308679 : cp = 1 + ( a1 + b1 ) * ( esc_nb + 1 );
1337 : }
1338 : else
1339 : {
1340 32344090 : cp = 12 + esc_nb;
1341 : }
1342 :
1343 : /*shift old bits and replace last 4 bits*/
1344 : #ifdef FIX_1464_UBSAN_RC_CONTEXT_MAP
1345 563652769 : s = s & 0x0F;
1346 : #endif
1347 563652769 : s = ( s << 4 ) + cp;
1348 563652769 : t = s & 0xFF;
1349 :
1350 : } /*end of the 2-tuples loop*/
1351 :
1352 2645131 : tot_bits2 = (int16_t) ( nbits2 + 0.5f );
1353 2645131 : if ( lastnz2 < lastnz ) /* Overflow occured because unable to code all tuples */
1354 : {
1355 1063947 : overflow_flag = 1;
1356 : }
1357 2645131 : if ( mode == -1 )
1358 : {
1359 0 : tot_bits2 = (int16_t) ( bit_estimate + 0.5f );
1360 : }
1361 2645131 : if ( overflow_flag == 0 ) /* No overflow */
1362 : {
1363 1581184 : *stop = 0;
1364 : }
1365 : else /* Overflow */
1366 : {
1367 1063947 : if ( *stop )
1368 : {
1369 6324 : *stop = tot_bits2;
1370 : }
1371 : else
1372 : {
1373 1057623 : *stop = (int16_t) ( bit_estimate + 0.5f );
1374 : }
1375 : }
1376 :
1377 2645131 : *lastnz_out = lastnz;
1378 2645131 : *nEncoded = lastnz2;
1379 : /* Safety mechanism to avoid overflow */
1380 2645131 : if ( lastnz2 == 2 && overflow_flag == 1 )
1381 : {
1382 120 : for ( k = 0; k < lastnz2; k++ )
1383 : {
1384 80 : x[k] = 0;
1385 : }
1386 : }
1387 :
1388 2645131 : return tot_bits2;
1389 : }
1390 : }
1391 :
1392 : /*-------------------------------------------------------------------*
1393 : * RCcontextMapping_encode2_estimate_bandWise_start()
1394 : *
1395 : * Range coder - start bandwise bit-estimation
1396 : *-------------------------------------------------------------------*/
1397 :
1398 415348 : int16_t RCcontextMapping_encode2_estimate_bandWise_start(
1399 : int16_t *x,
1400 : const int16_t nt,
1401 : const int16_t target,
1402 : HANDLE_RC_CONTEXT_MEM hContextMem )
1403 : {
1404 : int16_t i, k;
1405 :
1406 : /* Rate flag */
1407 415348 : if ( target > 400 )
1408 : {
1409 406684 : hContextMem->rateFlag = 2 << NBITS_CONTEXT;
1410 : }
1411 : else
1412 : {
1413 8664 : hContextMem->rateFlag = 0;
1414 : }
1415 :
1416 415348 : hContextMem->bit_estimate = 2.0f;
1417 :
1418 : /* Init */
1419 415348 : hContextMem->nt_half = nt >> 1;
1420 :
1421 : /* bits to encode lastnz */
1422 415348 : k = 1;
1423 :
1424 3898672 : while ( k < hContextMem->nt_half )
1425 : {
1426 3483324 : hContextMem->bit_estimate++;
1427 :
1428 3483324 : k = k << 1;
1429 : /* check while condition */
1430 : }
1431 :
1432 : /* bits to encode lastnz */
1433 415348 : hContextMem->nbits_old = (int16_t) hContextMem->bit_estimate;
1434 :
1435 415348 : hContextMem->ctx = 0;
1436 415348 : hContextMem->lastnz = 2;
1437 :
1438 : /* Find last non-zero tuple */
1439 :
1440 7665560 : for ( i = nt; i >= 4; i -= 2 )
1441 : {
1442 :
1443 7661210 : if ( x[i - 2] != 0 || x[i - 1] != 0 )
1444 : {
1445 410998 : hContextMem->lastnz = i;
1446 410998 : break;
1447 : }
1448 : }
1449 :
1450 415348 : return (int16_t) hContextMem->bit_estimate;
1451 : }
1452 :
1453 : /*-------------------------------------------------------------------*
1454 : * RCcontextMapping_encode2_estimate_bandWise()
1455 : *
1456 : * Range coder - bandwise bit-estimation
1457 : *-------------------------------------------------------------------*/
1458 :
1459 16892640 : int16_t RCcontextMapping_encode2_estimate_bandWise(
1460 : int16_t *x,
1461 : const int16_t start_line,
1462 : const int16_t end_line,
1463 : HANDLE_RC_CONTEXT_MEM hContextMem )
1464 : {
1465 : int16_t a1, b1, a1_i, b1_i;
1466 : int16_t k, pki, lev1;
1467 : uint16_t t;
1468 16892640 : int16_t bandBits = 0;
1469 : int16_t total_output_bits; /* No. of bits after finalization */
1470 : int16_t symbol;
1471 : const uint8_t *lookup;
1472 : int16_t idx;
1473 :
1474 : /* Main Loop through the 2-tuples */
1475 : /*hContextMem->nt_half = end_line >> 1;*/
1476 124911403 : for ( k = start_line; k < min( hContextMem->lastnz, end_line ); k += 2 )
1477 : {
1478 108018763 : a1_i = k;
1479 108018763 : b1_i = k + 1;
1480 :
1481 108018763 : idx = k;
1482 :
1483 : /* Get context */
1484 108018763 : t = hContextMem->ctx + hContextMem->rateFlag;
1485 108018763 : t += ( hContextMem->nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
1486 :
1487 : /* Init current 2-tuple encoding */
1488 108018763 : a1 = (int16_t) abs( x[a1_i] );
1489 108018763 : b1 = (int16_t) abs( x[b1_i] );
1490 108018763 : lev1 = -( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
1491 :
1492 : /* Signs Bits */
1493 108018763 : hContextMem->bit_estimate += min( a1, 1 );
1494 108018763 : hContextMem->bit_estimate += min( b1, 1 );
1495 :
1496 : /* pre-compute address of ari_pk_s17_LC_ext[0][Val_esc] to avoid doing it multiple times inside the loop */
1497 108018763 : lookup = &ari_lookup_s17_LC[t] + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
1498 :
1499 : /* check while condition */
1500 : /* MSBs coding */
1501 137293879 : while ( max( a1, b1 ) >= A_THRES )
1502 : {
1503 29275116 : pki = lookup[lev1];
1504 29275116 : hContextMem->bit_estimate = hContextMem->bit_estimate + ari_bit_estimate_s17_LC[pki][VAL_ESC];
1505 29275116 : hContextMem->bit_estimate += 2; /* Add the 2 LSB bits that were shifted out */
1506 :
1507 29275116 : ( a1 ) >>= 1;
1508 29275116 : ( b1 ) >>= 1;
1509 :
1510 29275116 : lev1 = min( lev1 + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) ), 2 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
1511 : /* check while condition */
1512 : }
1513 :
1514 108018763 : pki = lookup[lev1];
1515 108018763 : symbol = a1 + A_THRES * b1; /* MSB symbol */
1516 108018763 : hContextMem->bit_estimate = hContextMem->bit_estimate + ari_bit_estimate_s17_LC[pki][symbol];
1517 :
1518 : /* Update context */
1519 108018763 : lev1 >>= NBITS_CONTEXT + NBITS_RATEQ;
1520 :
1521 108018763 : if ( lev1 <= 0 )
1522 : {
1523 100606773 : t = 1 + ( a1 + b1 ) * ( lev1 + 2 );
1524 : }
1525 : else
1526 : {
1527 7411990 : t = 13 + lev1;
1528 : }
1529 :
1530 108018763 : hContextMem->ctx = ( hContextMem->ctx & 0xf ) * 16 + t;
1531 :
1532 : } /*end of the 2-tuples loop*/
1533 16892640 : total_output_bits = (int16_t) ( hContextMem->bit_estimate + 0.5f );
1534 :
1535 16892640 : bandBits = total_output_bits - hContextMem->nbits_old;
1536 16892640 : hContextMem->nbits_old = total_output_bits;
1537 :
1538 16892640 : return bandBits;
1539 : }
|