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_enc.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 : * After finalizing the range encoder, the produced bits are available in the
50 : * rc_st_enc->byte_buffer member array. The bits are packed into bytes, MSB first, as
51 : * byte_buffer[0].bit[7], ..., byte_buffer[0].bit[0], byte_buffer[1].bit[7], ...
52 : * The last byte may contain less than 8 bits. The total bit count is returned
53 : * by the rc_uni_enc_finish function but can also be computed afterwards as
54 : * total_bit_count = (rc_st_enc->byte_count - 1) * 8 + rc_st_enc->last_byte_bit_count
55 : */
56 :
57 : /*-------------------------------------------------------------------*
58 : * Local function prototypes
59 : *-------------------------------------------------------------------*/
60 :
61 : static void rc_uni_enc_shift( RangeUniEncState *rc_st_enc );
62 :
63 :
64 : /*-------------------------------------------------------------------*
65 : * rc_uni_enc_init()
66 : *
67 : * Initalize the range encoder
68 : *-------------------------------------------------------------------*/
69 :
70 931690 : void rc_uni_enc_init(
71 : RangeUniEncState *rc_st_enc /* i/o: RC state handle */
72 : )
73 : {
74 931690 : rc_st_enc->rc_low = 0;
75 931690 : rc_st_enc->rc_range = 0xFFFFFFFF;
76 931690 : rc_st_enc->rc_cache = -1;
77 931690 : rc_st_enc->rc_carry = 0;
78 931690 : rc_st_enc->rc_carry_count = 0;
79 :
80 931690 : rc_st_enc->byte_count = 0;
81 931690 : rc_st_enc->last_byte_bit_count = -1;
82 :
83 931690 : return;
84 : }
85 :
86 : /*-------------------------------------------------------------------*
87 : * rc_uni_enc_encode_fast()
88 : *
89 : * Encode given cumulative frequency and frequency when total frequency is a power of 2
90 : *-------------------------------------------------------------------*/
91 :
92 218785501 : void rc_uni_enc_encode_fast(
93 : RangeUniEncState *rc_st_enc, /* i/o: RC state handle */
94 : const uint16_t cum_freq, /* i : Cumulative frequency up to symbol */
95 : const uint16_t sym_freq, /* i : Symbol frequency */
96 : const uint16_t tot_shift /* i : Total frequency as a power of 2 */
97 : )
98 : {
99 : uint32_t r, tmp;
100 : #ifdef DEBUGGING
101 : assert( tot_shift <= 16 );
102 : #endif
103 :
104 218785501 : r = rc_st_enc->rc_range >> tot_shift;
105 218785501 : tmp = r * cum_freq;
106 :
107 218785501 : rc_st_enc->rc_low += tmp;
108 218785501 : if ( rc_st_enc->rc_low < tmp )
109 : {
110 11457839 : rc_st_enc->rc_carry = 1;
111 : }
112 :
113 218785501 : rc_st_enc->rc_range = r * sym_freq;
114 :
115 : /* rc_range was shifted right by up to 16, so at most two renormalizations are needed */
116 218785501 : if ( rc_st_enc->rc_range < 0x01000000 )
117 : {
118 65609962 : rc_st_enc->rc_range <<= 8;
119 65609962 : rc_uni_enc_shift( rc_st_enc );
120 65609962 : if ( rc_st_enc->rc_range < 0x01000000 )
121 : {
122 75670 : rc_st_enc->rc_range <<= 8;
123 75670 : rc_uni_enc_shift( rc_st_enc );
124 : }
125 : }
126 :
127 218785501 : return;
128 : }
129 :
130 :
131 : /*-------------------------------------------------------------------*
132 : * rc_uni_enc_encode_symbol_fastS()
133 : *
134 : * Encode an alphabet symbol when total frequency is a power of 2
135 : *-------------------------------------------------------------------*/
136 :
137 218072923 : void rc_uni_enc_encode_symbol_fastS(
138 : RangeUniEncState *rc_st_enc, /* i/o: Encoder state */
139 : const uint16_t symbol, /* i : Symbol to encode */
140 : const uint16_t cum_freq[], /* i : Cumulative frequency up to symbol */
141 : const uint16_t sym_freq[], /* i : Symbol frequency */
142 : const uint16_t tot_shift /* i : Total frequency as a power of 2 */
143 : )
144 : {
145 218072923 : rc_uni_enc_encode_fast( rc_st_enc, cum_freq[symbol], sym_freq[symbol], tot_shift );
146 :
147 218072923 : return;
148 : }
149 :
150 :
151 : /*-------------------------------------------------------------------*
152 : * rc_uni_enc_finish()
153 : *
154 : * Finalize the range encoder
155 : *-------------------------------------------------------------------*/
156 :
157 : /*! r: Total number of bits produced */
158 921110 : int16_t rc_uni_enc_finish(
159 : RangeUniEncState *rc_st_enc /* i/o: RC state handle */
160 : )
161 : {
162 : int16_t total_bit_count;
163 : uint32_t val, mask;
164 : int16_t bits;
165 : #ifdef DEBUGGING
166 : assert( rc_st_enc->rc_range >= 0x01000000 ); /* rc_range is normalized */
167 : assert( rc_st_enc->last_byte_bit_count == -1 ); /* make sure finalization was not done already */
168 : #endif
169 :
170 : /* floor(log2(x)) = floor(log2(x >> 24)) + 24, for any x >= 2 ^ 24 */
171 : /* 32 - floor(log2(y)) = norm_ul(y) + 1 = norm_l(y >> 24) - 22 */
172 921110 : bits = norm_l( rc_st_enc->rc_range >> 24 ) - 22; /* bits = 32 - floor(log2(rc_range)) */
173 : /* completely equivalent with norm_ul(rc_st_enc->rc_range) + 1, but norm_l is faster */
174 :
175 921110 : bits++; /* conservative number of bits, because the decoder only has rc_range available */
176 :
177 : #ifdef DEBUGGING
178 : assert( ( bits >= 2 ) && ( bits <= 9 ) ); /* depends on rc_range, which is normalized */
179 : #endif
180 :
181 921110 : mask = 0xFFFFFFFFu >> bits;
182 921110 : val = ( rc_st_enc->rc_low + mask ) & ~mask;
183 :
184 921110 : if ( val < rc_st_enc->rc_low )
185 : {
186 56237 : rc_st_enc->rc_carry = 1;
187 : }
188 :
189 921110 : rc_st_enc->rc_low = val;
190 :
191 1957153 : while ( bits > 0 )
192 : {
193 1036043 : rc_uni_enc_shift( rc_st_enc );
194 1036043 : bits -= 8;
195 : }
196 :
197 921110 : bits += 8;
198 :
199 921110 : if ( rc_st_enc->rc_carry_count > 0 )
200 : {
201 : /* rc_carry_count > 0, therefore the last call to rc_uni_enc_shift incremented rc_carry_count */
202 442 : if ( rc_st_enc->rc_cache >= 0 ) /* may actually be always true, but it is difficult to prove formally */
203 : {
204 : #ifdef DEBUGGING
205 : assert( rc_st_enc->byte_count < RANGE_UNI_BUFFER_BYTES_MAX );
206 : #endif
207 442 : rc_st_enc->byte_buffer[rc_st_enc->byte_count++] = (uint8_t) ( rc_st_enc->rc_cache + rc_st_enc->rc_carry );
208 : }
209 :
210 448 : while ( rc_st_enc->rc_carry_count > 1 )
211 : {
212 : #ifdef DEBUGGING
213 : assert( rc_st_enc->byte_count < RANGE_UNI_BUFFER_BYTES_MAX );
214 : #endif
215 6 : rc_st_enc->byte_buffer[rc_st_enc->byte_count++] = (uint8_t) ( rc_st_enc->rc_carry + 0xFF );
216 6 : rc_st_enc->rc_carry_count--;
217 : }
218 : /* pack the last 1 to 8 bits into the MSB of the last byte, with zero padding into the LSB */
219 : #ifdef DEBUGGING
220 : assert( rc_st_enc->byte_count < RANGE_UNI_BUFFER_BYTES_MAX );
221 : #endif
222 442 : rc_st_enc->byte_buffer[rc_st_enc->byte_count++] = (uint8_t) ( ( rc_st_enc->rc_carry + 0xFF ) & ( 0xFFu << ( 8 - bits ) ) );
223 442 : rc_st_enc->last_byte_bit_count = bits;
224 : }
225 : else
226 : {
227 : /* rc_carry_count == 0, therefore the last call to rc_uni_enc_shift wrote into rc_cache */
228 : #ifdef DEBUGGING
229 : assert( rc_st_enc->rc_cache >= 0 );
230 : #endif
231 : /* pack the last 1 to 8 bits into the MSB of the last byte, with zero padding into the LSB */
232 : #ifdef DEBUGGING
233 : assert( rc_st_enc->byte_count < RANGE_UNI_BUFFER_BYTES_MAX );
234 : #endif
235 920668 : rc_st_enc->byte_buffer[rc_st_enc->byte_count++] = (uint8_t) ( ( rc_st_enc->rc_cache + rc_st_enc->rc_carry ) & ( 0xFFu << ( 8 - bits ) ) );
236 920668 : rc_st_enc->last_byte_bit_count = bits;
237 : }
238 :
239 : #ifdef DEBUGGING
240 : assert( ( rc_st_enc->last_byte_bit_count >= 1 ) && ( rc_st_enc->last_byte_bit_count <= 8 ) );
241 : assert( rc_st_enc->byte_count >= 1 );
242 : #endif
243 :
244 921110 : total_bit_count = ( ( rc_st_enc->byte_count - 1 ) << 3 ) + rc_st_enc->last_byte_bit_count;
245 :
246 : #ifdef DEBUGGING
247 : assert( total_bit_count >= 2 );
248 : #endif
249 921110 : return total_bit_count;
250 : }
251 :
252 :
253 : /*-------------------------------------------------------------------*
254 : * rc_uni_enc_virtual_finish()
255 : *
256 : * Get the total number of bits that would be produced by finalization
257 : *-------------------------------------------------------------------*/
258 :
259 : /*! r: Total number of bits produced */
260 1211350 : int16_t rc_uni_enc_virtual_finish(
261 : RangeUniEncState *rc_st_enc /* i : RC state handle */
262 : )
263 : {
264 : #ifdef DEBUGGING
265 : assert( rc_st_enc->rc_range >= 0x01000000 ); /* rc_range is normalized */
266 : assert( rc_st_enc->last_byte_bit_count == -1 ); /* make sure finalization was not done already */
267 : assert( sizeof( rc_st_enc->rc_cache ) == 2 ); /* ensure the sign bit computation is correct */
268 : #endif
269 :
270 : /*
271 : byte_count bytes have already been written to the byte_buffer array
272 : 1 byte is pending if rc_cache >= 0, consisting of rc_cache or rc_cache + 1
273 : the pending byte bits are computed as 8 - 8 * ((uint16_t) rc_st_enc->rc_cache >> 15)
274 : rc_carry_count bytes are pending, consisting of 0x00 or 0xFF
275 : bits bits will be additionally written during the finalization procedure
276 : bits is computed as norm_l(rc_st_enc->rc_range >> 24) - 21, as in rc_uni_enc_finish
277 : */
278 2422700 : return ( ( rc_st_enc->byte_count + rc_st_enc->rc_carry_count ) << 3 ) +
279 1211350 : norm_l( rc_st_enc->rc_range >> 24 ) - 13 - 8 * ( (uint16_t) rc_st_enc->rc_cache >> 15 );
280 : }
281 :
282 :
283 : /*-------------------------------------------------------------------*
284 : * rc_uni_enc_shift()
285 : *
286 : * Shift a byte out to bitstream (internal function)
287 : *-------------------------------------------------------------------*/
288 :
289 66789431 : static void rc_uni_enc_shift(
290 : RangeUniEncState *rc_st_enc /* i/o: RC state handle */
291 : )
292 : {
293 66789431 : if ( ( rc_st_enc->rc_low < 0xFF000000u ) || rc_st_enc->rc_carry )
294 : {
295 66527513 : if ( rc_st_enc->rc_cache >= 0 )
296 : {
297 : #ifdef DEBUGGING
298 : assert( rc_st_enc->byte_count < RANGE_UNI_BUFFER_BYTES_MAX );
299 : #endif
300 65596298 : rc_st_enc->byte_buffer[rc_st_enc->byte_count++] = (uint8_t) ( rc_st_enc->rc_cache + rc_st_enc->rc_carry );
301 : }
302 :
303 66788944 : while ( rc_st_enc->rc_carry_count > 0 )
304 : {
305 : #ifdef DEBUGGING
306 : assert( rc_st_enc->byte_count < RANGE_UNI_BUFFER_BYTES_MAX );
307 : #endif
308 261431 : rc_st_enc->byte_buffer[rc_st_enc->byte_count++] = (uint8_t) ( rc_st_enc->rc_carry + 0xFF );
309 261431 : rc_st_enc->rc_carry_count--;
310 : }
311 :
312 66527513 : rc_st_enc->rc_cache = (int16_t) ( rc_st_enc->rc_low >> 24 );
313 66527513 : rc_st_enc->rc_carry = 0;
314 : }
315 : else
316 : {
317 261918 : rc_st_enc->rc_carry_count++;
318 : }
319 :
320 66789431 : rc_st_enc->rc_low <<= 8;
321 :
322 66789431 : return;
323 : }
324 :
325 :
326 : /*-------------------------------------------------------------------*
327 : * rc_uni_enc_encode_bits()
328 : *
329 : * Encode up to 16 bits with uniform probability
330 : *-------------------------------------------------------------------*/
331 :
332 529445 : void rc_uni_enc_encode_bits(
333 : RangeUniEncState *rc_st_enc, /* i/o: RC state handle */
334 : const uint16_t value, /* i : Value to encode */
335 : const int16_t bits /* i : Number of bits */
336 : )
337 : {
338 : uint32_t tmp;
339 : #ifdef DEBUGGING
340 : assert( bits <= 16 );
341 : #endif
342 :
343 529445 : rc_st_enc->rc_range >>= bits;
344 529445 : tmp = rc_st_enc->rc_range * value;
345 :
346 529445 : rc_st_enc->rc_low += tmp;
347 529445 : if ( rc_st_enc->rc_low < tmp )
348 : {
349 23664 : rc_st_enc->rc_carry = 1;
350 : }
351 :
352 : /* rc_range was shifted right by up to 16, so at most two renormalizations are needed */
353 529445 : if ( rc_st_enc->rc_range < 0x01000000 )
354 : {
355 67756 : rc_st_enc->rc_range <<= 8;
356 67756 : rc_uni_enc_shift( rc_st_enc );
357 67756 : if ( rc_st_enc->rc_range < 0x01000000 )
358 : {
359 0 : rc_st_enc->rc_range <<= 8;
360 0 : rc_uni_enc_shift( rc_st_enc );
361 : }
362 : }
363 :
364 529445 : return;
365 : }
|