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 : #include "ivas_cnst.h"
36 : #include "prot_fx.h"
37 : #include "ivas_stat_enc.h"
38 : #include "wmc_auto.h"
39 : #include "ivas_prot_fx.h"
40 : #include "prot_fx_enc.h"
41 : #ifdef DEBUGGING
42 : #include "debug.h"
43 : #endif
44 :
45 :
46 : /*-------------------------------------------------------------------*
47 : * ivas_ism_enc()
48 : *
49 : * ISM CoreCoders encoding routine
50 : *-------------------------------------------------------------------*/
51 128394 : ivas_error ivas_ism_enc_fx(
52 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
53 : Word32 *data[], /* i : input signal [channels][samples] Q(q_data)*/
54 : Word16 q_data, /* i : Q-factor of input signal */
55 : const Word16 input_frame, /* i : input frame length per channel Q0*/
56 : Word16 *nb_bits_metadata, /* i : number of metadata bits Q0*/
57 : const Word16 flag_omasa_ener_brate /* i : less bitrate for objects in OMASA flag Q0*/
58 : )
59 : {
60 : SCE_ENC_HANDLE hSCE;
61 : Encoder_State *st;
62 : Word16 sce_id;
63 : Word16 old_inp_12k8_fx[MAX_NUM_OBJECTS][1][L_INP_12k8]; /* buffer of input signal @ 12k8 Q_new-1 */
64 : Word16 old_inp_16k_fx[MAX_NUM_OBJECTS][1][L_INP]; /* buffer of input signal @ 12k8 Q_new-1 */
65 128394 : Word16 Q_new[MAX_NUM_OBJECTS][1] = { 0 };
66 : Word16 vad_flag[MAX_NUM_OBJECTS]; /* VAD flag */
67 : Word32 ener_fx[MAX_NUM_OBJECTS][1]; /* residual energy from Levinson-Durbin Q6 */
68 : Word16 relE_fx[MAX_NUM_OBJECTS][1];
69 : /* frame relative energy, Q8 */
70 128394 : Word16 A_fx[MAX_NUM_OBJECTS][1][NB_SUBFR16k * ( M + 1 )] = { 0 }; /* A(z) unquantized for subframes */
71 128394 : Word16 Aw_fx[MAX_NUM_OBJECTS][1][NB_SUBFR16k * ( M + 1 )] = { 0 }; /* weighted A(z) unquantized for subframes */
72 : Word32 epsP_fx[MAX_NUM_OBJECTS][1][M + 1]; /* LP prediction errors */
73 : Word16 epsP_fx_q[MAX_NUM_OBJECTS][1];
74 : Word16 lsp_new_fx[MAX_NUM_OBJECTS][1][M]; /* LSPs at the end of the frame Q15 */
75 : Word16 lsp_mid_fx[MAX_NUM_OBJECTS][1][M]; /* ISPs in the middle of the frame Q15 */
76 : Word16 vad_hover_flag[MAX_NUM_OBJECTS][1]; /* VAD hangover flag */
77 : Word16 attack_flag[MAX_NUM_OBJECTS][1]; /* attack flag (GSC or TC) */
78 : Word32 realBuffer_fx[MAX_NUM_OBJECTS][1][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* real buffer */
79 : Word32 imagBuffer_fx[MAX_NUM_OBJECTS][1][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* imag buffer */
80 : Word16 q_re_im_buf[MAX_NUM_OBJECTS];
81 : Word16 old_wsp_fx[MAX_NUM_OBJECTS][1][L_WSP]; /* old weighted input signal */
82 : Word16 q_old_wsp;
83 : Word16 e_old_wsp[MAX_NUM_OBJECTS][CPE_CHANNELS];
84 : Word16 pitch_fr_fx[MAX_NUM_OBJECTS][1][NB_SUBFR]; /* fractional pitch values Q6 */
85 : Word16 voicing_fr_fx[MAX_NUM_OBJECTS][1][NB_SUBFR]; /* fractional pitch gains Q15 */
86 : Word16 loc_harm[MAX_NUM_OBJECTS][1]; /* harmonicity flag */
87 : Word16 cor_map_sum_fx[MAX_NUM_OBJECTS][1]; /* speech/music clasif. parameter Q8 */
88 : Word16 vad_flag_dtx[MAX_NUM_OBJECTS][1]; /* HE-SAD flag with additional DTX HO */
89 : Word32 enerBuffer_fx[MAX_NUM_OBJECTS][1][CLDFB_NO_CHANNELS_MAX]; /* energy buffer */
90 : Word16 enerBuffer_fx_exp[MAX_NUM_OBJECTS][1]; /* energy buffer */
91 : Word32 currFlatness_fx[1]; /* flatness parameter */
92 : Word16 fft_buff_fx[MAX_NUM_OBJECTS][1][2 * L_FFT]; /* FFT buffer */
93 : Word16 fft_buff_fx_q[MAX_NUM_OBJECTS][1]; /* FFT buffer */
94 : Word32 fr_bands_fx[1][2 * NB_BANDS]; /* energy in frequency bands */
95 : Word16 Etot_LR_fx[1]; /* total energy; correlation shift Q8 */
96 : Word32 lf_E_fx[1][2 * VOIC_BINS]; /* per bin spectrum energy in lf */
97 : Word16 localVAD_HE_SAD[1]; /* local HE VAD */
98 : Word16 nchan_ism, dtx_flag, sid_flag, flag_noisy_speech;
99 : Word16 md_diff_flag[MAX_NUM_OBJECTS];
100 128394 : Encoder_State *prev_st = NULL;
101 : Word32 ism_total_brate_ref, ism_total_brate;
102 : Word16 i, nchan_transport_ism;
103 : Word16 q_st_inp_16;
104 : ivas_error error;
105 128394 : move16(); // Q_new
106 128394 : move16(); // Q_new
107 128394 : move16(); // Q_new
108 128394 : move16(); // Q_new
109 :
110 128394 : push_wmops( "ivas_ism_enc" );
111 :
112 : /*------------------------------------------------------------------*
113 : * Initialization
114 : *-----------------------------------------------------------------*/
115 :
116 128394 : error = IVAS_ERR_OK;
117 128394 : move32();
118 :
119 641970 : FOR( i = 0; i < MAX_NUM_OBJECTS; i++ )
120 : {
121 513576 : set16_zero_fx( old_inp_12k8_fx[i][0], L_INP_12k8 );
122 : }
123 :
124 128394 : set16_fx( q_re_im_buf, 0, MAX_NUM_OBJECTS );
125 :
126 128394 : dtx_flag = 0;
127 128394 : sid_flag = 0;
128 128394 : flag_noisy_speech = 0;
129 128394 : move16();
130 128394 : move16();
131 128394 : move16();
132 :
133 128394 : nchan_ism = st_ivas->hEncoderConfig->nchan_ism;
134 128394 : move16();
135 128394 : set16_fx( md_diff_flag, 1, nchan_ism );
136 :
137 128394 : nchan_transport_ism = st_ivas->nchan_transport; /* Q0 */
138 128394 : move16();
139 128394 : IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) )
140 : {
141 10152 : nchan_transport_ism = 1;
142 10152 : nchan_ism = 1;
143 10152 : move16();
144 10152 : move16();
145 : }
146 118242 : ELSE IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
147 : {
148 12916 : nchan_transport_ism = st_ivas->hEncoderConfig->nchan_ism; /* Q0 */
149 12916 : move16();
150 : }
151 451102 : FOR( sce_id = 0; sce_id < nchan_transport_ism; sce_id++ )
152 : {
153 322708 : set16_fx( old_inp_16k_fx[sce_id][0], 0, L_INP );
154 : }
155 :
156 : /*------------------------------------------------------------------*
157 : * Preprocesing
158 : *-----------------------------------------------------------------*/
159 :
160 451102 : FOR( sce_id = 0; sce_id < nchan_transport_ism; sce_id++ )
161 : {
162 322708 : hSCE = st_ivas->hSCE[sce_id];
163 322708 : st = hSCE->hCoreCoder[0];
164 :
165 : /*------------------------------------------------------------------*
166 : * Initialization - general
167 : *-----------------------------------------------------------------*/
168 322708 : Word16 Q_min = s_min( q_data, add( st->q_inp32, L_norm_arr( st->input32_fx - input_frame, input_frame ) ) );
169 322708 : scale_sig32( st->input32_fx - input_frame, input_frame, sub( Q_min, st->q_inp32 ) );
170 322708 : st->q_old_inp32 = Q_min;
171 322708 : move16();
172 322708 : Copy_Scale_sig32( data[sce_id], st->input32_fx, input_frame, sub( Q_min, q_data ) ); // Q(Q_min)
173 322708 : st->q_inp32 = Q_min;
174 322708 : move16();
175 322708 : q_st_inp_16 = sub( getScaleFactor32( st->input32_fx, input_frame ), 16 );
176 322708 : Copy_Scale_sig_32_16( st->input32_fx, st->input_fx, input_frame, q_st_inp_16 ); // Q_min -> Q(q_inp + Q_min)
177 322708 : st->q_inp = add( q_st_inp_16, Q_min );
178 322708 : move16();
179 :
180 322708 : st->element_mode = IVAS_SCE;
181 322708 : move16();
182 :
183 5486036 : FOR( i = 0; i < CLDFB_NO_COL_MAX; i++ )
184 : {
185 5163328 : set_zero_fx( realBuffer_fx[sce_id][0][i], CLDFB_NO_CHANNELS_MAX );
186 5163328 : set_zero_fx( imagBuffer_fx[sce_id][0][i], CLDFB_NO_CHANNELS_MAX );
187 : }
188 : /*------------------------------------------------------------------*
189 : * SCE initialization - core coder
190 : *-----------------------------------------------------------------*/
191 :
192 322708 : st->idchan = 0;
193 322708 : st->core = -1;
194 322708 : st->core_brate = -1; /* updated in dtx() */
195 322708 : st->max_bwidth = st_ivas->hEncoderConfig->max_bwidth;
196 322708 : st->input_bwidth = st->last_input_bwidth; /* updated in BWD */
197 322708 : st->bwidth = st->last_bwidth; /* updated in BWD */
198 322708 : st->rate_switching_reset = 0;
199 : #ifdef DEBUGGING
200 : st->force = st_ivas->hEncoderConfig->force;
201 : st->id_element = sce_id;
202 : #ifdef DEBUG_FORCE_DIR
203 : st->force_dir = st_ivas->hEncoderConfig->force_dir;
204 : #endif
205 : #endif
206 322708 : move16();
207 322708 : move16();
208 322708 : move16();
209 322708 : move16();
210 322708 : move16();
211 322708 : move16();
212 322708 : move16();
213 :
214 : /*---------------------------------------------------------------*
215 : * Time Domain Transient Detector
216 : *---------------------------------------------------------------*/
217 :
218 322708 : RunTransientDetection_ivas_fx( st->input_fx, input_frame, st->hTranDet, st->q_inp );
219 :
220 322708 : currFlatness_fx[0] = GetTCXAvgTemporalFlatnessMeasure_ivas_fx( st->hTranDet, NSUBBLOCKS, 0 ); // Q21
221 322708 : move16();
222 :
223 : /*----------------------------------------------------------------*
224 : * Configuration of core encoder
225 : *----------------------------------------------------------------*/
226 :
227 : /* Force to MODE1 in IVAS */
228 322708 : st->codec_mode = MODE1;
229 322708 : move16();
230 :
231 322708 : st->bits_frame_nominal = sub( extract_l( Mpy_32_32( hSCE->element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) ), ISM_NB_BITS_METADATA_NOMINAL );
232 322708 : move16();
233 :
234 : #ifdef DEBUG_MODE_INFO
235 : dbgwrite( &st->element_mode, sizeof( int16_t ), 1, input_frame, fname( debug_dir, "element_mode", 0, st->id_element, ENC ) );
236 : #endif
237 :
238 : /*----------------------------------------------------------------*
239 : * Front Pre-processing
240 : *----------------------------------------------------------------*/
241 1724916 : FOR( i = 0; i < hSCE->hCoreCoder[0]->nb_subfr; i++ )
242 : {
243 1402208 : Scale_sig( &A_fx[sce_id][0][i * ( M + 1 )], M + 1, sub( norm_s( A_fx[sce_id][0][i * ( M + 1 )] ), 2 ) ); // scaling to Q12
244 : }
245 :
246 322708 : set16_fx( old_wsp_fx[sce_id][0], 0, L_WSP );
247 322708 : q_old_wsp = Q15;
248 322708 : move16();
249 :
250 : Word16 q_fr_bands[2];
251 322708 : error = pre_proc_front_ivas_fx( hSCE, NULL, hSCE->element_brate, nb_bits_metadata[sce_id], input_frame, 0, old_inp_12k8_fx[sce_id][0], old_inp_16k_fx[sce_id][0],
252 322708 : &ener_fx[sce_id][0], &relE_fx[sce_id][0], A_fx[sce_id][0], Aw_fx[sce_id][0], epsP_fx[sce_id][0], &epsP_fx_q[sce_id][0], lsp_new_fx[sce_id][0], lsp_mid_fx[sce_id][0], &vad_hover_flag[sce_id][0], &attack_flag[sce_id][0],
253 322708 : realBuffer_fx[sce_id][0], imagBuffer_fx[sce_id][0], &q_re_im_buf[sce_id], old_wsp_fx[sce_id][0], &q_old_wsp, pitch_fr_fx[sce_id][0], voicing_fr_fx[sce_id][0], &loc_harm[sce_id][0], &cor_map_sum_fx[sce_id][0], &vad_flag_dtx[sce_id][0], enerBuffer_fx[sce_id][0], &enerBuffer_fx_exp[sce_id][0],
254 322708 : fft_buff_fx[sce_id][0], &fft_buff_fx_q[sce_id][0], A_fx[sce_id][0], lsp_new_fx[sce_id][0], currFlatness_fx[0], 0, fr_bands_fx, q_fr_bands, Etot_LR_fx, lf_E_fx, 31, localVAD_HE_SAD, NULL, 31, 0, 0, 0, 0, ISM_FORMAT, 0, st_ivas->hEncoderConfig->last_ivas_total_brate, st_ivas->hEncoderConfig->ivas_total_brate, &Q_new[sce_id][0]
255 : #ifdef DEBUG_MODE_INFO
256 : ,
257 : st->id_element
258 : #endif
259 : );
260 322708 : e_old_wsp[sce_id][0] = sub( Q15, q_old_wsp );
261 322708 : move16();
262 322708 : IF( error != IVAS_ERR_OK )
263 : {
264 0 : return error;
265 : }
266 :
267 322708 : IF( st_ivas->hEncoderConfig->Opt_DTX_ON )
268 : {
269 43516 : vad_flag[sce_id] = vad_flag_dtx[sce_id][0];
270 43516 : move16();
271 : }
272 : ELSE
273 : {
274 279192 : vad_flag[sce_id] = st->vad_flag; /* Q0 */
275 279192 : move16();
276 : }
277 : }
278 :
279 : /*------------------------------------------------------------------*
280 : * DTX analysis
281 : *-----------------------------------------------------------------*/
282 :
283 : /* compute the dominant sce_id using long term energy */
284 :
285 128394 : IF( st_ivas->hEncoderConfig->Opt_DTX_ON )
286 : {
287 16326 : ivas_ism_get_sce_id_dtx_fx( st_ivas->hISMDTX, st_ivas->hSCE, st_ivas->nchan_transport, input_frame );
288 :
289 16326 : dtx_flag = ivas_ism_dtx_enc_fx( st_ivas->hISMDTX, st_ivas->hSCE, st_ivas->hEncoderConfig->ivas_total_brate, nchan_ism, st_ivas->nchan_transport, vad_flag, st_ivas->hIsmMetaData, md_diff_flag, &sid_flag ); /* Q0 */
290 :
291 16326 : IF( sid_flag )
292 : {
293 : /* estimate coherence between objects */
294 542 : ivas_ism_coh_estim_dtx_enc_fx( st_ivas->hISMDTX, st_ivas->hSCE, st_ivas->nchan_transport, input_frame );
295 : }
296 : }
297 :
298 : /*------------------------------------------------------------------*
299 : * Analysis of objects, configuration and decision about bitrates per channel
300 : * Metadata quantization and encoding
301 : *-----------------------------------------------------------------*/
302 :
303 128394 : IF( EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
304 : {
305 21020 : ivas_param_ism_compute_noisy_speech_flag_fx( st_ivas );
306 21020 : flag_noisy_speech = st_ivas->hParamIsm->flag_noisy_speech; // Q0
307 21020 : move16();
308 : }
309 :
310 128394 : IF( dtx_flag )
311 : {
312 2117 : ivas_ism_metadata_sid_enc_fx( st_ivas->hISMDTX, flag_noisy_speech, nchan_ism, st_ivas->nchan_transport, st_ivas->ism_mode, st_ivas->hIsmMetaData, sid_flag, md_diff_flag, st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData, nb_bits_metadata );
313 : }
314 126277 : ELSE IF( EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
315 : {
316 20713 : IF( ( error = ivas_ism_metadata_enc_fx( &st_ivas->hEncoderConfig->ivas_total_brate, nchan_ism, nchan_transport_ism, st_ivas->hIsmMetaData, st_ivas->hSCE, st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData, nb_bits_metadata, vad_flag, st_ivas->ism_mode, st_ivas->hParamIsm, st_ivas->hEncoderConfig->ism_extended_metadata_flag, -256 /* -1.0f in Q8 */, 0, NULL, st_ivas->hSCE[0]->hCoreCoder[0]->ini_frame ) ) != IVAS_ERR_OK )
317 : {
318 0 : return error;
319 : }
320 : }
321 : ELSE /* ISM_MODE_DISC */
322 : {
323 105564 : test();
324 105564 : IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) )
325 : {
326 23068 : ism_total_brate = 0;
327 23068 : move16();
328 71440 : FOR( i = 0; i < st_ivas->nSCE; i++ )
329 : {
330 48372 : ism_total_brate = L_add( ism_total_brate, st_ivas->hSCE[i]->element_brate ); /* Q0 */
331 : }
332 : }
333 : ELSE
334 : {
335 82496 : ism_total_brate = st_ivas->hEncoderConfig->ivas_total_brate; /* Q0 */
336 82496 : move32();
337 : }
338 :
339 105564 : ism_total_brate_ref = ism_total_brate; /* Q0 */
340 105564 : move32();
341 :
342 : Word16 lp_noise_CPE_fx;
343 105564 : if ( st_ivas->hMasa != NULL )
344 : {
345 23068 : lp_noise_CPE_fx = st_ivas->hMasa->data.hOmasaData->lp_noise_CPE_fx;
346 23068 : move16();
347 : }
348 : else
349 : {
350 82496 : lp_noise_CPE_fx = 0;
351 82496 : move16();
352 : }
353 :
354 : Word16 *omasa_stereo_sw_cnt;
355 105564 : if ( st_ivas->hMasa != NULL )
356 : {
357 23068 : omasa_stereo_sw_cnt = &( st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt );
358 : }
359 : else
360 : {
361 82496 : omasa_stereo_sw_cnt = NULL;
362 : }
363 :
364 105564 : IF( ( error = ivas_ism_metadata_enc_fx( &ism_total_brate, nchan_ism, nchan_transport_ism, st_ivas->hIsmMetaData, st_ivas->hSCE, st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData,
365 : nb_bits_metadata, vad_flag, st_ivas->ism_mode, NULL, st_ivas->hEncoderConfig->ism_extended_metadata_flag, lp_noise_CPE_fx, flag_omasa_ener_brate, omasa_stereo_sw_cnt, st_ivas->hSCE[0]->hCoreCoder[0]->ini_frame ) ) != IVAS_ERR_OK )
366 : {
367 0 : return error;
368 : }
369 :
370 105564 : IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) )
371 : {
372 23068 : st_ivas->hCPE[0]->brate_surplus = L_sub( ism_total_brate_ref, ism_total_brate ); /* Q0 */
373 23068 : move32();
374 : }
375 : }
376 128394 : update_last_metadata_fx( nchan_ism, st_ivas->hIsmMetaData, md_diff_flag );
377 :
378 : /*----------------------------------------------------------------*
379 : * Write IVAS format signaling in SID frames
380 : *----------------------------------------------------------------*/
381 :
382 128394 : st = st_ivas->hSCE[0]->hCoreCoder[0];
383 :
384 128394 : IF( sid_flag )
385 : {
386 :
387 542 : ivas_write_format_sid_fx( st_ivas->hEncoderConfig->ivas_format, IVAS_SCE, st->hBstr, -1, -1 );
388 : }
389 :
390 : /*only metadata encoding is needed for this case*/
391 128394 : IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) )
392 : {
393 0 : assert( st_ivas->ism_mode != ISM_MODE_NONE );
394 0 : return error;
395 : }
396 :
397 : /*------------------------------------------------------------------*
398 : * CoreCoders encoding
399 : *-----------------------------------------------------------------*/
400 :
401 451102 : FOR( sce_id = 0; sce_id < nchan_transport_ism; sce_id++ )
402 : {
403 322708 : hSCE = st_ivas->hSCE[sce_id];
404 322708 : st = hSCE->hCoreCoder[0];
405 :
406 : /* update pointer to the buffer of indices of the next channel */
407 322708 : IF( sce_id > 0 )
408 : {
409 194314 : st->hBstr->ind_list = prev_st->hBstr->ind_list + prev_st->hBstr->nb_ind_tot;
410 : }
411 :
412 322708 : if ( st->low_rate_mode )
413 : {
414 8174 : st->bwidth = WB;
415 8174 : move16();
416 : }
417 :
418 : /*----------------------------------------------------------------*
419 : * Core codec configuration
420 : *----------------------------------------------------------------*/
421 :
422 : /* IGF reconfiguration */
423 322708 : test();
424 322708 : IF( NE_32( hSCE->last_element_brate, hSCE->element_brate ) || NE_32( st->last_bwidth, st->bwidth ) )
425 : {
426 : Word16 igf;
427 6527 : igf = getIgfPresent_fx( st->element_mode, L_mult0( st->bits_frame_nominal, FRAMES_PER_SEC ), st->max_bwidth, st->rf_mode );
428 6527 : IF( ( error = IGF_Reconfig_fx( &st->hIGFEnc, igf, 0, L_mult0( st->bits_frame_nominal, FRAMES_PER_SEC ), st->max_bwidth, st->element_mode, st->rf_mode ) ) != IVAS_ERR_OK )
429 : {
430 0 : return error;
431 : }
432 : }
433 :
434 : /* set ACELP@12k8 / ACELP@16k flag for flexible ACELP core */
435 322708 : test();
436 322708 : IF( EQ_32( st->core_brate, SID_2k40 ) || st->core_brate == FRAME_NO_DATA )
437 : {
438 5211 : st->flag_ACELP16k = set_ACELP_flag_IVAS( IVAS_SCE, hSCE->element_brate, st->core_brate, 0, 0, -1, -1 );
439 5211 : move16();
440 : }
441 317497 : ELSE IF( st->low_rate_mode )
442 : {
443 8174 : st->flag_ACELP16k = 0;
444 8174 : move16();
445 : }
446 : ELSE
447 : {
448 309323 : st->flag_ACELP16k = set_ACELP_flag_IVAS( IVAS_SCE, hSCE->element_brate, st->total_brate, 0, 0, -1, -1 ); /* Q0 */
449 309323 : move16();
450 : }
451 :
452 : /* modify the coder_type depending on the total_brate per channel */
453 322708 : coder_type_modif_ivas_fx( st, relE_fx[sce_id][0] );
454 :
455 : /*----------------------------------------------------------------*
456 : * Encoder
457 : *----------------------------------------------------------------*/
458 322708 : test();
459 322708 : test();
460 322708 : IF( !dtx_flag || ( dtx_flag && EQ_16( sce_id, st_ivas->hISMDTX->sce_id_dtx ) ) )
461 : {
462 319614 : Scale_sig( old_wsp_fx[sce_id][0], L_WSP, sub( e_old_wsp[sce_id][0], Q16 ) ); /* exp(16) */
463 319614 : e_old_wsp[sce_id][0] = 16;
464 319614 : move16();
465 319614 : IF( NE_32( ( error = ivas_core_enc_fx( hSCE, NULL, NULL, 1, old_inp_12k8_fx[sce_id], old_inp_16k_fx[sce_id], Q_new[sce_id], ener_fx[sce_id], A_fx[sce_id], Aw_fx[sce_id], epsP_fx[sce_id], epsP_fx_q[sce_id], lsp_new_fx[sce_id], lsp_mid_fx[sce_id], vad_hover_flag[sce_id], attack_flag[sce_id], realBuffer_fx[sce_id], imagBuffer_fx[sce_id], &q_re_im_buf[sce_id], old_wsp_fx[sce_id], e_old_wsp[sce_id], loc_harm[sce_id], cor_map_sum_fx[sce_id], vad_flag_dtx[sce_id], enerBuffer_fx[sce_id], enerBuffer_fx_exp[sce_id], fft_buff_fx[sce_id], 0, ISM_FORMAT, 0 ) ), IVAS_ERR_OK ) )
466 : {
467 0 : return error;
468 : }
469 : }
470 :
471 : /*----------------------------------------------------------------*
472 : * Common updates
473 : *----------------------------------------------------------------*/
474 :
475 : /* update input samples buffer */
476 :
477 322708 : Copy( st->input_fx, st->old_input_signal_fx, input_frame ); /* st->q_inp */
478 322708 : st->q_old_inp = st->q_inp;
479 322708 : move16();
480 322708 : Copy32( st->input32_fx, st->old_input_signal32_fx, input_frame ); /* st->q_inp32 */
481 322708 : st->q_old_inp32 = st->q_inp32;
482 322708 : move16();
483 :
484 322708 : hSCE->last_element_brate = hSCE->element_brate; /* Q0 */
485 322708 : move32();
486 :
487 : /* Store previous attack detection flag */
488 322708 : st->hTranDet->transientDetector.prev_bIsAttackPresent = st->hTranDet->transientDetector.bIsAttackPresent; /* Q0 */
489 322708 : move16();
490 :
491 322708 : prev_st = st;
492 : }
493 :
494 128394 : IF( dtx_flag )
495 : {
496 7328 : FOR( sce_id = 0; sce_id < nchan_transport_ism; sce_id++ )
497 : {
498 5211 : IF( NE_16( sce_id, st_ivas->hISMDTX->sce_id_dtx ) )
499 : {
500 3094 : st_ivas->hSCE[sce_id]->hCoreCoder[0]->last_core = st_ivas->hSCE[st_ivas->hISMDTX->sce_id_dtx]->hCoreCoder[0]->last_core; /* Q0 */
501 3094 : st_ivas->hSCE[sce_id]->hCoreCoder[0]->last_core_brate = st_ivas->hSCE[st_ivas->hISMDTX->sce_id_dtx]->hCoreCoder[0]->core_brate; /* Q0 */
502 3094 : st_ivas->hSCE[sce_id]->hCoreCoder[0]->last_L_frame = st_ivas->hSCE[st_ivas->hISMDTX->sce_id_dtx]->hCoreCoder[0]->last_L_frame; /* Q0 */
503 3094 : move16();
504 3094 : move32();
505 3094 : move16();
506 : }
507 : }
508 : }
509 :
510 : #ifdef DEBUG_MODE_INFO
511 : for ( sce_id = 0; sce_id < nchan_transport_ism; sce_id++ )
512 : {
513 : float tmpF;
514 : int16_t id;
515 :
516 : st = st_ivas->hSCE[sce_id]->hCoreCoder[0];
517 : id = st->id_element;
518 :
519 : tmpF = st->element_brate / 1000.0f;
520 : dbgwrite( &tmpF, sizeof( float ), 1, input_frame, fname( debug_dir, "element_brate", 0, id, ENC ) );
521 : }
522 : #endif
523 128394 : pop_wmops();
524 :
525 128394 : return error;
526 : }
527 :
528 : /*-------------------------------------------------------------------------
529 : * ivas_ism_enc_config()
530 : *
531 : * - select ISM format mode
532 : * - reconfigure the ISM format encoder
533 : *-------------------------------------------------------------------------*/
534 :
535 105326 : ivas_error ivas_ism_enc_config(
536 : Encoder_Struct *st_ivas /* i/o: IVAS encoder structure */
537 : )
538 : {
539 : ivas_error error;
540 : ISM_MODE last_ism_mode;
541 : Word16 nchan_transport_old;
542 : Word16 nSCE_old, nCPE_old;
543 :
544 105326 : error = IVAS_ERR_OK;
545 105326 : move32();
546 105326 : last_ism_mode = st_ivas->ism_mode;
547 105326 : move16();
548 :
549 : /* select ISM format mode */
550 105326 : st_ivas->ism_mode = ivas_ism_mode_select( st_ivas->hEncoderConfig->nchan_inp, st_ivas->hEncoderConfig->ivas_total_brate ); /* Q0 */
551 105326 : move16();
552 :
553 : /* ISM bit-rate switching */
554 105326 : test();
555 105326 : IF( NE_16( st_ivas->ism_mode, last_ism_mode ) || NE_32( st_ivas->hEncoderConfig->ivas_total_brate, st_ivas->hEncoderConfig->last_ivas_total_brate ) )
556 : {
557 : Word32 element_brate_tmp[MAX_NUM_OBJECTS];
558 :
559 1087 : nchan_transport_old = st_ivas->nchan_transport; /* Q0 */
560 1087 : move16();
561 :
562 : /* Reset and Initialize */
563 1087 : IF( EQ_16( st_ivas->ism_mode, ISM_MODE_PARAM ) )
564 : {
565 341 : st_ivas->nchan_transport = MAX_PARAM_ISM_WAVE;
566 : }
567 : ELSE
568 : {
569 746 : st_ivas->nchan_transport = st_ivas->hEncoderConfig->nchan_inp; /* Q0 */
570 : }
571 1087 : move16();
572 :
573 1087 : nCPE_old = st_ivas->nCPE; /* Q0 */
574 1087 : move16();
575 1087 : nSCE_old = st_ivas->nSCE; /* Q0 */
576 1087 : move16();
577 1087 : st_ivas->nSCE = st_ivas->nchan_transport; /* Q0 */
578 1087 : move16();
579 1087 : st_ivas->nCPE = 0;
580 1087 : move16();
581 :
582 1087 : IF( ( error = ivas_ism_config_fx( st_ivas->hEncoderConfig->ivas_total_brate, st_ivas->nchan_transport, st_ivas->hEncoderConfig->nchan_inp, NULL, 0, NULL, NULL, element_brate_tmp, NULL, NULL, 0 ) ) != IVAS_ERR_OK )
583 : {
584 0 : return error;
585 : }
586 :
587 1087 : IF( ( error = ivas_corecoder_enc_reconfig_fx( st_ivas, nSCE_old, nCPE_old, nchan_transport_old, st_ivas->hEncoderConfig->ivas_total_brate / st_ivas->nchan_transport, imult3216( ( st_ivas->hEncoderConfig->ivas_total_brate / st_ivas->nchan_transport ), CPE_CHANNELS ), MC_MODE_NONE ) ) != IVAS_ERR_OK )
588 : {
589 0 : return error;
590 : }
591 :
592 1087 : test();
593 1087 : IF( EQ_16( st_ivas->ism_mode, ISM_MODE_PARAM ) && EQ_16( last_ism_mode, ISM_MODE_DISC ) )
594 : {
595 : /* Allocate and Initialize the memory used by ParamISM when switch from Discrete ISM */
596 330 : IF( ( error = ivas_param_ism_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
597 : {
598 0 : return error;
599 : }
600 : }
601 :
602 1087 : test();
603 1087 : IF( EQ_16( st_ivas->ism_mode, ISM_MODE_DISC ) && EQ_16( last_ism_mode, ISM_MODE_PARAM ) )
604 : {
605 : /* Deallocate the memory used by ParamISM when switch to Discrete ISM */
606 333 : ivas_param_ism_enc_close_fx( &( st_ivas->hParamIsm ), st_ivas->hEncoderConfig->input_Fs );
607 : }
608 : }
609 :
610 105326 : return error;
611 : }
|