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 : * fill_spectrum()
49 : *
50 : * Apply spectral filling by
51 : * - filling zero-bit bands below BWE region
52 : * - applying BWE above transition frequency
53 : *--------------------------------------------------------------------------*/
54 :
55 24705 : void fill_spectrum(
56 : float *coeff, /* i/o: normalized MLT spectrum / nf spectrum */
57 : int16_t *R, /* i : number of pulses per band */
58 : const int16_t is_transient, /* i : transient flag */
59 : int16_t norm[], /* i : quantization indices for norms */
60 : const float *hq_generic_fenv, /* i : HQ GENERIC envelope */
61 : const int16_t hq_generic_offset, /* i : HQ GENERIC offset */
62 : const int16_t nf_idx, /* i : noise fill index */
63 : const int16_t length, /* i : Length of spectrum (32 or 48 kHz) */
64 : const float env_stab, /* i : Envelope stability measure [0..1] */
65 : int16_t *no_att_hangover, /* i/o: Frame counter for attenuation hangover */
66 : float *energy_lt, /* i/o: Long-term energy measure for transient detection */
67 : int16_t *bwe_seed, /* i/o: random seed for generating BWE input */
68 : const int16_t hq_generic_exc_clas, /* i : HQ generic hf excitation class */
69 : const int16_t core_sfm, /* i : index of the end band for core */
70 : int16_t HQ_mode, /* i : HQ mode */
71 : float noise_level[], /* i : noise levels for harmonic modes */
72 : int32_t core_brate, /* i : target bitrate */
73 : float prev_noise_level[], /* i/o: noise factor in previous frame */
74 : int16_t *prev_R, /* i/o: bit allocation info. in previous frame */
75 : float *prev_coeff_out, /* i/o: decoded spectrum in previous frame */
76 : const int16_t *peak_idx, /* i : HVQ peak indices */
77 : const int16_t Npeaks, /* i : Number of HVQ peaks */
78 : const int16_t *npulses, /* i : Number of assigned pulses per band */
79 : int16_t prev_is_transient, /* i : previous transient flag */
80 : float *prev_normq, /* i : previous norms */
81 : float *prev_env, /* i : previous noise envelopes */
82 : int16_t prev_bfi, /* i : previous bad frame indicator */
83 : const int16_t *sfmsize, /* i : Length of bands */
84 : const int16_t *sfm_start, /* i : Start of bands */
85 : const int16_t *sfm_end, /* i : End of bands */
86 : int16_t *prev_L_swb_norm, /* i/o: last normalize length for harmonic mode */
87 : int16_t prev_hq_mode, /* i : previous HQ mode */
88 : const int16_t num_sfm, /* i : Number of bands */
89 : const int16_t num_env_bands, /* i : Number of envelope bands */
90 : const int16_t element_mode /* i : element mode */
91 : )
92 : {
93 : float CodeBook[L_SPEC48k_EXT];
94 24705 : int16_t cb_size = 0;
95 : int16_t last_sfm;
96 : float CodeBook_mod[L_SPEC48k_EXT];
97 : float norm_adj[NB_SFM];
98 24705 : int16_t high_sfm = 23;
99 : int16_t flag_32K_env_hangover;
100 24705 : int16_t bin_th = 0;
101 : int16_t peak_pos[L_HARMONIC_EXC];
102 : int16_t bwe_peaks[L_FRAME48k];
103 : float normq_v[NB_SFM];
104 : float coeff_fine[L_SPEC48k_EXT];
105 : float coeff_out[L_SPEC48k_EXT];
106 :
107 24705 : set_s( peak_pos, 0, L_HARMONIC_EXC );
108 24705 : set_s( bwe_peaks, 0, L_FRAME48k );
109 24705 : set_f( norm_adj, 1.0f, num_sfm );
110 24705 : set_f( coeff_out, 0.0f, L_FRAME48k );
111 :
112 24705 : if ( HQ_mode == HQ_TRANSIENT )
113 : {
114 1617 : last_sfm = num_sfm - 1;
115 : }
116 23088 : else if ( HQ_mode == HQ_GEN_SWB || HQ_mode == HQ_GEN_FB )
117 : {
118 9003 : last_sfm = max( core_sfm, num_env_bands - 1 );
119 : }
120 : else
121 : {
122 14085 : last_sfm = core_sfm;
123 : }
124 :
125 24705 : if ( HQ_mode == HQ_HARMONIC )
126 : {
127 2352 : high_sfm = ( core_brate < HQ_BWE_CROSSOVER_BRATE ) ? HVQ_THRES_SFM_24k - 1 : HVQ_THRES_SFM_32k - 1;
128 :
129 2352 : if ( last_sfm < high_sfm )
130 : {
131 240 : last_sfm = high_sfm;
132 : }
133 : }
134 22353 : else if ( HQ_mode == HQ_HVQ )
135 : {
136 3867 : bin_th = sfm_end[last_sfm];
137 : }
138 :
139 : /* Transient analysis for envelope stability measure */
140 24705 : if ( length == L_FRAME32k )
141 : {
142 4581 : env_stab_transient_detect( is_transient, length, norm, no_att_hangover, energy_lt, HQ_mode, bin_th, coeff );
143 : }
144 :
145 24705 : if ( length == L_FRAME16k || ( ( length == L_FRAME32k && HQ_mode != HQ_HARMONIC && HQ_mode != HQ_HVQ ) && *no_att_hangover == 0 ) )
146 : {
147 : /* Norm adjustment function */
148 5502 : env_adj( npulses, length, last_sfm, norm_adj, env_stab, sfmsize );
149 : }
150 :
151 24705 : flag_32K_env_hangover = ( length == L_FRAME32k && ( ( env_stab < 0.5f && *no_att_hangover == 0 ) || HQ_mode == HQ_HVQ ) );
152 :
153 : /*----------------------------------------------------------------*
154 : * Build noise-fill codebook
155 : *----------------------------------------------------------------*/
156 :
157 24705 : if ( HQ_mode != HQ_HVQ )
158 : {
159 20838 : cb_size = build_nf_codebook( flag_32K_env_hangover, coeff, sfm_start, sfmsize, sfm_end, last_sfm, R, CodeBook, CodeBook_mod );
160 : }
161 :
162 : /*----------------------------------------------------------------*
163 : * Prepare fine structure for Harmonic and HVQ
164 : *----------------------------------------------------------------*/
165 :
166 24705 : if ( HQ_mode == HQ_HARMONIC )
167 : {
168 2352 : harm_bwe_fine( R, last_sfm, high_sfm, num_sfm, norm, sfm_start, sfm_end, prev_L_swb_norm, coeff, coeff_out, coeff_fine );
169 : }
170 22353 : else if ( HQ_mode == HQ_HVQ )
171 : {
172 3867 : hvq_bwe_fine( last_sfm, num_sfm, sfm_end, peak_idx, Npeaks, peak_pos, prev_L_swb_norm, coeff, bwe_peaks, coeff_fine );
173 : }
174 :
175 : /*----------------------------------------------------------------*
176 : * Apply noise-fill
177 : *----------------------------------------------------------------*/
178 :
179 24705 : if ( HQ_mode != HQ_HVQ && cb_size > 0 )
180 : {
181 20838 : apply_noisefill_HQ( R, length, flag_32K_env_hangover, core_brate, last_sfm, CodeBook, CodeBook_mod, cb_size, sfm_start, sfm_end, sfmsize, coeff );
182 : }
183 :
184 : /*----------------------------------------------------------------*
185 : * Normal mode BWE
186 : *----------------------------------------------------------------*/
187 :
188 24705 : if ( HQ_mode == HQ_NORMAL )
189 : {
190 7866 : hq_fold_bwe( last_sfm, sfm_end, num_sfm, coeff );
191 : }
192 :
193 : /*----------------------------------------------------------------*
194 : * Apply noise-fill adjustment
195 : *----------------------------------------------------------------*/
196 :
197 24705 : if ( ( length >= L_FRAME32k || core_brate > HQ_32k || core_brate < HQ_24k40 ) && HQ_mode != HQ_HVQ )
198 : {
199 20580 : apply_nf_gain( nf_idx, last_sfm, R, sfm_start, sfm_end, coeff );
200 : }
201 :
202 : /*----------------------------------------------------------------*
203 : * Prepare fine strucutre for HQ GENERIC
204 : *----------------------------------------------------------------*/
205 24705 : if ( HQ_mode == HQ_GEN_SWB || HQ_mode == HQ_GEN_FB )
206 : {
207 9003 : hq_generic_fine( coeff, last_sfm, sfm_start, sfm_end, bwe_seed, coeff_fine );
208 : }
209 :
210 : /*----------------------------------------------------------------*
211 : * Apply envelope
212 : *----------------------------------------------------------------*/
213 :
214 24705 : if ( HQ_mode != HQ_HARMONIC && HQ_mode != HQ_HVQ )
215 : {
216 18486 : apply_envelope( coeff, norm, norm_adj, num_sfm, last_sfm, HQ_mode, length, sfm_start, sfm_end, normq_v, coeff_out, coeff_fine );
217 : }
218 :
219 : /*----------------------------------------------------------------*
220 : * Harmonic BWE, HVQ BWE and HQ SWB BWE
221 : *----------------------------------------------------------------*/
222 :
223 24705 : if ( HQ_mode == HQ_HARMONIC )
224 : {
225 2352 : harm_bwe( coeff_fine, coeff, num_sfm, sfm_start, sfm_end, last_sfm, high_sfm, R, prev_hq_mode, norm, noise_level, prev_noise_level, bwe_seed, coeff_out, element_mode );
226 : }
227 22353 : else if ( HQ_mode == HQ_HVQ )
228 : {
229 3867 : hvq_bwe( coeff, coeff_fine, sfm_start, sfm_end, sfmsize, last_sfm, prev_hq_mode, bwe_peaks, bin_th, num_sfm, core_brate, R, norm, noise_level, prev_noise_level, bwe_seed, coeff_out );
230 : }
231 18486 : else if ( HQ_mode == HQ_GEN_SWB || HQ_mode == HQ_GEN_FB )
232 : {
233 9003 : hq_generic_bwe( HQ_mode, coeff_fine, hq_generic_fenv, coeff_out, hq_generic_offset, prev_L_swb_norm, hq_generic_exc_clas, sfm_end, num_sfm, num_env_bands, R );
234 : }
235 :
236 : /*----------------------------------------------------------------*
237 : * HQ WB BWE refinements
238 : *----------------------------------------------------------------*/
239 :
240 24705 : if ( length == L_FRAME16k && core_brate <= HQ_32k )
241 : {
242 1443 : hq_wb_nf_bwe( coeff, is_transient, prev_bfi, normq_v, num_sfm, sfm_start, sfm_end, sfmsize, last_sfm, R, prev_is_transient, prev_normq, prev_env, bwe_seed, prev_coeff_out, prev_R, coeff_out );
243 : }
244 :
245 : /*----------------------------------------------------------------*
246 : * Update memories
247 : *----------------------------------------------------------------*/
248 :
249 24705 : if ( HQ_mode != HQ_HARMONIC && HQ_mode != HQ_HVQ )
250 : {
251 18486 : prev_noise_level[0] = 0.1f;
252 18486 : prev_noise_level[1] = 0.1f;
253 : }
254 24705 : if ( !( length == L_FRAME16k && core_brate <= HQ_32k ) )
255 : {
256 23262 : set_f( prev_env, 0, SFM_N_WB );
257 23262 : set_f( prev_normq, 0, SFM_N_WB );
258 : }
259 :
260 24705 : if ( length == L_FRAME32k && core_brate <= HQ_32k )
261 : {
262 3354 : *prev_R = R[SFM_N_WB - 1];
263 3354 : mvr2r( coeff_out + L_FRAME16k - L_HQ_WB_BWE, prev_coeff_out, L_HQ_WB_BWE );
264 : }
265 :
266 24705 : mvr2r( coeff_out, coeff, length );
267 :
268 24705 : return;
269 : }
|