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 917306 : 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 13045160 : for ( i = 0; i < length; i++ )
60 : {
61 12127854 : if ( pArr[i] < min_val )
62 : {
63 3734 : pArr[i] = max_val - min_val + pArr[i] + 1;
64 : }
65 12127854 : if ( pArr[i] > max_val )
66 : {
67 3200 : pArr[i] = min_val + pArr[i] - max_val - 1;
68 : }
69 : }
70 :
71 917306 : return;
72 : }
73 :
74 :
75 : /*-----------------------------------------------------------------------------------------*
76 : * Function ivas_get_cum_freq_model()
77 : *
78 : * get cumulative frequency model
79 : *-----------------------------------------------------------------------------------------*/
80 :
81 562752 : 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 562752 : pCum_freq_model[length] = 0;
89 :
90 5237312 : for ( i = length; i > 0; i-- )
91 : {
92 4674560 : pCum_freq_model[i - 1] = pCum_freq_model[i] + pFreq_model[i];
93 : }
94 :
95 562752 : 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 46896 : 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 46896 : int16_t pred_r_to_idx = -1;
110 46896 : if ( active_w_flag == 0 )
111 : {
112 38364 : switch ( num_quant_points_pred_r )
113 : {
114 5664 : case 1:
115 5664 : pred_r_to_idx = PRED_Q_1;
116 5664 : break;
117 2848 : case 7:
118 2848 : pred_r_to_idx = PRED_Q_7;
119 2848 : break;
120 11848 : case 15:
121 11848 : pred_r_to_idx = PRED_Q_15;
122 11848 : break;
123 15172 : case 21:
124 15172 : pred_r_to_idx = PRED_Q_21;
125 15172 : break;
126 2832 : case 31:
127 2832 : pred_r_to_idx = PRED_Q_31;
128 2832 : break;
129 0 : default:
130 0 : assert( !"Forbidden value for prediction quantization strategy index" );
131 : break;
132 : }
133 : }
134 : else
135 : {
136 8532 : switch ( num_quant_points_pred_r )
137 : {
138 2176 : case 7:
139 2176 : pred_r_to_idx = PRED_Q_7_ACTIVE_W;
140 2176 : break;
141 5688 : case 15:
142 5688 : pred_r_to_idx = PRED_Q_15_ACTIVE_W;
143 5688 : break;
144 668 : case 21:
145 668 : pred_r_to_idx = PRED_Q_21_ACTIVE_W;
146 668 : break;
147 0 : default:
148 0 : assert( !"Forbidden value for prediction quantization strategy index" );
149 : break;
150 : }
151 : }
152 :
153 46896 : 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 46896 : int16_t ivas_map_num_drct_r_to_idx(
164 : const int16_t num_quant_points_drct_r )
165 : {
166 46896 : int16_t drct_r_to_idx = -1;
167 46896 : switch ( num_quant_points_drct_r )
168 : {
169 23488 : case 1:
170 23488 : drct_r_to_idx = DRCT_Q_1;
171 23488 : break;
172 14424 : case 7:
173 14424 : drct_r_to_idx = DRCT_Q_7;
174 14424 : break;
175 4032 : case 9:
176 4032 : drct_r_to_idx = DRCT_Q_9;
177 4032 : break;
178 4952 : case 11:
179 4952 : drct_r_to_idx = DRCT_Q_11;
180 4952 : break;
181 0 : default:
182 0 : assert( !"Forbidden value for DRCT quantization strategy index" );
183 : break;
184 : }
185 46896 : 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 46896 : int16_t ivas_map_num_decd_r_to_idx(
196 : const int16_t num_quant_points_decd_r )
197 : {
198 46896 : int16_t decd_r_to_idx = -1;
199 46896 : switch ( num_quant_points_decd_r )
200 : {
201 6424 : case 1:
202 6424 : decd_r_to_idx = DECD_Q_1;
203 6424 : break;
204 13792 : case 3:
205 13792 : decd_r_to_idx = DECD_Q_3;
206 13792 : break;
207 14816 : case 5:
208 14816 : decd_r_to_idx = DECD_Q_5;
209 14816 : break;
210 3800 : case 7:
211 3800 : decd_r_to_idx = DECD_Q_7;
212 3800 : break;
213 4032 : case 9:
214 4032 : decd_r_to_idx = DECD_Q_9;
215 4032 : break;
216 4032 : case 11:
217 4032 : decd_r_to_idx = DECD_Q_11;
218 4032 : break;
219 0 : default:
220 0 : assert( !"Forbidden value for DECD quantization strategy index" );
221 : break;
222 : }
223 :
224 46896 : return decd_r_to_idx;
225 : }
226 :
227 :
228 : /*---------------------------------------------------------------------------------------- - *
229 : * Function ivas_spar_arith_com_init()
230 : *
231 : * arith coder init
232 : *---------------------------------------------------------------------------------------- - */
233 :
234 70344 : 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 70344 : pArith->vals = pFreq_models->vals;
245 70344 : pArith->range = q_levels;
246 70344 : pArith->num_models = pFreq_models->num_models;
247 70344 : pArith->dyn_model_bits = ivas_get_bits_to_encode( pArith->num_models - 1 );
248 70344 : pArith->pFreq_model = pFreq_models->freq_model[0];
249 :
250 70344 : ivas_get_cum_freq_model( pArith->pFreq_model, pArith->range, pArith->cum_freq[0] );
251 :
252 281376 : for ( i = 0; i < pArith->num_models - 1; i++ )
253 : {
254 211032 : pArith->pAlt_freq_models[i] = pFreq_models->freq_model[i + 1];
255 211032 : ivas_get_cum_freq_model( pArith->pAlt_freq_models[i], pArith->range, pArith->cum_freq[i + 1] );
256 : }
257 :
258 70344 : if ( enc_dec == ENC )
259 : {
260 17586 : sum = 0;
261 163666 : for ( i = 1; i < pArith->range + 1; i++ )
262 : {
263 146080 : sum += pArith->pFreq_model[i];
264 : }
265 :
266 163666 : for ( i = 1; i < pArith->range + 1; i++ )
267 : {
268 146080 : pArith->saved_dist_arr[0][i - 1] = log2f( max( 1e-10f, pArith->pFreq_model[i] ) );
269 146080 : pArith->saved_dist_arr[0][i - 1] -= log2f( max( 1e-10f, sum ) );
270 : }
271 :
272 70344 : for ( j = 0; j < pArith->num_models - 1; j++ )
273 : {
274 52758 : sum = 0;
275 490998 : for ( i = 1; i < pArith->range + 1; i++ )
276 : {
277 438240 : sum += pArith->pAlt_freq_models[j][i];
278 : }
279 :
280 490998 : for ( i = 1; i < pArith->range + 1; i++ )
281 : {
282 438240 : pArith->saved_dist_arr[j + 1][i - 1] = log2f( max( 1e-10f, pArith->pAlt_freq_models[j][i] ) );
283 438240 : pArith->saved_dist_arr[j + 1][i - 1] -= log2f( max( 1e-10f, sum ) );
284 : }
285 : }
286 : }
287 :
288 70344 : pArith_diff->vals = pFreq_models->diff_vals;
289 70344 : pArith_diff->range = q_levels;
290 70344 : pArith_diff->num_models = pFreq_models->diff_num_models;
291 70344 : pArith_diff->dyn_model_bits = ivas_get_bits_to_encode( pArith_diff->num_models - 1 );
292 70344 : pArith_diff->pFreq_model = pFreq_models->diff_freq_model[0];
293 :
294 70344 : ivas_get_cum_freq_model( pArith_diff->pFreq_model, pArith_diff->range, pArith_diff->cum_freq[0] );
295 :
296 281376 : for ( i = 0; i < pArith_diff->num_models - 1; i++ )
297 : {
298 211032 : pArith_diff->pAlt_freq_models[i] = pFreq_models->diff_freq_model[i + 1];
299 211032 : ivas_get_cum_freq_model( pArith_diff->pAlt_freq_models[i], pArith_diff->range, pArith_diff->cum_freq[i + 1] );
300 : }
301 :
302 70344 : if ( enc_dec == ENC )
303 : {
304 17586 : sum = 0;
305 163666 : for ( i = 1; i < pArith_diff->range + 1; i++ )
306 : {
307 146080 : sum += pArith_diff->pFreq_model[i];
308 : }
309 :
310 163666 : for ( i = 1; i < pArith_diff->range + 1; i++ )
311 : {
312 146080 : pArith_diff->saved_dist_arr[0][i - 1] = log2f( max( 1e-10f, pArith_diff->pFreq_model[i] ) );
313 146080 : pArith_diff->saved_dist_arr[0][i - 1] -= log2f( max( 1e-10f, sum ) );
314 : }
315 :
316 70344 : for ( j = 0; j < pArith_diff->num_models - 1; j++ )
317 : {
318 52758 : sum = 0;
319 490998 : for ( i = 1; i < pArith_diff->range + 1; i++ )
320 : {
321 438240 : sum += pArith_diff->pAlt_freq_models[j][i];
322 : }
323 :
324 490998 : for ( i = 1; i < pArith_diff->range + 1; i++ )
325 : {
326 438240 : pArith_diff->saved_dist_arr[j + 1][i - 1] = log2f( max( 1e-10f, pArith_diff->pAlt_freq_models[j][i] ) );
327 438240 : pArith_diff->saved_dist_arr[j + 1][i - 1] -= log2f( max( 1e-10f, sum ) );
328 : }
329 : }
330 : }
331 :
332 70344 : return;
333 : }
334 :
335 :
336 : /*-----------------------------------------------------------------------------------------*
337 : * Function ivas_spar_arith_coeffs_com_init()
338 : *
339 : * Init for Arithm. coding
340 : *-----------------------------------------------------------------------------------------*/
341 :
342 7816 : 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 31264 : for ( i = 0; i < MAX_QUANT_STRATS; i++ )
352 : {
353 23448 : num_quant_points_pred_r = ivas_spar_br_table_consts[table_idx].q_lvls[i][0]; /* 0: pred_r */
354 23448 : 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 23448 : ivas_spar_arith_com_init( &pArith_coeffs->pred_arith_re[i], &ivas_arith_pred_r_consts[pred_r_index],
356 23448 : &pArith_coeffs->pred_arith_re_diff[i], pSpar_cfg->quant_strat[i].PR.q_levels[0], enc_dec );
357 :
358 23448 : num_quant_points_drct_r = ivas_spar_br_table_consts[table_idx].q_lvls[i][1]; /* 1: drct_r */
359 23448 : drct_r_index = ivas_map_num_drct_r_to_idx( num_quant_points_drct_r );
360 23448 : ivas_spar_arith_com_init( &pArith_coeffs->drct_arith_re[i], &ivas_arith_drct_r_consts[drct_r_index],
361 23448 : &pArith_coeffs->drct_arith_re_diff[i], pSpar_cfg->quant_strat[i].C.q_levels[0], enc_dec );
362 :
363 23448 : num_quant_points_decd_r = ivas_spar_br_table_consts[table_idx].q_lvls[i][2]; /* 2: decd_r */
364 23448 : decd_r_index = ivas_map_num_decd_r_to_idx( num_quant_points_decd_r );
365 23448 : ivas_spar_arith_com_init( &pArith_coeffs->decd_arith_re[i], &ivas_arith_decd_r_consts[decd_r_index],
366 23448 : &pArith_coeffs->decd_arith_re_diff[i], pSpar_cfg->quant_strat[i].P_r.q_levels[0], enc_dec );
367 : }
368 :
369 7816 : 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 52758 : 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 52758 : codebook = p_huff_cfg->codebook;
386 :
387 52758 : p_huff_cfg->min_len = p_huff_cfg->sym_len;
388 52758 : p_huff_cfg->max_len = 0;
389 :
390 490998 : for ( i = 0; i < p_huff_cfg->sym_len; i++ )
391 : {
392 438240 : code_len = codebook[1];
393 438240 : if ( p_huff_cfg->min_len > code_len )
394 : {
395 90750 : p_huff_cfg->min_len = code_len;
396 : }
397 438240 : if ( p_huff_cfg->max_len < code_len )
398 : {
399 39417 : p_huff_cfg->max_len = code_len;
400 : }
401 438240 : codebook = codebook + 3;
402 : }
403 :
404 52758 : return;
405 : }
406 :
407 :
408 : /*-----------------------------------------------------------------------------------------*
409 : * Function ivas_spar_huff_coeffs_com_init()
410 : *
411 : * Init for Huffman decoding
412 : *-----------------------------------------------------------------------------------------*/
413 :
414 7816 : 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 31264 : for ( i = 0; i < MAX_QUANT_STRATS; i++ )
425 : {
426 23448 : p_huff_cfg = &pHuff_coeffs->pred_huff_re[i];
427 23448 : num_quant_points_pred_r = ivas_spar_br_table_consts[table_idx].q_lvls[i][0]; /* 0: pred_r */
428 23448 : pred_r_index = ivas_map_num_pred_r_to_idx( num_quant_points_pred_r, 0 );
429 23448 : p_huff_cfg->codebook = &ivas_huff_pred_r_consts[pred_r_index].code_book[0][0];
430 23448 : if ( enc_dec == DEC )
431 : {
432 17586 : p_huff_cfg->sym_len = pSpar_cfg->quant_strat[i].PR.q_levels[0];
433 17586 : ivas_huffman_dec_init_min_max_len( p_huff_cfg );
434 : }
435 :
436 23448 : p_huff_cfg = &pHuff_coeffs->drct_huff_re[i];
437 23448 : num_quant_points_drct_r = ivas_spar_br_table_consts[table_idx].q_lvls[i][1]; /* 1: drct_r */
438 23448 : drct_r_index = ivas_map_num_drct_r_to_idx( num_quant_points_drct_r );
439 23448 : p_huff_cfg->codebook = &ivas_huff_drct_r_consts[drct_r_index].code_book[0][0];
440 23448 : if ( enc_dec == DEC )
441 : {
442 17586 : p_huff_cfg->sym_len = pSpar_cfg->quant_strat[i].C.q_levels[0];
443 17586 : ivas_huffman_dec_init_min_max_len( p_huff_cfg );
444 : }
445 :
446 23448 : p_huff_cfg = &pHuff_coeffs->decd_huff_re[i];
447 23448 : num_quant_points_decd_r = ivas_spar_br_table_consts[table_idx].q_lvls[i][2]; /* 2: decd_r */
448 23448 : decd_r_index = ivas_map_num_decd_r_to_idx( num_quant_points_decd_r );
449 23448 : p_huff_cfg->codebook = &ivas_huff_decd_r_consts[decd_r_index].code_book[0][0];
450 23448 : if ( enc_dec == DEC )
451 : {
452 17586 : p_huff_cfg->sym_len = pSpar_cfg->quant_strat[i].P_r.q_levels[0];
453 17586 : ivas_huffman_dec_init_min_max_len( p_huff_cfg );
454 : }
455 : }
456 :
457 7816 : return;
458 : }
|