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 "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 71463 : 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 71463 : push_wmops( "ivas_sce_enc" );
94 :
95 71463 : error = IVAS_ERR_OK;
96 :
97 71463 : hSCE = st_ivas->hSCE[sce_id];
98 71463 : st = hSCE->hCoreCoder[0];
99 71463 : ivas_format = st_ivas->hEncoderConfig->ivas_format;
100 :
101 : /*------------------------------------------------------------------*
102 : * Initialization - general
103 : *-----------------------------------------------------------------*/
104 :
105 71463 : mvr2r( data_f, st->input, input_frame );
106 :
107 71463 : st->element_mode = IVAS_SCE;
108 :
109 : /*------------------------------------------------------------------*
110 : * SCE initialization - core coder
111 : *-----------------------------------------------------------------*/
112 :
113 71463 : st->idchan = 0;
114 71463 : st->core = -1;
115 71463 : st->core_brate = -1; /* updated in dtx() */
116 71463 : st->max_bwidth = st_ivas->hEncoderConfig->max_bwidth;
117 71463 : st->input_bwidth = st->last_input_bwidth; /* updated in BWD */
118 71463 : st->bwidth = st->last_bwidth; /* updated in BWD */
119 71463 : 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 71463 : if ( ivas_format != MC_FORMAT || st_ivas->mc_mode != MC_MODE_PARAMMC )
130 : {
131 71463 : RunTransientDetection( st->input, input_frame, st->hTranDet );
132 : }
133 :
134 71463 : currFlatness[0] = GetTCXAvgTemporalFlatnessMeasure( st->hTranDet, NSUBBLOCKS, 0 );
135 :
136 : /*----------------------------------------------------------------*
137 : * Configuration of core encoder
138 : *----------------------------------------------------------------*/
139 :
140 : /* Force to MODE1 in IVAS */
141 71463 : st->codec_mode = MODE1;
142 :
143 : /* set "bits_frame_nominal" */
144 71463 : if ( st_ivas->hQMetaData != NULL && st_ivas->hSpar == NULL )
145 : {
146 27212 : 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 2487 : st->bits_frame_nominal = (int16_t) ( hSCE->element_brate / FRAMES_PER_SEC );
149 : }
150 : else
151 : {
152 24725 : st->bits_frame_nominal = st_ivas->hQMetaData->bits_frame_nominal;
153 : }
154 : }
155 44251 : else if ( st_ivas->hSpar != NULL )
156 : {
157 44251 : 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 71463 : 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 71463 : flag_16k_smc = 0;
169 71463 : 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 26562 : 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 125887 : error = pre_proc_front_ivas( hSCE, NULL, hSCE->element_brate, nb_bits_metadata, input_frame, 0, old_inp_12k8[0], old_inp_16k[0],
189 : &ener[0], &relE[0], A[0], Aw[0], epsP[0], lsp_new[0], lsp_mid[0], &vad_hover_flag[0], &attack_flag[0],
190 : 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],
191 : fft_buff[0], A[0], lsp_new[0], currFlatness[0], 0, fr_bands, Etot_LR, lf_E, localVAD_HE_SAD, NULL, flag_16k_smc,
192 125887 : 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 );
193 71463 : if ( error != IVAS_ERR_OK )
194 : {
195 0 : return error;
196 : }
197 :
198 : /* sanity check -> DTX not supported for more than one SCEs/CPEs */
199 71463 : if ( st_ivas->nSCE + st_ivas->nCPE > 1 )
200 : {
201 2487 : if ( st->core_brate == SID_2k40 || st->core_brate == FRAME_NO_DATA )
202 : {
203 0 : st->core_brate = -1;
204 0 : st->total_brate = hSCE->element_brate - nb_bits_metadata * FRAMES_PER_SEC;
205 : }
206 : }
207 :
208 : /*----------------------------------------------------------------*
209 : * Reset metadata
210 : *----------------------------------------------------------------*/
211 :
212 71463 : reset_metadata_spatial( ivas_format, hSCE->hMetaData, hSCE->element_brate, &st->total_brate, st->core_brate, nb_bits_metadata );
213 :
214 : /*----------------------------------------------------------------*
215 : * Combined format coding: get the ISM importance and the bit-rate
216 : *----------------------------------------------------------------*/
217 :
218 71463 : if ( ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
219 : {
220 1862 : 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 );
221 :
222 1862 : 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;
223 : }
224 :
225 : /*----------------------------------------------------------------*
226 : * Write IVAS format signaling in SID frames
227 : *----------------------------------------------------------------*/
228 :
229 71463 : if ( st->core_brate == SID_2k40 )
230 : {
231 408 : ivas_write_format_sid( ivas_format, IVAS_SCE, st->hBstr, st_ivas->hEncoderConfig->sba_order, st_ivas->hEncoderConfig->sba_planar );
232 : }
233 :
234 : /*----------------------------------------------------------------*
235 : * Core codec configuration
236 : *----------------------------------------------------------------*/
237 :
238 : /* IGF reconfiguration */
239 71463 : if ( hSCE->last_element_brate != hSCE->element_brate || st->last_bwidth != st->bwidth )
240 : {
241 : int16_t igf;
242 1219 : igf = getIgfPresent( st->element_mode, st->bits_frame_nominal * FRAMES_PER_SEC, st->max_bwidth, st->rf_mode );
243 1219 : 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 )
244 : {
245 0 : return error;
246 : }
247 : }
248 :
249 : /* set ACELP@12k8 / ACELP@16k flag for flexible ACELP core */
250 71463 : if ( st->core_brate == SID_2k40 || st->core_brate == FRAME_NO_DATA )
251 : {
252 2923 : st->flag_ACELP16k = set_ACELP_flag( IVAS_SCE, hSCE->element_brate, st->core_brate, 0, 0, -1, -1 );
253 : }
254 68540 : else if ( st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ && st->low_rate_mode )
255 : {
256 0 : st->flag_ACELP16k = 0;
257 : }
258 : else
259 : {
260 68540 : st->flag_ACELP16k = set_ACELP_flag( IVAS_SCE, hSCE->element_brate, st->total_brate, 0, 0, -1, -1 );
261 : }
262 :
263 : /* modify the coder_type depending on the total_brate per channel */
264 71463 : coder_type_modif( st, relE[0] );
265 :
266 : /*----------------------------------------------------------------*
267 : * Encoder
268 : *----------------------------------------------------------------*/
269 :
270 71463 : 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 )
271 : {
272 0 : return error;
273 : }
274 :
275 : /*----------------------------------------------------------------*
276 : * Common updates
277 : *----------------------------------------------------------------*/
278 :
279 : /* update input samples buffer */
280 71463 : mvr2r( st->input, st->old_input_signal, input_frame );
281 :
282 71463 : hSCE->last_element_brate = hSCE->element_brate;
283 :
284 : /* Store previous attack detection flag */
285 71463 : st->hTranDet->transientDetector.prev_bIsAttackPresent = st->hTranDet->transientDetector.bIsAttackPresent;
286 :
287 : #ifdef DEBUG_MODE_INFO
288 : {
289 : float tmpF = hSCE->element_brate / 1000.0f;
290 : dbgwrite( &tmpF, sizeof( float ), 1, input_frame, fname( debug_dir, "element_brate", 0, sce_id, ENC ) );
291 : }
292 : #endif
293 :
294 71463 : pop_wmops();
295 :
296 71463 : return error;
297 : }
298 :
299 :
300 : /*-------------------------------------------------------------------------
301 : * create_sce_enc()
302 : *
303 : * Create, allocate and initialize IVAS encoder SCE handle
304 : *-------------------------------------------------------------------------*/
305 :
306 3007 : ivas_error create_sce_enc(
307 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
308 : const int16_t sce_id, /* i : SCE # identifier */
309 : const int32_t element_brate /* i : element bitrate */
310 : )
311 : {
312 : SCE_ENC_HANDLE hSCE;
313 : Encoder_State *st;
314 : ivas_error error;
315 :
316 3007 : error = IVAS_ERR_OK;
317 :
318 : /*-----------------------------------------------------------------*
319 : * Allocate SCE handle
320 : *-----------------------------------------------------------------*/
321 :
322 3007 : if ( ( hSCE = (SCE_ENC_HANDLE) malloc( sizeof( SCE_ENC_DATA ) ) ) == NULL )
323 : {
324 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SCE\n" ) );
325 : }
326 :
327 : /*-----------------------------------------------------------------*
328 : * Initialization - general parameters
329 : *-----------------------------------------------------------------*/
330 :
331 3007 : hSCE->sce_id = sce_id;
332 3007 : hSCE->element_brate = element_brate;
333 3007 : hSCE->last_element_brate = hSCE->element_brate;
334 :
335 : /*-----------------------------------------------------------------*
336 : * Metadata: allocate and initialize
337 : *-----------------------------------------------------------------*/
338 :
339 3007 : if ( st_ivas->hEncoderConfig->ivas_format != MONO_FORMAT && sce_id == ( st_ivas->nSCE - 1 ) )
340 : {
341 1963 : if ( ( error = ivas_initialize_MD_bstr_enc( &( hSCE->hMetaData ), st_ivas ) ) != IVAS_ERR_OK )
342 : {
343 0 : return error;
344 : }
345 : }
346 : else
347 : {
348 1044 : hSCE->hMetaData = NULL;
349 : }
350 :
351 : /*-----------------------------------------------------------------*
352 : * Core Coder, 1 instance: allocate and initialize
353 : *-----------------------------------------------------------------*/
354 :
355 3007 : if ( ( st = (ENC_CORE_HANDLE) malloc( sizeof( Encoder_State ) ) ) == NULL )
356 : {
357 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
358 : }
359 :
360 3007 : copy_encoder_config( st_ivas, st, 1 );
361 :
362 3007 : 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 ) )
363 : {
364 1435 : st->element_mode = IVAS_SCE;
365 : }
366 :
367 3007 : st->total_brate = hSCE->element_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
368 3007 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
369 :
370 3007 : if ( ( error = init_encoder( st, st_ivas, 0, st_ivas->hEncoderConfig->var_SID_rate_flag, st_ivas->hEncoderConfig->interval_SID, 0, st_ivas->ism_mode, hSCE->element_brate ) ) != IVAS_ERR_OK )
371 : {
372 0 : return error;
373 : }
374 :
375 3007 : hSCE->hCoreCoder[0] = st;
376 :
377 3007 : st_ivas->hSCE[sce_id] = hSCE;
378 :
379 3007 : return error;
380 : }
381 :
382 :
383 : /*-------------------------------------------------------------------------
384 : * destroy_sce_enc()
385 : *
386 : * Destroy and deallocate IVAS encoder SCE handle
387 : *-------------------------------------------------------------------------*/
388 :
389 3007 : void destroy_sce_enc(
390 : SCE_ENC_HANDLE hSCE /* i/o: SCE encoder structure */
391 : )
392 : {
393 : Encoder_State *st;
394 :
395 3007 : st = hSCE->hCoreCoder[0];
396 :
397 3007 : if ( st != NULL )
398 : {
399 3007 : destroy_core_enc( st );
400 3007 : st = NULL;
401 : }
402 :
403 3007 : ivas_destroy_MD_bstr_enc( &( hSCE->hMetaData ) );
404 :
405 3007 : free( hSCE );
406 :
407 3007 : return;
408 : }
|