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 : #include <assert.h>
34 : #include "ivas_cnst.h"
35 : #include "ivas_stat_dec.h"
36 : #include "cnst.h"
37 : #include "rom_com.h"
38 : #include <stdint.h>
39 : #include "options.h"
40 : #include "prot_fx.h"
41 : #include "wmc_auto.h"
42 : #include "ivas_prot_fx.h"
43 :
44 :
45 : /*
46 : * Up to 30 bits are read internally from bit_buffer as look-ahead more than the
47 : * declared amount of max_available_bits, the total maximum bits available.
48 : * Therefore, the caller must ensure that bit_buffer has an allocated size of at
49 : * least max_available_bits + 30 entries, and also that the extra 30 padding
50 : * entries contain only binary (0 or 1) values, e.g., by setting them to zero.
51 : * The parameter max_available_bits does not represent the total number of bits
52 : * that will be consumed by the range coder, but the usually larger total number
53 : * of bits that have been read from the bitstream and have meaningful values.
54 : */
55 :
56 : /*-------------------------------------------------------------------*
57 : * Local function prototypes
58 : *-------------------------------------------------------------------*/
59 : static Word16 rc_uni_dec_read_fx( RangeUniDecState *rc_st_dec );
60 :
61 : static void rc_uni_dec_update_fx( RangeUniDecState *rc_st_dec, const UWord16 cum_freq, const UWord16 sym_freq );
62 :
63 :
64 : /*-------------------------------------------------------------------*
65 : * rc_uni_dec_init()
66 : *
67 : * Initialize the range decoder
68 : *-------------------------------------------------------------------*/
69 807320 : void rc_uni_dec_init_fx(
70 : RangeUniDecState *rc_st_dec, /* i/o: RC state handle */
71 : UWord16 *bit_buffer, /* i : Bit buffer Q0*/
72 : const Word16 max_available_bits /* i : Total maximum bits available Q0*/
73 : )
74 : {
75 : Word16 i;
76 :
77 807320 : rc_st_dec->rc_low = 0;
78 807320 : rc_st_dec->rc_range = 0xFFFFFFFF;
79 :
80 807320 : rc_st_dec->bit_buffer = bit_buffer; /*Q0*/
81 807320 : rc_st_dec->bit_count = 0;
82 : /* the renormalization procedure reads at most 32 - 2 extra bits than available */
83 807320 : rc_st_dec->max_allowable_bit_count = add( max_available_bits, 30 ); /*Q0*/
84 :
85 807320 : rc_st_dec->bit_error_detected = 0;
86 :
87 4036600 : FOR( i = 0; i < 4; i++ )
88 : {
89 : // Not adding basops becuase the resultant value greater than Word32
90 3229280 : rc_st_dec->rc_low = ( rc_st_dec->rc_low << 8 ) + rc_uni_dec_read_fx( rc_st_dec ); /*Q0*/
91 : }
92 :
93 807320 : return;
94 : }
95 :
96 : /*-------------------------------------------------------------------*
97 : * rc_uni_dec_read_symbol_fastS()
98 : *
99 : * Read an alphabet symbol when total frequency is a power of 2
100 : *-------------------------------------------------------------------*/
101 :
102 : /*! r: Read symbol */
103 197855937 : UWord16 rc_uni_dec_read_symbol_fastS_fx(
104 : RangeUniDecState *rc_st_dec, /* i/o: Decoder State */
105 : const UWord16 cum_freq_table[], /* i : Cumulative frequency up to symbol Q0*/
106 : const UWord16 sym_freq_table[], /* i : Symbol frequency Q0*/
107 : const UWord16 alphabet_size, /* i : Number of symbols in the alphabet Q0*/
108 : const UWord16 tot_shift /* i : Total frequency as a power of 2 Q0*/
109 : )
110 : {
111 : UWord16 sym_begin;
112 : UWord16 sym_end;
113 : UWord16 sym_middle;
114 : UWord32 low; /* local copy (2 to 1 + ceil(log2(alphabet_size)) uses) */
115 : UWord32 range; /* local copy (3 to 2 + ceil(log2(alphabet_size)) uses) */
116 : UWord16 ceil_log2_alphabet_size; /* ceil(log2(alphabet_size)) */
117 : UWord16 step;
118 : UWord32 reversed_low;
119 :
120 197855937 : low = rc_st_dec->rc_low; /*Q0*/
121 197855937 : move32();
122 197855937 : range = rc_st_dec->rc_range; /*Q0*/
123 197855937 : move32();
124 197855937 : range = UL_lshr( range, tot_shift ); /*range >>= tot_shift*/
125 :
126 : /* the cumulative value is val = low / range */
127 : /* the condition val >= level is equivalent with low >= range * level */
128 :
129 : /* in case of bitstream errors it is possible that val >= (1 << tot_shift) */
130 : /*basop comment : unsigned integers are getting compared hence basops are not used*/
131 197855937 : IF( GE_64( low, UL_lshl( range, tot_shift ) ) )
132 : {
133 0 : rc_st_dec->bit_error_detected = 1;
134 0 : move16();
135 :
136 : /* for valid bitstreams, always low < range, therefore setting low = range */
137 : /* will always reach the bitstream error condition branch on the next call */
138 0 : rc_st_dec->rc_range = 0xFFFFFFFF;
139 0 : move32();
140 0 : rc_st_dec->rc_low = rc_st_dec->rc_range; /*Q0*/
141 0 : move32();
142 :
143 : /* the current value and all the following values are very likely incorrect */
144 0 : return 0; /* return the minimum valid value for the output */
145 : }
146 :
147 : /* do a binary search to find the symbol read */
148 197855937 : sym_begin = 0;
149 197855937 : move16();
150 197855937 : sym_end = alphabet_size; /* the possible values are {sym_begin, .., sym_end - 1} Q0*/
151 197855937 : move16();
152 :
153 : /* ceil(log2(x)) = 1 + floor(log2(x - 1)), for any x >= 2 */
154 : /* floor(log2(y)) = 31 - norm_ul_float(y) = 30 - norm_l(y), for any 1 <= y <= 2 ^ 31 - 1 */
155 197855937 : ceil_log2_alphabet_size = sub( 31, norm_l( UL_subNsD( alphabet_size, 1 ) ) );
156 : /* completely equivalent with 32 - norm_ul_float(alphabet_size - 1), but norm_l is faster */
157 197855937 : reversed_low = UL_subNsD( UL_lshl( range, tot_shift ), low ); /*( range << tot_shift ) - low*/
158 :
159 : /* at most ceil_log2_alphabet_size steps are needed so that sym_end - sym_begin == 1 */
160 1186235384 : FOR( step = 0; step < ceil_log2_alphabet_size; step++ )
161 : {
162 988379447 : sym_middle = shr( add( sym_begin, sym_end ), 1 ); /*( sym_begin + sym_end ) >> 1 Q0*/
163 : /* completely equvalent with low >= range * ((1 << tot_shift) - table[sym_middle]) */
164 988379447 : IF( range * L_sub( L_shl( 1, tot_shift ), cum_freq_table[sym_middle] ) >= reversed_low )
165 : {
166 462384708 : sym_begin = sym_middle; /*Q0*/
167 462384708 : move16();
168 : }
169 : ELSE
170 : {
171 525994739 : sym_end = sym_middle; /*Q0*/
172 525994739 : move16();
173 : }
174 : }
175 :
176 : /* sym_begin contains the symbol read */
177 :
178 : /* low was not modified */
179 197855937 : rc_st_dec->rc_range = range; /*Q0*/
180 197855937 : move32();
181 :
182 197855937 : rc_uni_dec_update_fx( rc_st_dec, cum_freq_table[sym_begin], sym_freq_table[sym_begin] );
183 :
184 197855937 : return sym_begin;
185 : }
186 :
187 :
188 : /*-------------------------------------------------------------------*
189 : * rc_uni_dec_update()
190 : *
191 : * Update the state for the symbol found after using get_cumulative
192 : *-------------------------------------------------------------------*/
193 :
194 197855937 : static void rc_uni_dec_update_fx(
195 : RangeUniDecState *rc_st_dec, /* i/o: RC State handle */
196 : const UWord16 cum_freq, /* i : Cumulative frequency Q0*/
197 : const UWord16 sym_freq /* i : Symbol frequency Q0*/
198 : )
199 : {
200 197855937 : rc_st_dec->rc_low = UL_subNsD( rc_st_dec->rc_low, UL_Mpy_32_32( cum_freq, rc_st_dec->rc_range ) ); /*Q0*/
201 197855937 : rc_st_dec->rc_range = UL_Mpy_32_32( rc_st_dec->rc_range, sym_freq ); /*Q0*/
202 197855937 : move32();
203 197855937 : move32();
204 :
205 : /* rc_range was shifted right by up to 16, so at most two renormalizations are needed */
206 197855937 : IF( LT_64( rc_st_dec->rc_range, 0x01000000 ) )
207 : {
208 59566083 : rc_st_dec->rc_low = UL_addNsD( UL_lshl( rc_st_dec->rc_low, 8 ), (UWord32) rc_uni_dec_read_fx( rc_st_dec ) ); /*Q0*/
209 59566083 : rc_st_dec->rc_range = UL_lshl( rc_st_dec->rc_range, 8 ); /*Q0*/
210 59566083 : move32();
211 59566083 : move32();
212 59566083 : IF( LT_64( rc_st_dec->rc_range, 0x01000000 ) )
213 : {
214 67229 : rc_st_dec->rc_low = UL_addNsD( UL_lshl( rc_st_dec->rc_low, 8 ), (UWord32) rc_uni_dec_read_fx( rc_st_dec ) ); /*Q0*/
215 67229 : rc_st_dec->rc_range = UL_lshl( rc_st_dec->rc_range, 8 ); /*Q0*/
216 67229 : move32();
217 67229 : move32();
218 : }
219 : }
220 :
221 197855937 : return;
222 : }
223 : /*-------------------------------------------------------------------*
224 : * rc_uni_dec_read_bit()
225 : *
226 : * Read one bit with uniform probability
227 : *-------------------------------------------------------------------*/
228 :
229 : /*! r: Read bit */
230 446524 : UWord16 rc_uni_dec_read_bit(
231 : RangeUniDecState *rc_st_dec /* i/o: RC State handle */
232 : )
233 : {
234 : UWord32 val;
235 : UWord32 low; /* local copy (1 to 5 uses) */
236 : UWord32 range; /* local copy (3 to 6 uses) */
237 :
238 446524 : low = rc_st_dec->rc_low; /*Q0*/
239 446524 : move32();
240 446524 : range = rc_st_dec->rc_range; /*Q0*/
241 446524 : move32();
242 :
243 446524 : range = UL_lshr( range, 1 ); /*range >>= 1*/
244 :
245 : /* in case of bitstream errors it is possible that rc_low >= (rc_range << 1) */
246 446524 : val = 0;
247 446524 : move32();
248 : /*basop comment : comparision of unsigned integers hence basops are not used*/
249 446524 : IF( GE_64( low, range ) )
250 : {
251 222385 : val = UL_addNsD( val, 1 ); /*Q0*/
252 222385 : low = UL_subNsD( low, range ); /*Q0*/
253 :
254 : /* rc_range was already subtracted once from rc_low */
255 222385 : IF( GE_64( low, range ) )
256 : {
257 0 : rc_st_dec->bit_error_detected = 1;
258 0 : move16();
259 :
260 : /* for valid bitstreams, always low < range, therefore setting low = range */
261 : /* will always reach the bitstream error condition branch on the next call */
262 0 : rc_st_dec->rc_range = 0xFFFFFFFF;
263 0 : move32();
264 0 : rc_st_dec->rc_low = rc_st_dec->rc_range; /*Q0*/
265 0 : move32();
266 :
267 : /* the current value and all the following values are very likely incorrect */
268 0 : return 0; /* return the minimum valid value for the output */
269 : }
270 : }
271 :
272 : /* rc_range was shifted right by 1, so at most one renormalization is needed */
273 : /*basop comment : comparision of unsigned integers hence basops are not used*/
274 446524 : IF( LT_64( range, 0x01000000 ) )
275 : {
276 54618 : low = UL_addNsD( UL_lshl( low, 8 ), (UWord32) rc_uni_dec_read_fx( rc_st_dec ) ); /*Q0*/
277 54618 : range = UL_lshl( range, 8 );
278 : }
279 :
280 446524 : rc_st_dec->rc_low = low; /*Q0*/
281 446524 : move32();
282 446524 : rc_st_dec->rc_range = range; /*Q0*/
283 446524 : move32();
284 :
285 446524 : return (UWord16) val;
286 : }
287 :
288 :
289 : /*-------------------------------------------------------------------*
290 : * rc_uni_dec_read_bit_prob_fast()
291 : *
292 : * Read one bit with probability freq0 / 2 ^ tot_shift for symbol 0
293 : *-------------------------------------------------------------------*/
294 :
295 : /*! r: Read bit */
296 53509 : UWord16 rc_uni_dec_read_bit_prob_fast(
297 : RangeUniDecState *rc_st_dec, /* i/o: RC State handle */
298 : const Word16 freq0, /* i : Frequency for symbol 0 Q0*/
299 : const UWord16 tot_shift /* i : Total frequency as a power of 2 Q0*/
300 : )
301 : {
302 : UWord32 val;
303 : UWord32 low; /* local copy (2 to 7 uses) */
304 : UWord32 range; /* local copy (5 to 9 uses) */
305 :
306 53509 : low = rc_st_dec->rc_low; /*Q0*/
307 53509 : move32();
308 53509 : range = rc_st_dec->rc_range; /*Q0*/
309 53509 : move32();
310 :
311 53509 : range = UL_lshr( range, tot_shift ); /*range >>= tot_shift*/
312 :
313 : /* in case of bitstream errors it is possible that low >= (1 << tot_shift) * range */
314 : /*basop comment : unsigned integers are getting compared hence basops are not used*/
315 53509 : IF( GE_64( low, UL_lshl( range, tot_shift ) ) ) /* equivalent condition */
316 : {
317 0 : rc_st_dec->bit_error_detected = 1;
318 0 : move16();
319 :
320 : /* for valid bitstreams, always low < range, therefore setting low = range */
321 : /* will always reach the bitstream error condition branch on the next call */
322 0 : rc_st_dec->rc_range = 0xFFFFFFFF;
323 0 : move32();
324 0 : rc_st_dec->rc_low = rc_st_dec->rc_range; /*Q0*/
325 0 : move32();
326 :
327 : /* the current value and all the following values are very likely incorrect */
328 0 : return 0; /* return the minimum valid value for the output */
329 : }
330 :
331 53509 : val = 0;
332 53509 : move32();
333 : /*basop comment : unsigned comparision and multiplication of unsigned and signed hence basops are not used*/
334 53509 : IF( low >= range * freq0 )
335 : {
336 17606 : val = UL_addNsD( val, 1 ); /*Q0*/
337 :
338 17606 : low = UL_subNsD( low, UL_Mpy_32_32( range, freq0 ) ); /*Q0*/
339 : /*basop comment : multiplication of two unsigned integers and result is stored in unsigned hence basops are not used*/
340 17606 : range = UL_Mpy_32_32( range, UL_subNsD( UL_lshl( 1, tot_shift ), freq0 ) ); /* freq1 = (1 << tot_shift) - freq0 Q0*/
341 : }
342 : ELSE
343 : {
344 : /* basop comment : multiplication of two unsigned integers and result is stored in unsigned integer hence basops are not used*/
345 35903 : range = UL_Mpy_32_32( range, freq0 ); /*Q0*/
346 : }
347 :
348 : /* rc_range was shifted right by up to 16, so at most two renormalizations are needed */
349 : /*basop comment : comparision of unsigned integers hence basops are not used*/
350 53509 : IF( LT_64( range, 0x01000000 ) )
351 : {
352 5635 : low = UL_addNsD( UL_lshl( low, 8 ), (UWord32) rc_uni_dec_read_fx( rc_st_dec ) ); /*Q0*/
353 5635 : range = UL_lshl( range, 8 ); /*range <<= 8*/
354 5635 : IF( LT_64( range, 0x01000000 ) )
355 : {
356 0 : low = UL_addNsD( UL_lshl( low, 8 ), (UWord32) rc_uni_dec_read_fx( rc_st_dec ) ); /*Q0*/
357 0 : range = UL_lshl( range, 8 ); /*range <<= 8*/
358 : }
359 : }
360 :
361 53509 : rc_st_dec->rc_low = low; /*Q0*/
362 53509 : move32();
363 53509 : rc_st_dec->rc_range = range; /*Q0*/
364 53509 : move32();
365 :
366 53509 : return (UWord16) val;
367 : }
368 :
369 :
370 : /*-------------------------------------------------------------------*
371 : * rc_uni_dec_read_bits()
372 : *
373 : * Read up to 16 bits with uniform probability
374 : *-------------------------------------------------------------------*/
375 :
376 : /*! r: Read bits */
377 65990 : UWord16 rc_uni_dec_read_bits(
378 : RangeUniDecState *rc_st_dec, /* i/o: RC State handle */
379 : const Word16 bits /* i : Number of bits Q0*/
380 : )
381 : {
382 : UWord32 val;
383 : UWord32 low; /* local copy (2 to 6 uses) */
384 : UWord32 range; /* local copy (4 to 7 uses) */
385 : UWord32 tmp;
386 65990 : low = rc_st_dec->rc_low; /*Q0*/
387 65990 : range = rc_st_dec->rc_range; /*Q0*/
388 65990 : move32();
389 65990 : move32();
390 :
391 65990 : range = (UWord32) W_shr( range, bits ); /*range >>= bits*/
392 :
393 65990 : val = 0;
394 65990 : move32();
395 :
396 123714 : FOR( tmp = range; tmp <= low; tmp += range )
397 : {
398 57724 : val = UL_addNsD( val, 1 );
399 : }
400 :
401 : /* in case of bitstream errors it is possible that val >= (1 << bits) */
402 65990 : IF( W_shr( val, bits ) != 0 ) /* equivalent condition */
403 : {
404 0 : rc_st_dec->bit_error_detected = 1;
405 0 : move16();
406 :
407 : /* for valid bitstreams, always low < range, therefore setting low = range */
408 : /* will always reach the bitstream error condition branch on the next call */
409 0 : rc_st_dec->rc_range = 0xFFFFFFFF;
410 0 : rc_st_dec->rc_low = rc_st_dec->rc_range; /*Q0*/
411 0 : move32();
412 0 : move32();
413 :
414 : /* the current value and all the following values are very likely incorrect */
415 0 : return 0; /* return the minimum valid value for the output */
416 : }
417 :
418 65990 : low = (UWord32) W_sub( low, W_mult0_32_32( val, range ) ); /*Q0*/
419 :
420 : /* rc_range was shifted right by up to 16, so at most two renormalizations are needed */
421 65990 : IF( LT_64( range, 0x01000000 ) )
422 : {
423 10910 : low = (UWord32) W_add( W_shl( low, 8 ), rc_uni_dec_read_fx( rc_st_dec ) ); /*Q0*/
424 10910 : range = (UWord32) W_shl( range, 8 ); /*range <<= 8*/
425 10910 : if ( LT_64( range, 0x01000000 ) )
426 : {
427 0 : low = (UWord32) W_add( W_shl( low, 8 ), rc_uni_dec_read_fx( rc_st_dec ) ); /*Q0*/
428 0 : range = (UWord32) W_shl( range, 8 ); /*range <<= 8*/
429 : }
430 : }
431 :
432 65990 : rc_st_dec->rc_low = low; /*Q0*/
433 65990 : rc_st_dec->rc_range = range; /*Q0*/
434 65990 : move32();
435 65990 : move32();
436 :
437 65990 : return (UWord16) val; /*Q0*/
438 : }
439 166803038 : Word16 rc_uni_dec_virtual_finish_fx(
440 : RangeUniDecState *rc_st_dec /* i : RC state handle */
441 : )
442 : {
443 : /* the function is completely equivalent with rc_uni_dec_finish */
444 : // Not adding basop because value of rc_st_dec->rc_range exceeds Word32
445 166803038 : return add( rc_st_dec->bit_count, sub( norm_l( rc_st_dec->rc_range >> 24 ), 53 ) ); /*Q0*/
446 : }
447 :
448 62933755 : static Word16 rc_uni_dec_read_fx(
449 : RangeUniDecState *rc_st_dec /* i/o: RC State handle */
450 : )
451 : {
452 : Word16 byte_read;
453 : UWord16 *shifted_bit_buffer;
454 :
455 62933755 : shifted_bit_buffer = rc_st_dec->bit_buffer + rc_st_dec->bit_count; /*Q0*/
456 62933755 : rc_st_dec->bit_count = add( rc_st_dec->bit_count, 8 ); /*Q0*/
457 :
458 : /*
459 : * In case of bitstream errors the number the bits read may be larger than the total
460 : * number of bits that have been read from the bitstream and have meaningful values
461 : * plus the extra 30 padding bits that must be appended to the buffer by the caller.
462 : */
463 62933755 : IF( GT_16( rc_st_dec->bit_count, rc_st_dec->max_allowable_bit_count ) )
464 : {
465 0 : rc_st_dec->bit_error_detected = 1;
466 :
467 : /* for valid bitstreams, always low < range, therefore setting low = range */
468 : /* will always reach the bitstream error condition branch on the next call */
469 0 : rc_st_dec->rc_range = 0xFFFFFFFF;
470 0 : rc_st_dec->rc_low = rc_st_dec->rc_range; /*Q0*/
471 :
472 0 : return 0; /* reading the 8 bits would trigger an out-of-bounds array access */
473 : }
474 :
475 :
476 : /* pack the first 8 bits from shifted_bit_buffer, first bit is most significant */
477 62933755 : byte_read = s_or( shl( shifted_bit_buffer[0], 7 ), s_or( shl( shifted_bit_buffer[1], 6 ),
478 62933755 : s_or( shl( shifted_bit_buffer[2], 5 ), s_or( shl( shifted_bit_buffer[3], 4 ),
479 62933755 : s_or( shl( shifted_bit_buffer[4], 3 ), s_or( shl( shifted_bit_buffer[5], 2 ),
480 62933755 : s_or( shl( shifted_bit_buffer[6], 1 ), shifted_bit_buffer[7] ) ) ) ) ) ) );
481 :
482 62933755 : return byte_read; /*Q0*/
483 : }
|