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 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include "cnst.h"
43 : #include "rom_com.h"
44 : #include "prot.h"
45 : #include "wmc_auto.h"
46 :
47 : /*--------------------------------------------------------------------------*
48 : * sfm2mqb()
49 : *
50 : * Map sub-vectors to pbands
51 : *--------------------------------------------------------------------------*/
52 :
53 16880 : static void sfm2mqb(
54 : int16_t spe[], /* i : sub-vectors */
55 : int16_t spe2q[], /* o : pbands */
56 : const int16_t nb_sfm /* i : number of norms */
57 : )
58 : {
59 : int16_t tmp, i;
60 :
61 : /* short groups */
62 16880 : spe2q[0] = spe[0] + 3;
63 16880 : spe2q[1] = spe[1] + 3;
64 16880 : spe2q[2] = spe[2] + 3;
65 16880 : spe2q[3] = spe[3] + 3;
66 16880 : spe2q[4] = spe[4] + 3;
67 16880 : spe2q[5] = spe[5] + 3;
68 16880 : spe2q[6] = spe[6] + 3;
69 16880 : spe2q[7] = spe[7] + 3;
70 16880 : spe2q[8] = spe[8] + 3;
71 16880 : spe2q[9] = spe[9] + 3;
72 :
73 16880 : spe2q[10] = ( ( spe[10] + spe[11] ) >> 1 ) + 4;
74 16880 : spe2q[11] = ( ( spe[12] + spe[13] ) >> 1 ) + 4;
75 16880 : spe2q[12] = ( ( spe[14] + spe[15] ) >> 1 ) + 4;
76 :
77 16880 : spe2q[13] = ( ( spe[16] + spe[17] ) >> 1 ) + 5;
78 16880 : spe2q[14] = ( ( spe[18] + spe[19] ) >> 1 ) + 5;
79 :
80 16880 : tmp = 0;
81 84400 : for ( i = 20; i < 24; i++ )
82 : {
83 67520 : tmp += spe[i];
84 : }
85 16880 : spe2q[15] = (int16_t) ( ( (int32_t) tmp * 8192L ) >> 15 ) + 6;
86 :
87 16880 : tmp = 0;
88 67520 : for ( i = 24; i < 27; i++ )
89 : {
90 50640 : tmp += spe[i];
91 : }
92 16880 : spe2q[16] = (int16_t) ( ( (int32_t) tmp * 10923L ) >> 15 ) + 6;
93 :
94 16880 : if ( nb_sfm > 27 )
95 : {
96 16880 : tmp = 0;
97 67520 : for ( i = 27; i < 30; i++ )
98 : {
99 50640 : tmp += spe[i];
100 : }
101 16880 : spe2q[17] = (int16_t) ( ( (int32_t) tmp * 10923L ) >> 15 ) + 6;
102 :
103 16880 : if ( nb_sfm > 30 )
104 : {
105 16880 : tmp = 0;
106 101280 : for ( i = 30; i < 35; i++ )
107 : {
108 84400 : tmp += spe[i];
109 : }
110 16880 : spe2q[18] = (int16_t) ( ( (int32_t) tmp * 6553L ) >> 15 ) + 7;
111 :
112 16880 : tmp = 0;
113 168800 : for ( i = 35; i < 44; i++ )
114 : {
115 151920 : tmp += spe[i];
116 : }
117 16880 : spe2q[19] = (int16_t) ( ( (int32_t) tmp * 3641L ) >> 15 ) + 8;
118 : }
119 : }
120 :
121 16880 : return;
122 : }
123 :
124 : /*--------------------------------------------------------------------------*
125 : * mqb2sfm()
126 : *
127 : * Map pbands to sub-vectors
128 : *--------------------------------------------------------------------------*/
129 :
130 16880 : static void mqb2sfm(
131 : int16_t spe2q[], /* i : pbands */
132 : int16_t spe[], /* o : sub-vectors */
133 : const int16_t lnb_sfm /* i : number of norms */
134 : )
135 : {
136 : int16_t i;
137 :
138 16880 : spe[0] = spe2q[0];
139 16880 : spe[1] = spe2q[1];
140 16880 : spe[2] = spe2q[2];
141 16880 : spe[3] = spe2q[3];
142 16880 : spe[4] = spe2q[4];
143 16880 : spe[5] = spe2q[5];
144 16880 : spe[6] = spe2q[6];
145 16880 : spe[7] = spe2q[7];
146 16880 : spe[8] = spe2q[8];
147 16880 : spe[9] = spe2q[9];
148 :
149 16880 : spe[10] = spe2q[10];
150 16880 : spe[11] = spe2q[10];
151 :
152 16880 : spe[12] = spe2q[11];
153 16880 : spe[13] = spe2q[11];
154 :
155 16880 : spe[14] = spe2q[12];
156 16880 : spe[15] = spe2q[12];
157 :
158 16880 : spe[16] = spe2q[13];
159 16880 : spe[17] = spe2q[13];
160 :
161 16880 : spe[18] = spe2q[14];
162 16880 : spe[19] = spe2q[14];
163 84400 : for ( i = 20; i < 24; i++ )
164 : {
165 67520 : spe[i] = spe2q[15];
166 : }
167 :
168 67520 : for ( i = 24; i < 27; i++ )
169 : {
170 50640 : spe[i] = spe2q[16];
171 : }
172 :
173 16880 : if ( lnb_sfm > SFM_N_STA_8k )
174 : {
175 67520 : for ( i = 27; i < 30; i++ )
176 : {
177 50640 : spe[i] = spe2q[17];
178 : }
179 :
180 16880 : if ( lnb_sfm > SFM_N_STA_10k )
181 : {
182 101280 : for ( i = 30; i < 35; i++ )
183 : {
184 84400 : spe[i] = spe2q[18];
185 : }
186 :
187 168800 : for ( i = 35; i < 44; i++ )
188 : {
189 151920 : spe[i] = spe2q[19];
190 : }
191 : }
192 : }
193 :
194 16880 : return;
195 : }
196 :
197 : /*--------------------------------------------------------------------------*
198 : * map_quant_weight()
199 : *
200 : * Calculate the quantization weights
201 : *--------------------------------------------------------------------------*/
202 :
203 16880 : void map_quant_weight(
204 : const int16_t normqlg2[], /* i : quantized norms */
205 : int16_t wnorm[], /* o : weighted norm */
206 : const int16_t is_transient /* i : transient flag */
207 : )
208 : {
209 : int16_t sfm;
210 : int16_t tmp16;
211 : int16_t spe2q[NUM_MAP_BANDS];
212 : int16_t spe[NB_SFM];
213 :
214 : int16_t spe2q_max;
215 : int16_t spe2q_min;
216 : int16_t norm_max;
217 : int16_t shift;
218 : int16_t sum;
219 : int16_t k;
220 : int16_t lnb_sfm, num_map_bands;
221 :
222 16880 : if ( is_transient )
223 : {
224 1584 : lnb_sfm = NB_SFM;
225 1584 : num_map_bands = NUM_MAP_BANDS;
226 :
227 19008 : for ( sfm = 0; sfm < lnb_sfm; sfm += 4 )
228 : {
229 17424 : sum = 0;
230 87120 : for ( k = 0; k < 4; k++ )
231 : {
232 69696 : sum = sum + normqlg2[sfm + k];
233 : }
234 17424 : sum = sum >> 2;
235 87120 : for ( k = 0; k < 4; k++ )
236 : {
237 69696 : spe[sfm + k] = sum;
238 : }
239 : }
240 : }
241 : else
242 : {
243 15296 : lnb_sfm = NB_SFM;
244 15296 : num_map_bands = NUM_MAP_BANDS;
245 :
246 :
247 688320 : for ( sfm = 0; sfm < lnb_sfm; sfm++ )
248 : {
249 673024 : spe[sfm] = normqlg2[sfm];
250 : }
251 : }
252 :
253 16880 : sfm2mqb( spe, spe2q, lnb_sfm );
254 :
255 354480 : for ( sfm = 0; sfm < num_map_bands; sfm++ )
256 : {
257 337600 : spe2q[sfm] = spe2q[sfm] - 10;
258 : }
259 :
260 : /* spectral smoothing */
261 337600 : for ( sfm = 1; sfm < num_map_bands; sfm++ )
262 : {
263 320720 : tmp16 = spe2q[sfm - 1] - 4;
264 320720 : if ( spe2q[sfm] < tmp16 )
265 : {
266 40853 : spe2q[sfm] = tmp16;
267 : }
268 : }
269 :
270 337600 : for ( sfm = num_map_bands - 2; sfm >= 0; sfm-- )
271 : {
272 320720 : tmp16 = spe2q[sfm + 1] - 8;
273 320720 : if ( spe2q[sfm] < tmp16 )
274 : {
275 798 : spe2q[sfm] = tmp16;
276 : }
277 : }
278 :
279 354480 : for ( sfm = 0; sfm < num_map_bands; sfm++ )
280 : {
281 337600 : if ( spe2q[sfm] < a_map[sfm] )
282 : {
283 98765 : spe2q[sfm] = a_map[sfm];
284 : }
285 : }
286 :
287 : /* Saturate by the Absolute Threshold of Hearing */
288 16880 : spe2q_max = MIN16B;
289 16880 : spe2q_min = MAX16B;
290 :
291 354480 : for ( sfm = 0; sfm < num_map_bands; sfm++ )
292 : {
293 337600 : spe2q[sfm] = sfm_width[sfm] - spe2q[sfm];
294 :
295 337600 : if ( spe2q_max < spe2q[sfm] )
296 : {
297 107569 : spe2q_max = spe2q[sfm];
298 : }
299 :
300 337600 : if ( spe2q_min > spe2q[sfm] )
301 : {
302 27428 : spe2q_min = spe2q[sfm];
303 : }
304 : }
305 :
306 354480 : for ( sfm = 0; sfm < num_map_bands; sfm++ )
307 : {
308 337600 : spe2q[sfm] = spe2q[sfm] - spe2q_min;
309 : }
310 :
311 16880 : spe2q_max = spe2q_max - spe2q_min;
312 :
313 16880 : if ( spe2q_max == 0 )
314 : {
315 0 : norm_max = 0;
316 : }
317 : else
318 : {
319 16880 : if ( spe2q_max < 0 )
320 : {
321 0 : spe2q_max = ~spe2q_max;
322 : }
323 202245 : for ( norm_max = 0; spe2q_max < 0x4000; norm_max++ )
324 : {
325 185365 : spe2q_max <<= 1;
326 : }
327 : }
328 :
329 16880 : shift = norm_max - 13;
330 354480 : for ( sfm = 0; sfm < num_map_bands; sfm++ )
331 : {
332 337600 : if ( shift < 0 )
333 : {
334 337360 : spe2q[sfm] = spe2q[sfm] >> ( -shift );
335 : }
336 : else
337 : {
338 240 : spe2q[sfm] = spe2q[sfm] << shift;
339 : }
340 : }
341 :
342 16880 : mqb2sfm( spe2q, spe, lnb_sfm );
343 :
344 16880 : if ( is_transient )
345 : {
346 19008 : for ( sfm = 0; sfm < lnb_sfm; sfm += 4 )
347 : {
348 17424 : sum = 0;
349 87120 : for ( k = 0; k < 4; k++ )
350 : {
351 69696 : sum = sum + spe[sfm + k];
352 : }
353 :
354 17424 : sum = sum >> 2;
355 :
356 87120 : for ( k = 0; k < 4; k++ )
357 : {
358 69696 : spe[sfm + k] = sum;
359 : }
360 : }
361 : }
362 :
363 : /* modify the norms for bit-allocation */
364 759600 : for ( sfm = 0; sfm < lnb_sfm; sfm++ )
365 : {
366 742720 : wnorm[sfm] = spe[sfm] + normqlg2[sfm];
367 : }
368 :
369 16880 : return;
370 : }
|