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