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 : #include <math.h>
47 :
48 :
49 : /*--------------------------------------------------------------------------*
50 : * Local function prototypes
51 : *--------------------------------------------------------------------------*/
52 :
53 : static void read_stereo_mode_and_bwidth( CPE_DEC_HANDLE hCPE, const Decoder_Struct *st_ivas );
54 :
55 : static void stereo_mode_combined_format_dec( const Decoder_Struct *st_ivas, CPE_DEC_HANDLE hCPE );
56 :
57 : static ivas_error stereo_dft_dec_main( CPE_DEC_HANDLE hCPE, const int32_t ivas_total_brate, const int16_t n_channels, float *p_res_buf, float *output[], float outputHB[][L_FRAME48k], const int16_t output_frame );
58 :
59 :
60 : /*--------------------------------------------------------------------------*
61 : * ivas_cpe_dec()
62 : *
63 : * Channel Pair Element (CPE) decoding routine
64 : *--------------------------------------------------------------------------*/
65 :
66 1261158 : ivas_error ivas_cpe_dec(
67 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
68 : const int16_t cpe_id, /* i : CPE # identifier */
69 : float *output[CPE_CHANNELS], /* o : output synthesis signal */
70 : const int16_t output_frame, /* i : output frame length per channel */
71 : const int16_t nb_bits_metadata /* i : number of metadata bits */
72 : )
73 : {
74 : int16_t i, n, n_channels;
75 : int16_t nb_bits, last_core;
76 : int16_t last_bwidth;
77 : int16_t tdm_ratio_idx;
78 : float outputHB[CPE_CHANNELS][L_FRAME48k]; /* 'float' buffer for output HB synthesis, both channels */
79 1261158 : float *res_buf = NULL;
80 : CPE_DEC_HANDLE hCPE;
81 : Decoder_State **sts;
82 : STEREO_DFT_CONFIG_DATA_HANDLE hConfigDft;
83 : int32_t ivas_total_brate;
84 : ivas_error error;
85 : int32_t cpe_brate;
86 : int32_t element_brate_ref;
87 :
88 1261158 : error = IVAS_ERR_OK;
89 :
90 1261158 : push_wmops( "ivas_cpe_dec" );
91 :
92 1261158 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
93 :
94 1261158 : hCPE = st_ivas->hCPE[cpe_id];
95 1261158 : sts = hCPE->hCoreCoder;
96 :
97 1261158 : last_core = sts[0]->last_core;
98 1261158 : last_bwidth = sts[0]->last_bwidth;
99 :
100 1261158 : sts[0]->BER_detect |= st_ivas->BER_detect;
101 1261158 : sts[1]->BER_detect |= st_ivas->BER_detect;
102 :
103 1261158 : element_brate_ref = hCPE->element_brate;
104 :
105 : /*------------------------------------------------------------------*
106 : * Read stereo technology info & audio bandwidth
107 : *-----------------------------------------------------------------*/
108 :
109 1261158 : stereo_mode_combined_format_dec( st_ivas, hCPE );
110 :
111 1261158 : read_stereo_mode_and_bwidth( hCPE, st_ivas );
112 :
113 : /*----------------------------------------------------------------*
114 : * dynamically allocate data structures depending on the actual stereo mode
115 : *----------------------------------------------------------------*/
116 :
117 1261158 : if ( ( error = stereo_memory_dec( ivas_total_brate, hCPE, nb_bits_metadata, st_ivas->hDecoderConfig->output_Fs, st_ivas->ivas_format, st_ivas->mc_mode, st_ivas->nchan_transport ) ) != IVAS_ERR_OK )
118 : {
119 0 : return error;
120 : }
121 :
122 1261158 : hConfigDft = NULL;
123 1261158 : if ( hCPE->hStereoDft != NULL )
124 : {
125 197886 : hConfigDft = hCPE->hStereoDft->hConfig;
126 : }
127 :
128 : /*------------------------------------------------------------------*
129 : * Initialization
130 : *-----------------------------------------------------------------*/
131 :
132 1261158 : n_channels = CPE_CHANNELS;
133 1261158 : if ( hCPE->element_mode == IVAS_CPE_DFT )
134 : {
135 179058 : n_channels = 1; /* in DFT stereo, only M channel is coded */
136 : }
137 :
138 1261158 : tdm_ratio_idx = LRTD_STEREO_RIGHT_IS_PRIM;
139 :
140 3604416 : for ( n = 0; n < n_channels; n++ )
141 : {
142 2343258 : sts[n]->idchan = n;
143 2343258 : sts[n]->element_mode = hCPE->element_mode;
144 :
145 2343258 : if ( !st_ivas->bfi )
146 : {
147 2297607 : sts[n]->tdm_LRTD_flag = 0;
148 : }
149 :
150 : #ifdef DEBUGGING
151 : sts[n]->id_element = cpe_id + st_ivas->nSCE;
152 : #endif
153 : /* TD stereo parameters */
154 2343258 : if ( hCPE->hStereoTD != NULL )
155 : {
156 22680 : hCPE->hStereoTD->tdm_lp_reuse_flag = 0;
157 22680 : hCPE->hStereoTD->tdm_low_rate_mode = 0;
158 22680 : hCPE->hStereoTD->tdm_Pitch_reuse_flag = 0;
159 : }
160 : }
161 :
162 : /*----------------------------------------------------------------*
163 : * Resets/updates in case of stereo switching
164 : *----------------------------------------------------------------*/
165 :
166 1261158 : stereo_switching_dec( hCPE, ivas_total_brate );
167 :
168 : /*----------------------------------------------------------------*
169 : * Configuration of stereo decoder
170 : *----------------------------------------------------------------*/
171 :
172 : /* Force to MODE1 in IVAS */
173 3604416 : for ( n = 0; n < n_channels; n++ )
174 : {
175 2343258 : sts[n]->codec_mode = MODE1;
176 : }
177 :
178 1261158 : if ( hCPE->element_mode != IVAS_CPE_MDCT && ( hCPE->element_brate != hCPE->last_element_brate || hCPE->last_element_mode != hCPE->element_mode || sts[0]->ini_frame == 0 || ( ivas_total_brate != st_ivas->hDecoderConfig->last_ivas_total_brate ) ) )
179 : {
180 13515 : if ( st_ivas->hQMetaData != NULL && ivas_total_brate > IVAS_SID_5k2 )
181 : {
182 3588 : if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
183 : {
184 1482 : stereo_dft_config( hConfigDft, (int32_t) ( 0.7f * st_ivas->hQMetaData->bits_frame_nominal * FRAMES_PER_SEC ), &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
185 : }
186 : else
187 : {
188 2106 : stereo_dft_config( hConfigDft, st_ivas->hQMetaData->bits_frame_nominal * FRAMES_PER_SEC, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
189 : }
190 : }
191 : else
192 : {
193 : /* Note: This only works for stereo operation. If DTX would be applied for multiple CPEs a different bitrate signaling is needed */
194 9927 : if ( ivas_total_brate <= IVAS_SID_5k2 )
195 : {
196 8163 : stereo_dft_config( hConfigDft, ivas_total_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
197 : }
198 : else
199 : {
200 1764 : stereo_dft_config( hConfigDft, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
201 : }
202 : }
203 : }
204 :
205 1261158 : if ( hCPE->element_mode == IVAS_CPE_TD )
206 : {
207 11340 : if ( hCPE->hStereoTD->tdm_LRTD_flag )
208 : {
209 10965 : sts[0]->bits_frame_nominal = (int16_t) ( ( hCPE->element_brate >> 1 ) / FRAMES_PER_SEC );
210 10965 : sts[1]->bits_frame_nominal = (int16_t) ( ( hCPE->element_brate >> 1 ) / FRAMES_PER_SEC );
211 : }
212 : else
213 : {
214 375 : stereo_dft_config( NULL, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
215 : }
216 : }
217 :
218 : /*----------------------------------------------------------------*
219 : * Set bitrates per channel
220 : * Set bitstream buffers per channel
221 : *----------------------------------------------------------------*/
222 :
223 1261158 : if ( hCPE->element_mode == IVAS_CPE_DFT )
224 : {
225 179058 : if ( !st_ivas->bfi )
226 : {
227 : /* Update DFT Stereo memories */
228 174639 : stereo_dft_dec_update( hCPE->hStereoDft, output_frame, 0 );
229 :
230 174639 : if ( st_ivas->ivas_format == MASA_FORMAT && ivas_total_brate <= IVAS_SID_5k2 )
231 : {
232 1590 : if ( ivas_total_brate == FRAME_NO_DATA )
233 : {
234 1368 : hCPE->hCoreCoder[n]->core_brate = ivas_total_brate;
235 1368 : hCPE->hCoreCoder[0]->total_brate = ivas_total_brate;
236 : }
237 : else
238 : {
239 222 : hCPE->hCoreCoder[n]->core_brate = SID_2k40;
240 : }
241 : }
242 :
243 : /* read DFT Stereo side info */
244 174639 : nb_bits = (int16_t) ( ( hCPE->element_brate ) / FRAMES_PER_SEC - 0.8f * sts[0]->bits_frame_nominal );
245 174639 : cpe_brate = st_ivas->hCPE[0]->element_brate;
246 174639 : if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
247 : {
248 8217 : sts[1]->bit_stream = sts[0]->bit_stream + cpe_brate / FRAMES_PER_SEC - 1 - nb_bits_metadata;
249 8217 : sts[1]->bit_stream += hCPE->brate_surplus / FRAMES_PER_SEC;
250 : }
251 : else
252 : {
253 166422 : sts[1]->bit_stream = sts[0]->bit_stream + ivas_total_brate / FRAMES_PER_SEC - 1 - nb_bits_metadata;
254 : }
255 :
256 174639 : if ( ivas_total_brate == IVAS_SID_5k2 )
257 : {
258 4287 : nb_bits -= SID_FORMAT_NBITS;
259 4287 : sts[1]->bit_stream -= SID_FORMAT_NBITS;
260 : /* set total bitrate of Stereo CNG parameters for BER detection */
261 4287 : sts[1]->total_brate = IVAS_SID_5k2 - SID_2k40;
262 : }
263 :
264 174639 : if ( ( ( st_ivas->ivas_format == MASA_FORMAT && ivas_total_brate < MASA_STEREO_MIN_BITRATE ) || ( st_ivas->ivas_format == MASA_ISM_FORMAT && cpe_brate < MASA_STEREO_MIN_BITRATE ) ) && ivas_total_brate > IVAS_SID_5k2 )
265 : {
266 16614 : sts[0]->total_brate = hCPE->element_brate; /* Only mono downmix was transmitted in this case */
267 : }
268 : else
269 : {
270 158025 : res_buf = outputHB[0]; /* note: temporarily reused buffer */
271 :
272 158025 : if ( st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT )
273 : {
274 18597 : nb_bits -= nb_bits_metadata;
275 18597 : if ( hCPE->brate_surplus < 0 )
276 : {
277 2805 : nb_bits += (int16_t) ( hCPE->brate_surplus / FRAMES_PER_SEC );
278 : }
279 : }
280 :
281 158025 : stereo_dft_dec_read_BS( ivas_total_brate, hCPE->element_brate, &sts[0]->total_brate, sts[1], hCPE->hStereoDft, sts[0]->bwidth, output_frame, res_buf, &nb_bits, hCPE->hStereoCng->coh, st_ivas->ivas_format );
282 : }
283 :
284 : /* subtract metadata bitbudget */
285 174639 : sts[0]->total_brate -= ( nb_bits_metadata * FRAMES_PER_SEC );
286 :
287 : /* subtract bit-rate for combined format coding */
288 174639 : if ( st_ivas->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 ) )
289 : {
290 8217 : sts[0]->total_brate += hCPE->brate_surplus;
291 : }
292 : }
293 : else
294 : {
295 4419 : hCPE->hStereoDft->sg_mem_corrupt = 1;
296 : }
297 : }
298 1082100 : else if ( hCPE->element_mode == IVAS_CPE_TD )
299 : {
300 : /* signal bitrate for BW selection in the SCh */
301 11340 : sts[0]->bits_frame_channel = 0;
302 11340 : sts[1]->bits_frame_channel = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC );
303 11340 : sts[1]->bits_frame_channel += (int16_t) ( hCPE->brate_surplus / FRAMES_PER_SEC );
304 11340 : if ( st_ivas->hQMetaData != NULL )
305 : {
306 180 : sts[1]->bits_frame_channel -= st_ivas->hQMetaData->metadata_max_bits;
307 : }
308 : }
309 1070760 : else if ( hCPE->element_mode == IVAS_CPE_MDCT )
310 : {
311 : /* compute bit-rate surplus per channel in combined format coding */
312 : int32_t brate_surplus[CPE_CHANNELS];
313 1070760 : if ( 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 )
314 : {
315 12846 : brate_surplus[0] = ( ( hCPE->brate_surplus / FRAMES_PER_SEC ) >> 1 ) * FRAMES_PER_SEC;
316 12846 : brate_surplus[1] = hCPE->brate_surplus - brate_surplus[0];
317 : }
318 :
319 1070760 : if ( is_DTXrate( ivas_total_brate ) == 1 && ( sts[0]->first_CNG == 0 || sts[1]->first_CNG == 0 ) )
320 : {
321 90 : if ( ( error = initMdctStereoDtxData( hCPE ) ) != IVAS_ERR_OK )
322 : {
323 0 : return error;
324 : }
325 : }
326 :
327 : /* this is just for initialization, the true values of "total_brate" and "bits_frame_channel" are set later */
328 3212280 : for ( n = 0; n < n_channels; n++ )
329 : {
330 2141520 : if ( ivas_total_brate == IVAS_SID_5k2 )
331 :
332 : {
333 3408 : sts[n]->total_brate = SID_2k40;
334 3408 : sts[1]->bit_stream = sts[0]->bit_stream + SID_2k40 / FRAMES_PER_SEC;
335 : }
336 : else
337 : {
338 : /*total bitrate must be set to the element bitrate to avoid false BER if bits read are larger than half the bitrate*/
339 2138112 : sts[n]->total_brate = hCPE->element_brate;
340 : }
341 2141520 : sts[n]->bits_frame_nominal = (int16_t) ( sts[n]->total_brate / FRAMES_PER_SEC );
342 2141520 : sts[n]->bits_frame_channel = (int16_t) ( ( hCPE->element_brate / FRAMES_PER_SEC ) / n_channels );
343 :
344 : /* subtract bit-rate for combined format coding */
345 2141520 : if ( 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 )
346 : {
347 25692 : sts[n]->bits_frame_channel += (int16_t) ( brate_surplus[n] / FRAMES_PER_SEC );
348 25692 : sts[n]->total_brate += brate_surplus[n];
349 : }
350 : }
351 :
352 1070760 : if ( !st_ivas->hMCT )
353 : {
354 259752 : if ( st_ivas->ivas_format == SBA_FORMAT && ivas_total_brate == IVAS_SID_5k2 )
355 : {
356 1503 : for ( n = 0; n < n_channels; n++ )
357 : {
358 1002 : sts[n]->bits_frame_channel -= nb_bits_metadata / n_channels;
359 : }
360 : }
361 : else
362 : {
363 : /* subtract metadata bitbudget */
364 259251 : sts[0]->bits_frame_channel -= nb_bits_metadata;
365 : }
366 : }
367 : }
368 :
369 : /*----------------------------------------------------------------*
370 : * Core codec configuration
371 : *----------------------------------------------------------------*/
372 :
373 3604416 : for ( n = 0; n < n_channels; n++ )
374 : {
375 : /* set ACELP12k8 / ACELP16k flag for flexible ACELP core */
376 2343258 : sts[n]->flag_ACELP16k = set_ACELP_flag( hCPE->element_mode, hCPE->element_brate, sts[n]->total_brate, n, ( hCPE->hStereoTD != NULL ? hCPE->hStereoTD->tdm_LRTD_flag : 0 ), sts[n]->bwidth, sts[n]->cng_type );
377 : }
378 :
379 3604416 : for ( n = 0; n < n_channels; n++ )
380 : {
381 : /* set VAD flag */
382 2343258 : if ( is_DTXrate( ivas_total_brate ) == 1 )
383 : {
384 55782 : sts[n]->VAD = 0;
385 55782 : sts[n]->active_cnt = 0;
386 55782 : if ( sts[1] != NULL )
387 : {
388 55782 : sts[1]->active_cnt = 0;
389 55782 : if ( sts[1]->ini_frame == 0 )
390 : {
391 30024 : sts[1]->active_cnt = CNG_TYPE_HO;
392 : }
393 : }
394 : }
395 : else
396 : {
397 2287476 : sts[n]->VAD = 1;
398 2287476 : sts[n]->active_cnt++;
399 2287476 : sts[n]->active_cnt = min( sts[n]->active_cnt, 200 );
400 : }
401 :
402 : /* set CNA flag */
403 2343258 : if ( ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->element_brate <= CNA_MAX_BRATE_DFT_STEREO ) || hCPE->element_brate <= CNA_MAX_BRATE_STEREO )
404 : {
405 206328 : sts[n]->flag_cna = 1;
406 : }
407 : else
408 : {
409 2136930 : sts[n]->flag_cna = 0;
410 : }
411 : }
412 :
413 : /* configure TD stereo decoder */
414 1261158 : if ( hCPE->element_mode == IVAS_CPE_TD )
415 : {
416 11340 : if ( !st_ivas->bfi )
417 : {
418 10956 : tdm_configure_dec( st_ivas->ivas_format, st_ivas->ism_mode, hCPE, &tdm_ratio_idx, nb_bits_metadata );
419 :
420 10956 : sts[1]->bit_stream = sts[0]->bit_stream + ( sts[0]->total_brate / FRAMES_PER_SEC );
421 : }
422 : else
423 : {
424 384 : sts[1]->coder_type = sts[1]->last_coder_type;
425 384 : tdm_ratio_idx = hCPE->hStereoTD->tdm_last_ratio_idx;
426 : }
427 : }
428 :
429 : /*----------------------------------------------------------------*
430 : * Core Decoder
431 : *----------------------------------------------------------------*/
432 :
433 1261158 : if ( hCPE->element_mode != IVAS_CPE_DFT || ( hCPE->nchan_out == 1 && hConfigDft->res_cod_mode == STEREO_DFT_RES_COD_OFF ) )
434 : {
435 1133439 : if ( ( error = ivas_core_dec( st_ivas, NULL, hCPE, st_ivas->hMCT, n_channels, output, outputHB, NULL, st_ivas->sba_dirac_stereo_flag ) ) != IVAS_ERR_OK )
436 : {
437 0 : return error;
438 : }
439 : }
440 :
441 1261158 : if ( st_ivas->hMCT )
442 : {
443 811008 : pop_wmops();
444 :
445 811008 : return error;
446 : }
447 :
448 : /*----------------------------------------------------------------*
449 : * Stereo decoder & upmixing
450 : *----------------------------------------------------------------*/
451 :
452 450150 : if ( hCPE->element_mode == IVAS_CPE_DFT && !( hCPE->nchan_out == 1 && hConfigDft->res_cod_mode == STEREO_DFT_RES_COD_OFF ) )
453 : {
454 127719 : if ( ( error = stereo_dft_dec_main( hCPE, ivas_total_brate, n_channels, res_buf, output, outputHB, output_frame ) ) != IVAS_ERR_OK )
455 : {
456 0 : return error;
457 : }
458 : }
459 322431 : else if ( hCPE->element_mode == IVAS_CPE_TD )
460 : {
461 11340 : if ( hCPE->last_element_mode != IVAS_CPE_TD && sts[0]->tdm_LRTD_flag )
462 : {
463 177 : hCPE->hStereoTD->tdm_last_ratio_idx = tdm_ratio_idx;
464 : }
465 :
466 : /* TD stereo upmixing */
467 11340 : stereo_tdm_combine( hCPE, output[0], output[1], output_frame, 0, tdm_ratio_idx );
468 11340 : if ( sts[0]->tdm_LRTD_flag )
469 : {
470 10965 : stereo_tdm_combine( hCPE, outputHB[0], outputHB[1], output_frame, 1, tdm_ratio_idx );
471 : }
472 :
473 11340 : hCPE->hStereoCng->last_tdm_idx = hCPE->hStereoTD->tdm_last_ratio_idx;
474 11340 : hCPE->hStereoTD->tdm_last_ratio_idx = tdm_ratio_idx;
475 :
476 11340 : if ( hCPE->nchan_out == 1 )
477 : {
478 : /* Scale the Right channel with the gain */
479 4059 : stereo_tca_scale_R_channel( hCPE, output[1], output_frame );
480 :
481 : /* stereo to mono downmix */
482 2434779 : for ( i = 0; i < output_frame; i++ )
483 : {
484 2430720 : output[0][i] = ( output[0][i] + output[1][i] ) * 0.5f;
485 : }
486 : }
487 : }
488 :
489 : /*----------------------------------------------------------------*
490 : * Update parameters for stereo CNA
491 : *----------------------------------------------------------------*/
492 :
493 450150 : stereo_cna_update_params( hCPE, output, output_frame, tdm_ratio_idx );
494 :
495 : /*----------------------------------------------------------------*
496 : * Synthesis synchronization between CPE modes
497 : *----------------------------------------------------------------*/
498 :
499 450150 : if ( !st_ivas->sba_dirac_stereo_flag )
500 : {
501 446139 : synchro_synthesis( ivas_total_brate, hCPE, output, output_frame, 0 );
502 : }
503 :
504 450150 : if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->nchan_out == 1 && ( is_DTXrate( ivas_total_brate ) == 0 || ( is_DTXrate( ivas_total_brate ) == 1 && is_DTXrate( st_ivas->hDecoderConfig->last_ivas_total_brate ) == 0 ) ) )
505 : {
506 17754 : applyDmxMdctStereo( hCPE, output, output_frame );
507 : }
508 :
509 : #ifndef DEBUG_STEREO_DFT_OUTRESPRED
510 : /*----------------------------------------------------------------*
511 : * IC-BWE: output LB and HB mix in ACELP mode
512 : *----------------------------------------------------------------*/
513 :
514 : #ifndef DEBUG_STEREO_DFT_NOCORE
515 450150 : stereo_icBWE_decproc( hCPE, output, outputHB, last_core, last_bwidth, output_frame );
516 : #endif
517 :
518 450150 : smooth_dft2td_transition( hCPE, output, output_frame );
519 :
520 : /*----------------------------------------------------------------*
521 : * Temporal ICA, stereo adjustment and upmix
522 : *----------------------------------------------------------------*/
523 :
524 450150 : stereo_tca_dec( hCPE, output, output_frame );
525 : #endif
526 :
527 : /*----------------------------------------------------------------*
528 : * Common Stereo updates
529 : *----------------------------------------------------------------*/
530 :
531 450150 : hCPE->last_element_brate = hCPE->element_brate;
532 450150 : hCPE->last_element_mode = hCPE->element_mode;
533 :
534 450150 : if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
535 : {
536 21252 : hCPE->element_brate = element_brate_ref;
537 : }
538 :
539 450150 : if ( hCPE->element_mode == IVAS_CPE_DFT || hCPE->element_mode == IVAS_CPE_TD )
540 : {
541 190398 : stereo_cng_dec_update( hCPE, ivas_total_brate );
542 : }
543 :
544 450150 : st_ivas->BER_detect |= sts[0]->BER_detect;
545 450150 : st_ivas->BER_detect |= sts[1]->BER_detect;
546 :
547 : #ifdef DEBUG_MODE_INFO
548 : {
549 : float tmpF = hCPE->element_brate / 1000.0f;
550 :
551 : n = 1;
552 : if ( st_ivas->ini_frame == 0 && frame > 0 )
553 : {
554 : /* in case first frame(s) is/are lost, write info several times */
555 : n = (int16_t) frame - st_ivas->ini_frame + 1;
556 : }
557 :
558 : for ( i = 0; i < n; i++ )
559 : {
560 : dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "element_brate", 0, cpe_id, DEC ) );
561 : dbgwrite( &hCPE->element_mode, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "element_mode", 0, cpe_id, DEC ) );
562 :
563 : for ( int16_t j = 0; j < CPE_CHANNELS; j++ )
564 : {
565 : dbgwrite( output[j], sizeof( float ), output_frame, 1, fname( debug_dir, "output.cpe", j, cpe_id, DEC ) );
566 : }
567 :
568 : if ( st_ivas->ivas_format != MASA_ISM_FORMAT )
569 : {
570 : tmpF = 0;
571 : dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "output.sce", 0, cpe_id, DEC ) );
572 : dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "output.mct", 0, cpe_id, DEC ) );
573 : }
574 : }
575 : }
576 : #endif
577 :
578 450150 : pop_wmops();
579 450150 : return error;
580 : }
581 :
582 :
583 : /*-------------------------------------------------------------------------
584 : * stereo_dft_dec_main()
585 : *
586 : * DFT decoder main function
587 : *-------------------------------------------------------------------------*/
588 :
589 127719 : static ivas_error stereo_dft_dec_main(
590 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
591 : const int32_t ivas_total_brate, /* i : IVAS total bitrate */
592 : const int16_t n_channels, /* i : number of channels to be decoded */
593 : float *p_res_buf, /* i : DFT stereo residual S signal */
594 : float *output[], /* o : output synthesis signal */
595 : float outputHB[][L_FRAME48k], /* o : output HB synthesis signal */
596 : const int16_t output_frame /* i : output frame length per channel */
597 : )
598 : {
599 : float DFT[CPE_CHANNELS][STEREO_DFT_BUF_MAX];
600 : int16_t n;
601 : Decoder_State *st0;
602 : ivas_error error;
603 :
604 127719 : st0 = hCPE->hCoreCoder[0];
605 :
606 : /* copy from temporary buffer */
607 127719 : if ( hCPE->hStereoDft->res_cod_band_max > 0 && !st0->bfi )
608 : {
609 50169 : mvr2r( p_res_buf, DFT[1], STEREO_DFT_N_8k );
610 : }
611 :
612 : /* core decoder */
613 127719 : if ( ( error = ivas_core_dec( NULL, NULL, hCPE, NULL, n_channels, output, outputHB, DFT, 0 ) ) != IVAS_ERR_OK )
614 : {
615 0 : return error;
616 : }
617 :
618 : /* DFT Stereo residual decoding */
619 127719 : if ( hCPE->hStereoDft->res_cod_band_max > 0 && !st0->bfi )
620 : {
621 50169 : stereo_dft_dec_res( hCPE, DFT[1] /*res_buf*/, output[1] );
622 :
623 50169 : stereo_dft_dec_analyze( hCPE, output[1], DFT, 1, L_FRAME8k, output_frame, DFT_STEREO_DEC_ANA_LB, 0, 0 );
624 : }
625 :
626 : /* DFT stereo CNG */
627 127719 : stereo_dtf_cng( hCPE, ivas_total_brate, DFT, output_frame );
628 :
629 : /* decoding */
630 127719 : if ( hCPE->nchan_out == 1 )
631 : {
632 16026 : stereo_dft_unify_dmx( hCPE->hStereoDft, st0, DFT, hCPE->input_mem[1], hCPE->hStereoCng->prev_sid_nodata );
633 : }
634 : else
635 : {
636 111693 : stereo_dft_dec( hCPE->hStereoDft, st0, DFT, hCPE->input_mem[1], hCPE->hStereoCng, 0, 0, 0, 0, 0, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
637 : }
638 :
639 : /* synthesis iFFT */
640 367131 : for ( n = 0; n < hCPE->nchan_out; n++ )
641 : {
642 239412 : stereo_dft_dec_synthesize( hCPE, DFT, n, output[n], output_frame );
643 : }
644 :
645 127719 : return IVAS_ERR_OK;
646 : }
647 :
648 :
649 : /*-------------------------------------------------------------------------
650 : * create_cpe_dec()
651 : *
652 : * Create, allocate and initialize IVAS decoder CPE handle
653 : *-------------------------------------------------------------------------*/
654 :
655 8883 : ivas_error create_cpe_dec(
656 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
657 : const int16_t cpe_id, /* i : CPE # identifier */
658 : const int32_t element_brate /* i : element bitrate */
659 : )
660 : {
661 : int16_t i, n;
662 : CPE_DEC_HANDLE hCPE;
663 : Decoder_State *st;
664 : int32_t output_Fs;
665 : ivas_error error;
666 : int32_t cpe_brate;
667 :
668 8883 : error = IVAS_ERR_OK;
669 :
670 : /*-----------------------------------------------------------------*
671 : * Allocate CPE handle
672 : *-----------------------------------------------------------------*/
673 :
674 8883 : if ( ( hCPE = (CPE_DEC_HANDLE) malloc( sizeof( CPE_DEC_DATA ) ) ) == NULL )
675 : {
676 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CPE\n" ) );
677 : }
678 :
679 : /*-----------------------------------------------------------------*
680 : * Initialization - general parameters
681 : *-----------------------------------------------------------------*/
682 :
683 8883 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
684 :
685 8883 : hCPE->cpe_id = cpe_id;
686 :
687 8883 : hCPE->element_brate = element_brate;
688 8883 : hCPE->last_element_brate = hCPE->element_brate;
689 8883 : hCPE->element_mode = st_ivas->element_mode_init;
690 8883 : hCPE->last_element_mode = st_ivas->element_mode_init;
691 :
692 8883 : hCPE->hStereoDft = NULL;
693 8883 : hCPE->hStereoDftDmx = NULL;
694 8883 : hCPE->hStereoTD = NULL;
695 8883 : hCPE->hStereoMdct = NULL;
696 8883 : hCPE->hStereoTCA = NULL;
697 8883 : hCPE->hStereoICBWE = NULL;
698 8883 : hCPE->hStereoCng = NULL;
699 :
700 8883 : hCPE->stereo_switching_counter = 10;
701 8883 : hCPE->NbFrameMod = 7;
702 8883 : hCPE->lt_es_em = 0.0f;
703 :
704 : /* Note: nchan_out is considered to be related to the structure. This is nchan_out for CPE and for MASA_format is always 2. */
705 8883 : if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT || st_ivas->ivas_format == MC_FORMAT )
706 : {
707 6357 : hCPE->nchan_out = CPE_CHANNELS;
708 : }
709 : else
710 : {
711 2526 : hCPE->nchan_out = min( CPE_CHANNELS, st_ivas->hDecoderConfig->nchan_out );
712 : }
713 :
714 8883 : if ( st_ivas->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 ) )
715 : {
716 111 : cpe_brate = element_brate;
717 : }
718 : else
719 : {
720 8772 : cpe_brate = st_ivas->hDecoderConfig->ivas_total_brate;
721 : }
722 :
723 8883 : if ( ( ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->hDecoderConfig->ivas_total_brate < MASA_STEREO_MIN_BITRATE ) ||
724 8826 : ( st_ivas->ivas_format == MASA_ISM_FORMAT && cpe_brate < MASA_STEREO_MIN_BITRATE ) ) &&
725 84 : st_ivas->hDecoderConfig->ivas_total_brate > IVAS_SID_5k2 )
726 : {
727 84 : hCPE->nchan_out = 1;
728 : }
729 :
730 26649 : for ( n = 0; n < CPE_CHANNELS; n++ )
731 : {
732 17766 : set_f( hCPE->prev_hb_synth[n], 0, NS2SA( output_Fs, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS ) );
733 17766 : set_f( hCPE->prev_synth[n], 0, NS2SA( output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS ) );
734 : }
735 :
736 8883 : hCPE->brate_surplus = 0;
737 :
738 : /*-----------------------------------------------------------------*
739 : * DFT stereo I/O Buffers: allocate and initialize
740 : *-----------------------------------------------------------------*/
741 :
742 26649 : for ( i = 0; i < CPE_CHANNELS; i++ )
743 : {
744 17766 : if ( st_ivas->ivas_format == STEREO_FORMAT || st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT ||
745 16830 : ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCMASA ) || st_ivas->sba_dirac_stereo_flag )
746 : {
747 2382 : if ( ( hCPE->input_mem[i] = (float *) malloc( sizeof( float ) * NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) ) ) == NULL )
748 : {
749 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
750 : }
751 2382 : set_zero( hCPE->input_mem[i], NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
752 :
753 2382 : if ( ( hCPE->input_mem_LB[i] = (float *) malloc( sizeof( float ) * STEREO_DFT32MS_OVL_16k ) ) == NULL )
754 : {
755 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
756 : }
757 2382 : set_zero( hCPE->input_mem_LB[i], STEREO_DFT32MS_OVL_16k );
758 :
759 2382 : if ( i == 0 )
760 : {
761 1191 : if ( ( hCPE->input_mem_BPF[0] = (float *) malloc( sizeof( float ) * STEREO_DFT32MS_OVL_16k ) ) == NULL )
762 : {
763 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
764 : }
765 1191 : set_zero( hCPE->input_mem_BPF[0], STEREO_DFT32MS_OVL_16k );
766 : }
767 :
768 2382 : if ( ( hCPE->output_mem[i] = (float *) malloc( sizeof( float ) * NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) ) ) == NULL )
769 : {
770 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
771 : }
772 2382 : set_zero( hCPE->output_mem[i], NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
773 :
774 2382 : if ( i < hCPE->nchan_out )
775 : {
776 2232 : if ( ( hCPE->prev_synth_chs[i] = (float *) malloc( sizeof( float ) * NS2SA( output_Fs, FRAME_SIZE_NS ) ) ) == NULL )
777 : {
778 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
779 : }
780 2232 : set_zero( hCPE->prev_synth_chs[i], NS2SA( output_Fs, FRAME_SIZE_NS ) );
781 : }
782 : else
783 : {
784 150 : hCPE->prev_synth_chs[i] = NULL;
785 : }
786 : }
787 : else
788 : {
789 15384 : hCPE->input_mem[i] = NULL;
790 15384 : hCPE->input_mem_LB[i] = NULL;
791 15384 : if ( i == 0 )
792 : {
793 7692 : hCPE->input_mem_BPF[0] = NULL;
794 : }
795 15384 : hCPE->output_mem[i] = NULL;
796 15384 : hCPE->prev_synth_chs[i] = NULL;
797 : }
798 : }
799 :
800 : /*-----------------------------------------------------------------*
801 : * CoreCoder, 2 instances: allocate and initialize
802 : *-----------------------------------------------------------------*/
803 :
804 26427 : for ( n = 0; n < CPE_CHANNELS; n++ )
805 : {
806 17655 : if ( st_ivas->sba_dirac_stereo_flag && st_ivas->nchan_transport == 1 )
807 : {
808 : /* for SBA DirAC stereo output CPE element is only used for upmix, core coder is found in SCE element used for core decoding */
809 111 : break;
810 : }
811 :
812 17544 : if ( ( st = (DEC_CORE_HANDLE) malloc( sizeof( Decoder_State ) ) ) == NULL )
813 : {
814 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
815 : }
816 :
817 17544 : copy_decoder_config( st_ivas, st );
818 :
819 17544 : st->total_brate = hCPE->element_brate / ( CPE_CHANNELS ); /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
820 17544 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
821 17544 : st->is_ism_format = 0;
822 :
823 17544 : if ( ( error = init_decoder( st, n, st_ivas->mc_mode ) ) != IVAS_ERR_OK )
824 : {
825 0 : return error;
826 : }
827 :
828 17544 : hCPE->hCoreCoder[n] = st;
829 : }
830 :
831 : /*-----------------------------------------------------------------*
832 : * DFT stereo initialization
833 : *-----------------------------------------------------------------*/
834 :
835 8883 : if ( hCPE->element_mode == IVAS_CPE_DFT || ( st_ivas->sba_dirac_stereo_flag && hCPE->cpe_id == 0 ) )
836 : {
837 585 : if ( ( error = stereo_dft_dec_create( &( hCPE->hStereoDft ), hCPE->element_brate, output_Fs, st_ivas->sba_dirac_stereo_flag, st_ivas->nchan_transport ) ) != IVAS_ERR_OK )
838 : {
839 0 : return error;
840 : }
841 : }
842 :
843 : /*-----------------------------------------------------------------*
844 : * DFT stereo mono DMX initialization
845 : *-----------------------------------------------------------------*/
846 :
847 8883 : if ( hCPE->element_mode != IVAS_CPE_MDCT && hCPE->nchan_out == 1 )
848 : {
849 135 : if ( ( hCPE->hStereoDftDmx = (STEREO_DFT_DMX_DATA_HANDLE) malloc( sizeof( STEREO_DFT_DMX_DATA ) ) ) == NULL )
850 : {
851 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo DFT mono output\n" ) );
852 : }
853 :
854 135 : stereo_dft_dmx_out_reset( hCPE->hStereoDftDmx );
855 : }
856 :
857 : /*-----------------------------------------------------------------*
858 : * Temporal inter-channel alignment initialization
859 : *-----------------------------------------------------------------*/
860 :
861 8883 : if ( ( hCPE->element_mode != IVAS_CPE_MDCT || ( st_ivas->ivas_format == STEREO_FORMAT && hCPE->element_brate <= MAX_MDCT_ITD_BRATE ) ) && hCPE->nchan_out != 1 )
862 : {
863 300 : if ( ( hCPE->hStereoTCA = (STEREO_TCA_DEC_HANDLE) malloc( sizeof( STEREO_TCA_DEC_DATA ) ) ) == NULL )
864 : {
865 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
866 : }
867 :
868 300 : stereo_tca_init_dec( hCPE->hStereoTCA );
869 : }
870 :
871 : /*-----------------------------------------------------------------*
872 : * Stereo IC BWE initialization
873 : *-----------------------------------------------------------------*/
874 :
875 8883 : if ( hCPE->element_mode != IVAS_CPE_MDCT && !( hCPE->element_mode == IVAS_CPE_DFT && hCPE->nchan_out == 1 ) )
876 : {
877 273 : if ( ( hCPE->hStereoICBWE = (STEREO_ICBWE_DEC_HANDLE) malloc( sizeof( STEREO_ICBWE_DEC_DATA ) ) ) == NULL )
878 : {
879 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE\n" ) );
880 : }
881 :
882 273 : stereo_icBWE_init_dec( hCPE->hStereoICBWE );
883 : }
884 :
885 : /*-----------------------------------------------------------------*
886 : * TD stereo initialization
887 : *-----------------------------------------------------------------*/
888 :
889 8883 : if ( hCPE->element_mode == IVAS_CPE_TD )
890 : {
891 0 : if ( ( hCPE->hStereoTD = (STEREO_TD_DEC_DATA_HANDLE) malloc( sizeof( STEREO_TD_DEC_DATA ) ) ) == NULL )
892 : {
893 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD Stereo\n" ) );
894 : }
895 :
896 0 : stereo_td_init_dec( hCPE->hStereoTD, hCPE->last_element_mode );
897 : }
898 :
899 : /*-----------------------------------------------------------------*
900 : * MDCT stereo initialization
901 : *-----------------------------------------------------------------*/
902 :
903 8883 : if ( hCPE->element_mode == IVAS_CPE_MDCT && st_ivas->nCPE == 1 )
904 : {
905 1155 : if ( ( hCPE->hStereoMdct = (STEREO_MDCT_DEC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_DEC_DATA ) ) ) == NULL )
906 : {
907 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo MDCT\n" ) );
908 : }
909 :
910 1155 : if ( st_ivas->ivas_format == STEREO_FORMAT && hCPE->element_brate <= MAX_MDCT_ITD_BRATE )
911 : {
912 39 : hCPE->hStereoMdct->use_itd = 1;
913 : }
914 : else
915 : {
916 1116 : hCPE->hStereoMdct->use_itd = 0;
917 : }
918 :
919 1155 : hCPE->hStereoMdct->reverse_dmx = 0;
920 1155 : hCPE->hStereoMdct->smooth_ratio = 1.f;
921 1155 : set_s( hCPE->hStereoMdct->prev_ms_mask[0], 0, MAX_SFB );
922 1155 : set_s( hCPE->hStereoMdct->prev_ms_mask[1], 0, MAX_SFB );
923 1155 : hCPE->hStereoMdct->lastCoh = 1.f;
924 1155 : hCPE->hStereoMdct->mdct_stereo_mode[0] = SMDCT_DUAL_MONO;
925 1155 : hCPE->hStereoMdct->mdct_stereo_mode[1] = SMDCT_DUAL_MONO;
926 1155 : hCPE->hStereoMdct->IGFStereoMode[0] = -1;
927 1155 : hCPE->hStereoMdct->IGFStereoMode[1] = -1;
928 : }
929 :
930 : /*-----------------------------------------------------------------*
931 : * Stereo CNG initialization
932 : *-----------------------------------------------------------------*/
933 :
934 8883 : if ( hCPE->element_mode == IVAS_CPE_TD || hCPE->element_mode == IVAS_CPE_DFT )
935 : {
936 297 : if ( ( hCPE->hStereoCng = (STEREO_CNG_DEC_HANDLE) malloc( sizeof( STEREO_CNG_DEC ) ) ) == NULL )
937 : {
938 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo CNG\n" ) );
939 : }
940 297 : stereo_cng_init_dec( hCPE->hStereoCng, &hCPE->hCoreCoder[0]->hFdCngDec->hFdCngCom->frameSize );
941 : }
942 :
943 8883 : st_ivas->hCPE[cpe_id] = hCPE;
944 :
945 8883 : return error;
946 : }
947 :
948 :
949 : /*-------------------------------------------------------------------------
950 : * destroy_cpe_dec()
951 : *
952 : * Destroy and deallocate IVAS decoder CPE handle
953 : *-------------------------------------------------------------------------*/
954 :
955 8883 : void destroy_cpe_dec(
956 : CPE_DEC_HANDLE hCPE /* i/o: CPE decoder structure */
957 : )
958 : {
959 : int16_t n;
960 : Decoder_State *st;
961 :
962 26649 : for ( n = 0; n < CPE_CHANNELS; n++ )
963 : {
964 17766 : st = hCPE->hCoreCoder[n];
965 :
966 17766 : if ( st != NULL )
967 : {
968 17232 : destroy_core_dec( st );
969 :
970 17232 : free( st );
971 17232 : st = NULL;
972 : }
973 : }
974 :
975 8883 : if ( hCPE->hStereoDft != NULL )
976 : {
977 531 : stereo_dft_dec_destroy( &( hCPE->hStereoDft ) );
978 531 : hCPE->hStereoDft = NULL;
979 : }
980 :
981 8883 : if ( hCPE->hStereoDftDmx != NULL )
982 : {
983 96 : free( hCPE->hStereoDftDmx );
984 96 : hCPE->hStereoDftDmx = NULL;
985 : }
986 :
987 8883 : if ( hCPE->hStereoTD != NULL )
988 : {
989 3 : free( hCPE->hStereoTD );
990 3 : hCPE->hStereoTD = NULL;
991 : }
992 :
993 8883 : if ( hCPE->hStereoMdct != NULL )
994 : {
995 1245 : free( hCPE->hStereoMdct );
996 1245 : hCPE->hStereoMdct = NULL;
997 : }
998 :
999 8883 : if ( hCPE->hStereoTCA != NULL )
1000 : {
1001 294 : free( hCPE->hStereoTCA );
1002 294 : hCPE->hStereoTCA = NULL;
1003 : }
1004 :
1005 8883 : if ( hCPE->hStereoICBWE != NULL )
1006 : {
1007 312 : free( hCPE->hStereoICBWE );
1008 312 : hCPE->hStereoICBWE = NULL;
1009 : }
1010 :
1011 8883 : if ( hCPE->input_mem_LB[0] != NULL )
1012 : {
1013 3573 : for ( n = 0; n < CPE_CHANNELS; n++ )
1014 : {
1015 2382 : free( hCPE->input_mem_LB[n] );
1016 2382 : hCPE->input_mem_LB[n] = NULL;
1017 2382 : free( hCPE->input_mem[n] );
1018 2382 : hCPE->input_mem[n] = NULL;
1019 2382 : free( hCPE->output_mem[n] );
1020 2382 : hCPE->output_mem[n] = NULL;
1021 2382 : if ( hCPE->prev_synth_chs[n] != NULL )
1022 : {
1023 2268 : free( hCPE->prev_synth_chs[n] );
1024 2268 : hCPE->prev_synth_chs[n] = NULL;
1025 : }
1026 : }
1027 1191 : free( hCPE->input_mem_BPF[0] );
1028 1191 : hCPE->input_mem_BPF[0] = NULL;
1029 : }
1030 :
1031 8883 : if ( hCPE->hStereoCng != NULL )
1032 : {
1033 246 : free( hCPE->hStereoCng );
1034 246 : hCPE->hStereoCng = NULL;
1035 : }
1036 :
1037 8883 : free( hCPE );
1038 :
1039 8883 : return;
1040 : }
1041 :
1042 :
1043 : /*-------------------------------------------------------------------------
1044 : * read_stereo_mode_and_bwidth()
1045 : *
1046 : * Read stereo technology info & audio bandwidth
1047 : *-------------------------------------------------------------------------*/
1048 :
1049 1261158 : static void read_stereo_mode_and_bwidth(
1050 : CPE_DEC_HANDLE hCPE, /* i/o: CPE handle */
1051 : const Decoder_Struct *st_ivas /* i : decoder main structure */
1052 : )
1053 : {
1054 : Decoder_State **sts;
1055 :
1056 : /*-----------------------------------------------------------------*
1057 : * BFI or NO_DATA frame: Use stereo parameters from last (active) frame
1058 : *-----------------------------------------------------------------*/
1059 :
1060 1261158 : if ( st_ivas->bfi || st_ivas->hDecoderConfig->ivas_total_brate < IVAS_SID_5k2 )
1061 : {
1062 62337 : hCPE->element_mode = hCPE->last_element_mode;
1063 : }
1064 :
1065 : /*-----------------------------------------------------------------*
1066 : * SID frame: get element mode from SID side info
1067 : *-----------------------------------------------------------------*/
1068 :
1069 1198821 : else if ( st_ivas->hDecoderConfig->ivas_total_brate == IVAS_SID_5k2 )
1070 : {
1071 5991 : switch ( st_ivas->sid_format )
1072 : {
1073 4065 : case SID_DFT_STEREO:
1074 4065 : hCPE->element_mode = IVAS_CPE_DFT;
1075 : /* Read CNG type */
1076 4065 : hCPE->hCoreCoder[0]->cng_type = get_next_indice( hCPE->hCoreCoder[0], 1 );
1077 :
1078 : /* Read BW information in SID */
1079 4065 : hCPE->hCoreCoder[0]->bwidth = get_next_indice( hCPE->hCoreCoder[0], 2 );
1080 4065 : break;
1081 1704 : case SID_MDCT_STEREO:
1082 : /* 2TC SBA DTX also uses MDCT-Stereo DTX */
1083 : case SID_SBA_2TC:
1084 1704 : hCPE->element_mode = IVAS_CPE_MDCT;
1085 1704 : break;
1086 0 : case SID_SBA_1TC:
1087 0 : assert( !"Forbidden value for SID format in CPE (SBA 1TC), should have already been adressed earlier" );
1088 : break;
1089 0 : case SID_MASA_1TC:
1090 0 : hCPE->element_mode = IVAS_SCE;
1091 0 : break;
1092 222 : case SID_MASA_2TC:
1093 : /* 2TC MASA DTX uses MDCT or DFT based core */
1094 222 : if ( hCPE->element_mode == IVAS_CPE_TD )
1095 : {
1096 0 : hCPE->element_mode = IVAS_CPE_DFT;
1097 : }
1098 222 : if ( hCPE->element_mode == IVAS_CPE_DFT )
1099 : {
1100 : /* Read CNG type */
1101 222 : hCPE->hCoreCoder[0]->cng_type = get_next_indice( hCPE->hCoreCoder[0], 1 );
1102 :
1103 : /* Read BW information in SID */
1104 222 : hCPE->hCoreCoder[0]->bwidth = get_next_indice( hCPE->hCoreCoder[0], 2 );
1105 : }
1106 222 : break;
1107 0 : default:
1108 : /* this is what has been done for all modes previously, may need adaptation in the future */
1109 0 : hCPE->element_mode = hCPE->last_element_mode;
1110 0 : break;
1111 : }
1112 : }
1113 :
1114 : /*-----------------------------------------------------------------*
1115 : * active frame: read element mode and audio bandwidth info
1116 : *-----------------------------------------------------------------*/
1117 :
1118 : else
1119 : {
1120 1192830 : sts = hCPE->hCoreCoder;
1121 1192830 : if ( st_ivas->hMCT && hCPE->cpe_id != 0 )
1122 : {
1123 500982 : sts[0]->bwidth = st_ivas->hCPE[0]->hCoreCoder[0]->bwidth;
1124 500982 : sts[1]->bwidth = st_ivas->hCPE[0]->hCoreCoder[0]->bwidth;
1125 : }
1126 : else
1127 : {
1128 : /* read stereo technology info */
1129 691848 : if ( hCPE->element_brate < MIN_BRATE_MDCT_STEREO && st_ivas->hMCT == NULL )
1130 : {
1131 154791 : hCPE->element_mode = get_next_indice( sts[0], NBITS_ELEMENT_MODE ) + IVAS_CPE_DFT;
1132 : }
1133 : else
1134 : {
1135 537057 : hCPE->element_mode = IVAS_CPE_MDCT;
1136 : }
1137 :
1138 : /* read the bandwidth */
1139 691848 : if ( hCPE->element_brate < MIN_BRATE_FB_STEREO )
1140 : {
1141 : /* WB and SWB are supported */
1142 95658 : sts[0]->bwidth = get_next_indice( sts[0], 1 ) + WB;
1143 95658 : sts[1]->bwidth = sts[0]->bwidth;
1144 : }
1145 : else
1146 : {
1147 : /* WB, SWB and FB are supported */
1148 596190 : sts[0]->bwidth = get_next_indice( sts[0], NBITS_BWIDTH );
1149 596190 : sts[1]->bwidth = sts[0]->bwidth;
1150 : }
1151 : }
1152 : }
1153 :
1154 1261158 : return;
1155 : }
1156 :
1157 :
1158 : /*-------------------------------------------------------------------------
1159 : * stereo_mode_combined_format_dec()
1160 : *
1161 : * Set stereo format in a combined format
1162 : *-------------------------------------------------------------------------*/
1163 :
1164 1261158 : static void stereo_mode_combined_format_dec(
1165 : const Decoder_Struct *st_ivas, /* i : decoder main structure */
1166 : CPE_DEC_HANDLE hCPE /* i/o: CPE handle */
1167 : )
1168 : {
1169 : int32_t element_brate_ref;
1170 :
1171 1261158 : if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
1172 : {
1173 21252 : element_brate_ref = hCPE->element_brate;
1174 :
1175 21252 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC &&
1176 9447 : ( ( st_ivas->nchan_ism == 3 && st_ivas->hDecoderConfig->ivas_total_brate == IVAS_96k ) ||
1177 8367 : ( st_ivas->nchan_ism == 4 && st_ivas->hDecoderConfig->ivas_total_brate == IVAS_128k ) ) )
1178 : {
1179 1278 : if ( !st_ivas->bfi )
1180 : {
1181 : /* read OMASA stereo mode signalling */
1182 1272 : if ( get_next_indice( hCPE->hCoreCoder[0], NBITS_ELEMENT_MODE ) )
1183 : {
1184 0 : hCPE->element_mode = IVAS_CPE_MDCT;
1185 : }
1186 : else
1187 : {
1188 1272 : hCPE->element_mode = IVAS_CPE_DFT;
1189 : }
1190 : }
1191 :
1192 1278 : if ( hCPE->element_mode == IVAS_CPE_MDCT )
1193 : {
1194 0 : hCPE->element_brate = IVAS_64k;
1195 0 : hCPE->brate_surplus -= ( hCPE->element_brate - element_brate_ref );
1196 : }
1197 : }
1198 : }
1199 :
1200 1261158 : return;
1201 : }
|