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 : #include <stdint.h>
34 : #include "options.h"
35 : #ifdef DEBUGGING
36 : #include "debug.h"
37 : #endif
38 : #include "cnst.h"
39 : #include "stat_enc.h"
40 : #include "rom_com.h"
41 : #include "ivas_rom_com.h"
42 : #include "ivas_cnst.h"
43 : #include "prot.h"
44 : #include "ivas_prot.h"
45 : #include "wmc_auto.h"
46 :
47 :
48 : /*-------------------------------------------------------------------*
49 : * tdm_low_rate_enc()
50 : *
51 : * Encode secondary channel of TD Stereo with a low-bitrate encoder
52 : *-------------------------------------------------------------------*/
53 :
54 7561 : void tdm_low_rate_enc(
55 : Encoder_State *st, /* i/o: State structure */
56 : const float Aq[], /* i : 12k8 Lp coefficient */
57 : const float *res, /* i : residual signal */
58 : float *synth, /* i/o: core synthesis */
59 : float *exc, /* i/o: current non-enhanced excitation */
60 : float *pitch_buf, /* i/o: floating pitch values for each subframe */
61 : float *voice_factors, /* o : voicing factors */
62 : float *bwe_exc, /* o : excitation for SWB TBE */
63 : const int16_t attack_flag, /* i : GSC attack flag */
64 : const float *lsf_new, /* i : current frame ISF vector */
65 : float *tmp_noise /* o : long-term noise energy */
66 : )
67 : {
68 : const float *p_Aq;
69 : int16_t i_subfr, nb_subfr, last_pit_bin;
70 : int16_t tmp_nb_bits_tot;
71 : float dct_res[L_FRAME], dct_epit[L_FRAME];
72 : float exc_wo_nf[L_FRAME];
73 7561 : LPD_state_HANDLE hLPDmem = st->hLPDmem;
74 :
75 : /*---------------------------------------------------------------*
76 : * Initialization
77 : *---------------------------------------------------------------*/
78 :
79 7561 : nb_subfr = 2;
80 :
81 7561 : st->GSC_IVAS_mode = 0;
82 7561 : st->GSC_noisy_speech = 1;
83 7561 : st->hGSCEnc->noise_lev = 14;
84 :
85 7561 : hLPDmem->tilt_code = 0.0f;
86 7561 : set_f( dct_epit, 0, L_FRAME );
87 7561 : set_f( pitch_buf, L_SUBFR, NB_SUBFR );
88 7561 : last_pit_bin = L_FRAME / 2;
89 :
90 : /*---------------------------------------------------------------*
91 : * DCT transform of the residual and create a subsample residual
92 : *---------------------------------------------------------------*/
93 :
94 7561 : edct( res, dct_res, L_FRAME, st->element_mode );
95 :
96 : /*--------------------------------------------------------------------------------------*
97 : * GSC encoder
98 : *--------------------------------------------------------------------------------------*/
99 :
100 : /* Find the current total number of bits used */
101 7561 : tmp_nb_bits_tot = st->hBstr->nb_bits_tot;
102 :
103 7561 : if ( st->extl_brate > 0 )
104 : {
105 : /* subtract 1 bit for TBE/BWE BWE flag (bit counted in extl_brate) */
106 70 : tmp_nb_bits_tot--;
107 : }
108 :
109 7561 : gsc_enc( st, dct_res, dct_epit, last_pit_bin, tmp_nb_bits_tot, nb_subfr, lsf_new, exc_wo_nf, tmp_noise );
110 :
111 : /*--------------------------------------------------------------------------------------*
112 : * iDCT transform
113 : *--------------------------------------------------------------------------------------*/
114 :
115 7561 : edct( dct_epit, exc, L_FRAME, st->element_mode );
116 :
117 7561 : edct( exc_wo_nf, exc_wo_nf, L_FRAME, st->element_mode );
118 :
119 : /*--------------------------------------------------------------------------------------*
120 : * Remove potential pre-echo in case an onset has been detected
121 : *--------------------------------------------------------------------------------------*/
122 :
123 7561 : pre_echo_att( &st->hGSCEnc->Last_frame_ener, exc, attack_flag, st->last_coder_type, st->L_frame );
124 :
125 : /*--------------------------------------------------------------------------------------*
126 : * Update BWE excitation
127 : *--------------------------------------------------------------------------------------*/
128 :
129 7561 : if ( st->hBWE_TD != NULL )
130 : {
131 7545 : set_f( voice_factors, 0.0f, NB_SUBFR );
132 :
133 7545 : if ( st->tdm_LRTD_flag )
134 : {
135 11 : interp_code_5over2( exc, bwe_exc, L_FRAME );
136 : }
137 : else
138 : {
139 7534 : set_f( bwe_exc, 0, L_FRAME32k );
140 : }
141 : }
142 :
143 : /*--------------------------------------------------------------------------------------*
144 : * Synthesis
145 : *--------------------------------------------------------------------------------------*/
146 :
147 7561 : p_Aq = Aq;
148 37805 : for ( i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR )
149 : {
150 30244 : syn_filt( p_Aq, M, &exc_wo_nf[i_subfr], &synth[i_subfr], L_SUBFR, hLPDmem->mem_syn, 1 );
151 30244 : p_Aq += ( M + 1 );
152 : }
153 :
154 : /*--------------------------------------------------------------------------------------*
155 : * Updates
156 : *--------------------------------------------------------------------------------------*/
157 7561 : mvr2r( exc_wo_nf, exc, L_FRAME );
158 :
159 7561 : return;
160 : }
161 :
162 :
163 : /*-------------------------------------------------------------------*
164 : * encod_gen_2sbfr()
165 : *
166 : * Encode GC, 2 subframes mode
167 : *-------------------------------------------------------------------*/
168 :
169 0 : void encod_gen_2sbfr(
170 : Encoder_State *st, /* i/o: state structure */
171 : const float speech[], /* i : input speech */
172 : const float Aw[], /* i : weighted A(z) unquantized for subframes */
173 : const float Aq[], /* i : LP coefficients */
174 : const float *res, /* i : residual signal */
175 : float *syn, /* i/o: core synthesis */
176 : float *exc, /* i/o: current non-enhanced excitation */
177 : float *exc2, /* i/o: current enhanced excitation */
178 : float *pitch_buf, /* i/o: floating pitch values for each subframe */
179 : float *voice_factors, /* o : voicing factors */
180 : float *bwe_exc, /* o : excitation for SWB TBE */
181 : const int16_t tdm_Pitch_reuse_flag, /* i : primary channel pitch reuse flag */
182 : const float tdm_Pri_pitch_buf[] /* i : pitch values for primary channel */
183 : )
184 : {
185 : float xn[2 * L_SUBFR]; /* Target vector for pitch search */
186 : float xn2[2 * L_SUBFR]; /* Target vector for codebook search */
187 : float cn[2 * L_SUBFR]; /* Target vector in residual domain */
188 : float h1[2 * L_SUBFR + ( M + 1 )]; /* Impulse response vector */
189 : float code[2 * L_SUBFR]; /* Fixed codebook excitation */
190 : float y1[2 * L_SUBFR]; /* Filtered adaptive excitation */
191 : float y2[2 * L_SUBFR]; /* Filtered algebraic excitation */
192 : float gain_pit; /* Pitch gain */
193 : float voice_fac; /* Voicing factor */
194 : float gain_code; /* Gain of code */
195 : float gain_inov; /* inovation gain */
196 : int16_t L_frame, coder_type; /* L_frame; coder type */
197 : int16_t i, i_subfr; /* tmp variables */
198 : int16_t T0, T0_frac; /* close loop integer pitch and fractional part */
199 : int16_t T0_min, T0_max; /* pitch variables */
200 : float *pt_pitch; /* pointer to floating pitch buffer */
201 : float g_corr[6]; /* ACELP correl, values + gain pitch */
202 : float gains_mem[2 * ( NB_SUBFR - 1 )]; /* pitch gain and code gain from previous subframes */
203 : int16_t clip_gain; /* LSF clip gain */
204 : const float *p_Aw, *p_Aq; /* pointer to LP filter coeff. vector*/
205 : float norm_gain_code;
206 : int16_t pitch_limit_flag;
207 : float error;
208 :
209 0 : LPD_state_HANDLE hLPDmem = st->hLPDmem;
210 :
211 : /*------------------------------------------------------------------*
212 : * Initializations
213 : *------------------------------------------------------------------*/
214 :
215 0 : gain_pit = 0;
216 0 : gain_code = 0;
217 0 : error = 0.0f;
218 :
219 0 : T0_max = PIT_MAX;
220 0 : T0_min = PIT_MIN;
221 :
222 0 : L_frame = L_FRAME;
223 0 : coder_type = GENERIC;
224 :
225 0 : p_Aw = Aw;
226 0 : p_Aq = Aq;
227 0 : pt_pitch = pitch_buf;
228 :
229 : /*------------------------------------------------------------------*
230 : * ACELP subframe loop
231 : *------------------------------------------------------------------*/
232 :
233 0 : for ( i_subfr = 0; i_subfr < L_frame; i_subfr += 2 * L_SUBFR )
234 : {
235 : /*----------------------------------------------------------------*
236 : * Find the the excitation search target "xn" and innovation
237 : * target in residual domain "cn"
238 : * Compute impulse response, h1[], of weighted synthesis filter
239 : *----------------------------------------------------------------*/
240 :
241 0 : mvr2r( &res[i_subfr], &exc[i_subfr], 2 * L_SUBFR );
242 :
243 0 : find_targets( speech, hLPDmem->mem_syn, i_subfr, &hLPDmem->mem_w0, p_Aq, res, 2 * L_SUBFR, p_Aw, st->preemph_fac, xn, cn, h1 );
244 :
245 : /*------------------------------------------------------------------------*
246 : * Close-loop pitch search on the 1st and 3rd subfr only and quantization
247 : * Adaptive exc. construction
248 : *------------------------------------------------------------------------*/
249 :
250 0 : *pt_pitch = pit_encode( st->hBstr, st->acelp_cfg.pitch_bits, st->core_brate, 0, L_frame, coder_type, &pitch_limit_flag, i_subfr, exc, 2 * L_SUBFR, st->pitch, &T0_min, &T0_max, &T0, &T0_frac, h1, xn, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf );
251 :
252 0 : tbe_celp_exc( st->element_mode, st->idchan, bwe_exc, L_frame, 2 * L_SUBFR, i_subfr, T0, T0_frac, &error, st->tdm_LRTD_flag );
253 :
254 : /*-----------------------------------------------------------------*
255 : * Find adaptive exitation
256 : *-----------------------------------------------------------------*/
257 :
258 0 : pred_lt4( &exc[i_subfr], &exc[i_subfr], T0, T0_frac, 2 * L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
259 :
260 : /*-----------------------------------------------------------------*
261 : * Gain clipping test to avoid unstable synthesis on frame erasure
262 : *-----------------------------------------------------------------*/
263 :
264 0 : clip_gain = gp_clip( st->element_mode, st->core_brate, st->voicing, i_subfr, coder_type, xn, st->clip_var );
265 :
266 : /*-----------------------------------------------------------------*
267 : * LP filtering of the adaptive excitation, codebook target computation
268 : *-----------------------------------------------------------------*/
269 :
270 0 : lp_filt_exc_enc( MODE1, coder_type, i_subfr, exc, h1, xn, y1, xn2, 2 * L_SUBFR, L_frame, g_corr, clip_gain, &gain_pit, &st->acelp_cfg.ltf_mode );
271 :
272 : /* update long-term pitch gain for speech/music classifier */
273 0 : st->hSpMusClas->lowrate_pitchGain = 0.9f * st->hSpMusClas->lowrate_pitchGain + 0.1f * gain_pit;
274 :
275 : /*-----------------------------------------------------------------*
276 : * Innovation encoding
277 : *-----------------------------------------------------------------*/
278 :
279 0 : inov_encode( st, st->core_brate, 0, L_frame, st->last_L_frame, coder_type, st->bwidth, st->sharpFlag, i_subfr, -1, p_Aq, gain_pit, cn, exc, h1, hLPDmem->tilt_code, *pt_pitch, xn2, code, y2, &i, 2 * L_SUBFR );
280 :
281 : /*-----------------------------------------------------------------*
282 : * Gain encoding
283 : *-----------------------------------------------------------------*/
284 :
285 0 : gain_enc_lbr( st->hBstr, st->acelp_cfg.gains_mode, coder_type, i_subfr, xn, y1, y2, code, &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, gains_mem, clip_gain, 2 * L_SUBFR );
286 :
287 0 : if ( st->Opt_SC_VBR )
288 : {
289 0 : if ( st->hSC_VBR->last_ppp_mode == 1 )
290 : {
291 : /* SC-VBR - all other st->clip_var values will be updated even in a PPP frame */
292 0 : st->clip_var[1] = gain_pit;
293 : }
294 : }
295 :
296 : /*-----------------------------------------------------------------*
297 : * update LP-filtered gains for the case of frame erasures
298 : *-----------------------------------------------------------------*/
299 :
300 0 : gp_clip_test_gain_pit( st->element_mode, st->core_brate, gain_pit, st->clip_var );
301 :
302 0 : hLPDmem->tilt_code = est_tilt( exc + i_subfr, gain_pit, code, gain_code, &voice_fac, 2 * L_SUBFR, 0 );
303 :
304 : /*-----------------------------------------------------------------*
305 : * Update memory of the weighting filter
306 : *-----------------------------------------------------------------*/
307 :
308 0 : hLPDmem->mem_w0 = xn[2 * L_SUBFR - 1] - ( gain_pit * y1[2 * L_SUBFR - 1] ) - ( gain_code * y2[2 * L_SUBFR - 1] );
309 :
310 : /*-----------------------------------------------------------------*
311 : * Construct adaptive part of the excitation
312 : * Save the non-enhanced excitation for FEC_exc
313 : *-----------------------------------------------------------------*/
314 :
315 0 : for ( i = 0; i < 2 * L_SUBFR; i++ )
316 : {
317 0 : exc2[i + i_subfr] = gain_pit * exc[i + i_subfr];
318 0 : exc[i + i_subfr] = exc2[i + i_subfr] + gain_code * code[i];
319 : }
320 :
321 : /*-----------------------------------------------------------------*
322 : * Prepare TBE excitation
323 : *-----------------------------------------------------------------*/
324 :
325 0 : prep_tbe_exc( L_frame, L_SUBFR, i_subfr, gain_pit, gain_code, code, voice_fac, &voice_factors[i_subfr / L_SUBFR], bwe_exc, 0, NULL, T0, coder_type, st->core_brate, st->element_mode, st->idchan, st->hBWE_TD != NULL, st->tdm_LRTD_flag );
326 :
327 0 : voice_factors[i_subfr / L_SUBFR + 1] = voice_factors[i_subfr / L_SUBFR];
328 :
329 : /*-----------------------------------------------------------------*
330 : * Synthesize speech to update mem_syn[].
331 : * Update A(z) filters
332 : *-----------------------------------------------------------------*/
333 :
334 0 : syn_filt( p_Aq, M, &exc[i_subfr], &syn[i_subfr], 2 * L_SUBFR, hLPDmem->mem_syn, 1 );
335 :
336 0 : p_Aw += 2 * ( M + 1 );
337 0 : p_Aq += 2 * ( M + 1 );
338 :
339 0 : pt_pitch++;
340 0 : *pt_pitch = *( pt_pitch - 1 );
341 0 : pt_pitch++;
342 : }
343 :
344 : /* SC-VBR */
345 0 : if ( st->Opt_SC_VBR )
346 : {
347 0 : st->hSC_VBR->prev_ppp_gain_pit = gain_pit;
348 0 : st->hSC_VBR->prev_tilt_code = hLPDmem->tilt_code;
349 : }
350 :
351 0 : return;
352 : }
|