Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdint.h>
6 : #include <limits.h>
7 : #include "options.h" /* Compilation switches */
8 : #include "cnst.h" /* Common constants */
9 : #include "rom_com.h" /* Static table prototypes */
10 : #include "prot_fx.h" /* Function prototypes */
11 : #include "prot_fx_enc.h" /* Function prototypes */
12 :
13 :
14 : static void rc_enc_shift_fx( BSTR_ENC_HANDLE hBstr, PVQ_ENC_HANDLE hPVQ );
15 : static void rc_enc_write_fx( BSTR_ENC_HANDLE hBstr, Word16 byte, Word16 bits );
16 : static void rc_enc_shift_ivas_fx( BSTR_ENC_HANDLE hBstr, PVQ_ENC_HANDLE hPVQ );
17 : static void rc_enc_write_ivas_fx( BSTR_ENC_HANDLE hBstr, Word16 byte, Word16 bits );
18 :
19 : /*-------------------------------------------------------------------*
20 : * rc_enc_init()
21 : *
22 : * Initalize range coder
23 : *-------------------------------------------------------------------*/
24 :
25 30851 : void rc_enc_init_fx(
26 : PVQ_ENC_HANDLE hPVQ, /* i/o: PVQ encoder handle */
27 : Word16 tot_bits /* i : Total bit budget Q0*/
28 : )
29 : {
30 30851 : hPVQ->rc_low = L_deposit_l( 0 );
31 30851 : hPVQ->rc_range = 0xffffffff;
32 30851 : move32();
33 30851 : hPVQ->rc_cache = -1;
34 30851 : move16();
35 30851 : hPVQ->rc_carry = 0;
36 30851 : move16();
37 30851 : hPVQ->rc_carry_count = 0;
38 30851 : move16();
39 30851 : hPVQ->rc_num_bits = 0;
40 30851 : move16();
41 30851 : hPVQ->rc_tot_bits = tot_bits; // Q0
42 30851 : move16();
43 30851 : hPVQ->rc_offset = 0;
44 30851 : move16();
45 :
46 30851 : return;
47 : }
48 :
49 : /*-------------------------------------------------------------------*
50 : * rc_encode()
51 : *
52 : * Encode symbol with range coder
53 : *-------------------------------------------------------------------*/
54 304030 : void rc_encode_ivas_fx(
55 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
56 : PVQ_ENC_HANDLE hPVQ, /* i/o: PVQ encoder handle */
57 : UWord32 cum_freq, /* i : Cumulative frequency up to symbol Q0*/
58 : UWord32 sym_freq, /* i : Symbol probability Q0*/
59 : UWord32 tot /* i : Total cumulative frequency Q0*/
60 : )
61 : {
62 : UWord32 r, tmp, inv_tot, lsb;
63 : Word16 exp;
64 : UWord16 carry;
65 :
66 304030 : inv_tot = UL_inverse( tot, &exp ); // Q0
67 304030 : Mpy_32_32_uu( hPVQ->rc_range, inv_tot, &tmp, &lsb ); /*0+exp-32 */
68 304030 : r = UL_lshr( tmp, sub( exp, 32 ) ); /* exp-32-exp3+32 = 0 */
69 304030 : tmp = UL_Mpy_32_32( r, cum_freq );
70 :
71 304030 : hPVQ->rc_low = UL_addNs( hPVQ->rc_low, tmp, &carry ); // Q0
72 304030 : move32();
73 304030 : if ( carry != 0 )
74 : {
75 39886 : hPVQ->rc_carry = carry; // Q0
76 39886 : move16();
77 : }
78 :
79 304030 : hPVQ->rc_range = UL_Mpy_32_32( r, sym_freq ); // Q0
80 304030 : move32();
81 548723 : WHILE( LT_64( hPVQ->rc_range, 1 << 24 ) )
82 : {
83 244693 : hPVQ->rc_range = UL_lshl( hPVQ->rc_range, 8 ); // Q0
84 244693 : move32();
85 244693 : hPVQ->rc_num_bits = add( hPVQ->rc_num_bits, 8 ); // Q0
86 244693 : move16();
87 244693 : rc_enc_shift_ivas_fx( hBstr, hPVQ );
88 : }
89 :
90 304030 : return;
91 : }
92 20754 : void rc_encode_fx(
93 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
94 : PVQ_ENC_HANDLE hPVQ, /* i/o: PVQ encoder handle */
95 : UWord32 cum_freq, /* i : Cumulative frequency up to symbol Q0*/
96 : UWord32 sym_freq, /* i : Symbol probability Q0*/
97 : UWord32 tot /* i : Total cumulative frequency Q0*/
98 : )
99 : {
100 : UWord32 r, tmp, inv_tot, lsb;
101 : Word16 exp;
102 : UWord16 carry;
103 :
104 20754 : inv_tot = UL_inverse( tot, &exp ); // Q0
105 20754 : Mpy_32_32_uu( hPVQ->rc_range, inv_tot, &tmp, &lsb ); /*0+exp-32 */
106 20754 : r = UL_lshr( tmp, sub( exp, 32 ) ); /* exp-32-exp3+32 = 0 */
107 20754 : tmp = UL_Mpy_32_32( r, cum_freq );
108 :
109 20754 : hPVQ->rc_low = UL_addNs( hPVQ->rc_low, tmp, &carry ); // Q0
110 20754 : if ( carry != 0 )
111 : {
112 1613 : hPVQ->rc_carry = carry; // Q0
113 1613 : move16();
114 : }
115 :
116 20754 : hPVQ->rc_range = UL_Mpy_32_32( r, sym_freq ); // Q0
117 :
118 38657 : WHILE( hPVQ->rc_range < 1 << 24 )
119 : {
120 17903 : L_sub( 0, 0 ); /* Comparison in while */
121 17903 : hPVQ->rc_range = UL_lshl( hPVQ->rc_range, 8 ); // Q0
122 17903 : hPVQ->rc_num_bits = add( hPVQ->rc_num_bits, 8 ); // Q0
123 17903 : rc_enc_shift_fx( hBstr, hPVQ );
124 : }
125 :
126 20754 : return;
127 : }
128 :
129 : /*-------------------------------------------------------------------*
130 : * rc_enc_finish()
131 : *
132 : * Finalize range coder
133 : *-------------------------------------------------------------------*/
134 30451 : void rc_enc_finish_ivas_fx(
135 : BSTR_ENC_HANDLE hBstr,
136 : PVQ_ENC_HANDLE hPVQ /* i/o: PVQ encoder handle */
137 :
138 : )
139 : {
140 : UWord32 val, mask, high;
141 : Word16 bits;
142 : UWord16 over1, over2;
143 :
144 30451 : bits = add( norm_ul( hPVQ->rc_range ), 1 );
145 30451 : mask = UL_lshr( 0xffffffff, bits );
146 :
147 30451 : val = UL_addNs( hPVQ->rc_low, mask, &over1 ); // Q0
148 30451 : high = UL_addNs( hPVQ->rc_low, hPVQ->rc_range, &over2 ); // Q0
149 :
150 30451 : val = L_and( val, ~mask );
151 30451 : L_xor( 0, 0 ); /* For bit not */
152 :
153 30451 : IF( ( L_xor( over1, over2 ) ) == 0 )
154 : {
155 28968 : L_sub( 0, 0 ); /* For comparision in if */
156 28968 : IF( UL_addNsD( val, mask ) >= high )
157 : {
158 11211 : bits = add( bits, 1 ); // Q0
159 11211 : mask = UL_lshr( mask, 1 ); // Q0
160 11211 : val = UL_and( UL_addNsD( hPVQ->rc_low, mask ), ~mask ); // Q0
161 11211 : L_xor( 0, 0 ); /* For bit not */
162 : }
163 :
164 28968 : if ( LT_64( val, hPVQ->rc_low ) )
165 : {
166 2163 : hPVQ->rc_carry = 1;
167 2163 : move16();
168 : }
169 : }
170 :
171 30451 : hPVQ->rc_low = val;
172 30451 : move32();
173 :
174 30451 : IF( GT_16( bits, sub( hPVQ->rc_tot_bits, hPVQ->rc_num_bits ) ) )
175 : {
176 0 : bits = sub( hPVQ->rc_tot_bits, hPVQ->rc_num_bits ); // Q0
177 : }
178 :
179 30451 : hPVQ->rc_num_bits = add( hPVQ->rc_num_bits, bits ); // Q0
180 30451 : move16();
181 62535 : FOR( ; bits > 0; bits -= 8 )
182 : {
183 32084 : rc_enc_shift_ivas_fx( hBstr, hPVQ );
184 : }
185 30451 : bits = add( bits, 8 );
186 :
187 30451 : IF( hPVQ->rc_carry_count > 0 )
188 : {
189 10 : rc_enc_write_ivas_fx( hBstr, add( hPVQ->rc_cache, hPVQ->rc_carry ), 8 );
190 :
191 10 : FOR( ; hPVQ->rc_carry_count > 1; hPVQ->rc_carry_count-- )
192 : {
193 0 : rc_enc_write_ivas_fx( hBstr, add( hPVQ->rc_carry, 0xff ), 8 );
194 : }
195 10 : rc_enc_write_ivas_fx( hBstr, s_and( add( hPVQ->rc_carry, 0xff ), sub( lshl( 1, bits ), 1 ) ), bits );
196 : }
197 : ELSE
198 : {
199 30441 : rc_enc_write_ivas_fx( hBstr, lshr( add( hPVQ->rc_cache, hPVQ->rc_carry ), sub( 8, bits ) ), bits );
200 : }
201 :
202 30451 : bits = hPVQ->rc_num_bits; // Q0
203 30451 : move16();
204 30903 : WHILE( LT_16( bits, sub( hPVQ->rc_tot_bits, 16 ) ) )
205 : {
206 452 : rc_enc_write_ivas_fx( hBstr, 0, 16 );
207 452 : bits = add( bits, 16 );
208 : }
209 :
210 30451 : bits = sub( hPVQ->rc_tot_bits, bits ); // Q0
211 30451 : IF( bits > 0 )
212 : {
213 30418 : rc_enc_write_ivas_fx( hBstr, 0, bits );
214 : }
215 :
216 30451 : return;
217 : }
218 400 : void rc_enc_finish_fx(
219 : BSTR_ENC_HANDLE hBstr,
220 : PVQ_ENC_HANDLE hPVQ /* i/o: PVQ encoder handle */
221 :
222 : )
223 : {
224 : UWord32 val, mask, high;
225 : Word16 bits;
226 : UWord16 over1, over2;
227 :
228 400 : bits = add( norm_ul( hPVQ->rc_range ), 1 ); // Q0
229 400 : mask = UL_lshr( 0xffffffff, bits );
230 :
231 400 : val = UL_addNs( hPVQ->rc_low, mask, &over1 ); // Q0
232 400 : high = UL_addNs( hPVQ->rc_low, hPVQ->rc_range, &over2 ); // Q0
233 :
234 400 : val = L_and( val, ~mask );
235 400 : L_xor( 0, 0 ); /* For bit not */
236 :
237 400 : IF( ( L_xor( over1, over2 ) ) == 0 )
238 : {
239 376 : L_sub( 0, 0 ); /* For comparision in if */
240 376 : IF( UL_addNsD( val, mask ) >= high )
241 : {
242 223 : bits = add( bits, 1 ); // Q0
243 223 : mask = UL_lshr( mask, 1 ); // Q0
244 223 : val = UL_and( UL_addNsD( hPVQ->rc_low, mask ), ~mask ); // Q0
245 223 : L_xor( 0, 0 ); /* For bit not */
246 : }
247 :
248 376 : if ( val < hPVQ->rc_low )
249 : {
250 35 : hPVQ->rc_carry = 1;
251 35 : move16();
252 : }
253 : }
254 :
255 400 : hPVQ->rc_low = val;
256 400 : move32();
257 :
258 400 : IF( GT_16( bits, sub( hPVQ->rc_tot_bits, hPVQ->rc_num_bits ) ) )
259 : {
260 0 : bits = sub( hPVQ->rc_tot_bits, hPVQ->rc_num_bits ); // Q0
261 : }
262 :
263 400 : hPVQ->rc_num_bits = add( hPVQ->rc_num_bits, bits ); // Q0
264 819 : FOR( ; bits > 0; bits -= 8 )
265 : {
266 419 : rc_enc_shift_fx( hBstr, hPVQ );
267 : }
268 400 : bits = add( bits, 8 );
269 :
270 400 : IF( hPVQ->rc_carry_count > 0 )
271 : {
272 0 : rc_enc_write_fx( hBstr, add( hPVQ->rc_cache, hPVQ->rc_carry ), 8 );
273 :
274 0 : FOR( ; hPVQ->rc_carry_count > 1; hPVQ->rc_carry_count-- )
275 : {
276 0 : rc_enc_write_fx( hBstr, ( hPVQ->rc_carry + 0xff ), 8 );
277 : }
278 0 : rc_enc_write_fx( hBstr, s_and( add( hPVQ->rc_carry, 0xff ), sub( lshl( 1, bits ), 1 ) ), bits );
279 : }
280 : ELSE
281 : {
282 400 : rc_enc_write_fx( hBstr, lshr( add( hPVQ->rc_cache, hPVQ->rc_carry ), sub( 8, bits ) ), bits );
283 : }
284 :
285 400 : bits = hPVQ->rc_num_bits; // Q0
286 400 : move16();
287 400 : WHILE( LT_16( bits, sub( hPVQ->rc_tot_bits, 16 ) ) )
288 : {
289 0 : rc_enc_write_fx( hBstr, 0, 16 );
290 0 : bits = add( bits, 16 );
291 : }
292 :
293 400 : bits = sub( hPVQ->rc_tot_bits, bits );
294 400 : IF( bits > 0 )
295 : {
296 400 : rc_enc_write_fx( hBstr, 0, bits );
297 : }
298 :
299 400 : return;
300 : }
301 :
302 : /*-------------------------------------------------------------------*
303 : * rc_enc_shift()
304 : *
305 : * Shift a byte out to bitstream
306 : *-------------------------------------------------------------------*/
307 276777 : static void rc_enc_shift_ivas_fx(
308 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
309 : PVQ_ENC_HANDLE hPVQ /* i/o: PVQ encoder handle */
310 : )
311 : {
312 276777 : test();
313 276777 : L_sub( 0, 0 ); /* For comparision in if */
314 276777 : IF( LT_64( hPVQ->rc_low, 0xff000000UL ) || EQ_16( hPVQ->rc_carry, 1 ) )
315 : {
316 275634 : IF( hPVQ->rc_cache >= 0 )
317 : {
318 245183 : rc_enc_write_ivas_fx( hBstr, add( hPVQ->rc_cache, hPVQ->rc_carry ), 8 );
319 : }
320 :
321 276767 : WHILE( hPVQ->rc_carry_count > 0 )
322 : {
323 1133 : rc_enc_write_ivas_fx( hBstr, s_and( add( hPVQ->rc_carry, 0xff ), 255 ), 8 );
324 1133 : hPVQ->rc_carry_count = sub( hPVQ->rc_carry_count, 1 ); // Q0
325 1133 : move16();
326 : }
327 :
328 275634 : hPVQ->rc_cache = u_extract_l( UL_lshr( hPVQ->rc_low, 24 ) );
329 275634 : hPVQ->rc_carry = 0;
330 275634 : move16();
331 : }
332 : ELSE
333 : {
334 1143 : hPVQ->rc_carry_count = add( hPVQ->rc_carry_count, 1 ); // Q0
335 1143 : move16();
336 : }
337 276777 : hPVQ->rc_low = UL_lshl( hPVQ->rc_low, 8 );
338 276777 : move32();
339 276777 : return;
340 : }
341 18322 : static void rc_enc_shift_fx(
342 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
343 : PVQ_ENC_HANDLE hPVQ /* i/o: PVQ encoder handle */
344 : )
345 : {
346 18322 : test();
347 18322 : L_sub( 0, 0 ); /* For comparision in if */
348 18322 : IF( hPVQ->rc_low < ( 0xff000000UL ) || EQ_16( hPVQ->rc_carry, 1 ) )
349 : {
350 18251 : IF( hPVQ->rc_cache >= 0 )
351 : {
352 17851 : rc_enc_write_fx( hBstr, add( hPVQ->rc_cache, hPVQ->rc_carry ), 8 );
353 : }
354 :
355 18322 : WHILE( hPVQ->rc_carry_count > 0 )
356 : {
357 71 : rc_enc_write_fx( hBstr, s_and( add( hPVQ->rc_carry, 0xff ), 255 ), 8 );
358 71 : hPVQ->rc_carry_count = sub( hPVQ->rc_carry_count, 1 ); // Q0
359 : }
360 :
361 18251 : hPVQ->rc_cache = u_extract_l( UL_lshr( hPVQ->rc_low, 24 ) );
362 18251 : hPVQ->rc_carry = 0;
363 18251 : move16();
364 : }
365 : ELSE
366 : {
367 71 : hPVQ->rc_carry_count = add( hPVQ->rc_carry_count, 1 ); // Q0
368 : }
369 18322 : hPVQ->rc_low = UL_lshl( hPVQ->rc_low, 8 ); // Q0
370 :
371 18322 : return;
372 : }
373 :
374 : /*-------------------------------------------------------------------*
375 : * rc_enc_bits()
376 : *
377 : *
378 : *-------------------------------------------------------------------*/
379 482389 : void rc_enc_bits_ivas_fx(
380 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
381 : PVQ_ENC_HANDLE hPVQ, /* i/o: PVQ encoder handle */
382 : UWord32 value, /* i : Value to encode Q0*/
383 : Word16 bits /* i : Number of bits used Q0*/
384 : )
385 : {
386 482389 : IF( LE_16( add( rc_get_bits2_fx( hPVQ->rc_num_bits, hPVQ->rc_range ), bits ), hPVQ->rc_tot_bits ) )
387 : {
388 482389 : hPVQ->rc_num_bits = add( hPVQ->rc_num_bits, bits ); // Q0
389 482389 : move16();
390 482389 : IF( GT_16( bits, 16 ) )
391 : {
392 42328 : push_indice( hBstr, sub( IND_RC_END, hPVQ->rc_offset ), u_extract_l( UL_lshr( value, 16 ) ), sub( bits, 16 ) );
393 42328 : hPVQ->rc_offset = add( hPVQ->rc_offset, 1 ); // Q0
394 42328 : move16();
395 42328 : push_indice( hBstr, sub( IND_RC_END, hPVQ->rc_offset ), u_extract_l( UL_and( value, 0x0000ffff ) ), 16 );
396 42328 : hPVQ->rc_offset = add( hPVQ->rc_offset, 1 ); // Q0
397 42328 : move16();
398 : }
399 : ELSE
400 : {
401 440061 : push_indice( hBstr, sub( IND_RC_END, hPVQ->rc_offset ), u_extract_l( value ), bits );
402 440061 : hPVQ->rc_offset = add( hPVQ->rc_offset, 1 ); // Q0
403 440061 : move16();
404 : }
405 : }
406 : ELSE
407 : {
408 : }
409 :
410 482389 : return;
411 : }
412 32710 : void rc_enc_bits_fx(
413 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
414 : PVQ_ENC_HANDLE hPVQ, /* i/o: PVQ encoder handle */
415 : UWord32 value, /* i : Value to encode Q0*/
416 : Word16 bits /* i : Number of bits used Q0*/
417 : )
418 : {
419 32710 : IF( LE_16( add( rc_get_bits2_fx( hPVQ->rc_num_bits, hPVQ->rc_range ), bits ), hPVQ->rc_tot_bits ) )
420 : {
421 32710 : hPVQ->rc_num_bits = add( hPVQ->rc_num_bits, bits ); // Q0
422 :
423 32710 : IF( GT_16( bits, 16 ) )
424 : {
425 4072 : push_indice( hBstr, sub( IND_RC_END, hPVQ->rc_offset ), u_extract_l( UL_lshr( value, 16 ) ), sub( bits, 16 ) );
426 4072 : hPVQ->rc_offset = add( hPVQ->rc_offset, 1 ); // Q0
427 4072 : push_indice( hBstr, sub( IND_RC_END, hPVQ->rc_offset ), u_extract_l( UL_and( value, 0x0000ffff ) ), 16 );
428 4072 : hPVQ->rc_offset = add( hPVQ->rc_offset, 1 ); // Q0
429 : }
430 : ELSE
431 : {
432 28638 : push_indice( hBstr, sub( IND_RC_END, hPVQ->rc_offset ), u_extract_l( value ), bits );
433 28638 : hPVQ->rc_offset = add( hPVQ->rc_offset, 1 ); // Q0
434 : }
435 : }
436 : ELSE
437 : {
438 : }
439 :
440 32710 : return;
441 : }
442 :
443 : /*-------------------------------------------------------------------*
444 : * rc_enc_uniform()
445 : *
446 : * Encode with uniform distribution
447 : *-------------------------------------------------------------------*/
448 276508 : void rc_enc_uniform_ivas_fx(
449 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
450 : PVQ_ENC_HANDLE hPVQ, /* i/o: PVQ encoder handle */
451 : UWord32 value, /* i : Value to encode Q0*/
452 : UWord32 tot /* i : Maximum value Q0*/
453 : )
454 : {
455 : Word16 n;
456 :
457 276508 : n = sub( 32, norm_ul( UL_subNsD( tot, 1 ) ) );
458 :
459 276508 : IF( LE_16( n, 8 ) )
460 : {
461 70650 : rc_encode_ivas_fx( hBstr, hPVQ, value, 1, tot );
462 : }
463 : ELSE
464 : {
465 205858 : n = sub( n, 8 );
466 205858 : rc_encode_ivas_fx( hBstr, hPVQ, UL_lshr( value, n ), 1, UL_addNsD( UL_lshr( tot, n ), 1 ) );
467 205858 : rc_enc_bits_ivas_fx( hBstr, hPVQ, UL_and( value, UL_subNsD( UL_lshl( 1, n ), 1 ) ), n );
468 : }
469 :
470 276508 : return;
471 : }
472 16843 : void rc_enc_uniform_fx(
473 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
474 : PVQ_ENC_HANDLE hPVQ, /* i/o: PVQ encoder handle */
475 : UWord32 value, /* i : Value to encode Q0*/
476 : UWord32 tot /* i : Maximum value Q0*/
477 : )
478 : {
479 : Word16 n;
480 :
481 16843 : n = sub( 32, norm_ul( UL_subNsD( tot, 1 ) ) ); // Q0
482 :
483 16843 : IF( LE_16( n, 8 ) )
484 : {
485 976 : rc_encode_fx( hBstr, hPVQ, value, 1, tot );
486 : }
487 : ELSE
488 : {
489 15867 : n = sub( n, 8 ); // Q0
490 15867 : rc_encode_fx( hBstr, hPVQ, UL_lshr( value, n ), 1, UL_addNsD( UL_lshr( tot, n ), 1 ) );
491 15867 : rc_enc_bits_fx( hBstr, hPVQ, UL_and( value, UL_subNsD( UL_lshl( 1, n ), 1 ) ), n );
492 : }
493 :
494 16843 : return;
495 : }
496 :
497 : /*-------------------------------------------------------------------*
498 : * rc_enc_write()
499 : *
500 : * Write a byte to bitstream
501 : *-------------------------------------------------------------------*/
502 307647 : static void rc_enc_write_ivas_fx(
503 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
504 : Word16 byte, /* i : Byte to write Q0*/
505 : Word16 bits /* i : Number of bits Q0*/
506 : )
507 : {
508 307647 : push_indice( hBstr, IND_RC_START, byte, bits );
509 :
510 307647 : return;
511 : }
512 18722 : static void rc_enc_write_fx(
513 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
514 : Word16 byte, /* i : Byte to write Q0*/
515 : Word16 bits /* i : Number of bits Q0*/
516 : )
517 : {
518 18722 : push_indice( hBstr, IND_RC_START, byte, bits );
519 :
520 18722 : return;
521 : }
|