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 1047102 : 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 1047102 : error = IVAS_ERR_OK;
69 :
70 1047102 : push_wmops( "ivas_sce_dec" );
71 :
72 1047102 : hSCE = st_ivas->hSCE[sce_id];
73 1047102 : st = hSCE->hCoreCoder[0];
74 :
75 1047102 : st->BER_detect |= st_ivas->BER_detect;
76 :
77 1047102 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
78 1047102 : last_ivas_total_brate = st_ivas->hDecoderConfig->last_ivas_total_brate;
79 :
80 1047102 : if ( st_ivas->ivas_format == ISM_FORMAT )
81 : {
82 807027 : st->cng_type = FD_CNG;
83 : }
84 :
85 : /*------------------------------------------------------------------*
86 : * Read audio bandwidth info
87 : *-----------------------------------------------------------------*/
88 :
89 : /* set total_brate - needed in DTX */
90 1047102 : if ( !st_ivas->bfi && ivas_total_brate == IVAS_SID_5k2 )
91 : {
92 5619 : st->total_brate = ivas_total_brate - nb_bits_metadata * FRAMES_PER_SEC;
93 5619 : assert( st->total_brate == SID_2k40 && "SCE SID must be 2.4kbps!" );
94 :
95 5619 : if ( st_ivas->ivas_format == ISM_FORMAT && st_ivas->hISMDTX.sce_id_dtx != sce_id )
96 : {
97 3129 : st->total_brate = FRAME_NO_DATA;
98 : }
99 : }
100 1041483 : else if ( !st_ivas->bfi && ivas_total_brate == FRAME_NO_DATA )
101 : {
102 16407 : st->total_brate = FRAME_NO_DATA;
103 : }
104 1025076 : else if ( !st_ivas->bfi && st_ivas->ivas_format != ISM_FORMAT && last_ivas_total_brate <= IVAS_SID_5k2 )
105 : {
106 147 : st->total_brate = hSCE->element_brate - nb_bits_metadata * FRAMES_PER_SEC;
107 : }
108 :
109 : /* read the bandwidth */
110 1047102 : if ( st_ivas->bfi || st->total_brate <= SID_2k40 )
111 : {
112 : /* BFI or SID/NO_DATA frame -> do nothing */
113 : }
114 : else
115 : {
116 1003782 : if ( st->low_rate_mode )
117 : {
118 : /* ISM Low-rate mode -> always WB */
119 22428 : st->bwidth = WB;
120 : }
121 981354 : else if ( hSCE->element_brate < MIN_BRATE_SWB_SCE )
122 : {
123 : /* only WB is supported */
124 17850 : st->bwidth = WB;
125 : }
126 963504 : else if ( ( hSCE->element_brate < MIN_BRATE_FB_STEREO && !st->is_ism_format ) ||
127 855753 : ( hSCE->element_brate < MIN_BRATE_FB_ISM && st->is_ism_format ) )
128 : {
129 : /* WB and SWB are supported */
130 188799 : st->bwidth = get_next_indice( st, 1 ) + WB;
131 : }
132 : else
133 : {
134 : /* WB, SWB and FB are supported */
135 774705 : st->bwidth = get_next_indice( st, NBITS_BWIDTH );
136 : }
137 : }
138 :
139 : /*------------------------------------------------------------------*
140 : * Initialization
141 : *-----------------------------------------------------------------*/
142 :
143 1047102 : st->idchan = 0;
144 1047102 : st->element_mode = IVAS_SCE;
145 : #ifdef DEBUGGING
146 : st->id_element = sce_id;
147 : #endif
148 :
149 : /* Force to MODE1 in IVAS */
150 1047102 : st->codec_mode = MODE1;
151 :
152 : /* set "bits_frame_nominal" */
153 1047102 : if ( ( st_ivas->hQMetaData != NULL ) &&
154 240075 : ( st_ivas->ivas_format != SBA_FORMAT ) )
155 : {
156 134208 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC || st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
157 : {
158 31611 : st->bits_frame_nominal = (int16_t) ( ( hSCE->element_brate / FRAMES_PER_SEC ) - ISM_NB_BITS_METADATA_NOMINAL );
159 : }
160 102597 : 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 7470 : st->bits_frame_nominal = (int16_t) ( hSCE->element_brate / FRAMES_PER_SEC );
163 : }
164 95127 : else if ( st_ivas->ism_mode == ISM_MODE_NONE && st_ivas->ivas_format == SBA_ISM_FORMAT )
165 : {
166 20937 : st->bits_frame_nominal = (int16_t) ( st_ivas->hSpar->core_nominal_brate / FRAMES_PER_SEC );
167 : }
168 : else
169 : {
170 74190 : st->bits_frame_nominal = st_ivas->hQMetaData->bits_frame_nominal;
171 : }
172 : }
173 912894 : else if ( st_ivas->ivas_format == SBA_FORMAT )
174 : {
175 105867 : st->bits_frame_nominal = (int16_t) ( st_ivas->hSpar->core_nominal_brate / FRAMES_PER_SEC );
176 : }
177 : else
178 : {
179 807027 : 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 1047102 : if ( !st_ivas->bfi && ivas_total_brate == IVAS_SID_5k2 )
185 : {
186 5619 : st->total_brate = ivas_total_brate - nb_bits_metadata * FRAMES_PER_SEC;
187 :
188 5619 : if ( st_ivas->ivas_format == ISM_FORMAT && st_ivas->hISMDTX.sce_id_dtx != sce_id )
189 : {
190 3129 : st->total_brate = FRAME_NO_DATA;
191 : }
192 : }
193 1041483 : else if ( !st_ivas->bfi && ivas_total_brate == FRAME_NO_DATA )
194 : {
195 16407 : st->total_brate = ivas_total_brate;
196 : }
197 1025076 : 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 186420 : 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 1047102 : 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 844239 : st->low_rate_mode && !( st->total_brate == SID_2k40 || st->total_brate == FRAME_NO_DATA ) )
209 : {
210 22578 : st->flag_ACELP16k = 0;
211 : }
212 : else
213 : {
214 1024524 : st->flag_ACELP16k = set_ACELP_flag( IVAS_SCE, hSCE->element_brate, st->total_brate, 0, 0, -1, -1 );
215 : }
216 :
217 1047102 : if ( is_DTXrate( ivas_total_brate ) )
218 : {
219 22026 : st->VAD = 0;
220 : }
221 : else
222 : {
223 1025076 : st->VAD = 1;
224 : }
225 :
226 1047102 : if ( st->VAD && hSCE->element_brate <= CNA_MAX_BRATE )
227 : {
228 130407 : st->flag_cna = 1;
229 : }
230 916695 : else if ( ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT ) && hSCE->element_brate <= ACELP_32k )
231 : {
232 107013 : st->flag_cna = 1;
233 : }
234 : else
235 : {
236 809682 : st->flag_cna = 0;
237 : }
238 :
239 : /*----------------------------------------------------------------*
240 : * Decoder
241 : *----------------------------------------------------------------*/
242 :
243 1047102 : 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 1047102 : 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 13263 : mvr2r( outputHB[0], hSCE->save_hb_synth, output_frame );
252 : }
253 1033839 : else if ( !st_ivas->sba_dirac_stereo_flag )
254 : {
255 : int32_t output_Fs;
256 :
257 1033566 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
258 :
259 : /*----------------------------------------------------------------*
260 : * LB synthesis synchronization between IVAS formats
261 : *----------------------------------------------------------------*/
262 :
263 1033566 : 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 1033566 : 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 1033566 : v_add( output[0], outputHB[0], output[0], output_frame );
276 : }
277 :
278 : /*----------------------------------------------------------------*
279 : * Common updates
280 : *----------------------------------------------------------------*/
281 :
282 1047102 : hSCE->last_element_brate = hSCE->element_brate;
283 :
284 1047102 : 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 1047102 : pop_wmops();
317 1047102 : return error;
318 : }
319 :
320 :
321 : /*-------------------------------------------------------------------------
322 : * create_sce_dec()
323 : *
324 : * Create, allocate and initialize IVAS decoder SCE handle
325 : *-------------------------------------------------------------------------*/
326 :
327 8952 : 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 8952 : error = IVAS_ERR_OK;
339 8952 : output_frame = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / FRAMES_PER_SEC );
340 :
341 : /*-----------------------------------------------------------------*
342 : * Allocate SCE handle
343 : *-----------------------------------------------------------------*/
344 :
345 8952 : 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 8952 : hSCE->sce_id = sce_id;
355 8952 : hSCE->element_brate = element_brate;
356 :
357 8952 : set_f( hSCE->prev_hb_synth, 0.0f, NS2SA( st_ivas->hDecoderConfig->output_Fs, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS ) );
358 :
359 : /*-----------------------------------------------------------------*
360 : * Core Coder, 1 instance: allocate and initialize
361 : *-----------------------------------------------------------------*/
362 :
363 8952 : if ( ( st = (DEC_CORE_HANDLE) malloc( sizeof( Decoder_State ) ) ) == NULL )
364 : {
365 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
366 : }
367 :
368 8952 : copy_decoder_config( st_ivas, st );
369 :
370 8952 : st->total_brate = hSCE->element_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
371 8952 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
372 8952 : st->is_ism_format = 0;
373 8952 : if ( st_ivas->ivas_format == ISM_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT )
374 : {
375 6411 : st->is_ism_format = 1;
376 : }
377 :
378 8952 : if ( ( error = init_decoder( st, 0, st_ivas->mc_mode ) ) != IVAS_ERR_OK )
379 : {
380 0 : return error;
381 : }
382 :
383 8952 : 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 ) ) )
384 : {
385 171 : if ( ( error = openCldfb( &st->cldfbSynHB, CLDFB_SYNTHESIS, st->output_Fs, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
386 : {
387 0 : return error;
388 : }
389 : }
390 : else
391 : {
392 8781 : st->cldfbSynHB = NULL;
393 : }
394 :
395 : /* allocate and initialize "hTdCngDec" - needed in DTX */
396 8952 : if ( sce_id == 0 && st->hTdCngDec == NULL )
397 : {
398 4185 : if ( ( st->hTdCngDec = (TD_CNG_DEC_HANDLE) malloc( sizeof( TD_CNG_DEC_DATA ) ) ) == NULL )
399 : {
400 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DTX/TD CNG\n" ) );
401 : }
402 :
403 4185 : td_cng_dec_init( st );
404 : }
405 :
406 : /*-----------------------------------------------------------------*
407 : * Synthesis buffers: allocate and initialize
408 : *-----------------------------------------------------------------*/
409 :
410 8952 : if ( st_ivas->sba_dirac_stereo_flag )
411 : {
412 261 : if ( ( hSCE->save_synth = (float *) malloc( sizeof( float ) * output_frame ) ) == NULL )
413 : {
414 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for stereo output\n" ) );
415 : }
416 261 : set_zero( hSCE->save_synth, output_frame );
417 :
418 261 : if ( ( hSCE->save_hb_synth = (float *) malloc( sizeof( float ) * output_frame ) ) == NULL )
419 : {
420 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate HB memory for stereo output\n" ) );
421 : }
422 261 : set_zero( hSCE->save_hb_synth, output_frame );
423 : }
424 : else
425 : {
426 8691 : hSCE->save_synth = NULL;
427 8691 : hSCE->save_hb_synth = NULL;
428 : }
429 :
430 8952 : hSCE->hCoreCoder[0] = st;
431 8952 : st_ivas->hSCE[sce_id] = hSCE;
432 :
433 8952 : return error;
434 : }
435 :
436 :
437 : /*-------------------------------------------------------------------------
438 : * destroy_sce_dec()
439 : *
440 : * Destroy and deallocate IVAS decoder SCE handle
441 : *-------------------------------------------------------------------------*/
442 :
443 8952 : void destroy_sce_dec(
444 : SCE_DEC_HANDLE hSCE /* i/o: SCE decoder structure */
445 : )
446 : {
447 : Decoder_State *st;
448 :
449 8952 : st = hSCE->hCoreCoder[0];
450 :
451 8952 : if ( st != NULL )
452 : {
453 8952 : destroy_core_dec( st );
454 :
455 8952 : free( st );
456 8952 : st = NULL;
457 : }
458 :
459 8952 : if ( hSCE->save_synth != NULL )
460 : {
461 96 : free( hSCE->save_synth );
462 96 : hSCE->save_synth = NULL;
463 : }
464 8952 : if ( hSCE->save_hb_synth != NULL )
465 : {
466 96 : free( hSCE->save_hb_synth );
467 96 : hSCE->save_hb_synth = NULL;
468 : }
469 :
470 8952 : free( hSCE );
471 :
472 8952 : return;
473 : }
|