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_com.h"
45 : #include "wmc_auto.h"
46 :
47 : /*-------------------------------------------------------------------*
48 : * encod_gen_voic()
49 : *
50 : * Encode excitation signal
51 : *-------------------------------------------------------------------*/
52 :
53 118589 : void encod_gen_voic(
54 : Encoder_State *st, /* i/o: state structure */
55 : const float speech[], /* i : input speech */
56 : const float Aw[], /* i : weighted A(z) unquantized for subframes */
57 : const float Aq[], /* i : LP coefficients */
58 : const float Es_pred, /* i : predicted scaled innov. energy */
59 : const float *res, /* i : residual signal */
60 : float *syn, /* i/o: core synthesis */
61 : float *exc, /* i/o: current non-enhanced excitation */
62 : float *exc2, /* i/o: current enhanced excitation */
63 : float *pitch_buf, /* i/o: floating pitch values for each subframe */
64 : float *voice_factors, /* o : voicing factors */
65 : float *bwe_exc, /* o : excitation for SWB TBE */
66 : int16_t *unbits, /* i/o: number of unused bits */
67 : const int16_t tdm_Pitch_reuse_flag, /* i : primary channel pitch reuse flag */
68 : const float tdm_Pri_pitch_buf[] /* i : primary channel pitch buffer */
69 : )
70 : {
71 : float xn[L_SUBFR]; /* Target vector for pitch search */
72 : float xn2[L_SUBFR]; /* Target vector for codebook search */
73 : float cn[L_SUBFR]; /* Target vector in residual domain */
74 : float h1[L_SUBFR + ( M + 1 )]; /* Impulse response vector */
75 : float code[L_SUBFR]; /* Fixed codebook excitation */
76 : float y1[L_SUBFR]; /* Filtered adaptive excitation */
77 : float y2[L_SUBFR]; /* Filtered algebraic excitation */
78 : float gain_pit; /* Pitch gain */
79 : float voice_fac; /* Voicing factor */
80 : float gain_code; /* Gain of code */
81 : float gain_inov; /* inovation gain */
82 : int16_t i, i_subfr; /* tmp variables */
83 : int16_t T0, T0_frac; /* close loop integer pitch and fractional part */
84 : int16_t T0_min, T0_max; /* pitch variables */
85 : float *pt_pitch; /* pointer to floating pitch buffer */
86 : float g_corr[6]; /* ACELP correl, values + gain pitch */
87 : float gains_mem[2 * ( NB_SUBFR - 1 )]; /* pitch gain and code gain from previous subframes */
88 : int16_t clip_gain; /* LSF clip gain */
89 : const float *p_Aw, *p_Aq; /* pointer to LP filter coeff. vector*/
90 : float error;
91 : float gain_preQ; /* Gain of prequantizer excitation */
92 : float code_preQ[L_SUBFR]; /* Prequantizer excitation */
93 : int16_t unbits_PI; /* number of unused bits for EVS_PI */
94 : float norm_gain_code;
95 : int16_t pitch_limit_flag;
96 : int16_t harm_flag_acelp;
97 : int16_t lp_select, lp_flag;
98 : int16_t L_frame;
99 :
100 118589 : BSTR_ENC_HANDLE hBstr = st->hBstr;
101 118589 : LPD_state_HANDLE hLPDmem = st->hLPDmem;
102 :
103 : /*------------------------------------------------------------------*
104 : * Initializations
105 : *------------------------------------------------------------------*/
106 :
107 118589 : gain_pit = 0;
108 118589 : gain_code = 0;
109 118589 : gain_preQ = 0;
110 118589 : unbits_PI = 0;
111 118589 : error = 0.0f;
112 :
113 118589 : L_frame = st->L_frame;
114 :
115 118589 : if ( L_frame == L_FRAME )
116 : {
117 53851 : T0_max = PIT_MAX;
118 53851 : T0_min = PIT_MIN;
119 : }
120 : else /* L_frame == L_FRAME16k */
121 : {
122 64738 : T0_max = PIT16k_MAX;
123 64738 : T0_min = PIT16k_MIN;
124 : }
125 :
126 118589 : lp_flag = st->acelp_cfg.ltf_mode;
127 118589 : *unbits = 0;
128 :
129 118589 : p_Aw = Aw;
130 118589 : p_Aq = Aq;
131 118589 : pt_pitch = pitch_buf;
132 118589 : gain_preQ = 0;
133 118589 : set_f( code_preQ, 0, L_SUBFR );
134 :
135 : /* set and write harmonicity flag */
136 118589 : harm_flag_acelp = 0;
137 :
138 118589 : if ( st->core_brate >= MIN_BRATE_AVQ_EXC && st->core_brate <= MAX_BRATE_AVQ_EXC_TD && st->coder_type == GENERIC )
139 : {
140 8018 : if ( st->last_harm_flag_acelp > 2 )
141 : {
142 681 : harm_flag_acelp = 1;
143 : }
144 :
145 8018 : push_indice( hBstr, IND_HARM_FLAG_ACELP, harm_flag_acelp, 1 );
146 : }
147 :
148 : /*------------------------------------------------------------------*
149 : * ACELP subframe loop
150 : *------------------------------------------------------------------*/
151 :
152 657683 : for ( i_subfr = 0; i_subfr < L_frame; i_subfr += L_SUBFR )
153 : {
154 : /*----------------------------------------------------------------*
155 : * Find the the excitation search target "xn" and innovation
156 : * target in residual domain "cn"
157 : * Compute impulse response, h1[], of weighted synthesis filter
158 : *----------------------------------------------------------------*/
159 :
160 539094 : mvr2r( &res[i_subfr], &exc[i_subfr], L_SUBFR );
161 :
162 539094 : find_targets( speech, hLPDmem->mem_syn, i_subfr, &hLPDmem->mem_w0, p_Aq, res, L_SUBFR, p_Aw, st->preemph_fac, xn, cn, h1 );
163 :
164 : /*----------------------------------------------------------------*
165 : * Close-loop pitch search and quantization
166 : * Adaptive exc. construction
167 : *----------------------------------------------------------------*/
168 :
169 539094 : *pt_pitch = pit_encode( hBstr, st->acelp_cfg.pitch_bits, st->core_brate, 0, L_frame, st->coder_type, &pitch_limit_flag, i_subfr, exc, L_SUBFR, st->pitch, &T0_min, &T0_max, &T0, &T0_frac, h1, xn, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf );
170 :
171 539094 : tbe_celp_exc( st->element_mode, st->idchan, bwe_exc, L_frame, L_SUBFR, i_subfr, T0, T0_frac, &error, st->tdm_LRTD_flag );
172 :
173 : /*-----------------------------------------------------------------*
174 : * Find adaptive exitation
175 : *-----------------------------------------------------------------*/
176 :
177 539094 : pred_lt4( &exc[i_subfr], &exc[i_subfr], T0, T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
178 :
179 : /*-----------------------------------------------------------------*
180 : * Gain clipping test to avoid unstable synthesis on frame erasure
181 : *-----------------------------------------------------------------*/
182 :
183 539094 : clip_gain = gp_clip( st->element_mode, st->core_brate, st->voicing, i_subfr, st->coder_type, xn, st->clip_var );
184 :
185 539094 : if ( st->coder_type == INACTIVE )
186 : {
187 : /* in case of AVQ inactive, limit the gain to 0.65 */
188 13030 : clip_gain = 2;
189 : }
190 :
191 : /*-----------------------------------------------------------------*
192 : * LP filtering of the adaptive excitation, codebook target computation
193 : *-----------------------------------------------------------------*/
194 :
195 539094 : lp_select = lp_filt_exc_enc( MODE1, st->coder_type, i_subfr, exc, h1, xn, y1, xn2, L_SUBFR, L_frame, g_corr, clip_gain, &gain_pit, &lp_flag );
196 :
197 539094 : if ( lp_flag == NORMAL_OPERATION )
198 : {
199 471586 : push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
200 : }
201 :
202 : /* update long-term pitch gain for speech/music classifier */
203 539094 : st->hSpMusClas->lowrate_pitchGain = 0.9f * st->hSpMusClas->lowrate_pitchGain + 0.1f * gain_pit;
204 :
205 : /*-----------------------------------------------------------------*
206 : * Transform-domain contribution (active frames)
207 : *-----------------------------------------------------------------*/
208 :
209 539094 : if ( st->core_brate >= MIN_BRATE_AVQ_EXC && st->coder_type != INACTIVE )
210 : {
211 54355 : transf_cdbk_enc( st, harm_flag_acelp, i_subfr, cn, exc, p_Aq, p_Aw, h1, xn, xn2, y1, y2, Es_pred, &gain_pit, gain_code, g_corr, clip_gain, &gain_preQ, code_preQ, unbits );
212 : }
213 :
214 : /*-----------------------------------------------------------------*
215 : * Innovation encoding
216 : *-----------------------------------------------------------------*/
217 :
218 539094 : inov_encode( st, st->core_brate, 0, L_frame, st->last_L_frame, st->coder_type, st->bwidth, st->sharpFlag, i_subfr, -1, p_Aq, gain_pit, cn, exc, h1, hLPDmem->tilt_code, *pt_pitch, xn2, code, y2, &unbits_PI, L_SUBFR );
219 :
220 : /*-----------------------------------------------------------------*
221 : * Gain encoding
222 : *-----------------------------------------------------------------*/
223 :
224 539094 : if ( st->core_brate <= ACELP_8k00 )
225 : {
226 13380 : gain_enc_lbr( hBstr, st->acelp_cfg.gains_mode, st->coder_type, i_subfr, xn, y1, y2, code, &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, gains_mem, clip_gain, L_SUBFR );
227 : }
228 525714 : else if ( st->core_brate > ACELP_32k )
229 : {
230 43550 : gain_enc_SQ( hBstr, st->acelp_cfg.gains_mode, i_subfr, xn, y1, y2, code, Es_pred, &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, clip_gain );
231 : }
232 : else
233 : {
234 482164 : gain_enc_mless( hBstr, st->acelp_cfg.gains_mode, st->element_mode, L_frame, i_subfr, -1, xn, y1, y2, code, Es_pred, &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, clip_gain );
235 : }
236 :
237 539094 : if ( st->Opt_SC_VBR )
238 : {
239 0 : if ( st->hSC_VBR->last_ppp_mode == 1 )
240 : {
241 : /* SC-VBR - all other st->clip_var values will be updated even in a PPP frame */
242 0 : st->clip_var[1] = gain_pit;
243 : }
244 : }
245 :
246 : /*-----------------------------------------------------------------*
247 : * update LP-filtered gains for the case of frame erasures
248 : *-----------------------------------------------------------------*/
249 :
250 539094 : gp_clip_test_gain_pit( st->element_mode, st->core_brate, gain_pit, st->clip_var );
251 :
252 539094 : hLPDmem->tilt_code = est_tilt( exc + i_subfr, gain_pit, code, gain_code, &voice_fac, L_SUBFR, 0 );
253 :
254 : /*-----------------------------------------------------------------*
255 : * Transform-domain contribution (inactive frames)
256 : *-----------------------------------------------------------------*/
257 :
258 539094 : if ( !st->inactive_coder_type_flag && st->coder_type == INACTIVE )
259 : {
260 13030 : transf_cdbk_enc( st, 0, i_subfr, cn, exc, p_Aq, p_Aw, h1, xn, xn2, y1, y2, Es_pred, &gain_pit, gain_code, g_corr, clip_gain, &gain_preQ, code_preQ, unbits );
261 : }
262 :
263 : /*-----------------------------------------------------------------*
264 : * Update memory of the weighting filter
265 : *-----------------------------------------------------------------*/
266 :
267 539094 : hLPDmem->mem_w0 = xn[L_SUBFR - 1] - ( gain_pit * y1[L_SUBFR - 1] ) - ( gain_code * y2[L_SUBFR - 1] );
268 :
269 : /*-----------------------------------------------------------------*
270 : * Construct adaptive part of the excitation
271 : * Save the non-enhanced excitation for FEC_exc
272 : *-----------------------------------------------------------------*/
273 :
274 35041110 : for ( i = 0; i < L_SUBFR; i++ )
275 : {
276 34502016 : exc2[i + i_subfr] = gain_pit * exc[i + i_subfr];
277 34502016 : exc[i + i_subfr] = exc2[i + i_subfr] + gain_code * code[i];
278 : }
279 :
280 : /*-----------------------------------------------------------------*
281 : * Add the ACELP pre-quantizer contribution
282 : *-----------------------------------------------------------------*/
283 :
284 539094 : if ( gain_preQ != 0 )
285 : {
286 4380025 : for ( i = 0; i < L_SUBFR; i++ )
287 : {
288 4312640 : exc2[i + i_subfr] += gain_preQ * code_preQ[i];
289 4312640 : exc[i + i_subfr] += gain_preQ * code_preQ[i];
290 : }
291 : }
292 :
293 : /*-----------------------------------------------------------------*
294 : * Prepare TBE excitation
295 : *-----------------------------------------------------------------*/
296 :
297 539094 : prep_tbe_exc( L_frame, L_SUBFR, i_subfr, gain_pit, gain_code, code, voice_fac, &voice_factors[i_subfr / L_SUBFR], bwe_exc, gain_preQ, code_preQ, T0, st->coder_type, st->core_brate, st->element_mode, st->idchan, st->hBWE_TD != NULL, st->tdm_LRTD_flag );
298 :
299 : /*-----------------------------------------------------------------*
300 : * Synthesize speech to update mem_syn[].
301 : * Update A(z) filters
302 : *-----------------------------------------------------------------*/
303 :
304 539094 : syn_filt( p_Aq, M, &exc[i_subfr], &syn[i_subfr], L_SUBFR, hLPDmem->mem_syn, 1 );
305 :
306 539094 : p_Aw += ( M + 1 );
307 539094 : p_Aq += ( M + 1 );
308 539094 : pt_pitch++;
309 : }
310 :
311 : /* write reserved bits */
312 118589 : while ( unbits_PI > 0 )
313 : {
314 0 : i = min( unbits_PI, 16 );
315 0 : push_indice( hBstr, IND_UNUSED, 0, i );
316 0 : unbits_PI -= i;
317 : }
318 :
319 : /* SC-VBR */
320 118589 : if ( st->Opt_SC_VBR )
321 : {
322 0 : st->hSC_VBR->prev_ppp_gain_pit = gain_pit;
323 0 : st->hSC_VBR->prev_tilt_code = hLPDmem->tilt_code;
324 : }
325 :
326 118589 : return;
327 : }
|