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 "rom_com.h"
45 : #include "wmc_auto.h"
46 :
47 : /*-------------------------------------------------------------------*
48 : * encod_amr_wb()
49 : *
50 : * Encode excitation signal in AMR-WB IO mode
51 : *-------------------------------------------------------------------*/
52 :
53 0 : void encod_amr_wb(
54 : Encoder_State *st, /* i/o: state structure */
55 : const float speech[], /* i : input speech */
56 : const float Aw[], /* i : weighted A(z) unquantized for subframes */
57 : const float Aq[], /* i : 12k8 Lp coefficient */
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 : int16_t hf_gain[NB_SUBFR], /* o : decoded HF gain */
64 : const float *speech16k /* i : input speech @16kHz */
65 : )
66 : {
67 : float xn[L_SUBFR]; /* Target vector for pitch search */
68 : float xn2[L_SUBFR]; /* Target vector for codebook search */
69 : float cn[L_SUBFR]; /* Target vector in residual domain */
70 : float h1[L_SUBFR + ( M + 1 )]; /* Impulse response vector */
71 : float code[L_SUBFR]; /* Fixed codebook excitation */
72 : float y1[L_SUBFR]; /* Filtered adaptive excitation */
73 : float y2[L_SUBFR]; /* Filtered algebraic excitation */
74 : float gain_pit; /* Pitch gain */
75 : float voice_fac; /* Voicing factor */
76 : float gain_code; /* Gain of code */
77 : float gain_inov; /* inovation gain */
78 : int16_t i, i_subfr; /* tmp variables */
79 : int16_t T_op[3]; /* pitch period for quantization */
80 : int16_t T0, T0_frac; /* close loop integer pitch and fractional part */
81 : int16_t T0_min, T0_max; /* pitch variables */
82 : float *pt_pitch; /* pointer to floating pitch buffer */
83 : float g_corr[6]; /* ACELP correl, values + gain pitch */
84 : int16_t clip_gain; /* LSF clip gain */
85 : const float *p_Aw, *p_Aq; /* pointer to LP filter coeff. vector*/
86 0 : int16_t unbits = 0;
87 : float norm_gain_code;
88 : int16_t pitch_limit_flag;
89 : int16_t lp_select, lp_flag;
90 :
91 0 : BSTR_ENC_HANDLE hBstr = st->hBstr;
92 0 : LPD_state_HANDLE hLPDmem = st->hLPDmem;
93 :
94 : /*------------------------------------------------------------------*
95 : * Initializations
96 : *------------------------------------------------------------------*/
97 :
98 0 : pitch_limit_flag = 0; /* always restrained pitch Q range in IO mode */
99 0 : T0_max = PIT_MAX;
100 0 : T0_min = PIT_MIN;
101 :
102 0 : p_Aw = Aw;
103 0 : p_Aq = Aq;
104 0 : pt_pitch = pitch_buf;
105 :
106 0 : mvs2s( st->pitch, T_op, 2 );
107 0 : if ( T_op[0] <= PIT_MIN )
108 : {
109 0 : T_op[0] *= 2;
110 : }
111 :
112 0 : if ( T_op[1] <= PIT_MIN )
113 : {
114 0 : T_op[1] *= 2;
115 : }
116 :
117 0 : st->acelp_cfg.fcb_mode = 0; /* flag used in inov_encode() */
118 :
119 : /*-----------------------------------------------------------------*
120 : * Select LP filtering flag
121 : *-----------------------------------------------------------------*/
122 :
123 0 : if ( st->core_brate < ACELP_11k60 )
124 : {
125 0 : lp_flag = LOW_PASS;
126 : }
127 : else
128 : {
129 0 : lp_flag = NORMAL_OPERATION;
130 : }
131 :
132 : /*------------------------------------------------------------------*
133 : * ACELP subframe loop
134 : *------------------------------------------------------------------*/
135 :
136 0 : for ( i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR )
137 : {
138 : /*----------------------------------------------------------------*
139 : * Bandwidth expansion of A(z) filter coefficients
140 : * Find the the excitation search target "xn" and innovation
141 : * target in residual domain "cn"
142 : * Compute impulse response, h1[], of weighted synthesis filter
143 : *----------------------------------------------------------------*/
144 :
145 0 : mvr2r( &res[i_subfr], &exc[i_subfr], L_SUBFR );
146 :
147 0 : find_targets( speech, hLPDmem->mem_syn, i_subfr, &hLPDmem->mem_w0, p_Aq, res, L_SUBFR, p_Aw, TILT_FAC, xn, cn, h1 );
148 :
149 : /*----------------------------------------------------------------*
150 : * Close-loop pitch search and quantization
151 : * Adaptive exc. construction
152 : *----------------------------------------------------------------*/
153 :
154 0 : *pt_pitch = pit_encode( hBstr, st->acelp_cfg.pitch_bits, st->core_brate, 1, L_FRAME, -1, &pitch_limit_flag, i_subfr, exc, L_SUBFR, T_op, &T0_min, &T0_max, &T0, &T0_frac, h1, xn, 0 /*tdm_Pitch_reuse_flag*/, NULL /*tdm_Pri_pitch_buf*/ );
155 :
156 : /*-----------------------------------------------------------------*
157 : * Find adaptive exitation
158 : *-----------------------------------------------------------------*/
159 :
160 0 : pred_lt4( &exc[i_subfr], &exc[i_subfr], T0, T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
161 :
162 : /*-----------------------------------------------------------------*
163 : * Gain clipping test to avoid unstable synthesis on frame erasure
164 : * or in case of floating point encoder & fixed p. decoder
165 : *-----------------------------------------------------------------*/
166 :
167 0 : clip_gain = gp_clip( st->element_mode, st->core_brate, st->voicing, i_subfr, 0, xn, st->clip_var );
168 :
169 : /*-----------------------------------------------------------------*
170 : * LP filtering of the adaptive excitation, codebook target computation
171 : *-----------------------------------------------------------------*/
172 :
173 0 : lp_select = lp_filt_exc_enc( MODE1, -1, i_subfr, exc, h1, xn, y1, xn2, L_SUBFR, L_FRAME, g_corr, clip_gain, &gain_pit, &lp_flag );
174 :
175 0 : if ( lp_flag == NORMAL_OPERATION )
176 : {
177 0 : push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
178 : }
179 :
180 : /*-----------------------------------------------------------------*
181 : * Innovation encoding
182 : *-----------------------------------------------------------------*/
183 :
184 0 : inov_encode( st, st->core_brate, 1, L_FRAME, st->last_L_frame, -1, -1, 0, i_subfr, -1, p_Aq, gain_pit, cn, exc, h1, hLPDmem->tilt_code, *pt_pitch, xn2, code, y2, &unbits, L_SUBFR );
185 :
186 : /*-----------------------------------------------------------------*
187 : * Gain encoding
188 : * Pitch gain clipping test
189 : * Estimate spectrum tilt and voicing
190 : *-----------------------------------------------------------------*/
191 :
192 0 : gain_enc_amr_wb( hBstr, xn, y1, y2, code, st->core_brate, &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, clip_gain, st->hAmrwb_IO->past_qua_en );
193 :
194 0 : gp_clip_test_gain_pit( st->element_mode, st->core_brate, gain_pit, st->clip_var );
195 :
196 0 : hLPDmem->tilt_code = est_tilt( exc + i_subfr, gain_pit, code, gain_code, &voice_fac, L_SUBFR, 0 );
197 :
198 : /*-----------------------------------------------------------------*
199 : * Update memory of the weighting filter
200 : *-----------------------------------------------------------------*/
201 :
202 0 : hLPDmem->mem_w0 = xn[L_SUBFR - 1] - gain_pit * y1[L_SUBFR - 1] - gain_code * y2[L_SUBFR - 1];
203 :
204 : /*-----------------------------------------------------------------*
205 : * Find the total excitation
206 : *-----------------------------------------------------------------*/
207 :
208 0 : for ( i = 0; i < L_SUBFR; i++ )
209 : {
210 0 : exc2[i + i_subfr] = gain_pit * exc[i + i_subfr];
211 0 : exc[i + i_subfr] = exc2[i + i_subfr] + gain_code * code[i];
212 : }
213 :
214 : /*-----------------------------------------------------------------*
215 : * Synthesize speech to update mem_syn[]
216 : * Update A(z) filters
217 : *-----------------------------------------------------------------*/
218 :
219 0 : syn_filt( p_Aq, M, &exc[i_subfr], &syn[i_subfr], L_SUBFR, hLPDmem->mem_syn, 1 );
220 :
221 : /*-----------------------------------------------------------------*
222 : * HF gain modification factors at 23.85 kbps
223 : *-----------------------------------------------------------------*/
224 :
225 0 : if ( st->core_brate == ACELP_23k85 )
226 : {
227 0 : if ( st->input_Fs >= 16000 )
228 : {
229 0 : hf_cod( st->core_brate, &speech16k[i_subfr * L_SUBFR16k / L_SUBFR], p_Aq, &exc[i_subfr], &syn[i_subfr], &st->hAmrwb_IO->seed2_enc, st->hAmrwb_IO->mem_hp400_enc, st->hAmrwb_IO->mem_syn_hf_enc, st->hAmrwb_IO->mem_hf_enc, st->hAmrwb_IO->mem_hf2_enc, &st->hVAD->hangover_cnt, &st->hAmrwb_IO->gain_alpha, &hf_gain[i_subfr / L_SUBFR] );
230 : }
231 :
232 0 : push_indice( hBstr, IND_HF_GAIN_MODIFICATION, hf_gain[i_subfr / L_SUBFR], 4 );
233 : }
234 :
235 0 : p_Aw += ( M + 1 );
236 0 : p_Aq += ( M + 1 );
237 0 : pt_pitch++;
238 : }
239 :
240 0 : return;
241 : }
|