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 : * decod_gen_voic()
49 : *
50 : * Decode generic (GC), voiced (VC) and AMR-WB IO frames
51 : *---------------------------------------------------------------------*/
52 :
53 344403 : ivas_error decod_gen_voic(
54 : Decoder_State *st, /* i/o: decoder static memory */
55 : const int16_t L_frame, /* i : length of the frame */
56 : const int16_t sharpFlag, /* i : formant sharpening flag */
57 : const float *Aq, /* i : LP filter coefficient */
58 : const float Es_pred, /* i : predicted scaled innov. energy */
59 : const int16_t do_WI, /* i : FEC fast recovery flag */
60 : float *pitch_buf, /* o : floating pitch values for each subframe */
61 : float *voice_factors, /* o : voicing factors */
62 : float *exc, /* i/o: adapt. excitation exc */
63 : float *exc2, /* i/o: adapt. excitation/total exc */
64 : float *bwe_exc, /* o : excitation for SWB TBE */
65 : int16_t *unbits, /* number of unused bits */
66 : float *gain_buf, /* o : floating pitch gain for each subframe */
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 344403 : int16_t T0 = PIT_MIN, T0_frac = 0, T0_min, T0_max; /* integer pitch variables */
72 344403 : float gain_pit = 0.0f; /* pitch gain */
73 344403 : float gain_code = 0.0f; /* gain/normalized gain of the algebraic excitation */
74 344403 : float norm_gain_code = 0.0f; /* normalized gain of the algebraic excitation */
75 344403 : float gain_inov = 0; /* Innovation gain */
76 : float gains_mem[2 * ( NB_SUBFR - 1 )]; /* pitch gain and code gain from previous subframes */
77 : float voice_fac; /* voicing factor */
78 : float code[L_SUBFR]; /* algebraic codevector */
79 :
80 : const float *p_Aq; /* Pointer to frame LP coefficient */
81 : float *pt_pitch; /* pointer to floating pitch */
82 : int16_t i_subfr, i; /* tmp variables */
83 344403 : float error_flag = 0.0f;
84 344403 : float gain_preQ = 0; /* Gain of prequantizer excitation */
85 : float code_preQ[L_SUBFR]; /* Prequantizer excitation */
86 : float norm_gain_preQ;
87 : int16_t pitch_limit_flag;
88 :
89 : DTFS_STRUCTURE *PREVP, *CURRP;
90 344403 : int16_t shft_prev = 0, shft_curr = 0;
91 344403 : float ph_offset, dummy2[2], out[L_FRAME16k], enratio = 0.0f;
92 : float sp_enratio, curr_spch_nrg, prev_spch_nrg, curr_res_nrg, prev_res_nrg, syn_tmp[L_FRAME16k], mem_tmp[M];
93 : int16_t harm_flag_acelp;
94 : ivas_error error;
95 :
96 344403 : error = IVAS_ERR_OK;
97 :
98 : /* read harmonicity flag */
99 344403 : harm_flag_acelp = 0;
100 344403 : if ( st->core_brate >= MIN_BRATE_AVQ_EXC && st->core_brate <= MAX_BRATE_AVQ_EXC_TD && st->coder_type == GENERIC )
101 : {
102 23811 : harm_flag_acelp = get_next_indice( st, 1 );
103 : }
104 :
105 : /*------------------------------------------------------------------*
106 : * ACELP subframe loop
107 : *------------------------------------------------------------------*/
108 :
109 344403 : p_Aq = Aq; /* pointer to interpolated LPC parameters */
110 344403 : pt_pitch = pitch_buf; /* pointer to the pitch buffer */
111 344403 : norm_gain_preQ = 0.0f;
112 344403 : gain_preQ = 0;
113 344403 : set_f( code_preQ, 0, L_SUBFR );
114 :
115 1909236 : for ( i_subfr = 0; i_subfr < L_frame; i_subfr += L_SUBFR )
116 : {
117 : /*----------------------------------------------------------------------*
118 : * Decode pitch lag
119 : *----------------------------------------------------------------------*/
120 :
121 1564833 : *pt_pitch = pit_decode( st, st->core_brate, 0, L_frame, i_subfr, st->coder_type, &pitch_limit_flag, &T0, &T0_frac, &T0_min, &T0_max, L_SUBFR, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf );
122 :
123 : /*--------------------------------------------------------------*
124 : * Find the adaptive codebook vector
125 : *--------------------------------------------------------------*/
126 :
127 1564833 : pred_lt4( &exc[i_subfr], &exc[i_subfr], T0, T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
128 :
129 1564833 : tbe_celp_exc( st->element_mode, st->idchan, bwe_exc, L_frame, L_SUBFR, i_subfr, T0, T0_frac, &error_flag, st->tdm_LRTD_flag );
130 :
131 : /*--------------------------------------------------------------*
132 : * LP filtering of the adaptive excitation
133 : *--------------------------------------------------------------*/
134 :
135 1564833 : lp_filt_exc_dec( st, MODE1, i_subfr, L_SUBFR, L_frame, st->acelp_cfg.ltf_mode, exc );
136 :
137 : /*-----------------------------------------------------------------*
138 : * Transform-domain contribution decoding (active frames)
139 : *-----------------------------------------------------------------*/
140 :
141 1564833 : if ( st->core_brate >= MIN_BRATE_AVQ_EXC && st->coder_type != INACTIVE )
142 : {
143 161145 : gain_code = 0.0f;
144 161145 : transf_cdbk_dec( st, harm_flag_acelp, i_subfr, Es_pred, gain_code, &gain_preQ, &norm_gain_preQ, code_preQ, unbits );
145 : }
146 :
147 : /*--------------------------------------------------------------*
148 : * Innovation decoding
149 : *--------------------------------------------------------------*/
150 :
151 1564833 : inov_decode( st, st->core_brate, 0, L_frame, sharpFlag, i_subfr, p_Aq, st->tilt_code, *pt_pitch, code, L_SUBFR );
152 :
153 : /*--------------------------------------------------------------*
154 : * Gain decoding
155 : * Estimate spectrum tilt and voicing
156 : *--------------------------------------------------------------*/
157 :
158 1564833 : if ( st->core_brate <= ACELP_8k00 )
159 : {
160 39420 : gain_dec_lbr( st, st->coder_type, i_subfr, code, &gain_pit, &gain_code, &gain_inov, &norm_gain_code, gains_mem, L_SUBFR );
161 : }
162 1525413 : else if ( st->core_brate > ACELP_32k )
163 : {
164 129075 : gain_dec_SQ( st, i_subfr, code, Es_pred, &gain_pit, &gain_code, &gain_inov, &norm_gain_code );
165 : }
166 : else
167 : {
168 1396338 : gain_dec_mless( st, L_frame, st->coder_type, i_subfr, -1, code, Es_pred, &gain_pit, &gain_code, &gain_inov, &norm_gain_code );
169 : }
170 :
171 1564833 : st->tilt_code = est_tilt( exc + i_subfr, gain_pit, code, gain_code, &voice_fac, L_SUBFR, 0 );
172 :
173 : /*-----------------------------------------------------------------*
174 : * Transform-domain contribution decoding (inactive frames)
175 : *-----------------------------------------------------------------*/
176 :
177 1564833 : if ( !st->inactive_coder_type_flag && st->coder_type == INACTIVE )
178 : {
179 38610 : transf_cdbk_dec( st, 0, i_subfr, Es_pred, gain_code, &gain_preQ, &norm_gain_preQ, code_preQ, unbits );
180 : }
181 :
182 : /* update LP filtered gains for the case of frame erasures */
183 1564833 : lp_gain_updt( i_subfr, gain_pit, norm_gain_code + norm_gain_preQ, &st->lp_gainp, &st->lp_gainc, L_frame );
184 :
185 : /*----------------------------------------------------------------------*
186 : * Find the total excitation
187 : *----------------------------------------------------------------------*/
188 :
189 101714145 : for ( i = 0; i < L_SUBFR; i++ )
190 : {
191 100149312 : exc2[i + i_subfr] = gain_pit * exc[i + i_subfr];
192 100149312 : exc[i + i_subfr] = exc2[i + i_subfr] + gain_code * code[i];
193 : }
194 :
195 : /*-----------------------------------------------------------------*
196 : * Add the ACELP pre-quantizer contribution
197 : *-----------------------------------------------------------------*/
198 :
199 1564833 : if ( gain_preQ != 0 )
200 : {
201 12984075 : for ( i = 0; i < L_SUBFR; i++ )
202 : {
203 12784320 : exc2[i + i_subfr] += gain_preQ * code_preQ[i];
204 12784320 : exc[i + i_subfr] += gain_preQ * code_preQ[i];
205 : }
206 : }
207 :
208 : /*-----------------------------------------------------------------*
209 : * Prepare TBE excitation
210 : *-----------------------------------------------------------------*/
211 :
212 1564833 : 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 );
213 :
214 : /*----------------------------------------------------------------*
215 : * Excitation enhancements (update of total excitation signal)
216 : *----------------------------------------------------------------*/
217 :
218 1564833 : if ( st->core_brate > ACELP_32k || st->coder_type == INACTIVE )
219 : {
220 144810 : mvr2r( exc + i_subfr, exc2 + i_subfr, L_SUBFR );
221 : }
222 : else
223 : {
224 1420023 : enhancer( MODE1, st->core_brate, -1, 0, st->coder_type, L_frame, voice_fac, st->stab_fac, norm_gain_code, gain_inov, &st->gc_threshold, code, exc2 + i_subfr, gain_pit, st->dispMem );
225 : }
226 :
227 1564833 : p_Aq += ( M + 1 );
228 1564833 : pt_pitch++;
229 1564833 : gain_buf[i_subfr / L_SUBFR] = gain_pit;
230 1564833 : st->tilt_code_dec[i_subfr / L_SUBFR] = st->tilt_code;
231 : }
232 :
233 : /* FEC fast recovery */
234 344403 : if ( do_WI )
235 : {
236 0 : shft_prev = L_EXC_MEM - (int16_t) rint_new( st->bfi_pitch );
237 0 : prev_res_nrg = sum2_f( st->hWIDec->old_exc2 + shft_prev, (int16_t) rint_new( st->bfi_pitch ) ) + 1e-6f;
238 0 : prev_spch_nrg = sum2_f( st->hWIDec->old_syn2 + shft_prev, (int16_t) rint_new( st->bfi_pitch ) ) + 1e-6f;
239 :
240 0 : mvr2r( st->mem_syn2, mem_tmp, M );
241 0 : syn_12k8( st->L_frame, Aq, exc2, syn_tmp, mem_tmp, 1 );
242 :
243 0 : shft_curr = st->L_frame - (int16_t) rint_new( pitch_buf[NB_SUBFR16k - 1] );
244 0 : curr_res_nrg = sum2_f( exc2 + shft_curr, (int16_t) rint_new( pitch_buf[NB_SUBFR16k - 1] ) );
245 0 : curr_spch_nrg = sum2_f( syn_tmp + shft_curr, (int16_t) rint_new( pitch_buf[NB_SUBFR16k - 1] ) );
246 :
247 0 : enratio = curr_res_nrg / prev_res_nrg;
248 0 : sp_enratio = curr_spch_nrg / prev_spch_nrg;
249 :
250 0 : if ( enratio > 0.25f &&
251 0 : enratio < 15.0f &&
252 0 : sp_enratio > 0.15f &&
253 0 : st->bfi_pitch < 150 &&
254 0 : pitch_buf[NB_SUBFR16k - 1] < 150 )
255 : {
256 0 : if ( ( error = DTFS_new( &PREVP ) ) != IVAS_ERR_OK )
257 : {
258 0 : return error;
259 : }
260 :
261 0 : if ( ( error = DTFS_new( &CURRP ) ) != IVAS_ERR_OK )
262 : {
263 0 : return error;
264 : }
265 :
266 0 : DTFS_to_fs( st->hWIDec->old_exc2 + shft_prev, (int16_t) rint_new( st->bfi_pitch ), PREVP, (int16_t) st->output_Fs, do_WI );
267 0 : DTFS_to_fs( exc2 + shft_curr, (int16_t) rint_new( pitch_buf[NB_SUBFR16k - 1] ), CURRP, (int16_t) st->output_Fs, do_WI );
268 :
269 0 : ph_offset = 0.0f;
270 0 : if ( ( error = WIsyn( *PREVP, CURRP, dummy2, &ph_offset, out, st->L_frame, 1 ) ) != IVAS_ERR_OK )
271 : {
272 0 : return error;
273 : }
274 :
275 0 : mvr2r( out, exc2, st->L_frame );
276 0 : mvr2r( exc2, exc, st->L_frame );
277 :
278 : /* update bwe_exc for SWB-TBE */
279 0 : if ( st->hBWE_TD != NULL )
280 : {
281 0 : for ( i_subfr = 0; i_subfr < L_frame; i_subfr += L_SUBFR )
282 : {
283 0 : interp_code_4over2( exc + i_subfr, bwe_exc + ( i_subfr * 2 ), L_SUBFR );
284 : }
285 : }
286 :
287 0 : free( PREVP );
288 0 : free( CURRP );
289 : }
290 : }
291 :
292 : /* SC-VBR */
293 344403 : st->prev_gain_pit_dec = gain_pit;
294 :
295 344403 : return error;
296 : }
|