Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2026 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 43493 : 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 43493 : if ( length >= L_FRAME32k ) /* wb switch to swb */
65 : {
66 : /* calculate the switching parameters */
67 38801 : if ( ( hqswb_clas != HQ_GEN_SWB && st->core_brate <= HQ_32k ) || st->core_brate > HQ_32k )
68 : {
69 33882 : st->prev_ener_shb = 0.0f;
70 237174 : for ( i = 25; i < SFM_N_HARM; i++ )
71 : {
72 203292 : st->prev_ener_shb += dicn[ynrm[i]];
73 : }
74 33882 : st->prev_ener_shb /= 6;
75 : }
76 : else
77 : {
78 4919 : st->prev_ener_shb = 0.0f;
79 59028 : for ( i = 0; i < SWB_FENV - 3; i++ )
80 : {
81 54109 : st->prev_ener_shb += SWB_fenv[i];
82 : }
83 4919 : st->prev_ener_shb /= ( SWB_FENV - 3 );
84 : }
85 : }
86 :
87 43493 : if ( st->last_inner_frame >= L_FRAME32k && st->hBWE_FD != NULL )
88 : {
89 38717 : set_f( st->hBWE_FD->prev_SWB_fenv, st->prev_ener_shb, SWB_FENV );
90 : }
91 :
92 43493 : return;
93 : }
94 :
95 :
96 : /*--------------------------------------------------------------------------*
97 : * hq_hr_dec()
98 : *
99 : * HQ high rate decoding routine
100 : *--------------------------------------------------------------------------*/
101 :
102 43493 : 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 43493 : 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 43493 : 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 43493 : HQ_DEC_HANDLE hHQ_core = st->hHQ_core;
139 43493 : bits_left = num_bits;
140 43493 : hq_generic_offset = 0;
141 :
142 : /*------------------------------------------------------------------*
143 : * Initializations
144 : *------------------------------------------------------------------*/
145 43493 : set_s( pulses, 0, NB_SFM );
146 43493 : set_s( maxpulse, 0, NB_SFM );
147 43493 : flag_dis = 1;
148 43493 : har_freq_est1 = 0;
149 43493 : har_freq_est2 = 0;
150 43493 : set_s( peak_idx, 0, HVQ_MAX_PEAKS_32k );
151 :
152 : /*------------------------------------------------------------------*
153 : * Decode classification
154 : *------------------------------------------------------------------*/
155 43493 : bits_left -= hq_classifier_dec( st, st->core_brate, length, is_transient, hqswb_clas );
156 :
157 : /*------------------------------------------------------------------*
158 : * set quantization parameters
159 : *------------------------------------------------------------------*/
160 :
161 43493 : 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 );
162 :
163 : /*------------------------------------------------------------------*
164 : * Unpacking bitstream
165 : *------------------------------------------------------------------*/
166 :
167 43493 : nf_idx = 0;
168 43493 : if ( !*is_transient && *hqswb_clas != HQ_HVQ && !( length == L_FRAME16k && st->core_brate <= HQ_32k ) )
169 : {
170 31497 : nf_idx = get_next_indice( st, 2 );
171 : }
172 :
173 :
174 : /*------------------------------------------------------------------*
175 : * Decode envelope
176 : *------------------------------------------------------------------*/
177 :
178 43493 : hcode_l = decode_envelope_indices( st, start_norm, num_env_bands, numnrmibits, ynrm, NORMAL_HQ_CORE, *is_transient );
179 43493 : bits_left -= hcode_l + NORM0_BITS + FLAGS_BITS;
180 :
181 43493 : dequantize_norms( st, start_norm, num_env_bands, *is_transient, ynrm, normqlg2 );
182 :
183 43493 : if ( *hqswb_clas == HQ_GEN_SWB || *hqswb_clas == HQ_GEN_FB )
184 : {
185 16840 : 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 */
186 16840 : if ( hq_generic_exc_clas == HQ_GENERIC_SP_EXC )
187 : {
188 1584 : bits_left++; /* conditional 1 bit saving for representing FD3 BWE excitation class */
189 : }
190 16840 : map_hq_generic_fenv_norm( *hqswb_clas, SWB_fenv, ynrm, normqlg2, num_env_bands, nb_sfm, hq_generic_offset );
191 : }
192 :
193 43493 : env_stab = 0;
194 43493 : if ( *hqswb_clas == HQ_HVQ )
195 : {
196 4963 : hHQ_core->mem_env_delta = 0;
197 : }
198 38530 : else if ( length == L_FRAME32k || ( core_switching_flag && ( st->element_mode != EVS_MONO ) && length == L_SPEC32k_EXT ) )
199 : {
200 10098 : 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 ) );
201 : }
202 : else
203 : {
204 28432 : hHQ_core->mem_norm[0] = 31;
205 28432 : hHQ_core->mem_env_delta = 0;
206 : }
207 :
208 43493 : if ( *hqswb_clas == HQ_HVQ )
209 : {
210 4963 : hHQ_core->env_stab = 1.0f; /* stable by definition */
211 : }
212 : else
213 : {
214 38530 : if ( length == L_FRAME32k || ( core_switching_flag && ( st->element_mode != EVS_MONO ) && length == L_SPEC32k_EXT ) )
215 : {
216 10098 : hHQ_core->env_stab = env_stab; /* calculated stability */
217 : }
218 : else
219 : {
220 28432 : 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 ) );
221 : }
222 : }
223 43493 : 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 );
224 :
225 : /*------------------------------------------------------------------*
226 : * Bit allocation
227 : *------------------------------------------------------------------*/
228 :
229 43493 : 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 );
230 :
231 43493 : if ( st->bws_cnt1 > 0 && *hqswb_clas == HQ_GEN_SWB && st->core_brate < HQ_32k )
232 : {
233 0 : if ( st->L_frame == L_FRAME16k )
234 : {
235 0 : for ( n_band = 0; n_band < 4; n_band++ )
236 : {
237 0 : SWB_fenv[n_band] *= (float) st->bws_cnt1 / (float) N_NS2W_FRAMES;
238 : }
239 : }
240 :
241 0 : for ( n_band = 4; n_band < SWB_FENV; n_band++ )
242 : {
243 0 : SWB_fenv[n_band] *= (float) st->bws_cnt1 / (float) N_NS2W_FRAMES;
244 : }
245 : }
246 :
247 43493 : if ( *hqswb_clas == HQ_GEN_SWB || *hqswb_clas == HQ_GEN_FB )
248 : {
249 16840 : b_delta_env = get_nor_delta_hf( st, ynrm, Rsubband, num_env_bands, nb_sfm, core_sfm );
250 16840 : sum -= b_delta_env;
251 : }
252 :
253 : /*------------------------------------------------------------------*
254 : * Decode spectral fine structure using HVQ/PVQ
255 : *------------------------------------------------------------------*/
256 :
257 43493 : if ( *hqswb_clas == HQ_HVQ )
258 : {
259 4963 : hvq_dec( st, bits_left, st->core_brate, ynrm, R, noise_level, peak_idx, &Npeaks, t_audio_q, st->core );
260 : }
261 : else
262 : {
263 38530 : pvq_core_dec( st, sfm_start, sfm_end, sfmsize, t_audio_q, sum, nb_sfm, Rsubband, R, pulses, maxpulse, HQ_CORE );
264 : }
265 :
266 43493 : if ( *hqswb_clas == HQ_HVQ || *hqswb_clas == HQ_HARMONIC )
267 : {
268 10451 : subband_search_offset = subband_search_offsets_13p2kbps_Har;
269 10451 : wBands[0] = SWB_SB_BW_LEN0_16KBPS_HAR;
270 10451 : wBands[1] = SWB_SB_BW_LEN1_16KBPS_HAR;
271 :
272 10451 : 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 );
273 :
274 10451 : hHQ_core->prev_frm_hfe2 = har_freq_est2;
275 : }
276 :
277 : /* reset LR-HQ memories */
278 43493 : hHQ_core->prev_frm_hfe2 = 0;
279 43493 : hHQ_core->prev_stab_hfe2 = 0;
280 :
281 : /*------------------------------------------------------------------*
282 : * Spectral filling
283 : *------------------------------------------------------------------*/
284 :
285 43493 : 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 );
286 :
287 43493 : enforce_zero_for_min_envelope( *hqswb_clas, ynrm, t_audio_q, nb_sfm, sfm_start, sfm_end );
288 :
289 43493 : if ( *is_transient )
290 : {
291 2918 : de_interleave_spectrum( t_audio_q, length );
292 : }
293 :
294 : /*------------------------------------------------------------------*
295 : * WB/SWB bandwidth switching
296 : *------------------------------------------------------------------*/
297 :
298 43493 : hq_pred_hb_bws( st, ynrm, length, *hqswb_clas, SWB_fenv );
299 :
300 : /* update */
301 43493 : hHQ_core->prev_hqswb_clas = *hqswb_clas;
302 :
303 43493 : return;
304 : }
|