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 <assert.h>
38 : #include <stdint.h>
39 : #include "options.h"
40 : #ifdef DEBUGGING
41 : #include "debug.h"
42 : #endif
43 : #include <math.h>
44 : #include "cnst.h"
45 : #include "prot.h"
46 : #include "rom_com.h"
47 : #include "wmc_auto.h"
48 :
49 :
50 : /*------------------------------------------------------------------------*
51 : * Local function prototypes
52 : *------------------------------------------------------------------------*/
53 :
54 : static void dequant_peaks( Decoder_State *st, float *vect_out, const float *peak_gain );
55 :
56 : static int16_t hvq_dec_pos( Decoder_State *st, int16_t *pos_vec, const int16_t length, const int16_t num_peaks );
57 :
58 : static int16_t sparse_dec_pos( Decoder_State *st, int16_t *out, const int16_t length );
59 :
60 : static void peak_vq_dec( Decoder_State *st, float *coefs_out, const int32_t core_brate, const int16_t num_bits, const int16_t *ynrm, int16_t *R, int16_t *vq_peak_idx, int16_t *Npeaks, const int16_t core );
61 :
62 :
63 : /*--------------------------------------------------------------------------
64 : * hvq_dec()
65 : *
66 : * HVQ decoder
67 : *--------------------------------------------------------------------------*/
68 :
69 3867 : void hvq_dec(
70 : Decoder_State *st, /* i/o: decoder state structure */
71 : const int16_t num_bits, /* i : Number of available bits */
72 : const int32_t core_brate, /* i : Core bitrate */
73 : const int16_t *ynrm, /* i : Envelope coefficients */
74 : int16_t *R, /* i/o: Bit allocation/updated bit allocation */
75 : float *noise_level, /* o : Noise level */
76 : int16_t *peak_idx, /* o : Peak position vector */
77 : int16_t *Npeaks, /* o : Total number of peaks */
78 : float *coefsq_norm, /* o : Output vector */
79 : const int16_t core /* i : Core */
80 : )
81 : {
82 : int16_t i;
83 : int16_t bits;
84 : int16_t noise_level_idx;
85 :
86 3867 : bits = num_bits;
87 :
88 11601 : for ( i = 0; i < HVQ_BWE_NOISE_BANDS; i++ )
89 : {
90 7734 : noise_level_idx = get_next_indice( st, 2 );
91 7734 : noise_level[i] = usdequant( noise_level_idx, 0.0f, 0.1f );
92 :
93 7734 : bits -= 2;
94 : }
95 :
96 3867 : peak_vq_dec( st, coefsq_norm, core_brate, bits, ynrm, R, peak_idx, Npeaks, core );
97 :
98 3867 : return;
99 : }
100 :
101 :
102 : /*--------------------------------------------------------------------------
103 : * peak_vq_dec()
104 : *
105 : * Vector de-quantization of MDCT peaks
106 : *--------------------------------------------------------------------------*/
107 :
108 3867 : static void peak_vq_dec(
109 : Decoder_State *st, /* i/o: decoder state structure */
110 : float *coefs_out, /* o : Output coefficient vector */
111 : const int32_t core_brate, /* i : Core bitrate */
112 : const int16_t num_bits, /* i : Number of bits for HVQ */
113 : const int16_t *ynrm, /* i : Envelope coefficients */
114 : int16_t *R, /* i/o: Bit allocation/updated bit allocation */
115 : int16_t *vq_peak_idx, /* o : Peak position vector */
116 : int16_t *Npeaks, /* o : Number of peaks */
117 : const int16_t core /* i : Core type */
118 : )
119 : {
120 : int16_t vq_peaks, i, j, k, FlagN, hcode_l, diff;
121 : int16_t bin_th, max_peaks, pvq_bands;
122 3867 : int16_t nf_seed = RANDOM_INITSEED;
123 : int16_t nf_gains_idx[HVQ_NF_GROUPS], pgain_difidx[HVQ_MAX_PEAKS], pvq_norm[MAX_PVQ_BANDS];
124 : int16_t gain_bits_array[MAX_PVQ_BANDS];
125 : int16_t pos_bits;
126 : float nf_gains[HVQ_NF_GROUPS], peak_gains[HVQ_MAX_PEAKS];
127 : int32_t manE_peak, manPkEnrg; /* (man, exp) representation ported from BASOP for interoperability */
128 : int16_t expE_peak, expPkEnrg;
129 : float pvq_vector[HVQ_PVQ_BUF_LEN];
130 : int16_t res_vec[HVQ_THRES_BIN_32k];
131 : int16_t k_sort[HVQ_MAX_PVQ_WORDS];
132 : int16_t pvq_inp_vector[HVQ_PVQ_BUF_LEN];
133 : int16_t npulses[MAX_PVQ_BANDS];
134 : int16_t pvq_bits, Rk[MAX_PVQ_BANDS];
135 : float fg_pred[NB_SFM_MAX];
136 : int16_t Rk_f[MAX_PVQ_BANDS]; /* Q3 */
137 : int16_t sel_bnds[HVQ_NUM_SFM_24k];
138 : int16_t n_sel_bnds;
139 : int16_t hvq_band_end[MAX_PVQ_BANDS];
140 : int16_t hvq_band_start[MAX_PVQ_BANDS];
141 : int16_t hvq_band_width[MAX_PVQ_BANDS];
142 : int16_t n;
143 : int16_t s;
144 : float normq;
145 :
146 3867 : set_s( gain_bits_array, 0, MAX_PVQ_BANDS );
147 3867 : set_f( pvq_vector, 0.0f, HVQ_PVQ_BUF_LEN );
148 3867 : set_s( npulses, 0, MAX_PVQ_BANDS );
149 3867 : set_s( pvq_inp_vector, 0, HVQ_PVQ_BUF_LEN );
150 :
151 : /* Set bitrate dependent variables */
152 3867 : assert( ( core_brate > HQ_16k40 && core_brate <= HQ_48k ) && "HVQ rate not supported" );
153 3867 : max_peaks = (int16_t) ( ( core_brate * HVQ_PEAKS_PER_DELTA + HVQ_PEAKS_PER_DELTA_OFFS ) / HVQ_PEAKS_BPS_DELTA );
154 :
155 3867 : bin_th = HVQ_THRES_BIN_24k;
156 3867 : if ( core_brate >= HQ_BWE_CROSSOVER_BRATE )
157 : {
158 1638 : bin_th = HVQ_THRES_BIN_32k;
159 : }
160 :
161 : /* Get number of peaks */
162 3867 : vq_peaks = get_next_indice( st, 5 );
163 3867 : vq_peaks = max_peaks - vq_peaks;
164 3867 : *Npeaks = vq_peaks;
165 3867 : diff = 5;
166 :
167 : /* safety check in case of bit errors */
168 3867 : if ( *Npeaks < HVQ_MIN_PEAKS )
169 : {
170 0 : st->BER_detect = 1;
171 0 : vq_peaks = HVQ_MIN_PEAKS;
172 0 : *Npeaks = HVQ_MIN_PEAKS;
173 : }
174 :
175 : /* De-quantize peak positions */
176 1027323 : for ( i = 0; i < bin_th; i++ )
177 : {
178 1023456 : res_vec[i] = 0;
179 : }
180 :
181 : /* Unpack HVQ codewords */
182 3867 : pos_bits = hvq_dec_pos( st, res_vec, bin_th, vq_peaks );
183 3867 : diff += pos_bits;
184 :
185 989715 : for ( i = 0, j = 0; i < bin_th && j < vq_peaks; i++ ) /* safety check in case of bit errors */
186 : {
187 985848 : if ( res_vec[i] )
188 : {
189 66324 : vq_peak_idx[j++] = i;
190 : }
191 : }
192 :
193 : /* safety check in case of bit errors */
194 3867 : if ( j < vq_peaks )
195 : {
196 0 : st->BER_detect = 1;
197 0 : vq_peaks = j - 1;
198 0 : *Npeaks = j - 1;
199 : }
200 :
201 : /* Huffman or differential coding */
202 3867 : FlagN = get_next_indice( st, 1 );
203 :
204 : /* De-quantize peak gains */
205 3867 : pgain_difidx[0] = get_next_indice( st, GAIN0_BITS );
206 :
207 : /* safety check in case of bit errors */
208 3867 : if ( pgain_difidx[0] > 44 )
209 : {
210 0 : st->BER_detect = 1;
211 0 : pgain_difidx[0] = 44;
212 : }
213 3867 : peak_gains[0] = dicn_pg[pgain_difidx[0]] * sign( (float) res_vec[vq_peak_idx[0]] );
214 :
215 3867 : hcode_l = 0;
216 3867 : if ( FlagN )
217 : {
218 3801 : huff_dec( st, vq_peaks - 1, MAX_PG_HUFFLEN, NUM_PG_HUFFLEN, hvq_pg_huff_thres, hvq_pg_huff_offset, hvq_pg_huff_tab, &pgain_difidx[1] );
219 65298 : for ( i = 1; i < vq_peaks; i++ )
220 : {
221 61497 : hcode_l += pgain_huffsizn[pgain_difidx[i]];
222 : }
223 : }
224 : else
225 : {
226 1026 : for ( i = 1; i < vq_peaks; i++ )
227 : {
228 960 : pgain_difidx[i] = get_next_indice( st, GAINI_BITS );
229 960 : hcode_l += GAINI_BITS;
230 : }
231 : }
232 :
233 66324 : for ( i = 1; i < vq_peaks; i++ )
234 : {
235 62457 : pgain_difidx[i] += pgain_difidx[i - 1] - 15;
236 :
237 : /* safety check in case of bit errors */
238 62457 : if ( pgain_difidx[i] > 44 || pgain_difidx[i] < 0 )
239 : {
240 0 : st->BER_detect = 1;
241 0 : pgain_difidx[i] = 44;
242 : }
243 :
244 62457 : peak_gains[i] = dicn_pg[pgain_difidx[i]] * sign( (float) res_vec[vq_peak_idx[i]] );
245 : }
246 :
247 : /* Scale up peak gains and accumulate peak energy */
248 : /* Simulating BASOP code for interoperability */
249 3867 : manE_peak = 0;
250 3867 : expE_peak = 32;
251 70191 : for ( i = 0; i < vq_peaks; i++ )
252 : {
253 66324 : peak_gains[i] *= 4.0f;
254 66324 : manPkEnrg = manPkEnrg_tbl[pgain_difidx[i]];
255 66324 : expPkEnrg = expPkEnrg_tbl[pgain_difidx[i]];
256 66324 : floating_point_add( &manE_peak, &expE_peak, manPkEnrg, expPkEnrg );
257 : }
258 :
259 : /* Number of bits used for peak gain quantization */
260 3867 : diff += FLAGN_BITS + GAIN0_BITS + hcode_l;
261 :
262 : /* De-quantize peaks */
263 70191 : for ( i = 0; i < vq_peaks; i++ )
264 : {
265 66324 : dequant_peaks( st, &coefs_out[vq_peak_idx[i] - 2], &peak_gains[i] );
266 :
267 66324 : diff += 9;
268 : }
269 :
270 11601 : for ( i = 0; i < HVQ_NF_GROUPS; i++ )
271 : {
272 7734 : nf_gains_idx[i] = get_next_indice( st, 5 );
273 7734 : nf_gains[i] = 0.5f * dicn[nf_gains_idx[i]];
274 7734 : diff += 5;
275 : }
276 :
277 3867 : pvq_bits = num_bits - diff;
278 :
279 : /* Calculate number of PVQ bands to code and assign bits */
280 3867 : pvq_bands = hvq_pvq_bitalloc( pvq_bits, core_brate, st->bwidth, ynrm, manE_peak, expE_peak, Rk, R, sel_bnds, &n_sel_bnds );
281 :
282 : /* safety check in case of bit errors */
283 3867 : if ( ( pvq_bands == 0 ) && st->element_mode == EVS_MONO ) /* PVQ bands may be zero for IVAS */
284 : {
285 0 : st->BER_detect = 1;
286 : }
287 :
288 3867 : pvq_bits -= HVQ_PVQ_GAIN_BITS * pvq_bands;
289 :
290 : /* Get band limits for concatenated PVQ target */
291 3867 : hvq_concat_bands( pvq_bands, sel_bnds, n_sel_bnds, hvq_band_start, hvq_band_width, hvq_band_end );
292 :
293 3867 : s = 0;
294 11454 : for ( k = 0; k < pvq_bands; k++ )
295 : {
296 7587 : k_sort[k] = k;
297 7587 : Rk_f[k] = Rk[k] * 8;
298 : }
299 :
300 3867 : pvq_decode_frame( st, pvq_vector, npulses, pvq_inp_vector, hvq_band_start, hvq_band_end, hvq_band_width, pvq_bands, Rk_f, pvq_bits, core );
301 :
302 3867 : fine_gain_pred( hvq_band_start, hvq_band_end, hvq_band_width, k_sort, npulses, NULL, NULL, pvq_bands, pvq_vector, pvq_inp_vector, fg_pred, core );
303 :
304 3867 : fine_gain_dec( st, k_sort, pvq_bands, gain_bits_array, fg_pred );
305 :
306 3867 : apply_gain( k_sort, hvq_band_start, hvq_band_end, pvq_bands, fg_pred, pvq_vector );
307 :
308 3867 : i = 0;
309 3867 : n = 0;
310 3867 : s = 0;
311 11454 : for ( k = 0; k < pvq_bands; k++ )
312 : {
313 7587 : pvq_norm[k] = get_next_indice( st, HVQ_PVQ_GAIN_BITS );
314 7587 : pvq_norm[k] += 8;
315 :
316 7587 : diff += HVQ_PVQ_GAIN_BITS;
317 :
318 7587 : j = 0;
319 7587 : if ( k >= pvq_bands - n_sel_bnds )
320 : {
321 1047 : i = band_start_harm[sel_bnds[s++]];
322 : }
323 307623 : while ( j < hvq_band_width[k] )
324 : {
325 300036 : normq = dicn[pvq_norm[k]];
326 300036 : if ( coefs_out[i] == 0 )
327 : {
328 201504 : coefs_out[i] = pvq_vector[n] * normq;
329 201504 : j++;
330 201504 : n++;
331 : }
332 300036 : i++;
333 : }
334 : }
335 : /* Noise fill unqantized coeffs with one gain per group */
336 11601 : for ( i = 0; i < HVQ_NF_GROUPS; i++ )
337 : {
338 1031190 : for ( j = i * ( bin_th / HVQ_NF_GROUPS ); j < ( i + 1 ) * ( bin_th / HVQ_NF_GROUPS ); j++ )
339 : {
340 1023456 : if ( coefs_out[j] == 0 )
341 : {
342 594126 : coefs_out[j] = ( (float) own_random( &nf_seed ) / MAX16B ) * nf_gains[i];
343 : }
344 : }
345 : }
346 :
347 3867 : return;
348 : }
349 :
350 : /*--------------------------------------------------------------------------
351 : * dequant_peaks()
352 : *
353 : * Reads codebook vector and scales peak
354 : *--------------------------------------------------------------------------*/
355 :
356 66324 : static void dequant_peaks(
357 : Decoder_State *st, /* i/o: decoder state structure */
358 : float *vect_out, /* o : Quantized vector */
359 : const float *peak_gain /* i : Peak gain */
360 : )
361 : {
362 : float xq[4];
363 : const float *tmp;
364 : int16_t i, hvq_cb_rev;
365 : int16_t cb_idx;
366 :
367 66324 : hvq_cb_rev = get_next_indice( st, 1 );
368 66324 : cb_idx = get_next_indice( st, 8 );
369 :
370 66324 : if ( hvq_cb_rev )
371 : {
372 32799 : tmp = &hvq_peak_cb[cb_idx * 4 + 3];
373 163995 : for ( i = 0; i < 4; i++ )
374 : {
375 131196 : xq[i] = tmp[-i];
376 : }
377 : }
378 : else
379 : {
380 33525 : mvr2r( &hvq_peak_cb[cb_idx * 4], xq, 4 );
381 : }
382 66324 : if ( vect_out[0] == 0 )
383 : {
384 53901 : vect_out[0] = xq[0] * *peak_gain;
385 53901 : vect_out[1] = xq[1] * *peak_gain;
386 : }
387 : else
388 : {
389 12423 : if ( fabs( peak_gain[-1] ) <= fabs( *peak_gain ) )
390 : {
391 7896 : vect_out[0] = xq[0] * *peak_gain;
392 7896 : vect_out[1] = xq[1] * *peak_gain;
393 : }
394 : else
395 : {
396 4527 : if ( vect_out[1] == 0 || fabs( peak_gain[-1] ) <= fabs( *peak_gain ) )
397 : {
398 1881 : vect_out[1] = xq[1] * *peak_gain;
399 : }
400 : }
401 : }
402 66324 : vect_out[2] = *peak_gain;
403 66324 : vect_out[3] = xq[2] * *peak_gain;
404 66324 : vect_out[4] = xq[3] * *peak_gain;
405 :
406 66324 : return;
407 : }
408 :
409 : /*--------------------------------------------------------------------------
410 : * hvq_dec_pos()
411 : *
412 : * HVQ decode peak positions
413 : *--------------------------------------------------------------------------*/
414 :
415 3867 : static int16_t hvq_dec_pos(
416 : Decoder_State *st, /* i/o: decoder state structure */
417 : int16_t *pos_vec, /* o : decoded peak positions */
418 : const int16_t length, /* i : length */
419 : const int16_t num_peaks /* i : number of peaks */
420 : )
421 : {
422 : int16_t peak_idx[HVQ_MAX_PEAKS];
423 : int16_t delta[HVQ_MAX_PEAKS];
424 : int16_t sign_vec[HVQ_MAX_PEAKS];
425 :
426 : int16_t mode;
427 : int16_t num_bits;
428 : int16_t i, j;
429 :
430 3867 : num_bits = 0;
431 3867 : set_s( pos_vec, 0, length );
432 :
433 3867 : mode = get_next_indice( st, 1 );
434 3867 : num_bits += 1;
435 :
436 3867 : if ( mode == HVQ_CP_DELTA )
437 : {
438 3372 : huff_dec( st, num_peaks, HVQ_CP_HUFF_MAX_CODE, HVQ_CP_HUFF_NUM_LEN, hvq_cp_huff_thres, hvq_cp_huff_offset, hvq_cp_huff_tab, delta );
439 :
440 62106 : for ( i = 0; i < num_peaks; i++ )
441 : {
442 58734 : num_bits += hvq_cp_huff_len[delta[i]];
443 : }
444 :
445 3372 : peak_idx[0] = delta[0] - HVQ_CP_HUFF_OFFSET;
446 : /* safety check in case of bit errors */
447 3372 : if ( peak_idx[0] < 2 )
448 : {
449 0 : peak_idx[0] = 2;
450 0 : st->BER_detect = 1;
451 : }
452 58734 : for ( i = 1; i < num_peaks; i++ )
453 : {
454 55362 : peak_idx[i] = delta[i] + peak_idx[i - 1] + HVQ_CP_HUFF_OFFSET;
455 : /* safety check in case of bit errors */
456 55362 : if ( peak_idx[i] >= HVQ_THRES_BIN_32k )
457 : {
458 0 : peak_idx[i] = HVQ_THRES_BIN_32k - 1;
459 0 : st->BER_detect = 1;
460 : }
461 : }
462 :
463 62106 : for ( i = 0; i < num_peaks; i++ )
464 : {
465 58734 : pos_vec[peak_idx[i]] = 1;
466 : }
467 : }
468 : else
469 : {
470 495 : num_bits += sparse_dec_pos( st, pos_vec, length );
471 : }
472 :
473 70191 : for ( i = 0; i < num_peaks; i++ )
474 : {
475 66324 : sign_vec[i] = ( get_next_indice_1( st ) == 0 ) ? -1 : 1;
476 : }
477 3867 : num_bits += num_peaks;
478 :
479 989715 : for ( i = 0, j = 0; i < length && j < num_peaks; i++ ) /* safety check in case of bit errors */
480 : {
481 985848 : if ( pos_vec[i] )
482 : {
483 66324 : pos_vec[i] *= sign_vec[j++];
484 : }
485 : }
486 :
487 3867 : return num_bits;
488 : }
489 :
490 : /*--------------------------------------------------------------------------
491 : * sparse_dec_pos()
492 : *
493 : * Sparse decode positions
494 : *--------------------------------------------------------------------------*/
495 :
496 : /*! r: number of bits decoded */
497 495 : static int16_t sparse_dec_pos(
498 : Decoder_State *st, /* i/o: decoder state structure */
499 : int16_t *out, /* o : decoded peak positions */
500 : const int16_t length /* i : length */
501 : )
502 : {
503 : int16_t layer2[HVQ_CP_L2_MAX];
504 : int16_t layer_length;
505 : int16_t i, j;
506 : int16_t bits;
507 : int16_t idx, val;
508 :
509 495 : set_s( layer2, 0, HVQ_CP_L2_MAX );
510 495 : set_s( out, 0, length );
511 495 : bits = 0;
512 :
513 495 : layer_length = (int16_t) ( (float) length / HVQ_CP_L1_LEN + 0.5 );
514 :
515 24138 : for ( i = 0; i < layer_length; i++ )
516 : {
517 23643 : layer2[i] = get_next_indice_1( st );
518 : }
519 495 : bits += layer_length;
520 :
521 24138 : for ( j = 0; j < layer_length; j++ )
522 : {
523 23643 : if ( layer2[j] )
524 : {
525 6975 : idx = get_next_indice( st, HVQ_CP_MAP_IDX_LEN );
526 6975 : bits += HVQ_CP_MAP_IDX_LEN;
527 :
528 6975 : val = hvq_cp_layer1_map5[idx];
529 :
530 : /* safety check in case of bit errors */
531 6975 : if ( j == 0 && val > 4 ) /* out[0] and out[1] are invalid positions */
532 : {
533 0 : st->BER_detect = 1;
534 0 : val = 4;
535 : }
536 41823 : for ( i = min( ( j + 1 ) * HVQ_CP_L1_LEN, length ) - 1; i >= j * HVQ_CP_L1_LEN; i-- )
537 : {
538 34848 : out[i] = val & 1;
539 34848 : val >>= 1;
540 : }
541 : }
542 : }
543 :
544 495 : return bits;
545 : }
|