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 "ivas_prot.h"
45 : #include "rom_com.h"
46 : #include "wmc_auto.h"
47 :
48 : /*---------------------------------------------------------------------*
49 : * inov_encode()
50 : *
51 : * Encode the algebraic innovation
52 : *---------------------------------------------------------------------*/
53 :
54 618392 : void inov_encode(
55 : Encoder_State *st, /* i/o: encoder state structure */
56 : const int32_t core_brate, /* i : core bitrate */
57 : const int16_t Opt_AMR_WB, /* i : flag indicating AMR-WB IO mode */
58 : const int16_t L_frame, /* i : length of the frame */
59 : const int16_t last_L_frame, /* i : length of the last frame */
60 : const int16_t coder_type, /* i : coding type */
61 : const int16_t bwidth, /* i : input signal bandwidth */
62 : const int16_t sharpFlag, /* i : formant sharpening flag */
63 : const int16_t i_subfr, /* i : subframe index */
64 : const int16_t tc_subfr, /* i : TC subframe index */
65 : const float *p_Aq, /* i : LP filter coefficients */
66 : const float gain_pit, /* i : adaptive excitation gain */
67 : float *cn, /* i/o: target vector in residual domain */
68 : const float *exc, /* i : pointer to excitation signal frame */
69 : float *h1, /* i/o: weighted filter input response */
70 : const float tilt_code, /* i : tilt of the excitation of previous subframe */
71 : const float pt_pitch, /* i : pointer to current subframe fractional pitch */
72 : const float *xn2, /* i : target vector for innovation search */
73 : float *code, /* o : algebraic excitation */
74 : float *y2, /* o : zero-memory filtered algebraic excitation */
75 : int16_t *unbits, /* o : number of unused bits for EVS_PI */
76 : const int16_t L_subfr /* i : subframe length */
77 : )
78 : {
79 : float dn[2 * L_SUBFR]; /* Correlation between xn2 and h1 */
80 : int16_t nBits, cmpl_flag;
81 : int16_t k;
82 : float g1, g2;
83 : float cn2[L_SUBFR];
84 : float Rw[L_SUBFR];
85 : int16_t i, acelpautoc;
86 618392 : BSTR_ENC_HANDLE hBstr = st->hBstr;
87 :
88 618392 : if ( L_frame == L_FRAME )
89 : {
90 260587 : g1 = FORMANT_SHARPENING_G1;
91 260587 : g2 = FORMANT_SHARPENING_G2;
92 : }
93 : else
94 : {
95 357805 : g1 = FORMANT_SHARPENING_G1_16k;
96 357805 : g2 = FORMANT_SHARPENING_G2_16k;
97 : }
98 :
99 : /*----------------------------------------------------------------*
100 : * Update target vector for codebook search in residual domain
101 : * Preemphasize the impulse response and include fixed-gain pitch contribution into impulse resp. h1[] (pitch sharpenning)
102 : * Correlation between target xn2[] and impulse response h1[]
103 : *----------------------------------------------------------------*/
104 :
105 618392 : if ( core_brate > ACELP_13k20 && !Opt_AMR_WB && L_subfr == L_SUBFR )
106 : {
107 405573 : acelpautoc = 1;
108 :
109 405573 : cb_shape( 1, 1, 0, sharpFlag, 0, g1, g2, p_Aq, h1, tilt_code, pt_pitch, L_SUBFR );
110 :
111 405573 : corr_xh( h1, Rw, h1, L_SUBFR );
112 :
113 26362245 : for ( k = 0; k < L_SUBFR; k++ )
114 : {
115 25956672 : cn2[k] = xn2[k];
116 :
117 843591840 : for ( i = 0; i < k; i++ )
118 : {
119 817635168 : cn2[k] -= cn2[i] * h1[k - i];
120 : }
121 : }
122 :
123 405573 : E_ACELP_toeplitz_mul( Rw, cn2, dn );
124 405573 : mvr2r( cn2, cn, L_SUBFR );
125 : }
126 : else
127 : {
128 212819 : acelpautoc = 0;
129 :
130 212819 : updt_tar( cn, cn, &exc[i_subfr], gain_pit, L_subfr );
131 :
132 212819 : cb_shape( 1, 1, 0, sharpFlag, 0, g1, g2, p_Aq, h1, tilt_code, pt_pitch, L_subfr );
133 :
134 212819 : corr_xh( xn2, dn, h1, L_subfr );
135 : }
136 :
137 : /*-----------------------------------------------------------------*
138 : * Set complexity reduction flag to limit the number of iterations
139 : * in algebraic innovation search
140 : *-----------------------------------------------------------------*/
141 :
142 618392 : cmpl_flag = 0;
143 :
144 618392 : if ( st->acelp_cfg.fcb_mode )
145 : {
146 : /* set number of iterations in TD stereo, secondary channel */
147 538736 : if ( st->element_mode == IVAS_CPE_TD && st->idchan == 1 )
148 : {
149 15056 : cmpl_flag = 1;
150 : }
151 : }
152 : else
153 : {
154 79656 : if ( L_frame == L_FRAME && coder_type == TRANSITION )
155 : {
156 308 : if ( core_brate == ACELP_8k00 && i_subfr == 0 && tc_subfr < L_SUBFR )
157 : {
158 0 : cmpl_flag = 3;
159 : }
160 :
161 308 : if ( core_brate == ACELP_11k60 && ( ( i_subfr == 0 && tc_subfr < L_SUBFR ) || tc_subfr == TC_0_0 || ( i_subfr == 3 * L_SUBFR && tc_subfr == TC_0_64 ) ) )
162 : {
163 155 : cmpl_flag = 3;
164 : }
165 :
166 308 : if ( ( core_brate == ACELP_13k20 || core_brate == ACELP_12k15 ) && ( ( i_subfr == 0 && tc_subfr < L_SUBFR ) || tc_subfr <= TC_0_64 ) )
167 : {
168 0 : cmpl_flag = 3;
169 : }
170 : }
171 :
172 79656 : if ( L_frame == L_FRAME16k )
173 : {
174 73955 : if ( core_brate <= ACELP_32k )
175 : {
176 25970 : cmpl_flag = 4;
177 :
178 25970 : if ( coder_type == TRANSITION && bwidth > WB )
179 : {
180 2015 : if ( i_subfr <= L_SUBFR )
181 : {
182 806 : cmpl_flag -= 1;
183 : }
184 : else
185 : {
186 1209 : cmpl_flag -= 2;
187 : }
188 : }
189 : }
190 47985 : else if ( core_brate <= ACELP_48k )
191 : {
192 47985 : cmpl_flag = 3;
193 :
194 47985 : if ( coder_type == TRANSITION )
195 : {
196 4435 : if ( i_subfr <= L_SUBFR )
197 : {
198 1774 : cmpl_flag -= 1;
199 : }
200 : else
201 : {
202 2661 : cmpl_flag -= 2;
203 : }
204 : }
205 : }
206 : else
207 : {
208 0 : cmpl_flag = 4;
209 :
210 0 : if ( coder_type == TRANSITION )
211 : {
212 0 : if ( i_subfr <= L_SUBFR )
213 : {
214 0 : cmpl_flag -= 1;
215 : }
216 : else
217 : {
218 0 : cmpl_flag -= 2;
219 : }
220 : }
221 : }
222 :
223 73955 : if ( coder_type == INACTIVE )
224 : {
225 13030 : cmpl_flag = 4;
226 : }
227 : }
228 :
229 : /* reduce number of iterations in a frame where there is an internal sampling rate switch in order not to increase the WC complexity */
230 79656 : if ( L_frame != st->last_L_frame && core_brate > ACELP_13k20 && ( core_brate < ACELP_32k || bwidth == WB ) )
231 : {
232 75 : if ( cmpl_flag > 1 )
233 : {
234 72 : cmpl_flag--;
235 : }
236 : }
237 : }
238 :
239 : /*-----------------------------------------------------------------*
240 : * Find and encode the algebraic innovation
241 : *-----------------------------------------------------------------*/
242 :
243 618392 : set_f( y2, 0, L_SUBFR );
244 :
245 618392 : if ( !Opt_AMR_WB )
246 : {
247 618392 : if ( st->acelp_cfg.fcb_mode )
248 : {
249 538736 : if ( st->acelp_cfg.fixed_cdk_index[i_subfr / L_subfr] < ACELP_FIXED_CDK_NB )
250 : {
251 : int16_t wordcnt, bitcnt;
252 : int16_t prm[8];
253 :
254 538736 : if ( st->acelp_cfg.fixed_cdk_index[i_subfr / L_subfr] >= 0 )
255 : {
256 538698 : if ( L_subfr == 2 * L_SUBFR )
257 : {
258 4086 : nBits = st->acelp_cfg.fixed_cdk_index[i_subfr / L_subfr];
259 :
260 4086 : if ( nBits == 8 )
261 : {
262 0 : acelp_1t64( hBstr, dn, h1, code, y2, L_subfr );
263 : }
264 : else
265 : {
266 4086 : acelp_fast( hBstr, nBits, dn, cn, h1, code, y2, L_subfr );
267 : }
268 : }
269 534612 : else if ( ( st->idchan == 1 && st->acelp_cfg.fixed_cdk_index[i_subfr / L_SUBFR] <= 7 ) || ( st->idchan == 0 && st->acelp_cfg.fixed_cdk_index[i_subfr / L_SUBFR] <= 3 ) )
270 : {
271 25009 : if ( st->acelp_cfg.fixed_cdk_index[i_subfr / L_SUBFR] == 0 )
272 : {
273 382 : acelp_1t64( hBstr, dn, h1, code, y2, L_subfr );
274 : }
275 : else
276 : {
277 24627 : acelp_fast( hBstr, st->acelp_cfg.fixed_cdk_index[i_subfr / L_SUBFR], dn, cn, h1, code, y2, L_SUBFR );
278 : }
279 : }
280 : else
281 : {
282 509603 : E_ACELP_4t( dn, cn, h1, Rw, acelpautoc, code, st->acelp_cfg.fixed_cdk_index[i_subfr / L_SUBFR], prm, L_frame, last_L_frame, st->total_brate, i_subfr, cmpl_flag );
283 :
284 509603 : wordcnt = ACELP_FIXED_CDK_BITS( st->acelp_cfg.fixed_cdk_index[i_subfr / L_SUBFR] ) >> 4;
285 509603 : bitcnt = ACELP_FIXED_CDK_BITS( st->acelp_cfg.fixed_cdk_index[i_subfr / L_SUBFR] ) & 15;
286 :
287 1684165 : for ( i = 0; i < wordcnt; i++ )
288 : {
289 1174562 : push_indice( hBstr, IND_ALG_CDBK_4T64, prm[i], 16 );
290 : }
291 509603 : if ( bitcnt )
292 : {
293 473878 : push_indice( hBstr, IND_ALG_CDBK_4T64, prm[i], bitcnt );
294 : }
295 :
296 : /* Generate weighted code */
297 509603 : set_f( y2, 0.0f, L_SUBFR );
298 33124195 : for ( i = 0; i < L_SUBFR; i++ )
299 : {
300 : /* Code is sparse, so check which samples are non-zero */
301 32614592 : if ( code[i] != 0 )
302 : {
303 169474707 : for ( k = 0; k < L_SUBFR - i; k++ )
304 : {
305 164384417 : y2[i + k] += code[i] * h1[k];
306 : }
307 : }
308 : }
309 : }
310 : }
311 : else
312 : {
313 38 : set_f( code, 0.0f, L_SUBFR );
314 38 : set_f( y2, 0.0f, L_SUBFR );
315 : }
316 : }
317 : #ifdef DEBUGGING
318 : else
319 : {
320 : IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "invalid mode for acelp frame!\n" );
321 : }
322 : #endif
323 : }
324 : else
325 : {
326 79656 : nBits = st->acelp_cfg.fixed_cdk_index[i_subfr / L_SUBFR];
327 :
328 79656 : if ( nBits == 7 )
329 : {
330 0 : acelp_1t64( hBstr, dn, h1, code, y2, L_SUBFR );
331 : }
332 79656 : else if ( nBits == 12 )
333 : {
334 15097 : acelp_2t32( hBstr, dn, h1, code, y2 );
335 : }
336 : else
337 : {
338 64559 : *unbits += acelp_4t64( hBstr, dn, cn, h1, Rw, acelpautoc, code, y2, nBits, cmpl_flag, Opt_AMR_WB );
339 : }
340 : }
341 : }
342 : else
343 : {
344 0 : if ( core_brate == ACELP_6k60 )
345 : {
346 0 : acelp_2t32( hBstr, dn, h1, code, y2 );
347 : }
348 0 : else if ( ( core_brate == ACELP_8k85 ) )
349 : {
350 0 : acelp_4t64( hBstr, dn, cn, h1, Rw, acelpautoc, code, y2, 20, cmpl_flag, Opt_AMR_WB );
351 : }
352 0 : else if ( core_brate == ACELP_12k65 )
353 : {
354 0 : acelp_4t64( hBstr, dn, cn, h1, Rw, acelpautoc, code, y2, 36, cmpl_flag, Opt_AMR_WB );
355 : }
356 0 : else if ( core_brate == ACELP_14k25 )
357 : {
358 0 : acelp_4t64( hBstr, dn, cn, h1, Rw, acelpautoc, code, y2, 44, cmpl_flag, Opt_AMR_WB );
359 : }
360 0 : else if ( core_brate == ACELP_15k85 )
361 : {
362 0 : acelp_4t64( hBstr, dn, cn, h1, Rw, acelpautoc, code, y2, 52, cmpl_flag, Opt_AMR_WB );
363 : }
364 0 : else if ( core_brate == ACELP_18k25 )
365 : {
366 0 : acelp_4t64( hBstr, dn, cn, h1, Rw, acelpautoc, code, y2, 64, cmpl_flag, Opt_AMR_WB );
367 : }
368 0 : else if ( core_brate == ACELP_19k85 )
369 : {
370 0 : acelp_4t64( hBstr, dn, cn, h1, Rw, acelpautoc, code, y2, 72, cmpl_flag, Opt_AMR_WB );
371 : }
372 0 : else if ( core_brate == ACELP_23k05 )
373 : {
374 0 : acelp_4t64( hBstr, dn, cn, h1, Rw, acelpautoc, code, y2, 88, cmpl_flag, Opt_AMR_WB );
375 : }
376 0 : else if ( core_brate == ACELP_23k85 )
377 : {
378 0 : acelp_4t64( hBstr, dn, cn, h1, Rw, acelpautoc, code, y2, 88, cmpl_flag, Opt_AMR_WB );
379 : }
380 : }
381 :
382 : /*----------------------------------------------------------------*
383 : * Pitch sharpening
384 : *----------------------------------------------------------------*/
385 :
386 618392 : cb_shape( 1, 1, 0, sharpFlag, 0, g1, g2, p_Aq, code, tilt_code, pt_pitch, L_subfr );
387 :
388 618392 : return;
389 : }
|