Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2026 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 4761 : 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 4761 : a1 = 0; /* to avoid compilation warnings */
87 4761 : b1 = 0; /* to avoid compilation warnings */
88 :
89 : /* Rate flag */
90 4761 : if ( nbbits > 400 )
91 : {
92 3062 : rateFlag = 2 << NBITS_CONTEXT;
93 : }
94 : else
95 : {
96 1699 : rateFlag = 0;
97 : }
98 :
99 : /* Init */
100 4761 : nt_half = nt >> 1;
101 4761 : c[0] = c[1] = 0;
102 :
103 : /* Bits for encoding the number of encoded tuples */
104 4761 : nbbits_ntuples = 0;
105 4761 : k = 1;
106 :
107 46336 : while ( k < nt / 2 )
108 : {
109 41575 : nbbits_ntuples++;
110 41575 : k = k << 1;
111 : }
112 :
113 4761 : t = 0;
114 4761 : bp = nbbits_ntuples;
115 4761 : nbbits_signs = 0;
116 4761 : nbbits_lsbs = 0;
117 4761 : nbbits_m2 = nbbits - 2;
118 4761 : flag_overflow = 0;
119 :
120 4761 : if ( hm_cfg )
121 : {
122 : /* mapped domain */
123 444 : numPeakIndicesOrig = hm_cfg->numPeakIndices;
124 444 : hm_cfg->numPeakIndices = min( hm_cfg->numPeakIndices, lastnz );
125 444 : numHoleIndices = lastnz - hm_cfg->numPeakIndices;
126 :
127 : /* Mark hole indices beyond lastnz as pruned */
128 74052 : for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
129 : {
130 73608 : hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] + nt;
131 : }
132 :
133 444 : ii[0] = numPeakIndicesOrig;
134 444 : ii[1] = 0;
135 :
136 444 : p1 = p2 = 0; /* to avoid compilation warnings */
137 : }
138 : else
139 : {
140 : /* unmapped domain */
141 4317 : ii[0] = 0;
142 :
143 4317 : p1 = p2 = 0;
144 :
145 : /* Find last non-zero tuple */
146 : /* ensure termination of while loop by dummy value */
147 4317 : a1 = x[0];
148 4317 : x[0] = 1;
149 :
150 13561 : while ( x[lastnz - 1] == 0 && x[lastnz - 2] == 0 )
151 : {
152 9244 : lastnz -= 2;
153 : }
154 4317 : x[0] = a1;
155 : }
156 :
157 4761 : lsbs_bits = (int16_t *) ptr + nbbits - 1;
158 :
159 : /*Start Encoding*/
160 4761 : ari_start_encoding_14bits( &as );
161 :
162 : /*Main Loop through the 2-tuples*/
163 4761 : b1_i = -1;
164 :
165 914087 : for ( k = 0; k < lastnz; k += 2 )
166 : {
167 :
168 909330 : if ( hm_cfg )
169 : {
170 79211 : a1_i = get_next_coeff_mapped( ii, &p1, &idx1, hm_cfg );
171 79211 : b1_i = get_next_coeff_mapped( ii, &p2, &idx2, hm_cfg );
172 : }
173 : else
174 : {
175 830119 : a1_i = get_next_coeff_unmapped( ii, &idx1 );
176 830119 : b1_i = get_next_coeff_unmapped( ii, &idx2 );
177 : }
178 :
179 909330 : idx = min( idx1, idx2 );
180 :
181 : /* Get context */
182 909330 : ctx = &c[p1 | p2];
183 :
184 909330 : t = (uint16_t) ( *ctx + rateFlag );
185 909330 : t += ( nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
186 :
187 : /* Init current 2-tuple encoding */
188 :
189 909330 : if ( flag_overflow != 0 )
190 : {
191 0 : x[a1_i] = 0;
192 0 : x[b1_i] = 0;
193 : }
194 :
195 909330 : a1 = (int16_t) abs( x[a1_i] );
196 909330 : b1 = (int16_t) abs( x[b1_i] );
197 :
198 909330 : lev1 = -1;
199 :
200 : /*Copy states*/
201 909330 : ari_copy_states( &as, &as_overflow );
202 909330 : bp_overflow = bp;
203 909330 : nbbits_signs_overflow = nbbits_signs;
204 909330 : nbbits_lsbs_overflow = nbbits_lsbs;
205 :
206 : /*Signs encoding*/
207 :
208 909330 : if ( a1 > 0 )
209 : {
210 505452 : signs[nbbits_signs++] = ( (uint16_t) x[a1_i] >> ( sizeof( uint16_t ) * 8 - 1 ) );
211 : }
212 :
213 909330 : if ( b1 > 0 )
214 : {
215 509688 : signs[nbbits_signs++] = ( (uint16_t) x[b1_i] >> ( sizeof( uint16_t ) * 8 - 1 ) );
216 : }
217 :
218 : /* MSBs coding */
219 1325305 : while ( max( a1, b1 ) >= A_THRES )
220 : {
221 415975 : pki = ari_lookup_s17_LC[t + ( ( lev1 + 1 ) << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
222 415975 : bp = ari_encode_14bits_ext( ptr, bp, &as, VAL_ESC, ari_pk_s17_LC_ext[pki] );
223 :
224 415975 : *lsbs_bits-- = a1 & 1;
225 415975 : *lsbs_bits-- = b1 & 1;
226 :
227 : /* LSBs bit counting */
228 415975 : nbbits_lsbs += 2;
229 :
230 415975 : a1 >>= 1;
231 415975 : b1 >>= 1;
232 :
233 415975 : lev1 = min( lev1 + 1, 2 );
234 : }
235 :
236 909330 : pki = ari_lookup_s17_LC[t + ( ( lev1 + 1 ) << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
237 909330 : 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 909330 : if ( bp + as.bits_to_follow + nbbits_signs + nbbits_lsbs > nbbits_m2 )
242 : {
243 4 : ari_copy_states( &as_overflow, &as );
244 4 : bp = bp_overflow;
245 :
246 4 : if ( !flag_overflow )
247 : {
248 4 : nbbits_signs = nbbits_signs_overflow;
249 4 : nbbits_lsbs = nbbits_lsbs_overflow;
250 :
251 4 : 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 3 : break;
273 : }
274 : }
275 : else
276 : {
277 0 : break;
278 : }
279 : }
280 :
281 : /* Update context for next 2-tuple */
282 909326 : if ( p1 == p2 )
283 : {
284 : /* peak-peak or hole-hole context */
285 :
286 898355 : if ( lev1 <= 0 )
287 : {
288 790750 : t = 1 + ( a1 + b1 ) * ( lev1 + 2 );
289 : }
290 : else
291 : {
292 107605 : t = 13 + lev1;
293 : }
294 :
295 898355 : *ctx = ( *ctx & 0xf ) * 16 + t;
296 : }
297 : else
298 : {
299 : /* mixed context */
300 :
301 10971 : if ( idx1 & 1 )
302 : {
303 : /* update first context */
304 5524 : c[p1] = update_mixed_context( c[p1], (int16_t) abs( x[a1_i] ) );
305 : }
306 :
307 10971 : if ( idx2 & 1 )
308 : {
309 : /* update second context */
310 5447 : 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 4761 : bp = ari_done_encoding_14bits( ptr, bp, &as );
318 :
319 : /*Overflow is detected*/
320 :
321 4761 : if ( k != lastnz )
322 : {
323 :
324 4 : 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 3 : lastnz = k;
336 : }
337 : }
338 :
339 : /* Push number of encoded tuples */
340 4761 : value = ( lastnz >> 1 ) - 1;
341 4761 : push_next_indice( hBstr, value, nbbits_ntuples );
342 :
343 : /* Push arithmetic coded bits */
344 4761 : push_next_bits( hBstr, (uint16_t *) &ptr[nbbits_ntuples], bp - nbbits_ntuples );
345 :
346 : /* Push sign bits */
347 4761 : push_next_bits( hBstr, (uint16_t *) signs, nbbits_signs );
348 4761 : bp += nbbits_signs;
349 :
350 : /* write residual Quantization bits */
351 24011 : for ( k = 0; k < min( nbbits - bp - nbbits_lsbs, resQMaxBits ); k++ )
352 : {
353 19250 : ptr[nbbits - 1 - nbbits_lsbs - k] = x[nt + k];
354 : }
355 :
356 : /* Write filler bits */
357 27043 : for ( ; k < nbbits - bp - nbbits_lsbs; ++k )
358 : {
359 22282 : ptr[nbbits - 1 - nbbits_lsbs - k] = 0;
360 : }
361 :
362 : /* Check for debugging */
363 4761 : assert( bp + k <= nbbits );
364 :
365 : /* Push the rest of the buffer */
366 4761 : 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 4761 : return;
372 : }
373 :
374 : /*-------------------------------------------------------------------*
375 : * find_last_nz_pair()
376 : *
377 : *
378 : *-------------------------------------------------------------------*/
379 :
380 838193 : 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 838193 : last_nz = 2;
389 :
390 838193 : if ( hm_cfg )
391 : {
392 : /* mapped kernel */
393 815634 : tmp = hm_cfg->indexBuffer;
394 :
395 142233752 : for ( i = length; i >= 4; i -= 2 )
396 : {
397 :
398 142197927 : if ( x[tmp[i - 2]] != 0 || x[tmp[i - 1]] != 0 )
399 : {
400 779809 : last_nz = i;
401 779809 : break;
402 : }
403 : }
404 : }
405 : else
406 : {
407 : /* unmapped kernel */
408 :
409 2879142 : for ( i = length; i >= 4; i -= 2 )
410 : {
411 :
412 2879142 : if ( x[i - 2] != 0 || x[i - 1] != 0 )
413 : {
414 22559 : last_nz = i;
415 22559 : break;
416 : }
417 : }
418 : }
419 :
420 838193 : return last_nz;
421 : }
422 :
423 :
424 : /*-------------------------------------------------------------------*
425 : * ACcontextMapping_encode2_estimate_no_mem_s17_LC()
426 : *
427 : *
428 : *-------------------------------------------------------------------*/
429 :
430 29147 : 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 29147 : int16_t numPeakIndicesOrig = 0, numHoleIndices = 0; /* only to avoid compiler warning */
457 :
458 : /* Rate flag */
459 29147 : if ( target > 400 )
460 : {
461 17318 : rateFlag = 2 << NBITS_CONTEXT;
462 : }
463 : else
464 : {
465 11829 : rateFlag = 0;
466 : }
467 :
468 : /* 2 bits = arithmetic coder initialization interval = 1 bits for rounding last proba + 1 bit?*/
469 29147 : nbits = 2;
470 : /*proba coded on 14bits -> proba=1*/
471 29147 : proba = 16384;
472 :
473 : /* Init */
474 29147 : nt_half = nt >> 1;
475 29147 : stop2 = 0;
476 29147 : c[0] = c[1] = 0;
477 :
478 : /* bits to encode lastnz */
479 29147 : k = 1;
480 :
481 284019 : while ( k < nt / 2 )
482 : {
483 254872 : nbits++;
484 254872 : k = k << 1;
485 : /* check while condition */
486 : }
487 29147 : nbits_old = nbits;
488 :
489 29147 : nbits -= target;
490 :
491 : /* Find last non-zero tuple in the mapped domain signal */
492 29147 : lastnz = find_last_nz_pair( x, nt, hm_cfg );
493 :
494 : /* At least one tuple is coded */
495 29147 : lastnz2 = 2;
496 :
497 29147 : if ( hm_cfg )
498 : {
499 : /* mapped domain */
500 6588 : numPeakIndicesOrig = hm_cfg->numPeakIndices;
501 6588 : hm_cfg->numPeakIndices = min( hm_cfg->numPeakIndices, lastnz );
502 6588 : numHoleIndices = lastnz - hm_cfg->numPeakIndices;
503 :
504 : /* Mark hole indices beyond lastnz as pruned */
505 1473762 : for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
506 : {
507 1467174 : hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] + nt;
508 : }
509 :
510 6588 : ii[0] = numPeakIndicesOrig;
511 6588 : ii[1] = 0;
512 :
513 6588 : p1 = p2 = 0; /* to avoid compilation warnings */
514 : }
515 : else
516 : {
517 : /* unmapped domain */
518 22559 : ii[0] = 0;
519 :
520 22559 : p1 = p2 = 0;
521 : }
522 :
523 : /* Main Loop through the 2-tuples */
524 5486724 : for ( k = 0; k < lastnz; k += 2 )
525 : {
526 5460014 : if ( hm_cfg )
527 : {
528 1144014 : a1_i = get_next_coeff_mapped( ii, &p1, &idx1, hm_cfg );
529 1144014 : b1_i = get_next_coeff_mapped( ii, &p2, &idx2, hm_cfg );
530 : }
531 : else
532 : {
533 4316000 : a1_i = get_next_coeff_unmapped( ii, &idx1 );
534 4316000 : b1_i = get_next_coeff_unmapped( ii, &idx2 );
535 : }
536 :
537 5460014 : idx = min( idx1, idx2 );
538 :
539 : /* Get context */
540 5460014 : ctx = &c[p1 | p2];
541 :
542 5460014 : t = (uint16_t) ( *ctx + rateFlag );
543 5460014 : t += ( nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
544 :
545 : /* Init current 2-tuple encoding */
546 5460014 : a1 = (int16_t) abs( x[a1_i] );
547 5460014 : b1 = (int16_t) abs( x[b1_i] );
548 5460014 : lev1 = -( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
549 :
550 : /* Signs Bits */
551 5460014 : nbits += min( a1, 1 );
552 5460014 : 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 5460014 : lookup = &ari_lookup_s17_LC[t] + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
556 :
557 : /* check while condition */
558 : /* MSBs coding */
559 7581566 : while ( max( a1, b1 ) >= A_THRES )
560 : {
561 2121552 : pki = lookup[lev1];
562 2121552 : cum_freq = ari_pk_s17_LC_ext[pki] + VAL_ESC;
563 : /*p1*p2=proba on 28 bits: p=0.5->power(2,27)*/
564 2121552 : proba *= *( cum_freq );
565 : /*Number of leading zero computed in one cycle=norm_l() in BASOP*/
566 2121552 : nlz = 2;
567 4642275 : while ( proba < 134217728 ) /*power(2,27)*/
568 : {
569 2520723 : nlz++;
570 2520723 : proba = proba << 1;
571 : }
572 2121552 : nbits += nlz;
573 : /*addition added as shift not done in norm_l(): real shift = 14-nlz*/
574 2121552 : proba >>= 14; /*proba is rounded down on 14 bits ->automatic over-estimation of bit consumption*/
575 :
576 2121552 : ( a1 ) >>= 1;
577 2121552 : ( b1 ) >>= 1;
578 :
579 2121552 : lev1 = min( lev1 + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) ), 2 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
580 : /* check while condition */
581 : }
582 5460014 : pki = lookup[lev1];
583 5460014 : symbol = a1 + A_THRES * b1;
584 5460014 : cum_freq = ari_pk_s17_LC_ext[pki] + symbol;
585 : /*p1*p2=proba on 28 bits: p=0.5->power(2,27)*/
586 5460014 : proba *= ( cum_freq[0] - cum_freq[1] );
587 : /*Number of leading zero computed in one cycle=norm_l() in BASOP*/
588 5460014 : nlz = 0;
589 21973623 : while ( proba < 134217728 ) /*power(2,27)*/
590 : {
591 16513609 : nlz++;
592 16513609 : proba = proba << 1;
593 : }
594 :
595 5460014 : nbits += nlz;
596 5460014 : proba >>= 14; /*proba is rounded down on 14 bits ->automatic over-estimation of bit consumption*/
597 :
598 : /* Should we truncate? */
599 5460014 : if ( nbits > 0 )
600 : {
601 199656 : stop2 = 1;
602 :
603 199656 : if ( *stop )
604 : {
605 2437 : break;
606 : }
607 : }
608 : else
609 : {
610 5260358 : if ( hm_cfg || k == 0 || x[a1_i] || x[b1_i] )
611 : {
612 4117447 : nbits_old = nbits + target;
613 4117447 : lastnz2 = b1_i + 1;
614 : }
615 : }
616 :
617 : /* Update context for next 2-tuple */
618 5457577 : if ( p1 == p2 ) /* peak-peak or hole-hole context */
619 : {
620 5272922 : lev1 >>= NBITS_CONTEXT + NBITS_RATEQ;
621 :
622 5272922 : if ( lev1 <= 0 )
623 : {
624 4727492 : t = 1 + ( a1 + b1 ) * ( lev1 + 2 );
625 : }
626 : else
627 : {
628 545430 : t = 13 + lev1;
629 : }
630 :
631 5272922 : *ctx = ( *ctx & 0xf ) * 16 + t;
632 : }
633 : else
634 : {
635 : /* mixed context */
636 :
637 184655 : if ( idx1 & 1 )
638 : {
639 : /* update first context */
640 92615 : c[p1] = update_mixed_context( c[p1], (int16_t) abs( x[a1_i] ) );
641 : }
642 :
643 184655 : if ( idx2 & 1 )
644 : {
645 : /* update second context */
646 91790 : c[p2] = update_mixed_context( c[p2], (int16_t) abs( x[b1_i] ) );
647 : }
648 : }
649 : } /*end of the 2-tuples loop*/
650 :
651 29147 : nbits += target;
652 :
653 : /* Output */
654 :
655 29147 : if ( *stop )
656 : {
657 2678 : nbits = nbits_old;
658 : }
659 :
660 29147 : if ( stop2 )
661 : {
662 15170 : stop2 = nbits;
663 : }
664 29147 : *nEncoded = lastnz2;
665 29147 : *stop = stop2;
666 29147 : *lastnz_out = lastnz;
667 :
668 29147 : if ( hm_cfg )
669 : {
670 : /* Restore hole indices beyond lastnz */
671 1473762 : for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
672 : {
673 1467174 : hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] - nt;
674 : }
675 6588 : hm_cfg->numPeakIndices = numPeakIndicesOrig;
676 : }
677 :
678 29147 : 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 2457825 : 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 2457825 : a1 = 0; /* to avoid compilation warnings */
714 2457825 : b1 = 0; /* to avoid compilation warnings */
715 :
716 : /* Init */
717 2457825 : nt_half = nt >> 1;
718 :
719 : /* Bits for encoding the number of encoded tuples */
720 2457825 : nbbits_ntuples = 0;
721 2457825 : k = 1;
722 24260981 : while ( k < nt / 2 )
723 : {
724 21803156 : nbbits_ntuples++;
725 21803156 : k = k << 1;
726 : }
727 :
728 2457825 : t = 0;
729 2457825 : nbbits_signs = 0;
730 2457825 : nbbits_lsbs = 0;
731 :
732 2457825 : 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 18786 : if ( nbbits > 400 )
743 : {
744 8893 : rateFlag = 2 << NBITS_CONTEXT;
745 : }
746 : else
747 : {
748 9893 : rateFlag = 0;
749 : }
750 :
751 18786 : c[0] = c[1] = 0;
752 :
753 : /* mapped domain */
754 18786 : numPeakIndicesOrig = hm_cfg->numPeakIndices;
755 18786 : hm_cfg->numPeakIndices = min( hm_cfg->numPeakIndices, lastnz );
756 18786 : numHoleIndices = lastnz - hm_cfg->numPeakIndices;
757 :
758 : /* Mark hole indices beyond lastnz as pruned */
759 7630602 : for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
760 : {
761 7611816 : hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] + nt;
762 : }
763 :
764 18786 : ii[0] = numPeakIndicesOrig;
765 18786 : ii[1] = 0;
766 :
767 18786 : p1 = p2 = 0; /* to avoid compilation warnings */
768 :
769 18786 : lsbs_bits = ptr + nbbits - 1;
770 :
771 : /*Start Encoding*/
772 : /* Initialize range encoder */
773 18786 : rc_uni_enc_init( &rc_st_enc );
774 :
775 : /*Main Loop through the 2-tuples*/
776 18786 : b1_i = -1;
777 :
778 3333576 : for ( k = 0; k < lastnz; k += 2 )
779 : {
780 3314790 : a1_i = get_next_coeff_mapped( ii, &p1, &idx1, hm_cfg );
781 3314790 : b1_i = get_next_coeff_mapped( ii, &p2, &idx2, hm_cfg );
782 :
783 3314790 : idx = min( idx1, idx2 );
784 :
785 : /* Get context */
786 3314790 : ctx = &c[p1 | p2];
787 :
788 3314790 : t = (uint16_t) ( *ctx + rateFlag );
789 3314790 : t += ( nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
790 :
791 : /* Init current 2-tuple encoding */
792 3314790 : a1 = (int16_t) abs( x[a1_i] );
793 3314790 : b1 = (int16_t) abs( x[b1_i] );
794 :
795 3314790 : lev1 = -1;
796 :
797 : /*Signs encoding*/
798 3314790 : if ( a1 > 0 )
799 : {
800 895208 : signs[nbbits_signs++] = ( (uint16_t) x[a1_i] >> ( sizeof( uint16_t ) * 8 - 1 ) );
801 : }
802 :
803 3314790 : if ( b1 > 0 )
804 : {
805 885556 : signs[nbbits_signs++] = ( (uint16_t) x[b1_i] >> ( sizeof( uint16_t ) * 8 - 1 ) );
806 : }
807 :
808 : /* MSBs coding */
809 3776923 : while ( max( a1, b1 ) >= A_THRES )
810 : {
811 462133 : pki = ari_lookup_s17_LC[t + ( ( lev1 + 1 ) << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
812 462133 : 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 462133 : *lsbs_bits-- = a1 & 1;
815 462133 : *lsbs_bits-- = b1 & 1;
816 :
817 : /* LSBs bit counting */
818 462133 : nbbits_lsbs += 2;
819 :
820 462133 : a1 >>= 1;
821 462133 : b1 >>= 1;
822 :
823 462133 : lev1 = min( lev1 + 1, 2 );
824 : }
825 :
826 3314790 : pki = ari_lookup_s17_LC[t + ( ( lev1 + 1 ) << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
827 3314790 : 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 3314790 : if ( p1 == p2 )
836 : {
837 : /* peak-peak or hole-hole context */
838 :
839 3000725 : if ( lev1 <= 0 )
840 : {
841 2911914 : t = 1 + ( a1 + b1 ) * ( lev1 + 2 );
842 : }
843 : else
844 : {
845 88811 : t = 13 + lev1;
846 : }
847 :
848 3000725 : *ctx = ( *ctx & 0xf ) * 16 + t;
849 : }
850 : else
851 : {
852 : /* mixed context */
853 :
854 314065 : if ( idx1 & 1 )
855 : {
856 : /* update first context */
857 149606 : c[p1] = update_mixed_context( c[p1], (int16_t) abs( x[a1_i] ) );
858 : }
859 :
860 314065 : if ( idx2 & 1 )
861 : {
862 : /* update second context */
863 164459 : 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 2439039 : if ( nbbits > 400 )
877 : {
878 1742977 : rateFlag = 2;
879 : }
880 : else
881 : {
882 696062 : rateFlag = 0;
883 : }
884 :
885 2439039 : s = 0;
886 :
887 : /* Find last non-zero tuple */
888 : /* ensure termination of while loop by dummy value */
889 2439039 : a1 = x[0];
890 2439039 : x[0] = 1; /* ensure first tuple is non-zero */
891 :
892 23781018 : while ( x[lastnz - 1] == 0 && x[lastnz - 2] == 0 )
893 : {
894 21341979 : lastnz -= 2;
895 : }
896 2439039 : x[0] = a1;
897 :
898 2439039 : lsbs_bits = ptr + nbbits - 1;
899 :
900 : /*Start Encoding*/
901 : /* Initialize range encoder */
902 2439039 : rc_uni_enc_init( &rc_st_enc );
903 :
904 : /*Main Loop through the 2-tuples*/
905 512935086 : for ( k = 0; k < lastnz; k += 2 )
906 : {
907 :
908 : /* Init current 2-tuple encoding */
909 510496047 : a1 = (int16_t) abs( x[k + 0] );
910 510496047 : b1 = (int16_t) abs( x[k + 1] );
911 :
912 510496047 : lev1 = 0;
913 510496047 : esc_nb = 0;
914 :
915 : /*Signs encoding*/
916 510496047 : if ( a1 > 0 )
917 : {
918 211230453 : signs[nbbits_signs++] = ( (uint16_t) x[k + 0] >> ( sizeof( uint16_t ) * 8 - 1 ) );
919 : }
920 :
921 510496047 : if ( b1 > 0 )
922 : {
923 213020137 : signs[nbbits_signs++] = ( (uint16_t) x[k + 1] >> ( sizeof( uint16_t ) * 8 - 1 ) );
924 : }
925 :
926 510496047 : rateQ = rateFlag + ( k > ( nt >> 1 ) );
927 :
928 : /* MSBs coding */
929 626167209 : while ( max( a1, b1 ) >= A_THRES )
930 : {
931 115671162 : pki = ari_lookup_s17_LC[t + ( ( rateQ ) << NBITS_CONTEXT ) + ( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
932 115671162 : 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 115671162 : *lsbs_bits-- = a1 & 1;
935 115671162 : *lsbs_bits-- = b1 & 1;
936 :
937 : /* LSBs bit counting */
938 115671162 : nbbits_lsbs += 2;
939 :
940 115671162 : a1 >>= 1;
941 115671162 : b1 >>= 1;
942 :
943 115671162 : lev1++;
944 115671162 : esc_nb = min( lev1, 3 );
945 : }
946 :
947 510496047 : pki = ari_lookup_s17_LC[t + ( ( rateQ ) << NBITS_CONTEXT ) + ( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
948 510496047 : 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 510496047 : if ( esc_nb < 2 )
957 : {
958 481524607 : cp = 1 + ( ( a1 + b1 ) * ( esc_nb + 1 ) );
959 : }
960 : else
961 : {
962 28971440 : cp = 12 + esc_nb;
963 : }
964 :
965 : /*Shift old 4 bits, replace last 4 bits*/
966 510496047 : s = s & 0x0F;
967 510496047 : s = ( s << 4 ) + cp;
968 510496047 : t = s & 0xFF;
969 :
970 : } /*end of the 2-tuples loop*/
971 : }
972 :
973 : /* Finish range encoder */
974 2457825 : rc_tot_bits = rc_uni_enc_finish( &rc_st_enc ); /* No. of bits consumed by range coder */
975 2457825 : bp = rc_tot_bits + nbbits_ntuples; /* Update bitstream pointer */
976 :
977 : /* Cross-check that there is no overflow */
978 : #ifdef DEBUGGING
979 : assert( k == lastnz );
980 : #endif
981 :
982 : /* Push number of encoded tuples */
983 2457825 : value = ( lastnz >> 1 ) - 1;
984 2457825 : push_next_indice( hBstr, value, nbbits_ntuples );
985 :
986 : /* Push range coded bits from byte_buffer to bitstream */
987 : /* 1) Push all complete bytes, one byte at a time */
988 188737661 : for ( k = 0; k < ( rc_tot_bits >> 3 ); k++ )
989 : {
990 186279836 : push_next_indice( hBstr, rc_st_enc.byte_buffer[k], 8 );
991 : }
992 : /* 2) Push remaining bits */
993 2457825 : if ( ( rc_tot_bits & 7 ) != 0 )
994 : {
995 2156302 : push_next_indice( hBstr, rc_st_enc.byte_buffer[k] >> ( 8 - ( rc_tot_bits & 7 ) ), rc_tot_bits & 7 );
996 : }
997 :
998 : /* Push sign bits */
999 2457825 : push_next_bits( hBstr, (uint16_t *) signs, nbbits_signs );
1000 2457825 : bp += nbbits_signs;
1001 :
1002 : /*write residual Quantization bits*/
1003 :
1004 14064967 : for ( k = 0; k < min( nbbits - bp - nbbits_lsbs, resQMaxBits ); k++ )
1005 : {
1006 11607142 : ptr[nbbits - 1 - nbbits_lsbs - k] = x[nt + k];
1007 : }
1008 : /* Write filler bits */
1009 153067931 : for ( ; k < nbbits - bp - nbbits_lsbs; ++k )
1010 : {
1011 150610106 : ptr[nbbits - 1 - nbbits_lsbs - k] = 0;
1012 : }
1013 :
1014 : /* Check for debugging */
1015 2457825 : assert( bp + k <= nbbits );
1016 :
1017 : /* Push the rest of the buffer */
1018 2457825 : push_next_bits( hBstr, (uint16_t *) &ptr[bp], nbbits - bp );
1019 :
1020 : /* return (bp+nbbits_lsbs);*/ /*return only for debug plot*/
1021 2457825 : return;
1022 : }
1023 :
1024 : /*-------------------------------------------------------------------*
1025 : * RCcontextMapping_encode2_estimate_no_mem_s17_LCS()
1026 : *
1027 : * Range coder bit-estimation
1028 : *-------------------------------------------------------------------*/
1029 :
1030 10909867 : int16_t RCcontextMapping_encode2_estimate_no_mem_s17_LCS(
1031 : int16_t *x, /* Spectral coefficients */
1032 : const int16_t nt, /* L - size of spectrum (no. of spectral coefficients) */
1033 : int16_t *lastnz_out,
1034 : int16_t *nEncoded, /* No. of spectral coefficients that can be coded without an overflow occuring */
1035 : const int16_t target, /* Target bits */
1036 : int16_t *stop,
1037 : int16_t mode,
1038 : CONTEXT_HM_CONFIG *hm_cfg /* context-based harmonic model configuration */
1039 : )
1040 : {
1041 : /* Common variables */
1042 : int16_t a1, b1;
1043 : int16_t k, pki, lev1;
1044 : uint16_t t;
1045 : int16_t lastnz, lastnz2;
1046 : int16_t rateFlag;
1047 : float bit_estimate;
1048 : int16_t symbol;
1049 : const uint8_t *lookup;
1050 : float nbits2;
1051 :
1052 : /* Initialization */
1053 10909867 : bit_estimate = 2.0f;
1054 10909867 : nbits2 = 0.f;
1055 :
1056 : /* bits to encode lastnz */
1057 10909867 : k = 1;
1058 :
1059 106941111 : while ( k < nt / 2 )
1060 : {
1061 96031244 : bit_estimate++;
1062 96031244 : k = k << 1;
1063 : /* check while condition */
1064 : }
1065 :
1066 10909867 : nbits2 = bit_estimate;
1067 :
1068 10909867 : if ( hm_cfg )
1069 : {
1070 : int16_t a1_i, b1_i;
1071 : int16_t stop2;
1072 : int16_t total_output_bits;
1073 : int16_t nt_half;
1074 : int32_t c[2], *ctx;
1075 : int32_t p1, p2;
1076 : int16_t ii[2];
1077 : int16_t idx1, idx2, idx;
1078 809046 : int16_t numPeakIndicesOrig = 0, numHoleIndices = 0; /* only to avoid compiler warning */
1079 :
1080 : /* Rate flag */
1081 809046 : if ( target > 400 )
1082 : {
1083 517800 : rateFlag = 2 << NBITS_CONTEXT; /* Select context-A for higher bitrates */
1084 : }
1085 : else
1086 : {
1087 291246 : rateFlag = 0; /* Select context-B for lower bitrates */
1088 : }
1089 :
1090 809046 : nt_half = nt >> 1;
1091 809046 : stop2 = 0;
1092 809046 : c[0] = c[1] = 0;
1093 :
1094 : /* Find last non-zero tuple in the mapped domain signal */
1095 809046 : lastnz = find_last_nz_pair( x, nt, hm_cfg );
1096 :
1097 809046 : lastnz2 = 2;
1098 :
1099 : /* mapped domain */
1100 809046 : numPeakIndicesOrig = hm_cfg->numPeakIndices;
1101 809046 : hm_cfg->numPeakIndices = min( hm_cfg->numPeakIndices, lastnz );
1102 809046 : numHoleIndices = lastnz - hm_cfg->numPeakIndices;
1103 :
1104 : /* Mark hole indices beyond lastnz as pruned */
1105 266847258 : for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
1106 : {
1107 266038212 : hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] + nt;
1108 : }
1109 :
1110 809046 : ii[0] = numPeakIndicesOrig;
1111 809046 : ii[1] = 0;
1112 :
1113 809046 : p1 = p2 = 0; /* to avoid compilation warnings */
1114 :
1115 : /* Main Loop through the 2-tuples */
1116 157210975 : for ( k = 0; k < lastnz; k += 2 )
1117 : {
1118 156726873 : a1_i = get_next_coeff_mapped( ii, &p1, &idx1, hm_cfg );
1119 156726873 : b1_i = get_next_coeff_mapped( ii, &p2, &idx2, hm_cfg );
1120 :
1121 156726873 : idx = min( idx1, idx2 );
1122 :
1123 : /* Get context */
1124 156726873 : ctx = &c[p1 | p2];
1125 :
1126 156726873 : t = (uint16_t) ( *ctx + rateFlag );
1127 156726873 : t += ( nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
1128 :
1129 : /* Init current 2-tuple encoding */
1130 156726873 : a1 = (int16_t) abs( x[a1_i] );
1131 156726873 : b1 = (int16_t) abs( x[b1_i] );
1132 156726873 : lev1 = -( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
1133 :
1134 : /* Signs Bits */
1135 156726873 : bit_estimate += min( a1, 1 );
1136 156726873 : bit_estimate += min( b1, 1 );
1137 :
1138 : /* pre-compute address of ari_pk_s17_LC_ext[0][Val_esc] to avoid doing it multiple times inside the loop */
1139 156726873 : lookup = &ari_lookup_s17_LC[t] + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
1140 :
1141 : /* check while condition */
1142 : /* MSBs coding */
1143 175575569 : while ( max( a1, b1 ) >= A_THRES )
1144 : {
1145 18848696 : pki = lookup[lev1]; /* ESC symbol */
1146 :
1147 18848696 : bit_estimate = bit_estimate + ari_bit_estimate_s17_LC[pki][VAL_ESC];
1148 18848696 : bit_estimate += 2; /* Add 2 LSB bits corresponding to the bit-plane */
1149 :
1150 18848696 : ( a1 ) >>= 1;
1151 18848696 : ( b1 ) >>= 1;
1152 :
1153 18848696 : lev1 = min( lev1 + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) ), 2 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
1154 :
1155 : /* check while condition */
1156 : }
1157 :
1158 156726873 : pki = lookup[lev1];
1159 :
1160 156726873 : symbol = a1 + A_THRES * b1;
1161 156726873 : bit_estimate = bit_estimate + ari_bit_estimate_s17_LC[pki][symbol];
1162 :
1163 : /* Should we truncate? */
1164 156726873 : if ( bit_estimate > target )
1165 : {
1166 28046444 : stop2 = 1;
1167 :
1168 28046444 : if ( *stop )
1169 : {
1170 324944 : break;
1171 : }
1172 : }
1173 : else
1174 : {
1175 128680429 : lastnz2 = b1_i + 1;
1176 128680429 : nbits2 = bit_estimate;
1177 : }
1178 :
1179 : /* Update context for next 2-tuple */
1180 156401929 : if ( p1 == p2 ) /* peak-peak or hole-hole context */
1181 : {
1182 128284288 : lev1 >>= NBITS_CONTEXT + NBITS_RATEQ;
1183 :
1184 128284288 : if ( lev1 <= 0 )
1185 : {
1186 124902338 : t = 1 + ( a1 + b1 ) * ( lev1 + 2 );
1187 : }
1188 : else
1189 : {
1190 3381950 : t = 13 + lev1;
1191 : }
1192 :
1193 128284288 : *ctx = ( *ctx & 0xf ) * 16 + t;
1194 : }
1195 : else
1196 : {
1197 : /* mixed context */
1198 :
1199 28117641 : if ( idx1 & 1 )
1200 : {
1201 : /* update first context */
1202 14038389 : c[p1] = update_mixed_context( c[p1], (int16_t) abs( x[a1_i] ) );
1203 : }
1204 :
1205 28117641 : if ( idx2 & 1 )
1206 : {
1207 : /* update second context */
1208 14031351 : c[p2] = update_mixed_context( c[p2], (int16_t) abs( x[b1_i] ) );
1209 : }
1210 : }
1211 :
1212 : } /*end of the 2-tuples loop*/
1213 :
1214 809046 : total_output_bits = (int16_t) ( bit_estimate + 0.5f );
1215 809046 : if ( *stop )
1216 : {
1217 359668 : total_output_bits = (int16_t) ( nbits2 + 0.5f );
1218 : }
1219 :
1220 809046 : if ( stop2 )
1221 : {
1222 658295 : stop2 = total_output_bits;
1223 : }
1224 809046 : *nEncoded = lastnz2;
1225 809046 : *stop = stop2; /* If zero, it means no overflow occured during bit-estimation */
1226 809046 : *lastnz_out = lastnz;
1227 :
1228 : /* Restore hole indices beyond lastnz */
1229 266847258 : for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
1230 : {
1231 266038212 : hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] - nt;
1232 : }
1233 809046 : hm_cfg->numPeakIndices = numPeakIndicesOrig;
1234 :
1235 809046 : return (int16_t) ( nbits2 + 0.5f );
1236 : }
1237 : else /* if (!hm_cfg) */
1238 : {
1239 : int16_t esc_nb, cp, rateQ;
1240 : uint16_t s;
1241 : int16_t tot_bits2;
1242 10100821 : int16_t overflow_flag = 0;
1243 :
1244 : /* Rate flag */
1245 10100821 : if ( target > 400 )
1246 : {
1247 7704270 : rateFlag = 2;
1248 : }
1249 : else
1250 : {
1251 2396551 : rateFlag = 0; /* Select context-B for lower bitrates */
1252 : }
1253 :
1254 10100821 : t = 0;
1255 10100821 : s = 0;
1256 10100821 : cp = 0;
1257 10100821 : lastnz = 1;
1258 10100821 : lastnz2 = 0;
1259 10100821 : tot_bits2 = 0;
1260 :
1261 : /* Find last non-zero tuple in the mapped domain signal */
1262 1756606145 : for ( lastnz = ( nt - 2 ); lastnz >= 0; lastnz -= 2 )
1263 : {
1264 1756372999 : if ( ( x[lastnz] != 0 ) || ( x[lastnz + 1] != 0 ) )
1265 : {
1266 : break;
1267 : }
1268 : }
1269 10100821 : lastnz += 2;
1270 10100821 : if ( lastnz < 2 )
1271 : {
1272 233146 : lastnz = 2; /* At least one tuple is coded */
1273 : }
1274 :
1275 10100821 : lastnz2 = 2;
1276 :
1277 : /* Main Loop through the 2-tuples */
1278 2243189387 : for ( k = 0; k < lastnz; k += 2 )
1279 : {
1280 : /* Init current 2-tuple encoding */
1281 2233088566 : a1 = (int16_t) abs( x[k] );
1282 2233088566 : b1 = (int16_t) abs( x[k + 1] );
1283 2233088566 : lev1 = 0;
1284 2233088566 : esc_nb = 0;
1285 2233088566 : rateQ = rateFlag + ( k > ( nt >> 1 ) );
1286 :
1287 : /* Signs Bits */
1288 2233088566 : bit_estimate += min( a1, 1 );
1289 2233088566 : bit_estimate += min( b1, 1 );
1290 :
1291 : /* pre-compute address of ari_pk_s17_LC_ext[0][Val_esc] to avoid doing it multiple times inside the loop */
1292 2233088566 : lookup = &ari_lookup_s17_LC[t + ( rateQ << NBITS_CONTEXT )];
1293 :
1294 : /* check while condition */
1295 : /* MSBs coding */
1296 2749452593 : while ( max( a1, b1 ) >= A_THRES )
1297 : {
1298 516364027 : pki = lookup[( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
1299 :
1300 516364027 : bit_estimate = bit_estimate + ari_bit_estimate_s17_LC[pki][VAL_ESC];
1301 516364027 : bit_estimate += 2; /* Add 2 LSB bits corresponding to the bit-plane */
1302 :
1303 516364027 : ( a1 ) >>= 1;
1304 516364027 : ( b1 ) >>= 1;
1305 :
1306 516364027 : lev1++;
1307 516364027 : esc_nb = min( lev1, 3 );
1308 :
1309 : /* check while condition */
1310 : }
1311 :
1312 2233088566 : pki = lookup[( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
1313 :
1314 2233088566 : symbol = a1 + A_THRES * b1;
1315 2233088566 : bit_estimate = bit_estimate + ari_bit_estimate_s17_LC[pki][symbol];
1316 :
1317 : /* Should we truncate? */
1318 2233088566 : if ( bit_estimate > target ) /* Overflow occured */
1319 : {
1320 83012390 : overflow_flag = 1;
1321 : }
1322 : else
1323 : {
1324 2150076176 : if ( abs( x[k] ) || abs( x[k + 1] ) ) /* No overflow & non-zero tuple */
1325 : {
1326 1313100876 : nbits2 = bit_estimate;
1327 1313100876 : lastnz2 = k + 2;
1328 : }
1329 : }
1330 :
1331 : /* Update context for next 2-tuple */
1332 2233088566 : if ( esc_nb < 2 )
1333 : {
1334 2103299090 : cp = 1 + ( a1 + b1 ) * ( esc_nb + 1 );
1335 : }
1336 : else
1337 : {
1338 129789476 : cp = 12 + esc_nb;
1339 : }
1340 :
1341 : /*shift old bits and replace last 4 bits*/
1342 2233088566 : s = s & 0x0F;
1343 2233088566 : s = ( s << 4 ) + cp;
1344 2233088566 : t = s & 0xFF;
1345 :
1346 : } /*end of the 2-tuples loop*/
1347 :
1348 10100821 : tot_bits2 = (int16_t) ( nbits2 + 0.5f );
1349 10100821 : if ( lastnz2 < lastnz ) /* Overflow occured because unable to code all tuples */
1350 : {
1351 4057582 : overflow_flag = 1;
1352 : }
1353 10100821 : if ( mode == -1 )
1354 : {
1355 0 : tot_bits2 = (int16_t) ( bit_estimate + 0.5f );
1356 : }
1357 10100821 : if ( overflow_flag == 0 ) /* No overflow */
1358 : {
1359 6043239 : *stop = 0;
1360 : }
1361 : else /* Overflow */
1362 : {
1363 4057582 : if ( *stop )
1364 : {
1365 13758 : *stop = tot_bits2;
1366 : }
1367 : else
1368 : {
1369 4043824 : *stop = (int16_t) ( bit_estimate + 0.5f );
1370 : }
1371 : }
1372 :
1373 10100821 : *lastnz_out = lastnz;
1374 10100821 : *nEncoded = lastnz2;
1375 : /* Safety mechanism to avoid overflow */
1376 10100821 : if ( lastnz2 == 2 && overflow_flag == 1 )
1377 : {
1378 3489 : for ( k = 0; k < lastnz2; k++ )
1379 : {
1380 2326 : x[k] = 0;
1381 : }
1382 : }
1383 :
1384 10100821 : return tot_bits2;
1385 : }
1386 : }
1387 :
1388 : /*-------------------------------------------------------------------*
1389 : * RCcontextMapping_encode2_estimate_bandWise_start()
1390 : *
1391 : * Range coder - start bandwise bit-estimation
1392 : *-------------------------------------------------------------------*/
1393 :
1394 1883520 : int16_t RCcontextMapping_encode2_estimate_bandWise_start(
1395 : int16_t *x,
1396 : const int16_t nt,
1397 : const int16_t target,
1398 : HANDLE_RC_CONTEXT_MEM hContextMem )
1399 : {
1400 : int16_t i, k;
1401 :
1402 : /* Rate flag */
1403 1883520 : if ( target > 400 )
1404 : {
1405 1850056 : hContextMem->rateFlag = 2 << NBITS_CONTEXT;
1406 : }
1407 : else
1408 : {
1409 33464 : hContextMem->rateFlag = 0;
1410 : }
1411 :
1412 1883520 : hContextMem->bit_estimate = 2.0f;
1413 :
1414 : /* Init */
1415 1883520 : hContextMem->nt_half = nt >> 1;
1416 :
1417 : /* bits to encode lastnz */
1418 1883520 : k = 1;
1419 :
1420 17729228 : while ( k < hContextMem->nt_half )
1421 : {
1422 15845708 : hContextMem->bit_estimate++;
1423 :
1424 15845708 : k = k << 1;
1425 : /* check while condition */
1426 : }
1427 :
1428 : /* bits to encode lastnz */
1429 1883520 : hContextMem->nbits_old = (int16_t) hContextMem->bit_estimate;
1430 :
1431 1883520 : hContextMem->ctx = 0;
1432 1883520 : hContextMem->lastnz = 2;
1433 :
1434 : /* Find last non-zero tuple */
1435 :
1436 37089804 : for ( i = nt; i >= 4; i -= 2 )
1437 : {
1438 :
1439 37057770 : if ( x[i - 2] != 0 || x[i - 1] != 0 )
1440 : {
1441 1851486 : hContextMem->lastnz = i;
1442 1851486 : break;
1443 : }
1444 : }
1445 :
1446 1883520 : return (int16_t) hContextMem->bit_estimate;
1447 : }
1448 :
1449 : /*-------------------------------------------------------------------*
1450 : * RCcontextMapping_encode2_estimate_bandWise()
1451 : *
1452 : * Range coder - bandwise bit-estimation
1453 : *-------------------------------------------------------------------*/
1454 :
1455 74002612 : int16_t RCcontextMapping_encode2_estimate_bandWise(
1456 : int16_t *x,
1457 : const int16_t start_line,
1458 : const int16_t end_line,
1459 : HANDLE_RC_CONTEXT_MEM hContextMem )
1460 : {
1461 : int16_t a1, b1, a1_i, b1_i;
1462 : int16_t k, pki, lev1;
1463 : uint16_t t;
1464 74002612 : int16_t bandBits = 0;
1465 : int16_t total_output_bits; /* No. of bits after finalization */
1466 : int16_t symbol;
1467 : const uint8_t *lookup;
1468 : int16_t idx;
1469 :
1470 : /* Main Loop through the 2-tuples */
1471 : /*hContextMem->nt_half = end_line >> 1;*/
1472 577197919 : for ( k = start_line; k < min( hContextMem->lastnz, end_line ); k += 2 )
1473 : {
1474 503195307 : a1_i = k;
1475 503195307 : b1_i = k + 1;
1476 :
1477 503195307 : idx = k;
1478 :
1479 : /* Get context */
1480 503195307 : t = hContextMem->ctx + hContextMem->rateFlag;
1481 503195307 : t += ( hContextMem->nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
1482 :
1483 : /* Init current 2-tuple encoding */
1484 503195307 : a1 = (int16_t) abs( x[a1_i] );
1485 503195307 : b1 = (int16_t) abs( x[b1_i] );
1486 503195307 : lev1 = -( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
1487 :
1488 : /* Signs Bits */
1489 503195307 : hContextMem->bit_estimate += min( a1, 1 );
1490 503195307 : hContextMem->bit_estimate += min( b1, 1 );
1491 :
1492 : /* pre-compute address of ari_pk_s17_LC_ext[0][Val_esc] to avoid doing it multiple times inside the loop */
1493 503195307 : lookup = &ari_lookup_s17_LC[t] + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
1494 :
1495 : /* check while condition */
1496 : /* MSBs coding */
1497 619661397 : while ( max( a1, b1 ) >= A_THRES )
1498 : {
1499 116466090 : pki = lookup[lev1];
1500 116466090 : hContextMem->bit_estimate = hContextMem->bit_estimate + ari_bit_estimate_s17_LC[pki][VAL_ESC];
1501 116466090 : hContextMem->bit_estimate += 2; /* Add the 2 LSB bits that were shifted out */
1502 :
1503 116466090 : ( a1 ) >>= 1;
1504 116466090 : ( b1 ) >>= 1;
1505 :
1506 116466090 : lev1 = min( lev1 + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) ), 2 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
1507 : /* check while condition */
1508 : }
1509 :
1510 503195307 : pki = lookup[lev1];
1511 503195307 : symbol = a1 + A_THRES * b1; /* MSB symbol */
1512 503195307 : hContextMem->bit_estimate = hContextMem->bit_estimate + ari_bit_estimate_s17_LC[pki][symbol];
1513 :
1514 : /* Update context */
1515 503195307 : lev1 >>= NBITS_CONTEXT + NBITS_RATEQ;
1516 :
1517 503195307 : if ( lev1 <= 0 )
1518 : {
1519 473871005 : t = 1 + ( a1 + b1 ) * ( lev1 + 2 );
1520 : }
1521 : else
1522 : {
1523 29324302 : t = 13 + lev1;
1524 : }
1525 :
1526 503195307 : hContextMem->ctx = ( hContextMem->ctx & 0xf ) * 16 + t;
1527 :
1528 : } /*end of the 2-tuples loop*/
1529 74002612 : total_output_bits = (int16_t) ( hContextMem->bit_estimate + 0.5f );
1530 :
1531 74002612 : bandBits = total_output_bits - hContextMem->nbits_old;
1532 74002612 : hContextMem->nbits_old = total_output_bits;
1533 :
1534 74002612 : return bandBits;
1535 : }
|