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_fx.h"
40 : #include "ivas_prot_fx.h"
41 : #include "ivas_rom_com.h"
42 : #include "wmc_auto.h"
43 : #include <math.h>
44 :
45 :
46 : /*--------------------------------------------------------------------------*
47 : * Local function prototypes
48 : *--------------------------------------------------------------------------*/
49 :
50 : static void read_stereo_mode_and_bwidth_fx( CPE_DEC_HANDLE hCPE, const Decoder_Struct *st_ivas );
51 :
52 : static void stereo_mode_combined_format_dec_fx( const Decoder_Struct *st_ivas, CPE_DEC_HANDLE hCPE );
53 :
54 : #ifdef FIX_1320_STACK_CPE_DECODER
55 : static ivas_error stereo_dft_dec_main( CPE_DEC_HANDLE hCPE, const Word32 ivas_total_brate, const Word16 n_channels, Word32 *p_res_buf_fx, Word32 *output[], Word32 outputHB[][L_FRAME48k], const Word16 output_frame, const Word32 output_Fs );
56 : #endif
57 :
58 :
59 : /*--------------------------------------------------------------------------*
60 : * ivas_cpe_dec_fx()
61 : *
62 : * Channel Pair Element (CPE) decoding routine
63 : *--------------------------------------------------------------------------*/
64 :
65 421974 : ivas_error ivas_cpe_dec_fx(
66 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
67 : const Word16 cpe_id, /* i : CPE # identifier */
68 : Word32 *output[CPE_CHANNELS], /* o : output synthesis signal Q11*/
69 : const Word16 output_frame, /* i : output frame length per channel */
70 : const Word16 nb_bits_metadata /* i : number of metadata bits */
71 : )
72 : {
73 : Word16 i, n, n_channels;
74 : Word16 nb_bits, last_core;
75 : Word16 last_bwidth;
76 : Word16 tdm_ratio_idx;
77 : Word32 outputHB_fx[CPE_CHANNELS][L_FRAME48k]; /* buffer for output HB synthesis, both channels */ /* Q11 */
78 : #ifdef FIX_1320_STACK_CPE_DECODER
79 421974 : Word32 *res_buf_fx = NULL; /* Q8 */
80 : #else
81 : Word16 q_res_buf;
82 : Word32 res_buf_fx[STEREO_DFT_N_8k]; /* Q(q_res_buf) */
83 : #endif
84 : CPE_DEC_HANDLE hCPE;
85 : STEREO_DFT_CONFIG_DATA_HANDLE hConfigDft;
86 : Decoder_State **sts;
87 : Word32 ivas_total_brate;
88 : ivas_error error;
89 : Word32 cpe_brate;
90 : Word32 element_brate_ref;
91 :
92 421974 : error = IVAS_ERR_OK;
93 421974 : move32();
94 : #ifndef FIX_1320_STACK_CPE_DECODER
95 : q_res_buf = Q8;
96 : move16();
97 : #endif
98 :
99 421974 : push_wmops( "ivas_cpe_dec" );
100 :
101 421974 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
102 421974 : move32();
103 :
104 421974 : hCPE = st_ivas->hCPE[cpe_id];
105 421974 : sts = hCPE->hCoreCoder;
106 :
107 421974 : last_core = sts[0]->last_core;
108 421974 : move16();
109 421974 : last_bwidth = sts[0]->last_bwidth;
110 421974 : move16();
111 :
112 421974 : sts[0]->BER_detect = s_or( sts[0]->BER_detect, st_ivas->BER_detect );
113 421974 : sts[1]->BER_detect = s_or( sts[1]->BER_detect, st_ivas->BER_detect );
114 421974 : move16();
115 421974 : move16();
116 :
117 421974 : element_brate_ref = hCPE->element_brate;
118 421974 : move32();
119 :
120 : /*------------------------------------------------------------------*
121 : * Read stereo technology info & audio bandwidth
122 : *-----------------------------------------------------------------*/
123 :
124 421974 : stereo_mode_combined_format_dec_fx( st_ivas, hCPE );
125 :
126 421974 : read_stereo_mode_and_bwidth_fx( hCPE, st_ivas );
127 :
128 : /*----------------------------------------------------------------*
129 : * dynamically allocate data structures depending on the actual stereo mode
130 : *----------------------------------------------------------------*/
131 :
132 1265922 : FOR( Word16 ind1 = 0; ind1 < 2; ind1++ )
133 : {
134 843948 : test();
135 843948 : IF( hCPE->hCoreCoder[ind1] && hCPE->hCoreCoder[ind1]->hHQ_core )
136 : {
137 780465 : Copy_Scale_sig_16_32_no_sat( hCPE->hCoreCoder[ind1]->hHQ_core->old_out_fx, hCPE->hCoreCoder[ind1]->hHQ_core->old_out_fx32, L_FRAME48k, sub( Q11, hCPE->hCoreCoder[ind1]->hHQ_core->Q_old_wtda ) ); // Q11
138 : }
139 : }
140 1265922 : FOR( Word16 ind1 = 0; ind1 < 2; ind1++ )
141 : {
142 843948 : IF( hCPE->hCoreCoder[ind1]->hHQ_core )
143 : {
144 780465 : Copy_Scale_sig_16_32_no_sat( hCPE->hCoreCoder[ind1]->hHQ_core->old_out_LB_fx, hCPE->hCoreCoder[ind1]->hHQ_core->old_out_LB_fx32, L_FRAME32k, sub( Q11, hCPE->hCoreCoder[ind1]->hHQ_core->Q_old_wtda_LB ) ); // Q11
145 780465 : hCPE->hCoreCoder[ind1]->hHQ_core->Q_old_out_fx32 = Q11;
146 780465 : move16();
147 : }
148 : }
149 :
150 421974 : IF( NE_32( ( error = stereo_memory_dec_fx( 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 ) )
151 : {
152 0 : return error;
153 : }
154 :
155 1265922 : FOR( Word16 ind1 = 0; ind1 < 2; ind1++ )
156 : {
157 843948 : test();
158 843948 : IF( hCPE->hCoreCoder[ind1] && hCPE->hCoreCoder[ind1]->hHQ_core )
159 : {
160 780482 : Copy_Scale_sig_32_16( hCPE->hCoreCoder[ind1]->hHQ_core->old_out_fx32, hCPE->hCoreCoder[ind1]->hHQ_core->old_out_fx, L_FRAME48k, sub( hCPE->hCoreCoder[ind1]->hHQ_core->Q_old_wtda, Q11 ) ); // Q_old_wtda
161 : }
162 : }
163 1265922 : FOR( Word16 ind1 = 0; ind1 < 2; ind1++ )
164 : {
165 843948 : test();
166 843948 : IF( hCPE->hCoreCoder[ind1] && hCPE->hCoreCoder[ind1]->hHQ_core )
167 : {
168 780482 : Copy_Scale_sig_32_16( hCPE->hCoreCoder[ind1]->hHQ_core->old_out_LB_fx32, hCPE->hCoreCoder[ind1]->hHQ_core->old_out_LB_fx, L_FRAME32k, sub( hCPE->hCoreCoder[ind1]->hHQ_core->Q_old_wtda_LB, Q11 ) ); // Q_old_wtda_LB
169 : }
170 : }
171 :
172 421974 : hConfigDft = NULL;
173 421974 : if ( hCPE->hStereoDft != NULL )
174 : {
175 65943 : hConfigDft = hCPE->hStereoDft->hConfig;
176 : }
177 :
178 : /*------------------------------------------------------------------*
179 : * Initialization
180 : *-----------------------------------------------------------------*/
181 :
182 421974 : n_channels = CPE_CHANNELS;
183 421974 : move16();
184 421974 : if ( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
185 : {
186 59667 : n_channels = 1; /* in DFT stereo, only M channel is coded */
187 59667 : move16();
188 : }
189 :
190 421974 : tdm_ratio_idx = LRTD_STEREO_RIGHT_IS_PRIM;
191 421974 : move16();
192 :
193 1206255 : FOR( n = 0; n < n_channels; n++ )
194 : {
195 784281 : sts[n]->idchan = n;
196 784281 : move16();
197 784281 : sts[n]->element_mode = hCPE->element_mode;
198 784281 : move16();
199 :
200 784281 : if ( !st_ivas->bfi )
201 : {
202 769013 : sts[n]->tdm_LRTD_flag = 0;
203 769013 : move16();
204 : }
205 :
206 : /* TD stereo parameters */
207 784281 : IF( hCPE->hStereoTD != NULL )
208 : {
209 7598 : hCPE->hStereoTD->tdm_lp_reuse_flag = 0;
210 7598 : move16();
211 7598 : hCPE->hStereoTD->tdm_low_rate_mode = 0;
212 7598 : move16();
213 7598 : hCPE->hStereoTD->tdm_Pitch_reuse_flag = 0;
214 7598 : move16();
215 : }
216 : }
217 :
218 : /*----------------------------------------------------------------*
219 : * Resets/updates in case of stereo switching
220 : *----------------------------------------------------------------*/
221 :
222 421974 : stereo_switching_dec( hCPE, ivas_total_brate );
223 :
224 : /*----------------------------------------------------------------*
225 : * Configuration of stereo decoder
226 : *----------------------------------------------------------------*/
227 :
228 : /* Force to MODE1 in IVAS */
229 1206255 : FOR( n = 0; n < n_channels; n++ )
230 : {
231 784281 : sts[n]->codec_mode = MODE1;
232 784281 : move16();
233 : }
234 :
235 421974 : test();
236 421974 : test();
237 421974 : test();
238 421974 : test();
239 421974 : IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) && ( NE_32( hCPE->element_brate, hCPE->last_element_brate ) || NE_16( hCPE->last_element_mode, hCPE->element_mode ) || sts[0]->ini_frame == 0 || ( NE_32( ivas_total_brate, st_ivas->hDecoderConfig->last_ivas_total_brate ) ) ) )
240 : {
241 4565 : test();
242 4565 : IF( st_ivas->hQMetaData != NULL && GT_32( ivas_total_brate, IVAS_SID_5k2 ) )
243 : {
244 1196 : IF( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
245 : {
246 494 : stereo_dft_config_fx( hConfigDft, L_mult0( st_ivas->hQMetaData->bits_frame_nominal, 35 /* 0.7f * FRAMES_PER_SEC */ ), &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
247 : }
248 : ELSE
249 : {
250 702 : stereo_dft_config_fx( hConfigDft, L_mult0( st_ivas->hQMetaData->bits_frame_nominal, FRAMES_PER_SEC ), &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
251 : }
252 : }
253 : ELSE
254 : {
255 : /* Note: This only works for stereo operation. If DTX would be applied for multiple CPEs a different bitrate signaling is needed */
256 3369 : IF( LE_32( ivas_total_brate, IVAS_SID_5k2 ) )
257 : {
258 2765 : stereo_dft_config_fx( hConfigDft, ivas_total_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
259 : }
260 : ELSE
261 : {
262 604 : stereo_dft_config_fx( hConfigDft, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
263 : }
264 : }
265 : }
266 :
267 421974 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
268 : {
269 3799 : IF( hCPE->hStereoTD->tdm_LRTD_flag )
270 : {
271 3678 : i = extract_l( Mpy_32_32_r( L_shr( hCPE->element_brate, 1 ), ONE_BY_FRAMES_PER_SEC_Q31 ) );
272 3678 : sts[0]->bits_frame_nominal = i;
273 3678 : sts[1]->bits_frame_nominal = i;
274 3678 : move16();
275 3678 : move16();
276 : }
277 : ELSE
278 : {
279 121 : stereo_dft_config_fx( NULL, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
280 : }
281 : }
282 :
283 : /*----------------------------------------------------------------*
284 : * Set bitrates per channel
285 : * Set bitstream buffers per channel
286 : *----------------------------------------------------------------*/
287 :
288 421974 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
289 : {
290 59667 : IF( !st_ivas->bfi )
291 : {
292 : /* Update DFT Stereo memories */
293 58201 : stereo_dft_dec_update_fx( hCPE->hStereoDft, output_frame, 0 );
294 :
295 58201 : test();
296 58201 : IF( EQ_32( st_ivas->ivas_format, MASA_FORMAT ) && LE_32( ivas_total_brate, IVAS_SID_5k2 ) )
297 : {
298 528 : IF( ivas_total_brate == FRAME_NO_DATA )
299 : {
300 454 : hCPE->hCoreCoder[n]->core_brate = ivas_total_brate;
301 454 : move32();
302 454 : hCPE->hCoreCoder[0]->total_brate = ivas_total_brate;
303 454 : move32();
304 : }
305 : ELSE
306 : {
307 74 : hCPE->hCoreCoder[n]->core_brate = SID_2k40;
308 74 : move32();
309 : }
310 : }
311 :
312 : /* read DFT Stereo side info */
313 58201 : nb_bits = extract_h( L_msu( Mpy_32_16_1( L_shl( hCPE->element_brate, 15 ), 1311 /* 1/FRAMES_PER_SEC in Q16*/ ), 26214, sts[0]->bits_frame_nominal ) );
314 58201 : cpe_brate = st_ivas->hCPE[0]->element_brate;
315 58201 : move32();
316 58201 : IF( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
317 : {
318 2739 : i = extract_l( Mpy_32_32_r( cpe_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
319 2739 : sts[1]->bit_stream = sts[0]->bit_stream + sub( sub( i, 1 ), nb_bits_metadata );
320 2739 : IF( hCPE->brate_surplus < 0 )
321 : {
322 2250 : i = negate( extract_l( Mpy_32_32_r( L_abs( hCPE->brate_surplus ), ONE_BY_FRAMES_PER_SEC_Q31 ) ) );
323 : }
324 : ELSE
325 : {
326 489 : i = extract_l( Mpy_32_32_r( hCPE->brate_surplus, ONE_BY_FRAMES_PER_SEC_Q31 ) );
327 : }
328 2739 : sts[1]->bit_stream = sts[1]->bit_stream + i;
329 : }
330 : ELSE
331 : {
332 55462 : i = extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
333 55462 : sts[1]->bit_stream = sts[0]->bit_stream + sub( sub( i, 1 ), nb_bits_metadata );
334 : }
335 :
336 58201 : IF( EQ_32( ivas_total_brate, IVAS_SID_5k2 ) )
337 : {
338 1443 : nb_bits = sub( nb_bits, SID_FORMAT_NBITS );
339 1443 : sts[1]->bit_stream = sts[1]->bit_stream - SID_FORMAT_NBITS;
340 : /* set total bitrate of Stereo CNG parameters for BER detection */
341 1443 : sts[1]->total_brate = IVAS_SID_5k2 - SID_2k40;
342 1443 : move32();
343 : }
344 :
345 58201 : test();
346 58201 : test();
347 58201 : test();
348 58201 : test();
349 58201 : IF( ( ( EQ_32( st_ivas->ivas_format, MASA_FORMAT ) && LT_32( ivas_total_brate, MASA_STEREO_MIN_BITRATE ) ) || ( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) && LT_32( cpe_brate, MASA_STEREO_MIN_BITRATE ) ) ) && GT_32( ivas_total_brate, IVAS_SID_5k2 ) )
350 : {
351 5539 : sts[0]->total_brate = hCPE->element_brate; /* Only mono downmix was transmitted in this case */
352 5539 : move32();
353 : }
354 : ELSE
355 : {
356 : #ifdef FIX_1320_STACK_CPE_DECODER
357 52662 : res_buf_fx = outputHB_fx[0]; /* note: temporarily reused buffer */
358 :
359 : #endif
360 52662 : test();
361 52662 : IF( EQ_32( st_ivas->ivas_format, MASA_FORMAT ) || EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
362 : {
363 6198 : nb_bits = sub( nb_bits, nb_bits_metadata );
364 6198 : IF( hCPE->brate_surplus < 0 )
365 : {
366 935 : i = negate( extract_l( Mpy_32_32_r( L_abs( hCPE->brate_surplus ), ONE_BY_FRAMES_PER_SEC_Q31 ) ) );
367 935 : nb_bits = add( nb_bits, i );
368 : }
369 : }
370 :
371 52662 : stereo_dft_dec_read_BS_fx( ivas_total_brate, hCPE->element_brate, &sts[0]->total_brate, sts[1], hCPE->hStereoDft, sts[0]->bwidth, output_frame, res_buf_fx, &nb_bits, hCPE->hStereoCng->coh_fx, st_ivas->ivas_format );
372 : }
373 :
374 : /* subtract metadata bitbudget */
375 58201 : sts[0]->total_brate = L_sub( sts[0]->total_brate, L_mult0( nb_bits_metadata, FRAMES_PER_SEC ) ); // Q0
376 58201 : move32();
377 :
378 : /* subtract bit-rate for combined format coding */
379 58201 : test();
380 58201 : test();
381 58201 : test();
382 58201 : IF( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) && ( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) ) )
383 : {
384 2739 : sts[0]->total_brate = L_add( sts[0]->total_brate, hCPE->brate_surplus );
385 2739 : move32();
386 : }
387 : }
388 : ELSE
389 : {
390 1466 : hCPE->hStereoDft->sg_mem_corrupt = 1;
391 1466 : move16();
392 : }
393 : }
394 362307 : ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
395 : {
396 : /* signal bitrate for BW selection in the SCh */
397 3799 : sts[0]->bits_frame_channel = 0;
398 3799 : move16();
399 3799 : sts[1]->bits_frame_channel = extract_l( Mpy_32_32_r( hCPE->element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
400 3799 : move16();
401 3799 : IF( hCPE->brate_surplus < 0 )
402 : {
403 0 : i = negate( extract_l( Mpy_32_32_r( L_abs( hCPE->brate_surplus ), ONE_BY_FRAMES_PER_SEC_Q31 ) ) );
404 : }
405 : ELSE
406 : {
407 3799 : i = extract_l( Mpy_32_32_r( hCPE->brate_surplus, ONE_BY_FRAMES_PER_SEC_Q31 ) );
408 : }
409 3799 : sts[1]->bits_frame_channel = add( sts[1]->bits_frame_channel, i );
410 3799 : move16();
411 3799 : IF( st_ivas->hQMetaData != NULL )
412 : {
413 60 : sts[1]->bits_frame_channel = sub( sts[1]->bits_frame_channel, st_ivas->hQMetaData->metadata_max_bits );
414 60 : move16();
415 : }
416 : }
417 358508 : ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
418 : {
419 : /* compute bit-rate surplus per channel in combined format coding */
420 : Word32 brate_surplus[CPE_CHANNELS];
421 358508 : test();
422 358508 : test();
423 358508 : IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
424 : {
425 4282 : IF( hCPE->brate_surplus < 0 )
426 : {
427 4100 : i = negate( extract_l( Mpy_32_32_r( L_abs( hCPE->brate_surplus ), ONE_BY_FRAMES_PER_SEC_Q31 ) ) );
428 : }
429 : ELSE
430 : {
431 182 : i = extract_l( Mpy_32_32_r( hCPE->brate_surplus, ONE_BY_FRAMES_PER_SEC_Q31 ) );
432 : }
433 4282 : brate_surplus[0] = L_shr( L_mult( shr( i, 1 ), FRAMES_PER_SEC ), 1 );
434 4282 : move32();
435 4282 : brate_surplus[1] = L_sub( hCPE->brate_surplus, brate_surplus[0] );
436 4282 : move32();
437 : }
438 :
439 358508 : test();
440 358508 : test();
441 358508 : IF( EQ_16( is_DTXrate( ivas_total_brate ), 1 ) && ( sts[0]->first_CNG == 0 || sts[1]->first_CNG == 0 ) )
442 : {
443 29 : IF( NE_32( ( error = initMdctStereoDtxData_fx( hCPE ) ), IVAS_ERR_OK ) )
444 : {
445 0 : return error;
446 : }
447 : }
448 :
449 : /* this is just for initialization, the true values of "total_brate" and "bits_frame_channel" are set later */
450 1075524 : FOR( n = 0; n < n_channels; n++ )
451 : {
452 717016 : IF( EQ_32( ivas_total_brate, IVAS_SID_5k2 ) )
453 : {
454 892 : sts[n]->total_brate = SID_2k40;
455 892 : move32();
456 892 : sts[1]->bit_stream = sts[0]->bit_stream + SID_2k40 / FRAMES_PER_SEC;
457 : }
458 : ELSE
459 : {
460 : /*total bitrate must be set to the element bitrate to avoid false BER IF bits read are larger than half the bitrate*/
461 716124 : sts[n]->total_brate = hCPE->element_brate;
462 716124 : move32();
463 : }
464 717016 : sts[n]->bits_frame_nominal = extract_l( Mpy_32_32_r( sts[n]->total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
465 717016 : i = extract_l( Mpy_32_32_r( hCPE->element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
466 717016 : sts[n]->bits_frame_channel = shr( i, sub( n_channels, 1 ) );
467 717016 : move16();
468 717016 : move16();
469 :
470 : /* subtract bit-rate for combined format coding */
471 717016 : test();
472 717016 : test();
473 717016 : IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
474 : {
475 8564 : IF( brate_surplus[n] < 0 )
476 : {
477 8200 : i = negate( extract_l( Mpy_32_32_r( L_abs( brate_surplus[n] ), ONE_BY_FRAMES_PER_SEC_Q31 ) ) );
478 : }
479 : ELSE
480 : {
481 364 : i = extract_l( Mpy_32_32_r( brate_surplus[n], ONE_BY_FRAMES_PER_SEC_Q31 ) );
482 : }
483 8564 : sts[n]->bits_frame_channel = add( sts[n]->bits_frame_channel, i );
484 8564 : sts[n]->total_brate = L_add( sts[n]->total_brate, brate_surplus[n] );
485 8564 : move16();
486 8564 : move32();
487 : }
488 : }
489 :
490 358508 : IF( !st_ivas->hMCT )
491 : {
492 86544 : test();
493 86544 : IF( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) && EQ_32( ivas_total_brate, IVAS_SID_5k2 ) )
494 : {
495 132 : FOR( n = 0; n < n_channels; n++ )
496 : {
497 88 : sts[n]->bits_frame_channel = sub( sts[n]->bits_frame_channel, shr( nb_bits_metadata, sub( n_channels, 1 ) ) );
498 88 : move16();
499 : }
500 : }
501 : ELSE
502 : {
503 : /* subtract metadata bitbudget */
504 86500 : sts[0]->bits_frame_channel = sub( sts[0]->bits_frame_channel, nb_bits_metadata );
505 86500 : move16();
506 : }
507 : }
508 : }
509 :
510 : /*----------------------------------------------------------------*
511 : * Core codec configuration
512 : *----------------------------------------------------------------*/
513 :
514 1206255 : FOR( n = 0; n < n_channels; n++ )
515 : {
516 : /* set ACELP12k8 / ACELP16k flag for flexible ACELP core */
517 784281 : IF( hCPE->hStereoTD != NULL )
518 : {
519 7598 : sts[n]->flag_ACELP16k = set_ACELP_flag_IVAS( hCPE->element_mode, hCPE->element_brate, sts[n]->total_brate, n, hCPE->hStereoTD->tdm_LRTD_flag, sts[n]->bwidth, sts[n]->cng_type );
520 7598 : move16();
521 : }
522 : ELSE
523 : {
524 776683 : sts[n]->flag_ACELP16k = set_ACELP_flag_IVAS( hCPE->element_mode, hCPE->element_brate, sts[n]->total_brate, n, 0, sts[n]->bwidth, sts[n]->cng_type );
525 776683 : move16();
526 : }
527 : }
528 :
529 1206255 : FOR( n = 0; n < n_channels; n++ )
530 : {
531 : /* set VAD flag */
532 784281 : IF( EQ_16( is_DTXrate( ivas_total_brate ), 1 ) )
533 : {
534 16719 : sts[n]->VAD = 0;
535 16719 : move16();
536 16719 : sts[n]->active_cnt = 0;
537 16719 : move16();
538 16719 : IF( sts[1] != NULL )
539 : {
540 16719 : sts[1]->active_cnt = 0;
541 16719 : move16();
542 16719 : if ( sts[1]->ini_frame == 0 )
543 : {
544 10059 : sts[1]->active_cnt = CNG_TYPE_HO;
545 10059 : move16();
546 : }
547 : }
548 : }
549 : ELSE
550 : {
551 767562 : sts[n]->VAD = 1;
552 767562 : move16();
553 767562 : sts[n]->active_cnt = add( sts[n]->active_cnt, 1 );
554 767562 : sts[n]->active_cnt = s_min( sts[n]->active_cnt, 200 );
555 767562 : move16();
556 767562 : move16();
557 : }
558 :
559 : /* set CNA flag */
560 784281 : test();
561 784281 : test();
562 784281 : IF( ( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && LE_32( hCPE->element_brate, CNA_MAX_BRATE_DFT_STEREO ) ) || LE_32( hCPE->element_brate, CNA_MAX_BRATE_STEREO ) )
563 : {
564 66851 : sts[n]->flag_cna = 1;
565 66851 : move16();
566 : }
567 : ELSE
568 : {
569 717430 : sts[n]->flag_cna = 0;
570 717430 : move16();
571 : }
572 : }
573 :
574 : /* configure TD stereo decoder */
575 421974 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
576 : {
577 3799 : IF( !st_ivas->bfi )
578 : {
579 3672 : tdm_configure_dec_fx( st_ivas->ivas_format, st_ivas->ism_mode, hCPE, &tdm_ratio_idx, nb_bits_metadata );
580 :
581 3672 : i = extract_l( Mpy_32_32_r( sts[0]->total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
582 3672 : sts[1]->bit_stream = sts[0]->bit_stream + i;
583 : }
584 : ELSE
585 : {
586 127 : sts[1]->coder_type = sts[1]->last_coder_type;
587 127 : move16();
588 127 : tdm_ratio_idx = hCPE->hStereoTD->tdm_last_ratio_idx;
589 127 : move16();
590 : }
591 : }
592 :
593 : /*----------------------------------------------------------------*
594 : * Core Decoder
595 : *----------------------------------------------------------------*/
596 :
597 421974 : test();
598 421974 : test();
599 421974 : IF( NE_16( hCPE->element_mode, IVAS_CPE_DFT ) || ( EQ_16( hCPE->nchan_out, 1 ) && EQ_16( hConfigDft->res_cod_mode, STEREO_DFT_RES_COD_OFF ) ) )
600 : {
601 379415 : IF( NE_32( ( error = ivas_core_dec_fx( st_ivas, NULL, hCPE, st_ivas->hMCT, n_channels, output, outputHB_fx, NULL, st_ivas->sba_dirac_stereo_flag ) ), IVAS_ERR_OK ) )
602 : {
603 0 : return error;
604 : }
605 : }
606 :
607 421974 : IF( st_ivas->hMCT )
608 : {
609 271964 : pop_wmops();
610 :
611 271964 : return error;
612 : }
613 :
614 : /*----------------------------------------------------------------*
615 : * Stereo decoder & upmixing
616 : *----------------------------------------------------------------*/
617 :
618 150010 : test();
619 150010 : test();
620 150010 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && !( EQ_16( hCPE->nchan_out, 1 ) && EQ_16( hConfigDft->res_cod_mode, STEREO_DFT_RES_COD_OFF ) ) )
621 : {
622 : #ifdef FIX_1320_STACK_CPE_DECODER
623 42559 : IF( NE_32( ( error = stereo_dft_dec_main( hCPE, ivas_total_brate, n_channels, res_buf_fx, output, outputHB_fx, output_frame, st_ivas->hDecoderConfig->output_Fs ) ), IVAS_ERR_OK ) )
624 : {
625 0 : return error;
626 : }
627 : #else
628 : Word32 DFT_fx[CPE_CHANNELS][STEREO_DFT_BUF_MAX]; // q_dft
629 : set32_fx( DFT_fx[0], 0, STEREO_DFT_BUF_MAX );
630 : set32_fx( DFT_fx[1], 0, STEREO_DFT_BUF_MAX );
631 :
632 : /* core decoder */
633 : IF( NE_32( ( error = ivas_core_dec_fx( NULL, NULL, hCPE, st_ivas->hMCT, n_channels, output, outputHB_fx, DFT_fx, 0 ) ), IVAS_ERR_OK ) )
634 : {
635 : return error;
636 : }
637 :
638 : // Scaling of DFT's
639 : Word16 shift;
640 : Word32 tmp1, tmp2;
641 : Word16 shift1, shift2;
642 : maximum_abs_32_fx( DFT_fx[0], STEREO_DFT_BUF_MAX, &tmp1 );
643 : maximum_abs_32_fx( DFT_fx[1], STEREO_DFT_BUF_MAX, &tmp2 );
644 :
645 : IF( tmp1 == 0 )
646 : {
647 : shift1 = Q31;
648 : move16();
649 : }
650 : ELSE
651 : {
652 : shift1 = norm_l( tmp1 );
653 : }
654 : IF( tmp2 == 0 )
655 : {
656 : shift2 = Q31;
657 : move16();
658 : }
659 : ELSE
660 : {
661 : shift2 = norm_l( tmp2 );
662 : }
663 : shift = s_min( shift1, shift2 );
664 :
665 : IF( NE_16( shift, 31 ) )
666 : {
667 : shift = sub( add( hCPE->hStereoDft->q_dft, shift ), Q17 ); /* Q17 for guard bits */
668 :
669 : IF( GT_16( shift, hCPE->hStereoDft->q_dft ) )
670 : {
671 : Scale_sig32( DFT_fx[0], STEREO_DFT_BUF_MAX, sub( shift, hCPE->hStereoDft->q_dft ) ); // shift
672 : Scale_sig32( DFT_fx[1], STEREO_DFT_BUF_MAX, sub( shift, hCPE->hStereoDft->q_dft ) ); // shift
673 : hCPE->hStereoDft->q_dft = shift;
674 : move16();
675 : }
676 : }
677 : ELSE
678 : {
679 : hCPE->hStereoDft->q_dft = Q8;
680 : move16();
681 : }
682 :
683 : /* DFT Stereo residual decoding */
684 : test();
685 : IF( hCPE->hStereoDft->res_cod_band_max > 0 && !st_ivas->bfi )
686 : {
687 : Word16 q;
688 : Word16 q_out_DFT[2];
689 :
690 : q = Q11;
691 : move16();
692 :
693 : Copy_Scale_sig_32_16( hCPE->hCoreCoder[0]->old_pitch_buf_fx, hCPE->hCoreCoder[0]->old_pitch_buf_16_fx, ( ( 2 * NB_SUBFR16k ) + 2 ), -( Q10 ) ); // Q16->Q6
694 :
695 : stereo_dft_dec_res_fx( hCPE, res_buf_fx, q_res_buf, output[1] );
696 :
697 : Copy_Scale_sig_16_32_DEPREC( hCPE->hCoreCoder[0]->old_pitch_buf_16_fx, hCPE->hCoreCoder[0]->old_pitch_buf_fx, ( ( 2 * NB_SUBFR16k ) + 2 ), 10 ); // Q6->Q16
698 :
699 : Scale_sig32( output[1], L_FRAME8k, ( Q11 - Q15 ) ); // Q15 -> Q11
700 :
701 : q_out_DFT[0] = q_out_DFT[1] = hCPE->hStereoDft->q_dft;
702 : move16();
703 : move16();
704 :
705 : stereo_dft_dec_analyze_fx( hCPE, output[1], DFT_fx, 1, L_FRAME8k, output_frame, DFT_STEREO_DEC_ANA_LB, 0, 0, &q, q_out_DFT );
706 :
707 : Scale_sig32( DFT_fx[1], STEREO_DFT_BUF_MAX, sub( hCPE->hStereoDft->q_dft, q_out_DFT[1] ) ); // q_dft
708 : }
709 :
710 : /* DFT stereo CNG */
711 : {
712 : Word16 q_dft;
713 :
714 : q_dft = hCPE->hStereoDft->q_dft;
715 : move16();
716 :
717 : stereo_dtf_cng_fx( hCPE, ivas_total_brate, DFT_fx, output_frame, q_dft );
718 : }
719 :
720 : /* decoding */
721 : IF( EQ_16( hCPE->nchan_out, 1 ) )
722 : {
723 : IF( EQ_16( hCPE->hStereoDft->first_frame, 1 ) )
724 : {
725 : hCPE->hStereoDft->q_smoothed_nrg = hCPE->hStereoDft->q_dft;
726 : move16();
727 : FOR( Word16 ii = 0; ii < (Word16) ( sizeof( hCPE->hStereoDft->q_DFT_past_DMX_fx ) / sizeof( hCPE->hStereoDft->q_DFT_past_DMX_fx[0] ) ); ii++ )
728 : {
729 : hCPE->hStereoDft->q_DFT_past_DMX_fx[ii] = hCPE->hStereoDft->q_dft;
730 : move16();
731 : }
732 : hCPE->hStereoDft->first_frame = 0;
733 : move16();
734 : }
735 :
736 : scale_sig32( hCPE->hStereoDft->res_cod_mem_fx, STEREO_DFT_OVL_8k, sub( hCPE->hStereoDft->q_dft, hCPE->hStereoDft->q_res_cod_mem_fx ) ); // q_dft
737 : hCPE->hStereoDft->q_res_cod_mem_fx = hCPE->hStereoDft->q_dft;
738 : move16();
739 : stereo_dft_unify_dmx_fx( hCPE->hStereoDft, sts[0], DFT_fx, hCPE->input_mem_fx[1], hCPE->hStereoCng->prev_sid_nodata );
740 : scale_sig32( hCPE->hStereoDft->res_cod_mem_fx, STEREO_DFT_OVL_8k, sub( Q15, hCPE->hStereoDft->q_res_cod_mem_fx ) ); // Q15
741 : hCPE->hStereoDft->q_res_cod_mem_fx = Q15;
742 : move16();
743 : }
744 : ELSE
745 : {
746 : {
747 : IF( EQ_16( hCPE->hStereoDft->first_frame, 1 ) )
748 : {
749 : hCPE->hStereoDft->q_smoothed_nrg = hCPE->hStereoDft->q_dft;
750 : move16();
751 : FOR( Word16 ii = 0; ii < (Word16) ( sizeof( hCPE->hStereoDft->q_DFT_past_DMX_fx ) / sizeof( hCPE->hStereoDft->q_DFT_past_DMX_fx[0] ) ); ii++ )
752 : {
753 : hCPE->hStereoDft->q_DFT_past_DMX_fx[ii] = hCPE->hStereoDft->q_dft;
754 : move16();
755 : }
756 : hCPE->hStereoDft->first_frame = 0;
757 : move16();
758 : }
759 :
760 : scale_sig32( hCPE->hStereoDft->res_cod_mem_fx, STEREO_DFT_OVL_8k, sub( hCPE->hStereoDft->q_dft, hCPE->hStereoDft->q_res_cod_mem_fx ) ); // q_dft
761 : hCPE->hStereoDft->q_res_cod_mem_fx = hCPE->hStereoDft->q_dft;
762 : move16();
763 : }
764 : stereo_dft_dec_fx( hCPE->hStereoDft, sts[0], DFT_fx, hCPE->input_mem_fx[1], hCPE->hStereoCng, 0, 0, 0, 0, 0, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
765 : scale_sig32( hCPE->hStereoDft->res_cod_mem_fx, STEREO_DFT_OVL_8k, sub( Q15, hCPE->hStereoDft->q_res_cod_mem_fx ) ); // Q15
766 : hCPE->hStereoDft->q_res_cod_mem_fx = Q15;
767 : move16();
768 : }
769 :
770 : FOR( n = 0; n < hCPE->nchan_out; n++ )
771 : {
772 : Scale_sig32( output[n], L_FRAME48k, sub( hCPE->hStereoDft->q_dft, Q11 ) ); // q_dft
773 : scale_sig32( hCPE->output_mem_fx[n], NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, STEREO_DFT32MS_OVL_NS ), sub( hCPE->hStereoDft->q_dft, Q11 ) ); // q_dft
774 : hCPE->q_output_mem_fx[n] = hCPE->hStereoDft->q_dft;
775 : move16();
776 : }
777 :
778 : /* synthesis iFFT */
779 : FOR( n = 0; n < hCPE->nchan_out; n++ )
780 : {
781 : stereo_dft_dec_synthesize_fx( hCPE, DFT_fx, n, output[n], output_frame );
782 : }
783 :
784 : // delete below
785 : FOR( n = 0; n < hCPE->nchan_out; n++ )
786 : {
787 : Scale_sig32( output[n], L_FRAME48k, sub( Q11, hCPE->hStereoDft->q_dft ) ); // Q11
788 : scale_sig32( hCPE->output_mem_fx[n], NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, STEREO_DFT32MS_OVL_NS ), sub( Q11, hCPE->hStereoDft->q_dft ) ); // Q11
789 : hCPE->q_output_mem_fx[n] = Q11;
790 : move16();
791 : }
792 : #endif
793 : }
794 107451 : ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
795 : {
796 3799 : test();
797 3799 : if ( NE_16( hCPE->last_element_mode, IVAS_CPE_TD ) && sts[0]->tdm_LRTD_flag )
798 : {
799 60 : hCPE->hStereoTD->tdm_last_ratio_idx = tdm_ratio_idx;
800 60 : move16();
801 : }
802 :
803 : /* TD stereo upmixing */
804 3799 : stereo_tdm_combine_fx( hCPE, output[0], output[1], output_frame, 0, tdm_ratio_idx );
805 3799 : IF( sts[0]->tdm_LRTD_flag )
806 : {
807 3678 : stereo_tdm_combine_fx( hCPE, outputHB_fx[0], outputHB_fx[1], output_frame, 0, tdm_ratio_idx );
808 : }
809 :
810 3799 : hCPE->hStereoCng->last_tdm_idx = hCPE->hStereoTD->tdm_last_ratio_idx;
811 3799 : move16();
812 3799 : hCPE->hStereoTD->tdm_last_ratio_idx = tdm_ratio_idx;
813 3799 : move16();
814 :
815 3799 : IF( EQ_16( hCPE->nchan_out, 1 ) )
816 : {
817 : /* Scale the Right channel with the gain */
818 1353 : stereo_tca_scale_R_channel_fx( hCPE, output[1], output_frame );
819 : /* stereo to mono downmix */
820 810953 : FOR( i = 0; i < output_frame; i++ )
821 : {
822 809600 : output[0][i] = L_shr( L_add( output[0][i], output[1][i] ), 1 );
823 809600 : move32();
824 : }
825 : }
826 : }
827 :
828 : /*----------------------------------------------------------------*
829 : * Update parameters for stereo CNA
830 : *----------------------------------------------------------------*/
831 :
832 150010 : stereo_cna_update_params_fx( hCPE, output, output_frame, tdm_ratio_idx );
833 :
834 : /*----------------------------------------------------------------*
835 : * Synthesis synchronization between CPE modes
836 : *----------------------------------------------------------------*/
837 :
838 150010 : IF( !st_ivas->sba_dirac_stereo_flag )
839 : {
840 148673 : synchro_synthesis_fx( ivas_total_brate, hCPE, output, output_frame, 0, Q11 );
841 : }
842 :
843 150010 : test();
844 150010 : test();
845 150010 : test();
846 150010 : test();
847 150010 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && EQ_16( hCPE->nchan_out, 1 ) && ( is_DTXrate( ivas_total_brate ) == 0 || ( EQ_16( is_DTXrate( ivas_total_brate ), 1 ) && is_DTXrate( st_ivas->hDecoderConfig->last_ivas_total_brate ) == 0 ) ) )
848 : {
849 5917 : applyDmxMdctStereo_fx( hCPE, output, output_frame );
850 : }
851 :
852 : /*----------------------------------------------------------------*
853 : * IC-BWE: output LB and HB mix in ACELP mode
854 : *----------------------------------------------------------------*/
855 :
856 150010 : stereo_icBWE_decproc_fx( hCPE, output, outputHB_fx, last_core, last_bwidth, output_frame );
857 150010 : smooth_dft2td_transition_fx( hCPE, output, output_frame );
858 :
859 : /*----------------------------------------------------------------*
860 : * Temporal ICA, stereo adjustment and upmix
861 : *----------------------------------------------------------------*/
862 :
863 150010 : stereo_tca_dec_fx( hCPE, output, output_frame );
864 :
865 : /*----------------------------------------------------------------*
866 : * Common Stereo updates
867 : *----------------------------------------------------------------*/
868 :
869 150010 : hCPE->last_element_brate = hCPE->element_brate;
870 150010 : move32();
871 150010 : hCPE->last_element_mode = hCPE->element_mode;
872 150010 : move32();
873 :
874 150010 : if ( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
875 : {
876 7084 : hCPE->element_brate = element_brate_ref;
877 7084 : move32();
878 : }
879 :
880 150010 : test();
881 150010 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) || EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
882 : {
883 63466 : stereo_cng_dec_update( hCPE, ivas_total_brate );
884 : }
885 :
886 150010 : st_ivas->BER_detect = s_or( st_ivas->BER_detect, sts[0]->BER_detect );
887 150010 : st_ivas->BER_detect = s_or( st_ivas->BER_detect, sts[1]->BER_detect );
888 150010 : move16();
889 150010 : move16();
890 :
891 :
892 150010 : pop_wmops();
893 150010 : return error;
894 : }
895 :
896 : #ifdef FIX_1320_STACK_CPE_DECODER
897 :
898 : /*-------------------------------------------------------------------------
899 : * stereo_dft_dec_main()
900 : *
901 : * DFT decoder main function
902 : *-------------------------------------------------------------------------*/
903 :
904 42559 : static ivas_error stereo_dft_dec_main(
905 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
906 : const Word32 ivas_total_brate, /* i : IVAS total bitrate */
907 : const Word16 n_channels, /* i : number of channels to be decoded */
908 : Word32 *p_res_buf_fx, /* i : DFT stereo residual S signal */
909 : Word32 *output[], /* o : output synthesis signal */
910 : Word32 outputHB_fx[][L_FRAME48k], /* o : output HB synthesis signal */
911 : const Word16 output_frame, /* i : output frame length per channel */
912 : const Word32 output_Fs /* i : output sampling rate */
913 : )
914 : {
915 : Word32 DFT_fx[CPE_CHANNELS][STEREO_DFT_BUF_MAX];
916 : Word32 res_buf_fx[STEREO_DFT_N_8k]; /* Q(q_res_buf) */
917 : Word16 n;
918 : Word16 q_res_buf, q_dft;
919 : Decoder_State *st0;
920 : ivas_error error;
921 : Word16 shift;
922 : Word32 tmp1, tmp2;
923 : Word16 shift1, shift2;
924 :
925 42559 : st0 = hCPE->hCoreCoder[0];
926 :
927 42559 : q_res_buf = Q8;
928 42559 : move16();
929 :
930 42559 : set32_fx( DFT_fx[0], 0, STEREO_DFT_BUF_MAX );
931 42559 : set32_fx( DFT_fx[1], 0, STEREO_DFT_BUF_MAX );
932 :
933 : /* copy from temporary buffer */
934 42559 : test();
935 42559 : IF( hCPE->hStereoDft->res_cod_band_max > 0 && !st0->bfi )
936 : {
937 16737 : Copy32( p_res_buf_fx, res_buf_fx, STEREO_DFT_N_8k );
938 : }
939 :
940 : /* core decoder */
941 42559 : IF( NE_32( ( error = ivas_core_dec_fx( NULL, NULL, hCPE, NULL, n_channels, output, outputHB_fx, DFT_fx, 0 ) ), IVAS_ERR_OK ) )
942 : {
943 0 : return error;
944 : }
945 :
946 : /* Scaling of DFT's */
947 42559 : maximum_abs_32_fx( DFT_fx[0], STEREO_DFT_BUF_MAX, &tmp1 );
948 42559 : maximum_abs_32_fx( DFT_fx[1], STEREO_DFT_BUF_MAX, &tmp2 );
949 :
950 42559 : IF( tmp1 == 0 )
951 : {
952 5503 : shift1 = Q31;
953 5503 : move16();
954 : }
955 : ELSE
956 : {
957 37056 : shift1 = norm_l( tmp1 );
958 : }
959 42559 : IF( tmp2 == 0 )
960 : {
961 22902 : shift2 = Q31;
962 22902 : move16();
963 : }
964 : ELSE
965 : {
966 19657 : shift2 = norm_l( tmp2 );
967 : }
968 42559 : shift = s_min( shift1, shift2 );
969 :
970 42559 : IF( NE_16( shift, 31 ) )
971 : {
972 37060 : shift = sub( add( hCPE->hStereoDft->q_dft, shift ), Q17 ); /* Q17 for guard bits */
973 :
974 37060 : IF( GT_16( shift, hCPE->hStereoDft->q_dft ) )
975 : {
976 235 : Scale_sig32( DFT_fx[0], STEREO_DFT_BUF_MAX, sub( shift, hCPE->hStereoDft->q_dft ) ); // shift
977 235 : Scale_sig32( DFT_fx[1], STEREO_DFT_BUF_MAX, sub( shift, hCPE->hStereoDft->q_dft ) ); // shift
978 235 : hCPE->hStereoDft->q_dft = shift;
979 235 : move16();
980 : }
981 : }
982 : ELSE
983 : {
984 5499 : hCPE->hStereoDft->q_dft = Q8;
985 5499 : move16();
986 : }
987 :
988 : /* DFT Stereo residual decoding */
989 42559 : test();
990 42559 : IF( hCPE->hStereoDft->res_cod_band_max > 0 && !st0->bfi )
991 : {
992 : Word16 q;
993 : Word16 q_out_DFT[2];
994 :
995 16737 : q = Q11;
996 16737 : move16();
997 :
998 16737 : Copy_Scale_sig_32_16( hCPE->hCoreCoder[0]->old_pitch_buf_fx, hCPE->hCoreCoder[0]->old_pitch_buf_16_fx, ( ( 2 * NB_SUBFR16k ) + 2 ), -( Q10 ) ); // Q16->Q6
999 :
1000 16737 : stereo_dft_dec_res_fx( hCPE, res_buf_fx, q_res_buf, output[1] );
1001 :
1002 16737 : Copy_Scale_sig_16_32_DEPREC( hCPE->hCoreCoder[0]->old_pitch_buf_16_fx, hCPE->hCoreCoder[0]->old_pitch_buf_fx, ( ( 2 * NB_SUBFR16k ) + 2 ), 10 ); // Q6->Q16
1003 :
1004 16737 : Scale_sig32( output[1], L_FRAME8k, ( Q11 - Q15 ) ); // Q15 -> Q11
1005 :
1006 16737 : q_out_DFT[0] = q_out_DFT[1] = hCPE->hStereoDft->q_dft;
1007 16737 : move16();
1008 16737 : move16();
1009 :
1010 16737 : stereo_dft_dec_analyze_fx( hCPE, output[1], DFT_fx, 1, L_FRAME8k, output_frame, DFT_STEREO_DEC_ANA_LB, 0, 0, &q, q_out_DFT );
1011 :
1012 16737 : Scale_sig32( DFT_fx[1], STEREO_DFT_BUF_MAX, sub( hCPE->hStereoDft->q_dft, q_out_DFT[1] ) ); // q_dft
1013 : }
1014 :
1015 : /* DFT stereo CNG */
1016 42559 : q_dft = hCPE->hStereoDft->q_dft;
1017 42559 : move16();
1018 42559 : stereo_dtf_cng_fx( hCPE, ivas_total_brate, DFT_fx, output_frame, q_dft );
1019 :
1020 : /* decoding */
1021 42559 : IF( EQ_16( hCPE->nchan_out, 1 ) )
1022 : {
1023 5347 : IF( EQ_16( hCPE->hStereoDft->first_frame, 1 ) )
1024 : {
1025 21 : hCPE->hStereoDft->q_smoothed_nrg = hCPE->hStereoDft->q_dft;
1026 21 : move16();
1027 105 : FOR( Word16 ii = 0; ii < (Word16) ( sizeof( hCPE->hStereoDft->q_DFT_past_DMX_fx ) / sizeof( hCPE->hStereoDft->q_DFT_past_DMX_fx[0] ) ); ii++ )
1028 : {
1029 84 : hCPE->hStereoDft->q_DFT_past_DMX_fx[ii] = hCPE->hStereoDft->q_dft;
1030 84 : move16();
1031 : }
1032 21 : hCPE->hStereoDft->first_frame = 0;
1033 21 : move16();
1034 : }
1035 :
1036 5347 : scale_sig32( hCPE->hStereoDft->res_cod_mem_fx, STEREO_DFT_OVL_8k, sub( hCPE->hStereoDft->q_dft, hCPE->hStereoDft->q_res_cod_mem_fx ) ); // q_dft
1037 5347 : hCPE->hStereoDft->q_res_cod_mem_fx = hCPE->hStereoDft->q_dft;
1038 5347 : move16();
1039 5347 : stereo_dft_unify_dmx_fx( hCPE->hStereoDft, st0, DFT_fx, hCPE->input_mem_fx[1], hCPE->hStereoCng->prev_sid_nodata );
1040 5347 : scale_sig32( hCPE->hStereoDft->res_cod_mem_fx, STEREO_DFT_OVL_8k, sub( Q15, hCPE->hStereoDft->q_res_cod_mem_fx ) ); // Q15
1041 5347 : hCPE->hStereoDft->q_res_cod_mem_fx = Q15;
1042 5347 : move16();
1043 : }
1044 : ELSE
1045 : {
1046 37212 : IF( EQ_16( hCPE->hStereoDft->first_frame, 1 ) )
1047 : {
1048 461 : hCPE->hStereoDft->q_smoothed_nrg = hCPE->hStereoDft->q_dft;
1049 461 : move16();
1050 2305 : FOR( Word16 ii = 0; ii < (Word16) ( sizeof( hCPE->hStereoDft->q_DFT_past_DMX_fx ) / sizeof( hCPE->hStereoDft->q_DFT_past_DMX_fx[0] ) ); ii++ )
1051 : {
1052 1844 : hCPE->hStereoDft->q_DFT_past_DMX_fx[ii] = hCPE->hStereoDft->q_dft;
1053 1844 : move16();
1054 : }
1055 461 : hCPE->hStereoDft->first_frame = 0;
1056 461 : move16();
1057 : }
1058 :
1059 37212 : scale_sig32( hCPE->hStereoDft->res_cod_mem_fx, STEREO_DFT_OVL_8k, sub( hCPE->hStereoDft->q_dft, hCPE->hStereoDft->q_res_cod_mem_fx ) ); // q_dft
1060 37212 : hCPE->hStereoDft->q_res_cod_mem_fx = hCPE->hStereoDft->q_dft;
1061 37212 : move16();
1062 :
1063 37212 : stereo_dft_dec_fx( hCPE->hStereoDft, st0, DFT_fx, hCPE->input_mem_fx[1], hCPE->hStereoCng, 0, 0, 0, 0, 0, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
1064 37212 : scale_sig32( hCPE->hStereoDft->res_cod_mem_fx, STEREO_DFT_OVL_8k, sub( Q15, hCPE->hStereoDft->q_res_cod_mem_fx ) ); // Q15
1065 37212 : hCPE->hStereoDft->q_res_cod_mem_fx = Q15;
1066 37212 : move16();
1067 : }
1068 :
1069 122330 : FOR( n = 0; n < hCPE->nchan_out; n++ )
1070 : {
1071 79771 : Scale_sig32( output[n], L_FRAME48k, sub( hCPE->hStereoDft->q_dft, Q11 ) ); // q_dft
1072 79771 : scale_sig32( hCPE->output_mem_fx[n], NS2SA_FX2( output_Fs, STEREO_DFT32MS_OVL_NS ), sub( hCPE->hStereoDft->q_dft, Q11 ) ); // q_dft
1073 79771 : hCPE->q_output_mem_fx[n] = hCPE->hStereoDft->q_dft;
1074 79771 : move16();
1075 : }
1076 :
1077 : /* synthesis iFFT */
1078 122330 : FOR( n = 0; n < hCPE->nchan_out; n++ )
1079 : {
1080 79771 : stereo_dft_dec_synthesize_fx( hCPE, DFT_fx, n, output[n], output_frame );
1081 : }
1082 :
1083 122330 : FOR( n = 0; n < hCPE->nchan_out; n++ )
1084 : {
1085 79771 : Scale_sig32( output[n], L_FRAME48k, sub( Q11, hCPE->hStereoDft->q_dft ) ); // Q11
1086 79771 : scale_sig32( hCPE->output_mem_fx[n], NS2SA_FX2( output_Fs, STEREO_DFT32MS_OVL_NS ), sub( Q11, hCPE->hStereoDft->q_dft ) ); // Q11
1087 79771 : hCPE->q_output_mem_fx[n] = Q11;
1088 79771 : move16();
1089 : }
1090 :
1091 42559 : return IVAS_ERR_OK;
1092 : }
1093 :
1094 : #endif
1095 :
1096 : /*-------------------------------------------------------------------------
1097 : * create_cpe_dec_fx()
1098 : *
1099 : * Create, allocate and initialize IVAS decoder CPE handle
1100 : *-------------------------------------------------------------------------*/
1101 :
1102 2935 : ivas_error create_cpe_dec(
1103 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
1104 : const Word16 cpe_id, /* i : CPE # identifier */
1105 : const Word32 element_brate /* i : element bitrate */
1106 : )
1107 : {
1108 : Word16 i, n;
1109 : CPE_DEC_HANDLE hCPE;
1110 : Decoder_State *st;
1111 : Word32 output_Fs;
1112 : ivas_error error;
1113 : Word32 cpe_brate;
1114 :
1115 2935 : error = IVAS_ERR_OK;
1116 2935 : move32();
1117 :
1118 : /*-----------------------------------------------------------------*
1119 : * Allocate CPE handle
1120 : *-----------------------------------------------------------------*/
1121 :
1122 2935 : IF( ( hCPE = (CPE_DEC_HANDLE) malloc( sizeof( CPE_DEC_DATA ) ) ) == NULL )
1123 : {
1124 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CPE\n" ) );
1125 : }
1126 :
1127 : /*-----------------------------------------------------------------*
1128 : * Initialization - general parameters
1129 : *-----------------------------------------------------------------*/
1130 :
1131 2935 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
1132 2935 : move32();
1133 2935 : hCPE->cpe_id = cpe_id;
1134 2935 : move16();
1135 2935 : hCPE->element_brate = element_brate;
1136 2935 : move32();
1137 2935 : hCPE->last_element_brate = hCPE->element_brate;
1138 2935 : move32();
1139 2935 : hCPE->element_mode = st_ivas->element_mode_init;
1140 2935 : move16();
1141 2935 : hCPE->last_element_mode = st_ivas->element_mode_init;
1142 2935 : move16();
1143 :
1144 2935 : hCPE->hStereoDft = NULL;
1145 2935 : hCPE->hStereoDftDmx = NULL;
1146 2935 : hCPE->hStereoTD = NULL;
1147 2935 : hCPE->hStereoMdct = NULL;
1148 2935 : hCPE->hStereoTCA = NULL;
1149 2935 : hCPE->hStereoICBWE = NULL;
1150 2935 : hCPE->hStereoCng = NULL;
1151 :
1152 2935 : hCPE->stereo_switching_counter = 10;
1153 2935 : move16();
1154 2935 : hCPE->NbFrameMod = 7;
1155 2935 : move16();
1156 2935 : hCPE->lt_es_em_fx = 0;
1157 2935 : move32();
1158 :
1159 : /* Note: nchan_out is considered to be related to the structure. This is nchan_out for CPE and for MASA_format is always 2. */
1160 2935 : test();
1161 2935 : test();
1162 2935 : test();
1163 2935 : IF( EQ_16( (Word16) st_ivas->ivas_format, SBA_FORMAT ) || EQ_16( (Word16) st_ivas->ivas_format, MASA_FORMAT ) || EQ_16( (Word16) st_ivas->ivas_format, MASA_ISM_FORMAT ) || EQ_16( (Word16) st_ivas->ivas_format, MC_FORMAT ) )
1164 : {
1165 2093 : hCPE->nchan_out = CPE_CHANNELS;
1166 2093 : move16();
1167 : }
1168 : ELSE
1169 : {
1170 842 : hCPE->nchan_out = s_min( CPE_CHANNELS, st_ivas->hDecoderConfig->nchan_out );
1171 842 : move16();
1172 : }
1173 :
1174 2935 : test();
1175 2935 : test();
1176 2935 : test();
1177 2935 : IF( EQ_16( extract_l( st_ivas->ivas_format ), MASA_ISM_FORMAT ) && ( EQ_16( (Word16) st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) || EQ_16( (Word16) st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) || EQ_16( (Word16) st_ivas->ism_mode, ISM_MASA_MODE_DISC ) ) )
1178 : {
1179 37 : cpe_brate = element_brate;
1180 37 : move32();
1181 : }
1182 : ELSE
1183 : {
1184 2898 : cpe_brate = st_ivas->hDecoderConfig->ivas_total_brate;
1185 2898 : move32();
1186 : }
1187 :
1188 2935 : test();
1189 2935 : test();
1190 2935 : test();
1191 2935 : test();
1192 5851 : if ( ( ( EQ_16( (Word16) st_ivas->ivas_format, MASA_FORMAT ) && LT_32( st_ivas->hDecoderConfig->ivas_total_brate, MASA_STEREO_MIN_BITRATE ) ) ||
1193 2981 : ( EQ_16( (Word16) st_ivas->ivas_format, MASA_ISM_FORMAT ) && LT_32( cpe_brate, MASA_STEREO_MIN_BITRATE ) ) ) &&
1194 28 : GT_32( st_ivas->hDecoderConfig->ivas_total_brate, IVAS_SID_5k2 ) )
1195 : {
1196 28 : hCPE->nchan_out = 1;
1197 28 : move16();
1198 : }
1199 :
1200 8805 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1201 : {
1202 5870 : set32_fx( hCPE->prev_hb_synth_fx[n], 0, NS2SA_FX2( output_Fs, L_sub( IVAS_DEC_DELAY_NS, DELAY_BWE_TOTAL_NS ) ) );
1203 5870 : set32_fx( hCPE->prev_synth_fx[n], 0, NS2SA_FX2( output_Fs, L_sub( IVAS_DEC_DELAY_NS, STEREO_DFT32MS_OVL_NS ) ) );
1204 : }
1205 2935 : hCPE->q_prev_synth_fx = 0;
1206 2935 : move16();
1207 2935 : hCPE->brate_surplus = 0;
1208 2935 : move32();
1209 :
1210 : /*-----------------------------------------------------------------*
1211 : * DFT stereo I/O Buffers: allocate and initialize
1212 : *-----------------------------------------------------------------*/
1213 :
1214 8805 : FOR( i = 0; i < CPE_CHANNELS; i++ )
1215 : {
1216 5870 : test();
1217 5870 : test();
1218 5870 : test();
1219 5870 : test();
1220 5870 : test();
1221 5870 : IF( EQ_16( (Word16) st_ivas->ivas_format, STEREO_FORMAT ) || EQ_16( (Word16) st_ivas->ivas_format, MASA_FORMAT ) || EQ_16( (Word16) st_ivas->ivas_format, MASA_ISM_FORMAT ) ||
1222 : ( EQ_16( (Word16) st_ivas->ivas_format, MC_FORMAT ) && EQ_16( (Word16) st_ivas->mc_mode, MC_MODE_MCMASA ) ) || st_ivas->sba_dirac_stereo_flag )
1223 : {
1224 786 : IF( ( hCPE->input_mem_fx[i] = (Word32 *) malloc( sizeof( Word32 ) * NS2SA_FX2( output_Fs, STEREO_DFT32MS_OVL_NS ) ) ) == NULL )
1225 : {
1226 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
1227 : }
1228 786 : set32_fx( hCPE->input_mem_fx[i], 0, NS2SA_FX2( output_Fs, STEREO_DFT32MS_OVL_NS ) );
1229 :
1230 786 : IF( ( hCPE->input_mem_LB_fx[i] = (Word32 *) malloc( sizeof( Word32 ) * STEREO_DFT32MS_OVL_16k ) ) == NULL )
1231 : {
1232 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
1233 : }
1234 786 : set32_fx( hCPE->input_mem_LB_fx[i], 0, STEREO_DFT32MS_OVL_16k );
1235 :
1236 786 : IF( i == 0 )
1237 : {
1238 393 : IF( ( hCPE->input_mem_BPF_fx[0] = (Word32 *) malloc( sizeof( Word32 ) * STEREO_DFT32MS_OVL_16k ) ) == NULL )
1239 : {
1240 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
1241 : }
1242 393 : set32_fx( hCPE->input_mem_BPF_fx[0], 0, STEREO_DFT32MS_OVL_16k );
1243 : }
1244 :
1245 786 : IF( ( hCPE->output_mem_fx[i] = (Word32 *) malloc( sizeof( Word32 ) * NS2SA_FX2( output_Fs, STEREO_DFT32MS_OVL_NS ) ) ) == NULL )
1246 : {
1247 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
1248 : }
1249 786 : set32_fx( hCPE->output_mem_fx[i], 0, NS2SA_FX2( output_Fs, STEREO_DFT32MS_OVL_NS ) );
1250 786 : hCPE->q_output_mem_fx[i] = Q11;
1251 786 : move16();
1252 786 : IF( LT_16( i, hCPE->nchan_out ) )
1253 : {
1254 736 : IF( ( hCPE->prev_synth_chs_fx[i] = (Word32 *) malloc( sizeof( Word32 ) * NS2SA_FX2( output_Fs, FRAME_SIZE_NS ) ) ) == NULL )
1255 : {
1256 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
1257 : }
1258 736 : set32_fx( hCPE->prev_synth_chs_fx[i], 0, NS2SA_FX2( output_Fs, FRAME_SIZE_NS ) );
1259 : }
1260 : ELSE
1261 : {
1262 50 : hCPE->prev_synth_chs_fx[i] = NULL;
1263 : }
1264 : }
1265 : ELSE
1266 : {
1267 5084 : hCPE->input_mem_fx[i] = NULL;
1268 5084 : hCPE->input_mem_LB_fx[i] = NULL;
1269 5084 : if ( i == 0 )
1270 : {
1271 2542 : hCPE->input_mem_BPF_fx[0] = NULL;
1272 : }
1273 5084 : hCPE->output_mem_fx[i] = NULL;
1274 5084 : hCPE->q_output_mem_fx[i] = 0;
1275 5084 : move16();
1276 5084 : hCPE->prev_synth_chs_fx[i] = NULL;
1277 : }
1278 : }
1279 :
1280 : /*-----------------------------------------------------------------*
1281 : * CoreCoder, 2 instances: allocate and initialize
1282 : *-----------------------------------------------------------------*/
1283 :
1284 8731 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1285 : {
1286 5833 : IF( st_ivas->sba_dirac_stereo_flag && EQ_16( st_ivas->nchan_transport, 1 ) )
1287 : {
1288 : /* for SBA DirAC stereo output CPE element is only used for upmix, core coder is found in SCE element used for core decoding */
1289 37 : BREAK;
1290 : }
1291 :
1292 5796 : IF( ( st = (DEC_CORE_HANDLE) malloc( sizeof( Decoder_State ) ) ) == NULL )
1293 : {
1294 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
1295 : }
1296 :
1297 5796 : copy_decoder_config( st_ivas, st );
1298 :
1299 : // st->total_brate = hCPE->element_brate / ( CPE_CHANNELS );
1300 5796 : st->total_brate = L_shr( hCPE->element_brate, 1 ); /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
1301 5796 : move32();
1302 5796 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
1303 5796 : move32();
1304 5796 : st->is_ism_format = 0;
1305 5796 : move16();
1306 :
1307 5796 : IF( NE_32( ( error = init_decoder_ivas_fx( st, n, st_ivas->mc_mode ) ), IVAS_ERR_OK ) )
1308 : {
1309 0 : return error;
1310 : }
1311 :
1312 5796 : hCPE->hCoreCoder[n] = st;
1313 : }
1314 :
1315 : /*-----------------------------------------------------------------*
1316 : * DFT stereo initialization
1317 : *-----------------------------------------------------------------*/
1318 :
1319 :
1320 2935 : test();
1321 2935 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) || ( st_ivas->sba_dirac_stereo_flag && hCPE->cpe_id == 0 ) )
1322 : {
1323 195 : IF( NE_32( ( error = stereo_dft_dec_create_fx( &( hCPE->hStereoDft ), hCPE->element_brate, output_Fs, st_ivas->sba_dirac_stereo_flag, st_ivas->nchan_transport ) ), IVAS_ERR_OK ) )
1324 : {
1325 0 : return error;
1326 : }
1327 : }
1328 :
1329 : /*-----------------------------------------------------------------*
1330 : * DFT stereo mono DMX initialization
1331 : *-----------------------------------------------------------------*/
1332 :
1333 2935 : test();
1334 2935 : IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) && EQ_16( hCPE->nchan_out, 1 ) )
1335 : {
1336 45 : IF( ( hCPE->hStereoDftDmx = (STEREO_DFT_DMX_DATA_HANDLE) malloc( sizeof( STEREO_DFT_DMX_DATA ) ) ) == NULL )
1337 : {
1338 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo DFT mono output\n" ) );
1339 : }
1340 :
1341 45 : stereo_dft_dmx_out_reset_fx( hCPE->hStereoDftDmx );
1342 : }
1343 :
1344 : /*-----------------------------------------------------------------*
1345 : * Temporal inter-channel alignment initialization
1346 : *-----------------------------------------------------------------*/
1347 :
1348 2935 : test();
1349 2935 : test();
1350 2935 : test();
1351 2935 : IF( ( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) || ( EQ_16( (Word16) st_ivas->ivas_format, STEREO_FORMAT ) && LE_32( hCPE->element_brate, MAX_MDCT_ITD_BRATE ) ) ) && NE_16( hCPE->nchan_out, 1 ) )
1352 : {
1353 100 : IF( ( hCPE->hStereoTCA = (STEREO_TCA_DEC_HANDLE) malloc( sizeof( STEREO_TCA_DEC_DATA ) ) ) == NULL )
1354 : {
1355 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
1356 : }
1357 :
1358 100 : stereo_tca_init_dec_fx( hCPE->hStereoTCA );
1359 : }
1360 :
1361 : /*-----------------------------------------------------------------*
1362 : * Stereo IC BWE initialization
1363 : *-----------------------------------------------------------------*/
1364 :
1365 2935 : test();
1366 2935 : test();
1367 2935 : IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) && !( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->nchan_out, 1 ) ) )
1368 : {
1369 91 : IF( ( hCPE->hStereoICBWE = (STEREO_ICBWE_DEC_HANDLE) malloc( sizeof( STEREO_ICBWE_DEC_DATA ) ) ) == NULL )
1370 : {
1371 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE\n" ) );
1372 : }
1373 :
1374 91 : stereo_icBWE_init_dec_fx( hCPE->hStereoICBWE );
1375 : }
1376 :
1377 : /*-----------------------------------------------------------------*
1378 : * TD stereo initialization
1379 : *-----------------------------------------------------------------*/
1380 :
1381 2935 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
1382 : {
1383 0 : IF( ( hCPE->hStereoTD = (STEREO_TD_DEC_DATA_HANDLE) malloc( sizeof( STEREO_TD_DEC_DATA ) ) ) == NULL )
1384 : {
1385 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD Stereo\n" ) );
1386 : }
1387 :
1388 0 : stereo_td_init_dec_fx( hCPE->hStereoTD, hCPE->last_element_mode );
1389 : }
1390 :
1391 : /*-----------------------------------------------------------------*
1392 : * MDCT stereo initialization
1393 : *-----------------------------------------------------------------*/
1394 :
1395 2935 : test();
1396 2935 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && EQ_16( st_ivas->nCPE, 1 ) )
1397 : {
1398 381 : IF( ( hCPE->hStereoMdct = (STEREO_MDCT_DEC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_DEC_DATA ) ) ) == NULL )
1399 : {
1400 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo MDCT\n" ) );
1401 : }
1402 :
1403 381 : test();
1404 381 : IF( EQ_32( st_ivas->ivas_format, STEREO_FORMAT ) && LE_32( hCPE->element_brate, MAX_MDCT_ITD_BRATE ) )
1405 : {
1406 13 : hCPE->hStereoMdct->use_itd = 1;
1407 13 : move16();
1408 : }
1409 : ELSE
1410 : {
1411 368 : hCPE->hStereoMdct->use_itd = 0;
1412 368 : move16();
1413 : }
1414 :
1415 381 : hCPE->hStereoMdct->reverse_dmx = 0;
1416 381 : move16();
1417 381 : hCPE->hStereoMdct->smooth_ratio_fx = ONE_IN_Q26;
1418 381 : move32();
1419 381 : set16_fx( hCPE->hStereoMdct->prev_ms_mask[0], 0, MAX_SFB );
1420 381 : set16_fx( hCPE->hStereoMdct->prev_ms_mask[1], 0, MAX_SFB );
1421 381 : hCPE->hStereoMdct->lastCoh_fx = ONE_IN_Q14;
1422 381 : move16();
1423 381 : hCPE->hStereoMdct->mdct_stereo_mode[0] = SMDCT_DUAL_MONO;
1424 381 : move16();
1425 381 : hCPE->hStereoMdct->mdct_stereo_mode[1] = SMDCT_DUAL_MONO;
1426 381 : move16();
1427 381 : hCPE->hStereoMdct->IGFStereoMode[0] = -1;
1428 381 : move16();
1429 381 : hCPE->hStereoMdct->IGFStereoMode[1] = -1;
1430 381 : move16();
1431 : }
1432 :
1433 : /*-----------------------------------------------------------------*
1434 : * Stereo CNG initialization
1435 : *-----------------------------------------------------------------*/
1436 :
1437 2935 : test();
1438 2935 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) || EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
1439 : {
1440 99 : IF( ( hCPE->hStereoCng = (STEREO_CNG_DEC_HANDLE) malloc( sizeof( STEREO_CNG_DEC ) ) ) == NULL )
1441 : {
1442 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo CNG\n" ) );
1443 : }
1444 99 : stereo_cng_init_dec_fx( hCPE->hStereoCng, &hCPE->hCoreCoder[0]->hFdCngDec->hFdCngCom->frameSize );
1445 : }
1446 :
1447 2935 : st_ivas->hCPE[cpe_id] = hCPE;
1448 :
1449 2935 : return error;
1450 : }
1451 :
1452 :
1453 : /*-------------------------------------------------------------------------
1454 : * destroy_cpe_dec_fx()
1455 : *
1456 : * Destroy and deallocate IVAS decoder CPE handle
1457 : *-------------------------------------------------------------------------*/
1458 :
1459 2935 : void destroy_cpe_dec(
1460 : CPE_DEC_HANDLE hCPE /* i/o: CPE decoder structure */
1461 : )
1462 : {
1463 : Word16 n;
1464 : Decoder_State *st;
1465 :
1466 8805 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1467 : {
1468 5870 : st = hCPE->hCoreCoder[n];
1469 :
1470 5870 : IF( st != NULL )
1471 : {
1472 5692 : destroy_core_dec_fx( st );
1473 :
1474 5692 : free( st );
1475 5692 : st = NULL;
1476 : }
1477 : }
1478 :
1479 2935 : IF( hCPE->hStereoDft != NULL )
1480 : {
1481 177 : stereo_dft_dec_destroy_fx( &( hCPE->hStereoDft ) );
1482 177 : hCPE->hStereoDft = NULL;
1483 : }
1484 :
1485 2935 : IF( hCPE->hStereoDftDmx != NULL )
1486 : {
1487 32 : free( hCPE->hStereoDftDmx );
1488 32 : hCPE->hStereoDftDmx = NULL;
1489 : }
1490 :
1491 2935 : IF( hCPE->hStereoTD != NULL )
1492 : {
1493 1 : free( hCPE->hStereoTD );
1494 1 : hCPE->hStereoTD = NULL;
1495 : }
1496 :
1497 2935 : IF( hCPE->hStereoMdct != NULL )
1498 : {
1499 411 : free( hCPE->hStereoMdct );
1500 411 : hCPE->hStereoMdct = NULL;
1501 : }
1502 :
1503 2935 : IF( hCPE->hStereoTCA != NULL )
1504 : {
1505 98 : free( hCPE->hStereoTCA );
1506 98 : hCPE->hStereoTCA = NULL;
1507 : }
1508 :
1509 2935 : IF( hCPE->hStereoICBWE != NULL )
1510 : {
1511 104 : free( hCPE->hStereoICBWE );
1512 104 : hCPE->hStereoICBWE = NULL;
1513 : }
1514 :
1515 2935 : IF( hCPE->input_mem_LB_fx[0] != NULL )
1516 : {
1517 1179 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1518 : {
1519 786 : free( hCPE->input_mem_LB_fx[n] );
1520 786 : hCPE->input_mem_LB_fx[n] = NULL;
1521 786 : free( hCPE->input_mem_fx[n] );
1522 786 : hCPE->input_mem_fx[n] = NULL;
1523 786 : free( hCPE->output_mem_fx[n] );
1524 786 : hCPE->output_mem_fx[n] = NULL;
1525 :
1526 786 : IF( hCPE->prev_synth_chs_fx[n] != NULL )
1527 : {
1528 748 : free( hCPE->prev_synth_chs_fx[n] );
1529 748 : hCPE->prev_synth_chs_fx[n] = NULL;
1530 : }
1531 : }
1532 393 : free( hCPE->input_mem_BPF_fx[0] );
1533 393 : hCPE->input_mem_BPF_fx[0] = NULL;
1534 : }
1535 :
1536 2935 : IF( hCPE->hStereoCng != NULL )
1537 : {
1538 82 : free( hCPE->hStereoCng );
1539 82 : hCPE->hStereoCng = NULL;
1540 : }
1541 :
1542 2935 : free( hCPE );
1543 :
1544 2935 : return;
1545 : }
1546 :
1547 :
1548 : /*-------------------------------------------------------------------------
1549 : * read_stereo_mode_and_bwidth_fx()
1550 : *
1551 : * Read stereo technology info & audio bandwidth
1552 : *-------------------------------------------------------------------------*/
1553 :
1554 421974 : static void read_stereo_mode_and_bwidth_fx(
1555 : CPE_DEC_HANDLE hCPE, /* i/o: CPE handle */
1556 : const Decoder_Struct *st_ivas /* i : decoder main structure */
1557 : )
1558 : {
1559 : Decoder_State **sts;
1560 :
1561 : /*-----------------------------------------------------------------*
1562 : * BFI or NO_DATA frame: Use stereo parameters from last (active) frame
1563 : *-----------------------------------------------------------------*/
1564 :
1565 421974 : test();
1566 421974 : if ( st_ivas->bfi || LT_32( st_ivas->hDecoderConfig->ivas_total_brate, IVAS_SID_5k2 ) )
1567 : {
1568 19997 : hCPE->element_mode = hCPE->last_element_mode;
1569 19997 : move16();
1570 : }
1571 :
1572 : /*-----------------------------------------------------------------*
1573 : * SID frame: get element mode from SID side info
1574 : *-----------------------------------------------------------------*/
1575 :
1576 401977 : ELSE IF( EQ_32( st_ivas->hDecoderConfig->ivas_total_brate, IVAS_SID_5k2 ) )
1577 : {
1578 1889 : SWITCH( st_ivas->sid_format )
1579 : {
1580 1369 : case SID_DFT_STEREO:
1581 1369 : hCPE->element_mode = IVAS_CPE_DFT;
1582 1369 : move16();
1583 : /* Read CNG type */
1584 1369 : hCPE->hCoreCoder[0]->cng_type = get_next_indice_fx( hCPE->hCoreCoder[0], 1 );
1585 :
1586 : /* Read BW information in SID */
1587 1369 : hCPE->hCoreCoder[0]->bwidth = get_next_indice_fx( hCPE->hCoreCoder[0], 2 );
1588 1369 : move16();
1589 1369 : move16();
1590 :
1591 1369 : BREAK;
1592 446 : case SID_MDCT_STEREO:
1593 : /* 2TC SBA DTX also uses MDCT-Stereo DTX */
1594 : case SID_SBA_2TC:
1595 446 : hCPE->element_mode = IVAS_CPE_MDCT;
1596 446 : move16();
1597 446 : BREAK;
1598 0 : case SID_SBA_1TC:
1599 0 : assert( !"Forbidden value for SID format in CPE (SBA 1TC), should have already been adressed earlier" );
1600 : BREAK;
1601 0 : case SID_MASA_1TC:
1602 0 : hCPE->element_mode = IVAS_SCE;
1603 0 : move16();
1604 0 : BREAK;
1605 74 : case SID_MASA_2TC:
1606 : /* 2TC MASA DTX uses MDCT or DFT based core */
1607 74 : if ( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
1608 : {
1609 0 : hCPE->element_mode = IVAS_CPE_DFT;
1610 0 : move16();
1611 : }
1612 74 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
1613 : {
1614 : /* Read CNG type */
1615 74 : hCPE->hCoreCoder[0]->cng_type = get_next_indice_fx( hCPE->hCoreCoder[0], 1 );
1616 :
1617 : /* Read BW information in SID */
1618 74 : hCPE->hCoreCoder[0]->bwidth = get_next_indice_fx( hCPE->hCoreCoder[0], 2 );
1619 74 : move16();
1620 74 : move16();
1621 : }
1622 74 : BREAK;
1623 0 : default:
1624 : /* this is what has been done for all modes previously, may need adaptation in the future */
1625 0 : hCPE->element_mode = hCPE->last_element_mode;
1626 0 : move16();
1627 0 : BREAK;
1628 : }
1629 : }
1630 :
1631 : /*-----------------------------------------------------------------*
1632 : * active frame: read element mode and audio bandwidth info
1633 : *-----------------------------------------------------------------*/
1634 :
1635 : ELSE
1636 : {
1637 400088 : sts = hCPE->hCoreCoder;
1638 400088 : test();
1639 400088 : IF( st_ivas->hMCT && hCPE->cpe_id != 0 )
1640 : {
1641 167699 : sts[0]->bwidth = st_ivas->hCPE[0]->hCoreCoder[0]->bwidth;
1642 167699 : move16();
1643 167699 : sts[1]->bwidth = st_ivas->hCPE[0]->hCoreCoder[0]->bwidth;
1644 167699 : move16();
1645 : }
1646 : ELSE
1647 : {
1648 : /* read stereo technology info */
1649 232389 : test();
1650 232389 : IF( LT_32( hCPE->element_brate, MIN_BRATE_MDCT_STEREO ) && st_ivas->hMCT == NULL )
1651 : {
1652 51554 : hCPE->element_mode = add( get_next_indice_fx( sts[0], NBITS_ELEMENT_MODE ), IVAS_CPE_DFT );
1653 51554 : move16();
1654 : }
1655 : ELSE
1656 : {
1657 180835 : hCPE->element_mode = IVAS_CPE_MDCT;
1658 180835 : move16();
1659 : }
1660 :
1661 : /* read the bandwidth */
1662 232389 : IF( LT_32( hCPE->element_brate, MIN_BRATE_FB_STEREO ) )
1663 : {
1664 : /* WB and SWB are supported */
1665 31842 : sts[0]->bwidth = add( get_next_indice_fx( sts[0], 1 ), WB );
1666 31842 : move16();
1667 31842 : sts[1]->bwidth = sts[0]->bwidth;
1668 31842 : move16();
1669 : }
1670 : ELSE
1671 : {
1672 : /* WB, SWB and FB are supported */
1673 200547 : sts[0]->bwidth = get_next_indice_fx( sts[0], NBITS_BWIDTH );
1674 200547 : move16();
1675 200547 : sts[1]->bwidth = sts[0]->bwidth;
1676 200547 : move16();
1677 : }
1678 : }
1679 : }
1680 :
1681 421974 : return;
1682 : }
1683 :
1684 :
1685 : /*-------------------------------------------------------------------------
1686 : * stereo_mode_combined_format_dec_fx()
1687 : *
1688 : * Set stereo format in a combined format
1689 : *-------------------------------------------------------------------------*/
1690 :
1691 421974 : static void stereo_mode_combined_format_dec_fx(
1692 : const Decoder_Struct *st_ivas, /* i : decoder main structure */
1693 : CPE_DEC_HANDLE hCPE /* i/o: CPE handle */
1694 : )
1695 : {
1696 : Word32 element_brate_ref;
1697 :
1698 421974 : IF( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
1699 : {
1700 7084 : element_brate_ref = hCPE->element_brate;
1701 7084 : move32();
1702 :
1703 7084 : test();
1704 7084 : test();
1705 7084 : test();
1706 7084 : test();
1707 7084 : IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) &&
1708 : ( ( EQ_16( st_ivas->nchan_ism, 3 ) && EQ_32( st_ivas->hDecoderConfig->ivas_total_brate, IVAS_96k ) ) ||
1709 : ( EQ_16( st_ivas->nchan_ism, 4 ) && EQ_32( st_ivas->hDecoderConfig->ivas_total_brate, IVAS_128k ) ) ) )
1710 : {
1711 426 : IF( !st_ivas->bfi )
1712 : {
1713 : /* read OMASA stereo mode signalling */
1714 424 : IF( get_next_indice_fx( hCPE->hCoreCoder[0], NBITS_ELEMENT_MODE ) )
1715 : {
1716 0 : hCPE->element_mode = IVAS_CPE_MDCT;
1717 0 : move16();
1718 : }
1719 : ELSE
1720 : {
1721 424 : hCPE->element_mode = IVAS_CPE_DFT;
1722 424 : move16();
1723 : }
1724 : }
1725 :
1726 426 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1727 : {
1728 0 : hCPE->element_brate = IVAS_64k;
1729 0 : move32();
1730 0 : hCPE->brate_surplus = L_sub( hCPE->brate_surplus, L_sub( hCPE->element_brate, element_brate_ref ) );
1731 0 : move32();
1732 : }
1733 : }
1734 : }
1735 :
1736 421974 : return;
1737 : }
|