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 "prot.h"
44 : #include "rom_enc.h"
45 : #include "rom_com.h"
46 : #include "wmc_auto.h"
47 :
48 : /*--------------------------------------------------------------------------*
49 : * hq_hr_enc()
50 : *
51 : * HQ high rate encoding routine
52 : *--------------------------------------------------------------------------*/
53 :
54 8423 : void hq_hr_enc(
55 : Encoder_State *st, /* i/o: encoder state structure */
56 : float *t_audio, /* i/o: transform-domain coefficients */
57 : const int16_t length, /* i : length of spectrum */
58 : int16_t *num_bits, /* i/o: number of available bits */
59 : const int16_t is_transient, /* i : transient flag */
60 : const int16_t vad_hover_flag /* i : VAD hangover flag */
61 : )
62 : {
63 : int16_t nb_sfm;
64 : int16_t sum, hcode_l;
65 : int16_t difidx[NB_SFM];
66 : int16_t normqlg2[NB_SFM], ynrm[NB_SFM];
67 : int16_t nf_idx;
68 : int16_t LCmode;
69 : int16_t shape_bits, num_sfm, numnrmibits;
70 : int16_t hqswb_clas;
71 : int16_t num_env_bands;
72 : int16_t Npeaks, start_norm;
73 : int16_t difidx_org[NB_SFM];
74 : int16_t R[NB_SFM];
75 : int16_t peaks[HVQ_MAX_PEAKS];
76 : int16_t sfmsize[NB_SFM], sfm_start[NB_SFM], sfm_end[NB_SFM];
77 : int16_t npulses[NB_SFM], maxpulse[NB_SFM];
78 : int16_t Rsubband[NB_SFM]; /* Q3 */
79 : float t_audio_q[L_SPEC48k_EXT];
80 : float nf_gains[HVQ_NF_GROUPS], pe_gains[HVQ_NF_GROUPS];
81 : float noise_level[HVQ_BWE_NOISE_BANDS];
82 : int16_t hq_generic_offset;
83 : float hq_generic_fenv[HQ_FB_FENV];
84 8423 : int16_t hq_generic_exc_clas = 0;
85 : int16_t core_sfm;
86 8423 : int16_t har_freq_est1 = 0, har_freq_est2 = 0;
87 8423 : int16_t flag_dis = 1;
88 : const int16_t *subband_search_offset;
89 : int16_t wBands[2];
90 : int16_t b_delta_env;
91 : float att;
92 8423 : HQ_ENC_HANDLE hHQ_core = st->hHQ_core;
93 8423 : BSTR_ENC_HANDLE hBstr = st->hBstr;
94 :
95 : /*------------------------------------------------------------------*
96 : * Initializations
97 : *------------------------------------------------------------------*/
98 :
99 8423 : Npeaks = 0;
100 8423 : set_s( npulses, 0, NB_SFM );
101 8423 : set_s( maxpulse, 0, NB_SFM );
102 8423 : set_s( difidx_org, 0, NB_SFM );
103 8423 : set_f( t_audio_q, 0.0f, L_FRAME48k );
104 8423 : set_f( nf_gains, 0.0f, HVQ_NF_GROUPS );
105 8423 : set_f( pe_gains, 0.0f, HVQ_NF_GROUPS );
106 :
107 : /*------------------------------------------------------------------*
108 : * Classification
109 : *------------------------------------------------------------------*/
110 :
111 8423 : *num_bits -= hq_classifier_enc( st, length, t_audio, is_transient, &Npeaks, peaks, pe_gains, nf_gains, &hqswb_clas );
112 :
113 :
114 : /*------------------------------------------------------------------*
115 : * Set quantization parameters
116 : *------------------------------------------------------------------*/
117 :
118 8423 : 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 );
119 :
120 : /*------------------------------------------------------------------*
121 : * Transient frame handling
122 : *------------------------------------------------------------------*/
123 :
124 : /* Interleave MLT coefficients of 4 sub-vectors in case of transient frame */
125 8423 : if ( is_transient )
126 : {
127 562 : interleave_spectrum( t_audio, length );
128 : }
129 :
130 8423 : if ( st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD )
131 : {
132 1076 : calculate_hangover_attenuation_gain( st, &att, vad_hover_flag );
133 1076 : if ( att != 1.0f )
134 : {
135 0 : v_multc( t_audio, att, t_audio, sfm_end[num_sfm - 1] );
136 : }
137 : }
138 :
139 : /*------------------------------------------------------------------*
140 : * Scalar quantization of norms
141 : * Encode norm indices
142 : *------------------------------------------------------------------*/
143 :
144 : /* calculate and quantize norms */
145 8423 : calc_norm( t_audio, ynrm, normqlg2, start_norm, num_env_bands, sfmsize, sfm_start );
146 :
147 : /* create differential code of quantized norm indices */
148 8423 : diff_envelope_coding( is_transient, num_env_bands, start_norm, ynrm, normqlg2, difidx );
149 :
150 : /* Find norm coding mode and calculate number of bits */
151 8423 : hcode_l = encode_envelope_indices( hBstr, num_env_bands, numnrmibits, difidx, &LCmode, 0, NORMAL_HQ_CORE, is_transient );
152 8423 : *num_bits -= hcode_l + NORM0_BITS + FLAGS_BITS;
153 :
154 : /* Encode norm indices */
155 8423 : encode_envelope_indices( hBstr, num_env_bands, numnrmibits, difidx, &LCmode, 1, NORMAL_HQ_CORE, is_transient );
156 :
157 : /*------------------------------------------------------------------*
158 : * HQ GENERIC BWE encoding
159 : *------------------------------------------------------------------*/
160 :
161 8423 : if ( hqswb_clas == HQ_GEN_SWB || hqswb_clas == HQ_GEN_FB )
162 : {
163 3069 : hq_generic_hf_encoding( t_audio, hq_generic_fenv, hq_generic_offset, st, &hq_generic_exc_clas, length );
164 3069 : if ( hq_generic_exc_clas == HQ_GENERIC_SP_EXC )
165 : {
166 144 : ( *num_bits )++; /* conditional 1 bit saving for representing FD3 BWE excitation class */
167 : }
168 3069 : map_hq_generic_fenv_norm( hqswb_clas, hq_generic_fenv, ynrm, normqlg2, num_env_bands, nb_sfm, hq_generic_offset );
169 : }
170 :
171 : /*------------------------------------------------------------------*
172 : * Bit allocation
173 : *------------------------------------------------------------------*/
174 :
175 8423 : hq_bit_allocation( st->core_brate, length, hqswb_clas, num_bits, normqlg2, nb_sfm, sfmsize, noise_level, R, Rsubband, &sum, &core_sfm, num_env_bands );
176 :
177 : /*------------------------------------------------------------------*
178 : * Normalize coefficients with quantized norms
179 : *------------------------------------------------------------------*/
180 8423 : if ( hqswb_clas != HQ_HVQ )
181 : {
182 7096 : if ( hqswb_clas == HQ_GEN_SWB || hqswb_clas == HQ_GEN_FB )
183 : {
184 3069 : b_delta_env = calc_nor_delta_hf( hBstr, t_audio, ynrm, Rsubband, num_env_bands, nb_sfm, sfmsize, sfm_start, core_sfm );
185 3069 : sum -= b_delta_env;
186 : }
187 7096 : normalizecoefs( t_audio, ynrm, nb_sfm, sfm_start, sfm_end );
188 : }
189 :
190 : /*------------------------------------------------------------------*
191 : * Quantize/code spectral fine structure using PVQ or HVQ
192 : *------------------------------------------------------------------*/
193 :
194 8423 : if ( hqswb_clas == HQ_HVQ )
195 : {
196 : #ifdef DEBUGGING
197 : sum = hvq_enc( hBstr, st->bwidth, st->idchan, st->core_brate, *num_bits, Npeaks, ynrm, R, peaks, nf_gains, noise_level, pe_gains, t_audio, t_audio_q );
198 : #else
199 1327 : sum = hvq_enc( hBstr, st->bwidth, st->core_brate, *num_bits, Npeaks, ynrm, R, peaks, nf_gains, noise_level, pe_gains, t_audio, t_audio_q );
200 : #endif
201 1327 : *num_bits -= sum;
202 : }
203 : else
204 : {
205 7096 : shape_bits = pvq_core_enc( hBstr, t_audio, t_audio_q, sum, nb_sfm, sfm_start, sfm_end, sfmsize, Rsubband, R, npulses, maxpulse, HQ_CORE );
206 7096 : *num_bits += ( sum - shape_bits );
207 : }
208 :
209 8423 : if ( hqswb_clas == HQ_HVQ || hqswb_clas == HQ_HARMONIC )
210 : {
211 2133 : subband_search_offset = subband_search_offsets_13p2kbps_Har;
212 2133 : wBands[0] = SWB_SB_BW_LEN0_16KBPS_HAR;
213 2133 : wBands[1] = SWB_SB_BW_LEN1_16KBPS_HAR;
214 :
215 2133 : 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 );
216 :
217 2133 : hHQ_core->prev_frm_hfe2 = har_freq_est2;
218 : }
219 :
220 : /* reset LR-HQ memories */
221 8423 : hHQ_core->prev_frm_hfe2 = 0; /*reset*/
222 8423 : hHQ_core->prev_stab_hfe2 = 0;
223 :
224 8423 : nf_idx = 0;
225 8423 : if ( !is_transient && hqswb_clas != HQ_HVQ && !( length == L_FRAME16k && st->core_brate <= HQ_32k ) )
226 : {
227 6059 : if ( hqswb_clas == HQ_GEN_SWB || hqswb_clas == HQ_GEN_FB )
228 : {
229 3069 : nf_idx = noise_adjust( t_audio, R, sfm_start, sfm_end, max( core_sfm, num_env_bands - 1 ) );
230 3069 : push_indice( hBstr, IND_NF_IDX, nf_idx, 2 );
231 : }
232 : else
233 : {
234 2990 : nf_idx = noise_adjust( t_audio, R, sfm_start, sfm_end, core_sfm );
235 2990 : push_indice( hBstr, IND_NF_IDX, nf_idx, 2 );
236 : }
237 : }
238 :
239 :
240 : /* updates */
241 8423 : hHQ_core->prev_hqswb_clas = hqswb_clas;
242 :
243 : /* Prepare synthesis for LB generation in case of switch to ACELP */
244 8423 : if ( hqswb_clas != HQ_HVQ )
245 : {
246 7096 : apply_envelope_enc( t_audio_q, ynrm, num_sfm, sfm_start, sfm_end );
247 : }
248 :
249 8423 : if ( is_transient )
250 : {
251 562 : de_interleave_spectrum( t_audio_q, length );
252 : }
253 :
254 8423 : mvr2r( t_audio_q, t_audio, length );
255 :
256 :
257 8423 : return;
258 : }
|