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 : * dec_pit_exc()
49 : *
50 : * Decode pitch-only contribution (used by the GSC technology)
51 : *-------------------------------------------------------------------*/
52 :
53 38538 : void dec_pit_exc(
54 : Decoder_State *st, /* i/o: decoder static memory */
55 : const int16_t L_frame, /* i : length of the frame */
56 : const float *Aq, /* i : LP filter coefficient */
57 : const float Es_pred, /* i : predicted scaled innov. energy */
58 : float *pitch_buf, /* o : floating pitch values for each subframe */
59 : float *code, /* o : innovation */
60 : float *exc, /* i/o: adapt. excitation exc */
61 : const int16_t nb_subfr, /* i : Number of subframe considered */
62 : float *gain_buf, /* o : floating pitch gain for each subframe */
63 : const int16_t tdm_Pitch_reuse_flag, /* i : primary channel pitch reuse flag */
64 : const float tdm_Pri_pitch_buf[] /* i : primary channel pitch buffer */
65 : )
66 : {
67 : int16_t T0, T0_frac, T0_min, T0_max; /* integer pitch variables */
68 : float gain_pit; /* pitch gain */
69 : float gain_code; /* gain/normalized gain of the algebraic excitation */
70 : float norm_gain_code; /* normalized gain of the algebraic excitation */
71 : float gain_inov; /* Innovation gain */
72 : float voice_fac; /* voicing factor */
73 : int16_t L_subfr, pit_idx;
74 : const float *p_Aq; /* Pointer to frame LP coefficient */
75 : float *pt_pitch; /* pointer to floating pitch */
76 : int16_t i_subfr, i; /* tmp variables */
77 : int32_t Local_BR, Pitch_BR;
78 : int16_t Pitch_CT, pitch_limit_flag;
79 : int16_t nbits;
80 : float *pt_gain; /* Pointer to floating gain values for each subframe */
81 : int16_t use_fcb;
82 : float gains_mem[2 * ( NB_SUBFR - 1 )]; /* pitch gain and code gain from previous subframes */
83 :
84 : /*------------------------------------------------------------------*
85 : * Initialization
86 : *------------------------------------------------------------------*/
87 :
88 38538 : use_fcb = 0;
89 38538 : if ( st->GSC_IVAS_mode > 0 && ( st->GSC_noisy_speech || st->core_brate > GSC_H_RATE_STG ) )
90 : {
91 342 : Local_BR = ACELP_8k00;
92 342 : Pitch_CT = GENERIC;
93 342 : Pitch_BR = ACELP_8k00;
94 342 : if ( st->L_frame == L_FRAME16k )
95 : {
96 0 : Local_BR = ACELP_14k80;
97 0 : if ( st->GSC_IVAS_mode > 0 )
98 : {
99 0 : Local_BR = ACELP_9k60;
100 : }
101 0 : Pitch_BR = st->core_brate;
102 : }
103 : }
104 38196 : else if ( st->GSC_noisy_speech )
105 : {
106 2055 : Local_BR = ACELP_7k20;
107 2055 : Pitch_CT = GENERIC;
108 2055 : Pitch_BR = ACELP_7k20;
109 2055 : if ( L_frame == L_FRAME16k )
110 : {
111 0 : Pitch_BR = st->core_brate;
112 : }
113 : }
114 : else
115 : {
116 36141 : Local_BR = ACELP_7k20;
117 36141 : Pitch_CT = AUDIO;
118 36141 : Pitch_BR = st->core_brate;
119 :
120 36141 : if ( L_frame == L_FRAME16k )
121 : {
122 3231 : Local_BR = ACELP_13k20;
123 3231 : Pitch_CT = GENERIC;
124 : }
125 : }
126 :
127 38538 : gain_code = 0;
128 38538 : pitch_limit_flag = 1; /* always extended pitch Q range */
129 :
130 : /*------------------------------------------------------------------*
131 : * ACELP subframe loop
132 : *------------------------------------------------------------------*/
133 :
134 38538 : L_subfr = st->L_frame / nb_subfr;
135 38538 : p_Aq = Aq; /* pointer to interpolated LPC parameters */
136 38538 : pt_pitch = pitch_buf; /* pointer to the pitch buffer */
137 38538 : pt_gain = gain_buf; /* pointer to the gain buffer */
138 :
139 38538 : if ( ( ( st->core_brate >= MIN_RATE_FCB || ( st->GSC_noisy_speech == 1 && ( ( st->L_frame == L_FRAME && st->core_brate >= ACELP_13k20 ) || ( st->L_frame == L_FRAME16k && st->core_brate >= GSC_H_RATE_STG ) || st->GSC_IVAS_mode == 0 ) ) ) && L_subfr == L_SUBFR ) )
140 : {
141 2055 : use_fcb = 1;
142 : }
143 36483 : else if ( st->GSC_IVAS_mode > 0 && L_subfr == 2 * L_SUBFR && st->GSC_IVAS_mode < 3 )
144 : {
145 5988 : use_fcb = 2;
146 5988 : st->acelp_cfg.fcb_mode = 1;
147 5988 : set_s( st->acelp_cfg.gains_mode, 6, 4 );
148 5988 : set_s( st->acelp_cfg.pitch_bits, 9, 4 );
149 5988 : set_s( st->acelp_cfg.fixed_cdk_index, 14, 5 );
150 : }
151 :
152 117510 : for ( i_subfr = 0; i_subfr < L_frame; i_subfr += L_subfr )
153 : {
154 : /*----------------------------------------------------------------------*
155 : * Decode pitch lag
156 : *----------------------------------------------------------------------*/
157 :
158 78972 : *pt_pitch = pit_decode( st, Pitch_BR, 0, L_frame, i_subfr, Pitch_CT, &pitch_limit_flag, &T0, &T0_frac, &T0_min, &T0_max, L_subfr, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf );
159 :
160 : /*--------------------------------------------------------------*
161 : * Find the adaptive codebook vector.
162 : *--------------------------------------------------------------*/
163 :
164 78972 : pred_lt4( &exc[i_subfr], &exc[i_subfr], T0, T0_frac, L_subfr + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
165 :
166 : /*--------------------------------------------------------------*
167 : * Innovation decoding
168 : *--------------------------------------------------------------*/
169 :
170 78972 : if ( use_fcb == 1 )
171 : {
172 8220 : inov_decode( st, Local_BR, 0, L_frame, 1, i_subfr, p_Aq, st->tilt_code, *pt_pitch, code, L_SUBFR );
173 :
174 : /*--------------------------------------------------------------*
175 : * Gain decoding
176 : * Estimate spectrum tilt and voicing
177 : *--------------------------------------------------------------*/
178 :
179 8220 : gain_dec_mless( st, L_frame, LOCAL_CT, i_subfr, -1, code, Es_pred, &gain_pit, &gain_code, &gain_inov, &norm_gain_code );
180 :
181 8220 : st->tilt_code = est_tilt( exc + i_subfr, gain_pit, code, gain_code, &voice_fac, L_SUBFR, 0 );
182 : }
183 70752 : else if ( use_fcb == 2 )
184 : {
185 : /*--------------------------------------------------------------*
186 : * Innovation decoding
187 : *--------------------------------------------------------------*/
188 :
189 11976 : inov_decode( st, st->core_brate, 0, L_FRAME, 0, i_subfr, p_Aq, st->tilt_code, *pt_pitch, code, 2 * L_SUBFR );
190 :
191 : /*--------------------------------------------------------------*
192 : * Gain decoding
193 : * Estimate spectrum tilt and voicing
194 : *--------------------------------------------------------------*/
195 :
196 11976 : gain_dec_lbr( st, GENERIC, i_subfr, code, &gain_pit, &gain_code, &gain_inov, &norm_gain_code, gains_mem, 2 * L_SUBFR );
197 11976 : st->tilt_code = est_tilt( exc + i_subfr, gain_pit, code, gain_code, &voice_fac, 2 * L_SUBFR, 0 );
198 : }
199 : else
200 : {
201 58776 : nbits = 5;
202 58776 : if ( st->core_brate < MIN_RATE_FCB )
203 : {
204 57780 : nbits = 4;
205 : }
206 :
207 58776 : set_f( code, 0, L_SUBFR );
208 58776 : gain_code = 0.0f;
209 58776 : st->tilt_code = 0.0f;
210 :
211 58776 : pit_idx = get_next_indice( st, nbits );
212 :
213 58776 : gain_pit = 0.5853f + dic_gp[pit_idx];
214 :
215 58776 : if ( st->BER_detect ) /* Bitstream is corrupted, use the past pitch gain */
216 : {
217 0 : gain_pit = st->lp_gainp;
218 : }
219 : }
220 :
221 : /*----------------------------------------------------------------------*
222 : * Find the total excitation
223 : *----------------------------------------------------------------------*/
224 :
225 78972 : if ( use_fcb != 0 )
226 : {
227 2079204 : for ( i = 0; i < L_subfr; i++ )
228 : {
229 2059008 : exc[i + i_subfr] = gain_pit * exc[i + i_subfr] + gain_code * code[i];
230 : }
231 : }
232 : else
233 : {
234 8072280 : for ( i = 0; i < L_subfr; i++ )
235 : {
236 8013504 : exc[i + i_subfr] = gain_pit * exc[i + i_subfr];
237 : }
238 : }
239 :
240 78972 : if ( L_subfr == 5 * L_SUBFR )
241 : {
242 : /* update gains for FEC - equivalent to lp_gain_updt() */
243 3231 : st->lp_gainp = gain_pit;
244 3231 : st->lp_gainc = 0;
245 :
246 3231 : pt_pitch++;
247 3231 : *pt_pitch = *( pt_pitch - 1 );
248 3231 : pt_pitch++;
249 3231 : *pt_pitch = *( pt_pitch - 1 );
250 3231 : pt_pitch++;
251 3231 : *pt_pitch = *( pt_pitch - 1 );
252 3231 : pt_pitch++;
253 3231 : *pt_pitch = *( pt_pitch - 1 );
254 3231 : pt_pitch++;
255 :
256 3231 : p_Aq += 5 * ( M + 1 );
257 : }
258 75741 : else if ( L_subfr == 5 * L_SUBFR / 2 )
259 : {
260 0 : if ( i_subfr == 0 )
261 : {
262 0 : pt_pitch++;
263 0 : *pt_pitch = *( pt_pitch - 1 );
264 0 : pt_pitch++;
265 0 : p_Aq += 2 * ( M + 1 );
266 :
267 : /* update gains for FEC - equivalent to lp_gain_updt() */
268 0 : st->lp_gainp = ( 3.0f / 15.0f ) * gain_pit;
269 0 : st->lp_gainc = 0;
270 : }
271 : else
272 : {
273 0 : pt_pitch++;
274 0 : *pt_pitch = *( pt_pitch - 1 );
275 0 : pt_pitch++;
276 0 : *pt_pitch = *( pt_pitch - 1 );
277 0 : pt_pitch++;
278 0 : p_Aq += 3 * ( M + 1 );
279 :
280 : /* update gains for FEC - equivalent to lp_gain_updt() */
281 0 : st->lp_gainp = ( 12.0f / 15.0f ) * gain_pit;
282 0 : st->lp_gainc = 0;
283 : }
284 : }
285 75741 : else if ( L_subfr == 2 * L_SUBFR )
286 : {
287 16608 : p_Aq += 2 * ( M + 1 );
288 16608 : pt_pitch++;
289 16608 : *pt_pitch = *( pt_pitch - 1 );
290 16608 : pt_pitch++;
291 16608 : *pt_gain = gain_pit;
292 16608 : pt_gain++;
293 16608 : *pt_gain = *( pt_gain - 1 );
294 16608 : pt_gain++;
295 :
296 16608 : if ( i_subfr == 0 )
297 : {
298 : /* update gains for FEC - equivalent to lp_gain_updt() */
299 8304 : st->lp_gainp = ( 3.0f / 10.0f ) * gain_pit;
300 8304 : st->lp_gainc = 0;
301 : }
302 : else
303 : {
304 : /* update gains for FEC - equivalent to lp_gain_updt() */
305 8304 : st->lp_gainp = ( 7.0f / 10.0f ) * gain_pit;
306 8304 : st->lp_gainc = 0;
307 : }
308 : }
309 59133 : else if ( L_subfr == 4 * L_SUBFR )
310 : {
311 16293 : pt_pitch++;
312 16293 : *pt_pitch = *( pt_pitch - 1 );
313 16293 : pt_pitch++;
314 16293 : *pt_pitch = *( pt_pitch - 1 );
315 16293 : pt_pitch++;
316 16293 : *pt_pitch = *( pt_pitch - 1 );
317 16293 : pt_pitch++;
318 16293 : *pt_gain = gain_pit;
319 16293 : pt_gain++;
320 16293 : *pt_gain = *( pt_gain - 1 );
321 16293 : pt_gain++;
322 16293 : *pt_gain = *( pt_gain - 1 );
323 16293 : pt_gain++;
324 16293 : *pt_gain = *( pt_gain - 1 );
325 16293 : pt_gain++;
326 16293 : p_Aq += 4 * ( M + 1 );
327 :
328 : /* update gains for FEC - equivalent to lp_gain_updt() */
329 16293 : st->lp_gainp = gain_pit;
330 16293 : st->lp_gainc = 0;
331 : }
332 : else
333 : {
334 42840 : p_Aq += ( M + 1 );
335 42840 : pt_pitch++;
336 42840 : *pt_gain = gain_pit;
337 42840 : pt_gain++;
338 :
339 42840 : lp_gain_updt( i_subfr, gain_pit, 0, &st->lp_gainp, &st->lp_gainc, L_frame );
340 : }
341 : }
342 :
343 38538 : return;
344 : }
|