Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2026 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 "cnst.h"
36 : #include "ivas_cnst.h"
37 : #include "rom_com.h"
38 : #include "prot.h"
39 : #include "ivas_prot.h"
40 : #include "ivas_rom_com.h"
41 : #ifdef DEBUGGING
42 : #include "debug.h"
43 : #endif
44 : #include "wmc_auto.h"
45 :
46 :
47 : /*-------------------------------------------------------------------*
48 : * ivas_sce_enc()
49 : *
50 : * Single Channel Element (SCE) encoding routine
51 : *-------------------------------------------------------------------*/
52 :
53 156242 : ivas_error ivas_sce_enc(
54 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
55 : const int16_t sce_id, /* i : SCE # identifier */
56 : const float data_f[], /* i : input signal for single channel */
57 : const int16_t input_frame, /* i : input frame length per channel */
58 : const int16_t nb_bits_metadata /* i : number of metadata bits */
59 : )
60 : {
61 : float old_inp_12k8[1][L_INP_12k8]; /* buffer of input signal @ 12k8 */
62 : float old_inp_16k[1][L_INP]; /* buffer of input signal @ 16kHz */
63 : float ener[1]; /* residual energy from Levinson-Durbin */
64 : float relE[1]; /* frame relative energy */
65 : float A[1][NB_SUBFR16k * ( M + 1 )]; /* A(z) unquantized for subframes */
66 : float Aw[1][NB_SUBFR16k * ( M + 1 )]; /* weighted A(z) unquantized for subframes */
67 : float epsP[1][M + 1]; /* LP prediction errors */
68 : float lsp_new[1][M]; /* LSPs at the end of the frame */
69 : float lsp_mid[1][M]; /* ISPs in the middle of the frame */
70 : int16_t vad_hover_flag[1]; /* VAD hangover flag */
71 : int16_t attack_flag[1]; /* attack flag (GSC or TC) */
72 : float realBuffer[1][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* real buffer */
73 : float imagBuffer[1][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* imag buffer */
74 : float old_wsp[1][L_WSP]; /* old weighted input signal */
75 : float pitch_fr[1][NB_SUBFR]; /* fractional pitch values */
76 : float voicing_fr[1][NB_SUBFR]; /* fractional pitch gains */
77 : int16_t loc_harm[1]; /* harmonicity flag */
78 : float cor_map_sum[1]; /* speech/music clasif. parameter */
79 : int16_t vad_flag_dtx[1]; /* HE-SAD flag with additional DTX HO */
80 : float enerBuffer[1][CLDFB_NO_CHANNELS_MAX]; /* energy buffer */
81 : float currFlatness[1]; /* flatness parameter */
82 : float fft_buff[1][2 * L_FFT]; /* FFT buffer */
83 : float fr_bands[1][2 * NB_BANDS]; /* energy in frequency bands */
84 : float Etot_LR[1]; /* total energy */
85 : float lf_E[1][2 * VOIC_BINS]; /* per bin spectrum energy in lf */
86 : int16_t localVAD_HE_SAD[1]; /* local HE SAD */
87 : SCE_ENC_HANDLE hSCE;
88 : Encoder_State *st;
89 : IVAS_FORMAT ivas_format;
90 : ivas_error error;
91 : int16_t flag_16k_smc;
92 :
93 156242 : push_wmops( "ivas_sce_enc" );
94 :
95 156242 : error = IVAS_ERR_OK;
96 :
97 156242 : hSCE = st_ivas->hSCE[sce_id];
98 156242 : st = hSCE->hCoreCoder[0];
99 156242 : ivas_format = st_ivas->hEncoderConfig->ivas_format;
100 :
101 : /*------------------------------------------------------------------*
102 : * Initialization - general
103 : *-----------------------------------------------------------------*/
104 :
105 156242 : mvr2r( data_f, st->input, input_frame );
106 :
107 156242 : st->element_mode = IVAS_SCE;
108 :
109 : /*------------------------------------------------------------------*
110 : * SCE initialization - core coder
111 : *-----------------------------------------------------------------*/
112 :
113 156242 : st->idchan = 0;
114 156242 : st->core = -1;
115 156242 : st->core_brate = -1; /* updated in dtx() */
116 156242 : st->max_bwidth = st_ivas->hEncoderConfig->max_bwidth;
117 156242 : st->input_bwidth = st->last_input_bwidth; /* updated in BWD */
118 156242 : st->bwidth = st->last_bwidth; /* updated in BWD */
119 156242 : st->rate_switching_reset = 0;
120 : #ifdef DEBUGGING
121 : st->force = st_ivas->hEncoderConfig->force;
122 : st->id_element = sce_id;
123 : #endif
124 :
125 : /*---------------------------------------------------------------*
126 : * Time Domain Transient Detector
127 : *---------------------------------------------------------------*/
128 :
129 156242 : if ( ivas_format != MC_FORMAT || st_ivas->mc_mode != MC_MODE_PARAMMC )
130 : {
131 156242 : RunTransientDetection( st->input, input_frame, st->hTranDet );
132 : }
133 :
134 156242 : currFlatness[0] = GetTCXAvgTemporalFlatnessMeasure( st->hTranDet, NSUBBLOCKS, 0 );
135 :
136 : /*----------------------------------------------------------------*
137 : * Configuration of core encoder
138 : *----------------------------------------------------------------*/
139 :
140 : /* Force to MODE1 in IVAS */
141 156242 : st->codec_mode = MODE1;
142 :
143 : /* set "bits_frame_nominal" */
144 156242 : if ( st_ivas->hQMetaData != NULL && st_ivas->hSpar == NULL )
145 : {
146 84806 : if ( ( ( st_ivas->mc_mode == MC_MODE_MCMASA ) && ( st_ivas->hEncoderConfig->ivas_total_brate >= MCMASA_SEPARATE_BRATE ) ) || ( st_ivas->ism_mode >= ISM_MASA_MODE_MASA_ONE_OBJ ) )
147 : {
148 19279 : st->bits_frame_nominal = (int16_t) ( hSCE->element_brate / FRAMES_PER_SEC );
149 : }
150 : else
151 : {
152 65527 : st->bits_frame_nominal = st_ivas->hQMetaData->bits_frame_nominal;
153 : }
154 : }
155 71436 : else if ( st_ivas->hSpar != NULL )
156 : {
157 71436 : st->bits_frame_nominal = (int16_t) ( st_ivas->hSpar->core_nominal_brate / FRAMES_PER_SEC );
158 : }
159 : else
160 : {
161 0 : st->bits_frame_nominal = (int16_t) ( ( hSCE->element_brate / FRAMES_PER_SEC ) - ISM_NB_BITS_METADATA_NOMINAL );
162 : }
163 :
164 : /* set "total_brate" */
165 156242 : st->total_brate = hSCE->element_brate - nb_bits_metadata * FRAMES_PER_SEC;
166 :
167 : /* set flag for sampling rate of OL S/M classifier */
168 156242 : flag_16k_smc = 0;
169 156242 : if ( ivas_format == SBA_FORMAT && ( st_ivas->hEncoderConfig->ivas_total_brate == IVAS_24k4 || st_ivas->hEncoderConfig->ivas_total_brate == IVAS_32k ) && hSCE->element_brate == hSCE->last_element_brate )
170 :
171 : {
172 31514 : flag_16k_smc = 1;
173 : }
174 :
175 : #ifdef DEBUG_MODE_INFO
176 : dbgwrite( st->input - NS2SA( st->input_Fs, ACELP_LOOK_NS ), sizeof( float ), input_frame, 1, "res/input_DMX" );
177 : dbgwrite( &st->element_mode, sizeof( int16_t ), 1, input_frame, fname( debug_dir, "element_mode", 0, st->id_element, ENC ) );
178 : #endif
179 :
180 : /*----------------------------------------------------------------*
181 : * Front Pre-processing
182 : *----------------------------------------------------------------*/
183 :
184 : #ifdef DEBUGGING
185 : st_ivas->noClipping += check_clipping( hSCE->hCoreCoder[0]->input, input_frame, &st_ivas->maxOverload, &st_ivas->minOverload );
186 :
187 : #endif
188 : #ifndef FIX_2344_ALIGN_PREPROC
189 : error =
190 : #endif
191 325854 : pre_proc_front_ivas( hSCE, NULL, hSCE->element_brate, nb_bits_metadata, input_frame, 0, old_inp_12k8[0], old_inp_16k[0],
192 : &ener[0], &relE[0], A[0], Aw[0], epsP[0], lsp_new[0], lsp_mid[0], &vad_hover_flag[0], &attack_flag[0],
193 : realBuffer[0], imagBuffer[0], old_wsp[0], pitch_fr[0], voicing_fr[0], &loc_harm[0], &cor_map_sum[0], &vad_flag_dtx[0], enerBuffer[0],
194 : fft_buff[0], A[0], lsp_new[0], currFlatness[0], 0, fr_bands, Etot_LR, lf_E, localVAD_HE_SAD, NULL, flag_16k_smc,
195 325854 : st_ivas->hSpar != NULL ? st_ivas->hSpar->front_vad_flag : 0, st_ivas->hSpar != NULL ? st_ivas->hSpar->force_front_vad : 0, st_ivas->hSpar != NULL ? st_ivas->hSpar->front_vad_dtx_flag : 0, ivas_format, 0, st_ivas->hEncoderConfig->last_ivas_total_brate, st_ivas->hEncoderConfig->ivas_total_brate );
196 : #ifndef FIX_2344_ALIGN_PREPROC
197 : if ( error != IVAS_ERR_OK )
198 : {
199 : return error;
200 : }
201 : #endif
202 :
203 : /* sanity check -> DTX not supported for more than one SCEs/CPEs */
204 156242 : if ( st_ivas->nSCE + st_ivas->nCPE > 1 )
205 : {
206 19279 : if ( st->core_brate == SID_2k40 || st->core_brate == FRAME_NO_DATA )
207 : {
208 0 : st->core_brate = -1;
209 0 : st->total_brate = hSCE->element_brate - nb_bits_metadata * FRAMES_PER_SEC;
210 : }
211 : }
212 :
213 : /*----------------------------------------------------------------*
214 : * Reset metadata
215 : *----------------------------------------------------------------*/
216 :
217 156242 : reset_metadata_spatial( ivas_format, hSCE->hMetaData, hSCE->element_brate, &st->total_brate, st->core_brate, nb_bits_metadata );
218 :
219 : /*----------------------------------------------------------------*
220 : * Combined format coding: get the ISM importance and the bit-rate
221 : *----------------------------------------------------------------*/
222 :
223 156242 : if ( ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
224 : {
225 17504 : ivas_set_ism_importance_interformat( hSCE->element_brate, 1, st_ivas->hIsmMetaData, st_ivas->hSCE, st_ivas->hMasa->data.hOmasaData->lp_noise_CPE, &st_ivas->hIsmMetaData[0]->ism_imp );
226 :
227 17504 : st->total_brate = ivas_interformat_brate( ISM_MASA_MODE_PARAM_ONE_OBJ, 1, hSCE->element_brate, st_ivas->hIsmMetaData[0]->ism_imp, 0 ) - nb_bits_metadata * FRAMES_PER_SEC;
228 : }
229 :
230 : /*----------------------------------------------------------------*
231 : * Write IVAS format signaling in SID frames
232 : *----------------------------------------------------------------*/
233 :
234 156242 : if ( st->core_brate == SID_2k40 )
235 : {
236 377 : ivas_write_format_sid( ivas_format, IVAS_SCE, st->hBstr, st_ivas->hEncoderConfig->sba_order, st_ivas->hEncoderConfig->sba_planar );
237 : }
238 :
239 : /*----------------------------------------------------------------*
240 : * Core codec configuration
241 : *----------------------------------------------------------------*/
242 :
243 : /* IGF reconfiguration */
244 156242 : if ( hSCE->last_element_brate != hSCE->element_brate || st->last_bwidth != st->bwidth )
245 : {
246 : int16_t igf;
247 2338 : igf = getIgfPresent( st->element_mode, st->bits_frame_nominal * FRAMES_PER_SEC, st->max_bwidth, st->rf_mode );
248 2338 : if ( ( error = IGF_Reconfig( &st->hIGFEnc, igf, 0, st->bits_frame_nominal * FRAMES_PER_SEC, st->max_bwidth, st->element_mode, st->rf_mode ) ) != IVAS_ERR_OK )
249 : {
250 0 : return error;
251 : }
252 : }
253 :
254 : /* set ACELP@12k8 / ACELP@16k flag for flexible ACELP core */
255 156242 : if ( st->core_brate == SID_2k40 || st->core_brate == FRAME_NO_DATA )
256 : {
257 2647 : st->flag_ACELP16k = set_ACELP_flag( IVAS_SCE, hSCE->element_brate, st->core_brate, 0, 0, -1, -1 );
258 : }
259 153595 : else if ( st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ && st->low_rate_mode )
260 : {
261 2 : st->flag_ACELP16k = 0;
262 : }
263 : else
264 : {
265 153593 : st->flag_ACELP16k = set_ACELP_flag( IVAS_SCE, hSCE->element_brate, st->total_brate, 0, 0, -1, -1 );
266 : }
267 :
268 : /* modify the coder_type depending on the total_brate per channel */
269 156242 : coder_type_modif( st, relE[0] );
270 :
271 : /*----------------------------------------------------------------*
272 : * Encoder
273 : *----------------------------------------------------------------*/
274 :
275 156242 : if ( ( error = ivas_core_enc( hSCE, NULL, NULL, 1, old_inp_12k8, old_inp_16k, ener, A, Aw, epsP, lsp_new, lsp_mid, vad_hover_flag, attack_flag, realBuffer, imagBuffer, old_wsp, loc_harm, cor_map_sum, vad_flag_dtx, enerBuffer, fft_buff, 0, ivas_format, flag_16k_smc ) ) != IVAS_ERR_OK )
276 : {
277 0 : return error;
278 : }
279 :
280 : /*----------------------------------------------------------------*
281 : * Common updates
282 : *----------------------------------------------------------------*/
283 :
284 : /* update input samples buffer */
285 156242 : mvr2r( st->input, st->old_input_signal, input_frame );
286 :
287 156242 : hSCE->last_element_brate = hSCE->element_brate;
288 :
289 : /* Store previous attack detection flag */
290 156242 : st->hTranDet->transientDetector.prev_bIsAttackPresent = st->hTranDet->transientDetector.bIsAttackPresent;
291 :
292 : #ifdef DEBUG_MODE_INFO
293 : {
294 : float tmpF = hSCE->element_brate / 1000.0f;
295 : dbgwrite( &tmpF, sizeof( float ), 1, input_frame, fname( debug_dir, "element_brate", 0, sce_id, ENC ) );
296 : }
297 : #endif
298 :
299 156242 : pop_wmops();
300 :
301 156242 : return error;
302 : }
303 :
304 :
305 : /*-------------------------------------------------------------------------
306 : * create_sce_enc()
307 : *
308 : * Create, allocate and initialize IVAS encoder SCE handle
309 : *-------------------------------------------------------------------------*/
310 :
311 12263 : ivas_error create_sce_enc(
312 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
313 : const int16_t sce_id, /* i : SCE # identifier */
314 : const int32_t element_brate /* i : element bitrate */
315 : )
316 : {
317 : SCE_ENC_HANDLE hSCE;
318 : Encoder_State *st;
319 : ivas_error error;
320 :
321 12263 : error = IVAS_ERR_OK;
322 :
323 : /*-----------------------------------------------------------------*
324 : * Allocate SCE handle
325 : *-----------------------------------------------------------------*/
326 :
327 12263 : if ( ( hSCE = (SCE_ENC_HANDLE) malloc( sizeof( SCE_ENC_DATA ) ) ) == NULL )
328 : {
329 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SCE\n" ) );
330 : }
331 :
332 : /*-----------------------------------------------------------------*
333 : * Initialization - general parameters
334 : *-----------------------------------------------------------------*/
335 :
336 12263 : hSCE->sce_id = sce_id;
337 12263 : hSCE->element_brate = element_brate;
338 12263 : hSCE->last_element_brate = hSCE->element_brate;
339 :
340 : /*-----------------------------------------------------------------*
341 : * Metadata: allocate and initialize
342 : *-----------------------------------------------------------------*/
343 :
344 12263 : if ( st_ivas->hEncoderConfig->ivas_format != MONO_FORMAT && sce_id == ( st_ivas->nSCE - 1 ) )
345 : {
346 6973 : if ( ( error = ivas_initialize_MD_bstr_enc( &( hSCE->hMetaData ), st_ivas ) ) != IVAS_ERR_OK )
347 : {
348 0 : return error;
349 : }
350 : }
351 : else
352 : {
353 5290 : hSCE->hMetaData = NULL;
354 : }
355 :
356 : /*-----------------------------------------------------------------*
357 : * Core Coder, 1 instance: allocate and initialize
358 : *-----------------------------------------------------------------*/
359 :
360 12263 : if ( ( st = (ENC_CORE_HANDLE) malloc( sizeof( Encoder_State ) ) ) == NULL )
361 : {
362 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
363 : }
364 :
365 12263 : copy_encoder_config( st_ivas, st, 1 );
366 :
367 12263 : if ( st_ivas->hEncoderConfig->ivas_format == MASA_ISM_FORMAT && ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_DISC ) )
368 : {
369 7715 : st->element_mode = IVAS_SCE;
370 : }
371 :
372 12263 : st->total_brate = hSCE->element_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
373 12263 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
374 :
375 12263 : if ( ( error = init_encoder( st, st_ivas, 0, 0, st_ivas->ism_mode, hSCE->element_brate ) ) != IVAS_ERR_OK )
376 : {
377 0 : return error;
378 : }
379 :
380 12263 : hSCE->hCoreCoder[0] = st;
381 :
382 12263 : st_ivas->hSCE[sce_id] = hSCE;
383 :
384 12263 : return error;
385 : }
386 :
387 :
388 : /*-------------------------------------------------------------------------
389 : * destroy_sce_enc()
390 : *
391 : * Destroy and deallocate IVAS encoder SCE handle
392 : *-------------------------------------------------------------------------*/
393 :
394 12263 : void destroy_sce_enc(
395 : SCE_ENC_HANDLE hSCE /* i/o: SCE encoder structure */
396 : )
397 : {
398 : Encoder_State *st;
399 :
400 12263 : st = hSCE->hCoreCoder[0];
401 :
402 12263 : if ( st != NULL )
403 : {
404 12263 : destroy_core_enc( st );
405 12263 : st = NULL;
406 : }
407 :
408 12263 : ivas_destroy_MD_bstr_enc( &( hSCE->hMetaData ) );
409 :
410 12263 : free( hSCE );
411 :
412 12263 : return;
413 : }
|