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 <assert.h>
34 : #include <stdint.h>
35 : #include "options.h"
36 : #include "cnst.h"
37 : #include "ivas_cnst.h"
38 : #include "rom_com.h"
39 : #include "prot.h"
40 : #include "ivas_prot.h"
41 : #include "ivas_rom_com.h"
42 : #ifdef DEBUGGING
43 : #include "debug.h"
44 : #endif
45 : #include "wmc_auto.h"
46 :
47 :
48 : /*--------------------------------------------------------------------------*
49 : * ivas_sce_dec()
50 : *
51 : * Single Channel Element (SCE) decoding routine
52 : *--------------------------------------------------------------------------*/
53 :
54 14758710 : ivas_error ivas_sce_dec(
55 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
56 : const int16_t sce_id, /* i : SCE # identifier */
57 : float *output[1], /* o : output synthesis signal */
58 : const int16_t output_frame, /* i : output frame length per channel */
59 : const int16_t nb_bits_metadata /* i : number of metadata bits */
60 : )
61 : {
62 : float outputHB[1][L_FRAME48k]; /* 'float' buffer for output HB synthesis, one channel */
63 : SCE_DEC_HANDLE hSCE;
64 : Decoder_State *st;
65 : int32_t ivas_total_brate, last_ivas_total_brate;
66 : ivas_error error;
67 :
68 14758710 : error = IVAS_ERR_OK;
69 :
70 14758710 : push_wmops( "ivas_sce_dec" );
71 :
72 14758710 : hSCE = st_ivas->hSCE[sce_id];
73 14758710 : st = hSCE->hCoreCoder[0];
74 :
75 14758710 : st->BER_detect |= st_ivas->BER_detect;
76 :
77 14758710 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
78 14758710 : last_ivas_total_brate = st_ivas->hDecoderConfig->last_ivas_total_brate;
79 :
80 14758710 : if ( st_ivas->ivas_format == ISM_FORMAT )
81 : {
82 8159739 : st->cng_type = FD_CNG;
83 : }
84 :
85 : /*------------------------------------------------------------------*
86 : * Read audio bandwidth info
87 : *-----------------------------------------------------------------*/
88 :
89 : /* set total_brate - needed in DTX */
90 14758710 : if ( !st_ivas->bfi && ivas_total_brate == IVAS_SID_5k2 )
91 : {
92 24150 : st->total_brate = ivas_total_brate - nb_bits_metadata * FRAMES_PER_SEC;
93 24150 : assert( st->total_brate == SID_2k40 && "SCE SID must be 2.4kbps!" );
94 :
95 24150 : if ( st_ivas->ivas_format == ISM_FORMAT && st_ivas->hISMDTX.sce_id_dtx != sce_id )
96 : {
97 11733 : st->total_brate = FRAME_NO_DATA;
98 : }
99 : }
100 14734560 : else if ( !st_ivas->bfi && ivas_total_brate == FRAME_NO_DATA )
101 : {
102 83858 : st->total_brate = FRAME_NO_DATA;
103 : }
104 14650702 : else if ( !st_ivas->bfi && st_ivas->ivas_format != ISM_FORMAT && last_ivas_total_brate <= IVAS_SID_5k2 )
105 : {
106 1497 : st->total_brate = hSCE->element_brate - nb_bits_metadata * FRAMES_PER_SEC;
107 : }
108 :
109 : /* read the bandwidth */
110 14758710 : if ( st_ivas->bfi || st->total_brate <= SID_2k40 )
111 : {
112 : /* BFI or SID/NO_DATA frame -> do nothing */
113 : }
114 : else
115 : {
116 13857124 : if ( st->low_rate_mode )
117 : {
118 : /* ISM Low-rate mode -> always WB */
119 802154 : st->bwidth = WB;
120 : }
121 13054970 : else if ( hSCE->element_brate < MIN_BRATE_SWB_SCE )
122 : {
123 : /* only WB is supported */
124 110244 : st->bwidth = WB;
125 : }
126 12944726 : else if ( ( hSCE->element_brate < MIN_BRATE_FB_STEREO && !st->is_ism_format ) ||
127 11068759 : ( hSCE->element_brate < MIN_BRATE_FB_ISM && st->is_ism_format ) )
128 : {
129 : /* WB and SWB are supported */
130 2822226 : st->bwidth = get_next_indice( st, 1 ) + WB;
131 : }
132 : else
133 : {
134 : /* WB, SWB and FB are supported */
135 10122500 : st->bwidth = get_next_indice( st, NBITS_BWIDTH );
136 : }
137 : }
138 :
139 : /*------------------------------------------------------------------*
140 : * Initialization
141 : *-----------------------------------------------------------------*/
142 :
143 14758710 : st->idchan = 0;
144 14758710 : st->element_mode = IVAS_SCE;
145 : #ifdef DEBUGGING
146 : st->id_element = sce_id;
147 : #endif
148 :
149 : /* Force to MODE1 in IVAS */
150 14758710 : st->codec_mode = MODE1;
151 :
152 : /* set "bits_frame_nominal" */
153 14758710 : if ( ( st_ivas->hQMetaData != NULL ) &&
154 6598971 : ( st_ivas->ivas_format != SBA_FORMAT ) )
155 : {
156 4301677 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC || st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
157 : {
158 1936349 : st->bits_frame_nominal = (int16_t) ( ( hSCE->element_brate / FRAMES_PER_SEC ) - ISM_NB_BITS_METADATA_NOMINAL );
159 : }
160 2365328 : else if ( ( st_ivas->mc_mode == MC_MODE_MCMASA && ivas_total_brate >= MCMASA_SEPARATE_BRATE ) || ( st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ ) )
161 : {
162 410063 : st->bits_frame_nominal = (int16_t) ( hSCE->element_brate / FRAMES_PER_SEC );
163 : }
164 1955265 : else if ( st_ivas->ism_mode == ISM_MODE_NONE && st_ivas->ivas_format == SBA_ISM_FORMAT )
165 : {
166 668805 : st->bits_frame_nominal = (int16_t) ( st_ivas->hSpar->core_nominal_brate / FRAMES_PER_SEC );
167 : }
168 : else
169 : {
170 1286460 : st->bits_frame_nominal = st_ivas->hQMetaData->bits_frame_nominal;
171 : }
172 : }
173 10457033 : else if ( st_ivas->ivas_format == SBA_FORMAT )
174 : {
175 2297294 : st->bits_frame_nominal = (int16_t) ( st_ivas->hSpar->core_nominal_brate / FRAMES_PER_SEC );
176 : }
177 : else
178 : {
179 8159739 : st->bits_frame_nominal = (int16_t) ( ( hSCE->element_brate / FRAMES_PER_SEC ) - ISM_NB_BITS_METADATA_NOMINAL );
180 : }
181 :
182 :
183 : /* set "total_brate" */
184 14758710 : if ( !st_ivas->bfi && ivas_total_brate == IVAS_SID_5k2 )
185 : {
186 24150 : st->total_brate = ivas_total_brate - nb_bits_metadata * FRAMES_PER_SEC;
187 :
188 24150 : if ( st_ivas->ivas_format == ISM_FORMAT && st_ivas->hISMDTX.sce_id_dtx != sce_id )
189 : {
190 11733 : st->total_brate = FRAME_NO_DATA;
191 : }
192 : }
193 14734560 : else if ( !st_ivas->bfi && ivas_total_brate == FRAME_NO_DATA )
194 : {
195 83858 : st->total_brate = ivas_total_brate;
196 : }
197 14650702 : else if ( !st_ivas->bfi && st_ivas->ivas_format != ISM_FORMAT && st_ivas->ivas_format != MASA_ISM_FORMAT ) /* note: in ISMs, total_brate[] is set in ivas_ism_config() */
198 : {
199 3960648 : st->total_brate = hSCE->element_brate - nb_bits_metadata * FRAMES_PER_SEC;
200 : }
201 :
202 : /*----------------------------------------------------------------*
203 : * Core codec configuration
204 : *----------------------------------------------------------------*/
205 :
206 : /* set ACELP12k8 / ACELP16k flag for flexible ACELP core */
207 14758710 : if ( ( st_ivas->ivas_format == ISM_FORMAT || st_ivas->ism_mode == ISM_MASA_MODE_DISC || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ ) &&
208 10375584 : st->low_rate_mode && !( st->total_brate == SID_2k40 || st->total_brate == FRAME_NO_DATA ) )
209 : {
210 810586 : st->flag_ACELP16k = 0;
211 : }
212 : else
213 : {
214 13948124 : st->flag_ACELP16k = set_ACELP_flag( IVAS_SCE, hSCE->element_brate, st->total_brate, 0, 0, -1, -1 );
215 : }
216 :
217 14758710 : if ( is_DTXrate( ivas_total_brate ) )
218 : {
219 108008 : st->VAD = 0;
220 : }
221 : else
222 : {
223 14650702 : st->VAD = 1;
224 : }
225 :
226 14758710 : if ( st->VAD && hSCE->element_brate <= CNA_MAX_BRATE )
227 : {
228 1741752 : st->flag_cna = 1;
229 : }
230 13016958 : else if ( ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT ) && hSCE->element_brate <= ACELP_32k )
231 : {
232 2351922 : st->flag_cna = 1;
233 : }
234 : else
235 : {
236 10665036 : st->flag_cna = 0;
237 : }
238 :
239 : /*----------------------------------------------------------------*
240 : * Decoder
241 : *----------------------------------------------------------------*/
242 :
243 14758710 : if ( ( error = ivas_core_dec( st_ivas, hSCE, NULL, NULL, 1, output, outputHB, NULL, st_ivas->sba_dirac_stereo_flag ) ) != IVAS_ERR_OK )
244 : {
245 0 : return error;
246 : }
247 :
248 14758710 : if ( st_ivas->sba_dirac_stereo_flag && ( st->core_brate > SID_2k40 || st->cng_type == LP_CNG ) )
249 : {
250 : /* skip addition of ACELP BWE for now, will be done after upmix */
251 223273 : mvr2r( outputHB[0], hSCE->save_hb_synth, output_frame );
252 : }
253 14535437 : else if ( !st_ivas->sba_dirac_stereo_flag )
254 : {
255 : int32_t output_Fs;
256 :
257 14534984 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
258 :
259 : /*----------------------------------------------------------------*
260 : * LB synthesis synchronization between IVAS formats
261 : *----------------------------------------------------------------*/
262 :
263 14534984 : delay_signal( output[0], output_frame, st->prev_synth_buffer, NS2SA( output_Fs, IVAS_DEC_DELAY_NS - DELAY_CLDFB_NS ) );
264 :
265 : /*----------------------------------------------------------------*
266 : * HB synthesis synchronization between IVAS formats
267 : *----------------------------------------------------------------*/
268 :
269 14534984 : delay_signal( outputHB[0], output_frame, hSCE->prev_hb_synth, NS2SA( output_Fs, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS ) );
270 :
271 : /*----------------------------------------------------------------*
272 : * output LB and HB mix
273 : *----------------------------------------------------------------*/
274 :
275 14534984 : v_add( output[0], outputHB[0], output[0], output_frame );
276 : }
277 :
278 : /*----------------------------------------------------------------*
279 : * Common updates
280 : *----------------------------------------------------------------*/
281 :
282 14758710 : hSCE->last_element_brate = hSCE->element_brate;
283 :
284 14758710 : st_ivas->BER_detect |= st->BER_detect;
285 :
286 : #ifdef DEBUG_MODE_INFO
287 : {
288 : float tmpF = hSCE->element_brate / 1000.0f;
289 : int16_t i, n;
290 :
291 : n = 1;
292 : if ( st_ivas->ini_frame == 0 && frame > 0 )
293 : {
294 : /* in case first frame(s) is/are lost, write info several times */
295 : n = (int16_t) frame - st_ivas->ini_frame + 1;
296 : }
297 :
298 : for ( i = 0; i < n; i++ )
299 : {
300 : dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "element_brate", 0, sce_id, DEC ) );
301 : dbgwrite( &st->element_mode, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "element_mode", 0, sce_id, DEC ) );
302 :
303 : dbgwrite( output, sizeof( float ), output_frame, 1, fname( debug_dir, "output.sce", 0, sce_id, DEC ) );
304 :
305 : if ( st_ivas->ivas_format != MASA_ISM_FORMAT )
306 : {
307 : tmpF = 0;
308 : dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "output.cpe", 0, sce_id, DEC ) );
309 : dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "output.mct", 0, sce_id, DEC ) );
310 : }
311 : }
312 : }
313 : #endif
314 :
315 :
316 14758710 : pop_wmops();
317 14758710 : return error;
318 : }
319 :
320 :
321 : /*-------------------------------------------------------------------------
322 : * create_sce_dec()
323 : *
324 : * Create, allocate and initialize IVAS decoder SCE handle
325 : *-------------------------------------------------------------------------*/
326 :
327 263710 : ivas_error create_sce_dec(
328 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
329 : const int16_t sce_id, /* i : SCE # identifier */
330 : const int32_t element_brate /* i : element bitrate */
331 : )
332 : {
333 : SCE_DEC_HANDLE hSCE;
334 : Decoder_State *st;
335 : ivas_error error;
336 : int16_t output_frame;
337 :
338 263710 : error = IVAS_ERR_OK;
339 263710 : output_frame = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / FRAMES_PER_SEC );
340 :
341 : /*-----------------------------------------------------------------*
342 : * Allocate SCE handle
343 : *-----------------------------------------------------------------*/
344 :
345 263710 : if ( ( hSCE = (SCE_DEC_HANDLE) malloc( sizeof( SCE_DEC_DATA ) ) ) == NULL )
346 : {
347 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SCE\n" ) );
348 : }
349 :
350 : /*-----------------------------------------------------------------*
351 : * Initialization - general parameters
352 : *-----------------------------------------------------------------*/
353 :
354 263710 : hSCE->sce_id = sce_id;
355 263710 : hSCE->element_brate = element_brate;
356 : #ifdef FIX_1481_CLANG18_MSAN_INIT_LAST_ELEM_BRATE
357 263710 : hSCE->last_element_brate = hSCE->element_brate;
358 : #endif
359 :
360 263710 : set_f( hSCE->prev_hb_synth, 0.0f, NS2SA( st_ivas->hDecoderConfig->output_Fs, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS ) );
361 :
362 : /*-----------------------------------------------------------------*
363 : * Core Coder, 1 instance: allocate and initialize
364 : *-----------------------------------------------------------------*/
365 :
366 263710 : if ( ( st = (DEC_CORE_HANDLE) malloc( sizeof( Decoder_State ) ) ) == NULL )
367 : {
368 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
369 : }
370 :
371 263710 : copy_decoder_config( st_ivas, st );
372 :
373 263710 : st->total_brate = hSCE->element_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
374 : #ifdef FIX_1484_CLANG18_MSAN_INIT_ST_ELEM_BRATE
375 263710 : st->element_brate = -1;
376 : #endif
377 263710 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
378 263710 : st->is_ism_format = 0;
379 263710 : if ( st_ivas->ivas_format == ISM_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT )
380 : {
381 212646 : st->is_ism_format = 1;
382 : }
383 :
384 263710 : if ( ( error = init_decoder( st, 0, st_ivas->mc_mode ) ) != IVAS_ERR_OK )
385 : {
386 0 : return error;
387 : }
388 :
389 263710 : if ( st_ivas->ivas_format == SBA_FORMAT && ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO || ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_MONO && st_ivas->nchan_transport == 1 ) ) )
390 : {
391 1801 : if ( ( error = openCldfb( &st->cldfbSynHB, CLDFB_SYNTHESIS, st->output_Fs, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
392 : {
393 0 : return error;
394 : }
395 : }
396 : else
397 : {
398 261909 : st->cldfbSynHB = NULL;
399 : }
400 :
401 : /* allocate and initialize "hTdCngDec" - needed in DTX */
402 263710 : if ( sce_id == 0 && st->hTdCngDec == NULL )
403 : {
404 122595 : if ( ( st->hTdCngDec = (TD_CNG_DEC_HANDLE) malloc( sizeof( TD_CNG_DEC_DATA ) ) ) == NULL )
405 : {
406 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DTX/TD CNG\n" ) );
407 : }
408 :
409 122595 : td_cng_dec_init( st );
410 : }
411 :
412 : /*-----------------------------------------------------------------*
413 : * Synthesis buffers: allocate and initialize
414 : *-----------------------------------------------------------------*/
415 :
416 263710 : if ( st_ivas->sba_dirac_stereo_flag )
417 : {
418 3694 : if ( ( hSCE->save_synth = (float *) malloc( sizeof( float ) * output_frame ) ) == NULL )
419 : {
420 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for stereo output\n" ) );
421 : }
422 3694 : set_zero( hSCE->save_synth, output_frame );
423 :
424 3694 : if ( ( hSCE->save_hb_synth = (float *) malloc( sizeof( float ) * output_frame ) ) == NULL )
425 : {
426 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate HB memory for stereo output\n" ) );
427 : }
428 3694 : set_zero( hSCE->save_hb_synth, output_frame );
429 : }
430 : else
431 : {
432 260016 : hSCE->save_synth = NULL;
433 260016 : hSCE->save_hb_synth = NULL;
434 : }
435 :
436 263710 : hSCE->hCoreCoder[0] = st;
437 263710 : st_ivas->hSCE[sce_id] = hSCE;
438 :
439 263710 : return error;
440 : }
441 :
442 :
443 : /*-------------------------------------------------------------------------
444 : * destroy_sce_dec()
445 : *
446 : * Destroy and deallocate IVAS decoder SCE handle
447 : *-------------------------------------------------------------------------*/
448 :
449 263710 : void destroy_sce_dec(
450 : SCE_DEC_HANDLE hSCE /* i/o: SCE decoder structure */
451 : )
452 : {
453 : Decoder_State *st;
454 :
455 263710 : st = hSCE->hCoreCoder[0];
456 :
457 263710 : if ( st != NULL )
458 : {
459 263710 : destroy_core_dec( st );
460 :
461 263710 : free( st );
462 263710 : st = NULL;
463 : }
464 :
465 263710 : if ( hSCE->save_synth != NULL )
466 : {
467 2127 : free( hSCE->save_synth );
468 2127 : hSCE->save_synth = NULL;
469 : }
470 263710 : if ( hSCE->save_hb_synth != NULL )
471 : {
472 2127 : free( hSCE->save_hb_synth );
473 2127 : hSCE->save_hb_synth = NULL;
474 : }
475 :
476 263710 : free( hSCE );
477 :
478 263710 : return;
479 : }
|