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 <math.h>
43 : #include "cnst.h"
44 : #include "rom_com.h"
45 : #include "prot.h"
46 : #include "basop_util.h"
47 : #include "basop_proto_func.h"
48 : #include "wmc_auto.h"
49 :
50 : /*--------------------------------------------------------------------------*
51 : * mdct_spectrum_denorm()
52 : *
53 : *
54 : *--------------------------------------------------------------------------*/
55 :
56 136 : void mdct_spectrum_denorm(
57 : const int32_t inp_vector[],
58 : float y2[],
59 : const int16_t band_start[],
60 : const int16_t band_end[],
61 : const int16_t band_width[],
62 : const float band_energy[],
63 : const int16_t npulses[],
64 : const int16_t bands,
65 : const float ld_slope,
66 : const float pd_thresh )
67 : {
68 : int16_t i, k;
69 : float Eyy, gamma, pd, gain_tweak;
70 :
71 3248 : for ( k = 0; k < bands; k++ )
72 : {
73 3112 : Eyy = 0;
74 80360 : for ( i = band_start[k]; i <= band_end[k]; i++ )
75 : {
76 77248 : Eyy += (float) inp_vector[i] * inp_vector[i];
77 : }
78 :
79 3112 : if ( Eyy > 0.0f )
80 : {
81 : /* Set gamma to be pulse gain which results in perfect quantized subband energy */
82 2084 : gamma = (float) sqrt( pow( 2.0f, band_energy[k] ) / Eyy );
83 :
84 : /* Adjust gamma based on pulse density (0 bit MSE gain estimator) */
85 2084 : pd = (float) npulses[k] / band_width[k];
86 2084 : if ( pd < pd_thresh )
87 : {
88 1580 : gain_tweak = (float) pow( 2.0f, ( ld_slope * log2_f( pd / pd_thresh ) ) );
89 1580 : gamma *= gain_tweak;
90 : }
91 :
92 30044 : for ( i = band_start[k]; i <= band_end[k]; i++ )
93 : {
94 27960 : y2[i] = gamma * inp_vector[i];
95 : }
96 : }
97 : }
98 :
99 136 : return;
100 : }
101 : /*--------------------------------------------------------------------------*
102 : * hq2_core_configure()
103 : *
104 : *
105 : *--------------------------------------------------------------------------*/
106 :
107 136 : void hq2_core_configure(
108 : const int16_t frame_length, /* i : frame length */
109 : const int16_t num_bits, /* i : number of bits */
110 : const int16_t is_transient, /* i : transient flag */
111 : int16_t *bands,
112 : int16_t *length,
113 : int16_t band_width[],
114 : int16_t band_start[],
115 : int16_t band_end[],
116 : Word32 *L_qint,
117 : Word16 *eref_fx,
118 : Word16 *bit_alloc_weight_fx,
119 : int16_t *gqlevs,
120 : int16_t *Ngq,
121 : int16_t *p2a_bands,
122 : float *p2a_th,
123 : float *pd_thresh,
124 : float *ld_slope,
125 : float *ni_coef,
126 : float *ni_pd_th,
127 : int32_t bwe_br )
128 : {
129 : const Xcore_Config *xcore_config;
130 :
131 : int16_t i, k;
132 : int16_t bands_sh;
133 :
134 136 : if ( frame_length == L_FRAME8k )
135 : {
136 0 : if ( is_transient )
137 : {
138 0 : if ( num_bits <= ACELP_7k20 / FRAMES_PER_SEC )
139 : {
140 0 : xcore_config = &xcore_config_8kHz_007200bps_short;
141 : }
142 0 : else if ( num_bits <= ACELP_8k00 / FRAMES_PER_SEC )
143 : {
144 0 : xcore_config = &xcore_config_8kHz_008000bps_short;
145 : }
146 0 : else if ( num_bits <= ACELP_13k20 / FRAMES_PER_SEC )
147 : {
148 0 : xcore_config = &xcore_config_8kHz_013200bps_short;
149 : }
150 : else
151 : {
152 0 : xcore_config = &xcore_config_8kHz_016400bps_short;
153 : }
154 : }
155 : else
156 : {
157 0 : if ( num_bits <= ACELP_7k20 / FRAMES_PER_SEC )
158 : {
159 0 : xcore_config = &xcore_config_8kHz_007200bps_long;
160 : }
161 0 : else if ( num_bits <= ACELP_8k00 / FRAMES_PER_SEC )
162 : {
163 0 : xcore_config = &xcore_config_8kHz_008000bps_long;
164 : }
165 0 : else if ( num_bits <= ACELP_13k20 / FRAMES_PER_SEC )
166 : {
167 0 : xcore_config = &xcore_config_8kHz_013200bps_long;
168 : }
169 : else
170 : {
171 0 : xcore_config = &xcore_config_8kHz_016400bps_long;
172 : }
173 : }
174 : }
175 136 : else if ( frame_length == L_FRAME16k )
176 : {
177 0 : if ( is_transient )
178 : {
179 0 : if ( num_bits <= ACELP_13k20 / FRAMES_PER_SEC )
180 : {
181 0 : xcore_config = &xcore_config_16kHz_013200bps_short;
182 : }
183 : else
184 : {
185 0 : xcore_config = &xcore_config_16kHz_016400bps_short;
186 : }
187 : }
188 : else
189 : {
190 0 : if ( num_bits <= ACELP_13k20 / FRAMES_PER_SEC )
191 : {
192 0 : xcore_config = &xcore_config_16kHz_013200bps_long;
193 : }
194 : else
195 : {
196 0 : xcore_config = &xcore_config_16kHz_016400bps_long;
197 : }
198 : }
199 : }
200 : else /* (bwidth == SWB) */
201 : {
202 136 : if ( is_transient )
203 : {
204 12 : if ( bwe_br == ACELP_13k20 )
205 : {
206 12 : xcore_config = &xcore_config_32kHz_013200bps_short;
207 : }
208 : else
209 : {
210 0 : xcore_config = &xcore_config_32kHz_016400bps_short;
211 : }
212 : }
213 : else
214 : {
215 124 : if ( bwe_br == ACELP_13k20 )
216 : {
217 124 : xcore_config = &xcore_config_32kHz_013200bps_long;
218 : }
219 : else
220 : {
221 0 : xcore_config = &xcore_config_32kHz_016400bps_long;
222 : }
223 : }
224 : }
225 :
226 136 : *bands = xcore_config->bands;
227 136 : *length = xcore_config->bw;
228 136 : *L_qint = xcore_config->L_qint;
229 136 : *eref_fx = xcore_config->eref_fx;
230 136 : *bit_alloc_weight_fx = xcore_config->bit_alloc_weight_fx;
231 136 : *gqlevs = xcore_config->gqlevs;
232 136 : *Ngq = xcore_config->Ngq;
233 :
234 136 : *p2a_bands = xcore_config->p2a_bands;
235 136 : *p2a_th = xcore_config->p2a_th;
236 :
237 136 : *pd_thresh = xcore_config->pd_thresh;
238 136 : *ld_slope = xcore_config->ld_slope;
239 136 : *ni_coef = xcore_config->ni_coef;
240 136 : *ni_pd_th = xcore_config->ni_pd_th;
241 :
242 136 : mvs2s( xcore_config->band_width, band_width, *bands );
243 :
244 : /* Expand band_width[] table for short windows */
245 136 : if ( is_transient )
246 : {
247 12 : bands_sh = *bands;
248 12 : *bands = NUM_TIME_SWITCHING_BLOCKS * bands_sh;
249 12 : *length *= NUM_TIME_SWITCHING_BLOCKS;
250 :
251 48 : for ( i = 1; i <= 3; i++ )
252 : {
253 324 : for ( k = 0; k < bands_sh; k++ )
254 : {
255 288 : band_width[i * bands_sh + k] = band_width[k];
256 : }
257 : }
258 : }
259 :
260 : /* Formulate band_start and band_end tables from band_width table */
261 136 : band_start[0] = 0;
262 136 : band_end[0] = band_width[0] - 1;
263 3112 : for ( k = 1; k < *bands; k++ )
264 : {
265 2976 : band_start[k] = band_start[k - 1] + band_width[k - 1];
266 2976 : band_end[k] = band_start[k] + band_width[k] - 1;
267 : }
268 :
269 136 : return;
270 : }
271 :
272 : /*--------------------------------------------------------------------------*
273 : * reverse_transient_frame_energies()
274 : *
275 : *
276 : *--------------------------------------------------------------------------*/
277 :
278 15 : void reverse_transient_frame_energies(
279 : float band_energy[], /* o : band energies */
280 : const int16_t bands /* i : number of bands */
281 : )
282 : {
283 : int16_t k, k1, k2;
284 : float be;
285 :
286 15 : k1 = bands / 4;
287 15 : k2 = bands / 2 - 1;
288 75 : for ( k = 0; k < bands / 8; k++ )
289 : {
290 60 : be = band_energy[k1];
291 60 : band_energy[k1] = band_energy[k2];
292 60 : band_energy[k2] = be;
293 60 : k1++, k2--;
294 : }
295 :
296 15 : k1 = 3 * bands / 4;
297 15 : k2 = bands - 1;
298 75 : for ( k = 0; k < bands / 8; k++ )
299 : {
300 60 : be = band_energy[k1];
301 60 : band_energy[k1] = band_energy[k2];
302 60 : band_energy[k2] = be;
303 60 : k1++, k2--;
304 : }
305 :
306 15 : return;
307 : }
308 :
309 : #define WMC_TOOL_SKIP
310 0 : void bit_allocation_second_fx(
311 : Word32 *Rk,
312 : Word32 *Rk_sort,
313 : Word16 BANDS,
314 : const Word16 *band_width,
315 : Word16 *k_sort,
316 : Word16 *k_num,
317 : const Word16 *p2a_flags,
318 : const Word16 p2a_bands,
319 : const Word16 *last_bitalloc,
320 : const Word16 input_frame )
321 : {
322 0 : Word16 k, k2 = 0;
323 : Word16 ever_bits[BANDS_MAX], ever_sort[BANDS_MAX]; /*Q12 */
324 0 : Word16 class_flag = 0;
325 0 : Word16 rk_temp = 32767, ever_temp = 32767; /*Q12 */
326 : Word16 exp;
327 : Word16 tmp;
328 : Word32 L_tmp;
329 :
330 0 : for ( k = 0; k < BANDS; k++ )
331 : {
332 0 : if ( ( ( sub( k_sort[k], sub( BANDS, p2a_bands ) ) >= 0 ) && ( sub( p2a_flags[k_sort[k]], 1 ) == 0 ) ) ||
333 0 : ( ( sub( k_sort[k], sub( BANDS, 2 ) ) >= 0 ) && ( sub( last_bitalloc[sub( k_sort[k], sub( BANDS, 2 ) )], 1 ) == 0 ) ) )
334 : {
335 0 : exp = norm_s( band_width[k_sort[k]] );
336 0 : tmp = shl( band_width[k_sort[k]], exp ); /*Q(exp) */
337 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp = 29-exp) */
338 0 : L_tmp = Mult_32_16( Rk_sort[k], tmp ); /* Q(16+29-exp-15 = 30-exp) */
339 0 : tmp = sub( 18, exp );
340 0 : ever_bits[k] = extract_l( L_shr( L_tmp, tmp ) ); /*Q12 */
341 0 : if ( sub( ever_bits[k], rk_temp ) < 0 )
342 : {
343 0 : rk_temp = ever_bits[k];
344 0 : k2 = k;
345 : }
346 0 : class_flag = 1;
347 : }
348 : }
349 0 : if ( class_flag == 0 || sub( input_frame, L_FRAME8k ) == 0 )
350 : {
351 0 : for ( k = 0; k < BANDS; k++ )
352 : {
353 0 : if ( sub( k_sort[k], sub( BANDS, p2a_bands ) ) < 0 && Rk_sort[k] > 0 )
354 : {
355 0 : exp = norm_s( band_width[k_sort[k]] );
356 0 : tmp = shl( band_width[k_sort[k]], exp ); /*Q(exp) */
357 0 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp = 29-exp) */
358 0 : L_tmp = Mult_32_16( Rk_sort[k], tmp ); /* Q(16+29-exp-15 = 30-exp) */
359 0 : tmp = sub( 18, exp );
360 0 : ever_sort[k] = extract_l( L_shr( L_tmp, tmp ) ); /*Q12 */
361 0 : IF( sub( ever_sort[k], ever_temp ) < 0 )
362 : {
363 0 : ever_temp = ever_sort[k];
364 0 : k2 = k;
365 : }
366 : }
367 : }
368 : }
369 :
370 0 : k_num[0] = k2;
371 0 : if ( sub( k_sort[k2], sub( BANDS, 1 ) ) == 0 )
372 : {
373 0 : for ( k = 0; k < BANDS; k++ )
374 : {
375 0 : if ( sub( k_sort[k], sub( k_sort[k2], 1 ) ) == 0 )
376 : {
377 0 : k_num[1] = k;
378 : }
379 : }
380 : }
381 0 : else if ( k_sort[k2] == 0 )
382 : {
383 0 : for ( k = 0; k < BANDS; k++ )
384 : {
385 0 : if ( sub( k_sort[k], add( k_sort[k2], 1 ) ) == 0 )
386 : {
387 0 : k_num[1] = k;
388 : }
389 : }
390 : }
391 : else
392 : {
393 0 : if ( L_sub( Rk[sub( k_sort[k2], 1 )], Rk[add( k_sort[k2], 1 )] ) < 0 )
394 : {
395 0 : for ( k = 0; k < BANDS; k++ )
396 : {
397 0 : if ( sub( k_sort[k], sub( k_sort[k2], 1 ) ) == 0 )
398 : {
399 0 : k_num[1] = k;
400 : }
401 : }
402 : }
403 : else
404 : {
405 0 : for ( k = 0; k < BANDS; k++ )
406 : {
407 0 : if ( sub( k_sort[k], add( k_sort[k2], 1 ) ) == 0 )
408 : {
409 0 : k_num[1] = k;
410 : }
411 : }
412 : }
413 : }
414 :
415 0 : return;
416 : }
417 :
418 : #undef WMC_TOOL_SKIP
419 :
420 : /*--------------------------------------------------------------------------*
421 : * spt_shorten_domain_pre()
422 : *
423 : * Compute shorten subband if previous frame has spectral peak.
424 : *--------------------------------------------------------------------------*/
425 :
426 124 : void spt_shorten_domain_pre(
427 : const int16_t band_start[], /* i : Starting position of sub band */
428 : const int16_t band_end[], /* i : End position of sub band */
429 : const int16_t prev_SWB_peak_pos[], /* i : Spectral peak */
430 : const int16_t BANDS, /* i : total number of bands */
431 : const int32_t bwe_br, /* i : bitrate information */
432 : int16_t new_band_start[], /* o : Starting position of new shorten sub band */
433 : int16_t new_band_end[], /* o : End position of new shorten sub band */
434 : int16_t new_band_width[] /* o : new sub band bandwidth */
435 : )
436 : {
437 : int16_t j, k, kpos;
438 :
439 : int16_t new_band_width_half;
440 : const int16_t *p_bw_SPT_tbl; /* pointer of bw_SPT_tbl */
441 :
442 124 : p_bw_SPT_tbl = bw_SPT_tbl[0];
443 124 : if ( bwe_br == HQ_16k40 )
444 : {
445 0 : p_bw_SPT_tbl = bw_SPT_tbl[1];
446 : }
447 :
448 124 : kpos = 0;
449 124 : j = 0;
450 620 : for ( k = BANDS - SPT_SHORTEN_SBNUM; k < BANDS; k++ )
451 : {
452 496 : if ( prev_SWB_peak_pos[kpos] != 0 )
453 : {
454 4 : new_band_width[j] = p_bw_SPT_tbl[j];
455 :
456 : /*shorten the bandwidth for pulse resolution*/
457 4 : new_band_width_half = new_band_width[j] / 2;
458 4 : new_band_start[j] = prev_SWB_peak_pos[kpos] - new_band_width_half;
459 4 : new_band_end[j] = prev_SWB_peak_pos[kpos] + new_band_width_half;
460 :
461 4 : if ( new_band_start[j] < band_start[k] )
462 : {
463 0 : new_band_start[j] = band_start[k];
464 0 : new_band_end[j] = new_band_start[j] + ( new_band_width[j] - 1 );
465 : }
466 4 : else if ( new_band_end[j] > band_end[k] )
467 : {
468 4 : new_band_end[j] = band_end[k];
469 4 : new_band_start[j] = new_band_end[j] - ( new_band_width[j] - 1 );
470 : }
471 : }
472 : else
473 : {
474 492 : new_band_width[j] = p_bw_SPT_tbl[j];
475 :
476 : /*shorten the bandwidth for pulse resolution*/
477 492 : new_band_width_half = new_band_width[j] / 2;
478 492 : new_band_start[j] = ( band_start[k] + band_end[k] ) / 2 - new_band_width_half;
479 492 : new_band_end[j] = ( band_start[k] + band_end[k] ) / 2 + new_band_width_half;
480 : }
481 :
482 496 : kpos++;
483 496 : j++;
484 : }
485 :
486 124 : return;
487 : }
488 :
489 : /*--------------------------------------------------------------------------*
490 : * spt_shorten_domain_band_save()
491 : *
492 : * Store the original subband information
493 : *--------------------------------------------------------------------------*/
494 :
495 124 : void spt_shorten_domain_band_save(
496 : const int16_t bands, /* i : total subband */
497 : const int16_t band_start[], /* i : starting position of subband */
498 : const int16_t band_end[], /* i : end position of subband */
499 : const int16_t band_width[], /* i : band width of subband */
500 : int16_t org_band_start[], /* o : starting position of subband */
501 : int16_t org_band_end[], /* o : end position of subband */
502 : int16_t org_band_width[] /* o : band width of subband */
503 : )
504 : {
505 : int16_t k, kpos;
506 :
507 124 : kpos = 0;
508 620 : for ( k = bands - SPT_SHORTEN_SBNUM; k < bands; k++ )
509 : {
510 496 : org_band_start[kpos] = band_start[k];
511 496 : org_band_end[kpos] = band_end[k];
512 496 : org_band_width[kpos] = band_width[k];
513 496 : kpos++;
514 : }
515 :
516 124 : return;
517 : }
518 :
519 : /*--------------------------------------------------------------------------*
520 : * spt_shorten_domain_band_restore()
521 : *
522 : * Restrore the subband information
523 : *--------------------------------------------------------------------------*/
524 :
525 124 : void spt_shorten_domain_band_restore(
526 : const int16_t bands, /* i : total subband */
527 : int16_t band_start[], /* i/o: starting position of subband */
528 : int16_t band_end[], /* i/o: end position of subband */
529 : int16_t band_width[], /* i/o: band width of subband */
530 : const int16_t org_band_start[], /* o : starting position of subband */
531 : const int16_t org_band_end[], /* o : end position of subband */
532 : const int16_t org_band_width[] /* o : band width of subband */
533 : )
534 : {
535 : int16_t k, kpos;
536 :
537 124 : kpos = 0;
538 620 : for ( k = bands - SPT_SHORTEN_SBNUM; k < bands; k++ )
539 : {
540 496 : band_start[k] = org_band_start[kpos];
541 496 : band_end[k] = org_band_end[kpos];
542 496 : band_width[k] = org_band_width[kpos];
543 496 : kpos++;
544 : }
545 :
546 124 : return;
547 : }
548 :
549 : /*--------------------------------------------------------------------------*
550 : * spt_swb_peakpos_tmp_save
551 : *
552 : * Save Peak position for every higher subband
553 : *--------------------------------------------------------------------------*/
554 :
555 124 : void spt_swb_peakpos_tmp_save(
556 : const float y2[], /* i : coded spectral information */
557 : const int16_t bands, /* i : total number of bands */
558 : const int16_t band_start[], /* i : starting position of subband */
559 : const int16_t band_end[], /* i : end position of subband */
560 : int16_t prev_SWB_peak_pos_tmp[] /* o : spectral peaks */
561 : )
562 : {
563 :
564 : int16_t i, j, k;
565 : float peak_max;
566 :
567 124 : j = 0;
568 620 : for ( k = bands - SPT_SHORTEN_SBNUM; k < bands; k++ )
569 : {
570 496 : peak_max = 0;
571 496 : prev_SWB_peak_pos_tmp[j] = 0;
572 39184 : for ( i = band_start[k]; i <= band_end[k]; i++ )
573 : {
574 38688 : if ( peak_max < fabs( y2[i] ) )
575 : {
576 1152 : peak_max = (float) fabs( y2[i] );
577 1152 : prev_SWB_peak_pos_tmp[j] = i;
578 : }
579 : }
580 496 : j++;
581 : }
582 :
583 124 : return;
584 : }
|