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 <stdint.h>
34 : #include "options.h"
35 : #ifdef DEBUGGING
36 : #include "debug.h"
37 : #endif
38 : #include "ivas_prot.h"
39 : #include "ivas_rom_com.h"
40 : #include "math.h"
41 : #include "prot.h"
42 : #include "wmc_auto.h"
43 :
44 :
45 : /*-----------------------------------------------------------------------------------------*
46 : * Function ivas_wrap_arround)
47 : *
48 : * wrap around
49 : *-----------------------------------------------------------------------------------------*/
50 :
51 18965271 : void ivas_wrap_arround(
52 : int16_t *pArr,
53 : const int16_t min_val,
54 : const int16_t max_val,
55 : const int16_t length )
56 : {
57 : int16_t i;
58 :
59 269591577 : for ( i = 0; i < length; i++ )
60 : {
61 250626306 : if ( pArr[i] < min_val )
62 : {
63 88716 : pArr[i] = max_val - min_val + pArr[i] + 1;
64 : }
65 250626306 : if ( pArr[i] > max_val )
66 : {
67 90434 : pArr[i] = min_val + pArr[i] - max_val - 1;
68 : }
69 : }
70 :
71 18965271 : return;
72 : }
73 :
74 :
75 : /*-----------------------------------------------------------------------------------------*
76 : * Function ivas_get_cum_freq_model()
77 : *
78 : * get cumulative frequency model
79 : *-----------------------------------------------------------------------------------------*/
80 :
81 9390024 : void ivas_get_cum_freq_model(
82 : const int16_t *pFreq_model,
83 : const int16_t length,
84 : int16_t *pCum_freq_model )
85 : {
86 : int16_t i;
87 :
88 9390024 : pCum_freq_model[length] = 0;
89 :
90 86103952 : for ( i = length; i > 0; i-- )
91 : {
92 76713928 : pCum_freq_model[i - 1] = pCum_freq_model[i] + pFreq_model[i];
93 : }
94 :
95 9390024 : return;
96 : }
97 :
98 :
99 : /*-----------------------------------------------------------------------------------------*
100 : * Function ivas_map_num_pred_r_to_idx()
101 : *
102 : * Map the ivas_arith_pred_r_consts and ivas_huff_pred_r_consts tables
103 : *-----------------------------------------------------------------------------------------*/
104 :
105 782502 : int16_t ivas_map_num_pred_r_to_idx(
106 : const int16_t num_quant_points_pred_r,
107 : const int16_t active_w_flag )
108 : {
109 782502 : int16_t pred_r_to_idx = -1;
110 782502 : if ( active_w_flag == 0 )
111 : {
112 653118 : switch ( num_quant_points_pred_r )
113 : {
114 115656 : case 1:
115 115656 : pred_r_to_idx = PRED_Q_1;
116 115656 : break;
117 50645 : case 7:
118 50645 : pred_r_to_idx = PRED_Q_7;
119 50645 : break;
120 191924 : case 15:
121 191924 : pred_r_to_idx = PRED_Q_15;
122 191924 : break;
123 237065 : case 21:
124 237065 : pred_r_to_idx = PRED_Q_21;
125 237065 : break;
126 57828 : case 31:
127 57828 : pred_r_to_idx = PRED_Q_31;
128 57828 : break;
129 0 : default:
130 0 : assert( !"Forbidden value for prediction quantization strategy index" );
131 : break;
132 : }
133 : }
134 : else
135 : {
136 129384 : switch ( num_quant_points_pred_r )
137 : {
138 33751 : case 7:
139 33751 : pred_r_to_idx = PRED_Q_7_ACTIVE_W;
140 33751 : break;
141 86256 : case 15:
142 86256 : pred_r_to_idx = PRED_Q_15_ACTIVE_W;
143 86256 : break;
144 9377 : case 21:
145 9377 : pred_r_to_idx = PRED_Q_21_ACTIVE_W;
146 9377 : break;
147 0 : default:
148 0 : assert( !"Forbidden value for prediction quantization strategy index" );
149 : break;
150 : }
151 : }
152 :
153 782502 : return pred_r_to_idx;
154 : }
155 :
156 :
157 : /*-----------------------------------------------------------------------------------------*
158 : * Function ivas_map_num_drct_r_to_idx()
159 : *
160 : * Map the ivas_arith_drct_r_consts and ivas_huff_drct_r_consts tables
161 : *-----------------------------------------------------------------------------------------*/
162 :
163 782502 : int16_t ivas_map_num_drct_r_to_idx(
164 : const int16_t num_quant_points_drct_r )
165 : {
166 782502 : int16_t drct_r_to_idx = -1;
167 782502 : switch ( num_quant_points_drct_r )
168 : {
169 391324 : case 1:
170 391324 : drct_r_to_idx = DRCT_Q_1;
171 391324 : break;
172 240816 : case 7:
173 240816 : drct_r_to_idx = DRCT_Q_7;
174 240816 : break;
175 63916 : case 9:
176 63916 : drct_r_to_idx = DRCT_Q_9;
177 63916 : break;
178 86446 : case 11:
179 86446 : drct_r_to_idx = DRCT_Q_11;
180 86446 : break;
181 0 : default:
182 0 : assert( !"Forbidden value for DRCT quantization strategy index" );
183 : break;
184 : }
185 782502 : return drct_r_to_idx;
186 : }
187 :
188 :
189 : /*-----------------------------------------------------------------------------------------*
190 : * Function ivas_map_num_decd_r_to_idx()
191 : *
192 : * Map the ivas_arith_decd_r_consts and ivas_huff_decd_r_consts tables
193 : *-----------------------------------------------------------------------------------------*/
194 :
195 782502 : int16_t ivas_map_num_decd_r_to_idx(
196 : const int16_t num_quant_points_decd_r )
197 : {
198 782502 : int16_t decd_r_to_idx = -1;
199 782502 : switch ( num_quant_points_decd_r )
200 : {
201 132556 : case 1:
202 132556 : decd_r_to_idx = DECD_Q_1;
203 132556 : break;
204 223486 : case 3:
205 223486 : decd_r_to_idx = DECD_Q_3;
206 223486 : break;
207 230580 : case 5:
208 230580 : decd_r_to_idx = DECD_Q_5;
209 230580 : break;
210 63636 : case 7:
211 63636 : decd_r_to_idx = DECD_Q_7;
212 63636 : break;
213 63916 : case 9:
214 63916 : decd_r_to_idx = DECD_Q_9;
215 63916 : break;
216 68328 : case 11:
217 68328 : decd_r_to_idx = DECD_Q_11;
218 68328 : break;
219 0 : default:
220 0 : assert( !"Forbidden value for DECD quantization strategy index" );
221 : break;
222 : }
223 :
224 782502 : return decd_r_to_idx;
225 : }
226 :
227 :
228 : /*---------------------------------------------------------------------------------------- - *
229 : * Function ivas_spar_arith_com_init()
230 : *
231 : * arith coder init
232 : *---------------------------------------------------------------------------------------- - */
233 :
234 1173753 : static void ivas_spar_arith_com_init(
235 : ivas_arith_t *pArith,
236 : const ivas_freq_models_t *pFreq_models,
237 : ivas_arith_t *pArith_diff,
238 : const int16_t q_levels,
239 : const int16_t enc_dec )
240 : {
241 : int16_t i, j;
242 : float sum;
243 :
244 1173753 : pArith->vals = pFreq_models->vals;
245 1173753 : pArith->range = q_levels;
246 1173753 : pArith->num_models = pFreq_models->num_models;
247 1173753 : pArith->dyn_model_bits = ivas_get_bits_to_encode( pArith->num_models - 1 );
248 1173753 : pArith->pFreq_model = pFreq_models->freq_model[0];
249 :
250 1173753 : ivas_get_cum_freq_model( pArith->pFreq_model, pArith->range, pArith->cum_freq[0] );
251 :
252 4695012 : for ( i = 0; i < pArith->num_models - 1; i++ )
253 : {
254 3521259 : pArith->pAlt_freq_models[i] = pFreq_models->freq_model[i + 1];
255 3521259 : ivas_get_cum_freq_model( pArith->pAlt_freq_models[i], pArith->range, pArith->cum_freq[i + 1] );
256 : }
257 :
258 1173753 : if ( enc_dec == ENC )
259 : {
260 197055 : sum = 0;
261 1857446 : for ( i = 1; i < pArith->range + 1; i++ )
262 : {
263 1660391 : sum += pArith->pFreq_model[i];
264 : }
265 :
266 1857446 : for ( i = 1; i < pArith->range + 1; i++ )
267 : {
268 1660391 : pArith->saved_dist_arr[0][i - 1] = log2f( max( 1e-10f, pArith->pFreq_model[i] ) );
269 1660391 : pArith->saved_dist_arr[0][i - 1] -= log2f( max( 1e-10f, sum ) );
270 : }
271 :
272 788220 : for ( j = 0; j < pArith->num_models - 1; j++ )
273 : {
274 591165 : sum = 0;
275 5572338 : for ( i = 1; i < pArith->range + 1; i++ )
276 : {
277 4981173 : sum += pArith->pAlt_freq_models[j][i];
278 : }
279 :
280 5572338 : for ( i = 1; i < pArith->range + 1; i++ )
281 : {
282 4981173 : pArith->saved_dist_arr[j + 1][i - 1] = log2f( max( 1e-10f, pArith->pAlt_freq_models[j][i] ) );
283 4981173 : pArith->saved_dist_arr[j + 1][i - 1] -= log2f( max( 1e-10f, sum ) );
284 : }
285 : }
286 : }
287 :
288 1173753 : pArith_diff->vals = pFreq_models->diff_vals;
289 1173753 : pArith_diff->range = q_levels;
290 1173753 : pArith_diff->num_models = pFreq_models->diff_num_models;
291 1173753 : pArith_diff->dyn_model_bits = ivas_get_bits_to_encode( pArith_diff->num_models - 1 );
292 1173753 : pArith_diff->pFreq_model = pFreq_models->diff_freq_model[0];
293 :
294 1173753 : ivas_get_cum_freq_model( pArith_diff->pFreq_model, pArith_diff->range, pArith_diff->cum_freq[0] );
295 :
296 4695012 : for ( i = 0; i < pArith_diff->num_models - 1; i++ )
297 : {
298 3521259 : pArith_diff->pAlt_freq_models[i] = pFreq_models->diff_freq_model[i + 1];
299 3521259 : ivas_get_cum_freq_model( pArith_diff->pAlt_freq_models[i], pArith_diff->range, pArith_diff->cum_freq[i + 1] );
300 : }
301 :
302 1173753 : if ( enc_dec == ENC )
303 : {
304 197055 : sum = 0;
305 1857446 : for ( i = 1; i < pArith_diff->range + 1; i++ )
306 : {
307 1660391 : sum += pArith_diff->pFreq_model[i];
308 : }
309 :
310 1857446 : for ( i = 1; i < pArith_diff->range + 1; i++ )
311 : {
312 1660391 : pArith_diff->saved_dist_arr[0][i - 1] = log2f( max( 1e-10f, pArith_diff->pFreq_model[i] ) );
313 1660391 : pArith_diff->saved_dist_arr[0][i - 1] -= log2f( max( 1e-10f, sum ) );
314 : }
315 :
316 788220 : for ( j = 0; j < pArith_diff->num_models - 1; j++ )
317 : {
318 591165 : sum = 0;
319 5572338 : for ( i = 1; i < pArith_diff->range + 1; i++ )
320 : {
321 4981173 : sum += pArith_diff->pAlt_freq_models[j][i];
322 : }
323 :
324 5572338 : for ( i = 1; i < pArith_diff->range + 1; i++ )
325 : {
326 4981173 : pArith_diff->saved_dist_arr[j + 1][i - 1] = log2f( max( 1e-10f, pArith_diff->pAlt_freq_models[j][i] ) );
327 4981173 : pArith_diff->saved_dist_arr[j + 1][i - 1] -= log2f( max( 1e-10f, sum ) );
328 : }
329 : }
330 : }
331 :
332 1173753 : return;
333 : }
334 :
335 :
336 : /*-----------------------------------------------------------------------------------------*
337 : * Function ivas_spar_arith_coeffs_com_init()
338 : *
339 : * Init for Arithm. coding
340 : *-----------------------------------------------------------------------------------------*/
341 :
342 130417 : void ivas_spar_arith_coeffs_com_init(
343 : ivas_arith_coeffs_t *pArith_coeffs,
344 : ivas_spar_md_com_cfg *pSpar_cfg,
345 : const int16_t table_idx,
346 : const int16_t enc_dec )
347 : {
348 : int16_t i, pred_r_index, drct_r_index, decd_r_index;
349 : int16_t num_quant_points_pred_r, num_quant_points_drct_r, num_quant_points_decd_r;
350 :
351 521668 : for ( i = 0; i < MAX_QUANT_STRATS; i++ )
352 : {
353 391251 : num_quant_points_pred_r = ivas_spar_br_table_consts[table_idx].q_lvls[i][0]; /* 0: pred_r */
354 391251 : pred_r_index = ivas_map_num_pred_r_to_idx( num_quant_points_pred_r, ivas_spar_br_table_consts[table_idx].active_w );
355 391251 : ivas_spar_arith_com_init( &pArith_coeffs->pred_arith_re[i], &ivas_arith_pred_r_consts[pred_r_index],
356 391251 : &pArith_coeffs->pred_arith_re_diff[i], pSpar_cfg->quant_strat[i].PR.q_levels[0], enc_dec );
357 :
358 391251 : num_quant_points_drct_r = ivas_spar_br_table_consts[table_idx].q_lvls[i][1]; /* 1: drct_r */
359 391251 : drct_r_index = ivas_map_num_drct_r_to_idx( num_quant_points_drct_r );
360 391251 : ivas_spar_arith_com_init( &pArith_coeffs->drct_arith_re[i], &ivas_arith_drct_r_consts[drct_r_index],
361 391251 : &pArith_coeffs->drct_arith_re_diff[i], pSpar_cfg->quant_strat[i].C.q_levels[0], enc_dec );
362 :
363 391251 : num_quant_points_decd_r = ivas_spar_br_table_consts[table_idx].q_lvls[i][2]; /* 2: decd_r */
364 391251 : decd_r_index = ivas_map_num_decd_r_to_idx( num_quant_points_decd_r );
365 391251 : ivas_spar_arith_com_init( &pArith_coeffs->decd_arith_re[i], &ivas_arith_decd_r_consts[decd_r_index],
366 391251 : &pArith_coeffs->decd_arith_re_diff[i], pSpar_cfg->quant_strat[i].P_r.q_levels[0], enc_dec );
367 : }
368 :
369 130417 : return;
370 : }
371 :
372 :
373 : /*-----------------------------------------------------------------------------------------*
374 : * Function ivas_huffman_dec_init_min_max_len()
375 : *
376 : * Find min and max length in codebook and finalize initialization of ivas_huffman_cfg_t.
377 : *-----------------------------------------------------------------------------------------*/
378 :
379 976698 : static void ivas_huffman_dec_init_min_max_len(
380 : ivas_huffman_cfg_t *p_huff_cfg )
381 : {
382 : int16_t i, code_len;
383 : const int16_t *codebook;
384 :
385 976698 : codebook = p_huff_cfg->codebook;
386 :
387 976698 : p_huff_cfg->min_len = p_huff_cfg->sym_len;
388 976698 : p_huff_cfg->max_len = 0;
389 :
390 8905548 : for ( i = 0; i < p_huff_cfg->sym_len; i++ )
391 : {
392 7928850 : code_len = codebook[1];
393 7928850 : if ( p_huff_cfg->min_len > code_len )
394 : {
395 1657280 : p_huff_cfg->min_len = code_len;
396 : }
397 7928850 : if ( p_huff_cfg->max_len < code_len )
398 : {
399 706323 : p_huff_cfg->max_len = code_len;
400 : }
401 7928850 : codebook = codebook + 3;
402 : }
403 :
404 976698 : return;
405 : }
406 :
407 :
408 : /*-----------------------------------------------------------------------------------------*
409 : * Function ivas_spar_huff_coeffs_com_init()
410 : *
411 : * Init for Huffman decoding
412 : *-----------------------------------------------------------------------------------------*/
413 :
414 130417 : void ivas_spar_huff_coeffs_com_init(
415 : ivas_huff_coeffs_t *pHuff_coeffs,
416 : ivas_spar_md_com_cfg *pSpar_cfg,
417 : const int16_t table_idx,
418 : const int16_t enc_dec )
419 : {
420 : int16_t i, pred_r_index, drct_r_index, decd_r_index;
421 : int16_t num_quant_points_pred_r, num_quant_points_drct_r, num_quant_points_decd_r;
422 : ivas_huffman_cfg_t *p_huff_cfg;
423 :
424 521668 : for ( i = 0; i < MAX_QUANT_STRATS; i++ )
425 : {
426 391251 : p_huff_cfg = &pHuff_coeffs->pred_huff_re[i];
427 391251 : num_quant_points_pred_r = ivas_spar_br_table_consts[table_idx].q_lvls[i][0]; /* 0: pred_r */
428 391251 : pred_r_index = ivas_map_num_pred_r_to_idx( num_quant_points_pred_r, 0 );
429 391251 : p_huff_cfg->codebook = &ivas_huff_pred_r_consts[pred_r_index].code_book[0][0];
430 391251 : if ( enc_dec == DEC )
431 : {
432 325566 : p_huff_cfg->sym_len = pSpar_cfg->quant_strat[i].PR.q_levels[0];
433 325566 : ivas_huffman_dec_init_min_max_len( p_huff_cfg );
434 : }
435 :
436 391251 : p_huff_cfg = &pHuff_coeffs->drct_huff_re[i];
437 391251 : num_quant_points_drct_r = ivas_spar_br_table_consts[table_idx].q_lvls[i][1]; /* 1: drct_r */
438 391251 : drct_r_index = ivas_map_num_drct_r_to_idx( num_quant_points_drct_r );
439 391251 : p_huff_cfg->codebook = &ivas_huff_drct_r_consts[drct_r_index].code_book[0][0];
440 391251 : if ( enc_dec == DEC )
441 : {
442 325566 : p_huff_cfg->sym_len = pSpar_cfg->quant_strat[i].C.q_levels[0];
443 325566 : ivas_huffman_dec_init_min_max_len( p_huff_cfg );
444 : }
445 :
446 391251 : p_huff_cfg = &pHuff_coeffs->decd_huff_re[i];
447 391251 : num_quant_points_decd_r = ivas_spar_br_table_consts[table_idx].q_lvls[i][2]; /* 2: decd_r */
448 391251 : decd_r_index = ivas_map_num_decd_r_to_idx( num_quant_points_decd_r );
449 391251 : p_huff_cfg->codebook = &ivas_huff_decd_r_consts[decd_r_index].code_book[0][0];
450 391251 : if ( enc_dec == DEC )
451 : {
452 325566 : p_huff_cfg->sym_len = pSpar_cfg->quant_strat[i].P_r.q_levels[0];
453 325566 : ivas_huffman_dec_init_min_max_len( p_huff_cfg );
454 : }
455 : }
456 :
457 130417 : return;
458 : }
|