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 "math.h"
39 : #include "prot.h"
40 : #include "ivas_prot.h"
41 : #include "ivas_rom_com.h"
42 : #include <assert.h>
43 : #include "wmc_auto.h"
44 :
45 : /*-----------------------------------------------------------------------------------------*
46 : * Function ivas_quantise_real_values()
47 : *
48 : * Quantize real values
49 : *-----------------------------------------------------------------------------------------*/
50 3383704 : void ivas_quantise_real_values(
51 : const float *values,
52 : const int16_t q_levels,
53 : const float min_value,
54 : const float max_value,
55 : int16_t *index,
56 : float *quant,
57 : const int16_t dim )
58 : {
59 : int16_t i;
60 : float q_step, one_by_q_step;
61 3383704 : if ( q_levels == 1 )
62 : {
63 10564 : for ( i = 0; i < dim; i++ )
64 : {
65 5282 : quant[i] = 0;
66 5282 : index[i] = 0;
67 : }
68 : }
69 3378422 : else if ( q_levels && max_value != min_value )
70 3378422 : {
71 3378422 : q_step = ( max_value - min_value ) / ( q_levels - 1 );
72 3378422 : one_by_q_step = ( q_levels - 1 ) / ( max_value - min_value );
73 : float val;
74 18061532 : for ( i = 0; i < dim; i++ )
75 : {
76 14683110 : val = max( min_value, min( values[i], max_value ) );
77 14683110 : index[i] = (int16_t) round( one_by_q_step * val );
78 14683110 : quant[i] = index[i] * q_step;
79 : }
80 : }
81 : else
82 : {
83 0 : for ( i = 0; i < dim; i++ )
84 : {
85 0 : quant[i] = values[i];
86 : }
87 : }
88 3383704 : return;
89 : }
90 :
91 :
92 : /*-----------------------------------------------------------------------------------------*
93 : * Function ivas_spar_get_uniform_quant_strat()
94 : *
95 : * Sets the quant strat values
96 : *-----------------------------------------------------------------------------------------*/
97 9452 : void ivas_spar_get_uniform_quant_strat(
98 : ivas_spar_md_com_cfg *pSpar_md_com_cfg,
99 : const int16_t table_idx )
100 : {
101 : int16_t i;
102 9452 : int16_t active_w = ivas_spar_br_table_consts[table_idx].active_w;
103 : int16_t PQ_q_lvl, C_q_lvl, Pr_q_lvl, Pc_q_lvl;
104 :
105 9452 : pSpar_md_com_cfg->num_quant_strats = MAX_QUANT_STRATS;
106 :
107 37808 : for ( i = 0; i < pSpar_md_com_cfg->num_quant_strats; i++ )
108 : {
109 28356 : PQ_q_lvl = ivas_spar_br_table_consts[table_idx].q_lvls[i][0];
110 28356 : C_q_lvl = ivas_spar_br_table_consts[table_idx].q_lvls[i][1];
111 28356 : Pr_q_lvl = ivas_spar_br_table_consts[table_idx].q_lvls[i][2];
112 28356 : Pc_q_lvl = ivas_spar_br_table_consts[table_idx].q_lvls[i][3];
113 :
114 28356 : if ( active_w )
115 : {
116 10233 : pSpar_md_com_cfg->quant_strat[i].PR.q_levels[0] = PQ_q_lvl;
117 10233 : pSpar_md_com_cfg->quant_strat[i].PR.q_levels[1] = PQ_q_lvl;
118 10233 : pSpar_md_com_cfg->quant_strat[i].PR.min = -1.2f;
119 10233 : pSpar_md_com_cfg->quant_strat[i].PR.max = 1.2f;
120 :
121 10233 : pSpar_md_com_cfg->quant_strat[i].C.q_levels[0] = C_q_lvl;
122 10233 : pSpar_md_com_cfg->quant_strat[i].C.q_levels[1] = C_q_lvl;
123 10233 : pSpar_md_com_cfg->quant_strat[i].C.min = -0.8f;
124 10233 : pSpar_md_com_cfg->quant_strat[i].C.max = 0.8f;
125 :
126 10233 : pSpar_md_com_cfg->quant_strat[i].P_r.q_levels[0] = Pr_q_lvl;
127 10233 : pSpar_md_com_cfg->quant_strat[i].P_r.q_levels[1] = Pr_q_lvl;
128 10233 : pSpar_md_com_cfg->quant_strat[i].P_r.min = 0;
129 10233 : pSpar_md_com_cfg->quant_strat[i].P_r.max = 0.8f;
130 :
131 10233 : pSpar_md_com_cfg->quant_strat[i].P_c.q_levels[0] = Pc_q_lvl;
132 10233 : pSpar_md_com_cfg->quant_strat[i].P_c.q_levels[1] = Pc_q_lvl;
133 10233 : pSpar_md_com_cfg->quant_strat[i].P_c.min = -0.8f;
134 10233 : pSpar_md_com_cfg->quant_strat[i].P_c.max = 0.8f;
135 : }
136 : else
137 : {
138 18123 : pSpar_md_com_cfg->quant_strat[i].PR.q_levels[0] = PQ_q_lvl;
139 18123 : pSpar_md_com_cfg->quant_strat[i].PR.q_levels[1] = PQ_q_lvl;
140 18123 : pSpar_md_com_cfg->quant_strat[i].PR.max = 1;
141 18123 : pSpar_md_com_cfg->quant_strat[i].PR.min = -1;
142 :
143 18123 : pSpar_md_com_cfg->quant_strat[i].C.q_levels[0] = C_q_lvl;
144 18123 : pSpar_md_com_cfg->quant_strat[i].C.q_levels[1] = C_q_lvl;
145 18123 : pSpar_md_com_cfg->quant_strat[i].C.max = 2;
146 18123 : pSpar_md_com_cfg->quant_strat[i].C.min = -2;
147 :
148 18123 : pSpar_md_com_cfg->quant_strat[i].P_r.q_levels[0] = Pr_q_lvl;
149 18123 : pSpar_md_com_cfg->quant_strat[i].P_r.q_levels[1] = Pr_q_lvl;
150 18123 : pSpar_md_com_cfg->quant_strat[i].P_r.max = 1.0f;
151 18123 : pSpar_md_com_cfg->quant_strat[i].P_r.min = 0;
152 :
153 18123 : pSpar_md_com_cfg->quant_strat[i].P_c.q_levels[0] = Pc_q_lvl;
154 18123 : pSpar_md_com_cfg->quant_strat[i].P_c.q_levels[1] = Pc_q_lvl;
155 18123 : pSpar_md_com_cfg->quant_strat[i].P_c.max = 0.5;
156 18123 : pSpar_md_com_cfg->quant_strat[i].P_c.min = -0.5;
157 : }
158 : }
159 :
160 9452 : return;
161 : }
162 :
163 :
164 : /*-----------------------------------------------------------------------------------------*
165 : * Function ivas_map_prior_coeffs_quant()
166 : *
167 : * Map prior coeffs
168 : *-----------------------------------------------------------------------------------------*/
169 :
170 315102 : void ivas_map_prior_coeffs_quant(
171 : ivas_spar_md_prev_t *pSpar_md_prior,
172 : ivas_spar_md_com_cfg *pSpar_md_cfg,
173 : const int16_t qsi,
174 : const int16_t nB )
175 : {
176 : int16_t i, j;
177 :
178 315102 : if ( qsi != pSpar_md_cfg->prev_quant_idx )
179 : {
180 3130 : ivas_quant_strat_t qs = pSpar_md_cfg->quant_strat[qsi];
181 3130 : ivas_quant_strat_t prev_qs = pSpar_md_cfg->quant_strat[pSpar_md_cfg->prev_quant_idx];
182 3130 : float one_by_q_lvl_PR = 1.0f / max( prev_qs.PR.q_levels[0] - 1, 1 );
183 3130 : float one_by_q_lvl_C = 1.0f / max( prev_qs.C.q_levels[0] - 1, 1 );
184 3130 : float one_by_q_lvl_P_r = 1.0f / max( prev_qs.P_r.q_levels[0] - 1, 1 );
185 28170 : for ( i = 0; i < nB; i++ )
186 : {
187 275440 : for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
188 : {
189 250400 : pSpar_md_prior->band_coeffs_idx_mapped[i].pred_index_re[j] = (int16_t) round( ( qs.PR.q_levels[0] - 1 ) * pSpar_md_prior->band_coeffs_idx[i].pred_index_re[j] * one_by_q_lvl_PR );
190 250400 : pSpar_md_prior->band_coeffs_idx_mapped[i].decd_index_re[j] = (int16_t) round( ( qs.P_r.q_levels[0] - 1 ) * pSpar_md_prior->band_coeffs_idx[i].decd_index_re[j] * one_by_q_lvl_P_r );
191 : }
192 550880 : for ( j = 0; j < IVAS_SPAR_MAX_C_COEFF; j++ )
193 : {
194 525840 : pSpar_md_prior->band_coeffs_idx_mapped[i].drct_index_re[j] = (int16_t) round( ( qs.C.q_levels[0] - 1 ) * pSpar_md_prior->band_coeffs_idx[i].drct_index_re[j] * one_by_q_lvl_C );
195 : }
196 : }
197 : }
198 : else
199 : {
200 2807748 : for ( i = 0; i < nB; i++ )
201 : {
202 27453536 : for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
203 : {
204 24957760 : pSpar_md_prior->band_coeffs_idx_mapped[i].pred_index_re[j] = pSpar_md_prior->band_coeffs_idx[i].pred_index_re[j];
205 24957760 : pSpar_md_prior->band_coeffs_idx_mapped[i].decd_index_re[j] = pSpar_md_prior->band_coeffs_idx[i].decd_index_re[j];
206 : }
207 54907072 : for ( j = 0; j < IVAS_SPAR_MAX_C_COEFF; j++ )
208 : {
209 52411296 : pSpar_md_prior->band_coeffs_idx_mapped[i].drct_index_re[j] = pSpar_md_prior->band_coeffs_idx[i].drct_index_re[j];
210 : }
211 : }
212 : }
213 :
214 315102 : return;
215 : }
216 :
217 :
218 : /*-----------------------------------------------------------------------------------------*
219 : * Function ivas_spar_quant_dtx_init()
220 : *
221 : * Init SPAR MD with minmax vals
222 : *-----------------------------------------------------------------------------------------*/
223 :
224 6005 : void ivas_spar_quant_dtx_init(
225 : ivas_spar_md_t *spar_md,
226 : float *min_max )
227 : {
228 6005 : spar_md->min_max[0] = min_max[0];
229 6005 : spar_md->min_max[1] = min_max[1];
230 :
231 6005 : return;
232 : }
233 :
234 :
235 : /*-----------------------------------------------------------------------------------------*
236 : * Function ivas_copy_band_coeffs_idx_to_arr()
237 : *
238 : * Copy pred band coeffs to arr
239 : *-----------------------------------------------------------------------------------------*/
240 :
241 2245484 : void ivas_copy_band_coeffs_idx_to_arr(
242 : ivas_band_coeffs_ind_t *pBands_idx,
243 : const int16_t nB,
244 : int16_t *pSymbol_re,
245 : ivas_cell_dim_t *pCell_dims,
246 : const ivas_coeffs_type_t coeff_type )
247 : {
248 : int16_t i, len;
249 2245484 : int16_t *pPtr_idx = NULL;
250 :
251 20196828 : for ( i = 0; i < nB; i++ )
252 : {
253 17951344 : switch ( coeff_type )
254 : {
255 4538572 : case PRED_COEFF:
256 : {
257 4538572 : pPtr_idx = pBands_idx[i].pred_index_re;
258 4538572 : break;
259 : }
260 4530820 : case DRCT_COEFF:
261 : {
262 4530820 : pPtr_idx = pBands_idx[i].drct_index_re;
263 4530820 : break;
264 : }
265 4530756 : case DECD_COEFF:
266 : {
267 4530756 : pPtr_idx = pBands_idx[i].decd_index_re;
268 4530756 : break;
269 : }
270 4351196 : case DECX_COEFF:
271 : {
272 4351196 : break;
273 : }
274 : }
275 17951344 : len = pCell_dims[i].dim1 * pCell_dims[i].dim2;
276 17951344 : if ( ( coeff_type != DECX_COEFF ) )
277 : {
278 13600148 : mvs2s( pPtr_idx, pSymbol_re, len );
279 13600148 : pSymbol_re += len;
280 : }
281 : }
282 :
283 :
284 2245484 : return;
285 : }
286 :
287 :
288 : /*-----------------------------------------------------------------------------------------*
289 : * Function ivas_clear_band_coeffs()
290 : *
291 : * clear band coeffs array in SPAR MD
292 : *-----------------------------------------------------------------------------------------*/
293 :
294 13678 : void ivas_clear_band_coeffs(
295 : ivas_band_coeffs_t *pband_coeffs,
296 : const uint16_t num_bands )
297 : {
298 : uint16_t i;
299 :
300 177814 : for ( i = 0; i < num_bands; i++ )
301 : {
302 164136 : set_zero( (float *) pband_coeffs[i].C_re, ( IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS ) * ( IVAS_SPAR_MAX_DMX_CHS - 1 ) );
303 164136 : set_zero( (float *) pband_coeffs[i].P_re, ( IVAS_SPAR_MAX_CH - 1 ) );
304 164136 : set_zero( (float *) pband_coeffs[i].C_quant_re, ( IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS ) * ( IVAS_SPAR_MAX_DMX_CHS - 1 ) );
305 164136 : set_zero( (float *) pband_coeffs[i].P_quant_re, ( IVAS_SPAR_MAX_CH - 1 ) );
306 164136 : set_zero( pband_coeffs[i].pred_re, ( IVAS_SPAR_MAX_CH - 1 ) );
307 164136 : set_zero( pband_coeffs[i].pred_quant_re, ( IVAS_SPAR_MAX_CH - 1 ) );
308 : }
309 :
310 13678 : return;
311 : }
312 :
313 :
314 : /*-----------------------------------------------------------------------------------------*
315 : * Function ivas_clear_band_coeff_idx()
316 : *
317 : * clear band coeffs index array in SPAR MD
318 : *-----------------------------------------------------------------------------------------*/
319 :
320 23448 : void ivas_clear_band_coeff_idx(
321 : ivas_band_coeffs_ind_t *pband_coeff_idx,
322 : const uint16_t num_bands )
323 : {
324 23448 : uint16_t i = 0;
325 :
326 304824 : for ( i = 0; i < num_bands; i++ )
327 : {
328 281376 : set_s( pband_coeff_idx[i].pred_index_re, 0, ( IVAS_SPAR_MAX_CH - 1 ) );
329 281376 : set_s( pband_coeff_idx[i].drct_index_re, 0, IVAS_SPAR_MAX_C_COEFF );
330 281376 : set_s( pband_coeff_idx[i].decd_index_re, 0, ( IVAS_SPAR_MAX_CH - 1 ) );
331 : }
332 :
333 23448 : return;
334 : }
|