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 "prot.h"
43 : #include "rom_com.h"
44 : #include "wmc_auto.h"
45 :
46 : /*-------------------------------------------------------------------*
47 : * encod_tran()
48 : *
49 : * Encode transition (TC) frames
50 : *-------------------------------------------------------------------*/
51 :
52 14366 : int16_t encod_tran(
53 : Encoder_State *st, /* i/o: state structure */
54 : const float speech[], /* i : input speech */
55 : const float Aw[], /* i : weighted A(z) unquantized for subframes */
56 : const float Aq[], /* i : LP coefficients */
57 : const float Es_pred, /* i : predicted scaled innov. energy */
58 : const float *res, /* i : residual signal */
59 : float *syn, /* i/o: core synthesis */
60 : float *exc, /* i/o: current non-enhanced excitation */
61 : float *exc2, /* i/o: current enhanced excitation */
62 : float *pitch_buf, /* i/o: floating pitch values for each subframe */
63 : float *voice_factors, /* o : voicing factors */
64 : float *bwe_exc, /* i/o: excitation for SWB TBE */
65 : int16_t tc_subfr, /* i/o: TC subframe classification */
66 : int16_t position, /* i : maximum of residual signal index */
67 : int16_t *unbits /* i/o: number of unused bits */
68 : )
69 : {
70 : float xn[L_SUBFR]; /* Target vector for pitch search */
71 : float xn2[L_SUBFR]; /* Target vector for codebook search */
72 : float cn[L_SUBFR]; /* Target vector in residual domain */
73 : float h1[L_SUBFR + ( M + 1 )]; /* Impulse response vector */
74 : float code[L_SUBFR]; /* Fixed codebook excitation */
75 : float y1[L_SUBFR]; /* Filtered adaptive excitation */
76 : float y2[L_SUBFR]; /* Filtered algebraic excitation */
77 : float gain_pit; /* Pitch gain */
78 : float voice_fac; /* Voicing factor */
79 : float gain_code; /* Gain of code */
80 : float gain_inov; /* inovation gain */
81 : int16_t i, i_subfr; /* tmp variables */
82 : int16_t T0_min, T0_max; /* pitch and TC variables */
83 : int16_t unbits_ACELP;
84 : int16_t T0, T0_frac; /* close loop integer pitch and fractional part */
85 : float *pt_pitch; /* pointer to floating pitch buffer */
86 : float g_corr[6]; /* ACELP correlation values and gain pitch */
87 : int16_t clip_gain; /* LSF clip gain */
88 : const float *p_Aw, *p_Aq; /* pointer to LP filter coefficient vector */
89 : float gain_preQ; /* Gain of prequantizer excitation */
90 : float code_preQ[L_SUBFR]; /* Prequantizer excitation */
91 : int16_t Jopt_flag; /* joint optimization flag */
92 : int16_t unbits_PI; /* saved bits for EVS_PI */
93 : float norm_gain_code;
94 : int16_t L_frame;
95 :
96 14366 : BSTR_ENC_HANDLE hBstr = st->hBstr;
97 14366 : LPD_state_HANDLE hLPDmem = st->hLPDmem;
98 :
99 : /*------------------------------------------------------------------*
100 : * Initializations
101 : *------------------------------------------------------------------*/
102 :
103 14366 : gain_pit = 0;
104 14366 : gain_code = 0;
105 14366 : gain_preQ = 0;
106 14366 : unbits_PI = 0;
107 :
108 14366 : L_frame = st->L_frame;
109 :
110 14366 : if ( L_frame == L_FRAME )
111 : {
112 7547 : T0_max = PIT_MAX;
113 7547 : T0_min = PIT_MIN;
114 : }
115 : else /* L_frame == L_FRAME16k */
116 : {
117 6819 : T0_max = PIT16k_MAX;
118 6819 : T0_min = PIT16k_MIN;
119 : }
120 :
121 14366 : Jopt_flag = 0;
122 14366 : unbits_ACELP = *unbits;
123 14366 : *unbits = 0;
124 :
125 14366 : p_Aw = Aw;
126 14366 : p_Aq = Aq;
127 14366 : pt_pitch = pitch_buf;
128 14366 : gain_preQ = 0;
129 14366 : set_f( code_preQ, 0, L_SUBFR );
130 :
131 : /*----------------------------------------------------------------*
132 : * ACELP subframe loop
133 : *----------------------------------------------------------------*/
134 :
135 78649 : for ( i_subfr = 0; i_subfr < L_frame; i_subfr += L_SUBFR )
136 : {
137 : /*----------------------------------------------------------------*
138 : * Find the the excitation search target "xn" and innovation
139 : * target in residual domain "cn"
140 : * Compute impulse response, h1[], of weighted synthesis filter
141 : *----------------------------------------------------------------*/
142 :
143 64283 : mvr2r( &res[i_subfr], &exc[i_subfr], L_SUBFR );
144 :
145 64283 : find_targets( speech, hLPDmem->mem_syn, i_subfr, &hLPDmem->mem_w0, p_Aq, res, L_SUBFR, p_Aw, st->preemph_fac, xn, cn, h1 );
146 :
147 : /*-----------------------------------------------------------------*
148 : * TC: subframe determination &
149 : * adaptive/glottal part of excitation construction
150 : *-----------------------------------------------------------------*/
151 :
152 64283 : transition_enc( st, i_subfr, &tc_subfr, &Jopt_flag, &position, &T0, &T0_frac, &T0_min, &T0_max, exc, y1, h1, xn, xn2, st->clip_var, &gain_pit, g_corr, &clip_gain, &pt_pitch, bwe_exc, &unbits_ACELP );
153 :
154 : /*-----------------------------------------------------------------*
155 : * Transform domain contribution encoding - active frames
156 : *-----------------------------------------------------------------*/
157 :
158 64283 : if ( st->core_brate >= MIN_BRATE_AVQ_EXC )
159 : {
160 6550 : 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 );
161 : }
162 :
163 : /*-----------------------------------------------------------------*
164 : * ACELP codebook search + pitch sharpening
165 : *-----------------------------------------------------------------*/
166 :
167 64283 : inov_encode( st, st->core_brate, 0, L_frame, st->last_L_frame, st->coder_type, st->bwidth, st->sharpFlag, i_subfr, tc_subfr, p_Aq, gain_pit, cn, exc, h1, hLPDmem->tilt_code, *pt_pitch, xn2, code, y2, &unbits_PI, L_SUBFR );
168 :
169 64283 : if ( ( L_frame == L_FRAME16k ) && ( tc_subfr == 0 ) && ( i_subfr == L_SUBFR ) && ( T0 == 2 * L_SUBFR ) )
170 : {
171 463 : Jopt_flag = 1;
172 : }
173 :
174 : /*-----------------------------------------------------------------*
175 : * Quantize the gains
176 : * Test quantized gain of pitch for pitch clipping algorithm
177 : * Update tilt of code: 0.0 (unvoiced) to 0.5 (voiced)
178 : *-----------------------------------------------------------------*/
179 :
180 64283 : if ( Jopt_flag == 0 )
181 : {
182 : /* SQ gain_code */
183 20291 : gain_enc_tc( hBstr, st->acelp_cfg.gains_mode, i_subfr, xn, y2, code, Es_pred, &gain_pit, &gain_code, &gain_inov, &norm_gain_code );
184 : }
185 : else
186 : {
187 43992 : if ( st->core_brate > ACELP_32k )
188 : {
189 : /* SQ gain_pit and gain_code */
190 3005 : 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 );
191 : }
192 : else
193 : {
194 : /* VQ gain_pit and gain_code */
195 40987 : gain_enc_mless( hBstr, st->acelp_cfg.gains_mode, st->element_mode, L_frame, i_subfr, tc_subfr, xn, y1, y2, code, Es_pred, &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, clip_gain );
196 : }
197 : }
198 :
199 : /*-----------------------------------------------------------------*
200 : * update LP-filtered gains for the case of frame erasures
201 : *-----------------------------------------------------------------*/
202 :
203 64283 : gp_clip_test_gain_pit( st->element_mode, st->core_brate, gain_pit, st->clip_var );
204 :
205 64283 : hLPDmem->tilt_code = est_tilt( exc + i_subfr, gain_pit, code, gain_code, &voice_fac, L_SUBFR, 0 );
206 :
207 : /*-----------------------------------------------------------------*
208 : * Update memory of the weighting filter
209 : *-----------------------------------------------------------------*/
210 :
211 64283 : hLPDmem->mem_w0 = xn[L_SUBFR - 1] - ( gain_pit * y1[L_SUBFR - 1] ) - ( gain_code * y2[L_SUBFR - 1] );
212 :
213 : /*-----------------------------------------------------------------*
214 : * Construct adaptive part of the excitation
215 : * Save the non-enhanced excitation for FEC_exc
216 : *-----------------------------------------------------------------*/
217 :
218 4178395 : for ( i = 0; i < L_SUBFR; i++ )
219 : {
220 4114112 : exc2[i + i_subfr] = gain_pit * exc[i + i_subfr];
221 4114112 : exc[i + i_subfr] = exc2[i + i_subfr] + gain_code * code[i];
222 : }
223 :
224 : /*-----------------------------------------------------------------*
225 : * Add the ACELP pre-quantizer contribution
226 : *-----------------------------------------------------------------*/
227 :
228 64283 : if ( st->core_brate >= MIN_BRATE_AVQ_EXC )
229 : {
230 425750 : for ( i = 0; i < L_SUBFR; i++ )
231 : {
232 419200 : exc2[i + i_subfr] += gain_preQ * code_preQ[i];
233 419200 : exc[i + i_subfr] += gain_preQ * code_preQ[i];
234 : }
235 : }
236 :
237 : /*-----------------------------------------------------------------*
238 : * Prepare TBE excitation
239 : *-----------------------------------------------------------------*/
240 :
241 64283 : 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 );
242 :
243 : /*-----------------------------------------------------------------*
244 : * Synthesize speech to update mem_syn[].
245 : * Update A(z) filters
246 : *-----------------------------------------------------------------*/
247 :
248 64283 : syn_filt( p_Aq, M, &exc[i_subfr], &syn[i_subfr], L_SUBFR, hLPDmem->mem_syn, 1 );
249 :
250 64283 : p_Aw += ( M + 1 );
251 64283 : p_Aq += ( M + 1 );
252 64283 : pt_pitch++;
253 : }
254 :
255 : /* write reserved bits */
256 14366 : while ( unbits_PI > 0 )
257 : {
258 0 : i = min( unbits_PI, 16 );
259 0 : push_indice( hBstr, IND_UNUSED, 0, i );
260 0 : unbits_PI -= i;
261 : }
262 :
263 : /* write TC configuration */
264 14366 : if ( L_frame == L_FRAME )
265 : {
266 7547 : if ( tc_subfr == TC_0_0 )
267 : {
268 1750 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
269 : }
270 5797 : else if ( tc_subfr == TC_0_64 )
271 : {
272 1343 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
273 1343 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
274 1343 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
275 1343 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
276 : }
277 4454 : else if ( tc_subfr == TC_0_128 )
278 : {
279 691 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
280 691 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
281 691 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
282 691 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
283 : }
284 3763 : else if ( tc_subfr == TC_0_192 )
285 : {
286 221 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
287 221 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
288 221 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
289 : }
290 3542 : else if ( tc_subfr == L_SUBFR )
291 : {
292 1159 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
293 1159 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
294 1159 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
295 : }
296 2383 : else if ( tc_subfr == 2 * L_SUBFR )
297 : {
298 562 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
299 562 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
300 562 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
301 562 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
302 : }
303 1821 : else if ( tc_subfr == 3 * L_SUBFR )
304 : {
305 1821 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
306 1821 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
307 1821 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
308 1821 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
309 : }
310 : }
311 : else /* L_frame == L_FRAME16k */
312 : {
313 6819 : if ( tc_subfr == 0 )
314 : {
315 2804 : push_indice( hBstr, IND_TC_SUBFR, 0, 2 );
316 : }
317 4015 : else if ( tc_subfr == L_SUBFR )
318 : {
319 1019 : push_indice( hBstr, IND_TC_SUBFR, 1, 2 );
320 : }
321 2996 : else if ( tc_subfr == 2 * L_SUBFR )
322 : {
323 671 : push_indice( hBstr, IND_TC_SUBFR, 2, 2 );
324 : }
325 2325 : else if ( tc_subfr == 3 * L_SUBFR )
326 : {
327 249 : push_indice( hBstr, IND_TC_SUBFR, 3, 2 );
328 249 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
329 : }
330 2076 : else if ( tc_subfr == 4 * L_SUBFR )
331 : {
332 2076 : push_indice( hBstr, IND_TC_SUBFR, 3, 2 );
333 2076 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
334 : }
335 : }
336 :
337 : /* SC-VBR */
338 14366 : if ( st->Opt_SC_VBR )
339 : {
340 0 : st->hSC_VBR->prev_ppp_gain_pit = gain_pit;
341 0 : st->hSC_VBR->prev_tilt_code = hLPDmem->tilt_code;
342 : }
343 :
344 14366 : return tc_subfr;
345 : }
|