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 : * hq_pred_hb_bws()
49 : *
50 : * HQ core HB band-width switching handling
51 : *--------------------------------------------------------------------------*/
52 :
53 24705 : static void hq_pred_hb_bws(
54 : Decoder_State *st, /* i/o: decoder state structure */
55 : const int16_t *ynrm, /* i : norm quantization index vector */
56 : const int16_t length, /* i : frame length */
57 : const int16_t hqswb_clas, /* i : HQ SWB class */
58 : const float *SWB_fenv /* i : SWB frequency envelopes */
59 : )
60 : {
61 : int16_t i;
62 :
63 : /* SWB switching to WB */
64 24705 : if ( length >= L_FRAME32k ) /* wb switch to swb */
65 : {
66 : /* calculate the switching parameters */
67 22803 : if ( ( hqswb_clas != HQ_GEN_SWB && st->core_brate <= HQ_32k ) || st->core_brate > HQ_32k )
68 : {
69 20565 : st->prev_ener_shb = 0.0f;
70 143955 : for ( i = 25; i < SFM_N_HARM; i++ )
71 : {
72 123390 : st->prev_ener_shb += dicn[ynrm[i]];
73 : }
74 20565 : st->prev_ener_shb /= 6;
75 : }
76 : else
77 : {
78 2238 : st->prev_ener_shb = 0.0f;
79 26856 : for ( i = 0; i < SWB_FENV - 3; i++ )
80 : {
81 24618 : st->prev_ener_shb += SWB_fenv[i];
82 : }
83 2238 : st->prev_ener_shb /= ( SWB_FENV - 3 );
84 : }
85 : }
86 :
87 24705 : if ( st->last_inner_frame >= L_FRAME32k && st->hBWE_FD != NULL )
88 : {
89 22803 : set_f( st->hBWE_FD->prev_SWB_fenv, st->prev_ener_shb, SWB_FENV );
90 : }
91 :
92 24705 : return;
93 : }
94 :
95 :
96 : /*--------------------------------------------------------------------------*
97 : * hq_hr_dec()
98 : *
99 : * HQ high rate decoding routine
100 : *--------------------------------------------------------------------------*/
101 :
102 24705 : void hq_hr_dec(
103 : Decoder_State *st, /* i/o: decoder state structure */
104 : float *t_audio_q, /* o : transform-domain coefficients */
105 : const int16_t length, /* i : frame length */
106 : const int16_t num_bits, /* i : number of available bits */
107 : int16_t *ynrm, /* o : norm quantization index vector */
108 : int16_t *is_transient, /* o : transient flag */
109 : int16_t *hqswb_clas, /* o : HQ SWB class */
110 : float *SWB_fenv, /* o : SWB frequency envelopes */
111 : const int16_t core_switching_flag /* i : Core switching flag */
112 : )
113 : {
114 : int16_t nb_sfm;
115 : int16_t sum, hcode_l;
116 : int16_t sfmsize[NB_SFM], sfm_start[NB_SFM], sfm_end[NB_SFM];
117 : int16_t num_sfm, numnrmibits;
118 : int16_t nf_idx;
119 : int16_t normqlg2[NB_SFM], R[NB_SFM];
120 : int16_t pulses[NB_SFM], maxpulse[NB_SFM];
121 : float env_stab;
122 : int16_t Rsubband[NB_SFM]; /*Q3*/
123 24705 : int16_t start_norm, Npeaks = 0;
124 : float noise_level[HVQ_BWE_NOISE_BANDS];
125 : int16_t peak_idx[HVQ_MAX_PEAKS];
126 : int16_t hq_generic_offset;
127 : int16_t num_env_bands;
128 24705 : int16_t hq_generic_exc_clas = 0;
129 : int16_t core_sfm;
130 : int16_t har_freq_est1, har_freq_est2;
131 : int16_t flag_dis;
132 : const int16_t *subband_search_offset;
133 : int16_t wBands[2];
134 : int16_t b_delta_env;
135 : int16_t n_band;
136 : int16_t bits_left;
137 :
138 24705 : HQ_DEC_HANDLE hHQ_core = st->hHQ_core;
139 24705 : bits_left = num_bits;
140 :
141 : /*------------------------------------------------------------------*
142 : * Initializations
143 : *------------------------------------------------------------------*/
144 24705 : set_s( pulses, 0, NB_SFM );
145 24705 : set_s( maxpulse, 0, NB_SFM );
146 24705 : flag_dis = 1;
147 24705 : har_freq_est1 = 0;
148 24705 : har_freq_est2 = 0;
149 24705 : set_s( peak_idx, 0, HVQ_MAX_PEAKS_32k );
150 :
151 : /*------------------------------------------------------------------*
152 : * Decode classification
153 : *------------------------------------------------------------------*/
154 24705 : bits_left -= hq_classifier_dec( st, st->core_brate, length, is_transient, hqswb_clas );
155 :
156 : /*------------------------------------------------------------------*
157 : * set quantization parameters
158 : *------------------------------------------------------------------*/
159 :
160 24705 : hq_configure( length, *hqswb_clas, st->core_brate, &num_sfm, &nb_sfm, &start_norm, &num_env_bands, &numnrmibits, &hq_generic_offset, sfmsize, sfm_start, sfm_end );
161 :
162 : /*------------------------------------------------------------------*
163 : * Unpacking bitstream
164 : *------------------------------------------------------------------*/
165 :
166 24705 : nf_idx = 0;
167 24705 : if ( !*is_transient && *hqswb_clas != HQ_HVQ && !( length == L_FRAME16k && st->core_brate <= HQ_32k ) )
168 : {
169 17811 : nf_idx = get_next_indice( st, 2 );
170 : }
171 :
172 :
173 : /*------------------------------------------------------------------*
174 : * Decode envelope
175 : *------------------------------------------------------------------*/
176 :
177 24705 : hcode_l = decode_envelope_indices( st, start_norm, num_env_bands, numnrmibits, ynrm, NORMAL_HQ_CORE, *is_transient );
178 24705 : bits_left -= hcode_l + NORM0_BITS + FLAGS_BITS;
179 :
180 24705 : dequantize_norms( st, start_norm, num_env_bands, *is_transient, ynrm, normqlg2 );
181 :
182 24705 : if ( *hqswb_clas == HQ_GEN_SWB || *hqswb_clas == HQ_GEN_FB )
183 : {
184 9003 : hq_generic_exc_clas = swb_bwe_gain_deq( st, HQ_CORE, NULL, SWB_fenv, st->core_brate >= HQ_32k, *hqswb_clas ); /* Use (st->core_brate >= HQ_32k) to be consistent with hq_configure */
185 9003 : if ( hq_generic_exc_clas == HQ_GENERIC_SP_EXC )
186 : {
187 426 : bits_left++; /* conditional 1 bit saving for representing FD3 BWE excitation class */
188 : }
189 9003 : map_hq_generic_fenv_norm( *hqswb_clas, SWB_fenv, ynrm, normqlg2, num_env_bands, nb_sfm, hq_generic_offset );
190 : }
191 :
192 24705 : env_stab = 0;
193 24705 : if ( *hqswb_clas == HQ_HVQ )
194 : {
195 3867 : hHQ_core->mem_env_delta = 0;
196 : }
197 20838 : else if ( length == L_FRAME32k || ( core_switching_flag && ( st->element_mode != EVS_MONO ) && length == L_SPEC32k_EXT ) )
198 : {
199 4227 : env_stab = env_stability( ynrm, SFM_N_ENV_STAB, hHQ_core->mem_norm, &hHQ_core->mem_env_delta, core_switching_flag && ( st->element_mode != EVS_MONO ) );
200 : }
201 : else
202 : {
203 16611 : hHQ_core->mem_norm[0] = 31;
204 16611 : hHQ_core->mem_env_delta = 0;
205 : }
206 :
207 24705 : if ( *hqswb_clas == HQ_HVQ )
208 : {
209 3867 : hHQ_core->env_stab = 1.0f; /* stable by definition */
210 : }
211 : else
212 : {
213 20838 : if ( length == L_FRAME32k || ( core_switching_flag && ( st->element_mode != EVS_MONO ) && length == L_SPEC32k_EXT ) )
214 : {
215 4227 : hHQ_core->env_stab = env_stab; /* calculated stability */
216 : }
217 : else
218 : {
219 16611 : hHQ_core->env_stab = env_stability( ynrm, SFM_N_ENV_STAB_WB, hHQ_core->mem_norm_hqfec, &hHQ_core->mem_env_delta_hqfec, core_switching_flag && ( st->element_mode != EVS_MONO ) );
220 : }
221 : }
222 24705 : hHQ_core->env_stab_plc = env_stab_smo( min( hHQ_core->env_stab, 1.0f - stab_trans[L_STAB_TBL - 1] ), hHQ_core->env_stab_state_p, &hHQ_core->envstabplc_hocnt );
223 :
224 : /*------------------------------------------------------------------*
225 : * Bit allocation
226 : *------------------------------------------------------------------*/
227 :
228 24705 : hq_bit_allocation( st->core_brate, length, *hqswb_clas, &bits_left, normqlg2, nb_sfm, sfmsize, noise_level, R, Rsubband, &sum, &core_sfm, num_env_bands );
229 :
230 24705 : if ( st->bws_cnt1 > 0 && *hqswb_clas == HQ_GEN_SWB && st->core_brate < HQ_32k )
231 : {
232 0 : if ( st->L_frame == L_FRAME16k )
233 : {
234 0 : for ( n_band = 0; n_band < 4; n_band++ )
235 : {
236 0 : SWB_fenv[n_band] *= (float) st->bws_cnt1 / (float) N_NS2W_FRAMES;
237 : }
238 : }
239 :
240 0 : for ( n_band = 4; n_band < SWB_FENV; n_band++ )
241 : {
242 0 : SWB_fenv[n_band] *= (float) st->bws_cnt1 / (float) N_NS2W_FRAMES;
243 : }
244 : }
245 :
246 24705 : if ( *hqswb_clas == HQ_GEN_SWB || *hqswb_clas == HQ_GEN_FB )
247 : {
248 9003 : b_delta_env = get_nor_delta_hf( st, ynrm, Rsubband, num_env_bands, nb_sfm, core_sfm );
249 9003 : sum -= b_delta_env;
250 : }
251 :
252 : /*------------------------------------------------------------------*
253 : * Decode spectral fine structure using HVQ/PVQ
254 : *------------------------------------------------------------------*/
255 :
256 24705 : if ( *hqswb_clas == HQ_HVQ )
257 : {
258 3867 : hvq_dec( st, bits_left, st->core_brate, ynrm, R, noise_level, peak_idx, &Npeaks, t_audio_q, st->core );
259 : }
260 : else
261 : {
262 20838 : pvq_core_dec( st, sfm_start, sfm_end, sfmsize, t_audio_q, sum, nb_sfm, Rsubband, R, pulses, maxpulse, HQ_CORE );
263 : }
264 :
265 24705 : if ( *hqswb_clas == HQ_HVQ || *hqswb_clas == HQ_HARMONIC )
266 : {
267 6219 : subband_search_offset = subband_search_offsets_13p2kbps_Har;
268 6219 : wBands[0] = SWB_SB_BW_LEN0_16KBPS_HAR;
269 6219 : wBands[1] = SWB_SB_BW_LEN1_16KBPS_HAR;
270 :
271 6219 : har_est( t_audio_q, 300, &har_freq_est1, &har_freq_est2, &flag_dis, &hHQ_core->prev_frm_hfe2, subband_search_offset, wBands, &hHQ_core->prev_stab_hfe2 );
272 :
273 6219 : hHQ_core->prev_frm_hfe2 = har_freq_est2;
274 : }
275 :
276 : /* reset LR-HQ memories */
277 24705 : hHQ_core->prev_frm_hfe2 = 0;
278 24705 : hHQ_core->prev_stab_hfe2 = 0;
279 :
280 : /*------------------------------------------------------------------*
281 : * Spectral filling
282 : *------------------------------------------------------------------*/
283 :
284 24705 : fill_spectrum( t_audio_q, R, *is_transient, ynrm, SWB_fenv, hq_generic_offset, nf_idx, length, env_stab, &hHQ_core->no_att_hangover, &hHQ_core->energy_lt, &hHQ_core->hq_generic_seed, hq_generic_exc_clas, core_sfm, *hqswb_clas, noise_level, st->core_brate, hHQ_core->prev_noise_level, &( hHQ_core->prev_R ), hHQ_core->prev_coeff_out, peak_idx, Npeaks, pulses, hHQ_core->old_is_transient[0], hHQ_core->prev_normq, hHQ_core->prev_env, st->prev_bfi, sfmsize, sfm_start, sfm_end, &st->hBWE_FD->prev_L_swb_norm, hHQ_core->prev_hqswb_clas, num_sfm, num_env_bands, st->element_mode );
285 :
286 24705 : enforce_zero_for_min_envelope( *hqswb_clas, ynrm, t_audio_q, nb_sfm, sfm_start, sfm_end );
287 :
288 24705 : if ( *is_transient )
289 : {
290 1617 : de_interleave_spectrum( t_audio_q, length );
291 : }
292 :
293 : /*------------------------------------------------------------------*
294 : * WB/SWB bandwidth switching
295 : *------------------------------------------------------------------*/
296 :
297 24705 : hq_pred_hb_bws( st, ynrm, length, *hqswb_clas, SWB_fenv );
298 :
299 : /* update */
300 24705 : hHQ_core->prev_hqswb_clas = *hqswb_clas;
301 :
302 24705 : return;
303 : }
|